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
48 @implementation wxTabViewController
56 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)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;
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 class wxCocoaTabView : public wxWidgetCocoaImpl
99 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
103 void GetContentArea( int &left , int &top , int &width , int &height ) const
105 wxNSTabView* slf = (wxNSTabView*) m_osxView;
106 NSRect r = [slf contentRect];
109 width = r.size.width;
110 height = r.size.height;
113 void SetValue( wxInt32 value )
115 wxNSTabView* slf = (wxNSTabView*) m_osxView;
116 // avoid 'changed' events when setting the tab programmatically
117 wxTabViewController* controller = [slf delegate];
118 [slf setDelegate:nil];
119 [slf selectTabViewItemAtIndex:(value-1)];
120 [slf setDelegate:controller];
123 wxInt32 GetValue() const
125 wxNSTabView* slf = (wxNSTabView*) m_osxView;
126 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
127 if ( selectedItem == nil )
130 return [slf indexOfTabViewItem:selectedItem]+1;
133 void SetMaximum( wxInt32 maximum )
135 wxNSTabView* slf = (wxNSTabView*) m_osxView;
136 int cocoacount = [slf numberOfTabViewItems ];
137 // avoid 'changed' events when setting the tab programmatically
138 wxTabViewController* controller = [slf delegate];
139 [slf setDelegate:nil];
141 if ( maximum > cocoacount )
143 for ( int i = cocoacount ; i < maximum ; ++i )
145 NSTabViewItem* item = [[NSTabViewItem alloc] init];
146 [slf addTabViewItem:item];
150 else if ( maximum < cocoacount )
152 for ( int i = cocoacount -1 ; i >= maximum ; --i )
154 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
155 [slf removeTabViewItem:item];
158 [slf setDelegate:controller];
161 void SetupTabs( const wxNotebook& notebook)
163 int pcount = notebook.GetPageCount();
165 SetMaximum( pcount );
167 for ( int i = 0 ; i < pcount ; ++i )
169 wxNotebookPage* page = notebook.GetPage(i);
170 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
171 [item setView:page->GetHandle() ];
172 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
173 [item setLabel:cf.AsNSString()];
174 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
176 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
179 // TODO how to set an image on a tab
189 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
191 if ( bounds.right <= bounds.left )
192 bounds.right = bounds.left + 100;
193 if ( bounds.bottom <= bounds.top )
194 bounds.bottom = bounds.top + 100;
196 UInt16 tabstyle = kControlTabDirectionNorth;
197 if ( HasFlag(wxBK_LEFT) )
198 tabstyle = kControlTabDirectionWest;
199 else if ( HasFlag( wxBK_RIGHT ) )
200 tabstyle = kControlTabDirectionEast;
201 else if ( HasFlag( wxBK_BOTTOM ) )
202 tabstyle = kControlTabDirectionSouth;
204 ControlTabSize tabsize;
205 switch (GetWindowVariant())
207 case wxWINDOW_VARIANT_MINI:
211 case wxWINDOW_VARIANT_SMALL:
212 tabsize = kControlTabSizeSmall;
216 tabsize = kControlTabSizeLarge;
220 m_peer = new wxMacControl( this );
221 OSStatus err = CreateTabsControl(
222 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
223 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
227 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
235 static wxTabViewController* controller = NULL;
238 controller =[[wxTabViewController alloc] init];
240 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
242 NSTabViewType tabstyle = NSTopTabsBezelBorder;
243 if ( style & wxBK_LEFT )
244 tabstyle = NSLeftTabsBezelBorder;
245 else if ( style & wxBK_RIGHT )
246 tabstyle = NSRightTabsBezelBorder;
247 else if ( style & wxBK_BOTTOM )
248 tabstyle = NSBottomTabsBezelBorder;
250 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
251 [v setTabViewType:tabstyle];
252 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
253 [v setDelegate: controller];