]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/frame.mm
Applied [ 821234 ] Fix: erroneous assertion failed wxListBox::SetSelection
[wxWidgets.git] / src / cocoa / frame.mm
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
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
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/mbarman.h"
24
25 #import <AppKit/NSWindow.h>
26 #import <AppKit/NSApplication.h>
27 #import <AppKit/NSView.h>
28
29 // wxFrame
30
31 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
32 END_EVENT_TABLE()
33
34 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
35
36 void wxFrame::Init()
37 {
38 m_frameNSView = nil;
39 }
40
41 bool wxFrame::Create(wxWindow *parent,
42 wxWindowID winid,
43 const wxString& title,
44 const wxPoint& pos,
45 const wxSize& size,
46 long style,
47 const wxString& name)
48 {
49 bool rt = wxTopLevelWindow::Create(parent,winid,title,pos,size,style,name);
50
51 return rt;
52 }
53
54 wxFrame::~wxFrame()
55 {
56 [m_frameNSView release];
57 }
58
59 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
60 {
61 wxFrameBase::AttachMenuBar(mbar);
62 wxMenuBarManager::GetInstance()->UpdateMenuBar();
63 }
64
65 void wxFrame::DetachMenuBar()
66 {
67 wxFrameBase::DetachMenuBar();
68 wxMenuBarManager::GetInstance()->UpdateMenuBar();
69 }
70
71 void wxFrame::SetMenuBar(wxMenuBar *menubar)
72 {
73 if ( menubar == GetMenuBar() )
74 {
75 // nothing to do
76 return;
77 }
78
79 wxFrameBase::DetachMenuBar();
80 wxFrameBase::AttachMenuBar(menubar);
81 wxMenuBarManager::GetInstance()->UpdateMenuBar();
82 }
83
84 wxMenuBar* wxFrame::GetAppMenuBar(wxCocoaNSWindow *win)
85 {
86 if(GetMenuBar())
87 return GetMenuBar();
88 return wxFrameBase::GetAppMenuBar(win);
89 }
90
91 wxPoint wxFrame::GetClientAreaOrigin() const
92 {
93 return wxPoint(0,0);
94 }
95
96 void wxFrame::CocoaSetWxWindowSize(int width, int height)
97 {
98 if(m_frameStatusBar)
99 height += m_frameStatusBar->GetSize().y;
100 #if wxUSE_TOOLBAR
101 if(m_frameToolBar)
102 height += m_frameToolBar->GetSize().y;
103 #endif //wxUSE_TOOLBAR
104 wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
105 }
106
107 WX_NSView wxFrame::GetNonClientNSView()
108 {
109 if(m_frameNSView)
110 return m_frameNSView;
111 return GetNSViewForSuperview();
112 }
113
114 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
115 {
116 // If we have the additional toolbar/statbar view, then the
117 // default replaceSubview will work. Otherwise, the old view
118 // should be the content view and should be replaced that way
119 if(m_frameNSView)
120 wxWindow::CocoaReplaceView(oldView, newView);
121 else
122 wxTopLevelWindow::CocoaReplaceView(oldView, newView);
123 }
124
125 void wxFrame::UpdateFrameNSView()
126 {
127 if(!m_frameNSView)
128 {
129 m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
130 [m_cocoaNSWindow setContentView: m_frameNSView];
131 [m_frameNSView addSubview:m_cocoaNSView];
132 }
133 NSRect frameRect = [m_frameNSView frame];
134 float tbarheight = 0.0;
135 #if wxUSE_TOOLBAR
136 if(m_frameToolBar)
137 {
138 NSView *tbarNSView = m_frameToolBar->GetNSViewForSuperview();
139 if(![tbarNSView superview])
140 [m_frameNSView addSubview: tbarNSView];
141 NSRect tbarRect = [tbarNSView frame];
142 tbarRect.size.width = frameRect.size.width;
143 tbarRect.origin.x = 0.0;
144 tbarRect.origin.y = frameRect.size.height - tbarRect.size.height;
145 [tbarNSView setFrame:tbarRect];
146 // width expands, bottom margin expands
147 [tbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMinYMargin];
148 tbarheight = tbarRect.size.height;
149 }
150 #endif //wxUSE_TOOLBAR
151 float sbarheight = 0.0;
152 if(m_frameStatusBar)
153 {
154 NSView *sbarNSView = m_frameStatusBar->GetNSViewForSuperview();
155 if(![sbarNSView superview])
156 [m_frameNSView addSubview: sbarNSView];
157 NSRect sbarRect = [sbarNSView frame];
158 sbarRect.size.width = frameRect.size.width;
159 sbarRect.origin.x = 0.0;
160 sbarRect.origin.y = 0.0;
161 [sbarNSView setFrame:sbarRect];
162 // width expands, top margin expands
163 [sbarNSView setAutoresizingMask: NSViewWidthSizable|NSViewMaxYMargin];
164 sbarheight = sbarRect.size.height;
165 }
166 wxLogDebug("frame height=%f, tbar=%f, sbar=%f",frameRect.size.height,tbarheight,sbarheight);
167 NSRect innerRect = [m_cocoaNSView frame];
168 innerRect.size.height = frameRect.size.height - tbarheight - sbarheight;
169 innerRect.origin.y = sbarheight;
170 [m_cocoaNSView setFrame:innerRect];
171 [m_cocoaNSView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
172 // Don't let the frame get smaller than the toolbar+statusbar height
173 NSRect frameMinRect = [NSWindow frameRectForContentRect:
174 NSMakeRect(0.0,0.0,0.0,tbarheight+sbarheight)
175 styleMask: [m_cocoaNSWindow styleMask]];
176 [m_cocoaNSWindow setMinSize:frameMinRect.size];
177 }
178
179 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
180 {
181 if(m_frameStatusBar)
182 {
183 [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
184 [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
185 if(m_frameStatusBar->GetParent())
186 m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
187 }
188 m_frameStatusBar = statusbar;
189 if(m_frameStatusBar)
190 {
191 m_frameStatusBar->CocoaRemoveFromParent();
192 }
193 UpdateFrameNSView();
194 }
195
196 wxStatusBar* wxFrame::CreateStatusBar(int number,
197 long style,
198 wxWindowID winid,
199 const wxString& name)
200 {
201 wxFrameBase::CreateStatusBar(number,style,winid,name);
202 if(m_frameStatusBar)
203 {
204 m_frameStatusBar->CocoaRemoveFromParent();
205 }
206 UpdateFrameNSView();
207 return m_frameStatusBar;
208 }
209
210 #if wxUSE_TOOLBAR
211 void wxFrame::SetToolBar(wxToolBar *toolbar)
212 {
213 if(m_frameToolBar)
214 {
215 [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
216 [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
217 if(m_frameToolBar->GetParent())
218 m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
219 }
220 m_frameToolBar = toolbar;
221 if(m_frameToolBar)
222 {
223 m_frameToolBar->CocoaRemoveFromParent();
224 }
225 UpdateFrameNSView();
226 }
227
228 wxToolBar* wxFrame::CreateToolBar(long style,
229 wxWindowID winid,
230 const wxString& name)
231 {
232 wxFrameBase::CreateToolBar(style,winid,name);
233 if(m_frameToolBar)
234 {
235 m_frameToolBar->CocoaRemoveFromParent();
236 }
237 UpdateFrameNSView();
238 return m_frameToolBar;
239 }
240 #endif // wxUSE_TOOLBAR
241
242 void wxFrame::PositionStatusBar()
243 {
244 }
245