1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dialog.h"
17 #define XtDisplay XTDISPLAY
18 #define XtWindow XTWINDOW
19 #define XtParent XTPARENT
20 #define XtScreen XTSCREEN
23 #include "wx/dialog.h"
26 #include "wx/settings.h"
29 #pragma message disable nosimpint
33 #include <X11/Shell.h>
37 #include <Xm/MwmUtil.h>
39 #include <Xm/BulletinB.h>
42 #include <Xm/DialogS.h>
43 #include <Xm/FileSB.h>
44 #include <Xm/RowColumn.h>
45 #include <Xm/LabelG.h>
46 #include <Xm/AtomMgr.h>
48 #include <Xm/Protocols.h>
51 #pragma message enable nosimpint
54 #include "wx/motif/private.h"
56 // A stack of modal_showing flags, since we can't rely
57 // on accessing wxDialog::m_modalShowing within
58 // wxDialog::Show in case a callback has deleted the wxDialog.
59 static wxList wxModalShowingStack
;
61 // Lists to keep track of windows, so we can disable/enable them
63 wxList wxModalDialogs
;
64 extern wxList wxModelessWindows
; // Frames and modeless dialogs
65 extern wxList wxPendingDelete
;
67 #define wxUSE_INVISIBLE_RESIZE 1
69 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
71 BEGIN_EVENT_TABLE(wxDialog
, wxTopLevelWindow
)
72 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
73 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
74 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
75 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
76 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
77 EVT_CLOSE(wxDialog::OnCloseWindow
)
83 m_modalShowing
= FALSE
;
84 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
87 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
88 const wxString
& title
,
94 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
96 if( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
,
100 m_modalShowing
= FALSE
;
102 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
103 m_foregroundColour
= *wxBLACK
;
105 Widget dialogShell
= (Widget
) m_mainWidget
;
106 Widget shell
= XtParent(dialogShell
) ;
110 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
113 // Can't remember what this was about... but I think it's necessary.
114 if (wxUSE_INVISIBLE_RESIZE
)
117 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
120 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
124 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
126 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
129 // Positioning of the dialog doesn't work properly unless the dialog
130 // is managed, so we manage without mapping to the screen.
131 // To show, we map the shell (actually it's parent).
132 if (!wxUSE_INVISIBLE_RESIZE
)
133 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
135 if (!wxUSE_INVISIBLE_RESIZE
)
137 XtManageChild(dialogShell
);
138 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
140 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
141 wxUniversalRepaintProc
, (XtPointer
) this);
143 ChangeBackgroundColour();
148 bool wxDialog::DoCreate( wxWindow
* parent
, wxWindowID id
,
149 const wxString
& title
,
153 const wxString
& name
)
155 Widget parentWidget
= (Widget
) 0;
157 parentWidget
= (Widget
) parent
->GetTopWidget();
159 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
161 wxASSERT_MSG( (parentWidget
!= (Widget
) 0),
162 "Could not find a suitable parent shell for dialog." );
165 XtSetArg (args
[0], XmNdefaultPosition
, False
);
166 XtSetArg (args
[1], XmNautoUnmanage
, False
);
168 XmCreateBulletinBoardDialog( parentWidget
, (char*)name
.c_str(),
170 m_mainWidget
= (WXWidget
) dialogShell
;
172 // We don't want margins, since there is enough elsewhere.
173 XtVaSetValues( dialogShell
,
176 XmNresizePolicy
, XmRESIZE_NONE
,
180 XtOverrideTranslations(dialogShell
,
181 ptr
= XtParseTranslationTable("<Configure>: resize()"));
184 XtRealizeWidget(dialogShell
);
186 wxAddWindowToTable( (Widget
)m_mainWidget
, this );
191 void wxDialog::SetModal(bool flag
)
194 m_windowStyle
|= wxDIALOG_MODAL
;
196 if ( m_windowStyle
& wxDIALOG_MODAL
)
197 m_windowStyle
-= wxDIALOG_MODAL
;
199 wxModelessWindows
.DeleteObject(this);
201 wxModelessWindows
.Append(this);
204 wxDialog::~wxDialog()
206 m_isBeingDeleted
= TRUE
;
210 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
211 wxUniversalRepaintProc
, (XtPointer
) this);
214 m_modalShowing
= FALSE
;
215 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
217 XtUnmapWidget((Widget
) m_mainWidget
);
221 void wxDialog::DoDestroy()
225 wxDeleteWindowFromTable( (Widget
)m_mainWidget
);
226 XtDestroyWidget( (Widget
)m_mainWidget
);
230 // By default, pressing escape cancels the dialog
231 void wxDialog::OnCharHook(wxKeyEvent
& event
)
233 if (event
.m_keyCode
== WXK_ESCAPE
)
235 // Behaviour changed in 2.0: we'll send a Cancel message
236 // to the dialog instead of Close.
237 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
238 cancelEvent
.SetEventObject( this );
239 GetEventHandler()->ProcessEvent(cancelEvent
);
243 // We didn't process this event.
247 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
249 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
250 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
251 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
254 void wxDialog::DoSetClientSize(int width
, int height
)
256 wxWindow::SetSize(-1, -1, width
, height
);
259 void wxDialog::SetTitle(const wxString
& title
)
261 wxTopLevelWindow::SetTitle( title
);
265 wxXmString
str( title
);
266 XtVaSetValues( (Widget
)m_mainWidget
,
267 XmNtitle
, title
.c_str(),
268 XmNdialogTitle
, str(), // Roberto Cocchi
269 XmNiconName
, title
.c_str(),
274 bool wxDialog::Show( bool show
)
276 if( !wxTopLevelWindowMotif::Show( show
) )
283 if (!wxUSE_INVISIBLE_RESIZE
)
284 XtMapWidget(XtParent((Widget
) m_mainWidget
));
286 XtManageChild((Widget
)m_mainWidget
) ;
288 XRaiseWindow( XtDisplay( (Widget
)m_mainWidget
),
289 XtWindow( (Widget
)m_mainWidget
) );
294 if (!wxUSE_INVISIBLE_RESIZE
)
295 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
297 XtUnmanageChild((Widget
)m_mainWidget
) ;
299 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
300 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
306 // Shows a dialog modally, returning a return code
307 int wxDialog::ShowModal()
309 m_windowStyle
|= wxDIALOG_MODAL
;
316 wxModalShowingStack
.Insert((wxObject
*)TRUE
);
318 m_modalShowing
= TRUE
;
319 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
323 // Loop until we signal that the dialog should be closed
324 while ((wxModalShowingStack
.Number() > 0) && ((int)(wxModalShowingStack
.First()->Data()) != 0))
326 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
328 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
329 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
331 if (XtAppPending( (XtAppContext
) wxTheApp
->GetAppContext() ) == 0)
333 if (!wxTheApp
->ProcessIdle())
336 // leave the main loop to give other threads a chance to
337 // perform their GUI work
346 // Remove modal dialog flag from stack
347 wxNode
*node
= wxModalShowingStack
.First();
351 // Now process all events in case they get sent to a destroyed dialog
352 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
353 while (XtAppPending((XtAppContext
) wxTheApp
->GetAppContext()))
355 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
356 XtAppNextEvent((XtAppContext
) wxTheApp
->GetAppContext(), &event
);
358 wxTheApp
->ProcessXEvent((WXEvent
*) &event
);
361 // TODO: is it safe to call this, if the dialog may have been deleted
362 // by now? Probably only if we're using delayed deletion of dialogs.
363 return GetReturnCode();
366 void wxDialog::EndModal(int retCode
)
371 SetReturnCode(retCode
);
373 // Strangely, we don't seem to need this now.
374 // XtRemoveGrab((Widget) m_mainWidget);
378 m_modalShowing
= FALSE
;
380 wxNode
*node
= wxModalShowingStack
.First();
382 node
->SetData((wxObject
*)FALSE
);
386 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
388 if ( Validate() && TransferDataFromWindow() )
394 SetReturnCode(wxID_OK
);
400 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
403 TransferDataFromWindow();
404 // TODO probably need to disable the Apply button until things change again
407 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
410 EndModal(wxID_CANCEL
);
413 SetReturnCode(wxID_CANCEL
);
418 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
420 // We'll send a Cancel message by default,
421 // which may close the dialog.
422 // Check for looping if the Cancel event handler calls Close().
424 // Note that if a cancel button and handler aren't present in the dialog,
425 // nothing will happen when you close the dialog via the window manager, or
427 // We wouldn't want to destroy the dialog by default, since the dialog may have been
428 // created on the stack.
429 // However, this does mean that calling dialog->Close() won't delete the dialog
430 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
431 // sure to destroy the dialog.
432 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
434 static wxList closing
;
436 if ( closing
.Member(this) )
439 closing
.Append(this);
441 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
442 cancelEvent
.SetEventObject( this );
443 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
445 closing
.DeleteObject(this);
448 // Destroy the window (delayed, if a managed window)
449 bool wxDialog::Destroy()
451 if (!wxPendingDelete
.Member(this))
452 wxPendingDelete
.Append(this);
456 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
458 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
462 void wxDialog::ChangeFont(bool keepOriginalSize
)
464 wxWindow::ChangeFont(keepOriginalSize
);
467 void wxDialog::ChangeBackgroundColour()
470 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
473 void wxDialog::ChangeForegroundColour()
476 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);