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