* Clean up the includes and use wxprec.h
[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/menuitem.h"
23
24 #include "wx/cocoa/autorelease.h"
25
26 #import <AppKit/NSWindow.h>
27 #import <AppKit/NSApplication.h>
28 #import <AppKit/NSView.h>
29
30 // wxFrame
31
32 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
33 END_EVENT_TABLE()
34
35 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
36
37 void wxFrame::Init()
38 {
39     m_frameNSView = nil;
40 }
41
42 bool 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
55 wxFrame::~wxFrame()
56 {
57     [m_frameNSView release];
58 }
59
60 void wxFrame::Cocoa_wxMenuItemAction(wxMenuItem& item)
61 {
62     Command(item.GetId());
63 }
64
65 void wxFrame::AttachMenuBar(wxMenuBar *mbar)
66 {
67     wxFrameBase::AttachMenuBar(mbar);
68     if(m_frameMenuBar)
69     {
70         wxLogDebug("Attached menu");
71         [m_cocoaNSWindow setMenu:m_frameMenuBar->GetNSMenu()];
72     }
73 }
74
75 void wxFrame::DetachMenuBar()
76 {
77     if(m_frameMenuBar)
78     {
79         [m_cocoaNSWindow setMenu:nil];
80     }
81     wxFrameBase::DetachMenuBar();
82 }
83
84 bool wxFrame::Show(bool show)
85 {
86     wxAutoNSAutoreleasePool pool;
87     bool ret = wxFrameBase::Show(show);
88     if(show && GetMenuBar())
89         [wxTheApp->GetNSApplication() setMenu:GetMenuBar()->GetNSMenu() ];
90     return ret;
91 }
92
93 wxPoint wxFrame::GetClientAreaOrigin() const
94 {
95     return wxPoint(0,0);
96 }
97
98 void wxFrame::CocoaSetWxWindowSize(int width, int height)
99 {
100     if(m_frameStatusBar)
101         height += m_frameStatusBar->GetSize().y;
102 #if wxUSE_TOOLBAR
103     if(m_frameToolBar)
104         height += m_frameToolBar->GetSize().y;
105 #endif //wxUSE_TOOLBAR
106     wxTopLevelWindow::CocoaSetWxWindowSize(width,height);
107 }
108
109 void wxFrame::CocoaReplaceView(WX_NSView oldView, WX_NSView newView)
110 {
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
114     if(m_frameNSView)
115         wxWindow::CocoaReplaceView(oldView, newView);
116     else
117         wxTopLevelWindow::CocoaReplaceView(oldView, newView);
118 }
119
120 void wxFrame::UpdateFrameNSView()
121 {
122     if(!m_frameNSView)
123     {
124         m_frameNSView = [[NSView alloc] initWithFrame:[[m_cocoaNSWindow contentView] frame]];
125         [m_cocoaNSWindow setContentView: m_frameNSView];
126         [m_frameNSView addSubview:m_cocoaNSView];
127     }
128     NSRect frameRect = [m_frameNSView frame];
129     float tbarheight = 0.0;
130 #if wxUSE_TOOLBAR
131     if(m_frameToolBar)
132     {
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;
144     }
145 #endif //wxUSE_TOOLBAR
146     float sbarheight = 0.0;
147     if(m_frameStatusBar)
148     {
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;
160     }
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];
172 }
173
174 void wxFrame::SetStatusBar(wxStatusBar *statusbar)
175 {
176     if(m_frameStatusBar)
177     {
178         [m_frameStatusBar->GetNSViewForSuperview() removeFromSuperview];
179         [m_frameStatusBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
180         if(m_frameStatusBar->GetParent())
181             m_frameStatusBar->GetParent()->CocoaAddChild(m_frameStatusBar);
182     }
183     m_frameStatusBar = statusbar;
184     if(m_frameStatusBar)
185     {
186         m_frameStatusBar->CocoaRemoveFromParent();
187     }
188     UpdateFrameNSView();
189 }
190
191 wxStatusBar* wxFrame::CreateStatusBar(int number,
192                                           long style,
193                                           wxWindowID winid,
194                                           const wxString& name)
195 {
196     wxFrameBase::CreateStatusBar(number,style,winid,name);
197     if(m_frameStatusBar)
198     {
199         m_frameStatusBar->CocoaRemoveFromParent();
200     }
201     UpdateFrameNSView();
202     return m_frameStatusBar;
203 }
204
205 #if wxUSE_TOOLBAR
206 void wxFrame::SetToolBar(wxToolBar *toolbar)
207 {
208     if(m_frameToolBar)
209     {
210         [m_frameToolBar->GetNSViewForSuperview() removeFromSuperview];
211         [m_frameToolBar->GetNSViewForSuperview() setAutoresizingMask: NSViewMinYMargin];
212         if(m_frameToolBar->GetParent())
213             m_frameToolBar->GetParent()->CocoaAddChild(m_frameToolBar);
214     }
215     m_frameToolBar = toolbar;
216     if(m_frameToolBar)
217     {
218         m_frameToolBar->CocoaRemoveFromParent();
219     }
220     UpdateFrameNSView();
221 }
222
223 wxToolBar* wxFrame::CreateToolBar(long style,
224                                       wxWindowID winid,
225                                       const wxString& name)
226 {
227     wxFrameBase::CreateToolBar(style,winid,name);
228     if(m_frameToolBar)
229     {
230         m_frameToolBar->CocoaRemoveFromParent();
231     }
232     UpdateFrameNSView();
233     return m_frameToolBar;
234 }
235 #endif // wxUSE_TOOLBAR
236
237 void wxFrame::PositionStatusBar()
238 {
239 }
240