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 /* 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.
135 This avoids the problem, but looks odd. What shall we do?
137 decor
|= GDK_DECOR_RESIZEH
;
139 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
140 decor
|= GDK_DECOR_TITLE
;
141 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
143 decor
|= GDK_DECOR_MENU
;
144 func
|= GDK_FUNC_CLOSE
;
146 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
148 func
|= GDK_FUNC_MINIMIZE
;
149 decor
|= GDK_DECOR_MINIMIZE
;
151 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
153 decor
|= GDK_DECOR_MAXIMIZE
;
154 func
|= GDK_FUNC_MAXIMIZE
;
156 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
158 func
|= GDK_FUNC_RESIZE
;
159 decor
|= GDK_DECOR_RESIZEH
;
161 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
162 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
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);
168 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
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
;
176 gdk_window_set_hints( win
->m_widget
->window
,
178 win
->GetMinWidth(), win
->GetMinHeight(),
179 win
->GetMaxWidth(), win
->GetMaxHeight(),
184 if (win
->m_icon
!= wxNullIcon
)
186 wxIcon
icon( win
->m_icon
);
187 win
->m_icon
= wxNullIcon
;
188 win
->SetIcon( icon
);
194 //-----------------------------------------------------------------------------
195 // InsertChild for wxDialog
196 //-----------------------------------------------------------------------------
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. */
203 static void wxInsertChildInDialog( wxDialog
* parent
, wxWindow
* child
)
205 gtk_pizza_put( GTK_PIZZA(parent
->m_wxwindow
),
206 GTK_WIDGET(child
->m_widget
),
212 if (parent
->HasFlag(wxTAB_TRAVERSAL
))
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
);
220 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
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
)
232 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
234 void wxDialog::Init()
238 m_modalShowing
= FALSE
;
242 wxDialog::wxDialog( wxWindow
*parent
,
243 wxWindowID id
, const wxString
&title
,
244 const wxPoint
&pos
, const wxSize
&size
,
245 long style
, const wxString
&name
)
249 Create( parent
, id
, title
, pos
, size
, style
, name
);
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
)
257 wxTopLevelWindows
.Append( this );
259 m_needParent
= FALSE
;
261 if (!PreCreation( parent
, pos
, size
) ||
262 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
264 wxFAIL_MSG( wxT("wxDialog creation failed") );
268 m_insertCallback
= (wxInsertChildFunction
) wxInsertChildInDialog
;
270 m_widget
= gtk_window_new( GTK_WINDOW_DIALOG
);
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
) );
276 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
278 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
280 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
281 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
283 m_wxwindow
= gtk_pizza_new();
284 gtk_widget_show( m_wxwindow
);
285 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
287 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
291 if (m_parent
) m_parent
->AddChild( this );
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 );
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 );
304 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
305 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
310 wxDialog::~wxDialog()
312 m_isBeingDeleted
= TRUE
;
314 wxTopLevelWindows
.DeleteObject( this );
316 if (wxTheApp
->GetTopWindow() == this)
318 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
321 if (wxTopLevelWindows
.Number() == 0)
323 wxTheApp
->ExitMainLoop();
327 void wxDialog::SetTitle( const wxString
& title
)
330 if (m_title
.IsNull()) m_title
= wxT("");
331 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
334 wxString
wxDialog::GetTitle() const
336 return (wxString
&)m_title
;
339 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
341 if (Validate()) TransferDataFromWindow();
344 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
348 EndModal(wxID_CANCEL
);
352 SetReturnCode(wxID_CANCEL
);
357 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
359 if (Validate() && TransferDataFromWindow())
367 SetReturnCode(wxID_OK
);
373 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
378 void wxDialog::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
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().
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
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.
394 static wxList s_closing
;
396 if (s_closing
.Member(this))
399 s_closing
.Append(this);
401 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
402 cancelEvent
.SetEventObject( this );
403 GetEventHandler()->ProcessEvent(cancelEvent
);
404 s_closing
.DeleteObject(this);
407 bool wxDialog::Destroy()
409 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
414 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
416 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
418 #if wxUSE_CONSTRAINTS
424 #endif // wxUSE_CONSTRAINTS
426 /* no child: go out ! */
427 if (!GetChildren().First()) return;
429 /* do we have exactly one child? */
430 wxWindow
*child
= (wxWindow
*) NULL
;
431 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
433 wxWindow
*win
= (wxWindow
*)node
->Data();
434 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
436 /* it's the second one: do nothing */
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
);
449 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
451 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid dialog") );
452 wxASSERT_MSG( (m_wxwindow
!= NULL
), wxT("invalid dialog") );
454 if (m_resizing
) return; /* I don't like recursions */
460 int old_width
= m_width
;
461 int old_height
= m_height
;
463 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0)
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
;
479 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
481 if (width == -1) m_width = 80;
484 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
486 if (height == -1) m_height = 26;
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
;
495 if ((m_x
!= -1) || (m_y
!= -1))
497 if ((m_x
!= old_x
) || (m_y
!= old_y
))
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
);
505 if ((m_width
!= old_width
) || (m_height
!= old_height
))
507 /* actual resizing is deferred to GtkOnSize in idle time and
508 when showing the dialog */
515 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
517 // due to a bug in gtk, x,y are always 0
521 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
522 if (!m_wxwindow
) return;
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
;
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
);
537 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
538 event
.SetEventObject( this );
539 GetEventHandler()->ProcessEvent( event
);
542 void wxDialog::OnInternalIdle()
544 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
545 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
547 wxWindow::OnInternalIdle();
550 bool wxDialog::Show( bool show
)
552 if (!show
&& IsModal())
554 EndModal( wxID_CANCEL
);
557 if (show
&& !m_sizeSet
)
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. */
564 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
567 bool ret
= wxWindow::Show( show
);
569 if (show
) InitDialog();
574 bool wxDialog::IsModal() const
576 return m_modalShowing
;
579 void wxDialog::SetModal( bool WXUNUSED(flag
) )
583 m_windowStyle |= wxDIALOG_MODAL;
585 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
587 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
590 int wxDialog::ShowModal()
594 wxFAIL_MSG( wxT("wxDialog:ShowModal called twice") );
595 return GetReturnCode();
598 wxBusyCursorSuspender cs
; // temporarily suppress the busy cursor
602 m_modalShowing
= TRUE
;
604 gtk_grab_add( m_widget
);
606 gtk_grab_remove( m_widget
);
608 return GetReturnCode();
611 void wxDialog::EndModal( int retCode
)
613 SetReturnCode( retCode
);
617 wxFAIL_MSG( wxT("wxDialog:EndModal called twice") );
621 m_modalShowing
= FALSE
;
628 void wxDialog::InitDialog()
630 wxWindow::InitDialog();
633 void wxDialog::SetIcon( const wxIcon
&icon
)
636 if (!icon
.Ok()) return;
638 if (!m_widget
->window
) return;
640 wxMask
*mask
= icon
.GetMask();
641 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
642 if (mask
) bm
= mask
->GetBitmap();
644 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);