]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/dialog.cpp
480a949a13d9b7c3d35c792b44602719a554a19f
[wxWidgets.git] / src / gtk1 / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
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"
17
18 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20 #include "wx/gtk/win_gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern wxList wxPendingDelete;
34
35 //-----------------------------------------------------------------------------
36 // "delete_event"
37 //-----------------------------------------------------------------------------
38
39 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
40 {
41 if (g_isIdle)
42 wxapp_install_idle_handler();
43
44 win->Close();
45
46 return TRUE;
47 }
48
49 //-----------------------------------------------------------------------------
50 // "size_allocate"
51 //-----------------------------------------------------------------------------
52
53 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
54 {
55 if (g_isIdle)
56 wxapp_install_idle_handler();
57
58 if (!win->m_hasVMT) return;
59
60 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
61 {
62 win->m_width = alloc->width;
63 win->m_height = alloc->height;
64 win->UpdateSize();
65 }
66 }
67
68 //-----------------------------------------------------------------------------
69 // "configure_event"
70 //-----------------------------------------------------------------------------
71
72 static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
73 {
74 if (g_isIdle)
75 wxapp_install_idle_handler();
76
77 if (!win->m_hasVMT) return FALSE;
78
79 win->m_x = event->x;
80 win->m_y = event->y;
81
82 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
83 mevent.SetEventObject( win );
84 win->GetEventHandler()->ProcessEvent( mevent );
85
86 return FALSE;
87 }
88
89 //-----------------------------------------------------------------------------
90 // "realize" from m_widget
91 //-----------------------------------------------------------------------------
92
93 /* we cannot MWM hints and icons before the widget has been realized,
94 so we do this directly after realization */
95
96 static gint
97 gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win )
98 {
99 if (g_isIdle)
100 wxapp_install_idle_handler();
101
102 /* I haven''t been able to set the position of
103 the dialog before it is shown, so I set the
104 position in "realize" and "map" */
105 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
106
107 /* reset the icon */
108 if (win->m_icon != wxNullIcon)
109 {
110 wxIcon icon( win->m_icon );
111 win->m_icon = wxNullIcon;
112 win->SetIcon( icon );
113 }
114
115 return FALSE;
116 }
117
118 //-----------------------------------------------------------------------------
119 // "map" from m_widget
120 //-----------------------------------------------------------------------------
121
122 static gint
123 gtk_dialog_map_callback( GtkWidget *widget, wxDialog *win )
124 {
125 if (g_isIdle)
126 wxapp_install_idle_handler();
127
128 /* I haven''t been able to set the position of
129 the dialog before it is shown, so I set the
130 position in "realize" and "map" */
131 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
132
133 /* all this is for Motif Window Manager "hints" and is supposed to be
134 recognized by other WM as well. not tested. */
135 long decor = (long) GDK_DECOR_BORDER;
136 long func = (long) GDK_FUNC_MOVE ;
137
138 if ((win->GetWindowStyle() & wxCAPTION) != 0)
139 decor |= GDK_DECOR_TITLE;
140 if ((win->GetWindowStyle() & wxSYSTEM_MENU) != 0)
141 {
142 decor |= GDK_DECOR_MENU;
143 func |= GDK_FUNC_CLOSE;
144 }
145 if ((win->GetWindowStyle() & wxMINIMIZE_BOX) != 0)
146 {
147 func |= GDK_FUNC_MINIMIZE;
148 decor |= GDK_DECOR_MINIMIZE;
149 }
150 if ((win->GetWindowStyle() & wxMAXIMIZE_BOX) != 0)
151 {
152 decor |= GDK_DECOR_MAXIMIZE;
153 func |= GDK_FUNC_MAXIMIZE;
154 }
155 if ((win->GetWindowStyle() & wxRESIZE_BORDER) != 0)
156 {
157 func |= GDK_FUNC_RESIZE;
158 decor |= GDK_DECOR_RESIZEH;
159 }
160 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
161 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
162
163 /* GTK's shrinking/growing policy */
164 if ((win->GetWindowStyle() & wxRESIZE_BORDER) == 0)
165 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 0, 0, 1);
166 else
167 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
168
169 return FALSE;
170 }
171
172 //-----------------------------------------------------------------------------
173 // InsertChild for wxDialog
174 //-----------------------------------------------------------------------------
175
176 /* Callback for wxFrame. This very strange beast has to be used because
177 * C++ has no virtual methods in a constructor. We have to emulate a
178 * virtual function here as wxWindows requires different ways to insert
179 * a child in container classes. */
180
181 static void wxInsertChildInDialog( wxDialog* parent, wxWindow* child )
182 {
183 gtk_myfixed_put( GTK_MYFIXED(parent->m_wxwindow),
184 GTK_WIDGET(child->m_widget),
185 child->m_x,
186 child->m_y,
187 child->m_width,
188 child->m_height );
189
190 if (parent->HasFlag(wxTAB_TRAVERSAL))
191 {
192 /* we now allow a window to get the focus as long as it
193 doesn't have any children. */
194 GTK_WIDGET_UNSET_FLAGS( parent->m_wxwindow, GTK_CAN_FOCUS );
195 }
196 }
197
198 //-----------------------------------------------------------------------------
199 // wxDialog
200 //-----------------------------------------------------------------------------
201
202 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
203 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
204 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
205 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
206 EVT_SIZE (wxDialog::OnSize)
207 EVT_CLOSE (wxDialog::OnCloseWindow)
208 END_EVENT_TABLE()
209
210 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
211
212 void wxDialog::Init()
213 {
214 m_returnCode = 0;
215 m_sizeSet = FALSE;
216 m_modalShowing = FALSE;
217 }
218
219 wxDialog::wxDialog( wxWindow *parent,
220 wxWindowID id, const wxString &title,
221 const wxPoint &pos, const wxSize &size,
222 long style, const wxString &name )
223 {
224 Init();
225
226 Create( parent, id, title, pos, size, style, name );
227 }
228
229 bool wxDialog::Create( wxWindow *parent,
230 wxWindowID id, const wxString &title,
231 const wxPoint &pos, const wxSize &size,
232 long style, const wxString &name )
233 {
234 wxTopLevelWindows.Append( this );
235
236 m_needParent = FALSE;
237
238 PreCreation( parent, id, pos, size, style, name );
239
240 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
241
242 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
243
244 if (!name.IsEmpty())
245 gtk_window_set_wmclass( GTK_WINDOW(m_widget), name.mb_str(), name.mb_str() );
246
247 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
248
249 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
250 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
251
252 m_wxwindow = gtk_myfixed_new();
253 gtk_widget_show( m_wxwindow );
254 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
255
256 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
257
258 SetTitle( title );
259
260 if (m_parent) m_parent->AddChild( this );
261
262 PostCreation();
263
264 /* we cannot set MWM hints before the widget has
265 been realized, so we do this directly after realization */
266 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
267 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
268
269 /* we set the position of the window after the map event. setting it
270 before has no effect (with KWM) */
271 gtk_signal_connect( GTK_OBJECT(m_widget), "map",
272 GTK_SIGNAL_FUNC(gtk_dialog_map_callback), (gpointer) this );
273
274 /* the user resized the frame by dragging etc. */
275 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
276 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
277
278 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
279 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
280
281 return TRUE;
282 }
283
284 wxDialog::~wxDialog()
285 {
286 m_isBeingDeleted = TRUE;
287
288 wxTopLevelWindows.DeleteObject( this );
289
290 if (wxTheApp->GetTopWindow() == this)
291 {
292 wxTheApp->SetTopWindow( (wxWindow*) NULL );
293 }
294
295 if (wxTopLevelWindows.Number() == 0)
296 {
297 wxTheApp->ExitMainLoop();
298 }
299 }
300
301 void wxDialog::SetTitle( const wxString& title )
302 {
303 m_title = title;
304 if (m_title.IsNull()) m_title = _T("");
305 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
306 }
307
308 wxString wxDialog::GetTitle() const
309 {
310 return (wxString&)m_title;
311 }
312
313 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
314 {
315 if (Validate()) TransferDataFromWindow();
316 }
317
318 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
319 {
320 if (IsModal())
321 {
322 EndModal(wxID_CANCEL);
323 }
324 else
325 {
326 SetReturnCode(wxID_CANCEL);
327 Show(FALSE);
328 }
329 }
330
331 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
332 {
333 if (Validate() && TransferDataFromWindow())
334 {
335 if (IsModal())
336 {
337 EndModal(wxID_OK);
338 }
339 else
340 {
341 SetReturnCode(wxID_OK);
342 this->Show(FALSE);
343 }
344 }
345 }
346
347 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
348 {
349 // yes
350 }
351
352 void wxDialog::OnCloseWindow(wxCloseEvent& event)
353 {
354 // We'll send a Cancel message by default,
355 // which may close the dialog.
356 // Check for looping if the Cancel event handler calls Close().
357
358 // Note that if a cancel button and handler aren't present in the dialog,
359 // nothing will happen when you close the dialog via the window manager, or
360 // via Close().
361 // We wouldn't want to destroy the dialog by default, since the dialog may have been
362 // created on the stack.
363 // However, this does mean that calling dialog->Close() won't delete the dialog
364 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
365 // sure to destroy the dialog.
366 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
367
368 static wxList s_closing;
369
370 if (s_closing.Member(this))
371 return; // no loops
372
373 s_closing.Append(this);
374
375 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
376 cancelEvent.SetEventObject( this );
377 GetEventHandler()->ProcessEvent(cancelEvent);
378 s_closing.DeleteObject(this);
379 }
380
381 bool wxDialog::Destroy()
382 {
383 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
384
385 return TRUE;
386 }
387
388 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
389 {
390 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
391
392 if (GetAutoLayout())
393 {
394 Layout();
395 }
396 else
397 {
398 /* no child: go out ! */
399 if (!GetChildren().First()) return;
400
401 /* do we have exactly one child? */
402 wxWindow *child = (wxWindow *) NULL;
403 for(wxNode *node = GetChildren().First(); node; node = node->Next())
404 {
405 wxWindow *win = (wxWindow *)node->Data();
406 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
407 {
408 /* it's the second one: do nothing */
409 if (child) return;
410 child = win;
411 }
412 }
413
414 /* yes: set it's size to fill all the frame */
415 int client_x, client_y;
416 GetClientSize( &client_x, &client_y );
417 child->SetSize( 1, 1, client_x-2, client_y);
418 }
419 }
420
421 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
422 {
423 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
424 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
425
426 if (m_resizing) return; /* I don't like recursions */
427 m_resizing = TRUE;
428
429 int old_x = m_x;
430 int old_y = m_y;
431 int old_width = m_width;
432 int old_height = m_height;
433
434 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
435 {
436 if (x != -1) m_x = x;
437 if (y != -1) m_y = y;
438 if (width != -1) m_width = width;
439 if (height != -1) m_height = height;
440 }
441 else
442 {
443 m_x = x;
444 m_y = y;
445 m_width = width;
446 m_height = height;
447 }
448
449 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
450 {
451 if (width == -1) m_width = 80;
452 }
453
454 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
455 {
456 if (height == -1) m_height = 26;
457 }
458
459 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
460 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
461 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
462 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
463
464 if ((m_x != -1) || (m_y != -1))
465 {
466 if ((m_x != old_x) || (m_y != old_y))
467 {
468 /* we set the position here and when showing the dialog
469 for the first time in idle time */
470 gtk_widget_set_uposition( m_widget, m_x, m_y );
471 }
472 }
473
474 if ((m_width != old_width) || (m_height != old_height))
475 {
476 /* actual resizing is deferred to GtkOnSize in idle time and
477 when showing the dialog */
478 m_sizeSet = FALSE;
479 }
480
481 m_resizing = FALSE;
482 }
483
484 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
485 {
486 // due to a bug in gtk, x,y are always 0
487 // m_x = x;
488 // m_y = y;
489
490 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
491 if (!m_wxwindow) return;
492
493 m_width = width;
494 m_height = height;
495
496 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
497 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
498 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
499 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
500
501 /* we actually set the size of a frame here and no-where else */
502 gtk_widget_set_usize( m_widget, m_width, m_height );
503
504 m_sizeSet = TRUE;
505
506 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
507 event.SetEventObject( this );
508 GetEventHandler()->ProcessEvent( event );
509 }
510
511 void wxDialog::Centre( int direction )
512 {
513 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
514
515 int x = 0;
516 int y = 0;
517
518 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
519 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
520
521 Move( x, y );
522 }
523
524 void wxDialog::OnInternalIdle()
525 {
526 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
527 GtkOnSize( m_x, m_y, m_width, m_height );
528 }
529
530 bool wxDialog::Show( bool show )
531 {
532 if (!show && IsModal())
533 {
534 EndModal( wxID_CANCEL );
535 }
536
537 if (show && !m_sizeSet)
538 {
539 /* by calling GtkOnSize here, we don't have to call
540 either after showing the frame, which would entail
541 much ugly flicker nor from within the size_allocate
542 handler, because GTK 1.1.X forbids that. */
543
544 GtkOnSize( m_x, m_y, m_width, m_height );
545 }
546
547 bool ret = wxWindow::Show( show );
548
549 if (show) InitDialog();
550
551 return ret;
552 }
553
554 bool wxDialog::IsModal() const
555 {
556 return m_modalShowing;
557 }
558
559 void wxDialog::SetModal( bool WXUNUSED(flag) )
560 {
561 /*
562 if (flag)
563 m_windowStyle |= wxDIALOG_MODAL;
564 else
565 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
566 */
567 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
568 }
569
570 int wxDialog::ShowModal()
571 {
572 if (IsModal())
573 {
574 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
575 return GetReturnCode();
576 }
577
578 Show( TRUE );
579
580 m_modalShowing = TRUE;
581
582 gtk_grab_add( m_widget );
583 gtk_main();
584 gtk_grab_remove( m_widget );
585
586 return GetReturnCode();
587 }
588
589 void wxDialog::EndModal( int retCode )
590 {
591 SetReturnCode( retCode );
592
593 if (!IsModal())
594 {
595 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
596 return;
597 }
598
599 m_modalShowing = FALSE;
600
601 gtk_main_quit();
602
603 Show( FALSE );
604 }
605
606 void wxDialog::InitDialog()
607 {
608 wxWindow::InitDialog();
609 }
610
611 void wxDialog::SetIcon( const wxIcon &icon )
612 {
613 m_icon = icon;
614 if (!icon.Ok()) return;
615
616 if (!m_widget->window) return;
617
618 wxMask *mask = icon.GetMask();
619 GdkBitmap *bm = (GdkBitmap *) NULL;
620 if (mask) bm = mask->GetBitmap();
621
622 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
623 }