1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/notebook.mm
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/notebook.h"
19 #include "wx/string.h"
25 #include "wx/string.h"
26 #include "wx/imaglist.h"
27 #include "wx/osx/private.h"
33 @interface wxTabViewController : NSObject wxOSX_10_6_AND_LATER(<NSTabViewDelegate>)
37 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
38 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
42 @interface wxNSTabView : NSTabView
48 @implementation wxTabViewController
56 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
58 wxUnusedVar(tabViewItem);
59 wxNSTabView* view = (wxNSTabView*) tabView;
60 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
64 // wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
69 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
71 wxUnusedVar(tabViewItem);
72 wxNSTabView* view = (wxNSTabView*) tabView;
73 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
76 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
77 wxpeer->OSXHandleClicked(0);
83 @implementation wxNSTabView
87 static BOOL initialized = NO;
91 wxOSXCocoaClassAddWXMethods( self );
97 // ========================================================================
98 // WXCTabViewImageItem
99 // ========================================================================
100 @interface WXCTabViewImageItem : NSTabViewItem
106 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
107 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
109 - (void)setImage:(NSImage*)image;
110 @end // interface WXCTabViewImageItem : NSTabViewItem
113 @implementation WXCTabViewImageItem : NSTabViewItem
117 return [super initWithIdentifier:nil];
124 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
126 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
129 NSSize imageSize = [m_image size];
131 if(imageSize.height > labelSize.height)
133 imageSize.width *= labelSize.height/imageSize.height;
134 imageSize.height *= labelSize.height/imageSize.height;
135 [m_image setScalesWhenResized:YES];
136 [m_image setSize: imageSize];
138 labelSize.width += imageSize.width;
141 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
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;
152 [super drawLabel:shouldTruncateLabel inRect:tabRect];
158 - (void)setImage:(NSImage*)image
165 [[NSPasteboard generalPasteboard]
166 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
168 [[NSPasteboard generalPasteboard]
169 setData:[m_image TIFFRepresentation]
170 forType:NSTIFFPboardType];
172 @end // implementation WXCTabViewImageItem : NSTabViewItem
175 class wxCocoaTabView : public wxWidgetCocoaImpl
178 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
182 void GetContentArea( int &left , int &top , int &width , int &height ) const
184 wxNSTabView* slf = (wxNSTabView*) m_osxView;
185 NSRect r = [slf contentRect];
186 left = (int)r.origin.x;
187 top = (int)r.origin.y;
188 width = (int)r.size.width;
189 height = (int)r.size.height;
192 void SetValue( wxInt32 value )
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];
202 wxInt32 GetValue() const
204 wxNSTabView* slf = (wxNSTabView*) m_osxView;
205 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
206 if ( selectedItem == nil )
209 return [slf indexOfTabViewItem:selectedItem]+1;
212 void SetMaximum( wxInt32 maximum )
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];
220 if ( maximum > cocoacount )
222 for ( int i = cocoacount ; i < maximum ; ++i )
224 NSTabViewItem* item = [[WXCTabViewImageItem alloc] init];
225 [slf addTabViewItem:item];
229 else if ( maximum < cocoacount )
231 for ( int i = cocoacount -1 ; i >= maximum ; --i )
233 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
234 [slf removeTabViewItem:item];
237 [slf setDelegate:controller];
240 void SetupTabs( const wxNotebook& notebook)
242 int pcount = notebook.GetPageCount();
244 SetMaximum( pcount );
246 for ( int i = 0 ; i < pcount ; ++i )
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 )
255 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
258 [(WXCTabViewImageItem*) item setImage: bmap.GetNSImage()];
264 int TabHitTest(const wxPoint & pt, long* flags)
266 int retval = wxNOT_FOUND;
268 NSPoint nspt = wxToNSPoint( m_osxView, pt );
270 wxNSTabView* slf = (wxNSTabView*) m_osxView;
272 NSTabViewItem* hitItem = [slf tabViewItemAtPoint:nspt];
276 *flags = wxBK_HITTEST_NOWHERE;
278 retval = [slf indexOfTabViewItem:hitItem];
280 *flags = wxBK_HITTEST_ONLABEL;
290 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
292 if ( bounds.right <= bounds.left )
293 bounds.right = bounds.left + 100;
294 if ( bounds.bottom <= bounds.top )
295 bounds.bottom = bounds.top + 100;
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;
305 ControlTabSize tabsize;
306 switch (GetWindowVariant())
308 case wxWINDOW_VARIANT_MINI:
312 case wxWINDOW_VARIANT_SMALL:
313 tabsize = kControlTabSizeSmall;
317 tabsize = kControlTabSizeLarge;
321 m_peer = new wxMacControl( this );
322 OSStatus err = CreateTabsControl(
323 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
324 tabsize, tabstyle, 0, NULL, GetPeer()->GetControlRefAddr() );
328 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
329 wxWindowMac* WXUNUSED(parent),
330 wxWindowID WXUNUSED(id),
334 long WXUNUSED(extraStyle))
336 static wxTabViewController* controller = NULL;
339 controller =[[wxTabViewController alloc] init];
341 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
343 NSTabViewType tabstyle = NSTopTabsBezelBorder;
344 if ( style & wxBK_LEFT )
345 tabstyle = NSLeftTabsBezelBorder;
346 else if ( style & wxBK_RIGHT )
347 tabstyle = NSRightTabsBezelBorder;
348 else if ( style & wxBK_BOTTOM )
349 tabstyle = NSBottomTabsBezelBorder;
351 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
352 [v setTabViewType:tabstyle];
353 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
354 [v setDelegate: controller];