Skip to content

Instantly share code, notes, and snippets.

@rlipscombe
Created November 26, 2017 16:08
Show Gist options
  • Select an option

  • Save rlipscombe/5f400451706efde62acbbd80700a6b7c to your computer and use it in GitHub Desktop.

Select an option

Save rlipscombe/5f400451706efde62acbbd80700a6b7c to your computer and use it in GitHub Desktop.

Revisions

  1. rlipscombe created this gist Nov 26, 2017.
    61 changes: 61 additions & 0 deletions exwx.exs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #!/usr/bin/env elixir

    defmodule Canvas do
    @behaviour :wx_object

    @title "Canvas Example"
    @size {600, 600}

    def start_link() do
    :wx_object.start_link(__MODULE__, [], [])
    end

    def init(args \\ []) do
    wx = :wx.new
    frame = :wxFrame.new(wx, -1, @title, size: @size)
    :wxFrame.connect(frame, :size)
    :wxFrame.connect(frame, :close_window)

    panel = :wxPanel.new(frame, [])
    :wxPanel.connect(panel, :paint, [:callback])

    :wxFrame.show(frame)

    state = %{panel: panel}
    {frame, state}
    end

    def handle_event({:wx, _, _, _, {:wxSize, :size, size, _}}, state = %{panel: panel}) do
    :wxPanel.setSize(panel, size)
    {:noreply, state}
    end

    def handle_event({:wx, _, _, _, {:wxClose, :close_window}}, state) do
    {:stop, :normal, state}
    end

    def handle_sync_event({:wx, _, _, _, {:wxPaint, :paint}}, _, state = %{panel: panel}) do
    brush = :wxBrush.new
    :wxBrush.setColour(brush, {255, 255, 255, 255})

    dc = :wxPaintDC.new(panel)
    :wxDC.setBackground(dc, brush)
    :wxDC.clear(dc)
    :wxPaintDC.destroy(dc)
    :ok
    end
    end

    defmodule Script do
    def main(args) do
    {:wx_ref, _, _, pid} = Canvas.start_link
    ref = Process.monitor(pid)

    receive do
    {:DOWN, ^ref, _, _, _} ->
    :ok
    end
    end
    end

    Script.main(System.argv)