1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "frame.h"
15 #include "wx/dialog.h"
16 #include "wx/control.h"
19 #include "wx/toolbar.h"
20 #include "wx/statusbr.h"
21 #include "wx/dcclient.h"
22 #include "wx/gtk/win_gtk.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 const int wxMENU_HEIGHT
= 27;
29 const int wxSTATUS_HEIGHT
= 25;
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern wxList wxTopLevelWindows
;
36 extern wxList wxPendingDelete
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static void gtk_frame_size_callback( GtkWidget
*WXUNUSED(widget
), GtkAllocation
* alloc
, wxFrame
*win
)
44 if (!win
->HasVMT()) return;
47 printf( "OnFrameResize from " );
48 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
49 printf( win->GetClassInfo()->GetClassName() );
53 if ((win
->m_width
!= alloc
->width
) || (win
->m_height
!= alloc
->height
))
55 win
->m_sizeSet
= FALSE
;
56 win
->m_width
= alloc
->width
;
57 win
->m_height
= alloc
->height
;
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 static gint
gtk_frame_delete_callback( GtkWidget
*WXUNUSED(widget
), GdkEvent
*WXUNUSED(event
), wxFrame
*win
)
68 printf( "OnDelete from " );
69 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
70 printf( win->GetClassInfo()->GetClassName() );
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
83 static gint
gtk_frame_configure_callback( GtkWidget
*WXUNUSED(widget
), GdkEventConfigure
*event
, wxFrame
*win
)
85 if (!win
->HasVMT()) return FALSE
;
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
98 EVT_SIZE(wxFrame::OnSize
)
99 EVT_CLOSE(wxFrame::OnCloseWindow
)
100 EVT_IDLE(wxFrame::OnIdle
)
103 IMPLEMENT_DYNAMIC_CLASS(wxFrame
,wxWindow
)
107 m_frameMenuBar
= (wxMenuBar
*) NULL
;
108 m_frameStatusBar
= (wxStatusBar
*) NULL
;
109 m_frameToolBar
= (wxToolBar
*) NULL
;
115 wxFrame::wxFrame( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
116 const wxPoint
&pos
, const wxSize
&size
,
117 long style
, const wxString
&name
)
119 m_frameMenuBar
= (wxMenuBar
*) NULL
;
120 m_frameStatusBar
= (wxStatusBar
*) NULL
;
121 m_frameToolBar
= (wxToolBar
*) NULL
;
125 Create( parent
, id
, title
, pos
, size
, style
, name
);
128 bool wxFrame::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&title
,
129 const wxPoint
&pos
, const wxSize
&size
,
130 long style
, const wxString
&name
)
132 wxTopLevelWindows
.Append( this );
134 m_needParent
= FALSE
;
136 PreCreation( parent
, id
, pos
, size
, style
, name
);
140 GtkWindowType win_type
= GTK_WINDOW_TOPLEVEL
;
141 if (style
& wxSIMPLE_BORDER
) win_type
= GTK_WINDOW_POPUP
;
143 m_widget
= gtk_window_new( win_type
);
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
);
150 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
151 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_FOCUS
);
153 gtk_widget_set( m_widget
, "GtkWindow::allow_shrink", TRUE
, NULL
);
155 gtk_signal_connect( GTK_OBJECT(m_widget
), "delete_event",
156 GTK_SIGNAL_FUNC(gtk_frame_delete_callback
), (gpointer
)this );
158 m_wxwindow
= gtk_myfixed_new();
159 gtk_widget_show( m_wxwindow
);
160 GTK_WIDGET_UNSET_FLAGS( m_wxwindow
, GTK_CAN_FOCUS
);
162 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
164 gtk_signal_connect( GTK_OBJECT(m_widget
), "size_allocate",
165 GTK_SIGNAL_FUNC(gtk_frame_size_callback
), (gpointer
)this );
167 gtk_signal_connect( GTK_OBJECT(m_widget
), "configure_event",
168 GTK_SIGNAL_FUNC(gtk_frame_configure_callback
), (gpointer
)this );
170 if (m_parent
) m_parent
->AddChild( this );
179 if (m_frameMenuBar
) delete m_frameMenuBar
;
180 if (m_frameStatusBar
) delete m_frameStatusBar
;
181 if (m_frameToolBar
) delete m_frameToolBar
;
183 wxTopLevelWindows
.DeleteObject( this );
184 if (wxTopLevelWindows
.Number() == 0) wxTheApp
->ExitMainLoop();
187 bool wxFrame::Show( bool show
)
189 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
191 if (show
&& !m_sizeSet
)
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.
198 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
201 return wxWindow::Show( show
);
204 void wxFrame::OnCloseWindow( wxCloseEvent
&event
)
206 if (GetEventHandler()->OnClose() || event
.GetForce()) this->Destroy();
209 bool wxFrame::Destroy()
211 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
213 if (!wxPendingDelete
.Member(this)) wxPendingDelete
.Append(this);
218 wxPoint
wxFrame::GetClientAreaOrigin() const
220 wxPoint
pt( m_miniEdge
, m_miniEdge
+ m_miniTitle
);
224 m_frameMenuBar
->GetSize( (int*)NULL
, &h
);
230 m_frameToolBar
->GetSize( (int*)NULL
, &h
);
236 void wxFrame::SetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
238 wxASSERT_MSG( (m_widget
!= NULL
), "invalid window" );
240 // Don't do anything for children of wxMDIChildFrame
241 if (!m_wxwindow
) return;
243 if (m_resizing
) return; // I don't like recursions
248 int old_width
= m_width
;
249 int old_height
= m_height
;
251 if ((sizeFlags
& wxSIZE_USE_EXISTING
) == wxSIZE_USE_EXISTING
)
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
;
266 if ((sizeFlags
& wxSIZE_AUTO_WIDTH
) == wxSIZE_AUTO_WIDTH
)
268 if (width
== -1) m_width
= 80;
271 if ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) == wxSIZE_AUTO_HEIGHT
)
273 if (height
== -1) m_height
= 26;
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
;
281 if ((m_x
!= -1) || (m_y
!= -1))
283 if ((m_x
!= old_x
) || (m_y
!= old_y
))
284 gtk_widget_set_uposition( m_widget
, m_x
, m_y
);
287 if ((m_width
!= old_width
) || (m_height
!= old_height
))
289 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
292 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
293 event
.SetEventObject( this );
294 GetEventHandler()->ProcessEvent( event
);
299 void wxFrame::SetSize( int width
, int height
)
301 SetSize( -1, -1, width
, height
, wxSIZE_USE_EXISTING
);
304 void wxFrame::Centre( int direction
)
306 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
311 if (direction
& wxHORIZONTAL
== wxHORIZONTAL
) x
= (gdk_screen_width () - m_width
) / 2;
312 if (direction
& wxVERTICAL
== wxVERTICAL
) y
= (gdk_screen_height () - m_height
) / 2;
317 void wxFrame::GetClientSize( int *width
, int *height
) const
319 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
321 wxWindow::GetClientSize( width
, height
);
324 if (m_frameMenuBar
) (*height
) -= wxMENU_HEIGHT
;
325 if (m_frameStatusBar
) (*height
) -= wxSTATUS_HEIGHT
;
329 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
332 (*height
) -= m_miniEdge
*2 + m_miniTitle
;
336 (*width
) -= m_miniEdge
*2;
340 void wxFrame::SetClientSize( int const width
, int const height
)
342 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
345 if (m_frameMenuBar
) h
+= wxMENU_HEIGHT
;
346 if (m_frameStatusBar
) h
+= wxSTATUS_HEIGHT
;
350 m_frameToolBar
->GetSize( (int *) NULL
, &y
);
353 wxWindow::SetClientSize( width
+ m_miniEdge
*2, h
+ m_miniEdge
*2 + m_miniTitle
);
356 void wxFrame::GtkOnSize( int WXUNUSED(x
), int WXUNUSED(y
), int width
, int height
)
358 // due to a bug in gtk, x,y are always 0
362 if (m_resizing
) return;
365 if (!m_wxwindow
) return;
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
;
375 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
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.
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
;
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
);
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
;
405 m_frameToolBar
->m_x
= xx
;
406 m_frameToolBar
->m_y
= yy
;
407 m_frameToolBar
->m_height
= hh
;
408 m_frameToolBar
->m_width
= ww
;
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
);
414 if (m_frameStatusBar
)
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
;
421 m_frameStatusBar
->m_x
= xx
;
422 m_frameStatusBar
->m_y
= yy
;
423 m_frameStatusBar
->m_width
= ww
;
424 m_frameStatusBar
->m_height
= hh
;
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
);
432 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
433 event
.SetEventObject( this );
434 GetEventHandler()->ProcessEvent( event
);
439 void wxFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
) )
442 GtkOnSize( m_x
, m_y
, m_width
, m_height
);
447 void wxFrame::OnSize( wxSizeEvent
&WXUNUSED(event
) )
449 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
457 // no child: go out !
458 if (!GetChildren().First()) return;
460 // do we have exactly one child?
461 wxWindow
*child
= (wxWindow
*) NULL
;
462 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
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
)
473 // it's the second one: do nothing
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 );
486 static void SetInvokingWindow( wxMenu
*menu
, wxWindow
*win
)
488 menu
->SetInvokingWindow( win
);
489 wxNode
*node
= menu
->m_items
.First();
492 wxMenuItem
*menuitem
= (wxMenuItem
*)node
->Data();
493 if (menuitem
->IsSubMenu())
494 SetInvokingWindow( menuitem
->GetSubMenu(), win
);
499 void wxFrame::SetMenuBar( wxMenuBar
*menuBar
)
501 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
502 wxASSERT_MSG( (m_wxwindow
!= NULL
), "invalid frame" );
504 m_frameMenuBar
= menuBar
;
508 wxNode
*node
= m_frameMenuBar
->m_menus
.First();
511 wxMenu
*menu
= (wxMenu
*)node
->Data();
512 SetInvokingWindow( menu
, this );
516 if (m_frameMenuBar
->m_parent
!= this)
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
);
524 if (m_sizeSet
) GtkOnSize( m_x
, m_y
, m_width
, m_height
);
527 wxMenuBar
*wxFrame::GetMenuBar() const
529 return m_frameMenuBar
;
532 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
534 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
536 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
, "recreating toolbar in wxFrame" );
538 m_frameToolBar
= OnCreateToolBar( style
, id
, name
);
540 GetChildren().DeleteObject( m_frameToolBar
);
542 if (m_sizeSet
) GtkOnSize( m_x
, m_y
, m_width
, m_height
);
544 return m_frameToolBar
;
547 wxToolBar
* wxFrame::OnCreateToolBar( long style
, wxWindowID id
, const wxString
& name
)
549 return new wxToolBar( this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
552 wxToolBar
*wxFrame::GetToolBar() const
554 return m_frameToolBar
;
557 wxStatusBar
* wxFrame::CreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
559 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
561 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
, "recreating status bar in wxFrame" );
563 m_frameStatusBar
= OnCreateStatusBar( number
, style
, id
, name
);
565 if (m_sizeSet
) GtkOnSize( m_x
, m_y
, m_width
, m_height
);
567 return m_frameStatusBar
;
570 wxStatusBar
*wxFrame::OnCreateStatusBar( int number
, long style
, wxWindowID id
, const wxString
& name
)
572 wxStatusBar
*statusBar
= (wxStatusBar
*) NULL
;
574 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20), style
, name
);
576 // Set the height according to the font and the border size
577 wxClientDC
dc(statusBar
);
578 dc
.SetFont( statusBar
->GetFont() );
581 dc
.GetTextExtent( "X", &x
, &y
);
583 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
585 statusBar
->SetSize( -1, -1, 100, height
);
587 statusBar
->SetFieldsCount( number
);
591 void wxFrame::SetStatusText(const wxString
& text
, int number
)
593 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
595 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
597 m_frameStatusBar
->SetStatusText(text
, number
);
600 void wxFrame::SetStatusWidths(int n
, const int widths_field
[] )
602 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
604 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
606 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
609 wxStatusBar
*wxFrame::GetStatusBar() const
611 return m_frameStatusBar
;
614 void wxFrame::SetTitle( const wxString
&title
)
616 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
619 if (m_title
.IsNull()) m_title
= "";
620 gtk_window_set_title( GTK_WINDOW(m_widget
), title
);
623 void wxFrame::SetIcon( const wxIcon
&icon
)
625 wxASSERT_MSG( (m_widget
!= NULL
), "invalid frame" );
628 if (!icon
.Ok()) return;
630 wxMask
*mask
= icon
.GetMask();
631 GdkBitmap
*bm
= (GdkBitmap
*) NULL
;
632 if (mask
) bm
= mask
->GetBitmap();
634 gdk_window_set_icon( m_widget
->window
, (GdkWindow
*) NULL
, icon
.GetPixmap(), bm
);