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