Include wx/menu.h according to precompiled headers of wx/wx.h (with other minor clean...
[wxWidgets.git] / src / gtk / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/frame.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/frame.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/app.h"
25 #include "wx/dcclient.h"
26 #include "wx/menu.h"
27 #endif // WX_PRECOMP
28
29 #include "wx/dialog.h"
30 #include "wx/control.h"
31 #if wxUSE_TOOLBAR
32 #include "wx/toolbar.h"
33 #endif
34 #if wxUSE_STATUSBAR
35 #include "wx/statusbr.h"
36 #endif
37
38 #include <glib.h>
39 #include "wx/gtk/private.h"
40
41 #include <gdk/gdkkeysyms.h>
42 #include <gdk/gdkx.h>
43
44 #include "wx/gtk/win_gtk.h"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 const int wxSTATUS_HEIGHT = 25;
51 const int wxPLACE_HOLDER = 0;
52
53 // ----------------------------------------------------------------------------
54 // event tables
55 // ----------------------------------------------------------------------------
56
57 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
58
59 // ============================================================================
60 // implementation
61 // ============================================================================
62
63 // ----------------------------------------------------------------------------
64 // GTK callbacks
65 // ----------------------------------------------------------------------------
66
67 #if wxUSE_MENUS_NATIVE
68
69 //-----------------------------------------------------------------------------
70 // "child_attached" of menu bar
71 //-----------------------------------------------------------------------------
72
73 extern "C" {
74 static void gtk_menu_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
75 {
76 if (!win->m_hasVMT) return;
77
78 win->m_menuBarDetached = false;
79 win->GtkUpdateSize();
80 }
81 }
82
83 //-----------------------------------------------------------------------------
84 // "child_detached" of menu bar
85 //-----------------------------------------------------------------------------
86
87 extern "C" {
88 static void gtk_menu_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
89 {
90 if (g_isIdle)
91 wxapp_install_idle_handler();
92
93 if (!win->m_hasVMT) return;
94
95 // Raise the client area area
96 gdk_window_raise( win->m_wxwindow->window );
97
98 win->m_menuBarDetached = true;
99 win->GtkUpdateSize();
100 }
101 }
102
103 #endif // wxUSE_MENUS_NATIVE
104
105 #if wxUSE_TOOLBAR
106 //-----------------------------------------------------------------------------
107 // "child_attached" of tool bar
108 //-----------------------------------------------------------------------------
109
110 extern "C" {
111 static void gtk_toolbar_attached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
112 {
113 if (!win->m_hasVMT) return;
114
115 win->m_toolBarDetached = false;
116 win->GtkUpdateSize();
117 }
118 }
119
120 //-----------------------------------------------------------------------------
121 // "child_detached" of tool bar
122 //-----------------------------------------------------------------------------
123
124 extern "C" {
125 static void gtk_toolbar_detached_callback( GtkWidget *WXUNUSED(widget), GtkWidget *WXUNUSED(child), wxFrame *win )
126 {
127 if (g_isIdle)
128 wxapp_install_idle_handler();
129
130 if (!win->m_hasVMT) return;
131
132 // Raise the client area area
133 gdk_window_raise( win->m_wxwindow->window );
134
135 win->m_toolBarDetached = true;
136 win->GtkUpdateSize();
137 }
138 }
139 #endif // wxUSE_TOOLBAR
140
141
142 // ----------------------------------------------------------------------------
143 // wxFrame itself
144 // ----------------------------------------------------------------------------
145
146 //-----------------------------------------------------------------------------
147 // InsertChild for wxFrame
148 //-----------------------------------------------------------------------------
149
150 /* Callback for wxFrame. This very strange beast has to be used because
151 * C++ has no virtual methods in a constructor. We have to emulate a
152 * virtual function here as wxWidgets requires different ways to insert
153 * a child in container classes. */
154
155 static void wxInsertChildInFrame( wxFrame* parent, wxWindow* child )
156 {
157 wxASSERT( GTK_IS_WIDGET(child->m_widget) );
158
159 if (!parent->m_insertInClientArea)
160 {
161 // These are outside the client area
162 wxFrame* frame = (wxFrame*) parent;
163 gtk_pizza_put( GTK_PIZZA(frame->m_mainWidget),
164 GTK_WIDGET(child->m_widget),
165 child->m_x,
166 child->m_y,
167 child->m_width,
168 child->m_height );
169
170 #if wxUSE_TOOLBAR_NATIVE
171 // We connect to these events for recalculating the client area
172 // space when the toolbar is floating
173 if (wxIS_KIND_OF(child,wxToolBar))
174 {
175 wxToolBar *toolBar = (wxToolBar*) child;
176 if (toolBar->GetWindowStyle() & wxTB_DOCKABLE)
177 {
178 g_signal_connect (toolBar->m_widget, "child_attached",
179 G_CALLBACK (gtk_toolbar_attached_callback),
180 parent);
181 g_signal_connect (toolBar->m_widget, "child_detached",
182 G_CALLBACK (gtk_toolbar_detached_callback),
183 parent);
184 }
185 }
186 #endif // wxUSE_TOOLBAR
187 }
188 else
189 {
190 // These are inside the client area
191 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
192 GTK_WIDGET(child->m_widget),
193 child->m_x,
194 child->m_y,
195 child->m_width,
196 child->m_height );
197 }
198
199 // Resize on OnInternalIdle
200 parent->GtkUpdateSize();
201 }
202
203 // ----------------------------------------------------------------------------
204 // wxFrame creation
205 // ----------------------------------------------------------------------------
206
207 void wxFrame::Init()
208 {
209 m_menuBarDetached = false;
210 m_toolBarDetached = false;
211 m_menuBarHeight = 2;
212 }
213
214 bool wxFrame::Create( wxWindow *parent,
215 wxWindowID id,
216 const wxString& title,
217 const wxPoint& pos,
218 const wxSize& sizeOrig,
219 long style,
220 const wxString &name )
221 {
222 bool rt = wxTopLevelWindow::Create(parent, id, title, pos, sizeOrig,
223 style, name);
224 m_insertCallback = (wxInsertChildFunction) wxInsertChildInFrame;
225
226 return rt;
227 }
228
229 wxFrame::~wxFrame()
230 {
231 m_isBeingDeleted = true;
232 DeleteAllBars();
233 }
234
235 // ----------------------------------------------------------------------------
236 // overridden wxWindow methods
237 // ----------------------------------------------------------------------------
238
239 void wxFrame::DoGetClientSize( int *width, int *height ) const
240 {
241 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
242
243 wxTopLevelWindow::DoGetClientSize( width, height );
244
245 if (height)
246 {
247 #if wxUSE_MENUS_NATIVE
248 // menu bar
249 if (m_frameMenuBar)
250 {
251 if (!m_menuBarDetached)
252 (*height) -= m_menuBarHeight;
253 else
254 (*height) -= wxPLACE_HOLDER;
255 }
256 #endif // wxUSE_MENUS_NATIVE
257
258 #if wxUSE_STATUSBAR
259 // status bar
260 if (m_frameStatusBar && m_frameStatusBar->IsShown())
261 (*height) -= wxSTATUS_HEIGHT;
262 #endif // wxUSE_STATUSBAR
263
264 #if wxUSE_TOOLBAR
265 // tool bar
266 if (m_frameToolBar && m_frameToolBar->IsShown())
267 {
268 if (m_toolBarDetached)
269 {
270 *height -= wxPLACE_HOLDER;
271 }
272 else
273 {
274 int x, y;
275 m_frameToolBar->GetSize( &x, &y );
276 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
277 {
278 *width -= x;
279 }
280 else
281 {
282 *height -= y;
283 }
284 }
285 }
286 #endif // wxUSE_TOOLBAR
287 }
288 }
289
290 void wxFrame::DoSetClientSize( int width, int height )
291 {
292 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
293
294 #if wxUSE_MENUS_NATIVE
295 // menu bar
296 if (m_frameMenuBar)
297 {
298 if (!m_menuBarDetached)
299 height += m_menuBarHeight;
300 else
301 height += wxPLACE_HOLDER;
302 }
303 #endif // wxUSE_MENUS_NATIVE
304
305 #if wxUSE_STATUSBAR
306 // status bar
307 if (m_frameStatusBar && m_frameStatusBar->IsShown()) height += wxSTATUS_HEIGHT;
308 #endif
309
310 #if wxUSE_TOOLBAR
311 // tool bar
312 if (m_frameToolBar && m_frameToolBar->IsShown())
313 {
314 if (m_toolBarDetached)
315 {
316 height += wxPLACE_HOLDER;
317 }
318 else
319 {
320 int x, y;
321 m_frameToolBar->GetSize( &x, &y );
322 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
323 {
324 width += x;
325 }
326 else
327 {
328 height += y;
329 }
330 }
331 }
332 #endif
333
334 wxTopLevelWindow::DoSetClientSize( width, height );
335 }
336
337 void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y),
338 int width, int height )
339 {
340 // due to a bug in gtk, x,y are always 0
341 // m_x = x;
342 // m_y = y;
343
344 // avoid recursions
345 if (m_resizing) return;
346 m_resizing = true;
347
348 // this shouldn't happen: wxFrame, wxMDIParentFrame and wxMDIChildFrame have m_wxwindow
349 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
350
351 m_width = width;
352 m_height = height;
353
354 // space occupied by m_frameToolBar and m_frameMenuBar
355 int client_area_x_offset = 0,
356 client_area_y_offset = 0;
357
358 /* wxMDIChildFrame derives from wxFrame but it _is_ a wxWindow as it uses
359 wxWindow::Create to create it's GTK equivalent. m_mainWidget is only
360 set in wxFrame::Create so it is used to check what kind of frame we
361 have here. if m_mainWidget is NULL it is a wxMDIChildFrame and so we
362 skip the part which handles m_frameMenuBar, m_frameToolBar and (most
363 importantly) m_mainWidget */
364
365 int minWidth = GetMinWidth(),
366 minHeight = GetMinHeight(),
367 maxWidth = GetMaxWidth(),
368 maxHeight = GetMaxHeight();
369
370 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
371 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
372 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
373 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
374
375 if (m_mainWidget)
376 {
377 // set size hints
378 gint flag = 0; // GDK_HINT_POS;
379 if ((minWidth != -1) || (minHeight != -1)) flag |= GDK_HINT_MIN_SIZE;
380 if ((maxWidth != -1) || (maxHeight != -1)) flag |= GDK_HINT_MAX_SIZE;
381 GdkGeometry geom;
382 geom.min_width = minWidth;
383 geom.min_height = minHeight;
384 geom.max_width = maxWidth;
385 geom.max_height = maxHeight;
386 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget),
387 (GtkWidget*) NULL,
388 &geom,
389 (GdkWindowHints) flag );
390
391 // I revert back to wxGTK's original behaviour. m_mainWidget holds
392 // the menubar, the toolbar and the client area, which is represented
393 // by m_wxwindow.
394 // This hurts in the eye, but I don't want to call SetSize()
395 // because I don't want to call any non-native functions here.
396
397 #if wxUSE_MENUS_NATIVE
398 if (m_frameMenuBar)
399 {
400 int xx = m_miniEdge;
401 int yy = m_miniEdge + m_miniTitle;
402 int ww = m_width - 2*m_miniEdge;
403 int hh = m_menuBarHeight;
404 if (m_menuBarDetached) hh = wxPLACE_HOLDER;
405 m_frameMenuBar->m_x = xx;
406 m_frameMenuBar->m_y = yy;
407 m_frameMenuBar->m_width = ww;
408 m_frameMenuBar->m_height = hh;
409 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
410 m_frameMenuBar->m_widget,
411 xx, yy, ww, hh );
412 client_area_y_offset += hh;
413 }
414 #endif // wxUSE_MENUS_NATIVE
415
416 #if wxUSE_TOOLBAR
417 if ((m_frameToolBar) && m_frameToolBar->IsShown() &&
418 (m_frameToolBar->m_widget->parent == m_mainWidget))
419 {
420 int xx = m_miniEdge;
421 int yy = m_miniEdge + m_miniTitle;
422 #if wxUSE_MENUS_NATIVE
423 if (m_frameMenuBar)
424 {
425 if (!m_menuBarDetached)
426 yy += m_menuBarHeight;
427 else
428 yy += wxPLACE_HOLDER;
429 }
430 #endif // wxUSE_MENUS_NATIVE
431
432 m_frameToolBar->m_x = xx;
433 m_frameToolBar->m_y = yy;
434
435 // don't change the toolbar's reported height/width
436 int ww, hh;
437 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
438 {
439 ww = m_toolBarDetached ? wxPLACE_HOLDER
440 : m_frameToolBar->m_width;
441 hh = m_height - 2*m_miniEdge;
442
443 client_area_x_offset += ww;
444 }
445 else
446 {
447 ww = m_width - 2*m_miniEdge;
448 hh = m_toolBarDetached ? wxPLACE_HOLDER
449 : m_frameToolBar->m_height;
450
451 client_area_y_offset += hh;
452 }
453
454 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
455 m_frameToolBar->m_widget,
456 xx, yy, ww, hh );
457 }
458 #endif // wxUSE_TOOLBAR
459
460 int client_x = client_area_x_offset + m_miniEdge;
461 int client_y = client_area_y_offset + m_miniEdge + m_miniTitle;
462 int client_w = m_width - client_area_x_offset - 2*m_miniEdge;
463 int client_h = m_height - client_area_y_offset- 2*m_miniEdge - m_miniTitle;
464 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
465 m_wxwindow,
466 client_x, client_y, client_w, client_h );
467 }
468 else
469 {
470 // If there is no m_mainWidget between m_widget and m_wxwindow there
471 // is no need to set the size or position of m_wxwindow.
472 }
473
474 #if wxUSE_STATUSBAR
475 if (m_frameStatusBar && m_frameStatusBar->IsShown())
476 {
477 int xx = 0 + m_miniEdge;
478 int yy = m_height - wxSTATUS_HEIGHT - m_miniEdge - client_area_y_offset;
479 int ww = m_width - 2*m_miniEdge;
480 int hh = wxSTATUS_HEIGHT;
481 m_frameStatusBar->m_x = xx;
482 m_frameStatusBar->m_y = yy;
483 m_frameStatusBar->m_width = ww;
484 m_frameStatusBar->m_height = hh;
485 gtk_pizza_set_size( GTK_PIZZA(m_wxwindow),
486 m_frameStatusBar->m_widget,
487 xx, yy, ww, hh );
488 if (GTK_WIDGET_DRAWABLE (m_frameStatusBar->m_widget))
489 {
490 gtk_widget_queue_draw (m_frameStatusBar->m_widget);
491 // FIXME: Do we really want to force an immediate redraw?
492 gdk_window_process_updates (m_frameStatusBar->m_widget->window, TRUE);
493 }
494 }
495 #endif // wxUSE_STATUSBAR
496
497 m_sizeSet = true;
498
499 // send size event to frame
500 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
501 event.SetEventObject( this );
502 GetEventHandler()->ProcessEvent( event );
503
504 #if wxUSE_STATUSBAR
505 // send size event to status bar
506 if (m_frameStatusBar)
507 {
508 wxSizeEvent event2( wxSize(m_frameStatusBar->m_width,m_frameStatusBar->m_height), m_frameStatusBar->GetId() );
509 event2.SetEventObject( m_frameStatusBar );
510 m_frameStatusBar->GetEventHandler()->ProcessEvent( event2 );
511 }
512 #endif // wxUSE_STATUSBAR
513
514 m_resizing = false;
515 }
516
517 void wxFrame::OnInternalIdle()
518 {
519 wxFrameBase::OnInternalIdle();
520
521 #if wxUSE_MENUS_NATIVE
522 if (m_frameMenuBar) m_frameMenuBar->OnInternalIdle();
523 #endif // wxUSE_MENUS_NATIVE
524 #if wxUSE_TOOLBAR
525 if (m_frameToolBar) m_frameToolBar->OnInternalIdle();
526 #endif
527 #if wxUSE_STATUSBAR
528 if (m_frameStatusBar)
529 {
530 m_frameStatusBar->OnInternalIdle();
531
532 // There may be controls in the status bar that
533 // need to be updated
534 for ( wxWindowList::compatibility_iterator node = m_frameStatusBar->GetChildren().GetFirst();
535 node;
536 node = node->GetNext() )
537 {
538 wxWindow *child = node->GetData();
539 child->OnInternalIdle();
540 }
541 }
542 #endif
543 }
544
545 // ----------------------------------------------------------------------------
546 // menu/tool/status bar stuff
547 // ----------------------------------------------------------------------------
548
549 #if wxUSE_MENUS_NATIVE
550
551 void wxFrame::DetachMenuBar()
552 {
553 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
554 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
555
556 if ( m_frameMenuBar )
557 {
558 m_frameMenuBar->UnsetInvokingWindow( this );
559
560 if (m_frameMenuBar->GetWindowStyle() & wxMB_DOCKABLE)
561 {
562 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
563 (gpointer) gtk_menu_attached_callback,
564 this);
565
566 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
567 (gpointer) gtk_menu_detached_callback,
568 this);
569 }
570
571 gtk_widget_ref( m_frameMenuBar->m_widget );
572
573 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
574 }
575
576 wxFrameBase::DetachMenuBar();
577 }
578
579 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
580 {
581 wxFrameBase::AttachMenuBar(menuBar);
582
583 if (m_frameMenuBar)
584 {
585 m_frameMenuBar->SetInvokingWindow( this );
586
587 m_frameMenuBar->SetParent(this);
588 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
589 m_frameMenuBar->m_widget,
590 m_frameMenuBar->m_x,
591 m_frameMenuBar->m_y,
592 m_frameMenuBar->m_width,
593 m_frameMenuBar->m_height );
594
595 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
596 {
597 g_signal_connect (menuBar->m_widget, "child_attached",
598 G_CALLBACK (gtk_menu_attached_callback),
599 this);
600 g_signal_connect (menuBar->m_widget, "child_detached",
601 G_CALLBACK (gtk_menu_detached_callback),
602 this);
603 }
604
605 gtk_widget_show( m_frameMenuBar->m_widget );
606
607 UpdateMenuBarSize();
608 }
609 else
610 {
611 m_menuBarHeight = 2;
612 GtkUpdateSize(); // resize window in OnInternalIdle
613 }
614 }
615
616 void wxFrame::UpdateMenuBarSize()
617 {
618 GtkRequisition req;
619
620 req.width = 2;
621 req.height = 2;
622
623 // this is called after Remove with a NULL m_frameMenuBar
624 if ( m_frameMenuBar )
625 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_frameMenuBar->m_widget) )->size_request )
626 (m_frameMenuBar->m_widget, &req );
627
628 m_menuBarHeight = req.height;
629
630 // resize window in OnInternalIdle
631
632 GtkUpdateSize();
633 }
634
635 #endif // wxUSE_MENUS_NATIVE
636
637 #if wxUSE_TOOLBAR
638
639 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
640 {
641 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
642
643 m_insertInClientArea = false;
644
645 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
646
647 m_insertInClientArea = true;
648
649 GtkUpdateSize();
650
651 return m_frameToolBar;
652 }
653
654 void wxFrame::SetToolBar(wxToolBar *toolbar)
655 {
656 bool hadTbar = m_frameToolBar != NULL;
657
658 wxFrameBase::SetToolBar(toolbar);
659
660 if ( m_frameToolBar )
661 {
662 // insert into toolbar area if not already there
663 if ((m_frameToolBar->m_widget->parent) &&
664 (m_frameToolBar->m_widget->parent != m_mainWidget))
665 {
666 GetChildren().DeleteObject( m_frameToolBar );
667
668 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
669 GtkUpdateSize();
670 }
671 }
672 else // toolbar unset
673 {
674 // still need to update size if it had been there before
675 if ( hadTbar )
676 {
677 GtkUpdateSize();
678 }
679 }
680 }
681
682 #endif // wxUSE_TOOLBAR
683
684 #if wxUSE_STATUSBAR
685
686 wxStatusBar* wxFrame::CreateStatusBar(int number,
687 long style,
688 wxWindowID id,
689 const wxString& name)
690 {
691 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
692
693 // because it will change when toolbar is added
694 GtkUpdateSize();
695
696 return wxFrameBase::CreateStatusBar( number, style, id, name );
697 }
698
699 void wxFrame::SetStatusBar(wxStatusBar *statbar)
700 {
701 bool hadStatBar = m_frameStatusBar != NULL;
702
703 wxFrameBase::SetStatusBar(statbar);
704
705 if (hadStatBar && !m_frameStatusBar)
706 GtkUpdateSize();
707 }
708
709 void wxFrame::PositionStatusBar()
710 {
711 if ( !m_frameStatusBar )
712 return;
713
714 GtkUpdateSize();
715 }
716 #endif // wxUSE_STATUSBAR