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