Code Detail - wxEuphoria

Our sample window as it looks using wxEuphoria:

Sample Application Window with Widgets

The sample code below came straight from Matthew Lewis, the creator and maintainer of wxEuphoria. I've put additional comments in between to help you go with the flow of the program. I'm going to skip the includes. That should be self-explanatory at this point.

The first part of the program is simply defining the widgets. Since wxEuphoria can manage the size and position of controls for you, there are no hard-coded values for those parameters. We use the value -1 for all width, height, x-coord, and y-coord values, so wxEuphoria knows to manage those for us.

constant
    main = create( wxFrame, {0, -1, "Scramble Me", -1, -1, 250, 100}),
    win = create( wxPanel, main ),
    label = create( wxStaticText, {win, -1, "Enter your name:"}),
    name = create( wxTextCtrl, {win, -1, "", -1, -1} ),
    enter = create( wxButton, {win, -1,"Scramble Me"}),
    quit = create( wxButton, {win, -1, "Quit"})

When I said wxEuphoria would manage position and size for us, I didn't mean that we didn't have some say in the matter. In fact, here's what you get if you run the code above without running the setup() procedure:

wxEuphoria App with No Size/Position Definition

What we're going to do is specify relative positions. Let's look at the setup() procedure (I've removed the comments to make it more compact).

Our first call is to create a virtual box within which we place controls. The controls we place in this virtual box will be oriented vertically (the wxVERTICAL parameter).

    vsizer = create( wxBoxSizer, wxVERTICAL )

Now, into the vsizer virtual box, we're placing the label widget we created up top. We've specified that the label widget can grow horizontally, but not vertically and will have a 5-pixel space on its top and left.

    add_window_to_sizer( vsizer, label, 0, wxGROW + wxTOP + wxLEFT, 5 )

Next we add the 'name' textbox to the vsizer virtual box. We've specified that the label widget can grow horizontally and vertically and will have a 5-pixel space on its top, left, and right borders.

    add_window_to_sizer( vsizer, name, 1, wxGROW + wxTOP + wxLEFT + wxRIGHT, 5 )

For the buttons, we'll create another wxBoxSizer widget, but this one will orient its controls horizontally.

    hsizer = create( wxBoxSizer, wxHORIZONTAL )     add_window_to_sizer( hsizer, ScrambleButton, 1, wxLEFT, 5 )     add_window_to_sizer( hsizer, QuitButton, 1, wxLEFT + wxRIGHT, 5 )

Notice that we put the label and textbox above inside the vsizer widget. Now we're going to put the hsizer widget inside there as well. It's a wxBoxSizer widget embedded in another wxBoxSizer. This is perfectly legal, valid, and useful.

    add_sizer_to_sizer( vsizer, hsizer, 0, wxGROW + wxTOP + wxBOTTOM, 5 )

Now that we've built our vsizer widget, we place it on our window ('win').

    set_sizer( win, vsizer )

The controls will all be automatically resized/positioned whenever the window changes size or position. For an example, here's a screenshot of an expanded version of the window. I just dragged the bottom-right corner out a bit.

Sample Application Window with Widgets - Expanded

This may seem insignificant, but there was a time when GUI libraries required you to manage the size and position of the widgets yourself. For newbies, it could be quite a hassle getting this working perfectly.