1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "dialog.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
20 #define XtDisplay XTDISPLAY
21 #define XtWindow XTWINDOW
22 #define XtParent XTPARENT
23 #define XtScreen XTSCREEN
26 #include "wx/dialog.h"
29 #include "wx/settings.h"
30 #include "wx/evtloop.h"
33 #pragma message disable nosimpint
37 #include <X11/Shell.h>
41 #include <Xm/MwmUtil.h>
43 #include <Xm/BulletinB.h>
46 #include <Xm/DialogS.h>
47 #include <Xm/FileSB.h>
48 #include <Xm/RowColumn.h>
49 #include <Xm/LabelG.h>
50 #include <Xm/AtomMgr.h>
52 #include <Xm/Protocols.h>
55 #pragma message enable nosimpint
58 #include "wx/motif/private.h"
60 // A stack of modal_showing flags, since we can't rely
61 // on accessing wxDialog::m_modalShowing within
62 // wxDialog::Show in case a callback has deleted the wxDialog.
63 // static wxList wxModalShowingStack;
65 // Lists to keep track of windows, so we can disable/enable them
67 wxList wxModalDialogs
;
68 extern wxList wxModelessWindows
; // Frames and modeless dialogs
69 extern wxList wxPendingDelete
;
71 #define wxUSE_INVISIBLE_RESIZE 1
73 IMPLEMENT_DYNAMIC_CLASS(wxDialog
, wxTopLevelWindow
)
75 BEGIN_EVENT_TABLE(wxDialog
, wxTopLevelWindow
)
76 EVT_BUTTON(wxID_OK
, wxDialog::OnOK
)
77 EVT_BUTTON(wxID_APPLY
, wxDialog::OnApply
)
78 EVT_BUTTON(wxID_CANCEL
, wxDialog::OnCancel
)
79 EVT_CHAR_HOOK(wxDialog::OnCharHook
)
80 EVT_SYS_COLOUR_CHANGED(wxDialog::OnSysColourChanged
)
81 EVT_CLOSE(wxDialog::OnCloseWindow
)
87 m_modalShowing
= FALSE
;
89 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
92 bool wxDialog::Create(wxWindow
*parent
, wxWindowID id
,
93 const wxString
& title
,
99 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG
);
101 if( !wxTopLevelWindow::Create( parent
, id
, title
, pos
, size
, style
,
105 m_modalShowing
= FALSE
;
108 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
109 m_foregroundColour
= *wxBLACK
;
111 Widget dialogShell
= (Widget
) m_mainWidget
;
112 Widget shell
= XtParent(dialogShell
) ;
116 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
119 // Can't remember what this was about... but I think it's necessary.
120 if (wxUSE_INVISIBLE_RESIZE
)
123 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
126 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
130 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
132 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
135 // Positioning of the dialog doesn't work properly unless the dialog
136 // is managed, so we manage without mapping to the screen.
137 // To show, we map the shell (actually it's parent).
138 if (!wxUSE_INVISIBLE_RESIZE
)
139 XtVaSetValues(shell
, XmNmappedWhenManaged
, FALSE
, NULL
);
141 if (!wxUSE_INVISIBLE_RESIZE
)
143 XtManageChild(dialogShell
);
144 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
146 XtAddEventHandler(dialogShell
,ExposureMask
,FALSE
,
147 wxUniversalRepaintProc
, (XtPointer
) this);
149 ChangeBackgroundColour();
154 bool wxDialog::DoCreate( wxWindow
* parent
, wxWindowID id
,
155 const wxString
& title
,
159 const wxString
& name
)
161 Widget parentWidget
= (Widget
) 0;
163 parentWidget
= (Widget
) parent
->GetTopWidget();
165 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
167 wxASSERT_MSG( (parentWidget
!= (Widget
) 0),
168 "Could not find a suitable parent shell for dialog." );
171 XtSetArg (args
[0], XmNdefaultPosition
, False
);
172 XtSetArg (args
[1], XmNautoUnmanage
, False
);
174 XmCreateBulletinBoardDialog( parentWidget
,
175 wxConstCast(name
.c_str(), char),
177 m_mainWidget
= (WXWidget
) dialogShell
;
179 // We don't want margins, since there is enough elsewhere.
180 XtVaSetValues( dialogShell
,
183 XmNresizePolicy
, XmRESIZE_NONE
,
187 XtOverrideTranslations(dialogShell
,
188 ptr
= XtParseTranslationTable("<Configure>: resize()"));
191 XtRealizeWidget(dialogShell
);
193 wxAddWindowToTable( (Widget
)m_mainWidget
, this );
198 void wxDialog::SetModal(bool flag
)
201 #pragma message disable codcauunr
204 m_windowStyle
|= wxDIALOG_MODAL
;
206 if ( m_windowStyle
& wxDIALOG_MODAL
)
207 m_windowStyle
-= wxDIALOG_MODAL
;
209 wxModelessWindows
.DeleteObject(this);
211 wxModelessWindows
.Append(this);
213 #pragma message enable codcauunr
217 wxDialog::~wxDialog()
219 m_isBeingDeleted
= TRUE
;
225 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, FALSE
,
226 wxUniversalRepaintProc
, (XtPointer
) this);
229 m_modalShowing
= FALSE
;
230 if (!wxUSE_INVISIBLE_RESIZE
&& m_mainWidget
)
232 XtUnmapWidget((Widget
) m_mainWidget
);
239 void wxDialog::DoDestroy()
243 wxDeleteWindowFromTable( (Widget
)m_mainWidget
);
244 XtDestroyWidget( (Widget
)m_mainWidget
);
248 // By default, pressing escape cancels the dialog
249 void wxDialog::OnCharHook(wxKeyEvent
& event
)
251 if (event
.m_keyCode
== WXK_ESCAPE
)
253 // Behaviour changed in 2.0: we'll send a Cancel message
254 // to the dialog instead of Close.
255 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
256 cancelEvent
.SetEventObject( this );
257 GetEventHandler()->ProcessEvent(cancelEvent
);
261 // We didn't process this event.
265 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
267 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
268 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
269 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
272 void wxDialog::DoSetClientSize(int width
, int height
)
274 wxWindow::SetSize(-1, -1, width
, height
);
277 void wxDialog::SetTitle(const wxString
& title
)
279 wxTopLevelWindow::SetTitle( title
);
283 wxXmString
str( title
);
284 XtVaSetValues( (Widget
)m_mainWidget
,
285 XmNtitle
, title
.c_str(),
286 XmNdialogTitle
, str(), // Roberto Cocchi
287 XmNiconName
, title
.c_str(),
292 bool wxDialog::Show( bool show
)
294 if( !wxWindowBase::Show( show
) )
301 if (!wxUSE_INVISIBLE_RESIZE
)
302 XtMapWidget(XtParent((Widget
) m_mainWidget
));
304 XtManageChild((Widget
)m_mainWidget
) ;
306 XRaiseWindow( XtDisplay( (Widget
)m_mainWidget
),
307 XtWindow( (Widget
)m_mainWidget
) );
312 if (!wxUSE_INVISIBLE_RESIZE
)
313 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
315 XtUnmanageChild((Widget
)m_mainWidget
) ;
317 XFlush(XtDisplay((Widget
)m_mainWidget
));
318 XSync(XtDisplay((Widget
)m_mainWidget
), FALSE
);
324 // Shows a dialog modally, returning a return code
325 int wxDialog::ShowModal()
327 m_windowStyle
|= wxDIALOG_MODAL
;
331 // after the event loop ran, the widget might already have been destroyed
332 WXDisplay
* display
= (WXDisplay
*)XtDisplay( (Widget
)m_mainWidget
);
336 m_eventLoop
= new wxEventLoop
;
338 m_modalShowing
= TRUE
;
339 XtAddGrab((Widget
) m_mainWidget
, TRUE
, FALSE
);
343 // Now process all events in case they get sent to a destroyed dialog
344 wxFlushEvents( display
);
349 // TODO: is it safe to call this, if the dialog may have been deleted
350 // by now? Probably only if we're using delayed deletion of dialogs.
351 return GetReturnCode();
354 void wxDialog::EndModal(int retCode
)
359 SetReturnCode(retCode
);
361 // Strangely, we don't seem to need this now.
362 // XtRemoveGrab((Widget) m_mainWidget);
366 m_modalShowing
= FALSE
;
371 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
373 if ( Validate() && TransferDataFromWindow() )
379 SetReturnCode(wxID_OK
);
385 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
388 TransferDataFromWindow();
389 // TODO probably need to disable the Apply button until things change again
392 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
395 EndModal(wxID_CANCEL
);
398 SetReturnCode(wxID_CANCEL
);
403 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
405 // We'll send a Cancel message by default,
406 // which may close the dialog.
407 // Check for looping if the Cancel event handler calls Close().
409 // Note that if a cancel button and handler aren't present in the dialog,
410 // nothing will happen when you close the dialog via the window manager, or
412 // We wouldn't want to destroy the dialog by default, since the dialog may have been
413 // created on the stack.
414 // However, this does mean that calling dialog->Close() won't delete the dialog
415 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
416 // sure to destroy the dialog.
417 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
419 static wxList closing
;
421 if ( closing
.Member(this) )
424 closing
.Append(this);
426 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
427 cancelEvent
.SetEventObject( this );
428 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
430 closing
.DeleteObject(this);
433 // Destroy the window (delayed, if a managed window)
434 bool wxDialog::Destroy()
436 if (!wxPendingDelete
.Member(this))
437 wxPendingDelete
.Append(this);
441 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
443 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
447 void wxDialog::ChangeFont(bool keepOriginalSize
)
449 wxWindow::ChangeFont(keepOriginalSize
);
452 void wxDialog::ChangeBackgroundColour()
455 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
458 void wxDialog::ChangeForegroundColour()
461 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);