]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/dialog.cpp
wxPrivateDataObject works under MSW as well (hopefully it still does under
[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 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern wxList wxPendingDelete;
34
35 //-----------------------------------------------------------------------------
36 // "delete_event"
37 //-----------------------------------------------------------------------------
38
39 bool gtk_dialog_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxDialog *win )
40 {
41 if (g_isIdle)
42 wxapp_install_idle_handler();
43
44 win->Close();
45
46 return TRUE;
47 }
48
49 //-----------------------------------------------------------------------------
50 // "size_allocate"
51 //-----------------------------------------------------------------------------
52
53 static void gtk_dialog_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxDialog *win )
54 {
55 if (g_isIdle)
56 wxapp_install_idle_handler();
57
58 if (!win->m_hasVMT) return;
59
60 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
61 {
62 win->m_width = alloc->width;
63 win->m_height = alloc->height;
64 win->UpdateSize();
65 }
66 }
67
68 //-----------------------------------------------------------------------------
69 // "configure_event"
70 //-----------------------------------------------------------------------------
71
72 static gint gtk_dialog_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxDialog *win )
73 {
74 if (g_isIdle)
75 wxapp_install_idle_handler();
76
77 if (!win->m_hasVMT) return FALSE;
78
79 win->m_x = event->x;
80 win->m_y = event->y;
81
82 wxMoveEvent mevent( wxPoint(win->m_x,win->m_y), win->GetId() );
83 mevent.SetEventObject( win );
84 win->GetEventHandler()->ProcessEvent( mevent );
85
86 return FALSE;
87 }
88
89 //-----------------------------------------------------------------------------
90 // "realize" from m_widget
91 //-----------------------------------------------------------------------------
92
93 /* we cannot MWM hints and icons before the widget has been realized,
94 so we do this directly after realization */
95
96 static gint
97 gtk_dialog_realized_callback( GtkWidget *widget, wxDialog *win )
98 {
99 if (g_isIdle)
100 wxapp_install_idle_handler();
101
102 /* I haven''t been able to set the position of
103 the dialog before it is shown, so I set the
104 position in "realize" and "map" */
105 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
106
107 /* reset the icon */
108 if (win->m_icon != wxNullIcon)
109 {
110 wxIcon icon( win->m_icon );
111 win->m_icon = wxNullIcon;
112 win->SetIcon( icon );
113 }
114
115 return FALSE;
116 }
117
118 //-----------------------------------------------------------------------------
119 // "map" from m_widget
120 //-----------------------------------------------------------------------------
121
122 static gint
123 gtk_dialog_map_callback( GtkWidget *widget, wxDialog *win )
124 {
125 if (g_isIdle)
126 wxapp_install_idle_handler();
127
128 /* I haven''t been able to set the position of
129 the dialog before it is shown, so I set the
130 position in "realize" and "map" */
131 gtk_widget_set_uposition( widget, win->m_x, win->m_y );
132
133 /* all this is for Motif Window Manager "hints" and is supposed to be
134 recognized by other WM as well. not tested. */
135 long decor = (long) GDK_DECOR_BORDER;
136 long func = (long) GDK_FUNC_MOVE ;
137
138 if ((win->GetWindowStyle() & wxCAPTION) != 0)
139 decor |= GDK_DECOR_TITLE;
140 if ((win->GetWindowStyle() & wxSYSTEM_MENU) != 0)
141 {
142 decor |= GDK_DECOR_MENU;
143 func |= GDK_FUNC_CLOSE;
144 }
145 if ((win->GetWindowStyle() & wxMINIMIZE_BOX) != 0)
146 {
147 func |= GDK_FUNC_MINIMIZE;
148 decor |= GDK_DECOR_MINIMIZE;
149 }
150 if ((win->GetWindowStyle() & wxMAXIMIZE_BOX) != 0)
151 {
152 decor |= GDK_DECOR_MAXIMIZE;
153 func |= GDK_FUNC_MAXIMIZE;
154 }
155 if ((win->GetWindowStyle() & wxRESIZE_BORDER) != 0)
156 {
157 func |= GDK_FUNC_RESIZE;
158 decor |= GDK_DECOR_RESIZEH;
159 }
160 gdk_window_set_decorations( win->m_widget->window, (GdkWMDecoration)decor);
161 gdk_window_set_functions( win->m_widget->window, (GdkWMFunction)func);
162
163 /* GTK's shrinking/growing policy */
164 if ((win->GetWindowStyle() & wxRESIZE_BORDER) == 0)
165 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 0, 0, 1);
166 else
167 gtk_window_set_policy(GTK_WINDOW(win->m_widget), 1, 1, 1);
168
169 return FALSE;
170 }
171
172 //-----------------------------------------------------------------------------
173 // wxDialog
174 //-----------------------------------------------------------------------------
175
176 BEGIN_EVENT_TABLE(wxDialog,wxPanel)
177 EVT_BUTTON (wxID_OK, wxDialog::OnOK)
178 EVT_BUTTON (wxID_CANCEL, wxDialog::OnCancel)
179 EVT_BUTTON (wxID_APPLY, wxDialog::OnApply)
180 EVT_SIZE (wxDialog::OnSize)
181 EVT_CLOSE (wxDialog::OnCloseWindow)
182 END_EVENT_TABLE()
183
184 IMPLEMENT_DYNAMIC_CLASS(wxDialog,wxPanel)
185
186 void wxDialog::Init()
187 {
188 m_returnCode = 0;
189 m_sizeSet = FALSE;
190 m_modalShowing = FALSE;
191 }
192
193 wxDialog::wxDialog( wxWindow *parent,
194 wxWindowID id, const wxString &title,
195 const wxPoint &pos, const wxSize &size,
196 long style, const wxString &name )
197 {
198 Init();
199
200 Create( parent, id, title, pos, size, style, name );
201 }
202
203 bool wxDialog::Create( wxWindow *parent,
204 wxWindowID id, const wxString &title,
205 const wxPoint &pos, const wxSize &size,
206 long style, const wxString &name )
207 {
208 wxTopLevelWindows.Append( this );
209
210 m_needParent = FALSE;
211
212 PreCreation( parent, id, pos, size, style, name );
213
214 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
215
216 if (!name.IsEmpty())
217 gtk_window_set_wmclass( GTK_WINDOW(m_widget), name.mb_str(), name.mb_str() );
218
219 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
220
221 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
222 GTK_SIGNAL_FUNC(gtk_dialog_delete_callback), (gpointer)this );
223
224 m_wxwindow = gtk_myfixed_new();
225 gtk_widget_show( m_wxwindow );
226 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
227
228 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
229
230 SetTitle( title );
231
232 if (m_parent) m_parent->AddChild( this );
233
234 PostCreation();
235
236 /* we cannot set MWM hints before the widget has
237 been realized, so we do this directly after realization */
238 gtk_signal_connect( GTK_OBJECT(m_widget), "realize",
239 GTK_SIGNAL_FUNC(gtk_dialog_realized_callback), (gpointer) this );
240
241 /* we set the position of the window after the map event. setting it
242 before has no effect (with KWM) */
243 gtk_signal_connect( GTK_OBJECT(m_widget), "map",
244 GTK_SIGNAL_FUNC(gtk_dialog_map_callback), (gpointer) this );
245
246 /* the user resized the frame by dragging etc. */
247 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
248 GTK_SIGNAL_FUNC(gtk_dialog_size_callback), (gpointer)this );
249
250 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
251 GTK_SIGNAL_FUNC(gtk_dialog_configure_callback), (gpointer)this );
252
253 return TRUE;
254 }
255
256 wxDialog::~wxDialog()
257 {
258 m_isBeingDeleted = TRUE;
259
260 wxTopLevelWindows.DeleteObject( this );
261
262 if (wxTheApp->GetTopWindow() == this)
263 {
264 wxTheApp->SetTopWindow( (wxWindow*) NULL );
265 }
266
267 if (wxTopLevelWindows.Number() == 0)
268 {
269 wxTheApp->ExitMainLoop();
270 }
271 }
272
273 void wxDialog::SetTitle( const wxString& title )
274 {
275 m_title = title;
276 if (m_title.IsNull()) m_title = _T("");
277 gtk_window_set_title( GTK_WINDOW(m_widget), m_title.mbc_str() );
278 }
279
280 wxString wxDialog::GetTitle() const
281 {
282 return (wxString&)m_title;
283 }
284
285 void wxDialog::OnApply( wxCommandEvent &WXUNUSED(event) )
286 {
287 if (Validate()) TransferDataFromWindow();
288 }
289
290 void wxDialog::OnCancel( wxCommandEvent &WXUNUSED(event) )
291 {
292 if (IsModal())
293 {
294 EndModal(wxID_CANCEL);
295 }
296 else
297 {
298 SetReturnCode(wxID_CANCEL);
299 Show(FALSE);
300 }
301 }
302
303 void wxDialog::OnOK( wxCommandEvent &WXUNUSED(event) )
304 {
305 if (Validate() && TransferDataFromWindow())
306 {
307 if (IsModal())
308 {
309 EndModal(wxID_OK);
310 }
311 else
312 {
313 SetReturnCode(wxID_OK);
314 this->Show(FALSE);
315 }
316 }
317 }
318
319 void wxDialog::OnPaint( wxPaintEvent& WXUNUSED(event) )
320 {
321 // yes
322 }
323
324 void wxDialog::OnCloseWindow(wxCloseEvent& event)
325 {
326 // We'll send a Cancel message by default,
327 // which may close the dialog.
328 // Check for looping if the Cancel event handler calls Close().
329
330 // Note that if a cancel button and handler aren't present in the dialog,
331 // nothing will happen when you close the dialog via the window manager, or
332 // via Close().
333 // We wouldn't want to destroy the dialog by default, since the dialog may have been
334 // created on the stack.
335 // However, this does mean that calling dialog->Close() won't delete the dialog
336 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
337 // sure to destroy the dialog.
338 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
339
340 static wxList s_closing;
341
342 if (s_closing.Member(this))
343 return; // no loops
344
345 s_closing.Append(this);
346
347 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
348 cancelEvent.SetEventObject( this );
349 GetEventHandler()->ProcessEvent(cancelEvent);
350 s_closing.DeleteObject(this);
351 }
352
353 bool wxDialog::Destroy()
354 {
355 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
356
357 return TRUE;
358 }
359
360 void wxDialog::OnSize( wxSizeEvent &WXUNUSED(event) )
361 {
362 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
363
364 if (GetAutoLayout())
365 {
366 Layout();
367 }
368 else
369 {
370 /* no child: go out ! */
371 if (!GetChildren().First()) return;
372
373 /* do we have exactly one child? */
374 wxWindow *child = (wxWindow *) NULL;
375 for(wxNode *node = GetChildren().First(); node; node = node->Next())
376 {
377 wxWindow *win = (wxWindow *)node->Data();
378 if (!wxIS_KIND_OF(win,wxFrame) && !wxIS_KIND_OF(win,wxDialog))
379 {
380 /* it's the second one: do nothing */
381 if (child) return;
382 child = win;
383 }
384 }
385
386 /* yes: set it's size to fill all the frame */
387 int client_x, client_y;
388 GetClientSize( &client_x, &client_y );
389 child->SetSize( 1, 1, client_x-2, client_y);
390 }
391 }
392
393 void wxDialog::DoSetSize( int x, int y, int width, int height, int sizeFlags )
394 {
395 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
396 wxASSERT_MSG( (m_wxwindow != NULL), _T("invalid dialog") );
397
398 if (m_resizing) return; /* I don't like recursions */
399 m_resizing = TRUE;
400
401 int old_x = m_x;
402 int old_y = m_y;
403 int old_width = m_width;
404 int old_height = m_height;
405
406 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
407 {
408 if (x != -1) m_x = x;
409 if (y != -1) m_y = y;
410 if (width != -1) m_width = width;
411 if (height != -1) m_height = height;
412 }
413 else
414 {
415 m_x = x;
416 m_y = y;
417 m_width = width;
418 m_height = height;
419 }
420
421 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
422 {
423 if (width == -1) m_width = 80;
424 }
425
426 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
427 {
428 if (height == -1) m_height = 26;
429 }
430
431 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
432 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
433 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
434 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
435
436 if ((m_x != -1) || (m_y != -1))
437 {
438 if ((m_x != old_x) || (m_y != old_y))
439 {
440 /* we set the position here and when showing the dialog
441 for the first time in idle time */
442 gtk_widget_set_uposition( m_widget, m_x, m_y );
443 }
444 }
445
446 if ((m_width != old_width) || (m_height != old_height))
447 {
448 /* actual resizing is deferred to GtkOnSize in idle time and
449 when showing the dialog */
450 m_sizeSet = FALSE;
451 }
452
453 m_resizing = FALSE;
454 }
455
456 void wxDialog::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
457 {
458 // due to a bug in gtk, x,y are always 0
459 // m_x = x;
460 // m_y = y;
461
462 if ((m_height == height) && (m_width == width) && (m_sizeSet)) return;
463 if (!m_wxwindow) return;
464
465 m_width = width;
466 m_height = height;
467
468 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
469 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
470 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_maxWidth;
471 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_maxHeight;
472
473 /* we actually set the size of a frame here and no-where else */
474 gtk_widget_set_usize( m_widget, m_width, m_height );
475
476 m_sizeSet = TRUE;
477
478 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
479 event.SetEventObject( this );
480 GetEventHandler()->ProcessEvent( event );
481 }
482
483 void wxDialog::Centre( int direction )
484 {
485 wxASSERT_MSG( (m_widget != NULL), _T("invalid dialog") );
486
487 int x = 0;
488 int y = 0;
489
490 if ((direction & wxHORIZONTAL) == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
491 if ((direction & wxVERTICAL) == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
492
493 Move( x, y );
494 }
495
496 void wxDialog::OnInternalIdle()
497 {
498 if (!m_sizeSet && GTK_WIDGET_REALIZED(m_wxwindow))
499 GtkOnSize( m_x, m_y, m_width, m_height );
500 }
501
502 bool wxDialog::Show( bool show )
503 {
504 if (!show && IsModal())
505 {
506 EndModal( wxID_CANCEL );
507 }
508
509 if (show && !m_sizeSet)
510 {
511 /* by calling GtkOnSize here, we don't have to call
512 either after showing the frame, which would entail
513 much ugly flicker nor from within the size_allocate
514 handler, because GTK 1.1.X forbids that. */
515
516 GtkOnSize( m_x, m_y, m_width, m_height );
517 }
518
519 bool ret = wxWindow::Show( show );
520
521 if (show) InitDialog();
522
523 return ret;
524 }
525
526 bool wxDialog::IsModal() const
527 {
528 return m_modalShowing;
529 }
530
531 void wxDialog::SetModal( bool WXUNUSED(flag) )
532 {
533 /*
534 if (flag)
535 m_windowStyle |= wxDIALOG_MODAL;
536 else
537 if (m_windowStyle & wxDIALOG_MODAL) m_windowStyle -= wxDIALOG_MODAL;
538 */
539 wxFAIL_MSG( _T("wxDialog:SetModal obsolete now") );
540 }
541
542 int wxDialog::ShowModal()
543 {
544 if (IsModal())
545 {
546 wxFAIL_MSG( _T("wxDialog:ShowModal called twice") );
547 return GetReturnCode();
548 }
549
550 Show( TRUE );
551
552 m_modalShowing = TRUE;
553
554 gtk_grab_add( m_widget );
555 gtk_main();
556 gtk_grab_remove( m_widget );
557
558 return GetReturnCode();
559 }
560
561 void wxDialog::EndModal( int retCode )
562 {
563 SetReturnCode( retCode );
564
565 if (!IsModal())
566 {
567 wxFAIL_MSG( _T("wxDialog:EndModal called twice") );
568 return;
569 }
570
571 m_modalShowing = FALSE;
572
573 gtk_main_quit();
574
575 Show( FALSE );
576 }
577
578 void wxDialog::InitDialog()
579 {
580 wxWindow::InitDialog();
581 }
582
583 void wxDialog::SetIcon( const wxIcon &icon )
584 {
585 m_icon = icon;
586 if (!icon.Ok()) return;
587
588 if (!m_widget->window) return;
589
590 wxMask *mask = icon.GetMask();
591 GdkBitmap *bm = (GdkBitmap *) NULL;
592 if (mask) bm = mask->GetBitmap();
593
594 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
595 }