]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/frame.cpp
* wxCreateDynamicObject() uses an hashtable now
[wxWidgets.git] / src / gtk1 / frame.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: frame.cpp
3// Purpose:
4// Author: Robert Roebling
5// Created: 01/02/97
6// Id:
7// Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
19717c50 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12#pragma implementation "frame.h"
13#endif
14
15#include "wx/frame.h"
16#include "wx/dialog.h"
17#include "wx/control.h"
18#include "wx/app.h"
cf4219e7
RR
19#include "wx/menu.h"
20#include "wx/toolbar.h"
21#include "wx/statusbr.h"
d4c99d6f 22#include "wx/mdi.h"
362c6693 23#include "wx/dcclient.h"
c801d85f
KB
24#include "wx/gtk/win_gtk.h"
25
c67daf87
UR
26const int wxMENU_HEIGHT = 28;
27const int wxSTATUS_HEIGHT = 25;
c801d85f
KB
28
29extern wxList wxTopLevelWindows;
30extern wxList wxPendingDelete;
31
32//-----------------------------------------------------------------------------
33// wxFrame
34//-----------------------------------------------------------------------------
35
36//-----------------------------------------------------------------------------
47908e25 37// set size
c801d85f
KB
38
39void gtk_frame_size_callback( GtkWidget *WXUNUSED(widget), GtkAllocation* alloc, wxFrame *win )
ed7a557b 40{
c801d85f
KB
41 if (!win->HasVMT()) return;
42
43/*
44 printf( "OnFrameResize from " );
45 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
46 printf( win->GetClassInfo()->GetClassName() );
47 printf( ".\n" );
48*/
ed7a557b 49
219f895a 50 win->GtkOnSize( alloc->x, alloc->y, alloc->width, alloc->height );
362c6693 51}
c801d85f
KB
52
53//-----------------------------------------------------------------------------
54// delete
55
56bool gtk_frame_delete_callback( GtkWidget *WXUNUSED(widget), GdkEvent *WXUNUSED(event), wxFrame *win )
ed7a557b 57{
c801d85f
KB
58/*
59 printf( "OnDelete from " );
60 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
61 printf( win->GetClassInfo()->GetClassName() );
62 printf( ".\n" );
63*/
ed7a557b 64
c801d85f
KB
65 win->Close();
66
67 return TRUE;
362c6693 68}
c801d85f 69
47908e25
RR
70//-----------------------------------------------------------------------------
71// configure
72
73gint gtk_frame_configure_callback( GtkWidget *WXUNUSED(widget), GdkEventConfigure *event, wxFrame *win )
74{
75 if (!win->HasVMT()) return FALSE;
76
77 win->m_x = event->x;
78 win->m_y = event->y;
79
80 return FALSE;
362c6693 81}
47908e25 82
c801d85f
KB
83//-----------------------------------------------------------------------------
84
85BEGIN_EVENT_TABLE(wxFrame, wxWindow)
c801d85f 86 EVT_SIZE(wxFrame::OnSize)
716b7364 87 EVT_CLOSE(wxFrame::OnCloseWindow)
e2414cbe 88 EVT_IDLE(wxFrame::OnIdle)
c801d85f
KB
89END_EVENT_TABLE()
90
91IMPLEMENT_DYNAMIC_CLASS(wxFrame,wxWindow)
92
19717c50 93wxFrame::wxFrame()
c801d85f 94{
c67daf87
UR
95 m_frameMenuBar = (wxMenuBar *) NULL;
96 m_frameStatusBar = (wxStatusBar *) NULL;
97 m_frameToolBar = (wxToolBar *) NULL;
c801d85f 98 m_sizeSet = FALSE;
46dc76ba 99 m_addPrivateChild = FALSE;
c67daf87
UR
100 m_wxwindow = (GtkWidget *) NULL;
101 m_mainWindow = (GtkWidget *) NULL;
c801d85f 102 wxTopLevelWindows.Insert( this );
362c6693 103}
c801d85f 104
ed7a557b 105wxFrame::wxFrame( wxWindow *parent, wxWindowID id, const wxString &title,
debe6624
JS
106 const wxPoint &pos, const wxSize &size,
107 long style, const wxString &name )
c801d85f 108{
c67daf87
UR
109 m_frameMenuBar = (wxMenuBar *) NULL;
110 m_frameStatusBar = (wxStatusBar *) NULL;
111 m_frameToolBar = (wxToolBar *) NULL;
c801d85f 112 m_sizeSet = FALSE;
46dc76ba 113 m_addPrivateChild = FALSE;
c67daf87
UR
114 m_wxwindow = (GtkWidget *) NULL;
115 m_mainWindow = (GtkWidget *) NULL;
c801d85f
KB
116 Create( parent, id, title, pos, size, style, name );
117 wxTopLevelWindows.Insert( this );
362c6693 118}
c801d85f 119
debe6624 120bool wxFrame::Create( wxWindow *parent, wxWindowID id, const wxString &title,
c801d85f 121 const wxPoint &pos, const wxSize &size,
debe6624 122 long style, const wxString &name )
c801d85f
KB
123{
124 m_needParent = FALSE;
ed7a557b 125
c801d85f
KB
126 PreCreation( parent, id, pos, size, style, name );
127
c801d85f 128 m_title = title;
ed7a557b 129
c801d85f 130 m_widget = gtk_window_new( GTK_WINDOW_TOPLEVEL );
ed7a557b 131 if ((size.x != -1) && (size.y != -1))
c801d85f 132 gtk_widget_set_usize( m_widget, m_width, m_height );
ed7a557b 133 if ((pos.x != -1) && (pos.y != -1))
c801d85f 134 gtk_widget_set_uposition( m_widget, m_x, m_y );
ed7a557b 135
c801d85f
KB
136 gtk_window_set_title( GTK_WINDOW(m_widget), title );
137 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
ed7a557b 138
c801d85f 139 gtk_widget_set( m_widget, "GtkWindow::allow_shrink", TRUE, NULL);
ed7a557b
VZ
140
141 gtk_signal_connect( GTK_OBJECT(m_widget), "delete_event",
c801d85f 142 GTK_SIGNAL_FUNC(gtk_frame_delete_callback), (gpointer)this );
ed7a557b 143
c801d85f
KB
144 m_mainWindow = gtk_myfixed_new();
145 gtk_widget_show( m_mainWindow );
146 GTK_WIDGET_UNSET_FLAGS( m_mainWindow, GTK_CAN_FOCUS );
ed7a557b 147
c801d85f
KB
148 gtk_container_add( GTK_CONTAINER(m_widget), m_mainWindow );
149 gtk_widget_set_uposition( m_mainWindow, 0, 0 );
ed7a557b 150
c801d85f
KB
151 m_wxwindow = gtk_myfixed_new();
152 gtk_widget_show( m_wxwindow );
153 GTK_WIDGET_UNSET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
ed7a557b 154
c801d85f 155 gtk_container_add( GTK_CONTAINER(m_mainWindow), m_wxwindow );
ed7a557b 156
ed7a557b 157 gtk_signal_connect( GTK_OBJECT(m_widget), "size_allocate",
c801d85f 158 GTK_SIGNAL_FUNC(gtk_frame_size_callback), (gpointer)this );
ed7a557b 159
47908e25
RR
160 gtk_signal_connect( GTK_OBJECT(m_widget), "configure_event",
161 GTK_SIGNAL_FUNC(gtk_frame_configure_callback), (gpointer)this );
162
c801d85f 163 PostCreation();
ed7a557b 164
c801d85f 165 gtk_widget_realize( m_mainWindow );
ed7a557b 166
c801d85f 167 return TRUE;
362c6693 168}
c801d85f 169
19717c50 170wxFrame::~wxFrame()
c801d85f
KB
171{
172 if (m_frameMenuBar) delete m_frameMenuBar;
173 if (m_frameStatusBar) delete m_frameStatusBar;
ff7b1510 174 if (m_frameToolBar) delete m_frameToolBar;
ed7a557b 175
c801d85f
KB
176// if (m_mainWindow) gtk_widget_destroy( m_mainWindow );
177
178 wxTopLevelWindows.DeleteObject( this );
179 if (wxTopLevelWindows.Number() == 0) wxTheApp->ExitMainLoop();
362c6693 180}
ed7a557b 181
debe6624 182bool wxFrame::Show( bool show )
c801d85f
KB
183{
184 if (show)
185 {
186 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
187 m_sizeSet = FALSE;
188 ProcessEvent( event );
362c6693 189 }
c801d85f 190 return wxWindow::Show( show );
362c6693 191}
c801d85f 192
debe6624 193void wxFrame::Enable( bool enable )
c801d85f
KB
194{
195 wxWindow::Enable( enable );
196 gtk_widget_set_sensitive( m_mainWindow, enable );
362c6693 197}
c801d85f 198
716b7364 199void wxFrame::OnCloseWindow( wxCloseEvent &event )
c801d85f 200{
716b7364
RR
201 if ( GetEventHandler()->OnClose() || event.GetForce())
202 {
203 this->Destroy();
204 }
362c6693 205}
c801d85f 206
19717c50 207bool wxFrame::Destroy()
c801d85f
KB
208{
209 if (!wxPendingDelete.Member(this))
210 wxPendingDelete.Append(this);
ed7a557b 211
c801d85f
KB
212 return TRUE;
213}
214
903f689b
RR
215void wxFrame::ImplementSetPosition(void)
216{
217 if ((m_x != -1) || (m_y != -1))
218 gtk_widget_set_uposition( m_widget, m_x, m_y );
219}
220
221void wxFrame::Centre( int direction )
222{
223 if (direction & wxHORIZONTAL == wxHORIZONTAL) m_x = (gdk_screen_width () - m_width) / 2;
224 if (direction & wxVERTICAL == wxVERTICAL) m_y = (gdk_screen_height () - m_height) / 2;
225 ImplementSetPosition();
226}
227
c801d85f
KB
228void wxFrame::GetClientSize( int *width, int *height ) const
229{
230 wxWindow::GetClientSize( width, height );
231 if (height)
232 {
233 if (m_frameMenuBar) (*height) -= wxMENU_HEIGHT;
234 if (m_frameStatusBar) (*height) -= wxSTATUS_HEIGHT;
46dc76ba
RR
235 if (m_frameToolBar)
236 {
237 int y = 0;
c67daf87 238 m_frameToolBar->GetSize( (int *) NULL, &y );
46dc76ba
RR
239 (*height) -= y;
240 }
362c6693
RR
241 }
242}
c801d85f 243
b593568e
RR
244void wxFrame::SetClientSize( int const width, int const height )
245{
246 int h = height;
247 if (m_frameMenuBar) h += wxMENU_HEIGHT;
248 if (m_frameStatusBar) h += wxSTATUS_HEIGHT;
249 if (m_frameToolBar)
250 {
251 int y = 0;
c67daf87 252 m_frameToolBar->GetSize( (int *) NULL, &y );
b593568e
RR
253 h += y;
254 }
255 wxWindow::SetClientSize( width, h );
362c6693 256}
b593568e 257
47908e25 258void wxFrame::GtkOnSize( int WXUNUSED(x), int WXUNUSED(y), int width, int height )
c801d85f 259{
47908e25
RR
260 // due to a bug in gtk, x,y are always 0
261 // m_x = x;
262 // m_y = y;
219f895a 263
c801d85f
KB
264 if ((m_height == height) && (m_width == width) &&
265 (m_sizeSet)) return;
266 if (!m_mainWindow) return;
267 if (!m_wxwindow) return;
268
269 m_width = width;
270 m_height = height;
ed7a557b 271
d553992a
VZ
272 // VZ: why??
273 //gtk_widget_set_usize( m_widget, width, height );
ed7a557b 274
c801d85f
KB
275 int main_x = 0;
276 int main_y = 0;
277 int main_height = height;
278 int main_width = width;
ed7a557b 279
c801d85f
KB
280 // This emulates Windows behaviour:
281 // The menu bar is part of the main window, but the status bar
282 // is on the implementation side in the client area. The
283 // function GetClientSize returns the size of the client area
284 // minus the status bar height. Under wxGTK, the main window
285 // is represented by m_mainWindow. The menubar is inserted
286 // into m_mainWindow whereas the statusbar is insertes into
287 // m_wxwindow just like any other window.
ed7a557b 288
c801d85f 289// not really needed
47908e25 290// gtk_widget_set_usize( m_mainWindow, width, height );
ed7a557b 291
c801d85f
KB
292 if (m_frameMenuBar)
293 {
294 main_y = wxMENU_HEIGHT;
295 main_height -= wxMENU_HEIGHT;
362c6693 296 }
ed7a557b 297
46dc76ba 298 int toolbar_height = 0;
c67daf87 299 if (m_frameToolBar) m_frameToolBar->GetSize( (int *) NULL, &toolbar_height );
46dc76ba
RR
300
301 main_y += toolbar_height;
302 main_height -= toolbar_height;
303
d4c99d6f
RR
304 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_wxwindow, main_x, main_y );
305 gtk_widget_set_usize( m_wxwindow, main_width, main_height );
ed7a557b 306
c801d85f
KB
307 if (m_frameMenuBar)
308 {
d4c99d6f 309 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_frameMenuBar->m_widget, 1, 1 );
c801d85f 310 gtk_widget_set_usize( m_frameMenuBar->m_widget, width-2, wxMENU_HEIGHT-2 );
362c6693 311 }
ed7a557b 312
46dc76ba
RR
313 if (m_frameToolBar)
314 {
d4c99d6f 315 gtk_myfixed_move( GTK_MYFIXED(m_mainWindow), m_frameToolBar->m_widget, 1, wxMENU_HEIGHT );
46dc76ba 316 gtk_widget_set_usize( m_frameToolBar->m_widget, width-2, toolbar_height );
362c6693 317 }
46dc76ba 318
c801d85f
KB
319 if (m_frameStatusBar)
320 {
321 m_frameStatusBar->SetSize( 0, main_height-wxSTATUS_HEIGHT, width, wxSTATUS_HEIGHT );
362c6693 322 }
c801d85f
KB
323
324 m_sizeSet = TRUE;
ed7a557b 325
c801d85f
KB
326 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
327 event.SetEventObject( this );
328 ProcessEvent( event );
362c6693 329}
c801d85f
KB
330
331void wxFrame::OnSize( wxSizeEvent &WXUNUSED(event) )
332{
ed7a557b
VZ
333 if ( GetAutoLayout() )
334 Layout();
335 else {
cf5f9c9c
GL
336 // no child: go out !
337 if (!GetChildren()->First())
338 return;
46dc76ba 339
ed7a557b 340 // do we have exactly one child?
c67daf87 341 wxWindow *child = (wxWindow *) NULL;
ed7a557b 342 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
c801d85f 343 {
ed7a557b 344 wxWindow *win = (wxWindow *)node->Data();
e3e65dac 345 if (!IS_KIND_OF(win,wxFrame) && !IS_KIND_OF(win,wxDialog)
46dc76ba 346#if 0 // not in m_children anyway
ed7a557b 347 && (win != m_frameMenuBar) &&
46dc76ba 348 (win != m_frameToolBar) &&
ed7a557b
VZ
349 (win != m_frameStatusBar)
350#endif
351 )
352 {
353 if ( child ) // it's the second one: do nothing
354 return;
355
356 child = win;
362c6693
RR
357 }
358 }
c801d85f 359
ed7a557b 360 // yes: set it's size to fill all the frame
c801d85f 361 int client_x, client_y;
c801d85f 362 GetClientSize(&client_x, &client_y);
c801d85f
KB
363 child->SetSize( 1, 1, client_x-2, client_y);
364 }
362c6693 365}
c801d85f 366
46dc76ba
RR
367void wxFrame::AddChild( wxWindow *child )
368{
e3e65dac
RR
369 // wxFrame and wxDialog as children aren't placed into the parents
370
d4c99d6f
RR
371 if (IS_KIND_OF(child,wxMDIChildFrame)) printf( "wxFrame::AddChild error.\n" );
372
373 if ( IS_KIND_OF(child,wxFrame) || IS_KIND_OF(child,wxDialog))
e3e65dac
RR
374 {
375 m_children.Append( child );
376
377 if ((child->m_x != -1) && (child->m_y != -1))
378 gtk_widget_set_uposition( child->m_widget, child->m_x, child->m_y );
379
380 return;
381 }
382
46dc76ba
RR
383 if (m_addPrivateChild)
384 {
385 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow), child->m_widget, child->m_x, child->m_y );
386
387 gtk_widget_set_usize( child->m_widget, child->m_width, child->m_height );
388 }
389 else
390 {
391 m_children.Append( child );
392
393 if (m_wxwindow)
394 gtk_myfixed_put( GTK_MYFIXED(m_wxwindow), child->m_widget, child->m_x, child->m_y );
395
396 gtk_widget_set_usize( child->m_widget, child->m_width, child->m_height );
397 }
362c6693 398}
46dc76ba 399
716b7364 400static void SetInvokingWindow( wxMenu *menu, wxWindow *win )
c801d85f
KB
401{
402 menu->SetInvokingWindow( win );
403 wxNode *node = menu->m_items.First();
404 while (node)
405 {
406 wxMenuItem *menuitem = (wxMenuItem*)node->Data();
19717c50
VZ
407 if (menuitem->IsSubMenu())
408 SetInvokingWindow( menuitem->GetSubMenu(), win );
c801d85f 409 node = node->Next();
362c6693
RR
410 }
411}
c801d85f
KB
412
413void wxFrame::SetMenuBar( wxMenuBar *menuBar )
414{
415 m_frameMenuBar = menuBar;
716b7364
RR
416
417 if (m_frameMenuBar)
c801d85f 418 {
30dea054
RR
419 wxNode *node = m_frameMenuBar->m_menus.First();
420 while (node)
421 {
422 wxMenu *menu = (wxMenu*)node->Data();
423 SetInvokingWindow( menu, this );
424 node = node->Next();
362c6693 425 }
30dea054 426
716b7364
RR
427 if (m_frameMenuBar->m_parent != this)
428 {
716b7364
RR
429 m_frameMenuBar->m_parent = this;
430 gtk_myfixed_put( GTK_MYFIXED(m_mainWindow),
431 m_frameMenuBar->m_widget, m_frameMenuBar->m_x, m_frameMenuBar->m_y );
432 }
433 }
362c6693 434}
c801d85f 435
362c6693 436wxMenuBar *wxFrame::GetMenuBar(void) const
46dc76ba
RR
437{
438 return m_frameMenuBar;
362c6693 439}
46dc76ba 440
362c6693 441wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
46dc76ba 442{
362c6693
RR
443 wxCHECK_MSG( m_frameToolBar == NULL, FALSE, "recreating toolbar in wxFrame" );
444
46dc76ba 445 m_addPrivateChild = TRUE;
362c6693 446 m_frameToolBar = OnCreateToolBar( style, id, name );
46dc76ba
RR
447 m_addPrivateChild = FALSE;
448
449 return m_frameToolBar;
362c6693 450}
46dc76ba 451
362c6693 452wxToolBar* wxFrame::OnCreateToolBar( long style, wxWindowID id, const wxString& name )
46dc76ba 453{
362c6693
RR
454 return new wxToolBar( this, id, wxDefaultPosition, wxDefaultSize, style, name );
455}
456
457wxToolBar *wxFrame::GetToolBar(void) const
458{
459 return m_frameToolBar;
460}
46dc76ba 461
e3e65dac 462wxStatusBar* wxFrame::CreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
c801d85f 463{
362c6693 464 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE, "recreating status bar in wxFrame" );
c801d85f 465
362c6693 466 m_frameStatusBar = OnCreateStatusBar( number, style, id, name );
e3e65dac
RR
467
468 return m_frameStatusBar;
362c6693
RR
469}
470
471wxStatusBar *wxFrame::OnCreateStatusBar( int number, long style, wxWindowID id, const wxString& name )
472{
c67daf87 473 wxStatusBar *statusBar = (wxStatusBar *) NULL;
362c6693
RR
474
475 statusBar = new wxStatusBar(this, id, wxPoint(0, 0), wxSize(100, 20), style, name);
476
477 // Set the height according to the font and the border size
478 wxClientDC dc(statusBar);
479 dc.SetFont( *statusBar->GetFont() );
480
481 long x, y;
482 dc.GetTextExtent( "X", &x, &y );
483
484 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
485
486 statusBar->SetSize( -1, -1, 100, height );
487
488 statusBar->SetFieldsCount( number );
489 return statusBar;
490}
c801d85f 491
362c6693 492void wxFrame::SetStatusText(const wxString& text, int number)
c801d85f 493{
362c6693 494 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
c801d85f 495
362c6693
RR
496 m_frameStatusBar->SetStatusText(text, number);
497}
498
499void wxFrame::SetStatusWidths(int n, const int widths_field[] )
c801d85f 500{
362c6693
RR
501 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
502
503 m_frameStatusBar->SetStatusWidths(n, widths_field);
504}
c801d85f 505
362c6693 506wxStatusBar *wxFrame::GetStatusBar(void) const
c801d85f
KB
507{
508 return m_frameStatusBar;
362c6693 509}
c801d85f 510
c801d85f
KB
511void wxFrame::SetTitle( const wxString &title )
512{
513 m_title = title;
362c6693 514 if (m_title.IsNull()) m_title = "";
c801d85f 515 gtk_window_set_title( GTK_WINDOW(m_widget), title );
362c6693 516}
c801d85f 517
d553992a
VZ
518void wxFrame::SetSizeHints(int minW, int minH,
519 int WXUNUSED(maxW), int WXUNUSED(maxH),
edaa81ae 520 int WXUNUSED(incW), int WXUNUSED(incH) )
c801d85f 521{
d553992a
VZ
522 // gdk_window_set_hints alone doesn't really prevent the user from shrinking
523 // the window to the size smaller than (minW, minH)
524 gtk_widget_set_usize( GTK_WIDGET(m_widget), minW, minH );
525
526 /*
c33c4050 527 gdk_window_set_hints( m_widget->window, -1, -1,
d553992a
VZ
528 minW, minH, maxW, maxH,
529 GDK_HINT_MIN_SIZE | GDK_HINT_MAX_SIZE );
530 */
e2414cbe 531}
d355d3fe
RR
532
533void wxFrame::SetIcon( const wxIcon &icon )
534{
535 m_icon = icon;
536 if (!icon.Ok()) return;
537
538 wxMask *mask = icon.GetMask();
c67daf87 539 GdkBitmap *bm = (GdkBitmap *) NULL;
d355d3fe
RR
540 if (mask) bm = mask->GetBitmap();
541
c67daf87 542 gdk_window_set_icon( m_widget->window, (GdkWindow *) NULL, icon.GetPixmap(), bm );
d355d3fe
RR
543}
544