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
;
63 bool wxFrame::Create(wxWindow
*parent
,
65 const wxString
& title
,
72 wxTopLevelWindows
.Append(this);
75 m_windowStyle
= style
;
76 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
;
155 wxTopLevelWindows
.DeleteObject(this);
157 if (m_frameStatusBar
)
158 delete m_frameStatusBar
;
160 delete m_frameMenuBar
;
162 /* Check if it's the last top-level window */
164 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
166 wxTheApp
->SetTopWindow(NULL
);
168 if (wxTheApp
->GetExitOnFrameDelete())
170 wxTheApp
->ExitMainLoop() ;
174 wxModelessWindows
.DeleteObject(this);
178 void wxFrame::Iconize(bool iconize
)
183 // Equivalent to maximize/restore in Windows
184 void wxFrame::Maximize(bool maximize
)
189 bool wxFrame::IsIconized() const
195 // Is the frame maximized?
196 bool wxFrame::IsMaximized(void) const
202 void wxFrame::SetIcon(const wxIcon
& icon
)
208 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
, long style
, wxWindowID id
,
209 const wxString
& name
)
211 wxStatusBar
*statusBar
= NULL
;
213 statusBar
= new wxStatusBar(this, id
, wxPoint(0, 0), wxSize(100, 17),
216 // Set the height according to the font and the border size
217 // we shouldn't do this on the mac, because we have to fit the grow box
219 wxClientDC dc(statusBar);
220 dc.SetFont(statusBar->GetFont());
223 dc.GetTextExtent("X", &x, &y);
225 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
227 statusBar->SetSize(-1, -1, 100, height);
231 statusBar
->SetFieldsCount(number
);
235 wxStatusBar
* wxFrame::CreateStatusBar(int number
, long style
, wxWindowID id
,
236 const wxString
& name
)
238 // Calling CreateStatusBar twice is an error.
239 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
,
240 "recreating status bar in wxFrame" );
242 m_frameStatusBar
= OnCreateStatusBar(number
, style
, id
,
244 if ( m_frameStatusBar
)
247 return m_frameStatusBar
;
253 void wxFrame::SetStatusText(const wxString
& text
, int number
)
255 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
257 m_frameStatusBar
->SetStatusText(text
, number
);
260 void wxFrame::SetStatusWidths(int n
, const int widths_field
[])
262 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
264 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
268 void wxFrame::PositionStatusBar()
270 if (m_frameStatusBar
)
273 GetClientSize(&w
, &h
);
275 m_frameStatusBar
->GetSize(&sw
, &sh
);
277 // Since we wish the status bar to be directly under the client area,
278 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
279 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
283 void wxFrame::SetMenuBar(wxMenuBar
*menuBar
)
287 m_frameMenuBar
= NULL
;
291 m_frameMenuBar
= menuBar
;
292 // TODO : we move this into the app code
293 m_frameMenuBar
->MacInstallMenuBar() ;
298 // Work out max. size
299 wxNode
*node
= GetChildren().First();
304 // Find a child that's a subwindow, but not a dialog box.
305 wxWindow
*win
= (wxWindow
*)node
->Data();
307 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) &&
308 !win
->IsKindOf(CLASSINFO(wxDialog
)))
312 win
->GetSize(&width
, &height
);
313 win
->GetPosition(&x
, &y
);
315 if ((x
+ width
) > max_width
)
316 max_width
= x
+ width
;
317 if ((y
+ height
) > max_height
)
318 max_height
= y
+ height
;
322 SetClientSize(max_width
, max_height
);
325 // Responds to colour changes, and passes event on to children.
326 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
328 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
331 if ( m_frameStatusBar
)
333 wxSysColourChangedEvent event2
;
334 event2
.SetEventObject( m_frameStatusBar
);
335 m_frameStatusBar
->ProcessEvent(event2
);
338 // Propagate the event to the non-top-level children
339 wxWindow::OnSysColourChanged(event
);
342 // Default resizing behaviour - if only ONE subwindow,
343 // resize to client rectangle size
344 void wxFrame::OnSize(wxSizeEvent
& event
)
346 // if we're using constraints - do use them
347 #if wxUSE_CONSTRAINTS
348 if ( GetAutoLayout() ) {
354 // do we have _exactly_ one child?
355 wxWindow
*child
= NULL
;
356 for ( wxNode
*node
= GetChildren().First(); node
; node
= node
->Next() )
358 wxWindow
*win
= (wxWindow
*)node
->Data();
359 if ( !win
->IsKindOf(CLASSINFO(wxFrame
)) &&
360 !win
->IsKindOf(CLASSINFO(wxDialog
)) &&
361 (win
!= GetStatusBar())
364 (win
!= GetToolBar())
369 return; // it's our second subwindow - nothing to do
375 // we have exactly one child - set it's size to fill the whole frame
376 int clientW
, clientH
;
377 GetClientSize(&clientW
, &clientH
);
382 child
->SetSize(x
, y
, clientW
, clientH
);
386 // Default activation behaviour - set the focus for the first child
388 void wxFrame::OnActivate(wxActivateEvent
& event
)
390 for(wxNode
*node
= GetChildren().First(); node
; node
= node
->Next())
392 // Find a child that's a subwindow, but not a dialog box.
393 wxWindow
*child
= (wxWindow
*)node
->Data();
394 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
395 !child
->IsKindOf(CLASSINFO(wxDialog
)))
403 // The default implementation for the close window event.
404 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
409 // Destroy the window (delayed, if a managed window)
410 bool wxFrame::Destroy()
412 if (!wxPendingDelete
.Member(this))
413 wxPendingDelete
.Append(this);
417 // Default menu selection behaviour - display a help string
418 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
422 if (event
.GetMenuId() == -1)
426 wxMenuBar
*menuBar
= GetMenuBar();
429 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
430 if (helpString
!= "")
431 SetStatusText(helpString
);
437 wxMenuBar
*wxFrame::GetMenuBar() const
439 return m_frameMenuBar
;
443 // Call this to simulate a menu command
444 void wxFrame::Command(int id
)
449 void wxFrame::ProcessCommand(int id
)
451 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, id
);
452 commandEvent
.SetInt( id
);
453 commandEvent
.SetEventObject( this );
455 wxMenuBar
*bar
= GetMenuBar() ;
459 /* TODO: check the menu item if required
460 wxMenuItem *item = bar->FindItemForId(id) ;
461 if (item && item->IsCheckable())
463 bar->Check(id,!bar->Checked(id)) ;
467 GetEventHandler()->ProcessEvent(commandEvent
);
470 // Checks if there is a toolbar, and returns the first free client position
471 wxPoint
wxFrame::GetClientAreaOrigin() const
478 GetToolBar()->GetSize(& w
, & h
);
480 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
493 void wxFrame::DoGetClientSize(int *x
, int *y
) const
495 wxWindow::DoGetClientSize( x
, y
) ;
497 if ( GetStatusBar() )
499 int statusX
, statusY
;
500 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
504 wxPoint
pt(GetClientAreaOrigin());
509 void wxFrame::DoSetClientSize(int clientwidth
, int clientheight
)
511 int currentclientwidth
, currentclientheight
;
512 int currentwidth
, currentheight
;
514 GetClientSize( ¤tclientwidth
, ¤tclientheight
) ;
515 GetSize( ¤twidth
, ¤theight
) ;
517 // find the current client size
519 // Find the difference between the entire window (title bar and all)
520 // and the client area; add this to the new client size to move the
523 DoSetSize( -1 , -1 , currentwidth
+ clientwidth
- currentclientwidth
,
524 currentheight
+ clientheight
- currentclientheight
, wxSIZE_USE_EXISTING
) ;
529 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
531 wxCHECK_MSG( m_frameToolBar
== NULL
, FALSE
,
532 "recreating toolbar in wxFrame" );
534 wxToolBar
* toolBar
= OnCreateToolBar(style
, id
, name
);
547 wxToolBar
* wxFrame::OnCreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
549 return new wxToolBar(this, id
, wxDefaultPosition
, wxDefaultSize
, style
, name
);
552 void wxFrame::PositionToolBar()
556 // TODO: we actually need to use the low-level client size, before
557 // the toolbar/status bar were added.
558 // So DEFINITELY replace the line below with something appropriate.
560 // GetClientSize(& cw, &ch);
565 if ( GetStatusBar() )
567 int statusX
, statusY
;
568 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
575 GetToolBar()->GetSize(& tw
, & th
);
577 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
579 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
580 // means, pretend we don't have toolbar/status bar, so we
581 // have the original client size.
582 GetToolBar()->SetSize(0, 0, tw
, ch
, wxSIZE_NO_ADJUSTMENTS
);
586 // Use the 'real' position
587 GetToolBar()->SetSize(0, 0, cw
, th
, wxSIZE_NO_ADJUSTMENTS
);