Cover by Francesco Cesarini, Simon Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

A First Example: MicroBlog

The first example we will develop is minimal: a “micro” blogging application. It is stripped down so that all it can do is display an About box, but it will show you the principles of building a wxErlang application. In the next section, you’ll see how to add the blogging functionality.

Our program is given in one file, microblog.erl, which begins like this:[33]

%% A micro-blog, which sets up a frame with menus, and allows an
%% "about" box to be displayed.

-module(microblog).
-compile(export_all).

-include_lib("wx/include/wx.hrl").

-define(ABOUT,?wxID_ABOUT).
-define(EXIT,?wxID_EXIT)

This shows the inclusion of the wx.hrl header file (located in an installation-specific position) that contains definitions of the standard identifiers and types. The local macro definitions of ABOUT and EXIT link these to the standard wx identifiers for these menu items. This will allow the application to handle them in a platform-specific way.

The top-level function for our example is start/0:

%% Top-level function: create the wx-server, the graphical objects,
%% show the application, process and clean up on termination.

start() ->
   wx:new(),
   Frame = wxFrame:new(wx:null(), ?wxID_ANY, "MicroBlog"),
   setup(Frame),
   wxFrame:show(Frame),
   loop(Frame),
   wx:destroy().

This function first creates an instance of the wx-server, and before termination ensures that it is destroyed and memory is reclaimed. The principal graphical object is a wxFrame, created with no parent object (wx:null()

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required