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"
27 #include "wx/evtloop.h"
30 #pragma message disable nosimpint
34 #include <X11/Shell.h>
38 #include <Xm/MwmUtil.h>
40 #include <Xm/BulletinB.h>
43 #include <Xm/DialogS.h>
44 #include <Xm/FileSB.h>
45 #include <Xm/RowColumn.h>
46 #include <Xm/LabelG.h>
47 #include <Xm/AtomMgr.h>
49 #include <Xm/Protocols.h>
52 #pragma message enable nosimpint
55 #include "wx/motif/private.h"
57 // A stack of modal_showing flags, since we can't rely
58 // on accessing wxDialog::m_modalShowing within
59 // wxDialog::Show in case a callback has deleted the wxDialog.
60 // static wxList wxModalShowingStack;
62 // Lists to keep track of windows, so we can disable/enable them
64 wxList wxModalDialogs
;
65 extern wxList wxModelessWindows
; // Frames and modeless dialogs
66 extern wxList wxPendingDelete
;
68 #define wxUSE_INVISIBLE_RESIZE 1
70 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
72 BEGIN_EVENT_TABLE(wxDialog
, wxTopLevelWindow
)
73 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
74 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
75 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
76 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
77 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
78 EVT_CLOSE(wxDialog::OnCloseWindow
)
84 m_modalShowing
= FALSE
;
86 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
89 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
90 const wxString
& title
,
96 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
98 if( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
,
102 m_modalShowing
= FALSE
;
105 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
106 m_foregroundColour
= *wxBLACK
;
108 Widget dialogShell
= (Widget
) m_mainWidget
;
109 Widget shell
= XtParent(dialogShell
) ;
113 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
116 // Can't remember what this was about... but I think it's necessary.
117 if (wxUSE_INVISIBLE_RESIZE
)
120 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
123 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
127 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
129 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
132 // Positioning of the dialog doesn't work properly unless the dialog
133 // is managed, so we manage without mapping to the screen.
134 // To show, we map the shell (actually it's parent).
135 if (!wxUSE_INVISIBLE_RESIZE
)
136 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
138 if (!wxUSE_INVISIBLE_RESIZE
)
140 XtManageChild(dialogShell
);
141 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
143 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
144 wxUniversalRepaintProc
, (XtPointer
) this);
146 ChangeBackgroundColour();
151 bool wxDialog::DoCreate( wxWindow
* parent
, wxWindowID id
,
152 const wxString
& title
,
156 const wxString
& name
)
158 Widget parentWidget
= (Widget
) 0;
160 parentWidget
= (Widget
) parent
->GetTopWidget();
162 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
164 wxASSERT_MSG( (parentWidget
!= (Widget
) 0),
165 "Could not find a suitable parent shell for dialog." );
168 XtSetArg (args
[0], XmNdefaultPosition
, False
);
169 XtSetArg (args
[1], XmNautoUnmanage
, False
);
171 XmCreateBulletinBoardDialog( parentWidget
,
172 wxConstCast(name
.c_str(), char),
174 m_mainWidget
= (WXWidget
) dialogShell
;
176 // We don't want margins, since there is enough elsewhere.
177 XtVaSetValues( dialogShell
,
180 XmNresizePolicy
, XmRESIZE_NONE
,
184 XtOverrideTranslations(dialogShell
,
185 ptr
= XtParseTranslationTable("<Configure>: resize()"));
188 XtRealizeWidget(dialogShell
);
190 wxAddWindowToTable( (Widget
)m_mainWidget
, this );
195 void wxDialog::SetModal(bool flag
)
198 m_windowStyle
|= wxDIALOG_MODAL
;
200 if ( m_windowStyle
& wxDIALOG_MODAL
)
201 m_windowStyle
-= wxDIALOG_MODAL
;
203 wxModelessWindows
.DeleteObject(this);
205 wxModelessWindows
.Append(this);
208 wxDialog::~wxDialog()
210 m_isBeingDeleted
= TRUE
;
216 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
217 wxUniversalRepaintProc
, (XtPointer
) this);
220 m_modalShowing
= FALSE
;
221 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
223 XtUnmapWidget((Widget
) m_mainWidget
);
230 void wxDialog::DoDestroy()
234 wxDeleteWindowFromTable( (Widget
)m_mainWidget
);
235 XtDestroyWidget( (Widget
)m_mainWidget
);
239 // By default, pressing escape cancels the dialog
240 void wxDialog::OnCharHook(wxKeyEvent
& event
)
242 if (event
.m_keyCode
== WXK_ESCAPE
)
244 // Behaviour changed in 2.0: we'll send a Cancel message
245 // to the dialog instead of Close.
246 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
247 cancelEvent
.SetEventObject( this );
248 GetEventHandler()->ProcessEvent(cancelEvent
);
252 // We didn't process this event.
256 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
258 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
259 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
260 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
263 void wxDialog::DoSetClientSize(int width
, int height
)
265 wxWindow::SetSize(-1, -1, width
, height
);
268 void wxDialog::SetTitle(const wxString
& title
)
270 wxTopLevelWindow::SetTitle( title
);
274 wxXmString
str( title
);
275 XtVaSetValues( (Widget
)m_mainWidget
,
276 XmNtitle
, title
.c_str(),
277 XmNdialogTitle
, str(), // Roberto Cocchi
278 XmNiconName
, title
.c_str(),
283 bool wxDialog::Show( bool show
)
285 if( !wxTopLevelWindowMotif::Show( show
) )
292 if (!wxUSE_INVISIBLE_RESIZE
)
293 XtMapWidget(XtParent((Widget
) m_mainWidget
));
295 XtManageChild((Widget
)m_mainWidget
) ;
297 XRaiseWindow( XtDisplay( (Widget
)m_mainWidget
),
298 XtWindow( (Widget
)m_mainWidget
) );
303 if (!wxUSE_INVISIBLE_RESIZE
)
304 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
306 XtUnmanageChild((Widget
)m_mainWidget
) ;
308 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
309 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
315 // Shows a dialog modally, returning a return code
316 int wxDialog::ShowModal()
318 m_windowStyle
|= wxDIALOG_MODAL
;
324 m_eventLoop
= new wxEventLoop
;
326 m_modalShowing
= TRUE
;
327 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
331 // Now process all events in case they get sent to a destroyed dialog
332 XSync(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()), FALSE
);
333 while (m_eventLoop
->Pending())
335 XFlush(XtDisplay((Widget
) wxTheApp
->GetTopLevelWidget()));
336 m_eventLoop
->Dispatch();
342 // TODO: is it safe to call this, if the dialog may have been deleted
343 // by now? Probably only if we're using delayed deletion of dialogs.
344 return GetReturnCode();
347 void wxDialog::EndModal(int retCode
)
352 SetReturnCode(retCode
);
354 // Strangely, we don't seem to need this now.
355 // XtRemoveGrab((Widget) m_mainWidget);
359 m_modalShowing
= FALSE
;
364 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
366 if ( Validate() && TransferDataFromWindow() )
372 SetReturnCode(wxID_OK
);
378 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
381 TransferDataFromWindow();
382 // TODO probably need to disable the Apply button until things change again
385 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
388 EndModal(wxID_CANCEL
);
391 SetReturnCode(wxID_CANCEL
);
396 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
398 // We'll send a Cancel message by default,
399 // which may close the dialog.
400 // Check for looping if the Cancel event handler calls Close().
402 // Note that if a cancel button and handler aren't present in the dialog,
403 // nothing will happen when you close the dialog via the window manager, or
405 // We wouldn't want to destroy the dialog by default, since the dialog may have been
406 // created on the stack.
407 // However, this does mean that calling dialog->Close() won't delete the dialog
408 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
409 // sure to destroy the dialog.
410 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
412 static wxList closing
;
414 if ( closing
.Member(this) )
417 closing
.Append(this);
419 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
420 cancelEvent
.SetEventObject( this );
421 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
423 closing
.DeleteObject(this);
426 // Destroy the window (delayed, if a managed window)
427 bool wxDialog::Destroy()
429 if (!wxPendingDelete
.Member(this))
430 wxPendingDelete
.Append(this);
434 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
436 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
440 void wxDialog::ChangeFont(bool keepOriginalSize
)
442 wxWindow::ChangeFont(keepOriginalSize
);
445 void wxDialog::ChangeBackgroundColour()
448 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
451 void wxDialog::ChangeForegroundColour()
454 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);