[erlang-questions] wxErlang - some answers

Joe Armstrong <>
Tue Jul 4 18:18:34 CEST 2017


I'm trying to get to grips with wxErlang so I toddled over to Ericsson
and sat down for a few hours with Dan Gudmundsson and asked him a boat
load of stupid questions.

I'll be writing a lot more about this and setting up a github project
to document how to get tame wxErlang ...

Here's a first attempt at figuring out how things work.

My usual approach to understanding things is always the same.

"Take a program that works. Then remove non-essential bits until
all that is left is so simple that you can understand it."

How about making some buttons - this should be easy

I started with demo.erl in ${ERL_TOP}/lib/wx/examples/demo/demo.erl

This has some code for making buttons - but it also does *a lot more*
and *it is highly redundant* -- from the point of view of learning the
code volume does not assist comprehension, nor does using a generic behavior.

What do I mean by this?

   - ex_button.erl is written using wx_object.erl
     so to understand ex_button.erl I have to also understand wx_object.erl
   - ex_button.erl has repeated code. For example
     several buttons have tooltips, but I only need to be shown how to do this
     *once* not several times
   - ex_button.erl introduces several new concepts - in particular spacers
     and sizers - and I haven't a clue what they do.

Now I'm going to refactor ex_button.erl so take a deep breath ...

First refactoring: remove the dependency on wx_object

I'd already figured out that a *minimal* wx windows program is:

    start() ->
        Wx = wx_win:new(),
        Frame = wxFrame:new(Wx, -1, "Window Title"),
        wxFrame:show(Frame).

To make the window do something I'd want to write:

    start() ->
        Wx = wx_win:new(),
        Frame = wxFrame:new(Wx, -1, "Window Title"),
        setup_window(Frame),
        wxFrame:show(Frame),
        loop().

    loop() ->
        receive
            Any ->
                io:format("Any=~p~n",[Any])
        end,
        loop().

Given this structure I refactored ex_button.erl into buttons_demo.erl
which removes all the wx_object code. So now I can stare are the code
in buttons_demo.erl and see if I can understand it, for my point of
view it's far easier to understand than ex_buttons.erl since there is
no dependency on wx_object.erl

Second refactoring: Simplify buttons_demo.erl

The original program has four rows of buttons - but one will
be sufficient for me.

I now removed many lines of code, until only one line of buttons
remained.

The resulting code buttons_demo1.erl now only creates one line of buttons
and has a couple of sizers.

Dan told me that the line of code:

   wxStaticBoxSizer:new(?wxHORIZONTAL, Panel,[{label, "wxButton"}]),

Is wxWidgets way of making an hbox - Goodness wxWidgets can be build
using Knuthian hboxes and vboxes (horray).

I'm still not happy with buttons_demo1.erl since it mixes two ideas:

   - making buttons
   - putting buttons in a container and laying out the containers

This needs to be refactored into:

   - a pure button examples
   - a pure layout example

My intention is to try and decompose wxErlang into a large number of
small examples, where each example illustrates one feature of the
system. Also so show how to build complex examples from the small
parts.

I'll make all the code available on github so you can join in the fun
(is this fun?).

Notes:

[1] Feel free to send me examples of wxErlang code - preferably
one example per module that illustrates one feature.

[2] Anybody want to do this for elixir?

[3] The WxWidgets documentation is a terminological mess - they call
*everything* (including a button) a window.

[4] The WxWidget examples (in C++) require subclassing several objects
resulting in multiple files per example - the Erlang equivalent is
nicely contained within a single module (which is far nicer)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ex_button.erl
Type: application/octet-stream
Size: 7368 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170704/e9d512a7/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: buttons_demo.erl
Type: application/octet-stream
Size: 5464 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170704/e9d512a7/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: buttons_demo1.erl
Type: application/octet-stream
Size: 1968 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170704/e9d512a7/attachment-0002.obj>


More information about the erlang-questions mailing list