]> git.saurik.com Git - wxWidgets.git/blob - src/motif/dialog.cpp
Font work.
[wxWidgets.git] / src / motif / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dialog.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #define XtWindow XTWINDOW
19 #define XtParent XTPARENT
20 #define XtScreen XTSCREEN
21 #endif
22
23 #include "wx/dialog.h"
24 #include "wx/utils.h"
25 #include "wx/app.h"
26 #include "wx/settings.h"
27
28 #ifdef __VMS__
29 #pragma message disable nosimpint
30 #endif
31 #include <Xm/Xm.h>
32
33 #include <X11/Shell.h>
34 #if XmVersion >= 1002
35 #include <Xm/XmAll.h>
36 #endif
37 #include <Xm/MwmUtil.h>
38 #include <Xm/Label.h>
39 #include <Xm/BulletinB.h>
40 #include <Xm/Frame.h>
41 #include <Xm/Text.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>
47 #if XmVersion > 1000
48 #include <Xm/Protocols.h>
49 #endif
50 #ifdef __VMS__
51 #pragma message enable nosimpint
52 #endif
53
54 #include "wx/motif/private.h"
55
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;
60
61 // Lists to keep track of windows, so we can disable/enable them
62 // for modal dialogs
63 wxList wxModalDialogs;
64 extern wxList wxModelessWindows; // Frames and modeless dialogs
65 extern wxList wxPendingDelete;
66
67 #define wxUSE_INVISIBLE_RESIZE 1
68
69 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
70
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)
78 END_EVENT_TABLE()
79
80
81 wxDialog::wxDialog()
82 {
83 m_modalShowing = FALSE;
84 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
85 }
86
87 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
88 const wxString& title,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
93 {
94 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
95
96 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
97 name ) )
98 return FALSE;
99
100 m_modalShowing = FALSE;
101
102 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
103 m_foregroundColour = *wxBLACK;
104
105 Widget dialogShell = (Widget) m_mainWidget;
106 Widget shell = XtParent(dialogShell) ;
107
108 SetTitle( title );
109
110 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
111 ChangeFont(FALSE);
112
113 // Can't remember what this was about... but I think it's necessary.
114 if (wxUSE_INVISIBLE_RESIZE)
115 {
116 if (pos.x > -1)
117 XtVaSetValues(dialogShell, XmNx, pos.x,
118 NULL);
119 if (pos.y > -1)
120 XtVaSetValues(dialogShell, XmNy, pos.y,
121 NULL);
122
123 if (size.x > -1)
124 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
125 if (size.y > -1)
126 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
127 }
128
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);
134
135 if (!wxUSE_INVISIBLE_RESIZE)
136 {
137 XtManageChild(dialogShell);
138 SetSize(pos.x, pos.y, size.x, size.y);
139 }
140 XtAddEventHandler(dialogShell,ExposureMask,FALSE,
141 wxUniversalRepaintProc, (XtPointer) this);
142
143 ChangeBackgroundColour();
144
145 return TRUE;
146 }
147
148 bool wxDialog::DoCreate( wxWindow* parent, wxWindowID id,
149 const wxString& title,
150 const wxPoint& pos,
151 const wxSize& size,
152 long style,
153 const wxString& name )
154 {
155 Widget parentWidget = (Widget) 0;
156 if( parent )
157 parentWidget = (Widget) parent->GetTopWidget();
158 if( !parent )
159 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
160
161 wxASSERT_MSG( (parentWidget != (Widget) 0),
162 "Could not find a suitable parent shell for dialog." );
163
164 Arg args[2];
165 XtSetArg (args[0], XmNdefaultPosition, False);
166 XtSetArg (args[1], XmNautoUnmanage, False);
167 Widget dialogShell =
168 XmCreateBulletinBoardDialog( parentWidget, (char*)name.c_str(),
169 args, 2);
170 m_mainWidget = (WXWidget) dialogShell;
171
172 // We don't want margins, since there is enough elsewhere.
173 XtVaSetValues( dialogShell,
174 XmNmarginHeight, 0,
175 XmNmarginWidth, 0,
176 XmNresizePolicy, XmRESIZE_NONE,
177 NULL ) ;
178
179 XtTranslations ptr ;
180 XtOverrideTranslations(dialogShell,
181 ptr = XtParseTranslationTable("<Configure>: resize()"));
182 XtFree((char *)ptr);
183
184 XtRealizeWidget(dialogShell);
185
186 wxAddWindowToTable( (Widget)m_mainWidget, this );
187
188 return TRUE;
189 }
190
191 void wxDialog::SetModal(bool flag)
192 {
193 if ( flag )
194 m_windowStyle |= wxDIALOG_MODAL ;
195 else
196 if ( m_windowStyle & wxDIALOG_MODAL )
197 m_windowStyle -= wxDIALOG_MODAL ;
198
199 wxModelessWindows.DeleteObject(this);
200 if (!flag)
201 wxModelessWindows.Append(this);
202 }
203
204 wxDialog::~wxDialog()
205 {
206 m_isBeingDeleted = TRUE;
207
208 if (m_mainWidget)
209 {
210 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, FALSE,
211 wxUniversalRepaintProc, (XtPointer) this);
212 }
213
214 m_modalShowing = FALSE;
215 if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget)
216 {
217 XtUnmapWidget((Widget) m_mainWidget);
218 }
219 }
220
221 void wxDialog::DoDestroy()
222 {
223 if( m_mainWidget )
224 {
225 wxDeleteWindowFromTable( (Widget)m_mainWidget );
226 XtDestroyWidget( (Widget)m_mainWidget );
227 }
228 }
229
230 // By default, pressing escape cancels the dialog
231 void wxDialog::OnCharHook(wxKeyEvent& event)
232 {
233 if (event.m_keyCode == WXK_ESCAPE)
234 {
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);
240
241 return;
242 }
243 // We didn't process this event.
244 event.Skip();
245 }
246
247 void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
248 {
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);
252 }
253
254 void wxDialog::DoSetClientSize(int width, int height)
255 {
256 wxWindow::SetSize(-1, -1, width, height);
257 }
258
259 void wxDialog::SetTitle(const wxString& title)
260 {
261 wxTopLevelWindow::SetTitle( title );
262
263 if( !title.empty() )
264 {
265 wxXmString str( title );
266 XtVaSetValues( (Widget)m_mainWidget,
267 XmNtitle, title.c_str(),
268 XmNdialogTitle, str(), // Roberto Cocchi
269 XmNiconName, title.c_str(),
270 NULL );
271 }
272 }
273
274 bool wxDialog::Show( bool show )
275 {
276 if( !wxTopLevelWindowMotif::Show( show ) )
277 return FALSE;
278
279 m_isShown = show;
280
281 if (show)
282 {
283 if (!wxUSE_INVISIBLE_RESIZE)
284 XtMapWidget(XtParent((Widget) m_mainWidget));
285 else
286 XtManageChild((Widget)m_mainWidget) ;
287
288 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
289 XtWindow( (Widget)m_mainWidget) );
290
291 }
292 else
293 {
294 if (!wxUSE_INVISIBLE_RESIZE)
295 XtUnmapWidget(XtParent((Widget) m_mainWidget));
296 else
297 XtUnmanageChild((Widget)m_mainWidget) ;
298
299 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
300 XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE);
301 }
302
303 return TRUE;
304 }
305
306 // Shows a dialog modally, returning a return code
307 int wxDialog::ShowModal()
308 {
309 m_windowStyle |= wxDIALOG_MODAL;
310
311 Show(TRUE);
312
313 if (m_modalShowing)
314 return 0;
315
316 wxModalShowingStack.Insert((wxObject *)TRUE);
317
318 m_modalShowing = TRUE;
319 XtAddGrab((Widget) m_mainWidget, TRUE, FALSE);
320
321 XEvent event;
322
323 // Loop until we signal that the dialog should be closed
324 while ((wxModalShowingStack.Number() > 0) && ((int)(wxModalShowingStack.First()->Data()) != 0))
325 {
326 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
327
328 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
329 wxTheApp->ProcessXEvent((WXEvent*) &event);
330
331 if (XtAppPending( (XtAppContext) wxTheApp->GetAppContext() ) == 0)
332 {
333 if (!wxTheApp->ProcessIdle())
334 {
335 #if wxUSE_THREADS
336 // leave the main loop to give other threads a chance to
337 // perform their GUI work
338 wxMutexGuiLeave();
339 wxUsleep(20);
340 wxMutexGuiEnter();
341 #endif
342 }
343 }
344 }
345
346 // Remove modal dialog flag from stack
347 wxNode *node = wxModalShowingStack.First();
348 if (node)
349 delete node;
350
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()))
354 {
355 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
356 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
357
358 wxTheApp->ProcessXEvent((WXEvent*) &event);
359 }
360
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();
364 }
365
366 void wxDialog::EndModal(int retCode)
367 {
368 if (!m_modalShowing)
369 return;
370
371 SetReturnCode(retCode);
372
373 // Strangely, we don't seem to need this now.
374 // XtRemoveGrab((Widget) m_mainWidget);
375
376 Show(FALSE);
377
378 m_modalShowing = FALSE;
379
380 wxNode *node = wxModalShowingStack.First();
381 if (node)
382 node->SetData((wxObject *)FALSE);
383 }
384
385 // Standard buttons
386 void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
387 {
388 if ( Validate() && TransferDataFromWindow() )
389 {
390 if ( IsModal() )
391 EndModal(wxID_OK);
392 else
393 {
394 SetReturnCode(wxID_OK);
395 this->Show(FALSE);
396 }
397 }
398 }
399
400 void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
401 {
402 if (Validate())
403 TransferDataFromWindow();
404 // TODO probably need to disable the Apply button until things change again
405 }
406
407 void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
408 {
409 if ( IsModal() )
410 EndModal(wxID_CANCEL);
411 else
412 {
413 SetReturnCode(wxID_CANCEL);
414 this->Show(FALSE);
415 }
416 }
417
418 void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
419 {
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().
423
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
426 // via Close().
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.
433
434 static wxList closing;
435
436 if ( closing.Member(this) )
437 return;
438
439 closing.Append(this);
440
441 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
442 cancelEvent.SetEventObject( this );
443 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
444
445 closing.DeleteObject(this);
446 }
447
448 // Destroy the window (delayed, if a managed window)
449 bool wxDialog::Destroy()
450 {
451 if (!wxPendingDelete.Member(this))
452 wxPendingDelete.Append(this);
453 return TRUE;
454 }
455
456 void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
457 {
458 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
459 Refresh();
460 }
461
462 void wxDialog::ChangeFont(bool keepOriginalSize)
463 {
464 wxWindow::ChangeFont(keepOriginalSize);
465 }
466
467 void wxDialog::ChangeBackgroundColour()
468 {
469 if (GetMainWidget())
470 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
471 }
472
473 void wxDialog::ChangeForegroundColour()
474 {
475 if (GetMainWidget())
476 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
477 }