1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/notebook.mm
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/notebook.h"
18 #include "wx/string.h"
24 #include "wx/string.h"
25 #include "wx/imaglist.h"
26 #include "wx/osx/private.h"
32 @interface wxTabViewController : NSObject wxOSX_10_6_AND_LATER(<NSTabViewDelegate>)
36 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
37 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
41 @interface wxNSTabView : NSTabView
47 @implementation wxTabViewController
55 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
57 wxUnusedVar(tabViewItem);
58 wxNSTabView* view = (wxNSTabView*) tabView;
59 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
63 // wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
68 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
70 wxUnusedVar(tabViewItem);
71 wxNSTabView* view = (wxNSTabView*) tabView;
72 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
75 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
76 wxpeer->OSXHandleClicked(0);
82 @implementation wxNSTabView
86 static BOOL initialized = NO;
90 wxOSXCocoaClassAddWXMethods( self );
96 // ========================================================================
97 // WXCTabViewImageItem
98 // ========================================================================
99 @interface WXCTabViewImageItem : NSTabViewItem
105 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
106 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
108 - (void)setImage:(NSImage*)image;
109 @end // interface WXCTabViewImageItem : NSTabViewItem
112 @implementation WXCTabViewImageItem : NSTabViewItem
116 return [super initWithIdentifier:nil];
123 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
125 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
128 NSSize imageSize = [m_image size];
130 if(imageSize.height > labelSize.height)
132 imageSize.width *= labelSize.height/imageSize.height;
133 imageSize.height *= labelSize.height/imageSize.height;
134 [m_image setScalesWhenResized:YES];
135 [m_image setSize: imageSize];
137 labelSize.width += imageSize.width;
140 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
144 NSSize imageSize = [m_image size];
145 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
146 tabRect.origin.y+imageSize.height)
147 operation:NSCompositeSourceOver];
148 tabRect.size.width -= imageSize.width;
149 tabRect.origin.x += imageSize.width;
151 [super drawLabel:shouldTruncateLabel inRect:tabRect];
157 - (void)setImage:(NSImage*)image
164 [[NSPasteboard generalPasteboard]
165 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
167 [[NSPasteboard generalPasteboard]
168 setData:[m_image TIFFRepresentation]
169 forType:NSTIFFPboardType];
171 @end // implementation WXCTabViewImageItem : NSTabViewItem
174 class wxCocoaTabView : public wxWidgetCocoaImpl
177 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
181 void GetContentArea( int &left , int &top , int &width , int &height ) const
183 wxNSTabView* slf = (wxNSTabView*) m_osxView;
184 NSRect r = [slf contentRect];
185 left = (int)r.origin.x;
186 top = (int)r.origin.y;
187 width = (int)r.size.width;
188 height = (int)r.size.height;
191 void SetValue( wxInt32 value )
193 wxNSTabView* slf = (wxNSTabView*) m_osxView;
194 // avoid 'changed' events when setting the tab programmatically
195 wxTabViewController* controller = [slf delegate];
196 [slf setDelegate:nil];
197 [slf selectTabViewItemAtIndex:(value-1)];
198 [slf setDelegate:controller];
201 wxInt32 GetValue() const
203 wxNSTabView* slf = (wxNSTabView*) m_osxView;
204 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
205 if ( selectedItem == nil )
208 return [slf indexOfTabViewItem:selectedItem]+1;
211 void SetMaximum( wxInt32 maximum )
213 wxNSTabView* slf = (wxNSTabView*) m_osxView;
214 int cocoacount = [slf numberOfTabViewItems ];
215 // avoid 'changed' events when setting the tab programmatically
216 wxTabViewController* controller = [slf delegate];
217 [slf setDelegate:nil];
219 if ( maximum > cocoacount )
221 for ( int i = cocoacount ; i < maximum ; ++i )
223 NSTabViewItem* item = [[WXCTabViewImageItem alloc] init];
224 [slf addTabViewItem:item];
228 else if ( maximum < cocoacount )
230 for ( int i = cocoacount -1 ; i >= maximum ; --i )
232 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
233 [slf removeTabViewItem:item];
236 [slf setDelegate:controller];
239 void SetupTabs( const wxNotebook& notebook)
241 int pcount = notebook.GetPageCount();
243 SetMaximum( pcount );
245 for ( int i = 0 ; i < pcount ; ++i )
247 wxNotebookPage* page = notebook.GetPage(i);
248 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
249 [item setView:page->GetHandle() ];
250 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
251 [item setLabel:cf.AsNSString()];
252 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
254 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
257 [(WXCTabViewImageItem*) item setImage: bmap.GetNSImage()];
263 int TabHitTest(const wxPoint & pt, long* flags)
265 int retval = wxNOT_FOUND;
267 NSPoint nspt = wxToNSPoint( m_osxView, pt );
269 wxNSTabView* slf = (wxNSTabView*) m_osxView;
271 NSTabViewItem* hitItem = [slf tabViewItemAtPoint:nspt];
275 *flags = wxBK_HITTEST_NOWHERE;
277 retval = [slf indexOfTabViewItem:hitItem];
279 *flags = wxBK_HITTEST_ONLABEL;
289 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
291 if ( bounds.right <= bounds.left )
292 bounds.right = bounds.left + 100;
293 if ( bounds.bottom <= bounds.top )
294 bounds.bottom = bounds.top + 100;
296 UInt16 tabstyle = kControlTabDirectionNorth;
297 if ( HasFlag(wxBK_LEFT) )
298 tabstyle = kControlTabDirectionWest;
299 else if ( HasFlag( wxBK_RIGHT ) )
300 tabstyle = kControlTabDirectionEast;
301 else if ( HasFlag( wxBK_BOTTOM ) )
302 tabstyle = kControlTabDirectionSouth;
304 ControlTabSize tabsize;
305 switch (GetWindowVariant())
307 case wxWINDOW_VARIANT_MINI:
311 case wxWINDOW_VARIANT_SMALL:
312 tabsize = kControlTabSizeSmall;
316 tabsize = kControlTabSizeLarge;
320 m_peer = new wxMacControl( this );
321 OSStatus err = CreateTabsControl(
322 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
323 tabsize, tabstyle, 0, NULL, GetPeer()->GetControlRefAddr() );
327 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
328 wxWindowMac* WXUNUSED(parent),
329 wxWindowID WXUNUSED(id),
333 long WXUNUSED(extraStyle))
335 static wxTabViewController* controller = NULL;
338 controller =[[wxTabViewController alloc] init];
340 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
342 NSTabViewType tabstyle = NSTopTabsBezelBorder;
343 if ( style & wxBK_LEFT )
344 tabstyle = NSLeftTabsBezelBorder;
345 else if ( style & wxBK_RIGHT )
346 tabstyle = NSRightTabsBezelBorder;
347 else if ( style & wxBK_BOTTOM )
348 tabstyle = NSBottomTabsBezelBorder;
350 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
351 [v setTabViewType:tabstyle];
352 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
353 [v setDelegate: controller];