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