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