1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/frame.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/toolbar.h"
21 #include "wx/statusbr.h"
24 #include "wx/cocoa/autorelease.h"
25 #include "wx/cocoa/mbarman.h"
27 #import <AppKit/NSWindow.h>
28 #import <AppKit/NSApplication.h>
29 #import <AppKit/NSView.h>
30 #import <AppKit/NSMenuItem.h>
34 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
37 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
44 bool wxFrame::Create(wxWindow *parent,
46 const wxString& title,
52 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
59 [m_frameNSView release];
62 // -------------------------------------------------------------------
64 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
66 wxFrameBase::AttachMenuBar(mbar);
67 wxMenuBarManager::GetInstance()->UpdateMenuBar();
70 void wxFrame::DetachMenuBar()
72 wxFrameBase::DetachMenuBar();
73 wxMenuBarManager::GetInstance()->UpdateMenuBar();
76 void wxFrame::SetMenuBar(wxMenuBar *menubar)
78 if ( menubar == GetMenuBar() )
84 wxFrameBase::DetachMenuBar();
85 wxFrameBase::AttachMenuBar(menubar);
86 wxMenuBarManager::GetInstance()->UpdateMenuBar();
89 wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
93 return wxFrameBase::GetAppMenuBar(win);
96 void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
98 wxLogTrace(wxTRACE_COCOA,wxT("wxFrame::wxMenuItemAction"));
99 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
100 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
102 wxMenu *menu = item->GetMenu();
103 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu!"));
105 // Since we're handling the delegate messages there's a very good chance
106 // we'll receive a menu action from an item with a nil target.
107 wxMenuBar *menubar = menu->GetMenuBar();
110 wxFrame *frame = menubar->GetFrame();
111 wxASSERT_MSG(frame==this, wxT("Received wxMenuItemAction in NSWindow delegate from a menu item attached to a different frame."));
112 frame->ProcessCommand(item->GetId());
115 wxLogDebug(wxT("Received wxMenuItemAction in NSWindow delegate from an unknown menu item."));
118 bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
120 SEL itemAction = [menuItem action];
121 if(itemAction == @selector(wxMenuItemAction:))
123 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
124 wxCHECK_MSG(item,false,wxT("validateMenuItem received but no wxMenuItem exists!"));
125 // TODO: do more sanity checking
126 return item->IsEnabled();
128 // TODO: else if cut/copy/paste
129 wxLogDebug(wxT("Asked to validate an unknown menu item"));
133 // -------------------------------------------------------------------
135 wxPoint wxFrame::GetClientAreaOrigin() const
140 void wxFrame::CocoaSetWxWindowSize(int width, int height)
143 height += m_frameStatusBar->GetSize().y;
146 height += m_frameToolBar->GetSize().y;
147 #endif //wxUSE_TOOLBAR
148 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
151 // -------------------------------------------------------------------
152 WX_NSView wxFrame::GetNonClientNSView()
155 return m_frameNSView;
156 return GetNSViewForSuperview();
159 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
161 // If we have the additional toolbar/statbar view, then the
162 // default replaceSubview will work. Otherwise, the old view
163 // should be the content view and should be replaced that way
165 wxWindow::CocoaReplaceView(oldView, newView);
167 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
170 void wxFrame::UpdateFrameNSView()
174 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
175 [m_cocoaNSWindow setContentView: m_frameNSView];
176 [m_frameNSView addSubview:m_cocoaNSView];
178 NSRect frameRect = [m_frameNSView frame];
179 float tbarheight = 0.0;
183 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
184 // If the toolbar doesn't have a superview then set it to our
186 if(![tbarNSView superview])
187 [m_frameNSView addSubview: tbarNSView];
188 // Do this after addSubView so that SetSize can work
189 m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
190 NSRect tbarRect = [tbarNSView frame];
191 tbarRect.size.width = frameRect.size.width;
192 tbarRect.origin.x = 0.0;
193 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
194 [tbarNSView setFrame:tbarRect];
195 // width expands, bottom margin expands
196 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
197 tbarheight = tbarRect.size.height;
199 #endif //wxUSE_TOOLBAR
200 float sbarheight = 0.0;
203 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
204 if(![sbarNSView superview])
205 [m_frameNSView addSubview: sbarNSView];
206 NSRect sbarRect = [sbarNSView frame];
207 sbarRect.size.width = frameRect.size.width;
208 sbarRect.origin.x = 0.0;
209 sbarRect.origin.y = 0.0;
210 [sbarNSView setFrame:sbarRect];
211 // width expands, top margin expands
212 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
213 sbarheight = sbarRect.size.height;
215 wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
216 NSRect innerRect = [m_cocoaNSView frame];
217 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
218 innerRect.origin.y = sbarheight;
219 [m_cocoaNSView setFrame:innerRect];
220 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
221 // Don't let the frame get smaller than the toolbar+statusbar height
222 NSRect frameMinRect = [NSWindow frameRectForContentRect:
223 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
224 styleMask: [m_cocoaNSWindow styleMask]];
225 [m_cocoaNSWindow setMinSize:frameMinRect.size];
228 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
232 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
233 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
234 if(m_frameStatusBar->GetParent())
235 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
237 m_frameStatusBar = statusbar;
240 m_frameStatusBar->CocoaRemoveFromParent();
245 wxStatusBar* wxFrame::CreateStatusBar(int number,
248 const wxString& name)
250 wxAutoNSAutoreleasePool pool;
251 wxFrameBase::CreateStatusBar(number,style,winid,name);
254 m_frameStatusBar->CocoaRemoveFromParent();
257 return m_frameStatusBar;
261 void wxFrame::SetToolBar(wxToolBar *toolbar)
265 m_frameToolBar->SetOwningFrame(NULL);
266 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
267 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
268 if(m_frameToolBar->GetParent())
269 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
271 m_frameToolBar = toolbar;
274 m_frameToolBar->CocoaRemoveFromParent();
275 m_frameToolBar->SetOwningFrame(this);
280 wxToolBar* wxFrame::CreateToolBar(long style,
282 const wxString& name)
284 wxAutoNSAutoreleasePool pool;
285 return wxFrameBase::CreateToolBar(style,winid,name);
287 #endif // wxUSE_TOOLBAR
289 void wxFrame::PositionStatusBar()