1.The wxWidgets programming tutorial (wxWidgets helper classes)
Translations of this material:
- into Russian: Руководство по программированию с wxWidgets (1. Вспомогательные классы wxWidgets).. Translation complete.
-
Submitted for translation by ber113 14.03.2010
Published 2 years, 2 months ago.
Text
wxWidgets helper classes
wxWidgets consists of a large group of helper classes, that help programmers to do their job. These include classes for working with strings, files, xml files, streams, database or network. Here we will show only a tiny drop of the whole lake.
wxWidgets library can be used to create console and gui applications. In this chapter, we will illustrate some of the helper classes in console based applications.
Console
This is a simple console application. The application puts some text into the console window.
console.cpp
#include <wx/string.h>
int main(int argc, char **argv)
{
wxPuts(wxT("A wxWidgets console application"));
}
Output
A wxWidgets console application
wxString
This is probably the most useful class. wxString is a class representing a character string.
In the following example, we define three wxStrings. We create one string of these strings using addition operation.
addition.cpp
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str1 = wxT("Linux");
wxString str2 = wxT("Operating");
wxString str3 = wxT("System");
wxString str = str1 + wxT(" ") + str2 + wxT(" ") + str3;
wxPuts(str);
}
Output
Linux Operating System
The Printf() method is used to format strings.
formatted.cpp
#include <wx/string.h>
int main(int argc, char **argv)
{
int flowers = 21;
wxString str;
str.Printf(wxT("There are %d red roses."), flowers);
wxPuts(str);
}
Output
There are 21 red roses.
The following example checks, whether a string contains another string. For this we have a Contains() method.
contains.cpp
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
if (str.Contains(wxT("history"))) {
wxPuts(wxT("Contains!"));
}
if (!str.Contains(wxT("plain"))) {
wxPuts(wxT("Does not contain!"));
}
}
Output
Contains!
Does not contain!
The Len() method returns the number of characters in the string.
length.cpp
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
wxPrintf(wxT("The string has %d characters\n"), str.Len());
}
Output
The string has 22 characters
The MakeLower() and MakeUpper() methods make characters lower case and upper case.
cases.cpp
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
wxPuts(str.MakeLower());
wxPuts(str.MakeUpper());
}
Output
the history of my life
THE HISTORY OF MY LIFE
Utility functions
wxWidgets has several handy utility functions for executing a process, getting a home user directory or getting the OS name.
In the following example, we execute the ls command. For this, we have the wxShell() function. Unix only.
shell.cpp
#include <wx/string.h>
#include <wx/utils.h>
int main(int argc, char **argv)
{
wxShell(wxT("ls -l"));
}
Output
total 40
-rwxr-xr-x 1 vronskij vronskij 9028 2007-09-06 22:10 basic
-rw-r--r-- 1 vronskij vronskij 95 2007-09-06 22:09 basic.cpp
-rw-r--r-- 1 vronskij vronskij 430 2007-09-06 00:07 basic.cpp~
-rwxr-xr-x 1 vronskij vronskij 11080 2007-09-05 23:17 console
-rw-r--r-- 1 vronskij vronskij 500 2007-09-05 23:17 console.cpp
-rw-r--r-- 1 vronskij vronskij 485 2007-09-05 23:16 console.cpp~
Next we will we will get the home user directory, os name, user name, host name and total free memory.
system.cpp
#include <wx/string.h>
