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/menuitem.h"
24 #include "wx/cocoa/autorelease.h"
26 #import <AppKit/NSWindow.h>
27 #import <AppKit/NSApplication.h>
28 #import <AppKit/NSView.h>
32 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
35 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
42 bool wxFrame::Create(wxWindow *parent,
44 const wxString& title,
50 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
57 [m_frameNSView release];
60 void wxFrame::Cocoa_wxMenuItemAction(wxMenuItem& item)
62 Command(item.GetId());
65 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
67 wxFrameBase::AttachMenuBar(mbar);
70 wxLogDebug("Attached menu");
71 [m_cocoaNSWindow setMenu:m_frameMenuBar->GetNSMenu()];
75 void wxFrame::DetachMenuBar()
79 [m_cocoaNSWindow setMenu:nil];
81 wxFrameBase::DetachMenuBar();
84 bool wxFrame::Show(bool show)
86 wxAutoNSAutoreleasePool pool;
87 bool ret = wxFrameBase::Show(show);
88 if(show && GetMenuBar())
89 [wxTheApp->GetNSApplication() setMenu:GetMenuBar()->GetNSMenu() ];
93 wxPoint wxFrame::GetClientAreaOrigin() const
98 void wxFrame::CocoaSetWxWindowSize(int width, int height)
101 height += m_frameStatusBar->GetSize().y;
104 height += m_frameToolBar->GetSize().y;
105 #endif //wxUSE_TOOLBAR
106 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
109 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
111 // If we have the additional toolbar/statbar view, then the
112 // default replaceSubview will work. Otherwise, the old view
113 // should be the content view and should be replaced that way
115 wxWindow::CocoaReplaceView(oldView, newView);
117 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
120 void wxFrame::UpdateFrameNSView()
124 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
125 [m_cocoaNSWindow setContentView: m_frameNSView];
126 [m_frameNSView addSubview:m_cocoaNSView];
128 NSRect frameRect = [m_frameNSView frame];
129 float tbarheight = 0.0;
133 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
134 if(![tbarNSView superview])
135 [m_frameNSView addSubview: tbarNSView];
136 NSRect tbarRect = [tbarNSView frame];
137 tbarRect.size.width = frameRect.size.width;
138 tbarRect.origin.x = 0.0;
139 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
140 [tbarNSView setFrame:tbarRect];
141 // width expands, bottom margin expands
142 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
143 tbarheight = tbarRect.size.height;
145 #endif //wxUSE_TOOLBAR
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: NSViewMinYMargin];
180 if(m_frameStatusBar->GetParent())
181 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
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;
206 void wxFrame::SetToolBar(wxToolBar *toolbar)
210 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
211 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
212 if(m_frameToolBar->GetParent())
213 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
215 m_frameToolBar = toolbar;
218 m_frameToolBar->CocoaRemoveFromParent();
223 wxToolBar* wxFrame::CreateToolBar(long style,
225 const wxString& name)
227 wxFrameBase::CreateToolBar(style,winid,name);
230 m_frameToolBar->CocoaRemoveFromParent();
233 return m_frameToolBar;
235 #endif // wxUSE_TOOLBAR
237 void wxFrame::PositionStatusBar()