added SpinCtrl,
[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 }
250
251 wxDialog::wxDialog( wxWindow *parent,
252 wxWindowID id, const wxString &title,
253 const wxPoint &pos, const wxSize &size,
254 long style, const wxString &name )
255 {
256 Init();
257
258 Create( parent, id, title, pos, size, style, name );
259 }
260
261 bool wxDialog::Create( wxWindow *parent,
262 wxWindowID id, const wxString &title,
263 const wxPoint &pos, const wxSize &size,
264 long style, const wxString &name )
265 {
266 wxTopLevelWindows.Append( this );
267
268 m_needParent = FALSE;
269
270 if (!PreCreation( parent, pos, size ) ||
271 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
272 {
273 wxFAIL_MSG( wxT("wxDialog creation failed") );
274 return FALSE;
275 }
276
277 m_insertCallback = (wxInsertChildFunction) wxInsertChildInDialog;
278
279 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
280
281 if (!name.IsEmpty())
282 gtk_window_set_wmclass( GTK_WINDOW(m_widget), name.mb_str(), name.mb_str() );
283
284 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
285
286 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
287 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
288
289 m_wxwindow = gtk_myfixed_new();
290 gtk_widget_show( m_wxwindow );
291 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
292
293 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
294
295 SetTitle( title );
296
297 if (m_parent) m_parent->AddChild( this );
298
299 PostCreation();
300
301 /* we cannot set MWM hints before the widget has
302 been realized, so we do this directly after realization */
303 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
304 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
305
306 /* we set the position of the window after the map event. setting it
307 before has no effect (with KWM) */
308 gtk_signal_connect( GTK_OBJECT(m_widget), "map",
309 GTK_SIGNAL_FUNC(gtk_dialog_map_callback), (gpointer) this );
310
311 /* the user resized the frame by dragging etc. */
312 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
313 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
314
315 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
316 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
317
318 return TRUE;
319 }
320
321 wxDialog::~wxDialog()
322 {
323 m_isBeingDeleted = TRUE;
324
325 wxTopLevelWindows.DeleteObject( this );
326
327 if (wxTheApp->GetTopWindow() == this)
328 {
329 wxTheApp->SetTopWindow( (wxWindow*) NULL );
330 }
331
332 if (wxTopLevelWindows.Number() == 0)
333 {
334 wxTheApp->ExitMainLoop();
335 }
336 }
337
338 void wxDialog::SetTitle( const wxString& title )
339 {
340 m_title = title;
341 if (m_title.IsNull()) m_title = wxT("");
342 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
343 }
344
345 wxString wxDialog::GetTitle() const
346 {
347 return (wxString&)m_title;
348 }
349
350 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
351 {
352 if (Validate()) TransferDataFromWindow();
353 }
354
355 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
356 {
357 if (IsModal())
358 {
359 EndModal(wxID_CANCEL);
360 }
361 else
362 {
363 SetReturnCode(wxID_CANCEL);
364 Show(FALSE);
365 }
366 }
367
368 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
369 {
370 if (Validate() && TransferDataFromWindow())
371 {
372 if (IsModal())
373 {
374 EndModal(wxID_OK);
375 }
376 else
377 {
378 SetReturnCode(wxID_OK);
379 this->Show(FALSE);
380 }
381 }
382 }
383
384 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
385 {
386 // yes
387 }
388
389 void wxDialog::OnCloseWindow(wxCloseEvent& event)
390 {
391 // We'll send a Cancel message by default,
392 // which may close the dialog.
393 // Check for looping if the Cancel event handler calls Close().
394
395 // Note that if a cancel button and handler aren't present in the dialog,
396 // nothing will happen when you close the dialog via the window manager, or
397 // via Close().
398 // We wouldn't want to destroy the dialog by default, since the dialog may have been
399 // created on the stack.
400 // However, this does mean that calling dialog->Close() won't delete the dialog
401 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
402 // sure to destroy the dialog.
403 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
404
405 static wxList s_closing;
406
407 if (s_closing.Member(this))
408 return; // no loops
409
410 s_closing.Append(this);
411
412 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
413 cancelEvent.SetEventObject( this );
414 GetEventHandler()->ProcessEvent(cancelEvent);
415 s_closing.DeleteObject(this);
416 }
417
418 bool wxDialog::Destroy()
419 {
420 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
421
422 return TRUE;
423 }
424
425 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
426 {
427 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
428
429 #if wxUSE_CONSTRAINTS
430 if (GetAutoLayout())
431 {
432 Layout();
433 }
434 else
435 #endif // wxUSE_CONSTRAINTS
436 {
437 /* no child: go out ! */
438 if (!GetChildren().First()) return;
439
440 /* do we have exactly one child? */
441 wxWindow *child = (wxWindow *) NULL;
442 for(wxNode *node = GetChildren().First(); node; node = node->Next())
443 {
444 wxWindow *win = (wxWindow *)node->Data();
445 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
446 {
447 /* it's the second one: do nothing */
448 if (child) return;
449 child = win;
450 }
451 }
452
453 /* yes: set it's size to fill all the frame */
454 int client_x, client_y;
455 GetClientSize( &client_x, &client_y );
456 child->SetSize( 1, 1, client_x-2, client_y);
457 }
458 }
459
460 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
461 {
462 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
463 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid dialog") );
464
465 if (m_resizing) return; /* I don't like recursions */
466 m_resizing = TRUE;
467
468 int old_x = m_x;
469 int old_y = m_y;
470 int old_width = m_width;
471 int old_height = m_height;
472
473 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
474 {
475 if (x != -1) m_x = x;
476 if (y != -1) m_y = y;
477 if (width != -1) m_width = width;
478 if (height != -1) m_height = height;
479 }
480 else
481 {
482 m_x = x;
483 m_y = y;
484 m_width = width;
485 m_height = height;
486 }
487
488 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
489 {
490 if (width == -1) m_width = 80;
491 }
492
493 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
494 {
495 if (height == -1) m_height = 26;
496 }
497
498 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
499 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
500 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
501 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
502
503 if ((m_x != -1) || (m_y != -1))
504 {
505 if ((m_x != old_x) || (m_y != old_y))
506 {
507 /* we set the position here and when showing the dialog
508 for the first time in idle time */
509 gtk_widget_set_uposition( m_widget, m_x, m_y );
510 }
511 }
512
513 if ((m_width != old_width) || (m_height != old_height))
514 {
515 /* actual resizing is deferred to GtkOnSize in idle time and
516 when showing the dialog */
517 m_sizeSet = FALSE;
518 }
519
520 m_resizing = FALSE;
521 }
522
523 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
524 {
525 // due to a bug in gtk, x,y are always 0
526 // m_x = x;
527 // m_y = y;
528
529 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
530 if (!m_wxwindow) return;
531
532 m_width = width;
533 m_height = height;
534
535 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
536 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
537 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
538 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
539
540 /* we actually set the size of a frame here and no-where else */
541 gtk_widget_set_usize( m_widget, m_width, m_height );
542
543 m_sizeSet = TRUE;
544
545 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
546 event.SetEventObject( this );
547 GetEventHandler()->ProcessEvent( event );
548 }
549
550 void wxDialog::Centre( int direction )
551 {
552 wxASSERT_MSG( (m_widget != NULL), wxT("invalid dialog") );
553
554 int x = 0;
555 int y = 0;
556
557 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
558 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
559
560 Move( x, y );
561 }
562
563 void wxDialog::OnInternalIdle()
564 {
565 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
566 GtkOnSize( m_x, m_y, m_width, m_height );
567
568 wxWindow::OnInternalIdle();
569 }
570
571 bool wxDialog::Show( bool show )
572 {
573 if (!show && IsModal())
574 {
575 EndModal( wxID_CANCEL );
576 }
577
578 if (show && !m_sizeSet)
579 {
580 /* by calling GtkOnSize here, we don't have to call
581 either after showing the frame, which would entail
582 much ugly flicker nor from within the size_allocate
583 handler, because GTK 1.1.X forbids that. */
584
585 GtkOnSize( m_x, m_y, m_width, m_height );
586 }
587
588 bool ret = wxWindow::Show( show );
589
590 if (show) InitDialog();
591
592 return ret;
593 }
594
595 bool wxDialog::IsModal() const
596 {
597 return m_modalShowing;
598 }
599
600 void wxDialog::SetModal( bool WXUNUSED(flag) )
601 {
602 /*
603 if (flag)
604 m_windowStyle |= wxDIALOG_MODAL;
605 else
606 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
607 */
608 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
609 }
610
611 int wxDialog::ShowModal()
612 {
613 if (IsModal())
614 {
615 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
616 return GetReturnCode();
617 }
618
619 wxBusyCursorSuspender cs; // temporarily suppress the busy cursor
620
621 Show( TRUE );
622
623 m_modalShowing = TRUE;
624
625 gtk_grab_add( m_widget );
626 gtk_main();
627 gtk_grab_remove( m_widget );
628
629 return GetReturnCode();
630 }
631
632 void wxDialog::EndModal( int retCode )
633 {
634 SetReturnCode( retCode );
635
636 if (!IsModal())
637 {
638 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
639 return;
640 }
641
642 m_modalShowing = FALSE;
643
644 gtk_main_quit();
645
646 Show( FALSE );
647 }
648
649 void wxDialog::InitDialog()
650 {
651 wxWindow::InitDialog();
652 }
653
654 void wxDialog::SetIcon( const wxIcon &icon )
655 {
656 m_icon = icon;
657 if (!icon.Ok()) return;
658
659 if (!m_widget->window) return;
660
661 wxMask *mask = icon.GetMask();
662 GdkBitmap *bm = (GdkBitmap *) NULL;
663 if (mask) bm = mask->GetBitmap();
664
665 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
666 }