1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/frame.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/toolbar.h"
19 #include "wx/statusbr.h"
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/mbarman.h"
25 #import <AppKit/NSWindow.h>
26 #import <AppKit/NSApplication.h>
27 #import <AppKit/NSView.h>
31 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
34 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
41 bool wxFrame::Create(wxWindow *parent,
43 const wxString& title,
49 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
56 [m_frameNSView release];
59 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
61 wxFrameBase::AttachMenuBar(mbar);
62 wxMenuBarManager::GetInstance()->UpdateMenuBar();
65 void wxFrame::DetachMenuBar()
67 wxFrameBase::DetachMenuBar();
68 wxMenuBarManager::GetInstance()->UpdateMenuBar();
71 void wxFrame::SetMenuBar(wxMenuBar *menubar)
73 if ( menubar == GetMenuBar() )
79 wxFrameBase::DetachMenuBar();
80 wxFrameBase::AttachMenuBar(menubar);
81 wxMenuBarManager::GetInstance()->UpdateMenuBar();
84 wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
88 return wxFrameBase::GetAppMenuBar(win);
91 wxPoint wxFrame::GetClientAreaOrigin() const
96 void wxFrame::CocoaSetWxWindowSize(int width, int height)
99 height += m_frameStatusBar->GetSize().y;
102 height += m_frameToolBar->GetSize().y;
103 #endif //wxUSE_TOOLBAR
104 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
107 WX_NSView wxFrame::GetNonClientNSView()
110 return m_frameNSView;
111 return GetNSViewForSuperview();
114 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
116 // If we have the additional toolbar/statbar view, then the
117 // default replaceSubview will work. Otherwise, the old view
118 // should be the content view and should be replaced that way
120 wxWindow::CocoaReplaceView(oldView, newView);
122 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
125 void wxFrame::UpdateFrameNSView()
129 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
130 [m_cocoaNSWindow setContentView: m_frameNSView];
131 [m_frameNSView addSubview:m_cocoaNSView];
133 NSRect frameRect = [m_frameNSView frame];
134 float tbarheight = 0.0;
138 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
139 if(![tbarNSView superview])
140 [m_frameNSView addSubview: tbarNSView];
141 // Do this after addSubView so that SetSize can work
142 m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
143 NSRect tbarRect = [tbarNSView frame];
144 tbarRect.size.width = frameRect.size.width;
145 tbarRect.origin.x = 0.0;
146 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
147 [tbarNSView setFrame:tbarRect];
148 // width expands, bottom margin expands
149 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
150 tbarheight = tbarRect.size.height;
152 #endif //wxUSE_TOOLBAR
153 float sbarheight = 0.0;
156 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
157 if(![sbarNSView superview])
158 [m_frameNSView addSubview: sbarNSView];
159 NSRect sbarRect = [sbarNSView frame];
160 sbarRect.size.width = frameRect.size.width;
161 sbarRect.origin.x = 0.0;
162 sbarRect.origin.y = 0.0;
163 [sbarNSView setFrame:sbarRect];
164 // width expands, top margin expands
165 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
166 sbarheight = sbarRect.size.height;
168 wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
169 NSRect innerRect = [m_cocoaNSView frame];
170 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
171 innerRect.origin.y = sbarheight;
172 [m_cocoaNSView setFrame:innerRect];
173 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
174 // Don't let the frame get smaller than the toolbar+statusbar height
175 NSRect frameMinRect = [NSWindow frameRectForContentRect:
176 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
177 styleMask: [m_cocoaNSWindow styleMask]];
178 [m_cocoaNSWindow setMinSize:frameMinRect.size];
181 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
185 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
186 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
187 if(m_frameStatusBar->GetParent())
188 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
190 m_frameStatusBar = statusbar;
193 m_frameStatusBar->CocoaRemoveFromParent();
198 wxStatusBar* wxFrame::CreateStatusBar(int number,
201 const wxString& name)
203 wxAutoNSAutoreleasePool pool;
204 wxFrameBase::CreateStatusBar(number,style,winid,name);
207 m_frameStatusBar->CocoaRemoveFromParent();
210 return m_frameStatusBar;
214 void wxFrame::SetToolBar(wxToolBar *toolbar)
218 m_frameToolBar->SetOwningFrame(NULL);
219 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
220 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
221 if(m_frameToolBar->GetParent())
222 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
224 m_frameToolBar = toolbar;
227 m_frameToolBar->CocoaRemoveFromParent();
228 m_frameToolBar->SetOwningFrame(this);
233 wxToolBar* wxFrame::CreateToolBar(long style,
235 const wxString& name)
237 wxAutoNSAutoreleasePool pool;
238 wxFrameBase::CreateToolBar(style,winid,name);
241 m_frameToolBar->CocoaRemoveFromParent();
242 m_frameToolBar->SetOwningFrame(this);
245 return m_frameToolBar;
247 #endif // wxUSE_TOOLBAR
249 void wxFrame::PositionStatusBar()