Here it comes:
[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 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
60 {
61 win->m_sizeSet = FALSE;
62 win->m_width = alloc->width;
63 win->m_height = alloc->height;
64 }
65 }
66
67 //-----------------------------------------------------------------------------
68 // "configure_event"
69 //-----------------------------------------------------------------------------
70
71 static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
72 {
73 if (!win->HasVMT()) return FALSE;
74
75 win->m_x = event->x;
76 win->m_y = event->y;
77
78 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
79 mevent.SetEventObject( win );
80 win->GetEventHandler()->ProcessEvent( mevent );
81
82 return FALSE;
83 }
84
85 //-----------------------------------------------------------------------------
86 // wxDialog
87 //-----------------------------------------------------------------------------
88
89 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
90 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
91 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
92 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
93 EVT_SIZE (wxDialog::OnSize)
94 EVT_CLOSE (wxDialog::OnCloseWindow)
95 END_EVENT_TABLE()
96
97 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
98
99 void wxDialog::Init()
100 {
101 m_sizeSet = FALSE;
102 m_modalShowing = FALSE;
103 }
104
105 wxDialog::wxDialog( wxWindow *parent,
106 wxWindowID id, const wxString &title,
107 const wxPoint &pos, const wxSize &size,
108 long style, const wxString &name )
109 {
110 Init();
111
112 Create( parent, id, title, pos, size, style, name );
113 }
114
115 bool wxDialog::Create( wxWindow *parent,
116 wxWindowID id, const wxString &title,
117 const wxPoint &pos, const wxSize &size,
118 long style, const wxString &name )
119 {
120 wxTopLevelWindows.Append( this );
121
122 m_needParent = FALSE;
123
124 PreCreation( parent, id, pos, size, style, name );
125
126 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
127 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
128
129 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
130
131 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
132 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
133
134 m_wxwindow = gtk_myfixed_new();
135 gtk_widget_show( m_wxwindow );
136 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
137
138 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
139
140 SetTitle( title );
141
142 if (m_parent) m_parent->AddChild( this );
143
144 PostCreation();
145
146 gtk_widget_realize( m_widget );
147
148 long decor = (long) GDK_DECOR_ALL;
149 long func = (long) GDK_FUNC_ALL;
150
151 if ((m_windowStyle & wxCAPTION) == 0)
152 decor |= GDK_DECOR_TITLE;
153 if ((m_windowStyle & wxMINIMIZE) == 0)
154 func |= GDK_FUNC_MINIMIZE;
155 if ((m_windowStyle & wxMAXIMIZE) == 0)
156 func |= GDK_FUNC_MAXIMIZE;
157 if ((m_windowStyle & wxSYSTEM_MENU) == 0)
158 decor |= GDK_DECOR_MENU;
159 if ((m_windowStyle & wxMINIMIZE_BOX) == 0)
160 decor |= GDK_DECOR_MINIMIZE;
161 if ((m_windowStyle & wxMAXIMIZE_BOX) == 0)
162 decor |= GDK_DECOR_MAXIMIZE;
163 if ((m_windowStyle & wxRESIZE_BORDER) == 0)
164 func |= GDK_FUNC_RESIZE;
165
166 gdk_window_set_decorations(m_widget->window, (GdkWMDecoration)decor);
167 gdk_window_set_functions(m_widget->window, (GdkWMFunction)func);
168
169 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
170 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
171
172 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
173 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
174
175 return TRUE;
176 }
177
178 wxDialog::~wxDialog()
179 {
180 wxTopLevelWindows.DeleteObject( this );
181
182 if (wxTheApp->GetTopWindow() == this)
183 {
184 wxTheApp->SetTopWindow( (wxWindow*) NULL );
185 }
186
187 if (wxTopLevelWindows.Number() == 0)
188 {
189 wxTheApp->ExitMainLoop();
190 }
191 }
192
193 void wxDialog::SetTitle( const wxString& title )
194 {
195 m_title = title;
196 if (m_title.IsNull()) m_title = "";
197 gtk_window_set_title( GTK_WINDOW(m_widget), m_title );
198 }
199
200 wxString wxDialog::GetTitle() const
201 {
202 return (wxString&)m_title;
203 }
204
205 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
206 {
207 if (Validate()) TransferDataFromWindow();
208 }
209
210 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
211 {
212 if (IsModal())
213 {
214 EndModal(wxID_CANCEL);
215 }
216 else
217 {
218 SetReturnCode(wxID_CANCEL);
219 this->Show(FALSE);
220 }
221 }
222
223 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
224 {
225 if ( Validate() && TransferDataFromWindow())
226 {
227 if (IsModal())
228 {
229 EndModal(wxID_OK);
230 }
231 else
232 {
233 SetReturnCode(wxID_OK);
234 this->Show(FALSE);
235 }
236 }
237 }
238
239 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
240 {
241 // yes
242 }
243
244 void wxDialog::OnCloseWindow(wxCloseEvent& event)
245 {
246 // We'll send a Cancel message by default,
247 // which may close the dialog.
248 // Check for looping if the Cancel event handler calls Close().
249
250 // Note that if a cancel button and handler aren't present in the dialog,
251 // nothing will happen when you close the dialog via the window manager, or
252 // via Close().
253 // We wouldn't want to destroy the dialog by default, since the dialog may have been
254 // created on the stack.
255 // However, this does mean that calling dialog->Close() won't delete the dialog
256 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
257 // sure to destroy the dialog.
258 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
259
260 static wxList closing;
261
262 if (closing.Member(this))
263 return; // no loops
264
265 closing.Append(this);
266
267 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
268 cancelEvent.SetEventObject( this );
269 GetEventHandler()->ProcessEvent(cancelEvent);
270 closing.DeleteObject(this);
271 }
272
273 bool wxDialog::Destroy()
274 {
275 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
276
277 return TRUE;
278 }
279
280 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
281 {
282 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
283
284 if (GetAutoLayout())
285 {
286 Layout();
287 }
288 else
289 {
290 /* no child: go out ! */
291 if (!GetChildren().First()) return;
292
293 /* do we have exactly one child? */
294 wxWindow *child = (wxWindow *) NULL;
295 for(wxNode *node = GetChildren().First(); node; node = node->Next())
296 {
297 wxWindow *win = (wxWindow *)node->Data();
298 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
299 {
300 /* it's the second one: do nothing */
301 if (child) return;
302 child = win;
303 }
304 }
305
306 /* yes: set it's size to fill all the frame */
307 int client_x, client_y;
308 GetClientSize( &client_x, &client_y );
309 child->SetSize( 1, 1, client_x-2, client_y);
310 }
311 }
312
313 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
314 {
315 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
316 wxASSERT_MSG( (m_wxwindow != NULL), "invalid dialog" );
317
318 if (m_resizing) return; /* I don't like recursions */
319 m_resizing = TRUE;
320
321 int old_x = m_x;
322 int old_y = m_y;
323 int old_width = m_width;
324 int old_height = m_height;
325
326 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
327 {
328 if (x != -1) m_x = x;
329 if (y != -1) m_y = y;
330 if (width != -1) m_width = width;
331 if (height != -1) m_height = height;
332 }
333 else
334 {
335 m_x = x;
336 m_y = y;
337 m_width = width;
338 m_height = height;
339 }
340
341 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
342 {
343 if (width == -1) m_width = 80;
344 }
345
346 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
347 {
348 if (height == -1) m_height = 26;
349 }
350
351 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
352 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
353 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
354 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
355
356 if ((m_x != -1) || (m_y != -1))
357 {
358 if ((m_x != old_x) || (m_y != old_y))
359 {
360 /* m_sizeSet = FALSE; */
361 gtk_widget_set_uposition( m_widget, m_x, m_y );
362 }
363 }
364
365 if ((m_width != old_width) || (m_height != old_height))
366 {
367 m_sizeSet = FALSE;
368 }
369
370 m_resizing = FALSE;
371 }
372
373 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
374 {
375 // due to a bug in gtk, x,y are always 0
376 // m_x = x;
377 // m_y = y;
378
379 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
380 if (!m_wxwindow) return;
381
382 m_width = width;
383 m_height = height;
384
385 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
386 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
387 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
388 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
389
390 /* we actually set the size of a frame here and no-where else */
391 gtk_widget_set_usize( m_widget, m_width, m_height );
392
393 m_sizeSet = TRUE;
394
395 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
396 event.SetEventObject( this );
397 GetEventHandler()->ProcessEvent( event );
398 }
399
400 void wxDialog::Centre( int direction )
401 {
402 wxASSERT_MSG( (m_widget != NULL), "invalid dialog" );
403
404 int x = 0;
405 int y = 0;
406
407 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
408 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
409
410 Move( x, y );
411 }
412
413 void wxDialog::OnInternalIdle()
414 {
415 if (!m_sizeSet)
416 GtkOnSize( m_x, m_y, m_width, m_height );
417 }
418
419 bool wxDialog::Show( bool show )
420 {
421 if (!show && IsModal())
422 {
423 EndModal( wxID_CANCEL );
424 }
425
426 if (show && !m_sizeSet)
427 {
428 /* by calling GtkOnSize here, we don't have to call
429 either after showing the frame, which would entail
430 much ugly flicker nor from within the size_allocate
431 handler, because GTK 1.1.X forbids that. */
432
433 GtkOnSize( m_x, m_y, m_width, m_height );
434 }
435
436 wxWindow::Show( show );
437
438 if (show) InitDialog();
439
440 return TRUE;
441 }
442
443 bool wxDialog::IsModal() const
444 {
445 return m_modalShowing;
446 }
447
448 void wxDialog::SetModal( bool WXUNUSED(flag) )
449 {
450 /*
451 if (flag)
452 m_windowStyle |= wxDIALOG_MODAL;
453 else
454 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
455 */
456 wxFAIL_MSG( "wxDialog:SetModal obsolete now" );
457 }
458
459 int wxDialog::ShowModal()
460 {
461 if (IsModal())
462 {
463 wxFAIL_MSG( "wxDialog:ShowModal called twice" );
464 return GetReturnCode();
465 }
466
467 Show( TRUE );
468
469 m_modalShowing = TRUE;
470
471 gtk_grab_add( m_widget );
472 gtk_main();
473 gtk_grab_remove( m_widget );
474
475 return GetReturnCode();
476 }
477
478 void wxDialog::EndModal( int retCode )
479 {
480 SetReturnCode( retCode );
481
482 if (!IsModal())
483 {
484 wxFAIL_MSG( "wxDialog:EndModal called twice" );
485 return;
486 }
487
488 m_modalShowing = FALSE;
489
490 gtk_main_quit();
491
492 Show( FALSE );
493 }
494
495 void wxDialog::InitDialog()
496 {
497 wxWindow::InitDialog();
498 }
499
500 void wxDialog::SetIcon( const wxIcon &icon )
501 {
502 m_icon = icon;
503 if (!icon.Ok()) return;
504
505 wxMask *mask = icon.GetMask();
506 GdkBitmap *bm = (GdkBitmap *) NULL;
507 if (mask) bm = mask->GetBitmap();
508
509 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
510 }