In this chapter, we will cover the basics needed to create wxWidgets applications. We will create our first simple example, show how to display an icon. Next we will create a simple example demonstrating usage of an event. Finally, we will see, how widgets communicate in wxWidgets applications.
First we create the very basic wxWidgets program.
simple.bmx <- Open sourceSuperStrict Framework wx.wxApp Import wx.wxFrame New MyApp.Run() Type MyApp Extends wxApp Method OnInit:Int() Local sim:Simple = Simple(New Simple.Create(Null, wxID_ANY, .. "Simple", -1, -1, 250, 150)) sim.Show(True) Return True End Method End Type Type Simple Extends wxFrame Method OnInit() Centre() End Method End Type
This very basic example shows a small window on the screen. The window is centered.
This method centers the window on the screen. Both horizontally and vertically.
New MyApp.Run() Type MyApp Extends wxApp Method OnInit:Int() ... Return True End Method End Type
The wxApp type is the basic building block of a wxWidgets application. You implement a sub-type of wxApp and override the OnInit() method. Here you can initialize data, create windows, and perform any other startup routines for your application.
You return True from OnInit() to indicate that the application started successfully, in which case wxWidgets takes control and starts processing events. If you return False, the application will terminate.
In this example, we provide an icon for our application. It became a standard to display a small icon in the upper left corner of the window. The icon is a graphical identity of the program.
In our example we show a small web icon.
To display an application icon is a matter of one code line.
wxImage.AddHandler( New wxPNGHandler )
AddHandler tells wxWidgets to enable certain kinds of image formats. In this case, PNG images.
In the following example, we create a button on the frame widget. We will show, how to create a simple event handler.
Local panel:wxPanel = New wxPanel.Create(Self, wxID_ANY)
First we create a wxPanel widget. It will be placed inside a wxFrame widget.
Local button:wxButton = New wxButton.Create(panel, wxID_EXIT, "Quit", 20, 20)
We create a wxButton widget. It is placed on the panel. We use the predefined wxID_EXIT id for the button. It will cause to display a small exit icon on the button (on Linux). The label of the button is "Quit". The button is positioned manually at x=20, y=20 coordinates. The beginning of the coordinate system is at the upper left hand corner.
Connect(wxID_EXIT, wxEVT_COMMAND_BUTTON_CLICKED, OnQuit)
If we click on the button, a wxEVT_COMMAND_BUTTON_CLICKED event is generated. We connect the event to the OnQuit() function of the Button type. So when we click on the button, the OnQuit() function is called.
We set the keyboard focus to the button. So if we press the enter key, the button is being clicked.
Inside the OnQuit() function, we call the Close() method. This will terminate our application.
The event.parent field holds the widget reference which raised the event. This gives us access to the widget methods.
It is important to know, how widgets can communicate in application. Follow the next example.
In our example we have two panels. A left and right panel. The left panel has two buttons. The right panel has one static text. The buttons change the number displayed in the static text. The question is, how do we grab the reference to the static text?
m_parent = wxPanel(GetParent())
Here we save the reference to the parent widget of the LeftPanel. It is a wxPanel widget.
Local comm:Communicate = Communicate(panel.m_parent.GetParent()) comm.m_rp.m_text.SetLabel(panel.count)
These two lines are the most important lines of the example. It is shown, how we get access to the static text widget, which is placed on a different panel. First we get the parent of the both left and right panels. This parent widget has a reference to the right panel. And the right panel has a reference to the static text.