]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dialog.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
c801d85f KB |
7 | // Licence: wxWindows licence |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "dialog.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/dialog.h" | |
15 | #include "wx/frame.h" | |
16 | #include "wx/app.h" | |
83624f79 RR |
17 | |
18 | #include "gdk/gdk.h" | |
19 | #include "gtk/gtk.h" | |
c801d85f KB |
20 | #include "wx/gtk/win_gtk.h" |
21 | ||
e2414cbe RR |
22 | //----------------------------------------------------------------------------- |
23 | ||
24 | extern wxList wxPendingDelete; | |
25 | ||
c801d85f | 26 | //----------------------------------------------------------------------------- |
e1e955e1 RR |
27 | // "delete_event" |
28 | //----------------------------------------------------------------------------- | |
c801d85f KB |
29 | |
30 | bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win ) | |
31 | { | |
32 | /* | |
fb1585ae RR |
33 | printf( "OnDelete from " ); |
34 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
35 | printf( win->GetClassInfo()->GetClassName() ); | |
36 | printf( ".\n" ); | |
c801d85f KB |
37 | */ |
38 | ||
fb1585ae | 39 | win->Close(); |
c801d85f | 40 | |
fb1585ae | 41 | return TRUE; |
c33c4050 | 42 | } |
c801d85f | 43 | |
e52f60e6 RR |
44 | //----------------------------------------------------------------------------- |
45 | // "size_allocate" | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
48 | static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win ) | |
49 | { | |
50 | if (!win->HasVMT()) return; | |
51 | ||
52 | /* | |
53 | printf( "OnDialogResize from " ); | |
54 | if (win->GetClassInfo() && win->GetClassInfo()->GetClassName()) | |
55 | printf( win->GetClassInfo()->GetClassName() ); | |
56 | printf( ".\n" ); | |
57 | */ | |
58 | ||
59 | win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height ); | |
60 | } | |
61 | ||
36b3b54a RR |
62 | //----------------------------------------------------------------------------- |
63 | // "configure_event" | |
64 | //----------------------------------------------------------------------------- | |
65 | ||
66 | static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win ) | |
67 | { | |
68 | if (!win->HasVMT()) return FALSE; | |
69 | ||
70 | win->m_x = event->x; | |
71 | win->m_y = event->y; | |
72 | ||
73 | wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() ); | |
74 | mevent.SetEventObject( win ); | |
75 | win->GetEventHandler()->ProcessEvent( mevent ); | |
76 | ||
77 | return FALSE; | |
78 | } | |
79 | ||
c801d85f KB |
80 | //----------------------------------------------------------------------------- |
81 | // wxDialog | |
82 | //----------------------------------------------------------------------------- | |
83 | ||
a60c99e6 | 84 | BEGIN_EVENT_TABLE(wxDialog,wxPanel) |
fb1585ae RR |
85 | EVT_BUTTON (wxID_OK, wxDialog::OnOK) |
86 | EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel) | |
87 | EVT_BUTTON (wxID_APPLY, wxDialog::OnApply) | |
e52f60e6 | 88 | EVT_SIZE (wxDialog::OnSize) |
fb1585ae | 89 | EVT_CLOSE (wxDialog::OnCloseWindow) |
c801d85f KB |
90 | END_EVENT_TABLE() |
91 | ||
a60c99e6 | 92 | IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel) |
c801d85f | 93 | |
43a18898 | 94 | wxDialog::wxDialog() |
c801d85f | 95 | { |
fb1585ae RR |
96 | m_title = ""; |
97 | m_modalShowing = FALSE; | |
c33c4050 | 98 | } |
c801d85f KB |
99 | |
100 | wxDialog::wxDialog( wxWindow *parent, | |
fb1585ae RR |
101 | wxWindowID id, const wxString &title, |
102 | const wxPoint &pos, const wxSize &size, | |
103 | long style, const wxString &name ) | |
c801d85f | 104 | { |
fb1585ae | 105 | m_modalShowing = FALSE; |
fb1585ae | 106 | Create( parent, id, title, pos, size, style, name ); |
c33c4050 | 107 | } |
c801d85f KB |
108 | |
109 | bool wxDialog::Create( wxWindow *parent, | |
fb1585ae RR |
110 | wxWindowID id, const wxString &title, |
111 | const wxPoint &pos, const wxSize &size, | |
112 | long style, const wxString &name ) | |
c801d85f | 113 | { |
a802c3a1 RR |
114 | wxTopLevelWindows.Append( this ); |
115 | ||
fb1585ae | 116 | m_needParent = FALSE; |
c801d85f | 117 | |
fb1585ae | 118 | PreCreation( parent, id, pos, size, style, name ); |
c801d85f | 119 | |
fb1585ae RR |
120 | m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL ); |
121 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS ); | |
c801d85f | 122 | |
fb1585ae | 123 | gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL); |
c801d85f | 124 | |
fb1585ae RR |
125 | gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event", |
126 | GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this ); | |
c801d85f | 127 | |
fb1585ae RR |
128 | m_wxwindow = gtk_myfixed_new(); |
129 | gtk_widget_show( m_wxwindow ); | |
130 | GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS ); | |
c801d85f | 131 | |
fb1585ae | 132 | gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow ); |
c801d85f | 133 | |
fb1585ae | 134 | SetTitle( title ); |
c801d85f | 135 | |
fb1585ae RR |
136 | if ((m_x != -1) || (m_y != -1)) |
137 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
903f689b | 138 | |
fb1585ae | 139 | gtk_widget_set_usize( m_widget, m_width, m_height ); |
903f689b | 140 | |
36b3b54a RR |
141 | gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate", |
142 | GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this ); | |
143 | ||
144 | gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event", | |
145 | GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this ); | |
146 | ||
fb1585ae | 147 | if (m_parent) m_parent->AddChild( this ); |
6ca41e57 | 148 | |
fb1585ae | 149 | PostCreation(); |
c801d85f | 150 | |
fb1585ae | 151 | return TRUE; |
c33c4050 | 152 | } |
c801d85f | 153 | |
43a18898 | 154 | wxDialog::~wxDialog() |
c801d85f | 155 | { |
fb1585ae | 156 | wxTopLevelWindows.DeleteObject( this ); |
0d2a2b60 RR |
157 | |
158 | if (wxTheApp->GetTopWindow() == this) | |
159 | { | |
160 | wxTheApp->SetTopWindow( (wxWindow*) NULL ); | |
161 | } | |
162 | ||
163 | if (wxTopLevelWindows.Number() == 0) | |
164 | { | |
165 | wxTheApp->ExitMainLoop(); | |
166 | } | |
c33c4050 | 167 | } |
c801d85f | 168 | |
43a18898 | 169 | void wxDialog::SetTitle( const wxString& title ) |
c801d85f | 170 | { |
fb1585ae RR |
171 | m_title = title; |
172 | if (m_title.IsNull()) m_title = ""; | |
173 | gtk_window_set_title( GTK_WINDOW(m_widget), m_title ); | |
c33c4050 | 174 | } |
c801d85f | 175 | |
43a18898 | 176 | wxString wxDialog::GetTitle() const |
c801d85f | 177 | { |
fb1585ae | 178 | return (wxString&)m_title; |
c33c4050 | 179 | } |
c801d85f KB |
180 | |
181 | void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) ) | |
182 | { | |
fb1585ae | 183 | if (Validate()) TransferDataFromWindow(); |
c33c4050 | 184 | } |
c801d85f KB |
185 | |
186 | void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) ) | |
187 | { | |
fb1585ae RR |
188 | if (IsModal()) |
189 | { | |
190 | EndModal(wxID_CANCEL); | |
191 | } | |
192 | else | |
193 | { | |
194 | SetReturnCode(wxID_CANCEL); | |
195 | this->Show(FALSE); | |
196 | } | |
c33c4050 | 197 | } |
c801d85f | 198 | |
903f689b | 199 | void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) ) |
c801d85f | 200 | { |
fb1585ae | 201 | if ( Validate() && TransferDataFromWindow()) |
1a6944fd | 202 | { |
fb1585ae RR |
203 | if (IsModal()) |
204 | { | |
205 | EndModal(wxID_OK); | |
206 | } | |
207 | else | |
208 | { | |
209 | SetReturnCode(wxID_OK); | |
210 | this->Show(FALSE); | |
211 | } | |
1a6944fd | 212 | } |
c33c4050 | 213 | } |
c801d85f KB |
214 | |
215 | void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) ) | |
216 | { | |
217 | // yes | |
c33c4050 | 218 | } |
c801d85f | 219 | |
43a18898 | 220 | bool wxDialog::OnClose() |
c801d85f | 221 | { |
fb1585ae | 222 | static wxList closing; |
c801d85f | 223 | |
fb1585ae | 224 | if (closing.Member(this)) return FALSE; // no loops |
1a6944fd | 225 | |
fb1585ae | 226 | closing.Append(this); |
c801d85f | 227 | |
fb1585ae RR |
228 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); |
229 | cancelEvent.SetEventObject( this ); | |
230 | GetEventHandler()->ProcessEvent(cancelEvent); | |
231 | closing.DeleteObject(this); | |
c801d85f | 232 | |
fb1585ae | 233 | return FALSE; |
c801d85f KB |
234 | } |
235 | ||
43a18898 | 236 | bool wxDialog::Destroy() |
e2414cbe | 237 | { |
fb1585ae | 238 | if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this); |
e2414cbe | 239 | |
fb1585ae | 240 | return TRUE; |
e2414cbe RR |
241 | } |
242 | ||
43a18898 | 243 | void wxDialog::OnCloseWindow( wxCloseEvent& event ) |
c801d85f | 244 | { |
fb1585ae RR |
245 | if (GetEventHandler()->OnClose() || event.GetForce()) |
246 | { | |
247 | this->Destroy(); | |
248 | } | |
c33c4050 | 249 | } |
c801d85f | 250 | |
e52f60e6 RR |
251 | void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height ) |
252 | { | |
253 | // due to a bug in gtk, x,y are always 0 | |
254 | // m_x = x; | |
255 | // m_y = y; | |
256 | ||
257 | if ((m_height == height) && (m_width == width) && | |
258 | (m_sizeSet)) return; | |
259 | if (!m_wxwindow) return; | |
260 | ||
261 | m_width = width; | |
262 | m_height = height; | |
263 | ||
264 | if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth; | |
265 | if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight; | |
0c77152e RR |
266 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth; |
267 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight; | |
e52f60e6 RR |
268 | |
269 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
270 | ||
271 | m_sizeSet = TRUE; | |
272 | ||
273 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); | |
274 | event.SetEventObject( this ); | |
275 | GetEventHandler()->ProcessEvent( event ); | |
276 | } | |
277 | ||
278 | void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
279 | { | |
2830bf19 | 280 | wxASSERT_MSG( (m_widget != NULL), "invalid dialog" ); |
e52f60e6 RR |
281 | |
282 | if (GetAutoLayout()) | |
283 | { | |
284 | Layout(); | |
285 | } | |
286 | else | |
287 | { | |
288 | // no child: go out ! | |
db1b4961 | 289 | if (!GetChildren().First()) return; |
e52f60e6 RR |
290 | |
291 | // do we have exactly one child? | |
292 | wxWindow *child = (wxWindow *) NULL; | |
db1b4961 | 293 | for(wxNode *node = GetChildren().First(); node; node = node->Next()) |
e52f60e6 RR |
294 | { |
295 | wxWindow *win = (wxWindow *)node->Data(); | |
de135918 | 296 | if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog)) |
e52f60e6 RR |
297 | { |
298 | // it's the second one: do nothing | |
299 | if (child) return; | |
300 | child = win; | |
301 | } | |
302 | } | |
303 | ||
304 | // yes: set it's size to fill all the frame | |
305 | int client_x, client_y; | |
306 | GetClientSize( &client_x, &client_y ); | |
307 | child->SetSize( 1, 1, client_x-2, client_y); | |
308 | } | |
309 | } | |
310 | ||
fb1585ae | 311 | void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags ) |
903f689b | 312 | { |
fb1585ae RR |
313 | wxASSERT_MSG( (m_widget != NULL), "invalid window" ); |
314 | ||
315 | // Don't do anything for children of wxMDIChildFrame | |
316 | if (!m_wxwindow) return; | |
317 | ||
318 | if (m_resizing) return; // I don't like recursions | |
319 | m_resizing = TRUE; | |
320 | ||
321 | int old_x = m_x; | |
322 | int old_y = m_y; | |
323 | int old_width = m_width; | |
324 | int old_height = m_height; | |
325 | ||
326 | if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING) | |
327 | { | |
328 | if (x != -1) m_x = x; | |
329 | if (y != -1) m_y = y; | |
330 | if (width != -1) m_width = width; | |
331 | if (height != -1) m_height = height; | |
332 | } | |
333 | else | |
334 | { | |
335 | m_x = x; | |
336 | m_y = y; | |
337 | m_width = width; | |
338 | m_height = height; | |
339 | } | |
340 | ||
341 | if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH) | |
342 | { | |
343 | if (width == -1) m_width = 80; | |
344 | } | |
345 | ||
346 | if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT) | |
347 | { | |
348 | if (height == -1) m_height = 26; | |
349 | } | |
350 | ||
351 | if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth; | |
352 | if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight; | |
0c77152e RR |
353 | if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth; |
354 | if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight; | |
fb1585ae RR |
355 | |
356 | if ((m_x != -1) || (m_y != -1)) | |
357 | { | |
358 | if ((m_x != old_x) || (m_y != old_y)) | |
359 | gtk_widget_set_uposition( m_widget, m_x, m_y ); | |
360 | } | |
361 | ||
362 | if ((m_width != old_width) || (m_height != old_height)) | |
363 | { | |
364 | gtk_widget_set_usize( m_widget, m_width, m_height ); | |
365 | } | |
366 | ||
367 | m_sizeSet = TRUE; | |
368 | ||
369 | wxSizeEvent event( wxSize(m_width,m_height), GetId() ); | |
370 | event.SetEventObject( this ); | |
e52f60e6 | 371 | GetEventHandler()->ProcessEvent( event ); |
fb1585ae RR |
372 | |
373 | m_resizing = FALSE; | |
903f689b RR |
374 | } |
375 | ||
43a18898 RR |
376 | void wxDialog::SetSize( int width, int height ) |
377 | { | |
378 | SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING ); | |
379 | } | |
380 | ||
903f689b RR |
381 | void wxDialog::Centre( int direction ) |
382 | { | |
fb1585ae RR |
383 | wxASSERT_MSG( (m_widget != NULL), "invalid frame" ); |
384 | ||
43a18898 RR |
385 | int x = 0; |
386 | int y = 0; | |
fb1585ae RR |
387 | |
388 | if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2; | |
389 | if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2; | |
390 | ||
391 | Move( x, y ); | |
903f689b RR |
392 | } |
393 | ||
debe6624 | 394 | bool wxDialog::Show( bool show ) |
c801d85f | 395 | { |
fb1585ae RR |
396 | if (!show && IsModal()) |
397 | { | |
398 | EndModal( wxID_CANCEL ); | |
399 | } | |
c801d85f | 400 | |
fb1585ae | 401 | wxWindow::Show( show ); |
c801d85f | 402 | |
fb1585ae | 403 | if (show) InitDialog(); |
c801d85f | 404 | |
fb1585ae | 405 | return TRUE; |
c33c4050 RR |
406 | } |
407 | ||
43a18898 | 408 | bool wxDialog::IsModal() const |
e1e955e1 | 409 | { |
fb1585ae | 410 | return m_modalShowing; |
e1e955e1 RR |
411 | } |
412 | ||
413 | void wxDialog::SetModal( bool WXUNUSED(flag) ) | |
c33c4050 | 414 | { |
e1e955e1 | 415 | /* |
c33c4050 RR |
416 | if (flag) |
417 | m_windowStyle |= wxDIALOG_MODAL; | |
418 | else | |
419 | if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL; | |
e1e955e1 | 420 | */ |
fb1585ae | 421 | wxFAIL_MSG( "wxDialog:SetModal obsolete now" ); |
c33c4050 | 422 | } |
c801d85f | 423 | |
43a18898 | 424 | int wxDialog::ShowModal() |
c801d85f | 425 | { |
fb1585ae RR |
426 | if (IsModal()) |
427 | { | |
428 | wxFAIL_MSG( "wxDialog:ShowModal called twice" ); | |
429 | return GetReturnCode(); | |
430 | } | |
d355d3fe | 431 | |
fb1585ae | 432 | Show( TRUE ); |
d355d3fe | 433 | |
fb1585ae | 434 | m_modalShowing = TRUE; |
d355d3fe | 435 | |
fb1585ae RR |
436 | gtk_grab_add( m_widget ); |
437 | gtk_main(); | |
438 | gtk_grab_remove( m_widget ); | |
d355d3fe | 439 | |
fb1585ae | 440 | return GetReturnCode(); |
c33c4050 | 441 | } |
c801d85f KB |
442 | |
443 | void wxDialog::EndModal( int retCode ) | |
444 | { | |
fb1585ae | 445 | SetReturnCode( retCode ); |
d355d3fe | 446 | |
fb1585ae RR |
447 | if (!IsModal()) |
448 | { | |
449 | wxFAIL_MSG( "wxDialog:EndModal called twice" ); | |
450 | return; | |
451 | } | |
b6af8d80 | 452 | |
fb1585ae | 453 | m_modalShowing = FALSE; |
d355d3fe | 454 | |
fb1585ae | 455 | gtk_main_quit(); |
5b011451 | 456 | |
fb1585ae | 457 | Show( FALSE ); |
c33c4050 | 458 | } |
c801d85f | 459 | |
43a18898 | 460 | void wxDialog::InitDialog() |
c801d85f | 461 | { |
fb1585ae | 462 | wxWindow::InitDialog(); |
c33c4050 RR |
463 | } |
464 | ||
c33c4050 RR |
465 | void wxDialog::SetIcon( const wxIcon &icon ) |
466 | { | |
fb1585ae RR |
467 | m_icon = icon; |
468 | if (!icon.Ok()) return; | |
c33c4050 | 469 | |
fb1585ae RR |
470 | wxMask *mask = icon.GetMask(); |
471 | GdkBitmap *bm = (GdkBitmap *) NULL; | |
472 | if (mask) bm = mask->GetBitmap(); | |
c33c4050 | 473 | |
fb1585ae | 474 | gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm ); |
c33c4050 | 475 | } |