Source

erl-wxerlang-example / wxerlang_example.erl

Full commit
#!/usr/bin/env escript
%%! -smp enable
-mode(compile).

-include_lib("wx/include/wx.hrl").
-define(MENU_EXIT, 101).
-define(MENU_TUTORIAL, 102).
-define(MENU_ABOUT, 103).

main(_Args) ->
  Wx = wx:new(),
  Xrc = wxXmlResource:get(),
  wxXmlResource:initAllHandlers(Xrc),
  true = wxXmlResource:load(Xrc, "gui.xrc"),
  Frame = wxFrame:new(), 
  wxXmlResource:loadFrame(Xrc, Frame, Wx, "MainFrame"),
  wxFrame:setMinSize(Frame, { 400, 200 }),
  wxFrame:show(Frame),
  initCallbacks(Frame),
  eventLoop(Frame),
  wx:destroy().

initCallbacks(Frame) ->
  wxFrame:connect(Frame, close_window),

  InfileBrowseButton = wxXmlResource:xrcctrl(Frame,
    "InfileBrowseButton", wxButton),
  InfileTextCtrl = wxXmlResource:xrcctrl(Frame,
    "InfileTextCtrl", wxTextCtrl),

  wxButton:connect(InfileBrowseButton, command_button_clicked,
    [{ callback, fun onInfileBrowseButtonClicked/2 },
     { userData, { Frame, InfileTextCtrl } }]),

  wxFrame:connect(Frame, command_menu_selected,
    [{ callback, fun onMenuSelected/2 },
     { userData, Frame }]),
  ok.

onMenuSelected(Event,_) ->
  case Event of
    #wx{ id = ?MENU_EXIT, userData = Frame } ->
      wxFrame:close(Frame);
    #wx{ id = ?MENU_TUTORIAL } ->
      wx_misc:launchDefaultBrowser("http://eax.me/wxerlang/");
    #wx{ id = ?MENU_ABOUT, userData = Frame } ->
      Dialog = wxMessageDialog:new(Frame,
        "wxErlang example\n"
        "(c) Alex Alexeev 2012\n"
        "http://eax.me",
        [
          { caption, "wxErlang example" },
          { style, ?wxOK bor ?wxICON_INFORMATION }
        ]),
      wxDialog:showModal(Dialog),
      wxDialog:destroy(Dialog);
    _ ->
      io:format("onMenuSelected, Event = ~p~n", [Event])
  end.

onInfileBrowseButtonClicked(
    #wx{ userData = { Frame, InfileTextCtrl} },_) ->
  Dialog = wxFileDialog:new(Frame),
  wxDialog:showModal(Dialog),
  FilePath = wxFileDialog:getPath(Dialog),
  wxFileDialog:destroy(Dialog),
  wxTextCtrl:setValue(InfileTextCtrl, FilePath).

eventLoop(Frame) ->
  receive
    #wx{ event = #wxClose{} } ->
      ok;
    Event ->
      io:format("eventLoop: ~p~n", [Event]),
      eventLoop(Frame)
  end.