]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/notebook.mm
moving embedding to common API
[wxWidgets.git] / src / osx / cocoa / notebook.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/notebook.mm
3 // Purpose: implementation of wxNotebook
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: notebmac.cpp 55079 2008-08-13 14:56:42Z PC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_NOTEBOOK
15
16 #include "wx/notebook.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/string.h"
20 #include "wx/log.h"
21 #include "wx/app.h"
22 #include "wx/image.h"
23 #endif
24
25 #include "wx/string.h"
26 #include "wx/imaglist.h"
27 #include "wx/osx/private.h"
28
29 //
30 // controller
31 //
32
33 @interface wxTabViewController : NSObject
34 {
35 }
36
37 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
38 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
39
40 @end
41
42 @interface wxNSTabView : NSTabView
43 {
44 wxWidgetCocoaImpl* impl;
45 }
46
47 - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation;
48 - (wxWidgetCocoaImpl*) implementation;
49 - (BOOL) isFlipped;
50
51 @end
52
53 @implementation wxTabViewController
54
55 - (id) init
56 {
57 [super init];
58 return self;
59 }
60
61 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
62 {
63 wxNSTabView* view = (wxNSTabView*) tabView;
64 wxWidgetCocoaImpl* viewimpl = [view implementation];
65 if ( viewimpl )
66 {
67 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
68 }
69 return YES;
70 }
71
72 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
73
74 {
75 wxNSTabView* view = (wxNSTabView*) tabView;
76 wxWidgetCocoaImpl* viewimpl = [view implementation];
77 if ( viewimpl )
78 {
79 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
80 wxpeer->HandleClicked(0);
81 }
82 }
83
84 @end
85
86 @implementation wxNSTabView
87
88 - (void)setImplementation: (wxWidgetCocoaImpl *) theImplementation
89 {
90 impl = theImplementation;
91 }
92
93 - (wxWidgetCocoaImpl*) implementation
94 {
95 return impl;
96 }
97
98 - (BOOL) isFlipped
99 {
100 return YES;
101 }
102
103 @end
104
105 class wxCocoaTabView : public wxWidgetCocoaImpl
106 {
107 public:
108 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
109 {
110 }
111
112 void GetContentArea( int &left , int &top , int &width , int &height ) const
113 {
114 wxNSTabView* slf = (wxNSTabView*) m_osxView;
115 NSRect r = [slf contentRect];
116 left = r.origin.x;
117 top = r.origin.y;
118 width = r.size.width;
119 height = r.size.height;
120 }
121
122 void SetValue( wxInt32 value )
123 {
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];
130 }
131
132 wxInt32 GetValue() const
133 {
134 wxNSTabView* slf = (wxNSTabView*) m_osxView;
135 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
136 if ( selectedItem == nil )
137 return 0;
138 else
139 return [slf indexOfTabViewItem:selectedItem]+1;
140 }
141
142 void SetMaximum( wxInt32 maximum )
143 {
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];
149
150 if ( maximum > cocoacount )
151 {
152 for ( int i = cocoacount ; i < maximum ; ++i )
153 {
154 NSTabViewItem* item = [[NSTabViewItem alloc] init];
155 [slf addTabViewItem:item];
156 [item release];
157 }
158 }
159 else if ( maximum < cocoacount )
160 {
161 for ( int i = cocoacount -1 ; i >= maximum ; --i )
162 {
163 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
164 [slf removeTabViewItem:item];
165 }
166 }
167 [slf setDelegate:controller];
168 }
169
170 void SetupTabs( const wxNotebook& notebook)
171 {
172 int pcount = notebook.GetPageCount();
173
174 SetMaximum( pcount );
175
176 for ( int i = 0 ; i < pcount ; ++i )
177 {
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 )
184 {
185 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
186 if ( bmap.Ok() )
187 {
188 // TODO how to set an image on a tab
189 }
190 }
191 }
192 }
193 };
194
195
196 /*
197 #if 0
198 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
199
200 if ( bounds.right <= bounds.left )
201 bounds.right = bounds.left + 100;
202 if ( bounds.bottom <= bounds.top )
203 bounds.bottom = bounds.top + 100;
204
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;
212
213 ControlTabSize tabsize;
214 switch (GetWindowVariant())
215 {
216 case wxWINDOW_VARIANT_MINI:
217 tabsize = 3 ;
218 break;
219
220 case wxWINDOW_VARIANT_SMALL:
221 tabsize = kControlTabSizeSmall;
222 break;
223
224 default:
225 tabsize = kControlTabSizeLarge;
226 break;
227 }
228
229 m_peer = new wxMacControl( this );
230 OSStatus err = CreateTabsControl(
231 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
232 tabsize, tabstyle, 0, NULL, m_peer->GetControlRefAddr() );
233 verify_noerr( err );
234 #endif
235 */
236 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
237 wxWindowMac* parent,
238 wxWindowID id,
239 const wxPoint& pos,
240 const wxSize& size,
241 long style,
242 long extraStyle)
243 {
244 static wxTabViewController* controller = NULL;
245
246 if ( !controller )
247 controller =[[wxTabViewController alloc] init];
248
249 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
250
251 NSTabViewType tabstyle = NSTopTabsBezelBorder;
252 if ( style & wxBK_LEFT )
253 tabstyle = NSLeftTabsBezelBorder;
254 else if ( style & wxBK_RIGHT )
255 tabstyle = NSRightTabsBezelBorder;
256 else if ( style & wxBK_BOTTOM )
257 tabstyle = NSBottomTabsBezelBorder;
258
259 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
260 [v setTabViewType:tabstyle];
261 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
262 [v setImplementation:c];
263 [v setDelegate: controller];
264 return c;
265 }
266
267 #endif