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