We have learned about app development for Windows platform in the last few sections. But all our code was hand-written. We also noticed that even for a simple GUI we have to write several lines of code.
So, can we do something about it? How about automatic code generation of GUI codes? Sound interesting! Code::Blocks comes with a plugin, called wxSmith, which can generate C++ code (using wxWidgets toolkit) based on the user generated GUI inside a Visual editor. We'll learn this with another example.
MyNotePad
.MyNotePadFrame
item shown in the following screenshot on the resource tree and then click on the Title property in the property window. Type MyNotePad
in that text box. This will set title of our app to MyNotePad
.wxTextCtrl
control to our app. This will add a text box to our app. Click on the wxTextCtrl
control button on the toolbar below. Immediately the MyNotePadFrame window shown inside wxSmith window will be selected.TextCtrlNotePad
. This property will be used to name object of the wxTextCtrl
class.ID_TEXTCTRL_NOTEPAD
: It will be assigned a unique integer and then will be used to assign it with an event handler and an event type.idFileOpen
. This property will be defined as a unique integer and will be used to assign it with an event handler and an event type.&Open
. This text defines the menu label and &O
text will define an accelerator key. This menu can now be selected and clicked by pressing O button whenever this menu option is visible.Ctrl+O
. This property defines a keyboard accelerator to this menu option. A keyboard accelerator is a unique combination of keystrokes that will generate a click event for this menu option irrespective of menu item's visibility.Opens a file...
text. This will display this text in the status bar whenever this option is selected by mouse cursor or keyboard.MyNotePadFrame
class.*.txt
. This will set the filter text to files with .txt
extension.NotePadFileDialog
. This will be used to create an object of the wxFileDialog
class that represents a standard open or save dialog.OnFileOpen
text in the dialog box and then click on the OK button, as shown in the following screenshot:MyNotePadFrame::OnFileOpen()
function.int result; wxTextFile textFile; wxString fileContent; result = NotePadFileDialog->ShowModal(); if (result == wxID_OK) { if (textFile.Open(NotePadFileDialog->GetPath())) { for (size_t i = 0; i < textFile.GetLineCount(); i++) { fileContent << textFile.GetLine(i) << _T(" "); } textFile.Close(); TextCtrlNotePad->SetLabel(fileContent); } }
Let's explain preceding code. We have defined couple of variables in the beginning. We are showing file open dialog using the ShowModal()
function and result of this dialog will be stored inside the result
variable. Next line checks that we have received a wxID_OK
value, which indicates that user has selected a file.
We are opening a text file in using the Open()
function with the filename received from dialog box. If file opening succeeds then we'll create a loop to read all lines one by one. The fileContent
variable appends a line read from file and then appends a new line (
on Windows) to this string. When we are done reading all lines opened text file is closed with the Close()
function.
Finally we set text stored inside fileContent
variable is stored to our main text control.
We also need to include an extra header file in order to use the wxTextFile
class. Add the following line after the #include <wx/msgdlg.h>
line in the MyNotePadMain.cpp
file:
#include <wx/textfile.h>
Our app is now working! We wrote most of our GUI related code with the help of Code::Blocks. Only code that was manually written was the one we have inserted to open a text file. Code::Blocks offers an excellent cross-platform and rapid application development platform. We can develop a GUI app with Code::Blocks easily using this approach.
We have learned and developed our own notepad app in the previous section. However, our app was limited to opening a file. In this exercise we will extend our app to save a text file.
We'll perform the following steps:
&Save
to file menu after &Open
menu option, with Ctrl+S
as keyboard accelerator, idFileSave
as ID and Saves a file...
as Help text.OnFileSave()
.MyNotePadFrame::OnFileSave()
function:int result; result = NotePadFileDialog->ShowModal(); if (result == wxID_OK) { if (!TextCtrlNotePad->SaveFile(NotePadFileDialog->GetPath())) { wxMessageBox(_T("Couldn't save ") + NotePadFileDialog->GetPath(), _T("Error"), wxOK | wxICON_ERROR); } }
This code is similar to the code we wrote for the OnFileOpen()
function. We are using the wxTextCtrl::FileSave()
function to save our file in line number 5
. Code in line number 6
ensures that an error message should be shown when a file can't be written.
I leave it to you to follow earlier steps and complete this exercise. You can refer accompanying MyNotePad app source code for this completed exercise.
18.217.206.112