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