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 #include "wx/dcclient.h"
22 #include "wx/dialog.h"
23 #include "wx/settings.h"
26 #include <wx/mac/uma.h>
28 extern wxList wxModelessWindows
;
29 extern wxList wxPendingDelete
;
31 #if !USE_SHARED_LIBRARY
32 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
33 EVT_SIZE(wxFrame::OnSize
)
34 EVT_ACTIVATE(wxFrame::OnActivate
)
35 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight
)
36 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
37 EVT_IDLE(wxFrame::OnIdle
)
38 EVT_CLOSE(wxFrame::OnCloseWindow
)
41 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
44 #if wxUSE_NATIVE_STATUSBAR
45 bool wxFrame::m_useNativeStatusBar
= TRUE
;
47 bool wxFrame::m_useNativeStatusBar
= FALSE
;
53 m_frameToolBar
= NULL
;
56 // in order to be able to give size events on show
57 m_frameMenuBar
= NULL
;
58 m_frameStatusBar
= NULL
;
60 m_windowParent
= NULL
;
64 bool wxFrame::Create(wxWindow
*parent
,
66 const wxString
& title
,
73 wxTopLevelWindows
.Append(this);
76 m_windowStyle
= style
;
77 m_frameMenuBar
= NULL
;
80 m_frameToolBar
= NULL
;
82 m_frameStatusBar
= NULL
;
84 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
89 m_windowId
= (int)NewControlId();
91 if (parent
) parent
->AddChild(this);
93 wxModelessWindows
.Append(this);
113 m_macWindowData
= new MacWindowData() ;
115 ::SetRect(&theBoundsRect
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
117 WindowClass wclass
= kDocumentWindowClass
;
118 WindowAttributes attr
= kWindowNoAttributes
;
120 if ( ( m_windowStyle
& wxMINIMIZE_BOX
) || ( m_windowStyle
& wxMAXIMIZE_BOX
) )
122 attr
|= kWindowFullZoomAttribute
;
123 attr
|= kWindowResizableAttribute
;
125 if ( m_windowStyle
& wxSTAY_ON_TOP
)
127 wclass
= kFloatingWindowClass
;
129 // if ( m_windowStyle & wxCAPTION )
130 // attr |= kHasPaletteTitlebarMask ;
135 if ( m_windowStyle
& wxSYSTEM_MENU
)
137 attr
|= kWindowCloseBoxAttribute
;
139 UMACreateNewWindow( wclass
, attr
, &theBoundsRect
, &m_macWindowData
->m_macWindow
) ;
140 wxAssociateWinWithMacWindow( m_macWindowData
->m_macWindow
, this ) ;
142 if( wxApp::s_macDefaultEncodingIsPC
)
143 label
= wxMacMakeMacStringFromPC( title
) ;
146 UMASetWTitleC( m_macWindowData
->m_macWindow
, label
) ;
147 UMACreateRootControl( m_macWindowData
->m_macWindow
, &m_macWindowData
->m_macRootControl
) ;
148 m_macWindowData
->m_macWindowBackgroundTheme
= kThemeBrushDocumentWindowBackground
;
149 m_macWindowData
->m_macFocus
= NULL
;
156 wxTopLevelWindows
.DeleteObject(this);
158 if (m_frameStatusBar
)
159 delete m_frameStatusBar
;
161 delete m_frameMenuBar
;
163 /* Check if it's the last top-level window */
165 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
167 wxTheApp
->SetTopWindow(NULL
);
169 if (wxTheApp
->GetExitOnFrameDelete())
171 wxTheApp
->ExitMainLoop() ;
175 wxModelessWindows
.DeleteObject(this);
179 void wxFrame::Iconize(bool iconize
)
184 // Equivalent to maximize/restore in Windows
185 void wxFrame::Maximize(bool maximize
)
190 bool wxFrame::IsIconized() const
196 // Is the frame maximized?
197 bool wxFrame::IsMaximized(void) const
203 void wxFrame::SetIcon(const wxIcon
& icon
)
209 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
210 const wxString
& name
)
212 wxStatusBar
*statusBar
= NULL
;
214 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 17),
217 // Set the height according to the font and the border size
218 // we shouldn't do this on the mac, because we have to fit the grow box
220 wxClientDC dc(statusBar);
221 dc.SetFont(statusBar->GetFont());
224 dc.GetTextExtent("X", &x, &y);
226 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
228 statusBar->SetSize(-1, -1, 100, height);
232 statusBar
->SetFieldsCount(number
);
236 wxStatusBar
* wxFrame::CreateStatusBar(int number
, long style
, wxWindowID id
,
237 const wxString
& name
)
239 // Calling CreateStatusBar twice is an error.
240 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
,
241 "recreating status bar in wxFrame" );
243 m_frameStatusBar
= OnCreateStatusBar(number
, style
, id
,
245 if ( m_frameStatusBar
)
248 return m_frameStatusBar
;
254 void wxFrame::SetStatusText(const wxString
& text
, int number
)
256 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
258 m_frameStatusBar
->SetStatusText(text
, number
);
261 void wxFrame::SetStatusWidths(int n
, const int widths_field
[])
263 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
265 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
269 void wxFrame::PositionStatusBar()
271 if (m_frameStatusBar
)
274 GetClientSize(&w
, &h
);
276 m_frameStatusBar
->GetSize(&sw
, &sh
);
278 // Since we wish the status bar to be directly under the client area,
279 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
280 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
284 void wxFrame::SetMenuBar(wxMenuBar
*menuBar
)
288 m_frameMenuBar
= NULL
;
292 m_frameMenuBar
= menuBar
;
293 // TODO : we move this into the app code
294 m_frameMenuBar
->MacInstallMenuBar() ;
299 // Work out max. size
300 wxNode
*node
= GetChildren().First();
305 // Find a child that's a subwindow, but not a dialog box.
306 wxWindow
*win
= (wxWindow
*)node
->Data();
308 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) &&
309 !win
->IsKindOf(CLASSINFO(wxDialog
)))
313 win
->GetSize(&width
, &height
);
314 win
->GetPosition(&x
, &y
);
316 if ((x
+ width
) > max_width
)
317 max_width
= x
+ width
;
318 if ((y
+ height
) > max_height
)
319 max_height
= y
+ height
;
323 SetClientSize(max_width
, max_height
);
326 // Responds to colour changes, and passes event on to children.
327 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
329 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
332 if ( m_frameStatusBar
)
334 wxSysColourChangedEvent event2
;
335 event2
.SetEventObject( m_frameStatusBar
);
336 m_frameStatusBar
->ProcessEvent(event2
);
339 // Propagate the event to the non-top-level children
340 wxWindow::OnSysColourChanged(event
);
343 // Default resizing behaviour - if only ONE subwindow,
344 // resize to client rectangle size
345 void wxFrame::OnSize(wxSizeEvent
& event
)
347 // if we're using constraints - do use them
348 #if wxUSE_CONSTRAINTS
349 if ( GetAutoLayout() ) {
355 // do we have _exactly_ one child?
356 wxWindow
*child
= NULL
;
357 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
359 wxWindow
*win
= (wxWindow
*)node
->Data();
360 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
361 !win
->IsKindOf(CLASSINFO(wxDialog
)) &&
362 (win
!= GetStatusBar())
365 (win
!= GetToolBar())
370 return; // it's our second subwindow - nothing to do
376 // we have exactly one child - set it's size to fill the whole frame
377 int clientW
, clientH
;
378 GetClientSize(&clientW
, &clientH
);
383 child
->SetSize(x
, y
, clientW
, clientH
);
387 // Default activation behaviour - set the focus for the first child
389 void wxFrame::OnActivate(wxActivateEvent
& event
)
391 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
393 // Find a child that's a subwindow, but not a dialog box.
394 wxWindow
*child
= (wxWindow
*)node
->Data();
395 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
396 !child
->IsKindOf(CLASSINFO(wxDialog
)))
404 // The default implementation for the close window event.
405 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
410 // Destroy the window (delayed, if a managed window)
411 bool wxFrame::Destroy()
413 if (!wxPendingDelete
.Member(this))
414 wxPendingDelete
.Append(this);
418 // Default menu selection behaviour - display a help string
419 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
423 if (event
.GetMenuId() == -1)
427 wxMenuBar
*menuBar
= GetMenuBar();
430 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
431 if (helpString
!= "")
432 SetStatusText(helpString
);
438 wxMenuBar
*wxFrame::GetMenuBar() const
440 return m_frameMenuBar
;
444 // Call this to simulate a menu command
445 void wxFrame::Command(int id
)
450 void wxFrame::ProcessCommand(int id
)
452 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
453 commandEvent
.SetInt( id
);
454 commandEvent
.SetEventObject( this );
456 wxMenuBar
*bar
= GetMenuBar() ;
460 /* TODO: check the menu item if required
461 wxMenuItem *item = bar->FindItemForId(id) ;
462 if (item && item->IsCheckable())
464 bar->Check(id,!bar->Checked(id)) ;
468 GetEventHandler()->ProcessEvent(commandEvent
);
471 // Checks if there is a toolbar, and returns the first free client position
472 wxPoint
wxFrame::GetClientAreaOrigin() const
479 GetToolBar()->GetSize(& w
, & h
);
481 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
494 void wxFrame::GetClientSize(int *x
, int *y
) const
496 wxWindow::GetClientSize( x
, y
) ;
498 if ( GetStatusBar() )
500 int statusX
, statusY
;
501 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
505 wxPoint
pt(GetClientAreaOrigin());
510 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
512 int currentclientwidth
, currentclientheight
;
513 int currentwidth
, currentheight
;
515 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
516 GetSize( ¤twidth
, ¤theight
) ;
518 // find the current client size
520 // Find the difference between the entire window (title bar and all)
521 // and the client area; add this to the new client size to move the
524 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
525 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
530 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
532 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
,
533 "recreating toolbar in wxFrame" );
535 wxToolBar
* toolBar
= OnCreateToolBar(style
, id
, name
);
548 wxToolBar
* wxFrame::OnCreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
550 return new wxToolBar(this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
553 void wxFrame::PositionToolBar()
557 // TODO: we actually need to use the low-level client size, before
558 // the toolbar/status bar were added.
559 // So DEFINITELY replace the line below with something appropriate.
561 // GetClientSize(& cw, &ch);
566 if ( GetStatusBar() )
568 int statusX
, statusY
;
569 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
576 GetToolBar()->GetSize(& tw
, & th
);
578 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
580 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
581 // means, pretend we don't have toolbar/status bar, so we
582 // have the original client size.
583 GetToolBar()->SetSize(0, 0, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
587 // Use the 'real' position
588 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);