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