]> git.saurik.com Git - wxWidgets.git/blob - src/motif/dialog.cpp
Updating with Vadim's API changes.
[wxWidgets.git] / src / motif / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __VMS
16 #define XtDisplay XTDISPLAY
17 #define XtWindow XTWINDOW
18 #define XtParent XTPARENT
19 #define XtScreen XTSCREEN
20 #endif
21
22 #include "wx/dialog.h"
23
24 #ifndef WX_PRECOMP
25 #include "wx/app.h"
26 #include "wx/utils.h"
27 #include "wx/settings.h"
28 #endif
29
30 #include "wx/evtloop.h"
31
32 #ifdef __VMS__
33 #pragma message disable nosimpint
34 #endif
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
54 #ifdef __VMS__
55 #pragma message enable nosimpint
56 #endif
57
58 #include "wx/motif/private.h"
59
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;
64
65 // Lists to keep track of windows, so we can disable/enable them
66 // for modal dialogs
67 wxList wxModalDialogs;
68 extern wxList wxModelessWindows; // Frames and modeless dialogs
69
70 #define wxUSE_INVISIBLE_RESIZE 1
71
72 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
73
74 wxDialog::wxDialog()
75 {
76 m_modalShowing = false;
77 m_eventLoop = NULL;
78 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
79 }
80
81 bool wxDialog::Create(wxWindow *parent, wxWindowID id,
82 const wxString& title,
83 const wxPoint& pos,
84 const wxSize& size,
85 long style,
86 const wxString& name)
87 {
88 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
89
90 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
91 name ) )
92 return false;
93
94 m_modalShowing = false;
95 m_eventLoop = NULL;
96
97 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
98 m_foregroundColour = *wxBLACK;
99
100 Widget dialogShell = (Widget) m_mainWidget;
101
102 SetTitle( title );
103
104 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
105 ChangeFont(false);
106
107 // Can't remember what this was about... but I think it's necessary.
108 #if wxUSE_INVISIBLE_RESIZE
109 if (pos.x > -1)
110 XtVaSetValues(dialogShell, XmNx, pos.x,
111 NULL);
112 if (pos.y > -1)
113 XtVaSetValues(dialogShell, XmNy, pos.y,
114 NULL);
115
116 if (size.x > -1)
117 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
118 if (size.y > -1)
119 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
120 #endif
121
122 // Positioning of the dialog doesn't work properly unless the dialog
123 // is managed, so we manage without mapping to the screen.
124 // To show, we map the shell (actually it's parent).
125 #if !wxUSE_INVISIBLE_RESIZE
126 Widget shell = XtParent(dialogShell) ;
127 XtVaSetValues(shell, XmNmappedWhenManaged, False, NULL);
128 #endif
129
130 #if !wxUSE_INVISIBLE_RESIZE
131 XtManageChild(dialogShell);
132 SetSize(pos.x, pos.y, size.x, size.y);
133 #endif
134
135 XtAddEventHandler(dialogShell,ExposureMask,False,
136 wxUniversalRepaintProc, (XtPointer) this);
137
138 ChangeBackgroundColour();
139
140 return true;
141 }
142
143 bool wxDialog::XmDoCreateTLW(wxWindow* parent,
144 wxWindowID WXUNUSED(id),
145 const wxString& WXUNUSED(title),
146 const wxPoint& WXUNUSED(pos),
147 const wxSize& WXUNUSED(size),
148 long WXUNUSED(style),
149 const wxString& name)
150 {
151 Widget parentWidget = (Widget) 0;
152 if( parent )
153 parentWidget = (Widget) parent->GetTopWidget();
154 if( !parent )
155 parentWidget = (Widget) wxTheApp->GetTopLevelWidget();
156
157 wxASSERT_MSG( (parentWidget != (Widget) 0),
158 "Could not find a suitable parent shell for dialog." );
159
160 Arg args[2];
161 XtSetArg (args[0], XmNdefaultPosition, False);
162 XtSetArg (args[1], XmNautoUnmanage, False);
163 Widget dialogShell =
164 XmCreateBulletinBoardDialog( parentWidget,
165 wxConstCast(name.c_str(), char),
166 args, 2);
167 m_mainWidget = (WXWidget) dialogShell;
168
169 // We don't want margins, since there is enough elsewhere.
170 XtVaSetValues( dialogShell,
171 XmNmarginHeight, 0,
172 XmNmarginWidth, 0,
173 XmNresizePolicy, XmRESIZE_NONE,
174 NULL ) ;
175
176 XtTranslations ptr ;
177 XtOverrideTranslations(dialogShell,
178 ptr = XtParseTranslationTable("<Configure>: resize()"));
179 XtFree((char *)ptr);
180
181 XtRealizeWidget(dialogShell);
182
183 wxAddWindowToTable( (Widget)m_mainWidget, this );
184
185 return true;
186 }
187
188 void wxDialog::SetModal(bool flag)
189 {
190 if ( flag )
191 wxModelessWindows.DeleteObject(this);
192 else
193 wxModelessWindows.Append(this);
194 }
195
196 wxDialog::~wxDialog()
197 {
198 m_isBeingDeleted = true;
199
200 delete m_eventLoop;
201
202 if (m_mainWidget)
203 {
204 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, False,
205 wxUniversalRepaintProc, (XtPointer) this);
206 }
207
208 m_modalShowing = false;
209
210 #if !wxUSE_INVISIBLE_RESIZE
211 if (m_mainWidget)
212 {
213 XtUnmapWidget((Widget) m_mainWidget);
214 }
215 #endif
216
217 PreDestroy();
218
219 if ( m_mainWidget )
220 {
221 wxDeleteWindowFromTable( (Widget)m_mainWidget );
222 XtDestroyWidget( (Widget)m_mainWidget );
223 }
224 }
225
226 void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
227 {
228 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
229 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
230 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
231 }
232
233 void wxDialog::DoSetClientSize(int width, int height)
234 {
235 wxWindow::SetSize(-1, -1, width, height);
236 }
237
238 void wxDialog::SetTitle(const wxString& title)
239 {
240 wxTopLevelWindow::SetTitle( title );
241
242 if( !title.empty() )
243 {
244 wxXmString str( title );
245 XtVaSetValues( (Widget)m_mainWidget,
246 XmNtitle, title.c_str(),
247 XmNdialogTitle, str(), // Roberto Cocchi
248 XmNiconName, title.c_str(),
249 NULL );
250 }
251 }
252
253 bool wxDialog::Show( bool show )
254 {
255 if( !wxWindowBase::Show( show ) )
256 return false;
257
258 m_isShown = show;
259
260 if (show)
261 {
262 // this usually will result in TransferDataToWindow() being called
263 // which will change the controls values so do it before showing as
264 // otherwise we could have some flicker
265 InitDialog();
266 }
267
268 if (show)
269 {
270 #if !wxUSE_INVISIBLE_RESIZE
271 XtMapWidget(XtParent((Widget) m_mainWidget));
272 #else
273 XtManageChild((Widget)m_mainWidget) ;
274 #endif
275
276 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
277 XtWindow( (Widget)m_mainWidget) );
278
279 }
280 else
281 {
282 #if !wxUSE_INVISIBLE_RESIZE
283 XtUnmapWidget(XtParent((Widget) m_mainWidget));
284 #else
285 XtUnmanageChild((Widget)m_mainWidget) ;
286 #endif
287
288 XFlush(XtDisplay((Widget)m_mainWidget));
289 XSync(XtDisplay((Widget)m_mainWidget), False);
290 }
291
292 return true;
293 }
294
295 // Shows a dialog modally, returning a return code
296 int wxDialog::ShowModal()
297 {
298 Show(true);
299
300 // after the event loop ran, the widget might already have been destroyed
301 WXDisplay* display = (WXDisplay*)XtDisplay( (Widget)m_mainWidget );
302
303 if (m_modalShowing)
304 return 0;
305 m_eventLoop = new wxEventLoop;
306
307 m_modalShowing = true;
308 XtAddGrab((Widget) m_mainWidget, True, False);
309
310 m_eventLoop->Run();
311
312 // Now process all events in case they get sent to a destroyed dialog
313 wxFlushEvents( display );
314
315 delete m_eventLoop;
316 m_eventLoop = NULL;
317
318 // TODO: is it safe to call this, if the dialog may have been deleted
319 // by now? Probably only if we're using delayed deletion of dialogs.
320 return GetReturnCode();
321 }
322
323 void wxDialog::EndModal(int retCode)
324 {
325 if (!m_modalShowing)
326 return;
327
328 SetReturnCode(retCode);
329
330 // Strangely, we don't seem to need this now.
331 // XtRemoveGrab((Widget) m_mainWidget);
332
333 Show(false);
334
335 m_modalShowing = false;
336 m_eventLoop->Exit();
337
338 SetModal(false);
339 }
340
341 // Destroy the window (delayed, if a managed window)
342 bool wxDialog::Destroy()
343 {
344 if (!wxPendingDelete.Member(this))
345 wxPendingDelete.Append(this);
346 return true;
347 }
348
349 void wxDialog::ChangeFont(bool keepOriginalSize)
350 {
351 wxWindow::ChangeFont(keepOriginalSize);
352 }
353
354 void wxDialog::ChangeBackgroundColour()
355 {
356 if (GetMainWidget())
357 wxDoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
358 }
359
360 void wxDialog::ChangeForegroundColour()
361 {
362 if (GetMainWidget())
363 wxDoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
364 }