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 extern wxList wxPendingDelete
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 bool gtk_dialog_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxDialog
*win
)
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_dialog_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxDialog
*win
)
50 if (!win
->HasVMT()) return;
53 printf( "OnDialogResize from " );
54 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
55 printf( win->GetClassInfo()->GetClassName() );
59 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
61 win
->m_sizeSet
= FALSE
;
62 win
->m_width
= alloc
->width
;
63 win
->m_height
= alloc
->height
;
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 static gint
gtk_dialog_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxDialog
*win
)
73 if (!win
->HasVMT()) return FALSE
;
78 wxMoveEvent
mevent( wxPoint(win
->m_x
,win
->m_y
), win
->GetId() );
79 mevent
.SetEventObject( win
);
80 win
->GetEventHandler()->ProcessEvent( mevent
);
85 //-----------------------------------------------------------------------------
86 // "realize" from m_widget
87 //-----------------------------------------------------------------------------
89 /* we cannot MWM hints before the widget has been realized,
90 so we do this directly after realization */
93 gtk_dialog_realized_callback( GtkWidget
*widget
, wxWindow
*win
)
95 /* all this is for Motif Window Manager "hints" and is supposed to be
96 recognized by other WM as well. not tested. */
97 long decor
= (long) GDK_DECOR_ALL
;
98 long func
= (long) GDK_FUNC_ALL
;
100 if ((win
->m_windowStyle
& wxCAPTION
) == 0)
101 decor
|= GDK_DECOR_TITLE
;
102 /* if ((win->m_windowStyle & wxMINIMIZE) == 0)
103 func |= GDK_FUNC_MINIMIZE;
104 if ((win->m_windowStyle & wxMAXIMIZE) == 0)
105 func |= GDK_FUNC_MAXIMIZE; */
106 if ((win
->m_windowStyle
& wxSYSTEM_MENU
) == 0)
107 decor
|= GDK_DECOR_MENU
;
108 if ((win
->m_windowStyle
& wxMINIMIZE_BOX
) == 0)
109 decor
|= GDK_DECOR_MINIMIZE
;
110 if ((win
->m_windowStyle
& wxMAXIMIZE_BOX
) == 0)
111 decor
|= GDK_DECOR_MAXIMIZE
;
112 if ((win
->m_windowStyle
& wxRESIZE_BORDER
) == 0)
113 func
|= GDK_FUNC_RESIZE
;
115 gdk_window_set_decorations( win
->m_widget
->window
, (GdkWMDecoration
)decor
);
116 gdk_window_set_functions( win
->m_widget
->window
, (GdkWMFunction
)func
);
118 /* GTK's shrinking/growing policy */
119 if ((win
->m_windowStyle
& wxRESIZE_BORDER
) == 0)
120 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 0, 0, 1);
122 gtk_window_set_policy(GTK_WINDOW(win
->m_widget
), 1, 1, 1);
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 BEGIN_EVENT_TABLE(wxDialog
,wxPanel
)
132 EVT_BUTTON (wxID_OK
, wxDialog::OnOK
)
133 EVT_BUTTON (wxID_CANCEL
, wxDialog::OnCancel
)
134 EVT_BUTTON (wxID_APPLY
, wxDialog::OnApply
)
135 EVT_SIZE (wxDialog::OnSize
)
136 EVT_CLOSE (wxDialog::OnCloseWindow
)
139 IMPLEMENT_DYNAMIC_CLASS(wxDialog
,wxPanel
)
141 void wxDialog::Init()
144 m_modalShowing
= FALSE
;
147 wxDialog::wxDialog( wxWindow
*parent
,
148 wxWindowID id
, const wxString
&title
,
149 const wxPoint
&pos
, const wxSize
&size
,
150 long style
, const wxString
&name
)
154 Create( parent
, id
, title
, pos
, size
, style
, name
);
157 bool wxDialog::Create( wxWindow
*parent
,
158 wxWindowID id
, const wxString
&title
,
159 const wxPoint
&pos
, const wxSize
&size
,
160 long style
, const wxString
&name
)
162 wxTopLevelWindows
.Append( this );
164 m_needParent
= FALSE
;
166 PreCreation( parent
, id
, pos
, size
, style
, name
);
168 m_widget
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
169 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
171 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
173 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
174 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback
), (gpointer
)this );
176 m_wxwindow
= gtk_myfixed_new();
177 gtk_widget_show( m_wxwindow
);
178 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
180 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
184 if (m_parent
) m_parent
->AddChild( this );
188 /* we cannot set MWM hints before the widget has
189 been realized, so we do this directly after realization */
190 gtk_signal_connect( GTK_OBJECT(m_widget
), "realize",
191 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback
), (gpointer
) this );
193 /* the user resized the frame by dragging etc. */
194 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
195 GTK_SIGNAL_FUNC(gtk_dialog_size_callback
), (gpointer
)this );
197 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
198 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback
), (gpointer
)this );
203 wxDialog::~wxDialog()
205 wxTopLevelWindows
.DeleteObject( this );
207 if (wxTheApp
->GetTopWindow() == this)
209 wxTheApp
->SetTopWindow( (wxWindow
*) NULL
);
212 if (wxTopLevelWindows
.Number() == 0)
214 wxTheApp
->ExitMainLoop();
218 void wxDialog::SetTitle( const wxString
& title
)
221 if (m_title
.IsNull()) m_title
= _T("");
222 gtk_window_set_title( GTK_WINDOW(m_widget
), m_title
.mbc_str() );
225 wxString
wxDialog::GetTitle() const
227 return (wxString
&)m_title
;
230 void wxDialog::OnApply( wxCommandEvent
&WXUNUSED(event
) )
232 if (Validate()) TransferDataFromWindow();
235 void wxDialog::OnCancel( wxCommandEvent
&WXUNUSED(event
) )
239 EndModal(wxID_CANCEL
);
243 SetReturnCode(wxID_CANCEL
);
248 void wxDialog::OnOK( wxCommandEvent
&WXUNUSED(event
) )
250 if ( Validate() && TransferDataFromWindow())
258 SetReturnCode(wxID_OK
);
264 void wxDialog::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
269 void wxDialog::OnCloseWindow(wxCloseEvent
& event
)
271 // We'll send a Cancel message by default,
272 // which may close the dialog.
273 // Check for looping if the Cancel event handler calls Close().
275 // Note that if a cancel button and handler aren't present in the dialog,
276 // nothing will happen when you close the dialog via the window manager, or
278 // We wouldn't want to destroy the dialog by default, since the dialog may have been
279 // created on the stack.
280 // However, this does mean that calling dialog->Close() won't delete the dialog
281 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
282 // sure to destroy the dialog.
283 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
285 static wxList s_closing
;
287 if (s_closing
.Member(this))
290 s_closing
.Append(this);
292 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
293 cancelEvent
.SetEventObject( this );
294 GetEventHandler()->ProcessEvent(cancelEvent
);
295 s_closing
.DeleteObject(this);
298 bool wxDialog::Destroy()
300 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
305 void wxDialog::OnSize( wxSizeEvent
&WXUNUSED(event
) )
307 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
315 /* no child: go out ! */
316 if (!GetChildren().First()) return;
318 /* do we have exactly one child? */
319 wxWindow
*child
= (wxWindow
*) NULL
;
320 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
322 wxWindow
*win
= (wxWindow
*)node
->Data();
323 if (!wxIS_KIND_OF(win
,wxFrame
) && !wxIS_KIND_OF(win
,wxDialog
))
325 /* it's the second one: do nothing */
331 /* yes: set it's size to fill all the frame */
332 int client_x
, client_y
;
333 GetClientSize( &client_x
, &client_y
);
334 child
->SetSize( 1, 1, client_x
-2, client_y
);
338 void wxDialog::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
340 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
341 wxASSERT_MSG( (m_wxwindow
!= NULL
), _T("invalid dialog") );
343 if (m_resizing
) return; /* I don't like recursions */
348 int old_width
= m_width
;
349 int old_height
= m_height
;
351 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
353 if (x
!= -1) m_x
= x
;
354 if (y
!= -1) m_y
= y
;
355 if (width
!= -1) m_width
= width
;
356 if (height
!= -1) m_height
= height
;
366 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
368 if (width
== -1) m_width
= 80;
371 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
373 if (height
== -1) m_height
= 26;
376 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
377 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
378 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
379 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
381 if ((m_x
!= -1) || (m_y
!= -1))
383 if ((m_x
!= old_x
) || (m_y
!= old_y
))
385 /* m_sizeSet = FALSE; */
386 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
390 if ((m_width
!= old_width
) || (m_height
!= old_height
))
398 void wxDialog::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
400 // due to a bug in gtk, x,y are always 0
404 if ((m_height
== height
) && (m_width
== width
) && (m_sizeSet
)) return;
405 if (!m_wxwindow
) return;
410 if ((m_minWidth
!= -1) && (m_width
< m_minWidth
)) m_width
= m_minWidth
;
411 if ((m_minHeight
!= -1) && (m_height
< m_minHeight
)) m_height
= m_minHeight
;
412 if ((m_maxWidth
!= -1) && (m_width
> m_maxWidth
)) m_width
= m_maxWidth
;
413 if ((m_maxHeight
!= -1) && (m_height
> m_maxHeight
)) m_height
= m_maxHeight
;
415 /* we actually set the size of a frame here and no-where else */
416 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
420 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
421 event
.SetEventObject( this );
422 GetEventHandler()->ProcessEvent( event
);
425 void wxDialog::Centre( int direction
)
427 wxASSERT_MSG( (m_widget
!= NULL
), _T("invalid dialog") );
432 if ((direction
& wxHORIZONTAL
) == wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
433 if ((direction
& wxVERTICAL
) == wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
438 void wxDialog::OnInternalIdle()
441 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
444 bool wxDialog::Show( bool show
)
446 if (!show
&& IsModal())
448 EndModal( wxID_CANCEL
);
451 if (show
&& !m_sizeSet
)
453 /* by calling GtkOnSize here, we don't have to call
454 either after showing the frame, which would entail
455 much ugly flicker nor from within the size_allocate
456 handler, because GTK 1.1.X forbids that. */
458 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
461 wxWindow::Show( show
);
463 if (show
) InitDialog();
468 bool wxDialog::IsModal() const
470 return m_modalShowing
;
473 void wxDialog::SetModal( bool WXUNUSED(flag
) )
477 m_windowStyle |= wxDIALOG_MODAL;
479 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
481 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
484 int wxDialog::ShowModal()
488 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
489 return GetReturnCode();
494 m_modalShowing
= TRUE
;
496 gtk_grab_add( m_widget
);
498 gtk_grab_remove( m_widget
);
500 return GetReturnCode();
503 void wxDialog::EndModal( int retCode
)
505 SetReturnCode( retCode
);
509 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
513 m_modalShowing
= FALSE
;
520 void wxDialog::InitDialog()
522 wxWindow::InitDialog();
525 void wxDialog::SetIcon( const wxIcon
&icon
)
528 if (!icon
.Ok()) return;
530 wxMask
*mask
= icon
.GetMask();
531 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
532 if (mask
) bm
= mask
->GetBitmap();
534 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);