Build cross-platform GUI with wxwidget

Source: Internet
Author: User
Tags wxwidgets

Why is wxWidgets used? The reason is simple. It allows you to quickly and easily compile a GUI that can run across platforms. It allows you to select programming languages at will, and makes your GUI as good as shown in:

Figure 1. Chandler email client

Figure 1 shows Chandler, a calendar and email management program developed based on the open source application. It is written using the wxWidgets toolkit. Although the original version of wxWidgets is implemented in C ++, the creators of Chandler use the wxpython toolkit and python as the package to seamlessly interact with the c ++ library. The wxWidgets toolkit uses native objects as much as possible, which can be expanded by using powerful custom widgets as needed. You can write wxWidgets programs that can run on multiple platforms and use multiple programming languages.

Getting started with wxWidgets

Before you start, I assume that you have downloaded software packages for the platform on the wxWidgets homepage. If not, see the link in the references section and download it. I further assume that you have mastered the commands and settings required to integrate the wxWidgets library with the compiler or the selected integrated development environment (IDE. If you still do not understand it, the references section below will provide links to the required information. After completing the above work, you can focus on programming.

The body of the wxWidgets program includes two main objects: application objects and framework objects. There can be multiple frameworks, and some macros specific to wxWidgets may be required in the code. The following explains how they are combined.

Link to the wxWidgets Library

To link to the wxWidgets library, you must first include it. Put the following line of code at the top of the header file:

#include "wx/wx.h"

The wx/wx. h header file contains all wxWidgets definitions that may be required. If you are very concerned about performance, you can use some ofincludeStatement to replace the file.

Define application classes

Next, you must define the application class. In many simple cases, this class has little effect, but you must have your own class. The wxWidgets application inherits fromwxAppClass. Its definition is simple:

class DemoApp : public wxApp {public:  virtual bool OnInit();}

When the application startsOnInit()Function -- actuallymain()Method.

After defining the application class, put the following macro in the Code:

IMPLEMENT_APP(DemoApp)

You can replace it with the name of your application class.DemoApp. This macro is used to create the real wxWidgetsmain()Method. In addition, it creates an instance of the Application Object and starts the initialization process.

Define framework class

Now, define the framework class, which represents the main window in the application. The xwidgets parent class iswxFrame. Listing 1 provides a simple example.

Listing 1. Example wxframe class

class DemoFrame : public wxFrame {public:   DemoFrame(const wxString& title);   void OnButtonPress(wxCommandEvent& event);private:   DECLARE_EVENT_TABLE()};

To remove unfamiliar names,wxStringIs a string Wrapper class specific to wxWidgets, which is used for string operations throughout the wxWidgets toolkit. The toolkit rejects the use of the standard template library (STL) class to avoid limiting wxWidgets to the platforms available in STL. If necessary, you can use the compile time switch to use STL as the underlying implementation. Similarly,wxCommandEventIt is a parent class of an event. Specifically, it is a command event, which is usually related to user operations (such as clicking a button or selecting from the list. WhileDECLARE_EVENT_TABLEMacros are required for any wxWidgets object that needs to respond to events (which undoubtedly includes the small demo framework in this article.

Define event table

To truly respond to events, you must define the event table in the implementation file. It is another macro. In this example, It is shown in Listing 2.

List 2. Event table macros

BEGIN_EVENT_TABLE(DemoFrame, wxFrame)   EVT_BUTTON(wxID_CLOSE,  DemoFrame::OnButtonPress)END_EVENT_TABLE()

BEGIN_EVENT_TABLE()The macro has two parameters: the class actually targeted by the event table and the middle parent class of the class. As you expected,END_EVENT_TABLEMacro indicates the end of the event table. There can be several specific event Macros in the middle. In this example, there is only one.

The wxWidgets toolkit contains several different event macros, each corresponding to different events. In this example,EVT_BUTTONClick the icon. The first parameter of this macro is the identifier of a specific button being processed.wxID_CLOSEAn identifier is one of several predefined identifiers related to some common features of an application. The predefined identifier is used for convenience, although in some cases, some specific identifiers will trigger special processing of the wxWidgets system. The second parameter is the full qualified name of the method called when a menu event is triggered. You can use an event macro similar to the event macro described here to manage all events in wxWidgets.

Definition Method

Now, you can start to define some methods. Three simple methods are provided here. The first one is shown in listing 3.OnInit()Method.

Listing 3. oninit () method

bool DemoApp::OnInit() {   DemoFrame *frame = new DemoFrame("DeveloperWorks Demo");   frame->Show(true);   return true;}

The first two lines of this method are common at the beginning of the GUI program: they create and display the main window. The third line is very important to the rest of the application. IftrueSends a signal to the rest of the wxWidgets engine indicating that the initialization is successful and the program can continue. Conversely, iffalseThe application stops and exits.

OnInit()Method:DemoFrameReference the constructor. In this method, you can add a button to the framework, as shown in Listing 4.

Listing 4. Add a button to the Framework

DemoFrame::DemoFrame(const wxString& title)      : wxFrame(NULL, wxID_ANY, title) {   wxSizer *sizer = new wxBoxSizer(wxVERTICAL);   this->SetSizer(sizer);   wxButton *button = new wxButton(this, wxID_CLOSE, "Click Me");   sizer->Add(50, 50);   sizer->Add(button, 0, wxALL, 50);}

However, before you can add a button to the framework, you need to createSizer. Sizer in wxWidgets is equivalent to layout manager in Java programming language: they allow you to place objects in a window using predefined rules without setting the size and position for each widget separately. In this examplewxBoxSizerThe widget is arranged in a straight line. A box sizer can be vertical or horizontal. After creating a sizer, useSetSizer()Method to attach it to the framework. Then create a button. The parameters of the button constructor include:

  • Parent widget (frame in this example)
  • An integer ID
  • Tag to be displayed

You do not need to explicitly add the button to the Framework. You only need to identify the framework as the parent container. However, the button must be explicitly added to the sizer so that the Layout Algorithm of the sizer knows about it. This is implemented in the last line of the method, but it must be after a blank 50x50 pixels is added to the top of the row. When a button is added, sizer also uses a 50-pixel border around the button. You can usewxALLFlag and last parameter50.

Define event handlers

Finally, you need to define a simple event handler, as shown in listing 5.

Listing 5. A simple event handler

void DemoFrame::OnButtonPress(wxCommandEvent& event) {   Close(true);}

This is simple. After compilation, we will get a window with a button as shown in 2. Click the button to close the window. In wxWidgets, disabling the last parent framework will automatically exit the application. Therefore, clicking this button will also completely exit the application.

Figure 2. Example window

In this example, we have just been exposed to the functionality that wxWidgets can achieve. For further exploration, see the reference document guide.



Back to Top

Wxpython

WxWidgets is indeed a powerful toolkit, but not everyone is willing to use C ++ destructor, memory management, and so on. Fortunately, a group of excellent programmers have already created a package bound to the wxWidgets library that can be used in other programming languages. Therefore, even if the selected programming tool is not c ++, you can still benefit from the wxWidgets library.

The most mature and comprehensive wxWidgets binding is wxpython. You can use the python programming language to create the wxWidgets program. Download for Microsoft Windows, Mac, and Linux platforms. The user community is large and active. Want to know about it? The Python program shown in Listing 6 can create the same blank window as the one previously created in C ++.

Listing 6. Python blank window Application

#!/usr/bin/env pythonimport wxclass DemoApp(wx.App):   def OnInit(self):      frame = DemoFrame(parent=None, id=-1, title='DeveloperWorks')      frame.Show()      return Trueclass DemoFrame(wx.Frame):   def __init__(self, parent, id, title):      wx.Frame.__init__(self, parent, id, title)      sizer = wx.BoxSizer(wx.VERTICAL)      self.SetSizer(sizer)      button = wx.Button(self, wx.ID_CLOSE, "Click Me")      sizer.Add((50, 50))      sizer.Add(button, 0, wx.ALL, 50)      self.Bind(wx.EVT_BUTTON, self.OnButtonClick)   def OnButtonClick(self, event):      self.Close(True)if __name__ == "__main__":   app = DemoApp()   app.MainLoop()

As you can see, in such a short program, there is almost one-to-one correspondence between C ++ API calls and wxpython calls. In both cases, an application object and a framework object are created. Both useOnInit()Method, and similar constructor and object processing program are defined.

The biggest difference in this example is the binding of events to the handler. C ++ uses the event table macro to manage the binding, while python usesBind()Method. This method takes the python object of the event type and the object actually called when the event is called as the parameter. This structure utilizes the advantages of using python to treat methods as variables and passing them as parameters (passing in the same way as passing strings or integers.

Wxpython is more advantageous than the C ++ wxWidgets toolkit in programs that are longer or more complex. Even if C ++ and Python are not specifically compared as two programming languages, some attractive and outstanding features of the wxpython toolkit are still very attractive. UseBind()The event processing mechanism of the method is easier to integrate into wxpython than wxWidgets. In the Python version, it is easier to dynamically update the handler at runtime. Some complex or composite widgets, such as tree list controls or image radio buttons, are standard components in the wxpython toolkit, but not in C ++. Wxpython also contains the development tool's py package, which makes it easy to add interactive debugging to the wxpython program.



Back to Top

Wxeverythingelse

Python is not the only programming language that can access the wxWidgets library. Although wxpython is the most mature one, if you prefer to use a specific programming language, it is worth learning about the remaining. In this case, let's stay in a few places in wxworld. Please note that the reliability and robustness of these projects are evaluated based on available materials. Many of these projects are based on the love of one or two expert programmers. If you are interested in a project, check it yourself.

Wxperl

Wxperl binding was officially released in June 2006. It has restarted the delivery of daily snapshots, but the available documentation was prepared several years ago. The range of the active email list is generally two or three messages a day. Binary downloads for Win32, Linux, and Mac OS X are available. In addition to the main toolkit, there are some additional toolkit available, including OpenGL packages and packages used to create Mac OS X applications.

The main problem with wxperl is how to translate the wxWidgets API into some heterogeneous variants of Object-Oriented Programming (OOP) in Perl. The code snippets shown in listing 7 are similar to the preceding framework example.

Listing 7. Example of a wxperl window

package MyFrame;use base 'Wx::Frame';use Wx::Event qw(EVT_BUTTON);sub new {   my $class = shift;   my $self = $class->SUPER::new(undef, -1, 'Trying wxPerl',      [-1, -1], [250, 200]);   my $sizer = Wx::BoxSizer->new(wxVERTICAL);   my $button = Wx::Button->new($self, -1, 'Click me!', [-1, -1],      [-1, -1]);   EVT_BUTTON($self, $button, /&OnButtonClick);   $sizer->Add($button);   $self->SetSizer($sizer);   return $self;}sub OnButtonClick {   my($self, $event) = @_;   $self->SetTitle('You Did It');}

This code is basically a line-by-line translation of C ++ and Python code that we have seen earlier. In this example, the wxWidgets library is in the form of a Perl Package and usesEVT_BUTTONFunction call, which looks like the macro definition in C ++.

Wxruby

The current situation of the wxruby project is complex. In the earliest versions, binding to the wxWidgets API was manually created. The latest version of this tool was released in November 2004, but since then, development of the new version has been interrupted, and the new version uses the more powerful simplified wrapper and interface generator (swig) toolkit to generate the binding between Ruby and wxWidgets. The statement on the release time of the new version in its mail list is often "very fast, but may take a few months later ".

Another interesting aspect of wxruby is that, unlike most other wxWidgets bindings, most developers choose to adjust the names of wxWidgets API calls to better comply with Ruby naming conventions (specifically, they need to adoptLower_case_with_underscoresInstead of wxWidgetsUppercasewithcamelcase). Therefore, all the above code examples UseSetSizer()Function, which is called in wxrubyset_sizer(). In addition, most wxruby programs involving the wxWidgets API will be similar to the preceding example.

WxWidgets world

The number of other wxWidgets ports is different. The following is a summary of other wxWidgets:

  • WxbasicIt is both a basic language interpreter and a set bound to wxWidgets. The release of its new version is in preparation (the latest beta version was released on July 15, May 2006 ).
  • WxeuphoriaThe latest version was released in December 2005 and is a binding of euphoria programming language.
  • WxjsIs the Javascript port of wxWidgets. It was tested only in windows and contains an executable file to run JavaScript scripts. Its latest version was released in May 2006. The developer said that the next version will increase support for Linux.
  • WxluaIt is a binding of Lua programming language. It supports cross-platform and memory usage is relatively small. The latest version was released in March 2006.
  • Wx. netBind C # To wxWidgets. Its most recent release date is July 22, July 2005.



Back to Top

Conclusion

WxWidgets can provide a large number of available functions for various programmers. Its basic toolkit is flexible and can handle most of your GUI requirements. Multi-language binding allows most programmers to easily use wxWidgets. A thorough understanding of the wxWidgets toolkit in the selected language will help you build excellent interfaces in your own applications.

References

Learning

  • For more information, see the original article on the developerworks global site.

  • Cross-platform GUI programming with wxWidgetsPrepared by Julian smart, Kevin hock, and Stefan csomor (Prentice Hall, 2005), is an authoritative wxWidgets book.
  • Wxpython in actionWritten by Noel rappin and Robin Dunn (Manning, 2006), it is an authoritative book on wxpython wrapper.
  • Online wxWidgets documentation can be obtained from the wxWidgets documentation webpage. The wxWidgets document is often used as a reference for other ports.
  • Wxwiki has helped me set up my wxWidgets project in Apple's xcode environment.
  • In the developerworks Linux area, you can find more resources required by Linux developers.
  • Stay tuned to developerworks technical events and network broadcasts.

Obtain products and technologies

  • The main wxWidgets Toolkit can be downloaded from the wxWidgets website.

  • You can download wxpython from the wxpython website.
  • You can get more information about wxperl and download wxperl from sourceforge.net.
  • For more information and download wxruby, visit the wxruby wiki.
  • For more information about binding to other programming languages, refer to their respective home pages:
    • Wxbasic
    • Wxeuphoria
    • Wxjs
    • Wxlua
    • Wx. net

  • Order SEK for Linux, two DVDs, including the latest IBM trial software for Linux, from DB2, Lotus, rational, Tivoli, and websphere.
  • Use the IBM trial software to build your next Linux development project, which can be downloaded directly from developerworks.

Discussion

  • WxWidgets supports a large number of wxWidgets community site information on the webpage, including the mail list. There is also a Wiki Server.

  • The wxpython email list can be obtained at the wxpython site.
  • Join the developerworks community by participating in the developerworks blog.

About the author

 

Noel rappin obtained a doctorate from graphics, visualization and usability center at Georgia Institute of Technology and is now a senior software engineer at Motorola, Inc. He stillWxpython in action(Manning publications, March 2006) book andJython Essentials(O'reilly, May 2002) is a co-author of the book.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.