]>
Commit | Line | Data |
---|---|---|
4bb6408c | 1 | ///////////////////////////////////////////////////////////////////////////// |
55034339 | 2 | // Name: src/motif/dialog.cpp |
4bb6408c JS |
3 | // Purpose: wxDialog class |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
1248b41f MB |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
bcd055ae JJ |
15 | #ifdef __VMS |
16 | #define XtDisplay XTDISPLAY | |
17 | #define XtWindow XTWINDOW | |
18 | #define XtParent XTPARENT | |
19 | #define XtScreen XTSCREEN | |
20 | #endif | |
21 | ||
4bb6408c | 22 | #include "wx/dialog.h" |
670f9935 WS |
23 | |
24 | #ifndef WX_PRECOMP | |
25 | #include "wx/app.h" | |
de6185e2 | 26 | #include "wx/utils.h" |
670f9935 WS |
27 | #endif |
28 | ||
4bb6408c | 29 | #include "wx/settings.h" |
7e1bcfa8 | 30 | #include "wx/evtloop.h" |
4bb6408c | 31 | |
338dd992 JJ |
32 | #ifdef __VMS__ |
33 | #pragma message disable nosimpint | |
34 | #endif | |
dfc54541 JS |
35 | #include <Xm/Xm.h> |
36 | ||
37 | #include <X11/Shell.h> | |
38 | #if XmVersion >= 1002 | |
39 | #include <Xm/XmAll.h> | |
40 | #endif | |
41 | #include <Xm/MwmUtil.h> | |
42 | #include <Xm/Label.h> | |
43 | #include <Xm/BulletinB.h> | |
44 | #include <Xm/Frame.h> | |
45 | #include <Xm/Text.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> | |
51 | #if XmVersion > 1000 | |
52 | #include <Xm/Protocols.h> | |
53 | #endif | |
338dd992 JJ |
54 | #ifdef __VMS__ |
55 | #pragma message enable nosimpint | |
56 | #endif | |
dfc54541 JS |
57 | |
58 | #include "wx/motif/private.h" | |
59 | ||
dfc54541 JS |
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. | |
7e1bcfa8 | 63 | // static wxList wxModalShowingStack; |
dfc54541 | 64 | |
4bb6408c JS |
65 | // Lists to keep track of windows, so we can disable/enable them |
66 | // for modal dialogs | |
67 | wxList wxModalDialogs; | |
798a4529 | 68 | extern wxList wxModelessWindows; // Frames and modeless dialogs |
4bb6408c JS |
69 | extern wxList wxPendingDelete; |
70 | ||
47d67540 | 71 | #define wxUSE_INVISIBLE_RESIZE 1 |
dfc54541 | 72 | |
798a4529 | 73 | IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow) |
4bb6408c | 74 | |
798a4529 | 75 | BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow) |
1c067fe3 WS |
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) | |
4bb6408c JS |
82 | END_EVENT_TABLE() |
83 | ||
4bb6408c JS |
84 | |
85 | wxDialog::wxDialog() | |
86 | { | |
96be256b | 87 | m_modalShowing = false; |
7e1bcfa8 | 88 | m_eventLoop = NULL; |
a756f210 | 89 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
4bb6408c JS |
90 | } |
91 | ||
92 | bool wxDialog::Create(wxWindow *parent, wxWindowID id, | |
2d120f83 JS |
93 | const wxString& title, |
94 | const wxPoint& pos, | |
95 | const wxSize& size, | |
96 | long style, | |
97 | const wxString& name) | |
4bb6408c | 98 | { |
798a4529 MB |
99 | SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG); |
100 | ||
101 | if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style, | |
102 | name ) ) | |
96be256b | 103 | return false; |
798a4529 | 104 | |
96be256b | 105 | m_modalShowing = false; |
7e1bcfa8 | 106 | m_eventLoop = NULL; |
dfe1eee3 | 107 | |
a756f210 | 108 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
0d57be45 | 109 | m_foregroundColour = *wxBLACK; |
dfe1eee3 | 110 | |
798a4529 | 111 | Widget dialogShell = (Widget) m_mainWidget; |
798a4529 MB |
112 | |
113 | SetTitle( title ); | |
dfe1eee3 | 114 | |
a756f210 | 115 | m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT); |
96be256b | 116 | ChangeFont(false); |
dfe1eee3 | 117 | |
dfc54541 | 118 | // Can't remember what this was about... but I think it's necessary. |
355b4d3d WS |
119 | #if wxUSE_INVISIBLE_RESIZE |
120 | if (pos.x > -1) | |
121 | XtVaSetValues(dialogShell, XmNx, pos.x, | |
122 | NULL); | |
123 | if (pos.y > -1) | |
124 | XtVaSetValues(dialogShell, XmNy, pos.y, | |
125 | NULL); | |
126 | ||
127 | if (size.x > -1) | |
128 | XtVaSetValues(dialogShell, XmNwidth, size.x, NULL); | |
129 | if (size.y > -1) | |
130 | XtVaSetValues(dialogShell, XmNheight, size.y, NULL); | |
131 | #endif | |
dfe1eee3 | 132 | |
dfc54541 JS |
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). | |
355b4d3d | 136 | #if !wxUSE_INVISIBLE_RESIZE |
31df756d | 137 | Widget shell = XtParent(dialogShell) ; |
355b4d3d WS |
138 | XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL); |
139 | #endif | |
140 | ||
141 | #if !wxUSE_INVISIBLE_RESIZE | |
142 | XtManageChild(dialogShell); | |
143 | SetSize(pos.x, pos.y, size.x, size.y); | |
144 | #endif | |
dfe1eee3 | 145 | |
96be256b | 146 | XtAddEventHandler(dialogShell,ExposureMask,False, |
2e35f56f | 147 | wxUniversalRepaintProc, (XtPointer) this); |
dfe1eee3 | 148 | |
0d57be45 | 149 | ChangeBackgroundColour(); |
dfe1eee3 | 150 | |
96be256b | 151 | return true; |
4bb6408c JS |
152 | } |
153 | ||
02bcd285 | 154 | bool wxDialog::XmDoCreateTLW(wxWindow* parent, |
355b4d3d WS |
155 | wxWindowID WXUNUSED(id), |
156 | const wxString& WXUNUSED(title), | |
157 | const wxPoint& WXUNUSED(pos), | |
158 | const wxSize& WXUNUSED(size), | |
159 | long WXUNUSED(style), | |
f58585c0 | 160 | const wxString& name) |
798a4529 MB |
161 | { |
162 | Widget parentWidget = (Widget) 0; | |
163 | if( parent ) | |
164 | parentWidget = (Widget) parent->GetTopWidget(); | |
165 | if( !parent ) | |
166 | parentWidget = (Widget) wxTheApp->GetTopLevelWidget(); | |
167 | ||
168 | wxASSERT_MSG( (parentWidget != (Widget) 0), | |
169 | "Could not find a suitable parent shell for dialog." ); | |
170 | ||
171 | Arg args[2]; | |
172 | XtSetArg (args[0], XmNdefaultPosition, False); | |
173 | XtSetArg (args[1], XmNautoUnmanage, False); | |
174 | Widget dialogShell = | |
d3a80c92 MB |
175 | XmCreateBulletinBoardDialog( parentWidget, |
176 | wxConstCast(name.c_str(), char), | |
798a4529 MB |
177 | args, 2); |
178 | m_mainWidget = (WXWidget) dialogShell; | |
179 | ||
180 | // We don't want margins, since there is enough elsewhere. | |
181 | XtVaSetValues( dialogShell, | |
182 | XmNmarginHeight, 0, | |
183 | XmNmarginWidth, 0, | |
184 | XmNresizePolicy, XmRESIZE_NONE, | |
185 | NULL ) ; | |
186 | ||
187 | XtTranslations ptr ; | |
188 | XtOverrideTranslations(dialogShell, | |
189 | ptr = XtParseTranslationTable("<Configure>: resize()")); | |
190 | XtFree((char *)ptr); | |
191 | ||
192 | XtRealizeWidget(dialogShell); | |
193 | ||
194 | wxAddWindowToTable( (Widget)m_mainWidget, this ); | |
195 | ||
96be256b | 196 | return true; |
798a4529 MB |
197 | } |
198 | ||
4bb6408c JS |
199 | void wxDialog::SetModal(bool flag) |
200 | { | |
5f9a153c | 201 | if ( flag ) |
31df756d VZ |
202 | wxModelessWindows.DeleteObject(this); |
203 | else | |
204 | wxModelessWindows.Append(this); | |
4bb6408c JS |
205 | } |
206 | ||
207 | wxDialog::~wxDialog() | |
208 | { | |
96be256b | 209 | m_isBeingDeleted = true; |
2187eef5 | 210 | |
7e1bcfa8 | 211 | delete m_eventLoop; |
798a4529 | 212 | |
2e35f56f | 213 | if (m_mainWidget) |
798a4529 | 214 | { |
96be256b | 215 | XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False, |
798a4529 MB |
216 | wxUniversalRepaintProc, (XtPointer) this); |
217 | } | |
dfe1eee3 | 218 | |
96be256b | 219 | m_modalShowing = false; |
355b4d3d WS |
220 | |
221 | #if !wxUSE_INVISIBLE_RESIZE | |
222 | if (m_mainWidget) | |
dfc54541 | 223 | { |
2d120f83 | 224 | XtUnmapWidget((Widget) m_mainWidget); |
dfc54541 | 225 | } |
355b4d3d | 226 | #endif |
2187eef5 MB |
227 | |
228 | PreDestroy(); | |
dfe1eee3 | 229 | |
f58585c0 | 230 | if ( m_mainWidget ) |
cba2db0c | 231 | { |
798a4529 MB |
232 | wxDeleteWindowFromTable( (Widget)m_mainWidget ); |
233 | XtDestroyWidget( (Widget)m_mainWidget ); | |
dfc54541 | 234 | } |
4bb6408c JS |
235 | } |
236 | ||
237 | // By default, pressing escape cancels the dialog | |
238 | void wxDialog::OnCharHook(wxKeyEvent& event) | |
239 | { | |
2d120f83 JS |
240 | if (event.m_keyCode == WXK_ESCAPE) |
241 | { | |
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); | |
dfe1eee3 | 247 | |
2d120f83 JS |
248 | return; |
249 | } | |
250 | // We didn't process this event. | |
251 | event.Skip(); | |
4bb6408c JS |
252 | } |
253 | ||
bfc6fde4 | 254 | void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
4bb6408c | 255 | { |
dfc54541 | 256 | XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL); |
bfc6fde4 | 257 | wxWindow::DoSetSize(x, y, width, height, sizeFlags); |
dfc54541 | 258 | XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL); |
4bb6408c JS |
259 | } |
260 | ||
ad813b00 JS |
261 | void wxDialog::DoSetClientSize(int width, int height) |
262 | { | |
263 | wxWindow::SetSize(-1, -1, width, height); | |
264 | } | |
265 | ||
4bb6408c JS |
266 | void wxDialog::SetTitle(const wxString& title) |
267 | { | |
798a4529 | 268 | wxTopLevelWindow::SetTitle( title ); |
4bb6408c | 269 | |
798a4529 MB |
270 | if( !title.empty() ) |
271 | { | |
272 | wxXmString str( title ); | |
273 | XtVaSetValues( (Widget)m_mainWidget, | |
274 | XmNtitle, title.c_str(), | |
275 | XmNdialogTitle, str(), // Roberto Cocchi | |
276 | XmNiconName, title.c_str(), | |
277 | NULL ); | |
dfc54541 | 278 | } |
dfc54541 JS |
279 | } |
280 | ||
798a4529 | 281 | bool wxDialog::Show( bool show ) |
dfc54541 | 282 | { |
5a2e3d8c MB |
283 | if( !wxWindowBase::Show( show ) ) |
284 | return false; | |
dfc54541 | 285 | |
dfc54541 | 286 | m_isShown = show; |
dfe1eee3 | 287 | |
b3090d48 MB |
288 | if (show) |
289 | { | |
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 | |
293 | InitDialog(); | |
294 | } | |
295 | ||
dfc54541 JS |
296 | if (show) |
297 | { | |
355b4d3d WS |
298 | #if !wxUSE_INVISIBLE_RESIZE |
299 | XtMapWidget(XtParent((Widget) m_mainWidget)); | |
300 | #else | |
301 | XtManageChild((Widget)m_mainWidget) ; | |
302 | #endif | |
dfe1eee3 | 303 | |
55034339 | 304 | XRaiseWindow( XtDisplay( (Widget)m_mainWidget ), |
798a4529 | 305 | XtWindow( (Widget)m_mainWidget) ); |
dfe1eee3 | 306 | |
dfc54541 JS |
307 | } |
308 | else | |
309 | { | |
355b4d3d WS |
310 | #if !wxUSE_INVISIBLE_RESIZE |
311 | XtUnmapWidget(XtParent((Widget) m_mainWidget)); | |
312 | #else | |
313 | XtUnmanageChild((Widget)m_mainWidget) ; | |
314 | #endif | |
dfe1eee3 | 315 | |
eb6fa4b4 | 316 | XFlush(XtDisplay((Widget)m_mainWidget)); |
96be256b | 317 | XSync(XtDisplay((Widget)m_mainWidget), False); |
dfc54541 | 318 | } |
dfe1eee3 | 319 | |
96be256b | 320 | return true; |
dfc54541 JS |
321 | } |
322 | ||
323 | // Shows a dialog modally, returning a return code | |
4bb6408c JS |
324 | int wxDialog::ShowModal() |
325 | { | |
96be256b | 326 | Show(true); |
dfe1eee3 | 327 | |
eb6fa4b4 MB |
328 | // after the event loop ran, the widget might already have been destroyed |
329 | WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget ); | |
330 | ||
dfc54541 JS |
331 | if (m_modalShowing) |
332 | return 0; | |
7e1bcfa8 | 333 | m_eventLoop = new wxEventLoop; |
dfe1eee3 | 334 | |
96be256b MB |
335 | m_modalShowing = true; |
336 | XtAddGrab((Widget) m_mainWidget, True, False); | |
dfe1eee3 | 337 | |
7e1bcfa8 | 338 | m_eventLoop->Run(); |
dfe1eee3 | 339 | |
dfc54541 | 340 | // Now process all events in case they get sent to a destroyed dialog |
eb6fa4b4 | 341 | wxFlushEvents( display ); |
dfe1eee3 | 342 | |
7e1bcfa8 MB |
343 | delete m_eventLoop; |
344 | m_eventLoop = NULL; | |
345 | ||
dfc54541 JS |
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(); | |
4bb6408c JS |
349 | } |
350 | ||
351 | void wxDialog::EndModal(int retCode) | |
352 | { | |
dfc54541 JS |
353 | if (!m_modalShowing) |
354 | return; | |
dfe1eee3 | 355 | |
dfc54541 | 356 | SetReturnCode(retCode); |
dfe1eee3 | 357 | |
88150e60 JS |
358 | // Strangely, we don't seem to need this now. |
359 | // XtRemoveGrab((Widget) m_mainWidget); | |
dfe1eee3 | 360 | |
96be256b | 361 | Show(false); |
dfe1eee3 | 362 | |
96be256b | 363 | m_modalShowing = false; |
7e1bcfa8 | 364 | m_eventLoop->Exit(); |
e95b8d5a JS |
365 | |
366 | SetModal(false); | |
4bb6408c JS |
367 | } |
368 | ||
369 | // Standard buttons | |
f9e02ac7 | 370 | void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
4bb6408c | 371 | { |
2d120f83 JS |
372 | if ( Validate() && TransferDataFromWindow() ) |
373 | { | |
4bb6408c JS |
374 | if ( IsModal() ) |
375 | EndModal(wxID_OK); | |
376 | else | |
377 | { | |
2d120f83 | 378 | SetReturnCode(wxID_OK); |
96be256b | 379 | this->Show(false); |
4bb6408c | 380 | } |
2d120f83 | 381 | } |
4bb6408c JS |
382 | } |
383 | ||
f9e02ac7 | 384 | void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event)) |
4bb6408c | 385 | { |
2d120f83 JS |
386 | if (Validate()) |
387 | TransferDataFromWindow(); | |
388 | // TODO probably need to disable the Apply button until things change again | |
4bb6408c JS |
389 | } |
390 | ||
f9e02ac7 | 391 | void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
4bb6408c JS |
392 | { |
393 | if ( IsModal() ) | |
394 | EndModal(wxID_CANCEL); | |
395 | else | |
396 | { | |
397 | SetReturnCode(wxID_CANCEL); | |
96be256b | 398 | this->Show(false); |
4bb6408c JS |
399 | } |
400 | } | |
401 | ||
af111fc3 | 402 | void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
4bb6408c | 403 | { |
e3065973 | 404 | // We'll send a Cancel message by default, |
4bb6408c | 405 | // which may close the dialog. |
e3065973 JS |
406 | // Check for looping if the Cancel event handler calls Close(). |
407 | ||
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 | |
410 | // via Close(). | |
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. | |
417 | ||
4bb6408c | 418 | static wxList closing; |
dfe1eee3 | 419 | |
4bb6408c | 420 | if ( closing.Member(this) ) |
e3065973 | 421 | return; |
dfe1eee3 | 422 | |
4bb6408c | 423 | closing.Append(this); |
dfe1eee3 | 424 | |
2d120f83 JS |
425 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
426 | cancelEvent.SetEventObject( this ); | |
e3065973 | 427 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog |
4bb6408c | 428 | |
e3065973 | 429 | closing.DeleteObject(this); |
4bb6408c JS |
430 | } |
431 | ||
432 | // Destroy the window (delayed, if a managed window) | |
433 | bool wxDialog::Destroy() | |
434 | { | |
2d120f83 JS |
435 | if (!wxPendingDelete.Member(this)) |
436 | wxPendingDelete.Append(this); | |
96be256b | 437 | return true; |
4bb6408c JS |
438 | } |
439 | ||
f9e02ac7 | 440 | void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
4bb6408c | 441 | { |
a756f210 | 442 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); |
2d120f83 | 443 | Refresh(); |
4bb6408c JS |
444 | } |
445 | ||
94b49b93 | 446 | void wxDialog::ChangeFont(bool keepOriginalSize) |
0d57be45 | 447 | { |
94b49b93 | 448 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
449 | } |
450 | ||
451 | void wxDialog::ChangeBackgroundColour() | |
452 | { | |
94b49b93 | 453 | if (GetMainWidget()) |
a8680e3e | 454 | wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour); |
0d57be45 JS |
455 | } |
456 | ||
457 | void wxDialog::ChangeForegroundColour() | |
458 | { | |
94b49b93 | 459 | if (GetMainWidget()) |
a8680e3e | 460 | wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour); |
0d57be45 | 461 | } |