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();
34 extern int g_openDialogs
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern wxList wxPendingDelete
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
49 wxapp_install_idle_handler();
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
63 wxapp_install_idle_handler();
65 if (!win
->m_hasVMT
) return;
67 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
69 win
->m_width
= alloc
->width
;
70 win
->m_height
= alloc
->height
;
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
80 #if (GTK_MINOR_VERSION > 0)
81 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*WXUNUSED(event
), wxDialog
*win
)
83 gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
87 wxapp_install_idle_handler();
89 if (!win
->m_hasVMT
) return FALSE
;
91 #if (GTK_MINOR_VERSION > 0)
94 gdk_window_get_root_origin( win
->m_widget
->window
, &x
, &y
);
102 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
103 mevent
.SetEventObject( win
);
104 win
->GetEventHandler()->ProcessEvent( mevent
);
109 //-----------------------------------------------------------------------------
110 // "realize" from m_widget
111 //-----------------------------------------------------------------------------
113 /* we cannot MWM hints and icons before the widget has been realized,
114 so we do this directly after realization */
117 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
120 wxapp_install_idle_handler();
122 /* I haven't been able to set the position of
123 the dialog before it is shown, so I set the
124 position in "realize" */
125 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
127 /* all this is for Motif Window Manager "hints" and is supposed to be
128 recognized by other WM as well. not tested. */
129 long decor
= (long) GDK_DECOR_BORDER
;
130 long func
= (long) GDK_FUNC_MOVE
;
132 /* Some WM don't display any border around the frame contents if
133 used with these hints, so we add a resize border around it,
134 without automatically allowinng it to be resized though.
136 This avoids the problem, but looks odd. What shall we do?
138 decor
|= GDK_DECOR_RESIZEH
;
140 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
141 decor
|= GDK_DECOR_TITLE
;
142 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
144 decor
|= GDK_DECOR_MENU
;
145 func
|= GDK_FUNC_CLOSE
;
147 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
149 func
|= GDK_FUNC_MINIMIZE
;
150 decor
|= GDK_DECOR_MINIMIZE
;
152 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
154 decor
|= GDK_DECOR_MAXIMIZE
;
155 func
|= GDK_FUNC_MAXIMIZE
;
157 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
159 func
|= GDK_FUNC_RESIZE
;
160 decor
|= GDK_DECOR_RESIZEH
;
162 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
163 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
165 /* GTK's shrinking/growing policy */
166 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
167 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
169 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
172 gint flag
= 0; // GDK_HINT_POS;
173 if ((win
->GetMinWidth() != -1) || (win
->GetMinHeight() != -1)) flag
|= GDK_HINT_MIN_SIZE
;
174 if ((win
->GetMaxWidth() != -1) || (win
->GetMaxHeight() != -1)) flag
|= GDK_HINT_MAX_SIZE
;
177 gdk_window_set_hints( win
->m_widget
->window
,
179 win
->GetMinWidth(), win
->GetMinHeight(),
180 win
->GetMaxWidth(), win
->GetMaxHeight(),
185 if (win
->m_icon
!= wxNullIcon
)
187 wxIcon
icon( win
->m_icon
);
188 win
->m_icon
= wxNullIcon
;
189 win
->SetIcon( icon
);
195 //-----------------------------------------------------------------------------
196 // InsertChild for wxDialog
197 //-----------------------------------------------------------------------------
199 /* Callback for wxFrame. This very strange beast has to be used because
200 * C++ has no virtual methods in a constructor. We have to emulate a
201 * virtual function here as wxWindows requires different ways to insert
202 * a child in container classes. */
204 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
206 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
207 GTK_WIDGET(child
->m_widget
),
213 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
215 /* we now allow a window to get the focus as long as it
216 doesn't have any children. */
217 GTK_WIDGET_UNSET_FLAGS( parent
->m_wxwindow
, GTK_CAN_FOCUS
);
221 //-----------------------------------------------------------------------------
223 //-----------------------------------------------------------------------------
225 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
226 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
227 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
228 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
229 EVT_SIZE (wxDialog::OnSize
)
230 EVT_CLOSE (wxDialog::OnCloseWindow
)
233 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
235 void wxDialog::Init()
239 m_modalShowing
= FALSE
;
243 wxDialog::wxDialog( wxWindow
*parent
,
244 wxWindowID id
, const wxString
&title
,
245 const wxPoint
&pos
, const wxSize
&size
,
246 long style
, const wxString
&name
)
250 Create( parent
, id
, title
, pos
, size
, style
, name
);
253 bool wxDialog::Create( wxWindow
*parent
,
254 wxWindowID id
, const wxString
&title
,
255 const wxPoint
&pos
, const wxSize
&size
,
256 long style
, const wxString
&name
)
260 wxTopLevelWindows
.Append( this );
262 m_needParent
= FALSE
;
264 if (!PreCreation( parent
, pos
, size
) ||
265 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
267 wxFAIL_MSG( wxT("wxDialog creation failed") );
271 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
273 m_widget
= gtk_window_new( GTK_WINDOW_DIALOG
);
275 if ((m_parent
) && (GTK_IS_WINDOW(m_parent
->m_widget
)))
276 gtk_window_set_transient_for( GTK_WINDOW(m_widget
), GTK_WINDOW(m_parent
->m_widget
) );
279 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
281 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
283 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
284 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
286 m_wxwindow
= gtk_pizza_new();
287 gtk_widget_show( m_wxwindow
);
288 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
290 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
294 if (m_parent
) m_parent
->AddChild( this );
298 /* we cannot set MWM hints before the widget has
299 been realized, so we do this directly after realization */
300 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
301 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
303 /* the user resized the frame by dragging etc. */
304 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
305 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
307 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
308 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
313 wxDialog::~wxDialog()
315 m_isBeingDeleted
= TRUE
;
317 wxTopLevelWindows
.DeleteObject( this );
319 if (wxTheApp
->GetTopWindow() == this)
321 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
324 if (wxTopLevelWindows
.Number() == 0)
326 wxTheApp
->ExitMainLoop();
332 void wxDialog::SetTitle( const wxString
& title
)
335 if (m_title
.IsNull()) m_title
= wxT("");
336 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
339 wxString
wxDialog::GetTitle() const
341 return (wxString
&)m_title
;
344 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
346 if (Validate()) TransferDataFromWindow();
349 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
353 EndModal(wxID_CANCEL
);
357 SetReturnCode(wxID_CANCEL
);
362 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
364 if (Validate() && TransferDataFromWindow())
372 SetReturnCode(wxID_OK
);
378 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
383 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
385 // We'll send a Cancel message by default,
386 // which may close the dialog.
387 // Check for looping if the Cancel event handler calls Close().
389 // Note that if a cancel button and handler aren't present in the dialog,
390 // nothing will happen when you close the dialog via the window manager, or
392 // We wouldn't want to destroy the dialog by default, since the dialog may have been
393 // created on the stack.
394 // However, this does mean that calling dialog->Close() won't delete the dialog
395 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
396 // sure to destroy the dialog.
397 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
399 static wxList s_closing
;
401 if (s_closing
.Member(this))
404 s_closing
.Append(this);
406 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
407 cancelEvent
.SetEventObject( this );
408 GetEventHandler()->ProcessEvent(cancelEvent
);
409 s_closing
.DeleteObject(this);
412 bool wxDialog::Destroy()
414 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
419 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
421 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
423 #if wxUSE_CONSTRAINTS
429 #endif // wxUSE_CONSTRAINTS
431 /* no child: go out ! */
432 if (!GetChildren().First()) return;
434 /* do we have exactly one child? */
435 wxWindow
*child
= (wxWindow
*) NULL
;
436 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
438 wxWindow
*win
= (wxWindow
*)node
->Data();
439 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
441 /* it's the second one: do nothing */
447 /* yes: set it's size to fill all the frame */
448 int client_x
, client_y
;
449 GetClientSize( &client_x
, &client_y
);
450 child
->SetSize( 1, 1, client_x
-2, client_y
);
454 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
456 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
457 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
459 if (m_resizing
) return; /* I don't like recursions */
465 int old_width
= m_width
;
466 int old_height
= m_height
;
468 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
470 if (x
!= -1) m_x
= x
;
471 if (y
!= -1) m_y
= y
;
472 if (width
!= -1) m_width
= width
;
473 if (height
!= -1) m_height
= height
;
484 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
486 if (width == -1) m_width = 80;
489 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
491 if (height == -1) m_height = 26;
495 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
496 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
497 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
498 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
500 if ((m_x
!= -1) || (m_y
!= -1))
502 if ((m_x
!= old_x
) || (m_y
!= old_y
))
504 /* we set the position here and when showing the dialog
505 for the first time in idle time */
506 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
510 if ((m_width
!= old_width
) || (m_height
!= old_height
))
512 /* actual resizing is deferred to GtkOnSize in idle time and
513 when showing the dialog */
520 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
522 // due to a bug in gtk, x,y are always 0
526 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
527 if (!m_wxwindow
) return;
532 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
533 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
534 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
535 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
537 /* we actually set the size of a frame here and no-where else */
538 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
542 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
543 event
.SetEventObject( this );
544 GetEventHandler()->ProcessEvent( event
);
547 void wxDialog::OnInternalIdle()
549 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
550 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
552 wxWindow::OnInternalIdle();
555 bool wxDialog::Show( bool show
)
557 if (!show
&& IsModal())
559 EndModal( wxID_CANCEL
);
562 if (show
&& !m_sizeSet
)
564 /* by calling GtkOnSize here, we don't have to call
565 either after showing the frame, which would entail
566 much ugly flicker nor from within the size_allocate
567 handler, because GTK 1.1.X forbids that. */
569 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
572 bool ret
= wxWindow::Show( show
);
574 if (show
) InitDialog();
579 bool wxDialog::IsModal() const
581 return m_modalShowing
;
584 void wxDialog::SetModal( bool WXUNUSED(flag
) )
588 m_windowStyle |= wxDIALOG_MODAL;
590 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
592 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
595 int wxDialog::ShowModal()
599 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
600 return GetReturnCode();
603 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
607 m_modalShowing
= TRUE
;
609 gtk_grab_add( m_widget
);
611 gtk_grab_remove( m_widget
);
613 return GetReturnCode();
616 void wxDialog::EndModal( int retCode
)
618 SetReturnCode( retCode
);
622 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
626 m_modalShowing
= FALSE
;
633 void wxDialog::InitDialog()
635 wxWindow::InitDialog();
638 void wxDialog::SetIcon( const wxIcon
&icon
)
641 if (!icon
.Ok()) return;
643 if (!m_widget
->window
) return;
645 wxMask
*mask
= icon
.GetMask();
646 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
647 if (mask
) bm
= mask
->GetBitmap();
649 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);