]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/notebook.mm
Fix horizontal mouse wheel scrolling in wxGTK.
[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 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_NOTEBOOK
14
15 #include "wx/notebook.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/string.h"
19 #include "wx/log.h"
20 #include "wx/app.h"
21 #include "wx/image.h"
22 #endif
23
24 #include "wx/string.h"
25 #include "wx/imaglist.h"
26 #include "wx/osx/private.h"
27
28 //
29 // controller
30 //
31
32 @interface wxTabViewController : NSObject wxOSX_10_6_AND_LATER(<NSTabViewDelegate>)
33 {
34 }
35
36 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem;
37 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem;
38
39 @end
40
41 @interface wxNSTabView : NSTabView
42 {
43 }
44
45 @end
46
47 @implementation wxTabViewController
48
49 - (id) init
50 {
51 self = [super init];
52 return self;
53 }
54
55 - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
56 {
57 wxUnusedVar(tabViewItem);
58 wxNSTabView* view = (wxNSTabView*) tabView;
59 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
60
61 if ( viewimpl )
62 {
63 // wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
64 }
65 return YES;
66 }
67
68 - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
69 {
70 wxUnusedVar(tabViewItem);
71 wxNSTabView* view = (wxNSTabView*) tabView;
72 wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view );
73 if ( viewimpl )
74 {
75 wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer();
76 wxpeer->OSXHandleClicked(0);
77 }
78 }
79
80 @end
81
82 @implementation wxNSTabView
83
84 + (void)initialize
85 {
86 static BOOL initialized = NO;
87 if (!initialized)
88 {
89 initialized = YES;
90 wxOSXCocoaClassAddWXMethods( self );
91 }
92 }
93
94 @end
95
96 // ========================================================================
97 // WXCTabViewImageItem
98 // ========================================================================
99 @interface WXCTabViewImageItem : NSTabViewItem
100 {
101 NSImage *m_image;
102 }
103 - (id)init;
104 - (void)dealloc;
105 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel;
106 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect;
107 - (NSImage*)image;
108 - (void)setImage:(NSImage*)image;
109 @end // interface WXCTabViewImageItem : NSTabViewItem
110
111
112 @implementation WXCTabViewImageItem : NSTabViewItem
113 - (id)init
114 {
115 m_image = nil;
116 return [super initWithIdentifier:nil];
117 }
118 - (void)dealloc
119 {
120 [m_image release];
121 [super dealloc];
122 }
123 - (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
124 {
125 NSSize labelSize = [super sizeOfLabel:shouldTruncateLabel];
126 if(!m_image)
127 return labelSize;
128 NSSize imageSize = [m_image size];
129 // scale image size
130 if(imageSize.height > labelSize.height)
131 {
132 imageSize.width *= labelSize.height/imageSize.height;
133 imageSize.height *= labelSize.height/imageSize.height;
134 [m_image setScalesWhenResized:YES];
135 [m_image setSize: imageSize];
136 }
137 labelSize.width += imageSize.width;
138 return labelSize;
139 }
140 - (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
141 {
142 if(m_image)
143 {
144 NSSize imageSize = [m_image size];
145 [m_image compositeToPoint:NSMakePoint(tabRect.origin.x,
146 tabRect.origin.y+imageSize.height)
147 operation:NSCompositeSourceOver];
148 tabRect.size.width -= imageSize.width;
149 tabRect.origin.x += imageSize.width;
150 }
151 [super drawLabel:shouldTruncateLabel inRect:tabRect];
152 }
153 - (NSImage*)image
154 {
155 return m_image;
156 }
157 - (void)setImage:(NSImage*)image
158 {
159 [image retain];
160 [m_image release];
161 m_image = image;
162 if(!m_image)
163 return;
164 [[NSPasteboard generalPasteboard]
165 declareTypes:[NSArray arrayWithObject:NSTIFFPboardType]
166 owner:nil];
167 [[NSPasteboard generalPasteboard]
168 setData:[m_image TIFFRepresentation]
169 forType:NSTIFFPboardType];
170 }
171 @end // implementation WXCTabViewImageItem : NSTabViewItem
172
173
174 class wxCocoaTabView : public wxWidgetCocoaImpl
175 {
176 public:
177 wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w)
178 {
179 }
180
181 void GetContentArea( int &left , int &top , int &width , int &height ) const
182 {
183 wxNSTabView* slf = (wxNSTabView*) m_osxView;
184 NSRect r = [slf contentRect];
185 left = (int)r.origin.x;
186 top = (int)r.origin.y;
187 width = (int)r.size.width;
188 height = (int)r.size.height;
189 }
190
191 void SetValue( wxInt32 value )
192 {
193 wxNSTabView* slf = (wxNSTabView*) m_osxView;
194 // avoid 'changed' events when setting the tab programmatically
195 wxTabViewController* controller = [slf delegate];
196 [slf setDelegate:nil];
197 [slf selectTabViewItemAtIndex:(value-1)];
198 [slf setDelegate:controller];
199 }
200
201 wxInt32 GetValue() const
202 {
203 wxNSTabView* slf = (wxNSTabView*) m_osxView;
204 NSTabViewItem* selectedItem = [slf selectedTabViewItem];
205 if ( selectedItem == nil )
206 return 0;
207 else
208 return [slf indexOfTabViewItem:selectedItem]+1;
209 }
210
211 void SetMaximum( wxInt32 maximum )
212 {
213 wxNSTabView* slf = (wxNSTabView*) m_osxView;
214 int cocoacount = [slf numberOfTabViewItems ];
215 // avoid 'changed' events when setting the tab programmatically
216 wxTabViewController* controller = [slf delegate];
217 [slf setDelegate:nil];
218
219 if ( maximum > cocoacount )
220 {
221 for ( int i = cocoacount ; i < maximum ; ++i )
222 {
223 NSTabViewItem* item = [[WXCTabViewImageItem alloc] init];
224 [slf addTabViewItem:item];
225 [item release];
226 }
227 }
228 else if ( maximum < cocoacount )
229 {
230 for ( int i = cocoacount -1 ; i >= maximum ; --i )
231 {
232 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
233 [slf removeTabViewItem:item];
234 }
235 }
236 [slf setDelegate:controller];
237 }
238
239 void SetupTabs( const wxNotebook& notebook)
240 {
241 int pcount = notebook.GetPageCount();
242
243 SetMaximum( pcount );
244
245 for ( int i = 0 ; i < pcount ; ++i )
246 {
247 wxNotebookPage* page = notebook.GetPage(i);
248 NSTabViewItem* item = [(wxNSTabView*) m_osxView tabViewItemAtIndex:i];
249 [item setView:page->GetHandle() ];
250 wxCFStringRef cf( page->GetLabel() , notebook.GetFont().GetEncoding() );
251 [item setLabel:cf.AsNSString()];
252 if ( notebook.GetImageList() && notebook.GetPageImage(i) >= 0 )
253 {
254 const wxBitmap bmap = notebook.GetImageList()->GetBitmap( notebook.GetPageImage( i ) ) ;
255 if ( bmap.IsOk() )
256 {
257 [(WXCTabViewImageItem*) item setImage: bmap.GetNSImage()];
258 }
259 }
260 }
261 }
262
263 int TabHitTest(const wxPoint & pt, long* flags)
264 {
265 int retval = wxNOT_FOUND;
266
267 NSPoint nspt = wxToNSPoint( m_osxView, pt );
268
269 wxNSTabView* slf = (wxNSTabView*) m_osxView;
270
271 NSTabViewItem* hitItem = [slf tabViewItemAtPoint:nspt];
272
273 if (!hitItem) {
274 if ( flags )
275 *flags = wxBK_HITTEST_NOWHERE;
276 } else {
277 retval = [slf indexOfTabViewItem:hitItem];
278 if ( flags )
279 *flags = wxBK_HITTEST_ONLABEL;
280 }
281
282 return retval;
283 }
284 };
285
286
287 /*
288 #if 0
289 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
290
291 if ( bounds.right <= bounds.left )
292 bounds.right = bounds.left + 100;
293 if ( bounds.bottom <= bounds.top )
294 bounds.bottom = bounds.top + 100;
295
296 UInt16 tabstyle = kControlTabDirectionNorth;
297 if ( HasFlag(wxBK_LEFT) )
298 tabstyle = kControlTabDirectionWest;
299 else if ( HasFlag( wxBK_RIGHT ) )
300 tabstyle = kControlTabDirectionEast;
301 else if ( HasFlag( wxBK_BOTTOM ) )
302 tabstyle = kControlTabDirectionSouth;
303
304 ControlTabSize tabsize;
305 switch (GetWindowVariant())
306 {
307 case wxWINDOW_VARIANT_MINI:
308 tabsize = 3 ;
309 break;
310
311 case wxWINDOW_VARIANT_SMALL:
312 tabsize = kControlTabSizeSmall;
313 break;
314
315 default:
316 tabsize = kControlTabSizeLarge;
317 break;
318 }
319
320 m_peer = new wxMacControl( this );
321 OSStatus err = CreateTabsControl(
322 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds,
323 tabsize, tabstyle, 0, NULL, GetPeer()->GetControlRefAddr() );
324 verify_noerr( err );
325 #endif
326 */
327 wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer,
328 wxWindowMac* WXUNUSED(parent),
329 wxWindowID WXUNUSED(id),
330 const wxPoint& pos,
331 const wxSize& size,
332 long style,
333 long WXUNUSED(extraStyle))
334 {
335 static wxTabViewController* controller = NULL;
336
337 if ( !controller )
338 controller =[[wxTabViewController alloc] init];
339
340 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
341
342 NSTabViewType tabstyle = NSTopTabsBezelBorder;
343 if ( style & wxBK_LEFT )
344 tabstyle = NSLeftTabsBezelBorder;
345 else if ( style & wxBK_RIGHT )
346 tabstyle = NSRightTabsBezelBorder;
347 else if ( style & wxBK_BOTTOM )
348 tabstyle = NSBottomTabsBezelBorder;
349
350 wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r];
351 [v setTabViewType:tabstyle];
352 wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v );
353 [v setDelegate: controller];
354 return c;
355 }
356
357 #endif