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