wxDialog

From WxWiki
Jump to: navigation, search
Official Classes SmallBlocks.png Archive Containers Controls Data Structures Database Date & Time Debug Device Contexts Dialogs Document & Views Drag & Drop Events Filesystem Frames Graphics Grid Cell Help HTML Logging Miscellaneous Networking Printing Sizers Streams Threading Windows

A dialog box is a window with a title bar and sometimes a system menu, which can be moved around the screen. It can contain controls and other windows and is often used to allow the user to make some choice or to answer a question. Dialogs can be made scrollable, automatically, for computers with low resolution screens: please see Automatic Scrolled Dialogs for further details.

Dialogs usually contain either a single button allowing to close the dialog or two buttons, one accepting the changes and the other one discarding them (such button, if present, is automatically activated if the user presses the "Esc" key). By default, buttons with the standard wxID_OK and wxID_CANCEL identifiers behave as expected. Starting with wxWidgets 2.7 it is also possible to use a button with a different identifier instead, see SetAffirmativeId() and SetEscapeId().

Also notice that the CreateButtonSizer() should be used to create the buttons appropriate for the current platform and positioned correctly (including their order which is platform-dependent).

Preventing the parent frame from being raised when a modeless dialog is raised

Normally, when a modeless dialog is activated, its parent frame is raised. If you don't want that, you might just want to create the dialog with 'NULL' as its parent.

wxWindow::CaptureMouse

Also, you can use wxWindow::CaptureMouse to direct all input to a single dialog like modal dialogs.

Gotchas

No OnShow() in wxGTK

There is no OnShow event on wxGTK as of wx2.3.3.

As a workaround, consider using the EVT_PAINT instead.

Style parameter

Passing wxFRAME_DEFAULT_STYLE (instead of wxDIALOG_DEFAULT_STYLE) to a wxDialog works like a charm: you get a sizeable, closable, min/max-able Dialog; however, if you don't implement OnClose(), it won't close when the 'X' is clicked. --aZhnaZg 6/1/03 2.4.0

Change TAB order dynamically

The simplest way to manage TAB order dynamically is handle EVT_CHAR_HOOK event.

BEGIN_EVENT_TABLE(AnyDialog, wxDialog)
	EVT_CHAR_HOOK (AnyDialog::OnCharHook)
END_EVENT_TABLE()

void AnyDialog::OnCharHook (wxKeyEvent& event)
{
	wxWindow *win = FindFocus ();
	if (win == NULL) {
		event.Skip ();
		return;
		}
	if (event.GetKeyCode () == WXK_TAB) {
		bool backward = event.ShiftDown ();
		switch (win->GetId ()) {
			case SOME_CONTROL_ID:
				if (backward)
					FindWindow (CONTROL_BEFORE_THIS)->SetFocus ();
				else
					FindWindow (CONTROL_AFTER_THIS)->SetFocus ();			
				return;
			}
		}
	event.Skip ();
}

Crash upon closing the application or dialog

Don't use Close() with a wxDialog, use Destroy instead.

Disabled Close Button

If you forget to specify wxCLOSE_BOX, the dialog will not have a functional close button ('X'). wxCLOSE_BOX is included in wxDEFAULT_DIALOG_STYLE.

See Also