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::CocoaSetWxWindowSize(int width, int height)
97 height += m_frameStatusBar->GetSize().y;
99 height += m_frameToolBar->GetSize().y;
100 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
103 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
105 // If we have the additional toolbar/statbar view, then the
106 // default replaceSubview will work. Otherwise, the old view
107 // should be the content view and should be replaced that way
109 wxWindow::CocoaReplaceView(oldView, newView);
111 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
114 void wxFrame::UpdateFrameNSView()
118 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
119 [m_cocoaNSWindow setContentView: m_frameNSView];
120 [m_frameNSView addSubview:m_cocoaNSView];
122 NSRect frameRect = [m_frameNSView frame];
123 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 float sbarheight = 0.0;
141 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
142 if(![sbarNSView superview])
143 [m_frameNSView addSubview: sbarNSView];
144 NSRect sbarRect = [sbarNSView frame];
145 sbarRect.size.width = frameRect.size.width;
146 sbarRect.origin.x = 0.0;
147 sbarRect.origin.y = 0.0;
148 [sbarNSView setFrame:sbarRect];
149 // width expands, top margin expands
150 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
151 sbarheight = sbarRect.size.height;
153 wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
154 NSRect innerRect = [m_cocoaNSView frame];
155 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
156 innerRect.origin.y = sbarheight;
157 [m_cocoaNSView setFrame:innerRect];
158 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
159 // Don't let the frame get smaller than the toolbar+statusbar height
160 NSRect frameMinRect = [NSWindow frameRectForContentRect:
161 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
162 styleMask: [m_cocoaNSWindow styleMask]];
163 [m_cocoaNSWindow setMinSize:frameMinRect.size];
166 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
170 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
171 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
172 if(m_frameStatusBar->GetParent())
173 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameToolBar);
175 m_frameStatusBar = statusbar;
178 m_frameStatusBar->CocoaRemoveFromParent();
183 wxStatusBar* wxFrame::CreateStatusBar(int number,
186 const wxString& name)
188 wxFrameBase::CreateStatusBar(number,style,winid,name);
191 m_frameStatusBar->CocoaRemoveFromParent();
194 return m_frameStatusBar;
197 void wxFrame::SetToolBar(wxToolBar *toolbar)
201 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
202 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
203 if(m_frameToolBar->GetParent())
204 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
206 m_frameToolBar = toolbar;
209 m_frameToolBar->CocoaRemoveFromParent();
214 wxToolBar* wxFrame::CreateToolBar(long style,
216 const wxString& name)
218 wxFrameBase::CreateToolBar(style,winid,name);
221 m_frameToolBar->CocoaRemoveFromParent();
224 return m_frameToolBar;
227 void wxFrame::PositionStatusBar()