1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/notebook.mm
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: notebmac.cpp 55079 2008-08-13 14:56:42Z PC $
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
37 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
38 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
42 @interface wxNSTabView : NSTabView
44 wxWidgetCocoaImpl* impl;
47 - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation;
48 - (wxWidgetCocoaImpl*) implementation;
53 @implementation wxTabViewController
61 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
63 wxNSTabView* view = (wxNSTabView*) tabView;
64 wxWidgetCocoaImpl* viewimpl = [view implementation];
67 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
72 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
75 wxNSTabView* view = (wxNSTabView*) tabView;
76 wxWidgetCocoaImpl* viewimpl = [view implementation];
79 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
80 wxpeer->HandleClicked(0);
86 @implementation wxNSTabView
88 - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation
90 impl = theImplementation;
93 - (wxWidgetCocoaImpl*) implementation
105 class wxCocoaTabView : public wxWidgetCocoaImpl
108 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
112 void GetContentArea( int &left , int &top , int &width , int &height ) const
114 wxNSTabView* slf = (wxNSTabView*) m_osxView;
115 NSRect r = [slf contentRect];
118 width = r.size.width;
119 height = r.size.height;
122 void SetValue( wxInt32 value )
124 wxNSTabView* slf = (wxNSTabView*) m_osxView;
125 // avoid 'changed' events when setting the tab programmatically
126 wxTabViewController* controller = [slf delegate];
127 [slf setDelegate:nil];
128 [slf selectTabViewItemAtIndex:(value-1)];
129 [slf setDelegate:controller];
132 wxInt32 GetValue() const
134 wxNSTabView* slf = (wxNSTabView*) m_osxView;
135 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
136 if ( selectedItem == nil )
139 return [slf indexOfTabViewItem:selectedItem]+1;
142 void SetMaximum( wxInt32 maximum )
144 wxNSTabView* slf = (wxNSTabView*) m_osxView;
145 int cocoacount = [slf numberOfTabViewItems ];
146 // avoid 'changed' events when setting the tab programmatically
147 wxTabViewController* controller = [slf delegate];
148 [slf setDelegate:nil];
150 if ( maximum > cocoacount )
152 for ( int i = cocoacount ; i < maximum ; ++i )
154 NSTabViewItem* item = [[NSTabViewItem alloc] init];
155 [slf addTabViewItem:item];
159 else if ( maximum < cocoacount )
161 for ( int i = cocoacount -1 ; i >= maximum ; --i )
163 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
164 [slf removeTabViewItem:item];
167 [slf setDelegate:controller];
170 void SetupTabs( const wxNotebook& notebook)
172 int pcount = notebook.GetPageCount();
174 SetMaximum( pcount );
176 for ( int i = 0 ; i < pcount ; ++i )
178 wxNotebookPage* page = notebook.GetPage(i);
179 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
180 [item setView:page->GetHandle() ];
181 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
182 [item setLabel:cf.AsNSString()];
183 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
185 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
188 // TODO how to set an image on a tab
198 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
200 if ( bounds.right <= bounds.left )
201 bounds.right = bounds.left + 100;
202 if ( bounds.bottom <= bounds.top )
203 bounds.bottom = bounds.top + 100;
205 UInt16 tabstyle = kControlTabDirectionNorth;
206 if ( HasFlag(wxBK_LEFT) )
207 tabstyle = kControlTabDirectionWest;
208 else if ( HasFlag( wxBK_RIGHT ) )
209 tabstyle = kControlTabDirectionEast;
210 else if ( HasFlag( wxBK_BOTTOM ) )
211 tabstyle = kControlTabDirectionSouth;
213 ControlTabSize tabsize;
214 switch (GetWindowVariant())
216 case wxWINDOW_VARIANT_MINI:
220 case wxWINDOW_VARIANT_SMALL:
221 tabsize = kControlTabSizeSmall;
225 tabsize = kControlTabSizeLarge;
229 m_peer = new wxMacControl( this );
230 OSStatus err = CreateTabsControl(
231 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
232 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
236 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
244 static wxTabViewController* controller = NULL;
247 controller =[[wxTabViewController alloc] init];
249 NSView* sv = (wxpeer->GetParent()->GetHandle() );
251 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
253 NSTabViewType tabstyle = NSTopTabsBezelBorder;
254 if ( style & wxBK_LEFT )
255 tabstyle = NSLeftTabsBezelBorder;
256 else if ( style & wxBK_RIGHT )
257 tabstyle = NSRightTabsBezelBorder;
258 else if ( style & wxBK_BOTTOM )
259 tabstyle = NSBottomTabsBezelBorder;
261 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
263 [v setTabViewType:tabstyle];
264 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
265 [v setImplementation:c];
266 [v setDelegate: controller];