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"
24 #import <AppKit/NSWindow.h>
25 #import <AppKit/NSApplication.h>
26 #import <AppKit/NSView.h>
30 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
33 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
40 bool wxFrame::Create(wxWindow *parent,
42 const wxString& title,
48 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
55 [m_frameNSView release];
58 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
60 wxFrameBase::AttachMenuBar(mbar);
63 wxLogDebug("Attached menu");
64 [m_cocoaNSWindow setMenu:m_frameMenuBar->GetNSMenu()];
68 void wxFrame::DetachMenuBar()
72 [m_cocoaNSWindow setMenu:nil];
74 wxFrameBase::DetachMenuBar();
77 bool wxFrame::Show(bool show)
79 wxAutoNSAutoreleasePool pool;
80 bool ret = wxFrameBase::Show(show);
81 if(show && GetMenuBar())
82 [wxTheApp->GetNSApplication() setMenu:GetMenuBar()->GetNSMenu() ];
86 wxPoint wxFrame::GetClientAreaOrigin() const
91 void wxFrame::CocoaSetWxWindowSize(int width, int height)
94 height += m_frameStatusBar->GetSize().y;
97 height += m_frameToolBar->GetSize().y;
98 #endif //wxUSE_TOOLBAR
99 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
102 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
104 // If we have the additional toolbar/statbar view, then the
105 // default replaceSubview will work. Otherwise, the old view
106 // should be the content view and should be replaced that way
108 wxWindow::CocoaReplaceView(oldView, newView);
110 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
113 void wxFrame::UpdateFrameNSView()
117 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
118 [m_cocoaNSWindow setContentView: m_frameNSView];
119 [m_frameNSView addSubview:m_cocoaNSView];
121 NSRect frameRect = [m_frameNSView frame];
122 float tbarheight = 0.0;
126 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
127 if(![tbarNSView superview])
128 [m_frameNSView addSubview: tbarNSView];
129 NSRect tbarRect = [tbarNSView frame];
130 tbarRect.size.width = frameRect.size.width;
131 tbarRect.origin.x = 0.0;
132 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
133 [tbarNSView setFrame:tbarRect];
134 // width expands, bottom margin expands
135 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
136 tbarheight = tbarRect.size.height;
138 #endif //wxUSE_TOOLBAR
139 float sbarheight = 0.0;
142 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
143 if(![sbarNSView superview])
144 [m_frameNSView addSubview: sbarNSView];
145 NSRect sbarRect = [sbarNSView frame];
146 sbarRect.size.width = frameRect.size.width;
147 sbarRect.origin.x = 0.0;
148 sbarRect.origin.y = 0.0;
149 [sbarNSView setFrame:sbarRect];
150 // width expands, top margin expands
151 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
152 sbarheight = sbarRect.size.height;
154 wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
155 NSRect innerRect = [m_cocoaNSView frame];
156 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
157 innerRect.origin.y = sbarheight;
158 [m_cocoaNSView setFrame:innerRect];
159 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
160 // Don't let the frame get smaller than the toolbar+statusbar height
161 NSRect frameMinRect = [NSWindow frameRectForContentRect:
162 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
163 styleMask: [m_cocoaNSWindow styleMask]];
164 [m_cocoaNSWindow setMinSize:frameMinRect.size];
167 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
171 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
172 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
173 if(m_frameStatusBar->GetParent())
174 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
176 m_frameStatusBar = statusbar;
179 m_frameStatusBar->CocoaRemoveFromParent();
184 wxStatusBar* wxFrame::CreateStatusBar(int number,
187 const wxString& name)
189 wxFrameBase::CreateStatusBar(number,style,winid,name);
192 m_frameStatusBar->CocoaRemoveFromParent();
195 return m_frameStatusBar;
199 void wxFrame::SetToolBar(wxToolBar *toolbar)
203 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
204 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
205 if(m_frameToolBar->GetParent())
206 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
208 m_frameToolBar = toolbar;
211 m_frameToolBar->CocoaRemoveFromParent();
216 wxToolBar* wxFrame::CreateToolBar(long style,
218 const wxString& name)
220 wxFrameBase::CreateToolBar(style,winid,name);
223 m_frameToolBar->CocoaRemoveFromParent();
226 return m_frameToolBar;
228 #endif // wxUSE_TOOLBAR
230 void wxFrame::PositionStatusBar()