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