1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/dialog.cpp
3 // Purpose: wxDialog class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #define XtDisplay XTDISPLAY
17 #define XtWindow XTWINDOW
18 #define XtParent XTPARENT
19 #define XtScreen XTSCREEN
22 #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
;
115 m_font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
118 // Can't remember what this was about... but I think it's necessary.
119 #if wxUSE_INVISIBLE_RESIZE
121 XtVaSetValues(dialogShell
, XmNx
, pos
.x
,
124 XtVaSetValues(dialogShell
, XmNy
, pos
.y
,
128 XtVaSetValues(dialogShell
, XmNwidth
, size
.x
, NULL
);
130 XtVaSetValues(dialogShell
, XmNheight
, size
.y
, NULL
);
133 // Positioning of the dialog doesn't work properly unless the dialog
134 // is managed, so we manage without mapping to the screen.
135 // To show, we map the shell (actually it's parent).
136 #if !wxUSE_INVISIBLE_RESIZE
137 Widget shell
= XtParent(dialogShell
) ;
138 XtVaSetValues(shell
, XmNmappedWhenManaged
, False
, NULL
);
141 #if !wxUSE_INVISIBLE_RESIZE
142 XtManageChild(dialogShell
);
143 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
146 XtAddEventHandler(dialogShell
,ExposureMask
,False
,
147 wxUniversalRepaintProc
, (XtPointer
) this);
149 ChangeBackgroundColour();
154 bool wxDialog::XmDoCreateTLW(wxWindow
* parent
,
155 wxWindowID
WXUNUSED(id
),
156 const wxString
& WXUNUSED(title
),
157 const wxPoint
& WXUNUSED(pos
),
158 const wxSize
& WXUNUSED(size
),
159 long WXUNUSED(style
),
160 const wxString
& name
)
162 Widget parentWidget
= (Widget
) 0;
164 parentWidget
= (Widget
) parent
->GetTopWidget();
166 parentWidget
= (Widget
) wxTheApp
->GetTopLevelWidget();
168 wxASSERT_MSG( (parentWidget
!= (Widget
) 0),
169 "Could not find a suitable parent shell for dialog." );
172 XtSetArg (args
[0], XmNdefaultPosition
, False
);
173 XtSetArg (args
[1], XmNautoUnmanage
, False
);
175 XmCreateBulletinBoardDialog( parentWidget
,
176 wxConstCast(name
.c_str(), char),
178 m_mainWidget
= (WXWidget
) dialogShell
;
180 // We don't want margins, since there is enough elsewhere.
181 XtVaSetValues( dialogShell
,
184 XmNresizePolicy
, XmRESIZE_NONE
,
188 XtOverrideTranslations(dialogShell
,
189 ptr
= XtParseTranslationTable("<Configure>: resize()"));
192 XtRealizeWidget(dialogShell
);
194 wxAddWindowToTable( (Widget
)m_mainWidget
, this );
199 void wxDialog::SetModal(bool flag
)
202 wxModelessWindows
.DeleteObject(this);
204 wxModelessWindows
.Append(this);
207 wxDialog::~wxDialog()
209 m_isBeingDeleted
= true;
215 XtRemoveEventHandler((Widget
) m_mainWidget
, ExposureMask
, False
,
216 wxUniversalRepaintProc
, (XtPointer
) this);
219 m_modalShowing
= false;
221 #if !wxUSE_INVISIBLE_RESIZE
224 XtUnmapWidget((Widget
) m_mainWidget
);
232 wxDeleteWindowFromTable( (Widget
)m_mainWidget
);
233 XtDestroyWidget( (Widget
)m_mainWidget
);
237 // By default, pressing escape cancels the dialog
238 void wxDialog::OnCharHook(wxKeyEvent
& event
)
240 if (event
.m_keyCode
== WXK_ESCAPE
)
242 // Behaviour changed in 2.0: we'll send a Cancel message
243 // to the dialog instead of Close.
244 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
245 cancelEvent
.SetEventObject( this );
246 GetEventHandler()->ProcessEvent(cancelEvent
);
250 // We didn't process this event.
254 void wxDialog::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
256 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_ANY
, NULL
);
257 wxWindow::DoSetSize(x
, y
, width
, height
, sizeFlags
);
258 XtVaSetValues((Widget
) m_mainWidget
, XmNresizePolicy
, XmRESIZE_NONE
, NULL
);
261 void wxDialog::DoSetClientSize(int width
, int height
)
263 wxWindow::SetSize(-1, -1, width
, height
);
266 void wxDialog::SetTitle(const wxString
& title
)
268 wxTopLevelWindow::SetTitle( title
);
272 wxXmString
str( title
);
273 XtVaSetValues( (Widget
)m_mainWidget
,
274 XmNtitle
, title
.c_str(),
275 XmNdialogTitle
, str(), // Roberto Cocchi
276 XmNiconName
, title
.c_str(),
281 bool wxDialog::Show( bool show
)
283 if( !wxWindowBase::Show( show
) )
290 // this usually will result in TransferDataToWindow() being called
291 // which will change the controls values so do it before showing as
292 // otherwise we could have some flicker
298 #if !wxUSE_INVISIBLE_RESIZE
299 XtMapWidget(XtParent((Widget
) m_mainWidget
));
301 XtManageChild((Widget
)m_mainWidget
) ;
304 XRaiseWindow( XtDisplay( (Widget
)m_mainWidget
),
305 XtWindow( (Widget
)m_mainWidget
) );
310 #if !wxUSE_INVISIBLE_RESIZE
311 XtUnmapWidget(XtParent((Widget
) m_mainWidget
));
313 XtUnmanageChild((Widget
)m_mainWidget
) ;
316 XFlush(XtDisplay((Widget
)m_mainWidget
));
317 XSync(XtDisplay((Widget
)m_mainWidget
), False
);
323 // Shows a dialog modally, returning a return code
324 int wxDialog::ShowModal()
328 // after the event loop ran, the widget might already have been destroyed
329 WXDisplay
* display
= (WXDisplay
*)XtDisplay( (Widget
)m_mainWidget
);
333 m_eventLoop
= new wxEventLoop
;
335 m_modalShowing
= true;
336 XtAddGrab((Widget
) m_mainWidget
, True
, False
);
340 // Now process all events in case they get sent to a destroyed dialog
341 wxFlushEvents( display
);
346 // TODO: is it safe to call this, if the dialog may have been deleted
347 // by now? Probably only if we're using delayed deletion of dialogs.
348 return GetReturnCode();
351 void wxDialog::EndModal(int retCode
)
356 SetReturnCode(retCode
);
358 // Strangely, we don't seem to need this now.
359 // XtRemoveGrab((Widget) m_mainWidget);
363 m_modalShowing
= false;
370 void wxDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
372 if ( Validate() && TransferDataFromWindow() )
378 SetReturnCode(wxID_OK
);
384 void wxDialog::OnApply(wxCommandEvent
& WXUNUSED(event
))
387 TransferDataFromWindow();
388 // TODO probably need to disable the Apply button until things change again
391 void wxDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
394 EndModal(wxID_CANCEL
);
397 SetReturnCode(wxID_CANCEL
);
402 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
404 // We'll send a Cancel message by default,
405 // which may close the dialog.
406 // Check for looping if the Cancel event handler calls Close().
408 // Note that if a cancel button and handler aren't present in the dialog,
409 // nothing will happen when you close the dialog via the window manager, or
411 // We wouldn't want to destroy the dialog by default, since the dialog may have been
412 // created on the stack.
413 // However, this does mean that calling dialog->Close() won't delete the dialog
414 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
415 // sure to destroy the dialog.
416 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
418 static wxList closing
;
420 if ( closing
.Member(this) )
423 closing
.Append(this);
425 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
426 cancelEvent
.SetEventObject( this );
427 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
429 closing
.DeleteObject(this);
432 // Destroy the window (delayed, if a managed window)
433 bool wxDialog::Destroy()
435 if (!wxPendingDelete
.Member(this))
436 wxPendingDelete
.Append(this);
440 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
442 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
446 void wxDialog::ChangeFont(bool keepOriginalSize
)
448 wxWindow::ChangeFont(keepOriginalSize
);
451 void wxDialog::ChangeBackgroundColour()
454 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour
);
457 void wxDialog::ChangeForegroundColour()
460 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour
);