1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "frame.h"
17 #include "wx/statusbr.h"
18 #include "wx/toolbar.h"
19 #include "wx/menuitem.h"
21 extern wxList wxModelessWindows
;
22 extern wxList wxPendingDelete
;
24 #if !USE_SHARED_LIBRARY
25 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
26 EVT_SIZE(wxFrame::OnSize
)
27 EVT_ACTIVATE(wxFrame::OnActivate
)
28 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight
)
29 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
30 EVT_IDLE(wxFrame::OnIdle
)
31 EVT_CLOSE(wxFrame::OnCloseWindow
)
34 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
37 #if wxUSE_NATIVE_STATUSBAR
38 bool wxFrame::m_useNativeStatusBar
= TRUE
;
40 bool wxFrame::m_useNativeStatusBar
= FALSE
;
45 m_frameToolBar
= NULL
;
46 m_frameMenuBar
= NULL
;
47 m_frameStatusBar
= NULL
;
49 m_windowParent
= NULL
;
53 bool wxFrame::Create(wxWindow
*parent
,
55 const wxString
& title
,
62 wxTopLevelWindows
.Append(this);
65 m_windowStyle
= style
;
66 m_frameMenuBar
= NULL
;
67 m_frameToolBar
= NULL
;
68 m_frameStatusBar
= NULL
;
70 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
75 m_windowId
= (int)NewControlId();
77 if (parent
) parent
->AddChild(this);
79 wxModelessWindows
.Append(this);
81 // TODO: create frame.
88 wxTopLevelWindows
.DeleteObject(this);
91 delete m_frameStatusBar
;
93 delete m_frameMenuBar
;
95 /* Check if it's the last top-level window */
97 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
99 wxTheApp
->SetTopWindow(NULL
);
101 if (wxTheApp
->GetExitOnFrameDelete())
103 // TODO signal to the app that we're going to close
107 wxModelessWindows
.DeleteObject(this);
110 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
111 void wxFrame::GetClientSize(int *x
, int *y
) const
116 // Set the client size (i.e. leave the calculation of borders etc.
118 void wxFrame::SetClientSize(int width
, int height
)
123 void wxFrame::GetSize(int *width
, int *height
) const
128 void wxFrame::GetPosition(int *x
, int *y
) const
133 void wxFrame::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
138 bool wxFrame::Show(bool show
)
144 void wxFrame::Iconize(bool iconize
)
149 // Equivalent to maximize/restore in Windows
150 void wxFrame::Maximize(bool maximize
)
155 bool wxFrame::IsIconized() const
161 void wxFrame::SetTitle(const wxString
& title
)
166 wxString
wxFrame::GetTitle() const
172 void wxFrame::SetIcon(const wxIcon
& icon
)
178 void wxFrame::SetAcceleratorTable(const wxAcceleratorTable
& accel
)
180 m_acceleratorTable
= accel
;
183 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
184 const wxString
& name
)
186 wxStatusBar
*statusBar
= NULL
;
188 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 20),
191 // Set the height according to the font and the border size
192 wxClientDC
dc(statusBar
);
193 dc
.SetFont(* statusBar
->GetFont());
196 dc
.GetTextExtent("X", &x
, &y
);
198 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
200 statusBar
->SetSize(-1, -1, 100, height
);
202 statusBar
->SetFieldsCount(number
);
206 wxStatusBar
* wxFrame::CreateStatusBar(int number
, long style
, wxWindowID id
,
207 const wxString
& name
)
209 // Calling CreateStatusBar twice is an error.
210 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
,
211 "recreating status bar in wxFrame" );
213 m_frameStatusBar
= OnCreateStatusBar(number
, style
, id
,
215 if ( m_frameStatusBar
)
218 return m_frameStatusBar
;
224 void wxFrame::SetStatusText(const wxString
& text
, int number
)
226 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
228 m_frameStatusBar
->SetStatusText(text
, number
);
231 void wxFrame::SetStatusWidths(int n
, const int widths_field
[])
233 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
235 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
239 void wxFrame::PositionStatusBar()
242 GetClientSize(&w
, &h
);
244 m_frameStatusBar
->GetSize(&sw
, &sh
);
246 // Since we wish the status bar to be directly under the client area,
247 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
248 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
251 void wxFrame::SetMenuBar(wxMenuBar
*menuBar
)
255 m_frameMenuBar
= NULL
;
259 m_frameMenuBar
= menuBar
;
266 // Work out max. size
267 wxNode
*node
= GetChildren()->First();
272 // Find a child that's a subwindow, but not a dialog box.
273 wxWindow
*win
= (wxWindow
*)node
->Data();
275 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) &&
276 !win
->IsKindOf(CLASSINFO(wxDialog
)))
280 win
->GetSize(&width
, &height
);
281 win
->GetPosition(&x
, &y
);
283 if ((x
+ width
) > max_width
)
284 max_width
= x
+ width
;
285 if ((y
+ height
) > max_height
)
286 max_height
= y
+ height
;
290 SetClientSize(max_width
, max_height
);
293 // Responds to colour changes, and passes event on to children.
294 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
296 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
299 if ( m_frameStatusBar
)
301 wxSysColourChangedEvent event2
;
302 event2
.SetEventObject( m_frameStatusBar
);
303 m_frameStatusBar
->ProcessEvent(event2
);
306 // Propagate the event to the non-top-level children
307 wxWindow::OnSysColourChanged(event
);
310 // Default resizing behaviour - if only ONE subwindow,
311 // resize to client rectangle size
312 void wxFrame::OnSize(wxSizeEvent
& event
)
314 // if we're using constraints - do use them
315 #if wxUSE_CONSTRAINTS
316 if ( GetAutoLayout() ) {
322 // do we have _exactly_ one child?
323 wxWindow
*child
= NULL
;
324 for ( wxNode
*node
= GetChildren()->First(); node
; node
= node
->Next() )
326 wxWindow
*win
= (wxWindow
*)node
->Data();
327 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
328 !win
->IsKindOf(CLASSINFO(wxDialog
)) &&
329 (win
!= GetStatusBar()) &&
330 (win
!= GetToolBar()) )
333 return; // it's our second subwindow - nothing to do
339 // we have exactly one child - set it's size to fill the whole frame
340 int clientW
, clientH
;
341 GetClientSize(&clientW
, &clientH
);
346 child
->SetSize(x
, y
, clientW
, clientH
);
350 // Default activation behaviour - set the focus for the first child
352 void wxFrame::OnActivate(wxActivateEvent
& event
)
354 for(wxNode
*node
= GetChildren()->First(); node
; node
= node
->Next())
356 // Find a child that's a subwindow, but not a dialog box.
357 wxWindow
*child
= (wxWindow
*)node
->Data();
358 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
359 !child
->IsKindOf(CLASSINFO(wxDialog
)))
362 wxDebugMsg("wxFrame::OnActivate: about to set the child's focus.\n");
370 // The default implementation for the close window event - calls
371 // OnClose for backward compatibility.
373 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
376 if ( GetEventHandler()->OnClose() || event
.GetForce())
382 bool wxFrame::OnClose()
387 // Destroy the window (delayed, if a managed window)
388 bool wxFrame::Destroy()
390 if (!wxPendingDelete
.Member(this))
391 wxPendingDelete
.Append(this);
395 // Default menu selection behaviour - display a help string
396 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
400 if (event
.GetMenuId() == -1)
404 wxMenuBar
*menuBar
= GetMenuBar();
407 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
408 if (helpString
!= "")
409 SetStatusText(helpString
);
415 wxMenuBar
*wxFrame::GetMenuBar() const
417 return m_frameMenuBar
;
420 void wxFrame::Centre(int direction
)
422 int display_width
, display_height
, width
, height
, x
, y
;
423 wxDisplaySize(&display_width
, &display_height
);
425 GetSize(&width
, &height
);
428 if (direction
& wxHORIZONTAL
)
429 x
= (int)((display_width
- width
)/2);
430 if (direction
& wxVERTICAL
)
431 y
= (int)((display_height
- height
)/2);
433 SetSize(x
, y
, width
, height
);
436 // Call this to simulate a menu command
437 void wxFrame::Command(int id
)
442 void wxFrame::ProcessCommand(int id
)
444 wxCommandEvent
commandEvent(wxEVENT_TYPE_MENU_COMMAND
, id
);
445 commandEvent
.SetInt( id
);
446 commandEvent
.SetEventObject( this );
448 wxMenuBar
*bar
= GetMenuBar() ;
452 /* TODO: check the menu item if required
453 wxMenuItem *item = bar->FindItemForId(id) ;
454 if (item && item->IsCheckable())
456 bar->Check(id,!bar->Checked(id)) ;
460 GetEventHandler()->ProcessEvent(commandEvent
);
463 // Checks if there is a toolbar, and returns the first free client position
464 wxPoint
wxFrame::GetClientAreaOrigin() const
470 GetToolBar()->GetSize(& w
, & h
);
472 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
484 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
486 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
,
487 "recreating toolbar in wxFrame" );
489 wxToolBar
* toolBar
= OnCreateToolBar(style
, id
, name
);
502 wxToolBar
* wxFrame::OnCreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
504 return new wxToolBar(this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
507 void wxFrame::PositionToolBar()
511 // TODO: we actually need to use the low-level client size, before
512 // the toolbar/status bar were added.
513 // So DEFINITELY replace the line below with something appropriate.
515 wxCHECK_MSG( TRUE
, FALSE
,
516 "PositionToolBar not implemented properly, see frame.cpp" );
518 GetClientSize(& cw
, &ch
);
520 if ( GetStatusBar() )
522 int statusX
, statusY
;
523 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
530 GetToolBar()->GetSize(& tw
, & th
);
532 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
534 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
535 // means, pretend we don't have toolbar/status bar, so we
536 // have the original client size.
537 GetToolBar()->SetSize(0, 0, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
541 // Use the 'real' position
542 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);