Cross-Platform GUI Programming with wxWidgets

wxWidgets makes use of several data structures internally and as parameters and return types in public library methods. Application programmers are encouraged to use the wxWidgets helper data structures in their projects.

wxObject

The wxObject class is the base class of all wxWidgets classes, providing run-time type information, reference counting, virtual destructor declaration, and optional debugging versions of new and delete. The wxClassInfo class is used to store meta-data about classes and is used by some of the wxObject methods.

MyWindow* window = wxDynamicCast(FindWindow(ID_MYWINDOW), MyWindow);

IsKindOf takes a wxClassInfo pointer and returns TRue if the object is of the specified type. For example:

bool tmp = obj->IsKindOf(CLASSINFO(wxFrame));

Ref takes a const wxObject& and replaces the current object's data with a reference to the passed object's data. The reference count of the current object is decremented, possibly freeing its data, and the reference count of the passed object is incremented.

UnRef decrements the reference count of the associated data and deletes the data if the reference count has fallen to 0.

wxLongLong

The wxLongLong class represents a 64-bit long number. A native 64-bit type is always used when available, and emulation code is used when the native type is unavailable. You would usually use this type in exactly the same manner as any other (built-in) arithmetic type. Note that wxLongLong is a signed type; if you want unsigned values, use wxULongLong, which has exactly the same API as wxLongLong except for some logical exceptions (such as the absolute value method). All of the usual mathematical operations are defined, as well as several convenient accessors:

  • Abs returns the absolute value of the value as a wxLongLong, either as a copy if used on a constant reference or modifying it in place if mutable.

  • ToLong returns a long representation of the stored value, triggering a debug assertion if any precision was lost.

  • ToString returns the string representation of the stored value in a wxString.

wxPoint and wxRealPoint

wxPoint is used throughout wxWidgets for specifying integer screen or window locations. As the names imply, the point classes store coordinate pairs as x and y values. The data members are declared as public and can be accessed directly as x and y. wxPoint provides + and operators that allow you to add or subtract by wxPoint or wxSize. wxRealPoint stores coordinates as double rather than int and provides + and operators accepting only other wxRealPoint objects.

Constructing a wxPoint is very straightforward:

wxPoint myPoint(50, 60);

wxRect

Used for manipulating rectangle information, the wxRect class is used by wxWidgets mostly with drawing or widget classes, such as wxDC or wxtreeCtrl. The wxRect class stores an x, y coordinate pair, as well as width and height, all of which are public. Rectangles can be added and subtracted from each other, and there are some other calculation methods as well.

Getright returns the x position of the right edge of the rectangle.

GetBottom returns the y position of the bottom edge of the rectangle.

GetSize returns the size of the rectangle (the height and width) in a wxSize object.

Inflate increases the size of the rectangle by the specified values, either uniformly (one parameter) or differently in each direction (two parameters).

Inside determines whether a given point is inside the rectangle. The point can be specified as separate x and y coordinates or as a wxPoint.

Intersects takes another wxRect object and determines whether the two rectangles overlap.

Offset moves the rectangle by the specified offset. The offset can be specified as separate x and y coordinates or as a wxPoint.

A wxRect object can be constructed with data in three different ways. The following three objects would all represent the exact same rectangle:

wxRect myRect1(50, 60, 100, 200); wxRect myRect2(wxPoint(50, 60), wxPoint(150, 260)); wxRect myRect3(wxPoint(50, 60), wxSize(100, 200));

wxRegion

A wxRegion represents a simple or complex region on a device context or window. It uses reference counting, so copying and assignment operations are fast. The primary use for wxRegion is to define or query clipping or update regions.

Contains returns TRue if the given coordinate pair, wxPoint, rectangle, or wxRect is within the region.

GetBox returns a wxRect representing the area of the region.

Intersect returns TRue if the given rectangle, wxRect, or wxRegion intersects the current region.

Offset moves the region by the specified offset. The offset is specified as separate x and y coordinates.

Subtract, Union, and Xor change the region in a variety of ways, offering ten overloads among the three methods. All these methods have overloads that take a wxRegion or a wxPoint parameter; see the wxWidgets documentation for a complete list of all available methods.

The following are the four most common ways to create a wxRegion; all these examples would create an object representing the same region:

wxRegion myRegion1(50, 60, 100, 200); wxRegion myRegion2(wxPoint(50, 60), wxPoint(150, 260)); wxRect myRect(50, 60, 100, 200); wxRegion myRegion3(myRect); wxRegion myRegion4(myRegion1);

You can use the wxRegionIterator class to iterate through the rectangles in a region, for example to repaint "damaged" areas in a paint event handler, as in the following example:

// Called when the window needs to be repainted void MyWindow::OnPaint(wxPaintEvent& event) { wxPaintDC dc(this); wxRegionIterator upd(GetUpdateRegion()); while (upd) { wxRect rect(upd.GetRect()); // Repaint this rectangle ...some code... upd ++ ; } }

wxSize

wxSize is used throughout wxWidgets for specifying integer sizes for windows, controls, canvas objects, and so on. A wxSize object is also frequently returned when using methods that would return size information.

GetHeight and GetWidth return the height or width.

SetHeight and SetWidth take integer parameters for setting a new height or width.

Set takes both a height and a width parameter to update the current size.

A wxSize object is very simply created by specifying a height and a width:

wxSize mySize(100, 200);

wxVariant

The wxVariant class represents a container for any type. A variant's value can be changed at runtime, possibly to a different type of value. This class is useful for reducing the programming for certain tasks, such as an editor for different data types, or a remote procedure call protocol.

wxVariant can store values of type bool, char, double, long, wxString, wxArrayString, wxList, wxDateTime, void pointer, and list of variants. However, an application can extend wxVariant's capabilities by deriving from the class wxVariantData and using the wxVariantData form of the wxVariant constructor or assignment operator to assign this data to a variant. Actual values for user-defined types will need to be accessed via the wxVariantData object, unlike basic data types, for which convenience functions such as GetLong can be used.

Bear in mind that not all types can be converted automatically to all other types; for example, it doesn't make much sense to convert a boolean into a wxDateTime object or to convert an integer to a wxArrayString. Use common sense to decide which conversions are appropriate, and remember that you can always get the current type using GetType. Here is a small example using wxVariant:

wxVariant Var; // Store a wxDateTime, get a wxString Var = wxDateTime::Now(); wxString DateAsString = Var.GetString(); // Store a wxString, get a double Var = wxT("10.25"); double StringAsDouble = Var.GetDouble(); // Type will be "string" wxString Type = Var.GetType(); // This is not a valid conversion, a string can't become a character // so c will be 0 due to being unable to convert char c = Var.GetChar();

    Категории