respect minimal wxTLW size in wxUniv
[wxWidgets.git] / src / gtk1 / 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 int minWidth = GetMinWidth(),
354 minHeight = GetMinHeight(),
355 maxWidth = GetMaxWidth(),
356 maxHeight = GetMaxHeight();
357
358 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
359 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
360 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
361 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
362
363 if (m_mainWidget)
364 {
365 /* set size hints */
366 gint flag = 0; // GDK_HINT_POS;
367 if ((minWidth != -1) || (minHeight != -1)) flag |= GDK_HINT_MIN_SIZE;
368 if ((maxWidth != -1) || (maxHeight != -1)) flag |= GDK_HINT_MAX_SIZE;
369 GdkGeometry geom;
370 geom.min_width = minWidth;
371 geom.min_height = minHeight;
372 geom.max_width = maxWidth;
373 geom.max_height = maxHeight;
374 gtk_window_set_geometry_hints( GTK_WINDOW(m_widget),
375 (GtkWidget*) NULL,
376 &geom,
377 (GdkWindowHints) flag );
378
379 /* I revert back to wxGTK's original behaviour. m_mainWidget holds the
380 * menubar, the toolbar and the client area, which is represented by
381 * m_wxwindow.
382 * this hurts in the eye, but I don't want to call SetSize()
383 * because I don't want to call any non-native functions here. */
384
385 #if wxUSE_MENUS_NATIVE
386 if (m_frameMenuBar)
387 {
388 int xx = m_miniEdge;
389 int yy = m_miniEdge + m_miniTitle;
390 int ww = m_width - 2*m_miniEdge;
391 int hh = wxMENU_HEIGHT;
392 if (m_menuBarDetached) hh = wxPLACE_HOLDER;
393 m_frameMenuBar->m_x = xx;
394 m_frameMenuBar->m_y = yy;
395 m_frameMenuBar->m_width = ww;
396 m_frameMenuBar->m_height = hh;
397 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
398 m_frameMenuBar->m_widget,
399 xx, yy, ww, hh );
400 client_area_y_offset += hh;
401 }
402 #endif // wxUSE_MENUS_NATIVE
403
404 #if wxUSE_TOOLBAR
405 if ((m_frameToolBar) && m_frameToolBar->IsShown() &&
406 (m_frameToolBar->m_widget->parent == m_mainWidget))
407 {
408 int xx = m_miniEdge;
409 int yy = m_miniEdge + m_miniTitle;
410 #if wxUSE_MENUS_NATIVE
411 if (m_frameMenuBar)
412 {
413 if (!m_menuBarDetached)
414 yy += wxMENU_HEIGHT;
415 else
416 yy += wxPLACE_HOLDER;
417 }
418 #endif // wxUSE_MENUS_NATIVE
419
420 m_frameToolBar->m_x = xx;
421 m_frameToolBar->m_y = yy;
422
423 /* don't change the toolbar's reported height/width */
424 int ww, hh;
425 if ( m_frameToolBar->GetWindowStyle() & wxTB_VERTICAL )
426 {
427 ww = m_toolBarDetached ? wxPLACE_HOLDER
428 : m_frameToolBar->m_width;
429 hh = m_height - 2*m_miniEdge;
430
431 client_area_x_offset += ww;
432 }
433 else
434 {
435 ww = m_width - 2*m_miniEdge;
436 hh = m_toolBarDetached ? wxPLACE_HOLDER
437 : m_frameToolBar->m_height;
438
439 client_area_y_offset += hh;
440 }
441
442 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
443 m_frameToolBar->m_widget,
444 xx, yy, ww, hh );
445 }
446 #endif // wxUSE_TOOLBAR
447
448 int client_x = client_area_x_offset + m_miniEdge;
449 int client_y = client_area_y_offset + m_miniEdge + m_miniTitle;
450 int client_w = m_width - client_area_x_offset - 2*m_miniEdge;
451 int client_h = m_height - client_area_y_offset- 2*m_miniEdge - m_miniTitle;
452 gtk_pizza_set_size( GTK_PIZZA(m_mainWidget),
453 m_wxwindow,
454 client_x, client_y, client_w, client_h );
455 }
456 else
457 {
458 /* if there is no m_mainWidget between m_widget and m_wxwindow there
459 is no need to set the size or position of m_wxwindow. */
460 }
461
462 #if wxUSE_STATUSBAR
463 if (m_frameStatusBar && m_frameStatusBar->IsShown())
464 {
465 int xx = 0 + m_miniEdge;
466 int yy = m_height - wxSTATUS_HEIGHT - m_miniEdge - client_area_y_offset;
467 int ww = m_width - 2*m_miniEdge;
468 int hh = wxSTATUS_HEIGHT;
469 m_frameStatusBar->m_x = xx;
470 m_frameStatusBar->m_y = yy;
471 m_frameStatusBar->m_width = ww;
472 m_frameStatusBar->m_height = hh;
473 gtk_pizza_set_size( GTK_PIZZA(m_wxwindow),
474 m_frameStatusBar->m_widget,
475 xx, yy, ww, hh );
476 gtk_widget_draw( m_frameStatusBar->m_widget, (GdkRectangle*) NULL );
477 }
478 #endif // wxUSE_STATUSBAR
479
480 m_sizeSet = TRUE;
481
482 // send size event to frame
483 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
484 event.SetEventObject( this );
485 GetEventHandler()->ProcessEvent( event );
486
487 #if wxUSE_STATUSBAR
488 // send size event to status bar
489 if (m_frameStatusBar)
490 {
491 wxSizeEvent event2( wxSize(m_frameStatusBar->m_width,m_frameStatusBar->m_height), m_frameStatusBar->GetId() );
492 event2.SetEventObject( m_frameStatusBar );
493 m_frameStatusBar->GetEventHandler()->ProcessEvent( event2 );
494 }
495 #endif // wxUSE_STATUSBAR
496
497 m_resizing = FALSE;
498 }
499
500 void wxFrame::OnInternalIdle()
501 {
502 wxTopLevelWindow::OnInternalIdle();
503
504 #if wxUSE_MENUS_NATIVE
505 if (m_frameMenuBar) m_frameMenuBar->OnInternalIdle();
506 #endif // wxUSE_MENUS_NATIVE
507 #if wxUSE_TOOLBAR
508 if (m_frameToolBar) m_frameToolBar->OnInternalIdle();
509 #endif
510 #if wxUSE_STATUSBAR
511 if (m_frameStatusBar) m_frameStatusBar->OnInternalIdle();
512 #endif
513 }
514
515 // ----------------------------------------------------------------------------
516 // menu/tool/status bar stuff
517 // ----------------------------------------------------------------------------
518
519 #if wxUSE_MENUS_NATIVE
520
521 void wxFrame::DetachMenuBar()
522 {
523 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
524 wxASSERT_MSG( (m_wxwindow != NULL), wxT("invalid frame") );
525
526 if ( m_frameMenuBar )
527 {
528 m_frameMenuBar->UnsetInvokingWindow( this );
529
530 if (m_frameMenuBar->GetWindowStyle() & wxMB_DOCKABLE)
531 {
532 gtk_signal_disconnect_by_func( GTK_OBJECT(m_frameMenuBar->m_widget),
533 GTK_SIGNAL_FUNC(gtk_menu_attached_callback), (gpointer)this );
534
535 gtk_signal_disconnect_by_func( GTK_OBJECT(m_frameMenuBar->m_widget),
536 GTK_SIGNAL_FUNC(gtk_menu_detached_callback), (gpointer)this );
537 }
538
539 gtk_widget_ref( m_frameMenuBar->m_widget );
540
541 gtk_container_remove( GTK_CONTAINER(m_mainWidget), m_frameMenuBar->m_widget );
542 }
543
544 wxFrameBase::DetachMenuBar();
545 }
546
547 void wxFrame::AttachMenuBar( wxMenuBar *menuBar )
548 {
549 wxFrameBase::AttachMenuBar(menuBar);
550
551 if (m_frameMenuBar)
552 {
553 m_frameMenuBar->SetInvokingWindow( this );
554
555 m_frameMenuBar->SetParent(this);
556 gtk_pizza_put( GTK_PIZZA(m_mainWidget),
557 m_frameMenuBar->m_widget,
558 m_frameMenuBar->m_x,
559 m_frameMenuBar->m_y,
560 m_frameMenuBar->m_width,
561 m_frameMenuBar->m_height );
562
563 if (menuBar->GetWindowStyle() & wxMB_DOCKABLE)
564 {
565 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_attached",
566 GTK_SIGNAL_FUNC(gtk_menu_attached_callback), (gpointer)this );
567
568 gtk_signal_connect( GTK_OBJECT(menuBar->m_widget), "child_detached",
569 GTK_SIGNAL_FUNC(gtk_menu_detached_callback), (gpointer)this );
570 }
571
572 m_frameMenuBar->Show( TRUE );
573 }
574
575 // resize window in OnInternalIdle
576 m_sizeSet = FALSE;
577 }
578
579 #endif // wxUSE_MENUS_NATIVE
580
581 #if wxUSE_TOOLBAR
582
583 wxToolBar* wxFrame::CreateToolBar( long style, wxWindowID id, const wxString& name )
584 {
585 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
586
587 m_insertInClientArea = FALSE;
588
589 m_frameToolBar = wxFrameBase::CreateToolBar( style, id, name );
590
591 m_insertInClientArea = TRUE;
592
593 m_sizeSet = FALSE;
594
595 return m_frameToolBar;
596 }
597
598 void wxFrame::SetToolBar(wxToolBar *toolbar)
599 {
600 wxFrameBase::SetToolBar(toolbar);
601
602 if (m_frameToolBar)
603 {
604 // insert into toolbar area if not already there
605 if ((m_frameToolBar->m_widget->parent) &&
606 (m_frameToolBar->m_widget->parent != m_mainWidget))
607 {
608 GetChildren().DeleteObject( m_frameToolBar );
609
610 gtk_widget_reparent( m_frameToolBar->m_widget, m_mainWidget );
611 GtkUpdateSize();
612 }
613 }
614 }
615
616 #endif // wxUSE_TOOLBAR
617
618 #if wxUSE_STATUSBAR
619
620 wxStatusBar* wxFrame::CreateStatusBar(int number,
621 long style,
622 wxWindowID id,
623 const wxString& name)
624 {
625 wxASSERT_MSG( (m_widget != NULL), wxT("invalid frame") );
626
627 // because it will change when toolbar is added
628 m_sizeSet = FALSE;
629
630 return wxFrameBase::CreateStatusBar( number, style, id, name );
631 }
632
633 void wxFrame::PositionStatusBar()
634 {
635 if ( !m_frameStatusBar )
636 return;
637
638 m_sizeSet = FALSE;
639 }
640 #endif // wxUSE_STATUSBAR
641