Monday, August 17, 2009

wxErlang in Action -- Listings 1.4 & 1.5

These two listings are the same UI shown in C++ then Python. The wxErlang equivalent is awfully close to the miniblog example in the O'Reilly Erlang Programming book.

-module('helloworld').
-export([start/0]).
-include_lib("wx/include/wx.hrl").
start() ->
_WX = wx:new(),
Frame = makeFrame("Hello World", [{pos, {50,60}}, {size, {450, 340}}]),
wxFrame:connect(Frame, close_window),
wxFrame:show(Frame),
loop(Frame),
wx:destroy().
makeFrame(Title, Options) ->
Frame = wxFrame:new(wx:null(), ?wxID_ANY, Title, Options),
MenuFile = wxMenu:new(),
wxMenu:append(MenuFile, 1, "&About..."),
wxMenu:appendSeparator(MenuFile),
wxMenu:append(MenuFile, 2,"E&xit"),
MenuBar = wxMenuBar:new(),
wxMenuBar:append(MenuBar,MenuFile,"&File"),
wxFrame:setMenuBar(Frame,MenuBar),
wxFrame:createStatusBar(Frame),
wxFrame:setStatusText(Frame,"Welcome to wxErlang"),
wxFrame:connect(Frame, command_menu_selected),
Frame.
loop(Frame) ->
receive
#wx{id=1, event=#wxCommand{}} ->
MD = wxMessageDialog:new(Frame, "This is a wxErlang Hello world sample.",
[{style, ?wxOK bor ?wxICON_INFORMATION},
{caption, "About Hello World"}]),
wxDialog:showModal(MD),
wxDialog:destroy(MD),
loop(Frame);
#wx{id=2, event=#wxCommand{type=command_menu_selected}} ->
wxWindow:close(Frame,[]),
loop(Frame); % Fix -- drain Close event.
#wx{event=#wxClose{type=close_window}} ->
ok % NOT wxWindow:close(Frame,[])
end.
view raw gistfile1.erl hosted with ❤ by GitHub

Still looking for an approach that doesn't end up with all the message handling logic quite so jammed together into one God-function for the message loop, and is vaguely re-usable in more than just structure.

And that ends the first chapter of fairly trivial examples. Things should get more interesting from here on in.

No comments :