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