]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/frame.mm
fixed bug #947908: wxString("abcdefghij", wxConvLibc, n) returns an empty string...
[wxWidgets.git] / src / cocoa / frame.mm
CommitLineData
fb896a32
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/frame.mm
3// Purpose: wxFrame
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
7// RCS-ID: $Id:
8// Copyright: (c) 2003 David Elliott
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
26785f01
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/frame.h"
17 #include "wx/menu.h"
18 #include "wx/toolbar.h"
19 #include "wx/statusbr.h"
20#endif // WX_PRECOMP
21
7fc77f30 22#include "wx/cocoa/autorelease.h"
24c7767f 23#include "wx/cocoa/mbarman.h"
7fc77f30 24
fb896a32
DE
25#import <AppKit/NSWindow.h>
26#import <AppKit/NSApplication.h>
448cbf1d 27#import <AppKit/NSView.h>
205fef01 28#import <AppKit/NSMenuItem.h>
fb896a32
DE
29
30// wxFrame
31
32BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
33END_EVENT_TABLE()
34
35IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
36
37void wxFrame::Init()
38{
448cbf1d 39 m_frameNSView = nil;
fb896a32
DE
40}
41
42bool wxFrame::Create(wxWindow *parent,
43 wxWindowID winid,
44 const wxString& title,
45 const wxPoint& pos,
46 const wxSize& size,
47 long style,
48 const wxString& name)
49{
50 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
51
52 return rt;
53}
54
55wxFrame::~wxFrame()
56{
448cbf1d 57 [m_frameNSView release];
fb896a32
DE
58}
59
205fef01
DE
60// -------------------------------------------------------------------
61// Menubar
fb896a32
DE
62void wxFrame::AttachMenuBar(wxMenuBar *mbar)
63{
64 wxFrameBase::AttachMenuBar(mbar);
243f5c2d 65 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
66}
67
68void wxFrame::DetachMenuBar()
69{
fb896a32 70 wxFrameBase::DetachMenuBar();
243f5c2d 71 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
72}
73
24c7767f 74void wxFrame::SetMenuBar(wxMenuBar *menubar)
fb896a32 75{
24c7767f
DE
76 if ( menubar == GetMenuBar() )
77 {
78 // nothing to do
79 return;
80 }
81
82 wxFrameBase::DetachMenuBar();
83 wxFrameBase::AttachMenuBar(menubar);
243f5c2d 84 wxMenuBarManager::GetInstance()->UpdateMenuBar();
fb896a32
DE
85}
86
8ded703d 87wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
3e84f98f
DE
88{
89 if(GetMenuBar())
90 return GetMenuBar();
8ded703d 91 return wxFrameBase::GetAppMenuBar(win);
3e84f98f
DE
92}
93
205fef01
DE
94void wxFrame::CocoaDelegate_wxMenuItemAction(WX_NSMenuItem menuItem)
95{
96 wxLogTrace(wxTRACE_COCOA,wxT("wxFrame::wxMenuItemAction"));
97 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
98 wxCHECK_RET(item,wxT("wxMenuItemAction received but no wxMenuItem exists!"));
99
100 wxMenu *menu = item->GetMenu();
101 wxCHECK_RET(menu,wxT("wxMenuItemAction received but wxMenuItem is not in a wxMenu!"));
102
103 // Since we're handling the delegate messages there's a very good chance
104 // we'll receive a menu action from an item with a nil target.
105 wxMenuBar *menubar = menu->GetMenuBar();
106 if(menubar)
107 {
108 wxFrame *frame = menubar->GetFrame();
109 wxASSERT_MSG(frame==this, wxT("Received wxMenuItemAction in NSWindow delegate from a menu item attached to a different frame."));
110 frame->ProcessCommand(item->GetId());
111 }
112 else
113 wxLogDebug(wxT("Received wxMenuItemAction in NSWindow delegate from an unknown menu item."));
114}
115
116bool wxFrame::CocoaDelegate_validateMenuItem(WX_NSMenuItem menuItem)
117{
118 SEL itemAction = [menuItem action];
119 if(itemAction == @selector(wxMenuItemAction:))
120 {
121 wxMenuItem *item = wxMenuItem::GetFromCocoa(menuItem);
122 wxCHECK_MSG(item,false,wxT("validateMenuItem received but no wxMenuItem exists!"));
123 // TODO: do more sanity checking
124 return item->IsEnabled();
125 }
126 // TODO: else if cut/copy/paste
127 wxLogDebug(wxT("Asked to validate an unknown menu item"));
128 return false;
129}
130
131// -------------------------------------------------------------------
132// Origin/Size
fb896a32
DE
133wxPoint wxFrame::GetClientAreaOrigin() const
134{
135 return wxPoint(0,0);
136}
137
e08efb8d 138void wxFrame::CocoaSetWxWindowSize(int width, int height)
a58a6614 139{
e08efb8d 140 if(m_frameStatusBar)
a58a6614 141 height += m_frameStatusBar->GetSize().y;
26785f01 142#if wxUSE_TOOLBAR
e08efb8d
DE
143 if(m_frameToolBar)
144 height += m_frameToolBar->GetSize().y;
26785f01 145#endif //wxUSE_TOOLBAR
e08efb8d 146 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
a58a6614
DE
147}
148
205fef01 149// -------------------------------------------------------------------
514e7b7b
DE
150WX_NSView wxFrame::GetNonClientNSView()
151{
152 if(m_frameNSView)
153 return m_frameNSView;
154 return GetNSViewForSuperview();
155}
156
448cbf1d
DE
157void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
158{
159 // If we have the additional toolbar/statbar view, then the
160 // default replaceSubview will work. Otherwise, the old view
161 // should be the content view and should be replaced that way
162 if(m_frameNSView)
163 wxWindow::CocoaReplaceView(oldView, newView);
164 else
165 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
166}
167
168void wxFrame::UpdateFrameNSView()
169{
170 if(!m_frameNSView)
171 {
172 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
173 [m_cocoaNSWindow setContentView: m_frameNSView];
174 [m_frameNSView addSubview:m_cocoaNSView];
175 }
176 NSRect frameRect = [m_frameNSView frame];
177 float tbarheight = 0.0;
26785f01 178#if wxUSE_TOOLBAR
448cbf1d
DE
179 if(m_frameToolBar)
180 {
181 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
182 if(![tbarNSView superview])
183 [m_frameNSView addSubview: tbarNSView];
6613dc3f
DE
184 // Do this after addSubView so that SetSize can work
185 m_frameToolBar->SetSize(m_frameToolBar->DoGetBestSize());
448cbf1d
DE
186 NSRect tbarRect = [tbarNSView frame];
187 tbarRect.size.width = frameRect.size.width;
188 tbarRect.origin.x = 0.0;
189 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
190 [tbarNSView setFrame:tbarRect];
191 // width expands, bottom margin expands
192 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
193 tbarheight = tbarRect.size.height;
194 }
26785f01 195#endif //wxUSE_TOOLBAR
448cbf1d
DE
196 float sbarheight = 0.0;
197 if(m_frameStatusBar)
198 {
199 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
200 if(![sbarNSView superview])
201 [m_frameNSView addSubview: sbarNSView];
202 NSRect sbarRect = [sbarNSView frame];
203 sbarRect.size.width = frameRect.size.width;
204 sbarRect.origin.x = 0.0;
205 sbarRect.origin.y = 0.0;
206 [sbarNSView setFrame:sbarRect];
207 // width expands, top margin expands
208 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
209 sbarheight = sbarRect.size.height;
210 }
48580976 211 wxLogTrace(wxTRACE_COCOA,wxT("frame height=%f, tbar=%f, sbar=%f"),frameRect.size.height,tbarheight,sbarheight);
448cbf1d
DE
212 NSRect innerRect = [m_cocoaNSView frame];
213 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
214 innerRect.origin.y = sbarheight;
215 [m_cocoaNSView setFrame:innerRect];
216 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
217 // Don't let the frame get smaller than the toolbar+statusbar height
218 NSRect frameMinRect = [NSWindow frameRectForContentRect:
219 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
220 styleMask: [m_cocoaNSWindow styleMask]];
221 [m_cocoaNSWindow setMinSize:frameMinRect.size];
222}
223
224void wxFrame::SetStatusBar(wxStatusBar *statusbar)
225{
226 if(m_frameStatusBar)
227 {
228 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 229 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d 230 if(m_frameStatusBar->GetParent())
26785f01 231 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
448cbf1d
DE
232 }
233 m_frameStatusBar = statusbar;
234 if(m_frameStatusBar)
235 {
236 m_frameStatusBar->CocoaRemoveFromParent();
237 }
238 UpdateFrameNSView();
239}
240
241wxStatusBar* wxFrame::CreateStatusBar(int number,
242 long style,
243 wxWindowID winid,
244 const wxString& name)
a58a6614 245{
e1f4ff6d 246 wxAutoNSAutoreleasePool pool;
448cbf1d
DE
247 wxFrameBase::CreateStatusBar(number,style,winid,name);
248 if(m_frameStatusBar)
249 {
250 m_frameStatusBar->CocoaRemoveFromParent();
251 }
252 UpdateFrameNSView();
253 return m_frameStatusBar;
254}
a58a6614 255
26785f01 256#if wxUSE_TOOLBAR
448cbf1d
DE
257void wxFrame::SetToolBar(wxToolBar *toolbar)
258{
259 if(m_frameToolBar)
260 {
6613dc3f 261 m_frameToolBar->SetOwningFrame(NULL);
448cbf1d 262 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
c5bd9191 263 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
448cbf1d
DE
264 if(m_frameToolBar->GetParent())
265 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
266 }
267 m_frameToolBar = toolbar;
268 if(m_frameToolBar)
269 {
270 m_frameToolBar->CocoaRemoveFromParent();
6613dc3f 271 m_frameToolBar->SetOwningFrame(this);
448cbf1d
DE
272 }
273 UpdateFrameNSView();
274}
a58a6614 275
448cbf1d
DE
276wxToolBar* wxFrame::CreateToolBar(long style,
277 wxWindowID winid,
278 const wxString& name)
279{
e1f4ff6d 280 wxAutoNSAutoreleasePool pool;
448cbf1d
DE
281 wxFrameBase::CreateToolBar(style,winid,name);
282 if(m_frameToolBar)
283 {
284 m_frameToolBar->CocoaRemoveFromParent();
6613dc3f 285 m_frameToolBar->SetOwningFrame(this);
448cbf1d
DE
286 }
287 UpdateFrameNSView();
288 return m_frameToolBar;
289}
26785f01 290#endif // wxUSE_TOOLBAR
a58a6614 291
448cbf1d
DE
292void wxFrame::PositionStatusBar()
293{
a58a6614
DE
294}
295