1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "dialog.h"
14 #include "wx/dialog.h"
20 #include "wx/gtk/win_gtk.h"
21 #include "wx/cursor.h"
24 #include "gdk/gdkprivate.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern wxList wxPendingDelete
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
48 wxapp_install_idle_handler();
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
62 wxapp_install_idle_handler();
64 if (!win
->m_hasVMT
) return;
66 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
68 win
->m_width
= alloc
->width
;
69 win
->m_height
= alloc
->height
;
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
79 #if (GTK_MINOR_VERSON > 0)
80 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
82 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
86 wxapp_install_idle_handler();
88 if (!win
->m_hasVMT
) return FALSE
;
90 #if (GTK_MINOR_VERSON > 0)
93 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
101 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
102 mevent
.SetEventObject( win
);
103 win
->GetEventHandler()->ProcessEvent( mevent
);
108 //-----------------------------------------------------------------------------
109 // "realize" from m_widget
110 //-----------------------------------------------------------------------------
112 /* we cannot MWM hints and icons before the widget has been realized,
113 so we do this directly after realization */
116 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
119 wxapp_install_idle_handler();
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
);
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
;
131 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
132 decor
|= GDK_DECOR_TITLE
;
133 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
135 decor
|= GDK_DECOR_MENU
;
136 func
|= GDK_FUNC_CLOSE
;
138 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
140 func
|= GDK_FUNC_MINIMIZE
;
141 decor
|= GDK_DECOR_MINIMIZE
;
143 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
145 decor
|= GDK_DECOR_MAXIMIZE
;
146 func
|= GDK_FUNC_MAXIMIZE
;
148 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
150 func
|= GDK_FUNC_RESIZE
;
151 decor
|= GDK_DECOR_RESIZEH
;
153 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
154 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
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);
160 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
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
;
168 gdk_window_set_hints( win
->m_widget
->window
,
170 win
->GetMinWidth(), win
->GetMinHeight(),
171 win
->GetMaxWidth(), win
->GetMaxHeight(),
176 if (win
->m_icon
!= wxNullIcon
)
178 wxIcon
icon( win
->m_icon
);
179 win
->m_icon
= wxNullIcon
;
180 win
->SetIcon( icon
);
186 //-----------------------------------------------------------------------------
187 // InsertChild for wxDialog
188 //-----------------------------------------------------------------------------
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. */
195 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
197 gtk_myfixed_put( GTK_MYFIXED(parent
->m_wxwindow
),
198 GTK_WIDGET(child
->m_widget
),
204 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
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
);
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
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
)
224 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
226 void wxDialog::Init()
230 m_modalShowing
= FALSE
;
234 wxDialog::wxDialog( wxWindow
*parent
,
235 wxWindowID id
, const wxString
&title
,
236 const wxPoint
&pos
, const wxSize
&size
,
237 long style
, const wxString
&name
)
241 Create( parent
, id
, title
, pos
, size
, style
, name
);
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
)
249 wxTopLevelWindows
.Append( this );
251 m_needParent
= FALSE
;
253 if (!PreCreation( parent
, pos
, size
) ||
254 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
256 wxFAIL_MSG( wxT("wxDialog creation failed") );
260 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
262 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
265 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
267 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
269 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
270 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
272 m_wxwindow
= gtk_myfixed_new();
273 gtk_widget_show( m_wxwindow
);
274 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
276 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
280 if (m_parent
) m_parent
->AddChild( this );
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 );
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 );
293 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
294 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
299 wxDialog::~wxDialog()
301 m_isBeingDeleted
= TRUE
;
303 wxTopLevelWindows
.DeleteObject( this );
305 if (wxTheApp
->GetTopWindow() == this)
307 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
310 if (wxTopLevelWindows
.Number() == 0)
312 wxTheApp
->ExitMainLoop();
316 void wxDialog::SetTitle( const wxString
& title
)
319 if (m_title
.IsNull()) m_title
= wxT("");
320 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
323 wxString
wxDialog::GetTitle() const
325 return (wxString
&)m_title
;
328 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
330 if (Validate()) TransferDataFromWindow();
333 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
337 EndModal(wxID_CANCEL
);
341 SetReturnCode(wxID_CANCEL
);
346 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
348 if (Validate() && TransferDataFromWindow())
356 SetReturnCode(wxID_OK
);
362 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
367 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
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().
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
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.
383 static wxList s_closing
;
385 if (s_closing
.Member(this))
388 s_closing
.Append(this);
390 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
391 cancelEvent
.SetEventObject( this );
392 GetEventHandler()->ProcessEvent(cancelEvent
);
393 s_closing
.DeleteObject(this);
396 bool wxDialog::Destroy()
398 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
403 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
405 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
407 #if wxUSE_CONSTRAINTS
413 #endif // wxUSE_CONSTRAINTS
415 /* no child: go out ! */
416 if (!GetChildren().First()) return;
418 /* do we have exactly one child? */
419 wxWindow
*child
= (wxWindow
*) NULL
;
420 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
422 wxWindow
*win
= (wxWindow
*)node
->Data();
423 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
425 /* it's the second one: do nothing */
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
);
438 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
440 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
441 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
443 if (m_resizing
) return; /* I don't like recursions */
448 int old_width
= m_width
;
449 int old_height
= m_height
;
451 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
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
;
466 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
468 if (width
== -1) m_width
= 80;
471 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
473 if (height
== -1) m_height
= 26;
476 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
477 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
478 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
479 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
481 if ((m_x
!= -1) || (m_y
!= -1))
483 if ((m_x
!= old_x
) || (m_y
!= old_y
))
485 /* we set the position here and when showing the dialog
486 for the first time in idle time */
487 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
491 if ((m_width
!= old_width
) || (m_height
!= old_height
))
493 /* actual resizing is deferred to GtkOnSize in idle time and
494 when showing the dialog */
501 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
503 // due to a bug in gtk, x,y are always 0
507 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
508 if (!m_wxwindow
) return;
513 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
514 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
515 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
516 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
518 /* we actually set the size of a frame here and no-where else */
519 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
523 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
524 event
.SetEventObject( this );
525 GetEventHandler()->ProcessEvent( event
);
528 void wxDialog::OnInternalIdle()
530 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
531 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
533 wxWindow::OnInternalIdle();
536 bool wxDialog::Show( bool show
)
538 if (!show
&& IsModal())
540 EndModal( wxID_CANCEL
);
543 if (show
&& !m_sizeSet
)
545 /* by calling GtkOnSize here, we don't have to call
546 either after showing the frame, which would entail
547 much ugly flicker nor from within the size_allocate
548 handler, because GTK 1.1.X forbids that. */
550 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
553 bool ret
= wxWindow::Show( show
);
555 if (show
) InitDialog();
560 bool wxDialog::IsModal() const
562 return m_modalShowing
;
565 void wxDialog::SetModal( bool WXUNUSED(flag
) )
569 m_windowStyle |= wxDIALOG_MODAL;
571 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
573 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
576 int wxDialog::ShowModal()
580 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
581 return GetReturnCode();
584 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
588 m_modalShowing
= TRUE
;
590 gtk_grab_add( m_widget
);
592 gtk_grab_remove( m_widget
);
594 return GetReturnCode();
597 void wxDialog::EndModal( int retCode
)
599 SetReturnCode( retCode
);
603 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
607 m_modalShowing
= FALSE
;
614 void wxDialog::InitDialog()
616 wxWindow::InitDialog();
619 void wxDialog::SetIcon( const wxIcon
&icon
)
622 if (!icon
.Ok()) return;
624 if (!m_widget
->window
) return;
626 wxMask
*mask
= icon
.GetMask();
627 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
628 if (mask
) bm
= mask
->GetBitmap();
630 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);