]> git.saurik.com Git - wxWidgets.git/blame - src/motif/dialog.cpp
Unicode compilation fixes after wxFFile change
[wxWidgets.git] / src / motif / dialog.cpp
CommitLineData
4bb6408c
JS
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
dfe1eee3 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "dialog.h"
14#endif
15
bcd055ae
JJ
16#ifdef __VMS
17#define XtDisplay XTDISPLAY
18#define XtWindow XTWINDOW
19#define XtParent XTPARENT
20#define XtScreen XTSCREEN
21#endif
22
4bb6408c
JS
23#include "wx/dialog.h"
24#include "wx/utils.h"
4bb6408c
JS
25#include "wx/app.h"
26#include "wx/settings.h"
27
338dd992
JJ
28#ifdef __VMS__
29#pragma message disable nosimpint
30#endif
dfc54541
JS
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
338dd992
JJ
50#ifdef __VMS__
51#pragma message enable nosimpint
52#endif
dfc54541
JS
53
54#include "wx/motif/private.h"
55
dfc54541
JS
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.
59static wxList wxModalShowingStack;
60
4bb6408c
JS
61// Lists to keep track of windows, so we can disable/enable them
62// for modal dialogs
63wxList wxModalDialogs;
798a4529 64extern wxList wxModelessWindows; // Frames and modeless dialogs
4bb6408c
JS
65extern wxList wxPendingDelete;
66
47d67540 67#define wxUSE_INVISIBLE_RESIZE 1
dfc54541 68
798a4529 69IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
4bb6408c 70
798a4529 71BEGIN_EVENT_TABLE(wxDialog, wxTopLevelWindow)
8e877c19
RR
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)
4bb6408c
JS
78END_EVENT_TABLE()
79
4bb6408c
JS
80
81wxDialog::wxDialog()
82{
dfc54541 83 m_modalShowing = FALSE;
a756f210 84 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
4bb6408c
JS
85}
86
87bool wxDialog::Create(wxWindow *parent, wxWindowID id,
2d120f83
JS
88 const wxString& title,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxString& name)
4bb6408c 93{
798a4529
MB
94 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
95
96 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
97 name ) )
98 return FALSE;
99
dfc54541 100 m_modalShowing = FALSE;
dfe1eee3 101
a756f210 102 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
0d57be45 103 m_foregroundColour = *wxBLACK;
dfe1eee3 104
798a4529 105 Widget dialogShell = (Widget) m_mainWidget;
dfc54541 106 Widget shell = XtParent(dialogShell) ;
798a4529
MB
107
108 SetTitle( title );
dfe1eee3 109
a756f210 110 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
4b5f3fe6 111 ChangeFont(FALSE);
dfe1eee3 112
dfc54541 113 // Can't remember what this was about... but I think it's necessary.
dfad0599 114 if (wxUSE_INVISIBLE_RESIZE)
dfc54541 115 {
2d120f83
JS
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);
dfe1eee3 122
2d120f83
JS
123 if (size.x > -1)
124 XtVaSetValues(dialogShell, XmNwidth, size.x, NULL);
125 if (size.y > -1)
126 XtVaSetValues(dialogShell, XmNheight, size.y, NULL);
dfc54541 127 }
dfe1eee3 128
dfc54541
JS
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).
dfad0599 132 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541 133 XtVaSetValues(shell, XmNmappedWhenManaged, FALSE, NULL);
dfe1eee3 134
dfad0599 135 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
136 {
137 XtManageChild(dialogShell);
138 SetSize(pos.x, pos.y, size.x, size.y);
139 }
140 XtAddEventHandler(dialogShell,ExposureMask,FALSE,
2e35f56f 141 wxUniversalRepaintProc, (XtPointer) this);
dfe1eee3 142
0d57be45 143 ChangeBackgroundColour();
dfe1eee3 144
dfc54541 145 return TRUE;
4bb6408c
JS
146}
147
798a4529
MB
148bool 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
4bb6408c
JS
191void wxDialog::SetModal(bool flag)
192{
dfc54541
JS
193 if ( flag )
194 m_windowStyle |= wxDIALOG_MODAL ;
195 else
196 if ( m_windowStyle & wxDIALOG_MODAL )
2d120f83 197 m_windowStyle -= wxDIALOG_MODAL ;
dfe1eee3 198
2d120f83
JS
199 wxModelessWindows.DeleteObject(this);
200 if (!flag)
201 wxModelessWindows.Append(this);
4bb6408c
JS
202}
203
204wxDialog::~wxDialog()
205{
3ab377bd 206 m_isBeingDeleted = TRUE;
798a4529 207
2e35f56f 208 if (m_mainWidget)
798a4529
MB
209 {
210 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask, FALSE,
211 wxUniversalRepaintProc, (XtPointer) this);
212 }
dfe1eee3 213
dfc54541 214 m_modalShowing = FALSE;
dfad0599 215 if (!wxUSE_INVISIBLE_RESIZE && m_mainWidget)
dfc54541 216 {
2d120f83 217 XtUnmapWidget((Widget) m_mainWidget);
dfc54541 218 }
798a4529 219}
dfe1eee3 220
798a4529
MB
221void wxDialog::DoDestroy()
222{
223 if( m_mainWidget )
cba2db0c 224 {
798a4529
MB
225 wxDeleteWindowFromTable( (Widget)m_mainWidget );
226 XtDestroyWidget( (Widget)m_mainWidget );
dfc54541 227 }
4bb6408c
JS
228}
229
230// By default, pressing escape cancels the dialog
231void wxDialog::OnCharHook(wxKeyEvent& event)
232{
2d120f83
JS
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);
dfe1eee3 240
2d120f83
JS
241 return;
242 }
243 // We didn't process this event.
244 event.Skip();
4bb6408c
JS
245}
246
bfc6fde4 247void wxDialog::DoSetSize(int x, int y, int width, int height, int sizeFlags)
4bb6408c 248{
dfc54541 249 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_ANY, NULL);
bfc6fde4 250 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
dfc54541 251 XtVaSetValues((Widget) m_mainWidget, XmNresizePolicy, XmRESIZE_NONE, NULL);
4bb6408c
JS
252}
253
ad813b00
JS
254void wxDialog::DoSetClientSize(int width, int height)
255{
256 wxWindow::SetSize(-1, -1, width, height);
257}
258
4bb6408c
JS
259void wxDialog::SetTitle(const wxString& title)
260{
798a4529 261 wxTopLevelWindow::SetTitle( title );
4bb6408c 262
798a4529
MB
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 );
dfc54541 271 }
dfc54541
JS
272}
273
798a4529 274bool wxDialog::Show( bool show )
dfc54541 275{
798a4529
MB
276 if( !wxTopLevelWindowMotif::Show( show ) )
277 return FALSE;
dfc54541 278
dfc54541 279 m_isShown = show;
dfe1eee3 280
dfc54541
JS
281 if (show)
282 {
dfad0599 283 if (!wxUSE_INVISIBLE_RESIZE)
2d120f83 284 XtMapWidget(XtParent((Widget) m_mainWidget));
dfc54541 285 else
798a4529 286 XtManageChild((Widget)m_mainWidget) ;
dfe1eee3 287
798a4529
MB
288 XRaiseWindow( XtDisplay( (Widget)m_mainWidget ),
289 XtWindow( (Widget)m_mainWidget) );
dfe1eee3 290
dfc54541
JS
291 }
292 else
293 {
dfad0599 294 if (!wxUSE_INVISIBLE_RESIZE)
dfc54541
JS
295 XtUnmapWidget(XtParent((Widget) m_mainWidget));
296 else
798a4529 297 XtUnmanageChild((Widget)m_mainWidget) ;
dfe1eee3 298
2d120f83
JS
299 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
300 XSync(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()), FALSE);
dfc54541 301 }
dfe1eee3 302
dfc54541
JS
303 return TRUE;
304}
305
306// Shows a dialog modally, returning a return code
4bb6408c
JS
307int wxDialog::ShowModal()
308{
309 m_windowStyle |= wxDIALOG_MODAL;
dfe1eee3 310
dfc54541 311 Show(TRUE);
dfe1eee3 312
dfc54541
JS
313 if (m_modalShowing)
314 return 0;
dfe1eee3 315
dfc54541 316 wxModalShowingStack.Insert((wxObject *)TRUE);
dfe1eee3 317
dfc54541 318 m_modalShowing = TRUE;
793f619f 319 XtAddGrab((Widget) m_mainWidget, TRUE, FALSE);
dfe1eee3 320
dfc54541 321 XEvent event;
dfe1eee3 322
dfc54541 323 // Loop until we signal that the dialog should be closed
5dcf05ae 324 while ((wxModalShowingStack.Number() > 0) && ((int)(wxModalShowingStack.First()->Data()) != 0))
dfc54541 325 {
2d120f83 326 // XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMAll);
dfe1eee3 327
8aa04e8b
JS
328 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
329 wxTheApp->ProcessXEvent((WXEvent*) &event);
f6bcfd97
BP
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 }
dfc54541 344 }
dfe1eee3 345
dfc54541
JS
346 // Remove modal dialog flag from stack
347 wxNode *node = wxModalShowingStack.First();
348 if (node)
2d120f83 349 delete node;
dfe1eee3 350
dfc54541
JS
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 {
3d870472 355 XFlush(XtDisplay((Widget) wxTheApp->GetTopLevelWidget()));
dfc54541 356 XtAppNextEvent((XtAppContext) wxTheApp->GetAppContext(), &event);
dfe1eee3 357
8aa04e8b 358 wxTheApp->ProcessXEvent((WXEvent*) &event);
dfc54541 359 }
dfe1eee3 360
dfc54541
JS
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();
4bb6408c
JS
364}
365
366void wxDialog::EndModal(int retCode)
367{
dfc54541
JS
368 if (!m_modalShowing)
369 return;
dfe1eee3 370
dfc54541 371 SetReturnCode(retCode);
dfe1eee3 372
88150e60
JS
373 // Strangely, we don't seem to need this now.
374 // XtRemoveGrab((Widget) m_mainWidget);
dfe1eee3 375
dfc54541 376 Show(FALSE);
dfe1eee3 377
dfc54541 378 m_modalShowing = FALSE;
dfe1eee3 379
dfc54541
JS
380 wxNode *node = wxModalShowingStack.First();
381 if (node)
2d120f83 382 node->SetData((wxObject *)FALSE);
4bb6408c
JS
383}
384
385// Standard buttons
f9e02ac7 386void wxDialog::OnOK(wxCommandEvent& WXUNUSED(event))
4bb6408c 387{
2d120f83
JS
388 if ( Validate() && TransferDataFromWindow() )
389 {
4bb6408c
JS
390 if ( IsModal() )
391 EndModal(wxID_OK);
392 else
393 {
2d120f83
JS
394 SetReturnCode(wxID_OK);
395 this->Show(FALSE);
4bb6408c 396 }
2d120f83 397 }
4bb6408c
JS
398}
399
f9e02ac7 400void wxDialog::OnApply(wxCommandEvent& WXUNUSED(event))
4bb6408c 401{
2d120f83
JS
402 if (Validate())
403 TransferDataFromWindow();
404 // TODO probably need to disable the Apply button until things change again
4bb6408c
JS
405}
406
f9e02ac7 407void wxDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
4bb6408c
JS
408{
409 if ( IsModal() )
410 EndModal(wxID_CANCEL);
411 else
412 {
413 SetReturnCode(wxID_CANCEL);
2d120f83 414 this->Show(FALSE);
4bb6408c
JS
415 }
416}
417
af111fc3 418void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
4bb6408c 419{
e3065973 420 // We'll send a Cancel message by default,
4bb6408c 421 // which may close the dialog.
e3065973
JS
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
4bb6408c 434 static wxList closing;
dfe1eee3 435
4bb6408c 436 if ( closing.Member(this) )
e3065973 437 return;
dfe1eee3 438
4bb6408c 439 closing.Append(this);
dfe1eee3 440
2d120f83
JS
441 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
442 cancelEvent.SetEventObject( this );
e3065973 443 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
4bb6408c 444
e3065973 445 closing.DeleteObject(this);
4bb6408c
JS
446}
447
448// Destroy the window (delayed, if a managed window)
449bool wxDialog::Destroy()
450{
2d120f83
JS
451 if (!wxPendingDelete.Member(this))
452 wxPendingDelete.Append(this);
453 return TRUE;
4bb6408c
JS
454}
455
f9e02ac7 456void wxDialog::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
4bb6408c 457{
a756f210 458 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
2d120f83 459 Refresh();
4bb6408c
JS
460}
461
94b49b93 462void wxDialog::ChangeFont(bool keepOriginalSize)
0d57be45 463{
94b49b93 464 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
465}
466
467void wxDialog::ChangeBackgroundColour()
468{
94b49b93
JS
469 if (GetMainWidget())
470 DoChangeBackgroundColour(GetMainWidget(), m_backgroundColour);
0d57be45
JS
471}
472
473void wxDialog::ChangeForegroundColour()
474{
94b49b93
JS
475 if (GetMainWidget())
476 DoChangeForegroundColour(GetMainWidget(), m_foregroundColour);
0d57be45 477}