]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/frame.cpp
Redid parts of wxScroledWindow
[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 #ifdef __GNUG__
11 #pragma implementation "frame.h"
12 #endif
13
14 #include "wx/frame.h"
15 #include "wx/dialog.h"
16 #include "wx/control.h"
17 #include "wx/app.h"
18 #include "wx/menu.h"
19 #include "wx/toolbar.h"
20 #include "wx/statusbr.h"
21 #include "wx/dcclient.h"
22 #include "wx/gtk/win_gtk.h"
23
24 //-----------------------------------------------------------------------------
25 // constants
26 //-----------------------------------------------------------------------------
27
28 const int wxMENU_HEIGHT = 27;
29 const int wxSTATUS_HEIGHT = 25;
30
31 //-----------------------------------------------------------------------------
32 // data
33 //-----------------------------------------------------------------------------
34
35 extern wxList wxTopLevelWindows;
36 extern wxList wxPendingDelete;
37
38 //-----------------------------------------------------------------------------
39 // "size_allocate"
40 //-----------------------------------------------------------------------------
41
42 static void gtk_frame_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxFrame *win )
43 {
44 if (!win->HasVMT()) return;
45
46 /*
47 printf( "OnFrameResize from " );
48 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
49 printf( win->GetClassInfo()->GetClassName() );
50 printf( ".\n" );
51 */
52
53 if ((win->m_width != alloc->width) || (win->m_height != alloc->height))
54 {
55 win->m_sizeSet = FALSE;
56 win->m_width = alloc->width;
57 win->m_height = alloc->height;
58 }
59 }
60
61 //-----------------------------------------------------------------------------
62 // "delete_event"
63 //-----------------------------------------------------------------------------
64
65 static gint gtk_frame_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxFrame *win )
66 {
67 /*
68 printf( "OnDelete from " );
69 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
70 printf( win->GetClassInfo()->GetClassName() );
71 printf( ".\n" );
72 */
73
74 win->Close();
75
76 return TRUE;
77 }
78
79 //-----------------------------------------------------------------------------
80 // "configure_event"
81 //-----------------------------------------------------------------------------
82
83 static gint gtk_frame_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxFrame *win )
84 {
85 if (!win->HasVMT()) return FALSE;
86
87 win->m_x = event->x;
88 win->m_y = event->y;
89
90 return FALSE;
91 }
92
93 //-----------------------------------------------------------------------------
94 // wxFrame
95 //-----------------------------------------------------------------------------
96
97 BEGIN_EVENT_TABLE(wxFrame, wxWindow)
98 EVT_SIZE(wxFrame::OnSize)
99 EVT_CLOSE(wxFrame::OnCloseWindow)
100 EVT_IDLE(wxFrame::OnIdle)
101 END_EVENT_TABLE()
102
103 IMPLEMENT_DYNAMIC_CLASS(wxFrame,wxWindow)
104
105 wxFrame::wxFrame()
106 {
107 m_frameMenuBar = (wxMenuBar *) NULL;
108 m_frameStatusBar = (wxStatusBar *) NULL;
109 m_frameToolBar = (wxToolBar *) NULL;
110 m_sizeSet = FALSE;
111 m_miniEdge = 0;
112 m_miniTitle = 0;
113 }
114
115 wxFrame::wxFrame( wxWindow *parent, wxWindowID id, const wxString &title,
116 const wxPoint &pos, const wxSize &size,
117 long style, const wxString &name )
118 {
119 m_frameMenuBar = (wxMenuBar *) NULL;
120 m_frameStatusBar = (wxStatusBar *) NULL;
121 m_frameToolBar = (wxToolBar *) NULL;
122 m_sizeSet = FALSE;
123 m_miniEdge = 0;
124 m_miniTitle = 0;
125 Create( parent, id, title, pos, size, style, name );
126 }
127
128 bool wxFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
129 const wxPoint &pos, const wxSize &size,
130 long style, const wxString &name )
131 {
132 wxTopLevelWindows.Append( this );
133
134 m_needParent = FALSE;
135
136 PreCreation( parent, id, pos, size, style, name );
137
138 m_title = title;
139
140 GtkWindowType win_type = GTK_WINDOW_TOPLEVEL;
141 if (style & wxSIMPLE_BORDER) win_type = GTK_WINDOW_POPUP;
142
143 m_widget = gtk_window_new( win_type );
144
145 if ((size.x != -1) && (size.y != -1))
146 gtk_widget_set_usize( m_widget, m_width, m_height );
147 if ((pos.x != -1) && (pos.y != -1))
148 gtk_widget_set_uposition( m_widget, m_x, m_y );
149
150 gtk_window_set_title( GTK_WINDOW(m_widget), title );
151 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
152
153 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL );
154
155 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
156 GTK_SIGNAL_FUNC(gtk_frame_delete_callback), (gpointer)this );
157
158 m_wxwindow = gtk_myfixed_new();
159 gtk_widget_show( m_wxwindow );
160 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
161
162 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
163
164 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
165 GTK_SIGNAL_FUNC(gtk_frame_size_callback), (gpointer)this );
166
167 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
168 GTK_SIGNAL_FUNC(gtk_frame_configure_callback), (gpointer)this );
169
170 if (m_parent) m_parent->AddChild( this );
171
172 PostCreation();
173
174 return TRUE;
175 }
176
177 wxFrame::~wxFrame()
178 {
179 if (m_frameMenuBar) delete m_frameMenuBar;
180 if (m_frameStatusBar) delete m_frameStatusBar;
181 if (m_frameToolBar) delete m_frameToolBar;
182
183 wxTopLevelWindows.DeleteObject( this );
184 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
185 }
186
187 bool wxFrame::Show( bool show )
188 {
189 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
190
191 if (show && !m_sizeSet)
192 {
193 // by calling GtkOnSize here, we don't have to call
194 // either after showing the frame, which would entail
195 // much ugly flicker nor from within the size_allocate
196 // handler, because GTK 1.1.X forbids that.
197
198 GtkOnSize( m_x, m_y, m_width, m_height );
199 }
200
201 return wxWindow::Show( show );
202 }
203
204 void wxFrame::OnCloseWindow( wxCloseEvent &event )
205 {
206 if (GetEventHandler()->OnClose() || event.GetForce()) this->Destroy();
207 }
208
209 bool wxFrame::Destroy()
210 {
211 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
212
213 if (!wxPendingDelete.Member(this)) wxPendingDelete.Append(this);
214
215 return TRUE;
216 }
217
218 wxPoint wxFrame::GetClientAreaOrigin() const
219 {
220 wxPoint pt( m_miniEdge, m_miniEdge + m_miniTitle );
221 if (m_frameMenuBar)
222 {
223 int h = 0;
224 m_frameMenuBar->GetSize( (int*)NULL, &h );
225 pt.y += h;
226 }
227 if (m_frameToolBar)
228 {
229 int h = 0;
230 m_frameToolBar->GetSize( (int*)NULL, &h );
231 pt.y += h;
232 }
233 return pt;
234 }
235
236 void wxFrame::SetSize( int x, int y, int width, int height, int sizeFlags )
237 {
238 wxASSERT_MSG( (m_widget != NULL), "invalid window" );
239
240 // Don't do anything for children of wxMDIChildFrame
241 if (!m_wxwindow) return;
242
243 if (m_resizing) return; // I don't like recursions
244 m_resizing = TRUE;
245
246 int old_x = m_x;
247 int old_y = m_y;
248 int old_width = m_width;
249 int old_height = m_height;
250
251 if ((sizeFlags & wxSIZE_USE_EXISTING) == wxSIZE_USE_EXISTING)
252 {
253 if (x != -1) m_x = x;
254 if (y != -1) m_y = y;
255 if (width != -1) m_width = width;
256 if (height != -1) m_height = height;
257 }
258 else
259 {
260 m_x = x;
261 m_y = y;
262 m_width = width;
263 m_height = height;
264 }
265
266 if ((sizeFlags & wxSIZE_AUTO_WIDTH) == wxSIZE_AUTO_WIDTH)
267 {
268 if (width == -1) m_width = 80;
269 }
270
271 if ((sizeFlags & wxSIZE_AUTO_HEIGHT) == wxSIZE_AUTO_HEIGHT)
272 {
273 if (height == -1) m_height = 26;
274 }
275
276 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
277 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
278 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
279 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
280
281 if ((m_x != -1) || (m_y != -1))
282 {
283 if ((m_x != old_x) || (m_y != old_y))
284 gtk_widget_set_uposition( m_widget, m_x, m_y );
285 }
286
287 if ((m_width != old_width) || (m_height != old_height))
288 {
289 gtk_widget_set_usize( m_widget, m_width, m_height );
290 }
291
292 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
293 event.SetEventObject( this );
294 GetEventHandler()->ProcessEvent( event );
295
296 m_resizing = FALSE;
297 }
298
299 void wxFrame::SetSize( int width, int height )
300 {
301 SetSize( -1, -1, width, height, wxSIZE_USE_EXISTING );
302 }
303
304 void wxFrame::Centre( int direction )
305 {
306 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
307
308 int x = 0;
309 int y = 0;
310
311 if (direction & wxHORIZONTAL == wxHORIZONTAL) x = (gdk_screen_width () - m_width) / 2;
312 if (direction & wxVERTICAL == wxVERTICAL) y = (gdk_screen_height () - m_height) / 2;
313
314 Move( x, y );
315 }
316
317 void wxFrame::GetClientSize( int *width, int *height ) const
318 {
319 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
320
321 wxWindow::GetClientSize( width, height );
322 if (height)
323 {
324 if (m_frameMenuBar) (*height) -= wxMENU_HEIGHT;
325 if (m_frameStatusBar) (*height) -= wxSTATUS_HEIGHT;
326 if (m_frameToolBar)
327 {
328 int y = 0;
329 m_frameToolBar->GetSize( (int *) NULL, &y );
330 (*height) -= y;
331 }
332 (*height) -= m_miniEdge*2 + m_miniTitle;
333 }
334 if (width)
335 {
336 (*width) -= m_miniEdge*2;
337 }
338 }
339
340 void wxFrame::SetClientSize( int const width, int const height )
341 {
342 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
343
344 int h = height;
345 if (m_frameMenuBar) h += wxMENU_HEIGHT;
346 if (m_frameStatusBar) h += wxSTATUS_HEIGHT;
347 if (m_frameToolBar)
348 {
349 int y = 0;
350 m_frameToolBar->GetSize( (int *) NULL, &y );
351 h += y;
352 }
353 wxWindow::SetClientSize( width + m_miniEdge*2, h + m_miniEdge*2 + m_miniTitle );
354 }
355
356 void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
357 {
358 // due to a bug in gtk, x,y are always 0
359 // m_x = x;
360 // m_y = y;
361
362 if (m_resizing) return;
363 m_resizing = TRUE;
364
365 if (!m_wxwindow) return;
366
367 m_width = width;
368 m_height = height;
369
370 if ((m_minWidth != -1) && (m_width < m_minWidth)) m_width = m_minWidth;
371 if ((m_minHeight != -1) && (m_height < m_minHeight)) m_height = m_minHeight;
372 if ((m_maxWidth != -1) && (m_width > m_maxWidth)) m_width = m_minWidth;
373 if ((m_maxHeight != -1) && (m_height > m_maxHeight)) m_height = m_minHeight;
374
375 gtk_widget_set_usize( m_widget, m_width, m_height );
376
377 // this emulates the new wxMSW behaviour of placing all
378 // frame-subwindows (menu, toolbar..) on one native window
379 // OK, this hurts in the eye, but I don't want to call SetSize()
380 // because I don't want to call any non-native functions here.
381
382 if (m_frameMenuBar)
383 {
384 int xx = m_miniEdge;
385 int yy = m_miniEdge + m_miniTitle;
386 int ww = m_width - 2*m_miniEdge;
387 int hh = wxMENU_HEIGHT;
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
393 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow), m_frameMenuBar->m_widget, xx, yy );
394 gtk_widget_set_usize( m_frameMenuBar->m_widget, ww, hh );
395 }
396
397 if (m_frameToolBar)
398 {
399 int xx = m_miniEdge;
400 int yy = m_miniEdge + m_miniTitle;
401 if (m_frameMenuBar) yy += wxMENU_HEIGHT;
402 int ww = m_width - 2*m_miniEdge;
403 int hh = m_frameToolBar->m_height;
404
405 m_frameToolBar->m_x = xx;
406 m_frameToolBar->m_y = yy;
407 m_frameToolBar->m_height = hh;
408 m_frameToolBar->m_width = ww;
409
410 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow), m_frameToolBar->m_widget, xx, yy );
411 gtk_widget_set_usize( m_frameToolBar->m_widget, ww, hh );
412 }
413
414 if (m_frameStatusBar)
415 {
416 int xx = 0 + m_miniEdge;
417 int yy = m_height - wxSTATUS_HEIGHT - m_miniEdge;
418 int ww = m_width - 2*m_miniEdge;
419 int hh = wxSTATUS_HEIGHT;
420
421 m_frameStatusBar->m_x = xx;
422 m_frameStatusBar->m_y = yy;
423 m_frameStatusBar->m_width = ww;
424 m_frameStatusBar->m_height = hh;
425
426 gtk_myfixed_move( GTK_MYFIXED(m_wxwindow), m_frameStatusBar->m_widget, xx, yy );
427 gtk_widget_set_usize( m_frameStatusBar->m_widget, ww, hh );
428 }
429
430 m_sizeSet = TRUE;
431
432 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
433 event.SetEventObject( this );
434 GetEventHandler()->ProcessEvent( event );
435
436 m_resizing = FALSE;
437 }
438
439 void wxFrame::OnIdle(wxIdleEvent& WXUNUSED(event) )
440 {
441 if (!m_sizeSet)
442 GtkOnSize( m_x, m_y, m_width, m_height );
443
444 DoMenuUpdates();
445 }
446
447 void wxFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
448 {
449 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
450
451 if (GetAutoLayout())
452 {
453 Layout();
454 }
455 else
456 {
457 // no child: go out !
458 if (!GetChildren().First()) return;
459
460 // do we have exactly one child?
461 wxWindow *child = (wxWindow *) NULL;
462 for(wxNode *node = GetChildren().First(); node; node = node->Next())
463 {
464 wxWindow *win = (wxWindow *)node->Data();
465 if (!IS_KIND_OF(win,wxFrame) && !IS_KIND_OF(win,wxDialog)
466 #if 0 // not in m_children anyway ?
467 && (win != m_frameMenuBar) &&
468 (win != m_frameToolBar) &&
469 (win != m_frameStatusBar)
470 #endif
471 )
472 {
473 // it's the second one: do nothing
474 if (child) return;
475 child = win;
476 }
477 }
478
479 // yes: set it's size to fill all the frame
480 int client_x, client_y;
481 GetClientSize( &client_x, &client_y );
482 child->SetSize( 1, 1, client_x-2, client_y-2 );
483 }
484 }
485
486 static void SetInvokingWindow( wxMenu *menu, wxWindow *win )
487 {
488 menu->SetInvokingWindow( win );
489 wxNode *node = menu->m_items.First();
490 while (node)
491 {
492 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
493 if (menuitem->IsSubMenu())
494 SetInvokingWindow( menuitem->GetSubMenu(), win );
495 node = node->Next();
496 }
497 }
498
499 void wxFrame::SetMenuBar( wxMenuBar *menuBar )
500 {
501 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
502 wxASSERT_MSG( (m_wxwindow != NULL), "invalid frame" );
503
504 m_frameMenuBar = menuBar;
505
506 if (m_frameMenuBar)
507 {
508 wxNode *node = m_frameMenuBar->m_menus.First();
509 while (node)
510 {
511 wxMenu *menu = (wxMenu*)node->Data();
512 SetInvokingWindow( menu, this );
513 node = node->Next();
514 }
515
516 if (m_frameMenuBar->m_parent != this)
517 {
518 m_frameMenuBar->m_parent = this;
519 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow),
520 m_frameMenuBar->m_widget, m_frameMenuBar->m_x, m_frameMenuBar->m_y );
521 }
522 }
523
524 if (m_sizeSet) GtkOnSize( m_x, m_y, m_width, m_height );
525 }
526
527 wxMenuBar *wxFrame::GetMenuBar() const
528 {
529 return m_frameMenuBar;
530 }
531
532 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
533 {
534 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
535
536 wxCHECK_MSG( m_frameToolBar == NULL, FALSE, "recreating toolbar in wxFrame" );
537
538 m_frameToolBar = OnCreateToolBar( style, id, name );
539
540 GetChildren().DeleteObject( m_frameToolBar );
541
542 if (m_sizeSet) GtkOnSize( m_x, m_y, m_width, m_height );
543
544 return m_frameToolBar;
545 }
546
547 wxToolBar* wxFrame::OnCreateToolBar( long style, wxWindowID id, const wxString& name )
548 {
549 return new wxToolBar( this, id, wxDefaultPosition, wxDefaultSize, style, name );
550 }
551
552 wxToolBar *wxFrame::GetToolBar() const
553 {
554 return m_frameToolBar;
555 }
556
557 wxStatusBar* wxFrame::CreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
558 {
559 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
560
561 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE, "recreating status bar in wxFrame" );
562
563 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
564
565 if (m_sizeSet) GtkOnSize( m_x, m_y, m_width, m_height );
566
567 return m_frameStatusBar;
568 }
569
570 wxStatusBar *wxFrame::OnCreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
571 {
572 wxStatusBar *statusBar = (wxStatusBar *) NULL;
573
574 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20), style, name);
575
576 // Set the height according to the font and the border size
577 wxClientDC dc(statusBar);
578 dc.SetFont( statusBar->GetFont() );
579
580 long x, y;
581 dc.GetTextExtent( "X", &x, &y );
582
583 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
584
585 statusBar->SetSize( -1, -1, 100, height );
586
587 statusBar->SetFieldsCount( number );
588 return statusBar;
589 }
590
591 void wxFrame::SetStatusText(const wxString& text, int number)
592 {
593 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
594
595 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
596
597 m_frameStatusBar->SetStatusText(text, number);
598 }
599
600 void wxFrame::SetStatusWidths(int n, const int widths_field[] )
601 {
602 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
603
604 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
605
606 m_frameStatusBar->SetStatusWidths(n, widths_field);
607 }
608
609 wxStatusBar *wxFrame::GetStatusBar() const
610 {
611 return m_frameStatusBar;
612 }
613
614 void wxFrame::SetTitle( const wxString &title )
615 {
616 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
617
618 m_title = title;
619 if (m_title.IsNull()) m_title = "";
620 gtk_window_set_title( GTK_WINDOW(m_widget), title );
621 }
622
623 void wxFrame::SetIcon( const wxIcon &icon )
624 {
625 wxASSERT_MSG( (m_widget != NULL), "invalid frame" );
626
627 m_icon = icon;
628 if (!icon.Ok()) return;
629
630 wxMask *mask = icon.GetMask();
631 GdkBitmap *bm = (GdkBitmap *) NULL;
632 if (mask) bm = mask->GetBitmap();
633
634 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
635 }
636