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"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern wxList wxPendingDelete
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
42 wxapp_install_idle_handler();
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
56 wxapp_install_idle_handler();
58 if (!win
->m_hasVMT
) return;
60 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
62 win
->m_width
= alloc
->width
;
63 win
->m_height
= alloc
->height
;
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
72 static gint
gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
75 wxapp_install_idle_handler();
77 if (!win
->m_hasVMT
) return FALSE
;
82 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
83 mevent
.SetEventObject( win
);
84 win
->GetEventHandler()->ProcessEvent( mevent
);
89 //-----------------------------------------------------------------------------
90 // "realize" from m_widget
91 //-----------------------------------------------------------------------------
93 /* we cannot MWM hints and icons before the widget has been realized,
94 so we do this directly after realization */
97 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
99 if (g_isIdle
) wxapp_install_idle_handler();
102 if (win
->m_icon
!= wxNullIcon
)
104 wxIcon
icon( win
->m_icon
);
105 win
->m_icon
= wxNullIcon
;
106 win
->SetIcon( icon
);
112 //-----------------------------------------------------------------------------
113 // "map" from m_widget
114 //-----------------------------------------------------------------------------
117 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
119 /* I haven''t been able to set the position of
120 the dialog before it is shown, so I do it here */
121 gtk_widget_set_uposition( widget
, win
->m_x
, win
->m_y
);
123 /* all this is for Motif Window Manager "hints" and is supposed to be
124 recognized by other WM as well. not tested. */
125 long decor
= (long) GDK_DECOR_BORDER
;
126 long func
= (long) GDK_FUNC_MOVE
;
128 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
129 decor
|= GDK_DECOR_TITLE
;
130 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
132 decor
|= GDK_DECOR_MENU
;
133 func
|= GDK_FUNC_CLOSE
;
135 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
137 func
|= GDK_FUNC_MINIMIZE
;
138 decor
|= GDK_DECOR_MINIMIZE
;
140 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
142 decor
|= GDK_DECOR_MAXIMIZE
;
143 func
|= GDK_FUNC_MAXIMIZE
;
145 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
147 func
|= GDK_FUNC_RESIZE
;
148 decor
|= GDK_DECOR_RESIZEH
;
150 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
151 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
153 /* GTK's shrinking/growing policy */
154 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
155 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
157 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
167 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
168 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
169 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
170 EVT_SIZE (wxDialog::OnSize
)
171 EVT_CLOSE (wxDialog::OnCloseWindow
)
174 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
176 void wxDialog::Init()
180 m_modalShowing
= FALSE
;
183 wxDialog::wxDialog( wxWindow
*parent
,
184 wxWindowID id
, const wxString
&title
,
185 const wxPoint
&pos
, const wxSize
&size
,
186 long style
, const wxString
&name
)
190 Create( parent
, id
, title
, pos
, size
, style
, name
);
193 bool wxDialog::Create( wxWindow
*parent
,
194 wxWindowID id
, const wxString
&title
,
195 const wxPoint
&pos
, const wxSize
&size
,
196 long style
, const wxString
&name
)
198 wxTopLevelWindows
.Append( this );
200 m_needParent
= FALSE
;
202 PreCreation( parent
, id
, pos
, size
, style
, name
);
204 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
207 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
209 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
211 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
212 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
214 m_wxwindow
= gtk_myfixed_new();
215 gtk_widget_show( m_wxwindow
);
216 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
218 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
222 if (m_parent
) m_parent
->AddChild( this );
226 /* we cannot set MWM hints before the widget has
227 been realized, so we do this directly after realization */
228 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
229 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
231 /* we set the position of the window after the map event. setting it
232 before has no effect (with KWM) */
233 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
234 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
236 /* the user resized the frame by dragging etc. */
237 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
238 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
240 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
241 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
246 wxDialog::~wxDialog()
248 wxTopLevelWindows
.DeleteObject( this );
250 if (wxTheApp
->GetTopWindow() == this)
252 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
255 if (wxTopLevelWindows
.Number() == 0)
257 wxTheApp
->ExitMainLoop();
261 void wxDialog::SetTitle( const wxString
& title
)
264 if (m_title
.IsNull()) m_title
= _T("");
265 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
268 wxString
wxDialog::GetTitle() const
270 return (wxString
&)m_title
;
273 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
275 if (Validate()) TransferDataFromWindow();
278 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
282 EndModal(wxID_CANCEL
);
286 SetReturnCode(wxID_CANCEL
);
291 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
293 printf( "OnOK 1.\n" );
294 if (Validate() && TransferDataFromWindow())
296 printf( "OnOK 2.\n" );
299 printf( "OnOK: modal.\n" );
304 printf( "OnOK: non-modal.\n" );
305 SetReturnCode(wxID_OK
);
311 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
316 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
318 // We'll send a Cancel message by default,
319 // which may close the dialog.
320 // Check for looping if the Cancel event handler calls Close().
322 // Note that if a cancel button and handler aren't present in the dialog,
323 // nothing will happen when you close the dialog via the window manager, or
325 // We wouldn't want to destroy the dialog by default, since the dialog may have been
326 // created on the stack.
327 // However, this does mean that calling dialog->Close() won't delete the dialog
328 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
329 // sure to destroy the dialog.
330 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
332 static wxList s_closing
;
334 if (s_closing
.Member(this))
337 s_closing
.Append(this);
339 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
340 cancelEvent
.SetEventObject( this );
341 GetEventHandler()->ProcessEvent(cancelEvent
);
342 s_closing
.DeleteObject(this);
345 bool wxDialog::Destroy()
347 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
352 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
354 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
362 /* no child: go out ! */
363 if (!GetChildren().First()) return;
365 /* do we have exactly one child? */
366 wxWindow
*child
= (wxWindow
*) NULL
;
367 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
369 wxWindow
*win
= (wxWindow
*)node
->Data();
370 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
372 /* it's the second one: do nothing */
378 /* yes: set it's size to fill all the frame */
379 int client_x
, client_y
;
380 GetClientSize( &client_x
, &client_y
);
381 child
->SetSize( 1, 1, client_x
-2, client_y
);
385 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
387 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
388 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
390 if (m_resizing
) return; /* I don't like recursions */
395 int old_width
= m_width
;
396 int old_height
= m_height
;
398 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
400 if (x
!= -1) m_x
= x
;
401 if (y
!= -1) m_y
= y
;
402 if (width
!= -1) m_width
= width
;
403 if (height
!= -1) m_height
= height
;
413 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
415 if (width
== -1) m_width
= 80;
418 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
420 if (height
== -1) m_height
= 26;
423 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
424 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
425 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
426 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
428 if ((m_x
!= -1) || (m_y
!= -1))
430 if ((m_x
!= old_x
) || (m_y
!= old_y
))
432 /* we set the position here and when showing the dialog
433 for the first time in idle time */
434 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
438 if ((m_width
!= old_width
) || (m_height
!= old_height
))
440 /* actual resizing is deferred to GtkOnSize in idle time and
441 when showing the dialog */
448 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
450 // due to a bug in gtk, x,y are always 0
454 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
455 if (!m_wxwindow
) return;
460 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
461 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
462 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
463 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
465 /* we actually set the size of a frame here and no-where else */
466 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
470 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
471 event
.SetEventObject( this );
472 GetEventHandler()->ProcessEvent( event
);
475 void wxDialog::Centre( int direction
)
477 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
482 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
483 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
488 void wxDialog::OnInternalIdle()
490 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
491 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
494 bool wxDialog::Show( bool show
)
496 if (!show
&& IsModal())
498 EndModal( wxID_CANCEL
);
501 if (show
&& !m_sizeSet
)
503 /* by calling GtkOnSize here, we don't have to call
504 either after showing the frame, which would entail
505 much ugly flicker nor from within the size_allocate
506 handler, because GTK 1.1.X forbids that. */
508 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
511 bool ret
= wxWindow::Show( show
);
513 if (show
) InitDialog();
518 bool wxDialog::IsModal() const
520 return m_modalShowing
;
523 void wxDialog::SetModal( bool WXUNUSED(flag
) )
527 m_windowStyle |= wxDIALOG_MODAL;
529 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
531 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
534 int wxDialog::ShowModal()
538 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
539 return GetReturnCode();
544 m_modalShowing
= TRUE
;
546 gtk_grab_add( m_widget
);
548 gtk_grab_remove( m_widget
);
550 return GetReturnCode();
553 void wxDialog::EndModal( int retCode
)
555 SetReturnCode( retCode
);
559 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
563 m_modalShowing
= FALSE
;
570 void wxDialog::InitDialog()
572 wxWindow::InitDialog();
575 void wxDialog::SetIcon( const wxIcon
&icon
)
578 if (!icon
.Ok()) return;
580 wxMask
*mask
= icon
.GetMask();
581 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
582 if (mask
) bm
= mask
->GetBitmap();
584 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);