Welcome to TheCredence.com - You may like to subscribe to our RSS feed to stay updated.
wxWidgets is an open source cross-platform C++ programming toolkit that includes many features. It is most often used for developing applications with a GUI, but also provides features for networking, file access, and more. With versions available for Windows, Linux, Mac, and even some embedded devices, cross platform programming becomes much easier.
I am myself very new to wxWidgets but still have penned down a Beginners Tutorial on wxWidgets -
During this tutorial, a very simple text editor will be constructed that can load and save text files. The tutorial will focus on the application object, the main window, and the event table.
The Application
All wxWidgets applications are derived from wxApp, and simply need to override a single member, wxApp::OnInit, and create a window. As long as the window is open, the application is as well. The following code illustrates the code needed.
#include “wx/wx.h”class MyApplication : public wxApp { public: bool OnInit();</code> };bool MyApplication::OnInit(){ // TODO: Show window return false; }IMPLEMENT_APP(MyApplication);
The first line includes most of the header files needed for a wxWidgets application. Then a new class is derived from wxApp called MyApplication, and the MyApplication::OnInit() function is created. Finally, IMPLEMENT_APP(MyApplication), is needed to cause the application to run. This macro expands to the correct code for any given platform to actually start the application.
The Frame in wxWidgets
After creating the application, the window frame needs to be created and controls need to be created on it. The frame object is derived from wxFrame and, as shown below, includes several functions and the text control.
class MyFrame : public wxFrame { public: MyFrame(); void OnNew(wxCommandEvent& evt); void OnOpen(wxCommandEvent& evt); void OnSave(wxCommandEvent& evt);private: void CreateWidgets(); wxTextCtrl* m_text; }; MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, wxT("Sample Program")) { CreateWidgets(); } void MyFrame::OnNew(wxCommandEvent& WXUNUSED(evt)) { m_text->Clear(); } void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(evt)) { wxString file = ::wxFileSelector(wxT("Open file")); if(file.IsEmpty() == false) m_text->LoadFile(file); } void MyFrame::OnSave(wxCommandEvent& WXUNUSED(evt)) { wxString file = ::wxFileSelector(wxT("Save file")); if(file.IsEmpty() == false) m_text->SaveFile(file); } void MyFrame::CreateWidgets() { // Create a text control m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_TAB | wxTE_PROCESS_ENTER | wxTE_MULTILINE); // Create file menu wxMenu* fileMenu = new wxMenu(); fileMenu->Append(wxID_NEW, wxT("New File")); fileMenu->Append(wxID_OPEN, wxT("Open File")); fileMenu->Append(wxID_SAVE, wxT("Save File")); // Create menu bar wxMenuBar* menuBar = new wxMenuBar(); menuBar->Append(fileMenu, wxT("File")); // Set menu SetMenuBar(menuBar); }
All of that code creates the window. The constructor MyFrame::MyFrame calls the parent constructor wxFrame::wxFrame to create the frame and give it a name, and then it calls CreateWidgets, to create the contents of the frame. The MyFrame::CreateWidgets function creates a text control and a menu bar. In addition, function MyFrame::OnNew, MyFrame::OnOpen, and MyFrame::OnSave are implemented to clear, open, or save the text to a file. Also notice that text constants are enclosed in wxT(...). Since wxWidgets can compile in Unicode as well, wrapping text constants like this ensure that they are declare correctly.
The event Table in wxWidgets
The event table is used to route events from controls, menus, and anything else to the correct functions to handle the events. Without them, this application would not be able to load or save the text. The following changes are needed to implement the event table.
class MyFrame : public wxFrame { ... ...DECLARE_EVENT_TABLE(); }; BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_NEW, MyFrame::OnNew) EVT_MENU(wxID_OPEN, MyFrame::OnOpen) EVT_MENU(wxID_SAVE, MyFrame::OnSave) END_EVENT_TABLE()
First, the event table for the frame is declared. Then the table is created, and the event ids wxID_NEW, wxID_OPEN, and wxID_SAVE are used to call the functions MyFrame::OnNew, MyFrame::OnOpen, and MyFrame::OnSave. The events generated by the menus will get processed by the correct functions.
Showing the frame
Now that the frame is complete, a small change is needed to the application. It needs to create the window frame, show it, and set it as the application top window.
bool MyApplication::OnInit() { // Create and show the frame MyFrame* frame = new MyFrame(); frame->Show(); SetTopWindow(frame); return true; }
The complete code
#include "wx/wx.h"// Main window frame class MyFrame : public wxFrame { public: MyFrame(); void OnNew(wxCommandEvent& evt); void OnOpen(wxCommandEvent& evt); void OnSave(wxCommandEvent& evt); private: void CreateWidgets(); wxTextCtrl* m_text; DECLARE_EVENT_TABLE(); }; MyFrame::MyFrame() : wxFrame(NULL, wxID_ANY, wxT("Sample Program")) { CreateWidgets(); } void MyFrame::OnNew(wxCommandEvent& WXUNUSED(evt)) { m_text->Clear(); } void MyFrame::OnOpen(wxCommandEvent& WXUNUSED(evt)) { wxString file = ::wxFileSelector(wxT("Open file")); if(file.IsEmpty() == false) m_text->LoadFile(file); } void MyFrame::OnSave(wxCommandEvent& WXUNUSED(evt)) { wxString file = ::wxFileSelector(wxT("Save file")); if(file.IsEmpty() == false) m_text->SaveFile(file); } void MyFrame::CreateWidgets() { // Create a text control m_text = new wxTextCtrl(this, ID_TEXTCTRL, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_TAB | wxTE_PROCESS_ENTER | wxTE_MULTILINE); // Create file menu wxMenu* fileMenu = new wxMenu(); fileMenu->Append(wxID_NEW, wxT("New File")); fileMenu->Append(wxID_OPEN, wxT("Open File")); fileMenu->Append(wxID_SAVE, wxT("Save File")); // Create menu bar wxMenuBar* menuBar = new wxMenuBar(); menuBar->Append(fileMenu, wxT("File")); // Set menu SetMenuBar(menuBar); } // Event table to route menu events to functions BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(wxID_NEW, MyFrame::OnNew) EVT_MENU(wxID_OPEN, MyFrame::OnOpen) EVT_MENU(wxID_SAVE, MyFrame::OnSave) END_EVENT_TABLE() // Application class MyApplication : public wxApp { public: bool OnInit(); }; bool MyApplication::OnInit() { // Create and show the frame MyFrame* frame = new MyFrame(); frame->Show(); SetTopWindow(frame); return true; } IMPLEMENT_APP(MyApplication);
Normally to write a cross-platform GUI application, separate code would be needed for each supported platform. For Windows, the code would use the Windows API or MFC. For Linux it would probably use GTK or QT, or directly use X11. wxWidgets allows a single code base to compile and work on multiple platforms with little to no changes in the code. In addition it provides a complete set of features for networking, file and database access, and much more. For any programmer, especially a programmer developing for different platforms, wxWidgets is surely a valuable toolkit.
I hope this tutorial on wxWidgets helps you in understanding what they are and how to do programming with them. For more info on them you can always check there Official website. All Non spam comments are as usual welcomed with open hands
I would love to know How did you find the article and your inputs to it.
Links you may find interesting -
