1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/frame.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/menuitem.h"
17 #include "wx/statusbr.h"
18 #include "wx/toolbar.h"
20 #include "wx/cocoa/autorelease.h"
22 #import <AppKit/NSWindow.h>
23 #import <AppKit/NSApplication.h>
24 #import <AppKit/NSView.h>
28 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
31 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
38 bool wxFrame::Create(wxWindow *parent,
40 const wxString& title,
46 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
53 [m_frameNSView release];
56 void wxFrame::Cocoa_wxMenuItemAction(wxMenuItem& item)
58 Command(item.GetId());
61 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
63 wxFrameBase::AttachMenuBar(mbar);
66 wxLogDebug("Attached menu");
67 [m_cocoaNSWindow setMenu:m_frameMenuBar->GetNSMenu()];
71 void wxFrame::DetachMenuBar()
75 [m_cocoaNSWindow setMenu:nil];
77 wxFrameBase::DetachMenuBar();
80 bool wxFrame::Show(bool show)
82 wxAutoNSAutoreleasePool pool;
83 bool ret = wxFrameBase::Show(show);
84 if(show && GetMenuBar())
85 [wxTheApp->GetNSApplication() setMenu:GetMenuBar()->GetNSMenu() ];
89 wxPoint wxFrame::GetClientAreaOrigin() const
94 void wxFrame::DoGetClientSize(int *width, int *height) const
96 wxFrameBase::DoGetClientSize(width,height);
99 if(m_frameStatusBar && m_frameStatusBar->IsShown())
100 *height -= m_frameStatusBar->GetSize().y;
104 void wxFrame::DoSetClientSize(int width, int height)
106 if(m_frameStatusBar && m_frameStatusBar->IsShown())
107 height += m_frameStatusBar->GetSize().y;
108 wxFrameBase::DoSetClientSize(width,height);
111 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
113 // If we have the additional toolbar/statbar view, then the
114 // default replaceSubview will work. Otherwise, the old view
115 // should be the content view and should be replaced that way
117 wxWindow::CocoaReplaceView(oldView, newView);
119 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
122 void wxFrame::UpdateFrameNSView()
126 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
127 [m_cocoaNSWindow setContentView: m_frameNSView];
128 [m_frameNSView addSubview:m_cocoaNSView];
130 NSRect frameRect = [m_frameNSView frame];
131 float tbarheight = 0.0;
134 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
135 if(![tbarNSView superview])
136 [m_frameNSView addSubview: tbarNSView];
137 NSRect tbarRect = [tbarNSView frame];
138 tbarRect.size.width = frameRect.size.width;
139 tbarRect.origin.x = 0.0;
140 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
141 [tbarNSView setFrame:tbarRect];
142 // width expands, bottom margin expands
143 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
144 tbarheight = tbarRect.size.height;
146 float sbarheight = 0.0;
149 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
150 if(![sbarNSView superview])
151 [m_frameNSView addSubview: sbarNSView];
152 NSRect sbarRect = [sbarNSView frame];
153 sbarRect.size.width = frameRect.size.width;
154 sbarRect.origin.x = 0.0;
155 sbarRect.origin.y = 0.0;
156 [sbarNSView setFrame:sbarRect];
157 // width expands, top margin expands
158 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
159 sbarheight = sbarRect.size.height;
161 wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
162 NSRect innerRect = [m_cocoaNSView frame];
163 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
164 innerRect.origin.y = sbarheight;
165 [m_cocoaNSView setFrame:innerRect];
166 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
167 // Don't let the frame get smaller than the toolbar+statusbar height
168 NSRect frameMinRect = [NSWindow frameRectForContentRect:
169 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
170 styleMask: [m_cocoaNSWindow styleMask]];
171 [m_cocoaNSWindow setMinSize:frameMinRect.size];
174 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
178 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
179 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewNotSizable];
180 if(m_frameStatusBar->GetParent())
181 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameToolBar);
183 m_frameStatusBar = statusbar;
186 m_frameStatusBar->CocoaRemoveFromParent();
191 wxStatusBar* wxFrame::CreateStatusBar(int number,
194 const wxString& name)
196 wxFrameBase::CreateStatusBar(number,style,winid,name);
199 m_frameStatusBar->CocoaRemoveFromParent();
202 return m_frameStatusBar;
205 void wxFrame::SetToolBar(wxToolBar *toolbar)
209 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
210 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewNotSizable];
211 if(m_frameToolBar->GetParent())
212 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
214 m_frameToolBar = toolbar;
217 m_frameToolBar->CocoaRemoveFromParent();
222 wxToolBar* wxFrame::CreateToolBar(long style,
224 const wxString& name)
226 wxFrameBase::CreateToolBar(style,winid,name);
229 m_frameToolBar->CocoaRemoveFromParent();
232 return m_frameToolBar;
235 void wxFrame::PositionStatusBar()