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 WXCOCOAIMPL_COMMON_MEMBERS
47 WXCOCOAIMPL_COMMON_INTERFACE
51 @implementation wxTabViewController
59 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
61 wxNSTabView* view = (wxNSTabView*) tabView;
62 wxWidgetCocoaImpl* viewimpl = [view implementation];
65 // wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
70 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
73 wxNSTabView* view = (wxNSTabView*) tabView;
74 wxWidgetCocoaImpl* viewimpl = [view implementation];
77 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
78 wxpeer->OSXHandleClicked(0);
84 @implementation wxNSTabView
86 WXCOCOAIMPL_COMMON_IMPLEMENTATION
90 class wxCocoaTabView : public wxWidgetCocoaImpl
93 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
97 void GetContentArea( int &left , int &top , int &width , int &height ) const
99 wxNSTabView* slf = (wxNSTabView*) m_osxView;
100 NSRect r = [slf contentRect];
103 width = r.size.width;
104 height = r.size.height;
107 void SetValue( wxInt32 value )
109 wxNSTabView* slf = (wxNSTabView*) m_osxView;
110 // avoid 'changed' events when setting the tab programmatically
111 wxTabViewController* controller = [slf delegate];
112 [slf setDelegate:nil];
113 [slf selectTabViewItemAtIndex:(value-1)];
114 [slf setDelegate:controller];
117 wxInt32 GetValue() const
119 wxNSTabView* slf = (wxNSTabView*) m_osxView;
120 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
121 if ( selectedItem == nil )
124 return [slf indexOfTabViewItem:selectedItem]+1;
127 void SetMaximum( wxInt32 maximum )
129 wxNSTabView* slf = (wxNSTabView*) m_osxView;
130 int cocoacount = [slf numberOfTabViewItems ];
131 // avoid 'changed' events when setting the tab programmatically
132 wxTabViewController* controller = [slf delegate];
133 [slf setDelegate:nil];
135 if ( maximum > cocoacount )
137 for ( int i = cocoacount ; i < maximum ; ++i )
139 NSTabViewItem* item = [[NSTabViewItem alloc] init];
140 [slf addTabViewItem:item];
144 else if ( maximum < cocoacount )
146 for ( int i = cocoacount -1 ; i >= maximum ; --i )
148 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
149 [slf removeTabViewItem:item];
152 [slf setDelegate:controller];
155 void SetupTabs( const wxNotebook& notebook)
157 int pcount = notebook.GetPageCount();
159 SetMaximum( pcount );
161 for ( int i = 0 ; i < pcount ; ++i )
163 wxNotebookPage* page = notebook.GetPage(i);
164 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
165 [item setView:page->GetHandle() ];
166 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
167 [item setLabel:cf.AsNSString()];
168 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
170 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
173 // TODO how to set an image on a tab
183 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
185 if ( bounds.right <= bounds.left )
186 bounds.right = bounds.left + 100;
187 if ( bounds.bottom <= bounds.top )
188 bounds.bottom = bounds.top + 100;
190 UInt16 tabstyle = kControlTabDirectionNorth;
191 if ( HasFlag(wxBK_LEFT) )
192 tabstyle = kControlTabDirectionWest;
193 else if ( HasFlag( wxBK_RIGHT ) )
194 tabstyle = kControlTabDirectionEast;
195 else if ( HasFlag( wxBK_BOTTOM ) )
196 tabstyle = kControlTabDirectionSouth;
198 ControlTabSize tabsize;
199 switch (GetWindowVariant())
201 case wxWINDOW_VARIANT_MINI:
205 case wxWINDOW_VARIANT_SMALL:
206 tabsize = kControlTabSizeSmall;
210 tabsize = kControlTabSizeLarge;
214 m_peer = new wxMacControl( this );
215 OSStatus err = CreateTabsControl(
216 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
217 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
221 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
229 static wxTabViewController* controller = NULL;
232 controller =[[wxTabViewController alloc] init];
234 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
236 NSTabViewType tabstyle = NSTopTabsBezelBorder;
237 if ( style & wxBK_LEFT )
238 tabstyle = NSLeftTabsBezelBorder;
239 else if ( style & wxBK_RIGHT )
240 tabstyle = NSRightTabsBezelBorder;
241 else if ( style & wxBK_BOTTOM )
242 tabstyle = NSBottomTabsBezelBorder;
244 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
245 [v setTabViewType:tabstyle];
246 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
247 [v setImplementation:c];
248 [v setDelegate: controller];