]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/frame.cpp
corrected off by 1 error in cMB2WC() call (thanks valgrind)
[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 (HasVisibleMenubar() && !m_menuBarDetached)
206 {
207 *height -= m_menuBarHeight;
208 }
209 #endif // wxUSE_MENUS_NATIVE
210
211 #if wxUSE_STATUSBAR
212 // status bar
213 if (m_frameStatusBar && GTK_WIDGET_VISIBLE(m_frameStatusBar->m_widget))
214 *height -= wxSTATUS_HEIGHT;
215 #endif // wxUSE_STATUSBAR
216 }
217
218 #if wxUSE_TOOLBAR
219 // tool bar
220 if (m_frameToolBar &&
221 GTK_WIDGET_VISIBLE(m_frameToolBar->m_widget) && !m_toolBarDetached)
222 {
223 if (m_frameToolBar->IsVertical())
224 {
225 if (width)
226 *width -= m_frameToolBar->GetSize().x;
227 }
228 else
229 {
230 if (height)
231 *height -= m_frameToolBar->GetSize().y;
232 }
233 }
234 #endif // wxUSE_TOOLBAR
235
236 if (width != NULL && *width < 0)
237 *width = 0;
238 if (height != NULL && *height < 0)
239 *height = 0;
240 }
241
242 bool wxFrame::ShowFullScreen(bool show, long style)
243 {
244 if (!wxFrameBase::ShowFullScreen(show, style))
245 return false;
246
247 wxWindow* const bar[] = {
248 #if wxUSE_MENUS
249 m_frameMenuBar,
250 #else
251 NULL,
252 #endif
253 #if wxUSE_TOOLBAR
254 m_frameToolBar,
255 #else
256 NULL,
257 #endif
258 #if wxUSE_STATUSBAR
259 m_frameStatusBar,
260 #else
261 NULL,
262 #endif
263 };
264 const long fsNoBar[] = {
265 wxFULLSCREEN_NOMENUBAR, wxFULLSCREEN_NOTOOLBAR, wxFULLSCREEN_NOSTATUSBAR
266 };
267 for (int i = 0; i < 3; i++)
268 {
269 if (show)
270 {
271 if (bar[i] && (style & fsNoBar[i]))
272 {
273 if (bar[i]->IsShown())
274 bar[i]->Show(false);
275 else
276 style &= ~fsNoBar[i];
277 }
278 }
279 else
280 {
281 if (bar[i] && (m_fsSaveFlag & fsNoBar[i]))
282 bar[i]->Show(true);
283 }
284 }
285 if (show)
286 m_fsSaveFlag = style;
287
288 return true;
289 }
290
291 void wxFrame::GtkOnSize()
292 {
293 // avoid recursions
294 if (m_resizing) return;
295 m_resizing = true;
296
297 // this shouldn't happen: wxFrame, wxMDIParentFrame and wxMDIChildFrame have m_wxwindow
298 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
299
300 // space occupied by m_frameToolBar and m_frameMenuBar
301 int client_area_x_offset = 0,
302 client_area_y_offset = 0;
303
304 /* wxMDIChildFrame derives from wxFrame but it _is_ a wxWindow as it uses
305 wxWindow::Create to create it's GTK equivalent. m_mainWidget is only
306 set in wxFrame::Create so it is used to check what kind of frame we
307 have here. if m_mainWidget is NULL it is a wxMDIChildFrame and so we
308 skip the part which handles m_frameMenuBar, m_frameToolBar and (most
309 importantly) m_mainWidget */
310
311 ConstrainSize();
312
313 if (m_mainWidget)
314 {
315 // TODO
316 // Rewrite this terrible code to using GtkVBox
317
318 // m_mainWidget holds the menubar, the toolbar and the client
319 // area, which is represented by m_wxwindow.
320
321 #if wxUSE_MENUS_NATIVE
322 int menubarHeight = 0;
323 #endif
324
325 #if wxUSE_MENUS_NATIVE
326 if (HasVisibleMenubar())
327 {
328 int xx = m_miniEdge;
329 int yy = m_miniEdge + m_miniTitle;
330 int ww = m_width - 2*m_miniEdge;
331 if (ww < 0)
332 ww = 0;
333 menubarHeight = m_menuBarHeight;
334 if (m_menuBarDetached) menubarHeight = wxPLACE_HOLDER;
335 m_frameMenuBar->m_x = xx;
336 m_frameMenuBar->m_y = yy;
337 m_frameMenuBar->m_width = ww;
338 m_frameMenuBar->m_height = menubarHeight;
339 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
340 m_frameMenuBar->m_widget,
341 xx, yy, ww, menubarHeight);
342 client_area_y_offset += menubarHeight;
343 }
344 #endif // wxUSE_MENUS_NATIVE
345
346 #if wxUSE_TOOLBAR
347 if ((m_frameToolBar) && m_frameToolBar->IsShown() &&
348 (m_frameToolBar->m_widget->parent == m_mainWidget))
349 {
350 int xx = m_miniEdge;
351 int yy = m_miniEdge + m_miniTitle
352 #if wxUSE_MENUS_NATIVE
353 + menubarHeight
354 #endif
355 ;
356
357 m_frameToolBar->m_x = xx;
358 m_frameToolBar->m_y = yy;
359
360 // don't change the toolbar's reported height/width
361 int ww, hh;
362 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
363 {
364 ww = m_toolBarDetached ? wxPLACE_HOLDER
365 : m_frameToolBar->m_width;
366 hh = m_height - 2*m_miniEdge;
367
368 client_area_x_offset += ww;
369 }
370 else if( m_frameToolBar->HasFlag(wxTB_RIGHT) )
371 {
372 yy += 2;
373 ww = m_toolBarDetached ? wxPLACE_HOLDER
374 : m_frameToolBar->m_width;
375 xx = GetClientSize().x - 1;
376 hh = m_height - 2*m_miniEdge;
377 if( hh < 0 )
378 hh = 0;
379
380 }
381 else if( m_frameToolBar->GetWindowStyle() & wxTB_BOTTOM )
382 {
383 xx = m_miniEdge;
384 yy = GetClientSize().y;
385 #if wxUSE_MENUS_NATIVE
386 yy += m_menuBarHeight;
387 #endif // wxUSE_MENUS_NATIVE
388 m_frameToolBar->m_x = xx;
389 m_frameToolBar->m_y = yy;
390 ww = m_width - 2*m_miniEdge;
391 hh = m_toolBarDetached ? wxPLACE_HOLDER
392 : m_frameToolBar->m_height;
393 }
394 else
395 {
396 ww = m_width - 2*m_miniEdge;
397 hh = m_toolBarDetached ? wxPLACE_HOLDER
398 : m_frameToolBar->m_height;
399
400 client_area_y_offset += hh;
401 }
402
403 if (ww < 0)
404 ww = 0;
405 if (hh < 0)
406 hh = 0;
407 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
408 m_frameToolBar->m_widget,
409 xx, yy, ww, hh );
410 }
411 #endif // wxUSE_TOOLBAR
412
413 int client_x = client_area_x_offset + m_miniEdge;
414 int client_y = client_area_y_offset + m_miniEdge + m_miniTitle;
415 int client_w = m_width - client_area_x_offset - 2*m_miniEdge;
416 int client_h = m_height - client_area_y_offset- 2*m_miniEdge - m_miniTitle;
417 if (client_w < 0)
418 client_w = 0;
419 if (client_h < 0)
420 client_h = 0;
421 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
422 m_wxwindow,
423 client_x, client_y, client_w, client_h );
424 }
425 else
426 {
427 // If there is no m_mainWidget between m_widget and m_wxwindow there
428 // is no need to set the size or position of m_wxwindow.
429 }
430
431 #if wxUSE_STATUSBAR
432 if (m_frameStatusBar && m_frameStatusBar->IsShown())
433 {
434 int xx = 0 + m_miniEdge;
435 int yy = m_height - wxSTATUS_HEIGHT - m_miniEdge - client_area_y_offset;
436 int ww = m_width - 2*m_miniEdge;
437 if (ww < 0)
438 ww = 0;
439 int hh = wxSTATUS_HEIGHT;
440 m_frameStatusBar->m_x = xx;
441 m_frameStatusBar->m_y = yy;
442 m_frameStatusBar->m_width = ww;
443 m_frameStatusBar->m_height = hh;
444 gtk_pizza_set_size( GTK_PIZZA(m_wxwindow),
445 m_frameStatusBar->m_widget,
446 xx, yy, ww, hh );
447 }
448 #endif // wxUSE_STATUSBAR
449
450 m_sizeSet = true;
451
452 // send size event to frame
453 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
454 event.SetEventObject( this );
455 GetEventHandler()->ProcessEvent( event );
456
457 #if wxUSE_STATUSBAR
458 // send size event to status bar
459 if (m_frameStatusBar)
460 {
461 wxSizeEvent event2( wxSize(m_frameStatusBar->m_width,m_frameStatusBar->m_height), m_frameStatusBar->GetId() );
462 event2.SetEventObject( m_frameStatusBar );
463 m_frameStatusBar->GetEventHandler()->ProcessEvent( event2 );
464 }
465 #endif // wxUSE_STATUSBAR
466
467 m_resizing = false;
468 }
469
470 void wxFrame::OnInternalIdle()
471 {
472 wxFrameBase::OnInternalIdle();
473
474 #if wxUSE_MENUS_NATIVE
475 if (m_frameMenuBar) m_frameMenuBar->OnInternalIdle();
476 #endif // wxUSE_MENUS_NATIVE
477 #if wxUSE_TOOLBAR
478 if (m_frameToolBar) m_frameToolBar->OnInternalIdle();
479 #endif
480 #if wxUSE_STATUSBAR
481 if (m_frameStatusBar)
482 {
483 m_frameStatusBar->OnInternalIdle();
484
485 // There may be controls in the status bar that
486 // need to be updated
487 for ( wxWindowList::compatibility_iterator node = m_frameStatusBar->GetChildren().GetFirst();
488 node;
489 node = node->GetNext() )
490 {
491 wxWindow *child = node->GetData();
492 child->OnInternalIdle();
493 }
494 }
495 #endif
496 }
497
498 // ----------------------------------------------------------------------------
499 // menu/tool/status bar stuff
500 // ----------------------------------------------------------------------------
501
502 #if wxUSE_MENUS_NATIVE
503
504 void wxFrame::DetachMenuBar()
505 {
506 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
507 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
508
509 if ( m_frameMenuBar )
510 {
511 m_frameMenuBar->UnsetInvokingWindow( this );
512
513 if (m_frameMenuBar->GetWindowStyle() & wxMB_DOCKABLE)
514 {
515 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
516 (gpointer) gtk_menu_attached_callback,
517 this);
518
519 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
520 (gpointer) gtk_menu_detached_callback,
521 this);
522 }
523
524 gtk_widget_ref( m_frameMenuBar->m_widget );
525
526 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
527 }
528
529 wxFrameBase::DetachMenuBar();
530 }
531
532 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
533 {
534 wxFrameBase::AttachMenuBar(menuBar);
535
536 if (m_frameMenuBar)
537 {
538 m_frameMenuBar->SetInvokingWindow( this );
539
540 m_frameMenuBar->SetParent(this);
541 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
542 m_frameMenuBar->m_widget,
543 m_frameMenuBar->m_x,
544 m_frameMenuBar->m_y,
545 m_frameMenuBar->m_width,
546 m_frameMenuBar->m_height );
547
548 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
549 {
550 g_signal_connect (menuBar->m_widget, "child_attached",
551 G_CALLBACK (gtk_menu_attached_callback),
552 this);
553 g_signal_connect (menuBar->m_widget, "child_detached",
554 G_CALLBACK (gtk_menu_detached_callback),
555 this);
556 }
557
558 gtk_widget_show( m_frameMenuBar->m_widget );
559
560 UpdateMenuBarSize();
561 }
562 else
563 {
564 m_menuBarHeight = 2;
565 GtkUpdateSize(); // resize window in OnInternalIdle
566 }
567 }
568
569 void wxFrame::UpdateMenuBarSize()
570 {
571 m_menuBarHeight = 2;
572
573 // this is called after Remove with a NULL m_frameMenuBar
574 if ( m_frameMenuBar )
575 {
576 GtkRequisition req;
577 gtk_widget_ensure_style(m_frameMenuBar->m_widget);
578 // have to call class method directly because
579 // "size_request" signal is overridden by wx
580 GTK_WIDGET_GET_CLASS(m_frameMenuBar->m_widget)->size_request(
581 m_frameMenuBar->m_widget, &req);
582
583 m_menuBarHeight = req.height;
584 }
585
586 // resize window in OnInternalIdle
587 GtkUpdateSize();
588 }
589
590 bool wxFrame::HasVisibleMenubar() const
591 {
592 return m_frameMenuBar && m_frameMenuBar->IsShown();
593 }
594 #endif // wxUSE_MENUS_NATIVE
595
596 #if wxUSE_TOOLBAR
597
598 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
599 {
600 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
601
602 InsertChildFunction save = m_insertCallback;
603 m_insertCallback = wxInsertChildInFrame;
604 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
605 m_insertCallback = save;
606
607 GtkUpdateSize();
608
609 return m_frameToolBar;
610 }
611
612 void wxFrame::SetToolBar(wxToolBar *toolbar)
613 {
614 bool hadTbar = m_frameToolBar != NULL;
615
616 wxFrameBase::SetToolBar(toolbar);
617
618 if ( m_frameToolBar )
619 {
620 // insert into toolbar area if not already there
621 if ((m_frameToolBar->m_widget->parent) &&
622 (m_frameToolBar->m_widget->parent != m_mainWidget))
623 {
624 GetChildren().DeleteObject( m_frameToolBar );
625
626 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
627 GtkUpdateSize();
628 }
629 }
630 else // toolbar unset
631 {
632 // still need to update size if it had been there before
633 if ( hadTbar )
634 {
635 GtkUpdateSize();
636 }
637 }
638 }
639
640 #endif // wxUSE_TOOLBAR
641
642 #if wxUSE_STATUSBAR
643
644 wxStatusBar* wxFrame::CreateStatusBar(int number,
645 long style,
646 wxWindowID id,
647 const wxString& name)
648 {
649 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
650
651 // because it will change when toolbar is added
652 GtkUpdateSize();
653
654 return wxFrameBase::CreateStatusBar( number, style, id, name );
655 }
656
657 void wxFrame::SetStatusBar(wxStatusBar *statbar)
658 {
659 bool hadStatBar = m_frameStatusBar != NULL;
660
661 wxFrameBase::SetStatusBar(statbar);
662
663 if (hadStatBar && !m_frameStatusBar)
664 GtkUpdateSize();
665 }
666
667 void wxFrame::PositionStatusBar()
668 {
669 if ( !m_frameStatusBar )
670 return;
671
672 GtkUpdateSize();
673 }
674 #endif // wxUSE_STATUSBAR