]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/frame.cpp
Only send keypress events to focus widget, not its parents
[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 int width, height;
314 GTKDoGetSize(&width, &height);
315
316 if (m_mainWidget)
317 {
318 // TODO
319 // Rewrite this terrible code to using GtkVBox
320
321 // m_mainWidget holds the menubar, the toolbar and the client
322 // area, which is represented by m_wxwindow.
323
324 #if wxUSE_MENUS_NATIVE
325 int menubarHeight = 0;
326 #endif
327
328 #if wxUSE_MENUS_NATIVE
329 if (HasVisibleMenubar())
330 {
331 int xx = m_miniEdge;
332 int yy = m_miniEdge + m_miniTitle;
333 int ww = width - 2*m_miniEdge;
334 if (ww < 0)
335 ww = 0;
336 menubarHeight = m_menuBarHeight;
337 if (m_menuBarDetached) menubarHeight = wxPLACE_HOLDER;
338 m_frameMenuBar->m_x = xx;
339 m_frameMenuBar->m_y = yy;
340 m_frameMenuBar->m_width = ww;
341 m_frameMenuBar->m_height = menubarHeight;
342 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
343 m_frameMenuBar->m_widget,
344 xx, yy, ww, menubarHeight);
345 client_area_y_offset += menubarHeight;
346 }
347 #endif // wxUSE_MENUS_NATIVE
348
349 #if wxUSE_TOOLBAR
350 if ((m_frameToolBar) && m_frameToolBar->IsShown() &&
351 (m_frameToolBar->m_widget->parent == m_mainWidget))
352 {
353 int xx = m_miniEdge;
354 int yy = m_miniEdge + m_miniTitle
355 #if wxUSE_MENUS_NATIVE
356 + menubarHeight
357 #endif
358 ;
359
360 m_frameToolBar->m_x = xx;
361 m_frameToolBar->m_y = yy;
362
363 // don't change the toolbar's reported height/width
364 int ww, hh;
365 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
366 {
367 ww = m_toolBarDetached ? wxPLACE_HOLDER
368 : m_frameToolBar->m_width;
369 hh = height - 2*m_miniEdge;
370
371 client_area_x_offset += ww;
372 }
373 else if( m_frameToolBar->HasFlag(wxTB_RIGHT) )
374 {
375 yy += 2;
376 ww = m_toolBarDetached ? wxPLACE_HOLDER
377 : m_frameToolBar->m_width;
378 xx = GetClientSize().x - 1;
379 hh = height - 2*m_miniEdge;
380 if( hh < 0 )
381 hh = 0;
382
383 }
384 else if( m_frameToolBar->GetWindowStyle() & wxTB_BOTTOM )
385 {
386 xx = m_miniEdge;
387 yy = GetClientSize().y;
388 #if wxUSE_MENUS_NATIVE
389 yy += m_menuBarHeight;
390 #endif // wxUSE_MENUS_NATIVE
391 m_frameToolBar->m_x = xx;
392 m_frameToolBar->m_y = yy;
393 ww = width - 2*m_miniEdge;
394 hh = m_toolBarDetached ? wxPLACE_HOLDER
395 : m_frameToolBar->m_height;
396 }
397 else
398 {
399 ww = width - 2*m_miniEdge;
400 hh = m_toolBarDetached ? wxPLACE_HOLDER
401 : m_frameToolBar->m_height;
402
403 client_area_y_offset += hh;
404 }
405
406 if (ww < 0)
407 ww = 0;
408 if (hh < 0)
409 hh = 0;
410 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
411 m_frameToolBar->m_widget,
412 xx, yy, ww, hh );
413 }
414 #endif // wxUSE_TOOLBAR
415
416 int client_x = client_area_x_offset + m_miniEdge;
417 int client_y = client_area_y_offset + m_miniEdge + m_miniTitle;
418 int client_w = width - client_area_x_offset - 2*m_miniEdge;
419 int client_h = height - client_area_y_offset- 2*m_miniEdge - m_miniTitle;
420 if (client_w < 0)
421 client_w = 0;
422 if (client_h < 0)
423 client_h = 0;
424 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
425 m_wxwindow,
426 client_x, client_y, client_w, client_h );
427 }
428 else
429 {
430 // If there is no m_mainWidget between m_widget and m_wxwindow there
431 // is no need to set the size or position of m_wxwindow.
432 }
433
434 #if wxUSE_STATUSBAR
435 if (m_frameStatusBar && m_frameStatusBar->IsShown())
436 {
437 int xx = 0 + m_miniEdge;
438 int yy = height - wxSTATUS_HEIGHT - m_miniEdge - client_area_y_offset;
439 int ww = width - 2*m_miniEdge;
440 if (ww < 0)
441 ww = 0;
442 int hh = wxSTATUS_HEIGHT;
443 m_frameStatusBar->m_x = xx;
444 m_frameStatusBar->m_y = yy;
445 m_frameStatusBar->m_width = ww;
446 m_frameStatusBar->m_height = hh;
447 gtk_pizza_set_size( GTK_PIZZA(m_wxwindow),
448 m_frameStatusBar->m_widget,
449 xx, yy, ww, hh );
450 }
451 #endif // wxUSE_STATUSBAR
452
453 m_sizeSet = true;
454
455 // send size event to frame
456 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
457 event.SetEventObject( this );
458 GetEventHandler()->ProcessEvent( event );
459
460 #if wxUSE_STATUSBAR
461 // send size event to status bar
462 if (m_frameStatusBar)
463 {
464 wxSizeEvent event2( wxSize(m_frameStatusBar->m_width,m_frameStatusBar->m_height), m_frameStatusBar->GetId() );
465 event2.SetEventObject( m_frameStatusBar );
466 m_frameStatusBar->GetEventHandler()->ProcessEvent( event2 );
467 }
468 #endif // wxUSE_STATUSBAR
469
470 m_resizing = false;
471 }
472
473 void wxFrame::OnInternalIdle()
474 {
475 wxFrameBase::OnInternalIdle();
476
477 #if wxUSE_MENUS_NATIVE
478 if (m_frameMenuBar) m_frameMenuBar->OnInternalIdle();
479 #endif // wxUSE_MENUS_NATIVE
480 #if wxUSE_TOOLBAR
481 if (m_frameToolBar) m_frameToolBar->OnInternalIdle();
482 #endif
483 #if wxUSE_STATUSBAR
484 if (m_frameStatusBar)
485 {
486 m_frameStatusBar->OnInternalIdle();
487
488 // There may be controls in the status bar that
489 // need to be updated
490 for ( wxWindowList::compatibility_iterator node = m_frameStatusBar->GetChildren().GetFirst();
491 node;
492 node = node->GetNext() )
493 {
494 wxWindow *child = node->GetData();
495 child->OnInternalIdle();
496 }
497 }
498 #endif
499 }
500
501 // ----------------------------------------------------------------------------
502 // menu/tool/status bar stuff
503 // ----------------------------------------------------------------------------
504
505 #if wxUSE_MENUS_NATIVE
506
507 void wxFrame::DetachMenuBar()
508 {
509 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
510 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
511
512 if ( m_frameMenuBar )
513 {
514 m_frameMenuBar->UnsetInvokingWindow( this );
515
516 if (m_frameMenuBar->GetWindowStyle() & wxMB_DOCKABLE)
517 {
518 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
519 (gpointer) gtk_menu_attached_callback,
520 this);
521
522 g_signal_handlers_disconnect_by_func (m_frameMenuBar->m_widget,
523 (gpointer) gtk_menu_detached_callback,
524 this);
525 }
526
527 gtk_widget_ref( m_frameMenuBar->m_widget );
528
529 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
530 }
531
532 wxFrameBase::DetachMenuBar();
533 }
534
535 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
536 {
537 wxFrameBase::AttachMenuBar(menuBar);
538
539 if (m_frameMenuBar)
540 {
541 m_frameMenuBar->SetInvokingWindow( this );
542
543 m_frameMenuBar->SetParent(this);
544 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
545 m_frameMenuBar->m_widget,
546 m_frameMenuBar->m_x,
547 m_frameMenuBar->m_y,
548 m_frameMenuBar->m_width,
549 m_frameMenuBar->m_height );
550
551 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
552 {
553 g_signal_connect (menuBar->m_widget, "child_attached",
554 G_CALLBACK (gtk_menu_attached_callback),
555 this);
556 g_signal_connect (menuBar->m_widget, "child_detached",
557 G_CALLBACK (gtk_menu_detached_callback),
558 this);
559 }
560
561 gtk_widget_show( m_frameMenuBar->m_widget );
562
563 UpdateMenuBarSize();
564 }
565 else
566 {
567 m_menuBarHeight = 2;
568 GtkUpdateSize(); // resize window in OnInternalIdle
569 }
570 }
571
572 void wxFrame::UpdateMenuBarSize()
573 {
574 m_menuBarHeight = 2;
575
576 // this is called after Remove with a NULL m_frameMenuBar
577 if ( m_frameMenuBar )
578 {
579 GtkRequisition req;
580 gtk_widget_ensure_style(m_frameMenuBar->m_widget);
581 // have to call class method directly because
582 // "size_request" signal is overridden by wx
583 GTK_WIDGET_GET_CLASS(m_frameMenuBar->m_widget)->size_request(
584 m_frameMenuBar->m_widget, &req);
585
586 m_menuBarHeight = req.height;
587 }
588
589 // resize window in OnInternalIdle
590 GtkUpdateSize();
591 }
592
593 bool wxFrame::HasVisibleMenubar() const
594 {
595 return m_frameMenuBar && m_frameMenuBar->IsShown();
596 }
597 #endif // wxUSE_MENUS_NATIVE
598
599 #if wxUSE_TOOLBAR
600
601 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
602 {
603 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
604
605 InsertChildFunction save = m_insertCallback;
606 m_insertCallback = wxInsertChildInFrame;
607 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
608 m_insertCallback = save;
609
610 GtkUpdateSize();
611
612 return m_frameToolBar;
613 }
614
615 void wxFrame::SetToolBar(wxToolBar *toolbar)
616 {
617 bool hadTbar = m_frameToolBar != NULL;
618
619 wxFrameBase::SetToolBar(toolbar);
620
621 if ( m_frameToolBar )
622 {
623 // insert into toolbar area if not already there
624 if ((m_frameToolBar->m_widget->parent) &&
625 (m_frameToolBar->m_widget->parent != m_mainWidget))
626 {
627 GetChildren().DeleteObject( m_frameToolBar );
628
629 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
630 GtkUpdateSize();
631 }
632 }
633 else // toolbar unset
634 {
635 // still need to update size if it had been there before
636 if ( hadTbar )
637 {
638 GtkUpdateSize();
639 }
640 }
641 }
642
643 #endif // wxUSE_TOOLBAR
644
645 #if wxUSE_STATUSBAR
646
647 wxStatusBar* wxFrame::CreateStatusBar(int number,
648 long style,
649 wxWindowID id,
650 const wxString& name)
651 {
652 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
653
654 // because it will change when toolbar is added
655 GtkUpdateSize();
656
657 return wxFrameBase::CreateStatusBar( number, style, id, name );
658 }
659
660 void wxFrame::SetStatusBar(wxStatusBar *statbar)
661 {
662 bool hadStatBar = m_frameStatusBar != NULL;
663
664 wxFrameBase::SetStatusBar(statbar);
665
666 if (hadStatBar && !m_frameStatusBar)
667 GtkUpdateSize();
668 }
669
670 void wxFrame::PositionStatusBar()
671 {
672 if ( !m_frameStatusBar )
673 return;
674
675 GtkUpdateSize();
676 }
677 #endif // wxUSE_STATUSBAR