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()->UpdateWindowMenuBar(this);
65 void wxFrame::DetachMenuBar()
67 wxFrameBase::DetachMenuBar();
68 wxMenuBarManager::GetInstance()->UpdateWindowMenuBar(this);
71 void wxFrame::SetMenuBar(wxMenuBar *menubar)
73 if ( menubar == GetMenuBar() )
79 wxFrameBase::DetachMenuBar();
80 wxFrameBase::AttachMenuBar(menubar);
81 wxMenuBarManager::GetInstance()->UpdateWindowMenuBar(this);
84 wxPoint wxFrame::GetClientAreaOrigin() const
89 void wxFrame::CocoaSetWxWindowSize(int width, int height)
92 height += m_frameStatusBar->GetSize().y;
95 height += m_frameToolBar->GetSize().y;
96 #endif //wxUSE_TOOLBAR
97 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
100 WX_NSView wxFrame::GetNonClientNSView()
103 return m_frameNSView;
104 return GetNSViewForSuperview();
107 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
109 // If we have the additional toolbar/statbar view, then the
110 // default replaceSubview will work. Otherwise, the old view
111 // should be the content view and should be replaced that way
113 wxWindow::CocoaReplaceView(oldView, newView);
115 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
118 void wxFrame::UpdateFrameNSView()
122 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
123 [m_cocoaNSWindow setContentView: m_frameNSView];
124 [m_frameNSView addSubview:m_cocoaNSView];
126 NSRect frameRect = [m_frameNSView frame];
127 float tbarheight = 0.0;
131 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
132 if(![tbarNSView superview])
133 [m_frameNSView addSubview: tbarNSView];
134 NSRect tbarRect = [tbarNSView frame];
135 tbarRect.size.width = frameRect.size.width;
136 tbarRect.origin.x = 0.0;
137 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
138 [tbarNSView setFrame:tbarRect];
139 // width expands, bottom margin expands
140 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
141 tbarheight = tbarRect.size.height;
143 #endif //wxUSE_TOOLBAR
144 float sbarheight = 0.0;
147 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
148 if(![sbarNSView superview])
149 [m_frameNSView addSubview: sbarNSView];
150 NSRect sbarRect = [sbarNSView frame];
151 sbarRect.size.width = frameRect.size.width;
152 sbarRect.origin.x = 0.0;
153 sbarRect.origin.y = 0.0;
154 [sbarNSView setFrame:sbarRect];
155 // width expands, top margin expands
156 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
157 sbarheight = sbarRect.size.height;
159 wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
160 NSRect innerRect = [m_cocoaNSView frame];
161 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
162 innerRect.origin.y = sbarheight;
163 [m_cocoaNSView setFrame:innerRect];
164 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
165 // Don't let the frame get smaller than the toolbar+statusbar height
166 NSRect frameMinRect = [NSWindow frameRectForContentRect:
167 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
168 styleMask: [m_cocoaNSWindow styleMask]];
169 [m_cocoaNSWindow setMinSize:frameMinRect.size];
172 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
176 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
177 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
178 if(m_frameStatusBar->GetParent())
179 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
181 m_frameStatusBar = statusbar;
184 m_frameStatusBar->CocoaRemoveFromParent();
189 wxStatusBar* wxFrame::CreateStatusBar(int number,
192 const wxString& name)
194 wxFrameBase::CreateStatusBar(number,style,winid,name);
197 m_frameStatusBar->CocoaRemoveFromParent();
200 return m_frameStatusBar;
204 void wxFrame::SetToolBar(wxToolBar *toolbar)
208 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
209 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
210 if(m_frameToolBar->GetParent())
211 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
213 m_frameToolBar = toolbar;
216 m_frameToolBar->CocoaRemoveFromParent();
221 wxToolBar* wxFrame::CreateToolBar(long style,
223 const wxString& name)
225 wxFrameBase::CreateToolBar(style,winid,name);
228 m_frameToolBar->CocoaRemoveFromParent();
231 return m_frameToolBar;
233 #endif // wxUSE_TOOLBAR
235 void wxFrame::PositionStatusBar()