]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/dialog.cpp
Revised #ifndef WX_PRECOMP headers, added missing #include wx/wxcrtvararg.h
[wxWidgets.git] / src / gtk1 / dialog.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/gtk1/dialog.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
a81258be 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/dialog.h"
670f9935
WS
14
15#ifndef WX_PRECOMP
16 #include "wx/app.h"
76b49cf4 17 #include "wx/frame.h"
c8326d64 18 #include "wx/cursor.h"
670f9935
WS
19#endif // WX_PRECOMP
20
924b84ab 21#include "wx/evtloop.h"
83624f79 22
071a2d78
RR
23#include <gdk/gdk.h>
24#include <gtk/gtk.h>
25#include <gdk/gdkkeysyms.h>
26
3cbab641 27#include "wx/gtk1/win_gtk.h"
5e014a0c 28
acfd422a 29//-----------------------------------------------------------------------------
91af0895 30// global data
acfd422a
RR
31//-----------------------------------------------------------------------------
32
2d68e1b4 33extern int g_openDialogs;
acfd422a 34
c801d85f
KB
35//-----------------------------------------------------------------------------
36// wxDialog
37//-----------------------------------------------------------------------------
38
7d9f12f3 39BEGIN_EVENT_TABLE(wxDialog,wxDialogBase)
fb1585ae
RR
40 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
41 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
42 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
43 EVT_CLOSE (wxDialog::OnCloseWindow)
c801d85f
KB
44END_EVENT_TABLE()
45
7d9f12f3 46IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxTopLevelWindow)
c801d85f 47
68995f26 48void wxDialog::Init()
c801d85f 49{
f03fc89f 50 m_returnCode = 0;
91af0895
WS
51 m_sizeSet = false;
52 m_modalShowing = false;
53 m_themeEnabled = true;
c33c4050 54}
c801d85f 55
2b854a32 56wxDialog::wxDialog( wxWindow *parent,
fb1585ae 57 wxWindowID id, const wxString &title,
2b854a32 58 const wxPoint &pos, const wxSize &size,
fb1585ae 59 long style, const wxString &name )
c801d85f 60{
68995f26
VZ
61 Init();
62
82c9f85c 63 (void)Create( parent, id, title, pos, size, style, name );
c33c4050 64}
c801d85f
KB
65
66bool wxDialog::Create( wxWindow *parent,
fb1585ae 67 wxWindowID id, const wxString &title,
2b854a32 68 const wxPoint &pos, const wxSize &size,
fb1585ae 69 long style, const wxString &name )
c801d85f 70{
21f4383a 71 SetExtraStyle(GetExtraStyle() | wxTOPLEVEL_EX_DIALOG);
2b854a32 72
82c9f85c
VZ
73 // all dialogs should have tab traversal enabled
74 style |= wxTAB_TRAVERSAL;
75
7d9f12f3 76 return wxTopLevelWindow::Create(parent, id, title, pos, size, style, name);
c33c4050 77}
c801d85f
KB
78
79void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
80{
82c9f85c
VZ
81 if (Validate())
82 TransferDataFromWindow();
c33c4050 83}
c801d85f
KB
84
85void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
86{
fb1585ae
RR
87 if (IsModal())
88 {
89 EndModal(wxID_CANCEL);
90 }
91 else
92 {
93 SetReturnCode(wxID_CANCEL);
91af0895 94 Show(false);
fb1585ae 95 }
c33c4050 96}
c801d85f 97
903f689b 98void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
c801d85f 99{
32ac755d 100 if (Validate() && TransferDataFromWindow())
1a6944fd 101 {
2b854a32 102 if (IsModal())
fb1585ae
RR
103 {
104 EndModal(wxID_OK);
105 }
106 else
107 {
108 SetReturnCode(wxID_OK);
91af0895 109 Show(false);
fb1585ae 110 }
1a6944fd 111 }
c33c4050 112}
c801d85f
KB
113
114void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
115{
2b854a32 116 // yes
c33c4050 117}
c801d85f 118
a492cb0f 119void wxDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
c801d85f 120{
e3065973
JS
121 // We'll send a Cancel message by default,
122 // which may close the dialog.
123 // Check for looping if the Cancel event handler calls Close().
124
125 // Note that if a cancel button and handler aren't present in the dialog,
126 // nothing will happen when you close the dialog via the window manager, or
127 // via Close().
128 // We wouldn't want to destroy the dialog by default, since the dialog may have been
129 // created on the stack.
130 // However, this does mean that calling dialog->Close() won't delete the dialog
131 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
132 // sure to destroy the dialog.
133 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
134
ab2b3dd4 135 static wxList s_closing;
c801d85f 136
ab2b3dd4 137 if (s_closing.Member(this))
2b854a32
VZ
138 return; // no loops
139
ab2b3dd4 140 s_closing.Append(this);
c801d85f 141
fb1585ae
RR
142 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
143 cancelEvent.SetEventObject( this );
937013e0 144 HandleWindowEvent(cancelEvent);
ab2b3dd4 145 s_closing.DeleteObject(this);
c801d85f
KB
146}
147
debe6624 148bool wxDialog::Show( bool show )
c801d85f 149{
fb1585ae
RR
150 if (!show && IsModal())
151 {
de8113d9 152 EndModal( wxID_CANCEL );
fb1585ae 153 }
c801d85f 154
de8113d9
RR
155 if (show && !m_sizeSet)
156 {
157 /* by calling GtkOnSize here, we don't have to call
158 either after showing the frame, which would entail
159 much ugly flicker nor from within the size_allocate
160 handler, because GTK 1.1.X forbids that. */
161
162 GtkOnSize( m_x, m_y, m_width, m_height );
163 }
2b854a32 164
3aa8e4ea
JS
165 if (show && CanDoLayoutAdaptation())
166 DoLayoutAdaptation();
167
739730ca 168 bool ret = wxWindow::Show( show );
e146b8c8 169
fb1585ae 170 if (show) InitDialog();
2b854a32 171
739730ca 172 return ret;
c33c4050
RR
173}
174
43a18898 175bool wxDialog::IsModal() const
e1e955e1 176{
fb1585ae 177 return m_modalShowing;
e1e955e1
RR
178}
179
180void wxDialog::SetModal( bool WXUNUSED(flag) )
c33c4050 181{
223d09f6 182 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
c33c4050 183}
c801d85f 184
43a18898 185int wxDialog::ShowModal()
c801d85f 186{
fb1585ae
RR
187 if (IsModal())
188 {
223d09f6 189 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
fb1585ae
RR
190 return GetReturnCode();
191 }
e146b8c8 192
b3daa5a3
VZ
193 // use the apps top level window as parent if none given unless explicitly
194 // forbidden
195 if ( !GetParent() && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
f6bcfd97 196 {
16b08116 197 wxWindow * const parent = GetParentForModalDialog();
4813bb8d 198 if ( parent )
f6bcfd97
BP
199 {
200 m_parent = parent;
201 gtk_window_set_transient_for( GTK_WINDOW(m_widget), GTK_WINDOW(parent->m_widget) );
202 }
203 }
204
eebe4016 205 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
91af0895
WS
206
207 Show( true );
2b854a32 208
91af0895 209 m_modalShowing = true;
2b854a32 210
304e5625
RR
211 g_openDialogs++;
212
fb1585ae 213 gtk_grab_add( m_widget );
924b84ab
VS
214
215 wxEventLoop().Run();
216
fb1585ae 217 gtk_grab_remove( m_widget );
2b854a32 218
304e5625
RR
219 g_openDialogs--;
220
fb1585ae 221 return GetReturnCode();
c33c4050 222}
c801d85f
KB
223
224void wxDialog::EndModal( int retCode )
225{
fb1585ae 226 SetReturnCode( retCode );
2b854a32 227
fb1585ae
RR
228 if (!IsModal())
229 {
223d09f6 230 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
fb1585ae
RR
231 return;
232 }
2b854a32 233
91af0895 234 m_modalShowing = false;
2b854a32 235
fb1585ae 236 gtk_main_quit();
2b854a32 237
91af0895 238 Show( false );
c33c4050 239}