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
)
41 if (g_isIdle
) wxapp_install_idle_handler();
44 printf( "OnDelete from " );
45 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
46 printf( win->GetClassInfo()->GetClassName() );
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
61 if (g_isIdle
) wxapp_install_idle_handler();
63 if (!win
->HasVMT()) return;
66 printf( "OnDialogResize from " );
67 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
68 printf( win->GetClassInfo()->GetClassName() );
72 if ((win
->GetWidth() != alloc
->width
) || (win
->GetHeight() != alloc
->height
))
74 win
->InternalSetSize( alloc
->width
, alloc
->height
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
82 static gint
gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
84 if (g_isIdle
) wxapp_install_idle_handler();
86 if (!win
->HasVMT()) return FALSE
;
88 win
->InternalSetPosition(event
->x
, event
->y
);
90 wxMoveEvent
mevent( wxPoint(win
->GetX(),win
->GetY()), win
->GetId() );
91 mevent
.SetEventObject( win
);
92 win
->GetEventHandler()->ProcessEvent( mevent
);
97 //-----------------------------------------------------------------------------
98 // "realize" from m_widget
99 //-----------------------------------------------------------------------------
101 /* we cannot MWM hints and icons before the widget has been realized,
102 so we do this directly after realization */
105 gtk_dialog_realized_callback( GtkWidget
*widget
, wxDialog
*win
)
107 if (g_isIdle
) wxapp_install_idle_handler();
110 if (win
->m_icon
!= wxNullIcon
)
112 wxIcon
icon( win
->m_icon
);
113 win
->m_icon
= wxNullIcon
;
114 win
->SetIcon( icon
);
120 //-----------------------------------------------------------------------------
121 // "map" from m_widget
122 //-----------------------------------------------------------------------------
125 gtk_dialog_map_callback( GtkWidget
*widget
, wxDialog
*win
)
127 /* I haven''t been able to set the position of
128 the dialog before it is shown, so I do it here */
129 gtk_widget_set_uposition( widget
, win
->GetX(), win
->GetY() );
131 /* all this is for Motif Window Manager "hints" and is supposed to be
132 recognized by other WM as well. not tested. */
133 long decor
= (long) GDK_DECOR_BORDER
;
134 long func
= (long) GDK_FUNC_MOVE
;
136 if ((win
->GetWindowStyle() & wxCAPTION
) != 0)
137 decor
|= GDK_DECOR_TITLE
;
138 if ((win
->GetWindowStyle() & wxSYSTEM_MENU
) != 0)
140 decor
|= GDK_DECOR_MENU
;
141 func
|= GDK_FUNC_CLOSE
;
143 if ((win
->GetWindowStyle() & wxMINIMIZE_BOX
) != 0)
145 func
|= GDK_FUNC_MINIMIZE
;
146 decor
|= GDK_DECOR_MINIMIZE
;
148 if ((win
->GetWindowStyle() & wxMAXIMIZE_BOX
) != 0)
150 decor
|= GDK_DECOR_MAXIMIZE
;
151 func
|= GDK_FUNC_MAXIMIZE
;
153 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) != 0)
155 func
|= GDK_FUNC_RESIZE
;
156 decor
|= GDK_DECOR_RESIZEH
;
158 gdk_window_set_decorations( win
->GetHandle()->window
, (GdkWMDecoration
)decor
);
159 gdk_window_set_functions( win
->GetHandle()->window
, (GdkWMFunction
)func
);
161 /* GTK's shrinking/growing policy */
162 if ((win
->GetWindowStyle() & wxRESIZE_BORDER
) == 0)
163 gtk_window_set_policy(GTK_WINDOW(win
->GetHandle()), 0, 0, 1);
165 gtk_window_set_policy(GTK_WINDOW(win
->GetHandle()), 1, 1, 1);
170 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
174 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
175 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
176 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
177 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
178 EVT_SIZE (wxDialog::OnSize
)
179 EVT_CLOSE (wxDialog::OnCloseWindow
)
182 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
184 void wxDialog::Init()
188 m_modalShowing
= FALSE
;
191 wxDialog::wxDialog( wxWindow
*parent
,
192 wxWindowID id
, const wxString
&title
,
193 const wxPoint
&pos
, const wxSize
&size
,
194 long style
, const wxString
&name
)
198 Create( parent
, id
, title
, pos
, size
, style
, name
);
201 bool wxDialog::Create( wxWindow
*parent
,
202 wxWindowID id
, const wxString
&title
,
203 const wxPoint
&pos
, const wxSize
&size
,
204 long style
, const wxString
&name
)
206 wxTopLevelWindows
.Append( this );
208 m_needParent
= FALSE
;
210 PreCreation( parent
, id
, pos
, size
, style
, name
);
212 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
215 gtk_window_set_wmclass( GTK_WINDOW(m_widget
), name
.mb_str(), name
.mb_str() );
217 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
219 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
220 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
222 m_wxwindow
= gtk_myfixed_new();
223 gtk_widget_show( m_wxwindow
);
224 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
226 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
230 if (m_parent
) m_parent
->AddChild( this );
234 /* we cannot set MWM hints before the widget has
235 been realized, so we do this directly after realization */
236 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
237 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
239 /* we set the position of the window after the map event. setting it
240 before has no effect (with KWM) */
241 gtk_signal_connect( GTK_OBJECT(m_widget
), "map",
242 GTK_SIGNAL_FUNC(gtk_dialog_map_callback
), (gpointer
) this );
244 /* the user resized the frame by dragging etc. */
245 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
246 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
248 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
249 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
254 wxDialog::~wxDialog()
256 wxTopLevelWindows
.DeleteObject( this );
258 if (wxTheApp
->GetTopWindow() == this)
260 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
263 if (wxTopLevelWindows
.Number() == 0)
265 wxTheApp
->ExitMainLoop();
269 void wxDialog::SetTitle( const wxString
& title
)
272 if (m_title
.IsNull()) m_title
= _T("");
273 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
276 wxString
wxDialog::GetTitle() const
278 return (wxString
&)m_title
;
281 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
283 if (Validate()) TransferDataFromWindow();
286 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
290 EndModal(wxID_CANCEL
);
294 SetReturnCode(wxID_CANCEL
);
299 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
301 if ( Validate() && TransferDataFromWindow())
309 SetReturnCode(wxID_OK
);
315 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
320 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
322 // We'll send a Cancel message by default,
323 // which may close the dialog.
324 // Check for looping if the Cancel event handler calls Close().
326 // Note that if a cancel button and handler aren't present in the dialog,
327 // nothing will happen when you close the dialog via the window manager, or
329 // We wouldn't want to destroy the dialog by default, since the dialog may have been
330 // created on the stack.
331 // However, this does mean that calling dialog->Close() won't delete the dialog
332 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
333 // sure to destroy the dialog.
334 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
336 static wxList s_closing
;
338 if (s_closing
.Member(this))
341 s_closing
.Append(this);
343 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
344 cancelEvent
.SetEventObject( this );
345 GetEventHandler()->ProcessEvent(cancelEvent
);
346 s_closing
.DeleteObject(this);
349 bool wxDialog::Destroy()
351 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
356 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
358 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
366 /* no child: go out ! */
367 if (!GetChildren().First()) return;
369 /* do we have exactly one child? */
370 wxWindow
*child
= (wxWindow
*) NULL
;
371 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
373 wxWindow
*win
= (wxWindow
*)node
->Data();
374 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
376 /* it's the second one: do nothing */
382 /* yes: set it's size to fill all the frame */
383 int client_x
, client_y
;
384 GetClientSize( &client_x
, &client_y
);
385 child
->SetSize( 1, 1, client_x
-2, client_y
);
389 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
391 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
392 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
394 if (m_resizing
) return; /* I don't like recursions */
399 int old_width
= m_width
;
400 int old_height
= m_height
;
402 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
404 if (x
!= -1) m_x
= x
;
405 if (y
!= -1) m_y
= y
;
406 if (width
!= -1) m_width
= width
;
407 if (height
!= -1) m_height
= height
;
417 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
419 if (width
== -1) m_width
= 80;
422 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
424 if (height
== -1) m_height
= 26;
427 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
428 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
429 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
430 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
432 if ((m_x
!= -1) || (m_y
!= -1))
434 if ((m_x
!= old_x
) || (m_y
!= old_y
))
436 /* we set the position here and when showing the dialog
437 for the first time in idle time */
438 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
442 if ((m_width
!= old_width
) || (m_height
!= old_height
))
444 /* actual resizing is deferred to GtkOnSize in idle time and
445 when showing the dialog */
452 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
454 // due to a bug in gtk, x,y are always 0
458 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
459 if (!m_wxwindow
) return;
464 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
465 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
466 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
467 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
469 /* we actually set the size of a frame here and no-where else */
470 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
474 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
475 event
.SetEventObject( this );
476 GetEventHandler()->ProcessEvent( event
);
479 void wxDialog::Centre( int direction
)
481 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
486 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
487 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
492 void wxDialog::OnInternalIdle()
494 if (!m_sizeSet
&& GTK_WIDGET_REALIZED(m_wxwindow
))
495 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
498 bool wxDialog::Show( bool show
)
500 if (!show
&& IsModal())
502 EndModal( wxID_CANCEL
);
505 if (show
&& !m_sizeSet
)
507 /* by calling GtkOnSize here, we don't have to call
508 either after showing the frame, which would entail
509 much ugly flicker nor from within the size_allocate
510 handler, because GTK 1.1.X forbids that. */
512 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
515 if (show
!= m_isShown
)
519 gtk_widget_show( m_widget
);
522 gtk_widget_hide( m_widget
);
527 if (show
) InitDialog();
532 bool wxDialog::IsModal() const
534 return m_modalShowing
;
537 void wxDialog::SetModal( bool WXUNUSED(flag
) )
541 m_windowStyle |= wxDIALOG_MODAL;
543 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
545 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
548 int wxDialog::ShowModal()
552 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
553 return GetReturnCode();
558 m_modalShowing
= TRUE
;
560 gtk_grab_add( m_widget
);
562 gtk_grab_remove( m_widget
);
564 return GetReturnCode();
567 void wxDialog::EndModal( int retCode
)
569 SetReturnCode( retCode
);
573 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
577 m_modalShowing
= FALSE
;
584 void wxDialog::InitDialog()
586 wxWindow::InitDialog();
589 void wxDialog::SetIcon( const wxIcon
&icon
)
592 if (!icon
.Ok()) return;
594 wxMask
*mask
= icon
.GetMask();
595 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
596 if (mask
) bm
= mask
->GetBitmap();
598 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);