Header changes (gtk.h etc no longer included in defs.h
[wxWidgets.git] / src / gtk1 / dialog.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dialog.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "dialog.h"
12 #endif
13
14 #include "wx/dialog.h"
15 #include "wx/frame.h"
16 #include "wx/app.h"
17
18 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20 #include "wx/gtk/win_gtk.h"
21
22 //-----------------------------------------------------------------------------
23
24 extern wxList wxPendingDelete;
25
26 //-----------------------------------------------------------------------------
27 // "delete_event"
28 //-----------------------------------------------------------------------------
29
30 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
31 {
32 /*
33 printf( "OnDelete from " );
34 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
35 printf( win->GetClassInfo()->GetClassName() );
36 printf( ".\n" );
37 */
38
39 win->Close();
40
41 return TRUE;
42 }
43
44 //-----------------------------------------------------------------------------
45 // "size_allocate"
46 //-----------------------------------------------------------------------------
47
48 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
49 {
50 if (!win->HasVMT()) return;
51
52 /*
53 printf( "OnDialogResize from " );
54 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
55 printf( win->GetClassInfo()->GetClassName() );
56 printf( ".\n" );
57 */
58
59 win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height );
60 }
61
62 //-----------------------------------------------------------------------------
63 // wxDialog
64 //-----------------------------------------------------------------------------
65
66 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
67 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
68 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
69 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
70 EVT_SIZE (wxDialog::OnSize)
71 EVT_CLOSE (wxDialog::OnCloseWindow)
72 END_EVENT_TABLE()
73
74 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
75
76 wxDialog::wxDialog()
77 {
78 m_title = "";
79 m_modalShowing = FALSE;
80 }
81
82 wxDialog::wxDialog( wxWindow *parent,
83 wxWindowID id, const wxString &title,
84 const wxPoint &pos, const wxSize &size,
85 long style, const wxString &name )
86 {
87 m_modalShowing = FALSE;
88 Create( parent, id, title, pos, size, style, name );
89 }
90
91 bool wxDialog::Create( wxWindow *parent,
92 wxWindowID id, const wxString &title,
93 const wxPoint &pos, const wxSize &size,
94 long style, const wxString &name )
95 {
96 wxTopLevelWindows.Append( this );
97
98 m_needParent = FALSE;
99
100 PreCreation( parent, id, pos, size, style, name );
101
102 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
103 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
104
105 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
106
107 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
108 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
109
110 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
111 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
112
113 m_wxwindow = gtk_myfixed_new();
114 gtk_widget_show( m_wxwindow );
115 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
116
117 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
118
119 SetTitle( title );
120
121 if ((m_x != -1) || (m_y != -1))
122 gtk_widget_set_uposition( m_widget, m_x, m_y );
123
124 gtk_widget_set_usize( m_widget, m_width, m_height );
125
126 if (m_parent) m_parent->AddChild( this );
127
128
129 PostCreation();
130
131 return TRUE;
132 }
133
134 wxDialog::~wxDialog()
135 {
136 wxTopLevelWindows.DeleteObject( this );
137 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
138 }
139
140 void wxDialog::SetTitle( const wxString& title )
141 {
142 m_title = title;
143 if (m_title.IsNull()) m_title = "";
144 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
145 }
146
147 wxString wxDialog::GetTitle() const
148 {
149 return (wxString&)m_title;
150 }
151
152 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
153 {
154 if (Validate()) TransferDataFromWindow();
155 }
156
157 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
158 {
159 if (IsModal())
160 {
161 EndModal(wxID_CANCEL);
162 }
163 else
164 {
165 SetReturnCode(wxID_CANCEL);
166 this->Show(FALSE);
167 }
168 }
169
170 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
171 {
172 if ( Validate() && TransferDataFromWindow())
173 {
174 if (IsModal())
175 {
176 EndModal(wxID_OK);
177 }
178 else
179 {
180 SetReturnCode(wxID_OK);
181 this->Show(FALSE);
182 }
183 }
184 }
185
186 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
187 {
188 // yes
189 }
190
191 bool wxDialog::OnClose()
192 {
193 static wxList closing;
194
195 if (closing.Member(this)) return FALSE; // no loops
196
197 closing.Append(this);
198
199 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
200 cancelEvent.SetEventObject( this );
201 GetEventHandler()->ProcessEvent(cancelEvent);
202 closing.DeleteObject(this);
203
204 return FALSE;
205 }
206
207 bool wxDialog::Destroy()
208 {
209 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
210
211 return TRUE;
212 }
213
214 void wxDialog::OnCloseWindow( wxCloseEvent& event )
215 {
216 if (GetEventHandler()->OnClose() || event.GetForce())
217 {
218 this->Destroy();
219 }
220 }
221
222 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
223 {
224 // due to a bug in gtk, x,y are always 0
225 // m_x = x;
226 // m_y = y;
227
228 if ((m_height == height) && (m_width == width) &&
229 (m_sizeSet)) return;
230 if (!m_wxwindow) return;
231
232 m_width = width;
233 m_height = height;
234
235 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
236 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
237 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
238 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
239
240 gtk_widget_set_usize( m_widget, m_width, m_height );
241
242 m_sizeSet = TRUE;
243
244 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
245 event.SetEventObject( this );
246 GetEventHandler()->ProcessEvent( event );
247 }
248
249 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
250 {
251 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
252
253 if (GetAutoLayout())
254 {
255 Layout();
256 }
257 else
258 {
259 // no child: go out !
260 if (!GetChildren().First()) return;
261
262 // do we have exactly one child?
263 wxWindow *child = (wxWindow *) NULL;
264 for(wxNode *node = GetChildren().First(); node; node = node->Next())
265 {
266 wxWindow *win = (wxWindow *)node->Data();
267 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
268 {
269 // it's the second one: do nothing
270 if (child) return;
271 child = win;
272 }
273 }
274
275 // yes: set it's size to fill all the frame
276 int client_x, client_y;
277 GetClientSize( &client_x, &client_y );
278 child->SetSize( 1, 1, client_x-2, client_y);
279 }
280 }
281
282 void wxDialog::SetSize( int x, int y, int width, int height, int sizeFlags )
283 {
284 wxASSERT_MSG( (m_widget != NULL), "invalid window" );
285
286 // Don't do anything for children of wxMDIChildFrame
287 if (!m_wxwindow) return;
288
289 if (m_resizing) return; // I don't like recursions
290 m_resizing = TRUE;
291
292 int old_x = m_x;
293 int old_y = m_y;
294 int old_width = m_width;
295 int old_height = m_height;
296
297 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
298 {
299 if (x != -1) m_x = x;
300 if (y != -1) m_y = y;
301 if (width != -1) m_width = width;
302 if (height != -1) m_height = height;
303 }
304 else
305 {
306 m_x = x;
307 m_y = y;
308 m_width = width;
309 m_height = height;
310 }
311
312 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
313 {
314 if (width == -1) m_width = 80;
315 }
316
317 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
318 {
319 if (height == -1) m_height = 26;
320 }
321
322 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
323 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
324 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
325 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
326
327 if ((m_x != -1) || (m_y != -1))
328 {
329 if ((m_x != old_x) || (m_y != old_y))
330 gtk_widget_set_uposition( m_widget, m_x, m_y );
331 }
332
333 if ((m_width != old_width) || (m_height != old_height))
334 {
335 gtk_widget_set_usize( m_widget, m_width, m_height );
336 }
337
338 m_sizeSet = TRUE;
339
340 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
341 event.SetEventObject( this );
342 GetEventHandler()->ProcessEvent( event );
343
344 m_resizing = FALSE;
345 }
346
347 void wxDialog::SetSize( int width, int height )
348 {
349 SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING );
350 }
351
352 void wxDialog::Centre( int direction )
353 {
354 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
355
356 int x = 0;
357 int y = 0;
358
359 if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
360 if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
361
362 Move( x, y );
363 }
364
365 bool wxDialog::Show( bool show )
366 {
367 if (!show && IsModal())
368 {
369 EndModal( wxID_CANCEL );
370 }
371
372 wxWindow::Show( show );
373
374 if (show) InitDialog();
375
376 return TRUE;
377 }
378
379 bool wxDialog::IsModal() const
380 {
381 return m_modalShowing;
382 }
383
384 void wxDialog::SetModal( bool WXUNUSED(flag) )
385 {
386 /*
387 if (flag)
388 m_windowStyle |= wxDIALOG_MODAL;
389 else
390 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
391 */
392 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
393 }
394
395 int wxDialog::ShowModal()
396 {
397 if (IsModal())
398 {
399 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
400 return GetReturnCode();
401 }
402
403 Show( TRUE );
404
405 m_modalShowing = TRUE;
406
407 gtk_grab_add( m_widget );
408 gtk_main();
409 gtk_grab_remove( m_widget );
410
411 return GetReturnCode();
412 }
413
414 void wxDialog::EndModal( int retCode )
415 {
416 SetReturnCode( retCode );
417
418 if (!IsModal())
419 {
420 wxFAIL_MSG( "wxDialog:EndModal called twice" );
421 return;
422 }
423
424 m_modalShowing = FALSE;
425
426 gtk_main_quit();
427
428 Show( FALSE );
429 }
430
431 void wxDialog::InitDialog()
432 {
433 wxWindow::InitDialog();
434 }
435
436 void wxDialog::SetIcon( const wxIcon &icon )
437 {
438 m_icon = icon;
439 if (!icon.Ok()) return;
440
441 wxMask *mask = icon.GetMask();
442 GdkBitmap *bm = (GdkBitmap *) NULL;
443 if (mask) bm = mask->GetBitmap();
444
445 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
446 }