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 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;
72 wxUnusedVar(tabViewItem);
73 wxNSTabView* view = (wxNSTabView*) tabView;
74 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
77 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
78 wxpeer->OSXHandleClicked(0);
84 @implementation wxNSTabView
88 static BOOL initialized = NO;
92 wxOSXCocoaClassAddWXMethods( self );
98 class wxCocoaTabView : public wxWidgetCocoaImpl
101 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
105 void GetContentArea( int &left , int &top , int &width , int &height ) const
107 wxNSTabView* slf = (wxNSTabView*) m_osxView;
108 NSRect r = [slf contentRect];
111 width = r.size.width;
112 height = r.size.height;
115 void SetValue( wxInt32 value )
117 wxNSTabView* slf = (wxNSTabView*) m_osxView;
118 // avoid 'changed' events when setting the tab programmatically
119 wxTabViewController* controller = [slf delegate];
120 [slf setDelegate:nil];
121 [slf selectTabViewItemAtIndex:(value-1)];
122 [slf setDelegate:controller];
125 wxInt32 GetValue() const
127 wxNSTabView* slf = (wxNSTabView*) m_osxView;
128 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
129 if ( selectedItem == nil )
132 return [slf indexOfTabViewItem:selectedItem]+1;
135 void SetMaximum( wxInt32 maximum )
137 wxNSTabView* slf = (wxNSTabView*) m_osxView;
138 int cocoacount = [slf numberOfTabViewItems ];
139 // avoid 'changed' events when setting the tab programmatically
140 wxTabViewController* controller = [slf delegate];
141 [slf setDelegate:nil];
143 if ( maximum > cocoacount )
145 for ( int i = cocoacount ; i < maximum ; ++i )
147 NSTabViewItem* item = [[NSTabViewItem alloc] init];
148 [slf addTabViewItem:item];
152 else if ( maximum < cocoacount )
154 for ( int i = cocoacount -1 ; i >= maximum ; --i )
156 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
157 [slf removeTabViewItem:item];
160 [slf setDelegate:controller];
163 void SetupTabs( const wxNotebook& notebook)
165 int pcount = notebook.GetPageCount();
167 SetMaximum( pcount );
169 for ( int i = 0 ; i < pcount ; ++i )
171 wxNotebookPage* page = notebook.GetPage(i);
172 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
173 [item setView:page->GetHandle() ];
174 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
175 [item setLabel:cf.AsNSString()];
176 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
178 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
181 // TODO how to set an image on a tab
191 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
193 if ( bounds.right <= bounds.left )
194 bounds.right = bounds.left + 100;
195 if ( bounds.bottom <= bounds.top )
196 bounds.bottom = bounds.top + 100;
198 UInt16 tabstyle = kControlTabDirectionNorth;
199 if ( HasFlag(wxBK_LEFT) )
200 tabstyle = kControlTabDirectionWest;
201 else if ( HasFlag( wxBK_RIGHT ) )
202 tabstyle = kControlTabDirectionEast;
203 else if ( HasFlag( wxBK_BOTTOM ) )
204 tabstyle = kControlTabDirectionSouth;
206 ControlTabSize tabsize;
207 switch (GetWindowVariant())
209 case wxWINDOW_VARIANT_MINI:
213 case wxWINDOW_VARIANT_SMALL:
214 tabsize = kControlTabSizeSmall;
218 tabsize = kControlTabSizeLarge;
222 m_peer = new wxMacControl( this );
223 OSStatus err = CreateTabsControl(
224 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
225 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
229 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
230 wxWindowMac* WXUNUSED(parent),
231 wxWindowID WXUNUSED(id),
235 long WXUNUSED(extraStyle))
237 static wxTabViewController* controller = NULL;
240 controller =[[wxTabViewController alloc] init];
242 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
244 NSTabViewType tabstyle = NSTopTabsBezelBorder;
245 if ( style & wxBK_LEFT )
246 tabstyle = NSLeftTabsBezelBorder;
247 else if ( style & wxBK_RIGHT )
248 tabstyle = NSRightTabsBezelBorder;
249 else if ( style & wxBK_BOTTOM )
250 tabstyle = NSBottomTabsBezelBorder;
252 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
253 [v setTabViewType:tabstyle];
254 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
255 [v setDelegate: controller];