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 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 class wxCocoaTabView : public wxWidgetCocoaImpl
100 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
104 void GetContentArea( int &left , int &top , int &width , int &height ) const
106 wxNSTabView* slf = (wxNSTabView*) m_osxView;
107 NSRect r = [slf contentRect];
108 left = (int)r.origin.x;
109 top = (int)r.origin.y;
110 width = (int)r.size.width;
111 height = (int)r.size.height;
114 void SetValue( wxInt32 value )
116 wxNSTabView* slf = (wxNSTabView*) m_osxView;
117 // avoid 'changed' events when setting the tab programmatically
118 wxTabViewController* controller = [slf delegate];
119 [slf setDelegate:nil];
120 [slf selectTabViewItemAtIndex:(value-1)];
121 [slf setDelegate:controller];
124 wxInt32 GetValue() const
126 wxNSTabView* slf = (wxNSTabView*) m_osxView;
127 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
128 if ( selectedItem == nil )
131 return [slf indexOfTabViewItem:selectedItem]+1;
134 void SetMaximum( wxInt32 maximum )
136 wxNSTabView* slf = (wxNSTabView*) m_osxView;
137 int cocoacount = [slf numberOfTabViewItems ];
138 // avoid 'changed' events when setting the tab programmatically
139 wxTabViewController* controller = [slf delegate];
140 [slf setDelegate:nil];
142 if ( maximum > cocoacount )
144 for ( int i = cocoacount ; i < maximum ; ++i )
146 NSTabViewItem* item = [[NSTabViewItem alloc] init];
147 [slf addTabViewItem:item];
151 else if ( maximum < cocoacount )
153 for ( int i = cocoacount -1 ; i >= maximum ; --i )
155 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
156 [slf removeTabViewItem:item];
159 [slf setDelegate:controller];
162 void SetupTabs( const wxNotebook& notebook)
164 int pcount = notebook.GetPageCount();
166 SetMaximum( pcount );
168 for ( int i = 0 ; i < pcount ; ++i )
170 wxNotebookPage* page = notebook.GetPage(i);
171 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
172 [item setView:page->GetHandle() ];
173 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
174 [item setLabel:cf.AsNSString()];
175 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
177 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
180 // TODO how to set an image on a tab
190 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
192 if ( bounds.right <= bounds.left )
193 bounds.right = bounds.left + 100;
194 if ( bounds.bottom <= bounds.top )
195 bounds.bottom = bounds.top + 100;
197 UInt16 tabstyle = kControlTabDirectionNorth;
198 if ( HasFlag(wxBK_LEFT) )
199 tabstyle = kControlTabDirectionWest;
200 else if ( HasFlag( wxBK_RIGHT ) )
201 tabstyle = kControlTabDirectionEast;
202 else if ( HasFlag( wxBK_BOTTOM ) )
203 tabstyle = kControlTabDirectionSouth;
205 ControlTabSize tabsize;
206 switch (GetWindowVariant())
208 case wxWINDOW_VARIANT_MINI:
212 case wxWINDOW_VARIANT_SMALL:
213 tabsize = kControlTabSizeSmall;
217 tabsize = kControlTabSizeLarge;
221 m_peer = new wxMacControl( this );
222 OSStatus err = CreateTabsControl(
223 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
224 tabsize, tabstyle, 0, NULL, GetPeer()->GetControlRefAddr() );
228 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
229 wxWindowMac* WXUNUSED(parent),
230 wxWindowID WXUNUSED(id),
234 long WXUNUSED(extraStyle))
236 static wxTabViewController* controller = NULL;
239 controller =[[wxTabViewController alloc] init];
241 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
243 NSTabViewType tabstyle = NSTopTabsBezelBorder;
244 if ( style & wxBK_LEFT )
245 tabstyle = NSLeftTabsBezelBorder;
246 else if ( style & wxBK_RIGHT )
247 tabstyle = NSRightTabsBezelBorder;
248 else if ( style & wxBK_BOTTOM )
249 tabstyle = NSBottomTabsBezelBorder;
251 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
252 [v setTabViewType:tabstyle];
253 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
254 [v setDelegate: controller];