wxWidget Helper Classes

Home / wxWidget Helper Classes

wxWidget Helper Classes

December 9, 2015 | Article | 1 Comment

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.

In this article, we will discuss about some helper classes that frequently used or useful for developing with wxWidget later. We will also illustrate some of the helper classes in simple console-based applications (although we can also use GUI applications).

Console

This is a simple console application. The application puts some text into the console window.

/** filename: console.cpp **/
#include <wx/string.h>

int main(int argc, char **argv) {
  wxPuts(wxT("A wxWidgets console application"));
}

You should get output, a string “A wxWidgets console application” on your terminal.

wxString

wxString is a class representing a character string.

In the following example, we define three wxStrings instances. We create one string of these strings using addition operation. Create a file addition.cpp and write these into it:

/** filename: 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);
}

It should give you “Linux Operating System” as the output.

A method printf() of wxString can also be used for formatting the strings.
/** filename: printf.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);
}

The following example checks, whether a string contains another string. For this we have a Contains() method.

/** filename: 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!"));
  }
}

A wxString always has length and this length can be obtained by method Len(). It will return the number of characters on string.

#include <wx/string.h>

int main(int argc, char **argv)
{
  wxString str = wxT("The history of my life");
  wxPrintf(wxT("The string has %ld characters\n"), str.Len());
}

The MakeLower() and MakeUpper() methods can be used to make characters lower case and upper case.

#include <wx/string.h>

int main(int argc, char **argv)
{
  wxString str = wxT("The history of my life");

  wxPuts(str.MakeLower());
  wxPuts(str.MakeUpper());
}

Also note that, when the MakeLower() or MakeUpper() used, the effect will remain.

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. This methods is only for for Unix.

#include <wx/string.h>
#include <wx/utils.h>

int main(int argc, char **argv)
{
  wxShell(wxT("ls -l"));
}

Another thing we can do are getting the home user directory, os name, user name, host name and total free memory.

#include <wx/string.h>
#include <wx/utils.h>

int main(int argc, char **argv)
{
  wxPuts(wxGetHomeDir());
  wxPuts(wxGetOsDescription());
  wxPuts(wxGetUserName());
  wxPuts(wxGetFullHostName());

  long mem = wxGetFreeMemory().ToLong();

  wxPrintf(wxT("Memory: %ld\n"), mem);
}

It looks like wxWidget is optimized for lsb based linux distribution. wxWidget on my Slackware64, which is not using LSB, gives some expected result.

Time & date

wxWidget also offer utilities for working with date & time. The example shows current date or time in various formats.

#include <wx/datetime.h>

int main(int argc, char **argv)
{
  wxDateTime now = wxDateTime::Now();

  wxString date1 = now.Format();
  wxString date2 = now.Format(wxT("%X"));
  wxString date3 = now.Format(wxT("%x"));

  wxPuts(date1);
  wxPuts(date2);
  wxPuts(date3);
}

Next we will show current time in different cities.

#include <wx/datetime.h>

int main(int argc, char **argv)
{
  wxDateTime now = wxDateTime::Now();

  wxPrintf(wxT("  Jakarta: %s\n"), now.Format(wxT("%a %T"),
      wxDateTime::GMT7).c_str());
  wxPrintf(wxT("  Moscow: %s\n"), now.Format(wxT("%a %T"),
      wxDateTime::MSD).c_str());
  wxPrintf(wxT("Budapest: %s\n"), now.Format(wxT("%a %T"),
      wxDateTime::CEST).c_str());
  wxPrintf(wxT("  London: %s\n"), now.Format(wxT("%a %T"),
      wxDateTime::WEST).c_str());
  wxPrintf(wxT("New York: %s\n"), now.Format(wxT("%a %T"),
      wxDateTime::EDT).c_str());
}

The following example shows, how we can add date spans to our date/time. We add one month to the current time.

#include <wx/datetime.h>

int main(int argc, char **argv)
{
  wxDateTime now = wxDateTime::Now();
  wxString date1 = now.Format(wxT("%B %d %Y"));
  wxPuts(date1);

  wxDateSpan span(0, 1); // 0 year, 1 month
  wxDateTime then = now.Add(span);

  wxString date2 = then.Format(wxT("%B %d %Y"));
  wxPuts(date2);

}

Files

wxWidgets has several classes to facilitate working with files. wxWidgets use low level access to files, as opposed to working with streams.

In the following example, we use the wxFile class to create a new file and write data to it. We also test, whether the file is opened. Note, that when we create a file, it automatically stays as opened.

#include <wx/file.h>

int main(int argc, char **argv)
{
  wxString str = wxT("You make me want to be a better man.\n");

  wxFile file;
  file.Create(wxT("quote"), true);

  if (file.IsOpened())
      wxPuts(wxT("the file is opened"));

  file.Write(str);
  file.Close();

  if (!file.IsOpened())
      wxPuts(wxT("the file is not opened"));
}

The wxTextFile is a simple class which allows to work with text files on line by line basis. It is easier to work with this class than with wxFile class.In the next example, we will print the number of lines in a file, first and last lines and finally we will read and show the contents of the file.

#include <wx/textfile.h>

int main(int argc, char **argv)
{
  wxTextFile file(wxT("test.c"));

  file.Open();

  wxPrintf(wxT("Number of lines: %d\n"), file.GetLineCount());
  wxPrintf(wxT("First line: %s\n"), file.GetFirstLine().c_str());
  wxPrintf(wxT("Last line: %s\n"), file.GetLastLine().c_str());

  wxPuts(wxT("-------------------------------------"));

  wxString s;

  for ( s = file.GetFirstLine(); !file.Eof();
      s = file.GetNextLine() )
  {
       wxPuts(s);
  }

  file.Close();
}

The wxDir class allows us to enumerate files and directories.

In the following example, we will print all files and directories available in the current working directory.

#include <wx/dir.h>
#include <wx/filefn.h>

int main(int argc, char **argv)
{
  wxDir dir(wxGetCwd());

  wxString file;

  bool cont = dir.GetFirst(&file, wxEmptyString,
      wxDIR_FILES | wxDIR_DIRS);

  while (cont) {
      wxPuts(file);
      cont = dir.GetNext(&file);
  }
}

,

About Author

about author

xathrya

A man who is obsessed to low level technology.

1 Comment
  1. First wxWidget Program - Xathrya.ID

    […] learning some wxWidgets basic, lets open new article for discussing a simple […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial