]>
Commit | Line | Data |
---|---|---|
f033830e SC |
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 | |
f033830e SC |
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 | ||
dbeddfb9 SC |
28 | // |
29 | // controller | |
30 | // | |
31 | ||
c8fdb345 | 32 | @interface wxTabViewController : NSObject wxOSX_10_6_AND_LATER(<NSTabViewDelegate>) |
dbeddfb9 SC |
33 | { |
34 | } | |
35 | ||
36 | - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem; | |
37 | - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem; | |
38 | ||
39 | @end | |
40 | ||
f033830e SC |
41 | @interface wxNSTabView : NSTabView |
42 | { | |
f033830e SC |
43 | } |
44 | ||
f033830e SC |
45 | @end |
46 | ||
dbeddfb9 | 47 | @implementation wxTabViewController |
f033830e | 48 | |
dbeddfb9 | 49 | - (id) init |
f033830e | 50 | { |
f599a415 | 51 | self = [super init]; |
dbeddfb9 | 52 | return self; |
f033830e SC |
53 | } |
54 | ||
dbeddfb9 | 55 | - (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem |
f033830e | 56 | { |
d8207702 | 57 | wxUnusedVar(tabViewItem); |
dbeddfb9 | 58 | wxNSTabView* view = (wxNSTabView*) tabView; |
4dd9fdf8 SC |
59 | wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view ); |
60 | ||
dbeddfb9 SC |
61 | if ( viewimpl ) |
62 | { | |
ffad7b0d | 63 | // wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer(); |
dbeddfb9 SC |
64 | } |
65 | return YES; | |
f033830e SC |
66 | } |
67 | ||
dbbb040c | 68 | - (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem |
f033830e | 69 | { |
d8207702 | 70 | wxUnusedVar(tabViewItem); |
dbeddfb9 | 71 | wxNSTabView* view = (wxNSTabView*) tabView; |
4dd9fdf8 | 72 | wxWidgetCocoaImpl* viewimpl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( view ); |
dbeddfb9 SC |
73 | if ( viewimpl ) |
74 | { | |
75 | wxNotebook* wxpeer = (wxNotebook*) viewimpl->GetWXPeer(); | |
04236b74 | 76 | wxpeer->OSXHandleClicked(0); |
dbeddfb9 SC |
77 | } |
78 | } | |
79 | ||
80 | @end | |
81 | ||
82 | @implementation wxNSTabView | |
83 | ||
4dd9fdf8 SC |
84 | + (void)initialize |
85 | { | |
86 | static BOOL initialized = NO; | |
03647350 | 87 | if (!initialized) |
4dd9fdf8 SC |
88 | { |
89 | initialized = YES; | |
90 | wxOSXCocoaClassAddWXMethods( self ); | |
91 | } | |
92 | } | |
f033830e SC |
93 | |
94 | @end | |
95 | ||
294de5ca VZ |
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 | ||
dbeddfb9 SC |
174 | class wxCocoaTabView : public wxWidgetCocoaImpl |
175 | { | |
176 | public: | |
177 | wxCocoaTabView( wxWindowMac* peer , WXWidget w ) : wxWidgetCocoaImpl(peer, w) | |
178 | { | |
179 | } | |
03647350 VZ |
180 | |
181 | void GetContentArea( int &left , int &top , int &width , int &height ) const | |
dbeddfb9 SC |
182 | { |
183 | wxNSTabView* slf = (wxNSTabView*) m_osxView; | |
184 | NSRect r = [slf contentRect]; | |
e490b0d2 VZ |
185 | left = (int)r.origin.x; |
186 | top = (int)r.origin.y; | |
187 | width = (int)r.size.width; | |
188 | height = (int)r.size.height; | |
dbeddfb9 | 189 | } |
03647350 VZ |
190 | |
191 | void SetValue( wxInt32 value ) | |
dbeddfb9 SC |
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 | } | |
03647350 | 200 | |
dbeddfb9 SC |
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 | } | |
03647350 | 210 | |
dbeddfb9 SC |
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]; | |
03647350 | 218 | |
dbeddfb9 SC |
219 | if ( maximum > cocoacount ) |
220 | { | |
221 | for ( int i = cocoacount ; i < maximum ; ++i ) | |
222 | { | |
294de5ca | 223 | NSTabViewItem* item = [[WXCTabViewImageItem alloc] init]; |
dbeddfb9 SC |
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(); | |
03647350 | 242 | |
dbeddfb9 | 243 | SetMaximum( pcount ); |
03647350 | 244 | |
dbeddfb9 SC |
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 ) ) ; | |
a1b806b9 | 255 | if ( bmap.IsOk() ) |
dbeddfb9 | 256 | { |
294de5ca | 257 | [(WXCTabViewImageItem*) item setImage: bmap.GetNSImage()]; |
dbeddfb9 SC |
258 | } |
259 | } | |
260 | } | |
261 | } | |
005659f0 SC |
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) { | |
789e9abb VZ |
274 | if ( flags ) |
275 | *flags = wxBK_HITTEST_NOWHERE; | |
005659f0 SC |
276 | } else { |
277 | retval = [slf indexOfTabViewItem:hitItem]; | |
789e9abb VZ |
278 | if ( flags ) |
279 | *flags = wxBK_HITTEST_ONLABEL; | |
005659f0 SC |
280 | } |
281 | ||
282 | return retval; | |
283 | } | |
dbeddfb9 SC |
284 | }; |
285 | ||
286 | ||
f033830e SC |
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, | |
22756322 | 323 | tabsize, tabstyle, 0, NULL, GetPeer()->GetControlRefAddr() ); |
f033830e SC |
324 | verify_noerr( err ); |
325 | #endif | |
326 | */ | |
03647350 VZ |
327 | wxWidgetImplType* wxWidgetImpl::CreateTabView( wxWindowMac* wxpeer, |
328 | wxWindowMac* WXUNUSED(parent), | |
329 | wxWindowID WXUNUSED(id), | |
330 | const wxPoint& pos, | |
f033830e | 331 | const wxSize& size, |
03647350 | 332 | long style, |
d8207702 | 333 | long WXUNUSED(extraStyle)) |
f033830e | 334 | { |
dbeddfb9 SC |
335 | static wxTabViewController* controller = NULL; |
336 | ||
337 | if ( !controller ) | |
338 | controller =[[wxTabViewController alloc] init]; | |
339 | ||
dbeddfb9 | 340 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
03647350 | 341 | |
f033830e | 342 | NSTabViewType tabstyle = NSTopTabsBezelBorder; |
dbeddfb9 | 343 | if ( style & wxBK_LEFT ) |
f033830e SC |
344 | tabstyle = NSLeftTabsBezelBorder; |
345 | else if ( style & wxBK_RIGHT ) | |
346 | tabstyle = NSRightTabsBezelBorder; | |
347 | else if ( style & wxBK_BOTTOM ) | |
348 | tabstyle = NSBottomTabsBezelBorder; | |
03647350 | 349 | |
f033830e | 350 | wxNSTabView* v = [[wxNSTabView alloc] initWithFrame:r]; |
f033830e | 351 | [v setTabViewType:tabstyle]; |
dbeddfb9 | 352 | wxWidgetCocoaImpl* c = new wxCocoaTabView( wxpeer, v ); |
dbeddfb9 | 353 | [v setDelegate: controller]; |
f033830e SC |
354 | return c; |
355 | } | |
356 | ||
f033830e | 357 | #endif |