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()];
268 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
270 if ( bounds.right <= bounds.left )
271 bounds.right = bounds.left + 100;
272 if ( bounds.bottom <= bounds.top )
273 bounds.bottom = bounds.top + 100;
275 UInt16 tabstyle = kControlTabDirectionNorth;
276 if ( HasFlag(wxBK_LEFT) )
277 tabstyle = kControlTabDirectionWest;
278 else if ( HasFlag( wxBK_RIGHT ) )
279 tabstyle = kControlTabDirectionEast;
280 else if ( HasFlag( wxBK_BOTTOM ) )
281 tabstyle = kControlTabDirectionSouth;
283 ControlTabSize tabsize;
284 switch (GetWindowVariant())
286 case wxWINDOW_VARIANT_MINI:
290 case wxWINDOW_VARIANT_SMALL:
291 tabsize = kControlTabSizeSmall;
295 tabsize = kControlTabSizeLarge;
299 m_peer = new wxMacControl( this );
300 OSStatus err = CreateTabsControl(
301 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
302 tabsize, tabstyle, 0, NULL, GetPeer()->GetControlRefAddr() );
306 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
307 wxWindowMac* WXUNUSED(parent),
308 wxWindowID WXUNUSED(id),
312 long WXUNUSED(extraStyle))
314 static wxTabViewController* controller = NULL;
317 controller =[[wxTabViewController alloc] init];
319 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
321 NSTabViewType tabstyle = NSTopTabsBezelBorder;
322 if ( style & wxBK_LEFT )
323 tabstyle = NSLeftTabsBezelBorder;
324 else if ( style & wxBK_RIGHT )
325 tabstyle = NSRightTabsBezelBorder;
326 else if ( style & wxBK_BOTTOM )
327 tabstyle = NSBottomTabsBezelBorder;
329 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
330 [v setTabViewType:tabstyle];
331 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
332 [v setDelegate: controller];