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