]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/notebook.mm
wiring OnInit on osx to a later point in event processing
[wxWidgets.git] / src / osx / cocoa / notebook.mm
CommitLineData
f033830e
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/cocoa/notebook.mm
3// Purpose: implementation of wxNotebook
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
a9a4f229 7// RCS-ID: $Id$
f033830e
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_NOTEBOOK
15
16#include "wx/notebook.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/string.h"
20 #include "wx/log.h"
21 #include "wx/app.h"
22 #include "wx/image.h"
23#endif
24
25#include "wx/string.h"
26#include "wx/imaglist.h"
27#include "wx/osx/private.h"
28
dbeddfb9
SC
29//
30// controller
31//
32
c8fdb345 33@interface wxTabViewController : NSObject wxOSX_10_6_AND_LATER(<NSTabViewDelegate>)
dbeddfb9
SC
34{
35}
36
37- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
38- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
39
40@end
41
f033830e
SC
42@interface wxNSTabView : NSTabView
43{
f033830e
SC
44}
45
f033830e
SC
46@end
47
dbeddfb9 48@implementation wxTabViewController
f033830e 49
dbeddfb9 50- (id) init
f033830e 51{
f599a415 52 self = [super init];
dbeddfb9 53 return self;
f033830e
SC
54}
55
dbeddfb9 56- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
f033830e 57{
d8207702 58 wxUnusedVar(tabViewItem);
dbeddfb9 59 wxNSTabView* view = (wxNSTabView*) tabView;
4dd9fdf8
SC
60 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
61
dbeddfb9
SC
62 if ( viewimpl )
63 {
ffad7b0d 64 // wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
dbeddfb9
SC
65 }
66 return YES;
f033830e
SC
67}
68
dbbb040c 69- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
f033830e 70{
d8207702 71 wxUnusedVar(tabViewItem);
dbeddfb9 72 wxNSTabView* view = (wxNSTabView*) tabView;
4dd9fdf8 73 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
dbeddfb9
SC
74 if ( viewimpl )
75 {
76 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
04236b74 77 wxpeer->OSXHandleClicked(0);
dbeddfb9
SC
78 }
79}
80
81@end
82
83@implementation wxNSTabView
84
4dd9fdf8
SC
85+ (void)initialize
86{
87 static BOOL initialized = NO;
03647350 88 if (!initialized)
4dd9fdf8
SC
89 {
90 initialized = YES;
91 wxOSXCocoaClassAddWXMethods( self );
92 }
93}
f033830e
SC
94
95@end
96
294de5ca
VZ
97// ========================================================================
98// WXCTabViewImageItem
99// ========================================================================
100@interface WXCTabViewImageItem : NSTabViewItem
101{
102 NSImage *m_image;
103}
104- (id)init;
105- (void)dealloc;
106- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
107- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
108- (NSImage*)image;
109- (void)setImage:(NSImage*)image;
110@end // interface WXCTabViewImageItem : NSTabViewItem
111
112
113@implementation WXCTabViewImageItem : NSTabViewItem
114- (id)init
115{
116 m_image = nil;
117 return [super initWithIdentifier:nil];
118}
119- (void)dealloc
120{
121 [m_image release];
122 [super dealloc];
123}
124- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
125{
126 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
127 if(!m_image)
128 return labelSize;
129 NSSize imageSize = [m_image size];
130 // scale image size
131 if(imageSize.height > labelSize.height)
132 {
133 imageSize.width *= labelSize.height/imageSize.height;
134 imageSize.height *= labelSize.height/imageSize.height;
135 [m_image setScalesWhenResized:YES];
136 [m_image setSize: imageSize];
137 }
138 labelSize.width += imageSize.width;
139 return labelSize;
140}
141- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
142{
143 if(m_image)
144 {
145 NSSize imageSize = [m_image size];
146 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
147 tabRect.origin.y+imageSize.height)
148 operation:NSCompositeSourceOver];
149 tabRect.size.width -= imageSize.width;
150 tabRect.origin.x += imageSize.width;
151 }
152 [super drawLabel:shouldTruncateLabel inRect:tabRect];
153}
154- (NSImage*)image
155{
156 return m_image;
157}
158- (void)setImage:(NSImage*)image
159{
160 [image retain];
161 [m_image release];
162 m_image = image;
163 if(!m_image)
164 return;
165 [[NSPasteboard generalPasteboard]
166 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
167 owner:nil];
168 [[NSPasteboard generalPasteboard]
169 setData:[m_image TIFFRepresentation]
170 forType:NSTIFFPboardType];
171}
172@end // implementation WXCTabViewImageItem : NSTabViewItem
173
174
dbeddfb9
SC
175class wxCocoaTabView : public wxWidgetCocoaImpl
176{
177public:
178 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
179 {
180 }
03647350
VZ
181
182 void GetContentArea( int &left , int &top , int &width , int &height ) const
dbeddfb9
SC
183 {
184 wxNSTabView* slf = (wxNSTabView*) m_osxView;
185 NSRect r = [slf contentRect];
e490b0d2
VZ
186 left = (int)r.origin.x;
187 top = (int)r.origin.y;
188 width = (int)r.size.width;
189 height = (int)r.size.height;
dbeddfb9 190 }
03647350
VZ
191
192 void SetValue( wxInt32 value )
dbeddfb9
SC
193 {
194 wxNSTabView* slf = (wxNSTabView*) m_osxView;
195 // avoid 'changed' events when setting the tab programmatically
196 wxTabViewController* controller = [slf delegate];
197 [slf setDelegate:nil];
198 [slf selectTabViewItemAtIndex:(value-1)];
199 [slf setDelegate:controller];
200 }
03647350 201
dbeddfb9
SC
202 wxInt32 GetValue() const
203 {
204 wxNSTabView* slf = (wxNSTabView*) m_osxView;
205 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
206 if ( selectedItem == nil )
207 return 0;
208 else
209 return [slf indexOfTabViewItem:selectedItem]+1;
210 }
03647350 211
dbeddfb9
SC
212 void SetMaximum( wxInt32 maximum )
213 {
214 wxNSTabView* slf = (wxNSTabView*) m_osxView;
215 int cocoacount = [slf numberOfTabViewItems ];
216 // avoid 'changed' events when setting the tab programmatically
217 wxTabViewController* controller = [slf delegate];
218 [slf setDelegate:nil];
03647350 219
dbeddfb9
SC
220 if ( maximum > cocoacount )
221 {
222 for ( int i = cocoacount ; i < maximum ; ++i )
223 {
294de5ca 224 NSTabViewItem* item = [[WXCTabViewImageItem alloc] init];
dbeddfb9
SC
225 [slf addTabViewItem:item];
226 [item release];
227 }
228 }
229 else if ( maximum < cocoacount )
230 {
231 for ( int i = cocoacount -1 ; i >= maximum ; --i )
232 {
233 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
234 [slf removeTabViewItem:item];
235 }
236 }
237 [slf setDelegate:controller];
238 }
239
240 void SetupTabs( const wxNotebook& notebook)
241 {
242 int pcount = notebook.GetPageCount();
03647350 243
dbeddfb9 244 SetMaximum( pcount );
03647350 245
dbeddfb9
SC
246 for ( int i = 0 ; i < pcount ; ++i )
247 {
248 wxNotebookPage* page = notebook.GetPage(i);
249 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
250 [item setView:page->GetHandle() ];
251 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
252 [item setLabel:cf.AsNSString()];
253 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
254 {
255 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
a1b806b9 256 if ( bmap.IsOk() )
dbeddfb9 257 {
294de5ca 258 [(WXCTabViewImageItem*) item setImage: bmap.GetNSImage()];
dbeddfb9
SC
259 }
260 }
261 }
262 }
005659f0
SC
263
264 int TabHitTest(const wxPoint & pt, long* flags)
265 {
266 int retval = wxNOT_FOUND;
267
268 NSPoint nspt = wxToNSPoint( m_osxView, pt );
269
270 wxNSTabView* slf = (wxNSTabView*) m_osxView;
271
272 NSTabViewItem* hitItem = [slf tabViewItemAtPoint:nspt];
273
274 if (!hitItem) {
789e9abb
VZ
275 if ( flags )
276 *flags = wxBK_HITTEST_NOWHERE;
005659f0
SC
277 } else {
278 retval = [slf indexOfTabViewItem:hitItem];
789e9abb
VZ
279 if ( flags )
280 *flags = wxBK_HITTEST_ONLABEL;
005659f0
SC
281 }
282
283 return retval;
284 }
dbeddfb9
SC
285};
286
287
f033830e
SC
288/*
289#if 0
290 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
291
292 if ( bounds.right <= bounds.left )
293 bounds.right = bounds.left + 100;
294 if ( bounds.bottom <= bounds.top )
295 bounds.bottom = bounds.top + 100;
296
297 UInt16 tabstyle = kControlTabDirectionNorth;
298 if ( HasFlag(wxBK_LEFT) )
299 tabstyle = kControlTabDirectionWest;
300 else if ( HasFlag( wxBK_RIGHT ) )
301 tabstyle = kControlTabDirectionEast;
302 else if ( HasFlag( wxBK_BOTTOM ) )
303 tabstyle = kControlTabDirectionSouth;
304
305 ControlTabSize tabsize;
306 switch (GetWindowVariant())
307 {
308 case wxWINDOW_VARIANT_MINI:
309 tabsize = 3 ;
310 break;
311
312 case wxWINDOW_VARIANT_SMALL:
313 tabsize = kControlTabSizeSmall;
314 break;
315
316 default:
317 tabsize = kControlTabSizeLarge;
318 break;
319 }
320
321 m_peer = new wxMacControl( this );
322 OSStatus err = CreateTabsControl(
323 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
22756322 324 tabsize, tabstyle, 0, NULL, GetPeer()->GetControlRefAddr() );
f033830e
SC
325 verify_noerr( err );
326#endif
327*/
03647350
VZ
328wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
329 wxWindowMac* WXUNUSED(parent),
330 wxWindowID WXUNUSED(id),
331 const wxPoint& pos,
f033830e 332 const wxSize& size,
03647350 333 long style,
d8207702 334 long WXUNUSED(extraStyle))
f033830e 335{
dbeddfb9
SC
336 static wxTabViewController* controller = NULL;
337
338 if ( !controller )
339 controller =[[wxTabViewController alloc] init];
340
dbeddfb9 341 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
03647350 342
f033830e 343 NSTabViewType tabstyle = NSTopTabsBezelBorder;
dbeddfb9 344 if ( style & wxBK_LEFT )
f033830e
SC
345 tabstyle = NSLeftTabsBezelBorder;
346 else if ( style & wxBK_RIGHT )
347 tabstyle = NSRightTabsBezelBorder;
348 else if ( style & wxBK_BOTTOM )
349 tabstyle = NSBottomTabsBezelBorder;
03647350 350
f033830e 351 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
f033830e 352 [v setTabViewType:tabstyle];
dbeddfb9 353 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
dbeddfb9 354 [v setDelegate: controller];
f033830e
SC
355 return c;
356}
357
f033830e 358#endif