]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dialog.cpp
little doc updates
[wxWidgets.git] / src / gtk / 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 /* all this is for Motif Window Manager "hints" and is supposed to be
149 recognized by other WM as well. not tested. */
150 long decor = (long) GDK_DECOR_ALL;
151 long func = (long) GDK_FUNC_ALL;
152
153 if ((m_windowStyle & wxCAPTION) == 0)
154 decor |= GDK_DECOR_TITLE;
155 /* if ((m_windowStyle & wxMINIMIZE) == 0)
156 func |= GDK_FUNC_MINIMIZE;
157 if ((m_windowStyle & wxMAXIMIZE) == 0)
158 func |= GDK_FUNC_MAXIMIZE; */
159 if ((m_windowStyle & wxSYSTEM_MENU) == 0)
160 decor |= GDK_DECOR_MENU;
161 if ((m_windowStyle & wxMINIMIZE_BOX) == 0)
162 decor |= GDK_DECOR_MINIMIZE;
163 if ((m_windowStyle & wxMAXIMIZE_BOX) == 0)
164 decor |= GDK_DECOR_MAXIMIZE;
165 if ((m_windowStyle & wxRESIZE_BORDER) == 0)
166 func |= GDK_FUNC_RESIZE;
167
168 gdk_window_set_decorations(m_widget->window, (GdkWMDecoration)decor);
169 gdk_window_set_functions(m_widget->window, (GdkWMFunction)func);
170
171 /* GTK's shrinking/growing policy */
172 if ((m_windowStyle & wxRESIZE_BORDER) == 0)
173 gtk_window_set_policy(GTK_WINDOW(m_widget), 0, 0, 1);
174 else
175 gtk_window_set_policy(GTK_WINDOW(m_widget), 1, 1, 1);
176
177 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
178 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
179
180 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
181 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
182
183 return TRUE;
184 }
185
186 wxDialog::~wxDialog()
187 {
188 wxTopLevelWindows.DeleteObject( this );
189
190 if (wxTheApp->GetTopWindow() == this)
191 {
192 wxTheApp->SetTopWindow( (wxWindow*) NULL );
193 }
194
195 if (wxTopLevelWindows.Number() == 0)
196 {
197 wxTheApp->ExitMainLoop();
198 }
199 }
200
201 void wxDialog::SetTitle( const wxString& title )
202 {
203 m_title = title;
204 if (m_title.IsNull()) m_title = _T("");
205 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
206 }
207
208 wxString wxDialog::GetTitle() const
209 {
210 return (wxString&)m_title;
211 }
212
213 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
214 {
215 if (Validate()) TransferDataFromWindow();
216 }
217
218 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
219 {
220 if (IsModal())
221 {
222 EndModal(wxID_CANCEL);
223 }
224 else
225 {
226 SetReturnCode(wxID_CANCEL);
227 this->Show(FALSE);
228 }
229 }
230
231 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
232 {
233 if ( Validate() && TransferDataFromWindow())
234 {
235 if (IsModal())
236 {
237 EndModal(wxID_OK);
238 }
239 else
240 {
241 SetReturnCode(wxID_OK);
242 this->Show(FALSE);
243 }
244 }
245 }
246
247 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
248 {
249 // yes
250 }
251
252 void wxDialog::OnCloseWindow(wxCloseEvent& event)
253 {
254 // We'll send a Cancel message by default,
255 // which may close the dialog.
256 // Check for looping if the Cancel event handler calls Close().
257
258 // Note that if a cancel button and handler aren't present in the dialog,
259 // nothing will happen when you close the dialog via the window manager, or
260 // via Close().
261 // We wouldn't want to destroy the dialog by default, since the dialog may have been
262 // created on the stack.
263 // However, this does mean that calling dialog->Close() won't delete the dialog
264 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
265 // sure to destroy the dialog.
266 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
267
268 static wxList s_closing;
269
270 if (s_closing.Member(this))
271 return; // no loops
272
273 s_closing.Append(this);
274
275 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
276 cancelEvent.SetEventObject( this );
277 GetEventHandler()->ProcessEvent(cancelEvent);
278 s_closing.DeleteObject(this);
279 }
280
281 bool wxDialog::Destroy()
282 {
283 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
284
285 return TRUE;
286 }
287
288 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
289 {
290 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
291
292 if (GetAutoLayout())
293 {
294 Layout();
295 }
296 else
297 {
298 /* no child: go out ! */
299 if (!GetChildren().First()) return;
300
301 /* do we have exactly one child? */
302 wxWindow *child = (wxWindow *) NULL;
303 for(wxNode *node = GetChildren().First(); node; node = node->Next())
304 {
305 wxWindow *win = (wxWindow *)node->Data();
306 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
307 {
308 /* it's the second one: do nothing */
309 if (child) return;
310 child = win;
311 }
312 }
313
314 /* yes: set it's size to fill all the frame */
315 int client_x, client_y;
316 GetClientSize( &client_x, &client_y );
317 child->SetSize( 1, 1, client_x-2, client_y);
318 }
319 }
320
321 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
322 {
323 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
324 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
325
326 if (m_resizing) return; /* I don't like recursions */
327 m_resizing = TRUE;
328
329 int old_x = m_x;
330 int old_y = m_y;
331 int old_width = m_width;
332 int old_height = m_height;
333
334 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
335 {
336 if (x != -1) m_x = x;
337 if (y != -1) m_y = y;
338 if (width != -1) m_width = width;
339 if (height != -1) m_height = height;
340 }
341 else
342 {
343 m_x = x;
344 m_y = y;
345 m_width = width;
346 m_height = height;
347 }
348
349 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
350 {
351 if (width == -1) m_width = 80;
352 }
353
354 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
355 {
356 if (height == -1) m_height = 26;
357 }
358
359 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
360 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
361 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
362 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
363
364 if ((m_x != -1) || (m_y != -1))
365 {
366 if ((m_x != old_x) || (m_y != old_y))
367 {
368 /* m_sizeSet = FALSE; */
369 gtk_widget_set_uposition( m_widget, m_x, m_y );
370 }
371 }
372
373 if ((m_width != old_width) || (m_height != old_height))
374 {
375 m_sizeSet = FALSE;
376 }
377
378 m_resizing = FALSE;
379 }
380
381 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
382 {
383 // due to a bug in gtk, x,y are always 0
384 // m_x = x;
385 // m_y = y;
386
387 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
388 if (!m_wxwindow) return;
389
390 m_width = width;
391 m_height = height;
392
393 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
394 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
395 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
396 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
397
398 /* we actually set the size of a frame here and no-where else */
399 gtk_widget_set_usize( m_widget, m_width, m_height );
400
401 m_sizeSet = TRUE;
402
403 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
404 event.SetEventObject( this );
405 GetEventHandler()->ProcessEvent( event );
406 }
407
408 void wxDialog::Centre( int direction )
409 {
410 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
411
412 int x = 0;
413 int y = 0;
414
415 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
416 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
417
418 Move( x, y );
419 }
420
421 void wxDialog::OnInternalIdle()
422 {
423 if (!m_sizeSet)
424 GtkOnSize( m_x, m_y, m_width, m_height );
425 }
426
427 bool wxDialog::Show( bool show )
428 {
429 if (!show && IsModal())
430 {
431 EndModal( wxID_CANCEL );
432 }
433
434 if (show && !m_sizeSet)
435 {
436 /* by calling GtkOnSize here, we don't have to call
437 either after showing the frame, which would entail
438 much ugly flicker nor from within the size_allocate
439 handler, because GTK 1.1.X forbids that. */
440
441 GtkOnSize( m_x, m_y, m_width, m_height );
442 }
443
444 wxWindow::Show( show );
445
446 if (show) InitDialog();
447
448 return TRUE;
449 }
450
451 bool wxDialog::IsModal() const
452 {
453 return m_modalShowing;
454 }
455
456 void wxDialog::SetModal( bool WXUNUSED(flag) )
457 {
458 /*
459 if (flag)
460 m_windowStyle |= wxDIALOG_MODAL;
461 else
462 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
463 */
464 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
465 }
466
467 int wxDialog::ShowModal()
468 {
469 if (IsModal())
470 {
471 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
472 return GetReturnCode();
473 }
474
475 Show( TRUE );
476
477 m_modalShowing = TRUE;
478
479 gtk_grab_add( m_widget );
480 gtk_main();
481 gtk_grab_remove( m_widget );
482
483 return GetReturnCode();
484 }
485
486 void wxDialog::EndModal( int retCode )
487 {
488 SetReturnCode( retCode );
489
490 if (!IsModal())
491 {
492 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
493 return;
494 }
495
496 m_modalShowing = FALSE;
497
498 gtk_main_quit();
499
500 Show( FALSE );
501 }
502
503 void wxDialog::InitDialog()
504 {
505 wxWindow::InitDialog();
506 }
507
508 void wxDialog::SetIcon( const wxIcon &icon )
509 {
510 m_icon = icon;
511 if (!icon.Ok()) return;
512
513 wxMask *mask = icon.GetMask();
514 GdkBitmap *bm = (GdkBitmap *) NULL;
515 if (mask) bm = mask->GetBitmap();
516
517 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
518 }