|
In this section, we'll discuss some further topics to bear in mind when you're working with sizers. Dialog UnitsAlthough sizers protect you from changes in basic control size on different platforms and in different languages, you may still have some hard-coded sizes in your dialogs (for example, for list boxes). If you would like these sizes to adjust to the current system font (or font supplied by the application), you can use dialog units instead of pixels. Dialog units are based on average character width and height for a window's font, and so the actual pixel dimension for a given dialog unit value will vary according to the current font. wxWindow has functions ConvertDialogToPixels and ConvertPixelsToDialog, and a convenience macro wxDLG_UNIT(window, ptOrSz) that can be used with both wxPoint and wxSize objects. So instead of passing a pixel size to your control, use the wxDLG_UNIT macro, for example: wxListBox* listBox = new wxListBox(parent, wxID_ANY, wxDefaultPosition, wxDLG_UNIT(parent, wxSize(60, 20))); Dialog units can be specified in an XRC file by appending "d" to dimension values. Platform-Adaptive LayoutsAlthough dialogs on different platforms are largely similar, sometimes the style guides are incompatible. For example, on Windows and Linux, it's acceptable to have right-justified or centered OK, Cancel, and Help buttons, in that order. On Mac OS X, the Help should be on the left, and Cancel and OK buttons are right aligned, in that order. To help with this issue, wxStdDialogButtonSizer is provided. It's derived from wxBoxSizer, so it can be used in a similar way, but its orientation will depend on platform. This sizer's constructor has no arguments. There are two ways of adding buttons: pass the button pointer to AddButton, or (if you're not using standard identifiers) call SetAffirmativeButton, SetNegativeButton, and SetCancelButton. If using AddButton, you should use identifiers from this list: wxID_OK, wxID_YES, wxID_CANCEL, wxID_NO, wxID_SAVE, wxID_APPLY, wxID_HELP, and wxID_CONTEXT_HELP. Then, after the buttons have been added, call Realize so that the sizer can add the buttons in the appropriate order with the appropriate spacing (which it can only do when it knows about all the buttons in the sizer). The following code creates a standard button sizer with OK, Cancel, and Help buttons: wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL); dialog->SetSizer(topSizer); wxButton* ok = new wxButton(dialog, wxID_OK); wxButton* cancel = new wxButton(dialog, wxID_CANCEL); wxButton* help = new wxButton(dialog, wxID_HELP); wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer; topSizer->Add(buttonSizer, 0, wxEXPAND|wxALL, 10); buttonSizer->AddButton(ok); buttonSizer->AddButton(cancel); buttonSizer->AddButton(help); buttonSizer->Realize(); As a convenience, wxDialog::CreateButtonSizer can be used, indirectly creating a wxStdDialogButtonSizer with buttons based on a flag list. If you look at the dialog implementations in src/generic, you will see that CreateButtonSizer is used for many of them. The flags in Table 7-2 can be passed to this function.
Using CreateButtonSizer simplifies the example code shown previously to the following: wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL); dialog->SetSizer(topSizer); topSizer->Add(CreateButtonSizer(wxOK|wxCANCEL|wxHELP), 0, wxEXPAND|wxALL, 10); There is another way to specify variations in UI on different platforms. XRC allows a platform parameter to be associated with each object; this parameter's value can be a combination of unix, win, mac, and os2 separated by the pipe character ("|"). XRC will only create the element if the platform value matches the actual platform the program is running on. DialogBlocks supports this property and can generate conditional C++ code if XRC is not being used. Alternatively, you can load a different XRC file for each platform, but this is harder to maintain than having the differences contained within a single dialog design. Dynamic LayoutsSometimes you'd like the layout to change dynamically; for example, clicking on a Details button might expand the dialog and show further controls. You can hide controls contained in sizers the same way you would hide any control, using the wxWindow::Show method. However, wxSizer also offers a separate method, which can tell the sizer not to consider a window in its size calculations. To hide a window using the sizer, pass false to wxSizer::Show. You must then call wxSizer::Layout to force an update. |
|