]>
Commit | Line | Data |
---|---|---|
0f9b48d1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/osx/cocoa/toolbar.mm |
0f9b48d1 SC |
3 | // Purpose: wxToolBar |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
a9a4f229 | 7 | // RCS-ID: $Id$ |
0f9b48d1 SC |
8 | // Copyright: (c) Stefan Csomor |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #if wxUSE_TOOLBAR | |
15 | ||
16 | #include "wx/toolbar.h" | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wx.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/app.h" | |
23 | #include "wx/osx/private.h" | |
24 | #include "wx/geometry.h" | |
25 | #include "wx/sysopt.h" | |
26 | ||
27 | const short kwxMacToolBarToolDefaultWidth = 16; | |
28 | const short kwxMacToolBarToolDefaultHeight = 16; | |
29 | const short kwxMacToolBarTopMargin = 4; | |
30 | const short kwxMacToolBarLeftMargin = 4; | |
31 | const short kwxMacToolBorder = 0; | |
32 | const short kwxMacToolSpacing = 6; | |
33 | ||
34 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
35 | EVT_PAINT( wxToolBar::OnPaint ) | |
36 | END_EVENT_TABLE() | |
37 | ||
38 | ||
39 | #pragma mark - | |
40 | #pragma mark Tool Implementation | |
41 | ||
0f9b48d1 SC |
42 | // ---------------------------------------------------------------------------- |
43 | // private classes | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
baac7154 SC |
46 | class wxToolBarTool; |
47 | ||
48 | @interface wxNSToolBarButton : NSButton | |
49 | { | |
50 | wxToolBarTool* impl; | |
51 | } | |
52 | ||
53 | - (id)initWithFrame:(NSRect)frame; | |
54 | - (void) clickedAction: (id) sender; | |
55 | - (void)setImplementation: (wxToolBarTool *) theImplementation; | |
56 | - (wxToolBarTool*) implementation; | |
57 | - (BOOL) isFlipped; | |
58 | ||
59 | @end | |
60 | ||
0f9b48d1 SC |
61 | // We have a dual implementation for each tool, WXWidget and NSToolbarItem* |
62 | ||
63 | // when embedding native controls in the native toolbar we must make sure the | |
64 | // control does not get deleted behind our backs, so the retain count gets increased | |
65 | // (after creation it is 1), first be the creation of the custom NSToolbarItem wrapper | |
66 | // object, and second by the code 'creating' the custom HIView (which is the same as the | |
67 | // already existing native control, therefore we just increase the ref count) | |
68 | // when this view is removed from the native toolbar its count gets decremented again | |
69 | // and when the HITooolbarItem wrapper object gets destroyed it is decremented as well | |
70 | // so in the end the control lives with a refcount of one and can be disposed of by the | |
71 | // wxControl code. For embedded controls on a non-native toolbar this ref count is less | |
72 | // so we can only test against a range, not a specific value of the refcount. | |
73 | ||
74 | class wxToolBarTool : public wxToolBarToolBase | |
75 | { | |
76 | public: | |
77 | wxToolBarTool( | |
78 | wxToolBar *tbar, | |
79 | int id, | |
80 | const wxString& label, | |
81 | const wxBitmap& bmpNormal, | |
82 | const wxBitmap& bmpDisabled, | |
83 | wxItemKind kind, | |
84 | wxObject *clientData, | |
85 | const wxString& shortHelp, | |
86 | const wxString& longHelp ); | |
87 | ||
88 | wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) | |
89 | : wxToolBarToolBase(tbar, control, label) | |
90 | { | |
91 | Init(); | |
92 | if (control != NULL) | |
93 | SetControlHandle( (WXWidget) control->GetHandle() ); | |
94 | } | |
95 | ||
96 | virtual ~wxToolBarTool() | |
97 | { | |
98 | ClearControl(); | |
99 | } | |
100 | ||
101 | WXWidget GetControlHandle() | |
102 | { | |
103 | return (WXWidget) m_controlHandle; | |
104 | } | |
105 | ||
106 | void SetControlHandle( WXWidget handle ) | |
107 | { | |
108 | m_controlHandle = handle; | |
109 | } | |
110 | ||
111 | void SetPosition( const wxPoint& position ); | |
112 | ||
113 | void ClearControl() | |
114 | { | |
115 | if ( m_controlHandle ) | |
116 | { | |
117 | if ( !IsControl() ) | |
118 | { | |
119 | [m_controlHandle retain]; | |
120 | } | |
121 | else | |
122 | { | |
123 | // the embedded control is not under the responsibility of the tool, it gets disposed of in the | |
124 | // proper wxControl destructor | |
125 | } | |
126 | m_controlHandle = NULL ; | |
127 | } | |
128 | ||
129 | #if wxOSX_USE_NATIVE_TOOLBAR | |
130 | if ( m_toolbarItem ) | |
131 | { | |
132 | [m_toolbarItem release]; | |
133 | m_toolbarItem = NULL; | |
134 | } | |
135 | #endif // wxOSX_USE_NATIVE_TOOLBAR | |
136 | } | |
137 | ||
138 | wxSize GetSize() const | |
139 | { | |
140 | wxSize curSize; | |
141 | ||
142 | if ( IsControl() ) | |
143 | { | |
144 | curSize = GetControl()->GetSize(); | |
145 | } | |
146 | else if ( IsButton() ) | |
147 | { | |
baac7154 SC |
148 | // curSize = GetToolBar()->GetToolSize(); |
149 | NSRect best = [(wxNSToolBarButton*)m_controlHandle frame]; | |
150 | curSize = wxSize(best.size.width, best.size.height); | |
0f9b48d1 SC |
151 | } |
152 | else | |
153 | { | |
154 | // separator size | |
155 | curSize = GetToolBar()->GetToolSize(); | |
baac7154 | 156 | if ( GetToolBar()->IsVertical() ) |
0f9b48d1 SC |
157 | curSize.y /= 4; |
158 | else | |
159 | curSize.x /= 4; | |
160 | } | |
161 | ||
162 | return curSize; | |
163 | } | |
164 | ||
165 | wxPoint GetPosition() const | |
166 | { | |
167 | return wxPoint( m_x, m_y ); | |
168 | } | |
169 | ||
35a11fc7 | 170 | bool Enable( bool enable ); |
0f9b48d1 SC |
171 | |
172 | void UpdateImages(); | |
03647350 | 173 | |
0f9b48d1 | 174 | void UpdateToggleImage( bool toggle ); |
03647350 | 175 | |
0f9b48d1 SC |
176 | void UpdateLabel() |
177 | { | |
baac7154 SC |
178 | wxString labelStr = wxStripMenuCodes(m_label); |
179 | wxCFStringRef l(labelStr, GetToolBarFontEncoding()); | |
180 | wxCFStringRef sh( GetShortHelp(), GetToolBarFontEncoding() ); | |
01a33e96 | 181 | #if wxOSX_USE_NATIVE_TOOLBAR |
0f9b48d1 SC |
182 | if ( m_toolbarItem ) |
183 | { | |
184 | // strip mnemonics from the label for compatibility with the usual | |
185 | // labels in wxStaticText sense | |
0f9b48d1 SC |
186 | |
187 | [m_toolbarItem setLabel:l.AsNSString()]; | |
03647350 | 188 | |
0f9b48d1 SC |
189 | [m_toolbarItem setToolTip:sh.AsNSString()]; |
190 | } | |
01a33e96 | 191 | #endif |
baac7154 SC |
192 | if ( IsButton() ) |
193 | [(NSButton*)m_controlHandle setTitle:l.AsNSString()]; | |
194 | ||
0e506592 VZ |
195 | if ( m_controlHandle ) |
196 | { | |
197 | [m_controlHandle setToolTip:sh.AsNSString()]; | |
198 | } | |
0f9b48d1 SC |
199 | } |
200 | ||
201 | void Action() | |
202 | { | |
203 | wxToolBar *tbar = (wxToolBar*) GetToolBar(); | |
204 | if (CanBeToggled()) | |
205 | { | |
206 | bool shouldToggle; | |
207 | ||
208 | shouldToggle = !IsToggled(); | |
209 | tbar->ToggleTool( GetId(), shouldToggle ); | |
210 | } | |
211 | ||
212 | tbar->OnLeftClick( GetId(), IsToggled() ); | |
213 | } | |
03647350 | 214 | |
0f9b48d1 SC |
215 | #if wxOSX_USE_NATIVE_TOOLBAR |
216 | void SetToolbarItemRef( NSToolbarItem* ref ) | |
217 | { | |
218 | if ( m_controlHandle ) | |
219 | [m_controlHandle setHidden:YES]; | |
220 | if ( m_toolbarItem ) | |
221 | [m_toolbarItem release]; | |
222 | ||
223 | m_toolbarItem = ref; | |
224 | } | |
225 | ||
226 | NSToolbarItem* GetToolbarItemRef() const | |
227 | { | |
228 | return m_toolbarItem; | |
229 | } | |
230 | ||
231 | void SetIndex( CFIndex idx ) | |
232 | { | |
233 | m_index = idx; | |
234 | } | |
235 | ||
236 | CFIndex GetIndex() const | |
237 | { | |
238 | return m_index; | |
239 | } | |
240 | ||
241 | virtual void SetLabel(const wxString& label) | |
242 | { | |
243 | wxToolBarToolBase::SetLabel(label); | |
244 | UpdateLabel(); | |
ed310c35 VZ |
245 | } |
246 | ||
247 | virtual bool SetShortHelp(const wxString& help) | |
248 | { | |
249 | if ( !wxToolBarToolBase::SetShortHelp(help) ) | |
250 | return false; | |
251 | ||
252 | UpdateLabel(); | |
253 | ||
254 | return true; | |
255 | } | |
0f9b48d1 SC |
256 | #endif // wxOSX_USE_NATIVE_TOOLBAR |
257 | ||
258 | private: | |
259 | #if wxOSX_USE_NATIVE_TOOLBAR | |
260 | wxFontEncoding GetToolBarFontEncoding() const | |
261 | { | |
262 | wxFont f; | |
263 | if ( GetToolBar() ) | |
264 | f = GetToolBar()->GetFont(); | |
265 | return f.IsOk() ? f.GetEncoding() : wxFont::GetDefaultEncoding(); | |
266 | } | |
267 | #endif // wxOSX_USE_NATIVE_TOOLBAR | |
268 | ||
269 | void Init() | |
270 | { | |
271 | m_controlHandle = NULL; | |
272 | ||
273 | #if wxOSX_USE_NATIVE_TOOLBAR | |
274 | m_toolbarItem = NULL; | |
275 | m_index = -1; | |
276 | #endif | |
277 | } | |
278 | ||
279 | WXWidget m_controlHandle; | |
280 | wxCoord m_x; | |
281 | wxCoord m_y; | |
282 | wxBitmap m_alternateBitmap; | |
283 | ||
284 | #if wxOSX_USE_NATIVE_TOOLBAR | |
285 | NSToolbarItem* m_toolbarItem; | |
286 | // position in its toolbar, -1 means not inserted | |
287 | CFIndex m_index; | |
288 | #endif | |
289 | }; | |
290 | ||
01a33e96 SC |
291 | #if wxOSX_USE_NATIVE_TOOLBAR |
292 | ||
0f9b48d1 SC |
293 | @interface wxNSToolbarItem : NSToolbarItem |
294 | { | |
295 | wxToolBarTool* impl; | |
296 | } | |
297 | ||
298 | - (id) initWithItemIdentifier: (NSString*) identifier; | |
299 | - (void)setImplementation: (wxToolBarTool *) theImplementation; | |
300 | - (wxToolBarTool*) implementation; | |
301 | - (void) clickedAction: (id) sender; | |
35a11fc7 | 302 | - (BOOL) validateToolbarItem:(NSToolbarItem *)theItem; |
0f9b48d1 SC |
303 | |
304 | @end | |
305 | ||
306 | ||
c8fdb345 | 307 | @interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER(<NSToolbarDelegate>) |
0f9b48d1 SC |
308 | { |
309 | } | |
310 | ||
311 | - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag; | |
03647350 | 312 | |
0f9b48d1 SC |
313 | - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar; |
314 | ||
315 | - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar; | |
316 | ||
317 | - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar; | |
318 | ||
319 | ||
320 | @end | |
321 | ||
01a33e96 SC |
322 | #endif |
323 | ||
0f9b48d1 | 324 | |
01a33e96 SC |
325 | #if wxOSX_USE_NATIVE_TOOLBAR |
326 | ||
0f9b48d1 SC |
327 | @implementation wxNSToolbarItem |
328 | ||
329 | - (id)initWithItemIdentifier: (NSString*) identifier | |
330 | { | |
f599a415 | 331 | self = [super initWithItemIdentifier:identifier]; |
0f9b48d1 SC |
332 | impl = NULL; |
333 | [self setTarget: self]; | |
334 | [self setAction: @selector(clickedAction:)]; | |
335 | return self; | |
336 | } | |
337 | ||
338 | - (void) clickedAction: (id) sender | |
339 | { | |
d8207702 | 340 | wxUnusedVar(sender); |
0f9b48d1 SC |
341 | if ( impl ) |
342 | { | |
343 | impl->Action(); | |
344 | } | |
345 | } | |
346 | ||
347 | - (void)setImplementation: (wxToolBarTool *) theImplementation | |
348 | { | |
349 | impl = theImplementation; | |
350 | } | |
351 | ||
352 | - (wxToolBarTool*) implementation | |
353 | { | |
354 | return impl; | |
355 | } | |
356 | ||
35a11fc7 SC |
357 | - (BOOL)validateToolbarItem:(NSToolbarItem *)theItem |
358 | { | |
359 | wxUnusedVar(theItem); | |
360 | return impl->IsEnabled() ? YES:NO; | |
361 | } | |
362 | ||
0f9b48d1 SC |
363 | @end |
364 | ||
365 | @implementation wxNSToolbarDelegate | |
366 | ||
367 | - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar | |
368 | { | |
d8207702 | 369 | wxUnusedVar(toolbar); |
0f9b48d1 SC |
370 | return nil; |
371 | } | |
372 | ||
373 | - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar | |
374 | { | |
d8207702 | 375 | wxUnusedVar(toolbar); |
0f9b48d1 SC |
376 | return nil; |
377 | } | |
378 | ||
379 | - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar | |
380 | { | |
d8207702 | 381 | wxUnusedVar(toolbar); |
0f9b48d1 SC |
382 | return nil; |
383 | } | |
384 | ||
385 | - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag | |
386 | { | |
d8207702 | 387 | wxUnusedVar(toolbar); |
5bf56597 SC |
388 | #ifdef __LP64__ |
389 | wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier longLongValue]; | |
390 | #else | |
0f9b48d1 | 391 | wxToolBarTool* tool = (wxToolBarTool*) [itemIdentifier intValue]; |
5bf56597 | 392 | #endif |
0f9b48d1 SC |
393 | if ( tool ) |
394 | { | |
395 | wxNSToolbarItem* item = (wxNSToolbarItem*) tool->GetToolbarItemRef(); | |
396 | if ( flag && tool->IsControl() ) | |
397 | { | |
398 | NSView* view = tool->GetControl()->GetHandle(); | |
399 | [view removeFromSuperview]; | |
400 | [item setView:view]; | |
401 | wxSize sz = tool->GetControl()->GetSize(); | |
402 | NSSize size = NSMakeSize((float)sz.x, (float)sz.y); | |
403 | [item setMaxSize:size]; | |
404 | [item setMinSize:size]; | |
405 | [view setHidden:NO]; | |
406 | } | |
407 | return item; | |
408 | } | |
409 | return nil; | |
410 | } | |
411 | ||
412 | @end | |
413 | ||
01a33e96 SC |
414 | #endif |
415 | ||
0f9b48d1 SC |
416 | @implementation wxNSToolBarButton |
417 | ||
418 | - (id)initWithFrame:(NSRect)frame | |
419 | { | |
f599a415 | 420 | self = [super initWithFrame:frame]; |
0f9b48d1 SC |
421 | impl = NULL; |
422 | [self setTarget: self]; | |
423 | [self setAction: @selector(clickedAction:)]; | |
424 | return self; | |
425 | } | |
426 | ||
427 | - (void) clickedAction: (id) sender | |
428 | { | |
d8207702 | 429 | wxUnusedVar(sender); |
0f9b48d1 SC |
430 | if ( impl ) |
431 | { | |
432 | impl->Action(); | |
433 | } | |
434 | } | |
435 | ||
436 | - (void)setImplementation: (wxToolBarTool *) theImplementation | |
437 | { | |
438 | impl = theImplementation; | |
439 | } | |
440 | ||
441 | - (wxToolBarTool*) implementation | |
442 | { | |
443 | return impl; | |
444 | } | |
445 | ||
446 | - (BOOL) isFlipped | |
447 | { | |
448 | return YES; | |
449 | } | |
450 | ||
451 | @end | |
452 | ||
35a11fc7 | 453 | bool wxToolBarTool::Enable( bool enable ) |
0f9b48d1 | 454 | { |
35a11fc7 SC |
455 | if ( wxToolBarToolBase::Enable( enable ) == false ) |
456 | return false; | |
457 | ||
0f9b48d1 SC |
458 | if ( IsControl() ) |
459 | { | |
460 | GetControl()->Enable( enable ); | |
461 | } | |
462 | else if ( IsButton() ) | |
463 | { | |
464 | #if wxOSX_USE_NATIVE_TOOLBAR | |
465 | if ( m_toolbarItem != NULL ) | |
466 | [m_toolbarItem setEnabled:enable]; | |
467 | #endif | |
468 | ||
469 | if ( m_controlHandle != NULL ) | |
470 | [(NSControl*)m_controlHandle setEnabled:enable]; | |
471 | } | |
472 | ||
473 | return true; | |
474 | } | |
475 | ||
476 | void wxToolBarTool::SetPosition( const wxPoint& position ) | |
477 | { | |
478 | m_x = position.x; | |
479 | m_y = position.y; | |
480 | ||
481 | int mac_x = position.x; | |
482 | int mac_y = position.y; | |
483 | ||
484 | if ( IsButton() ) | |
485 | { | |
486 | NSRect frame = [m_controlHandle frame]; | |
487 | if ( frame.origin.x != mac_x || frame.origin.y != mac_y ) | |
488 | { | |
489 | frame.origin.x = mac_x; | |
490 | frame.origin.y = mac_y; | |
491 | [m_controlHandle setFrame:frame]; | |
492 | } | |
493 | } | |
494 | else if ( IsControl() ) | |
495 | { | |
496 | // embedded native controls are moved by the OS | |
497 | #if wxOSX_USE_NATIVE_TOOLBAR | |
498 | if ( ((wxToolBar*)GetToolBar())->MacWantsNativeToolbar() == false ) | |
499 | #endif | |
500 | { | |
501 | GetControl()->Move( position ); | |
502 | } | |
503 | } | |
504 | else | |
505 | { | |
506 | NSRect frame = [m_controlHandle frame]; | |
507 | if ( frame.origin.x != mac_x || frame.origin.y != mac_y ) | |
508 | { | |
509 | frame.origin.x = mac_x; | |
510 | frame.origin.y = mac_y; | |
511 | [m_controlHandle setFrame:frame]; | |
512 | } | |
03647350 | 513 | } |
0f9b48d1 SC |
514 | } |
515 | ||
516 | void wxToolBarTool::UpdateImages() | |
517 | { | |
518 | [(NSButton*) m_controlHandle setImage:m_bmpNormal.GetNSImage()]; | |
519 | ||
520 | if ( CanBeToggled() ) | |
521 | { | |
522 | int w = m_bmpNormal.GetWidth(); | |
523 | int h = m_bmpNormal.GetHeight(); | |
524 | m_alternateBitmap = wxBitmap( w, h ); | |
525 | wxMemoryDC dc; | |
526 | ||
527 | dc.SelectObject( m_alternateBitmap ); | |
528 | dc.SetPen( wxPen(*wxBLACK) ); | |
529 | dc.SetBrush( wxBrush( *wxLIGHT_GREY )); | |
b895954d | 530 | dc.DrawRoundedRectangle( 0, 0, w, h, 2 ); |
0f9b48d1 SC |
531 | dc.DrawBitmap( m_bmpNormal, 0, 0, true ); |
532 | dc.SelectObject( wxNullBitmap ); | |
03647350 | 533 | |
0f9b48d1 SC |
534 | [(NSButton*) m_controlHandle setAlternateImage:m_alternateBitmap.GetNSImage()]; |
535 | } | |
536 | UpdateToggleImage( CanBeToggled() && IsToggled() ); | |
537 | } | |
538 | ||
539 | void wxToolBarTool::UpdateToggleImage( bool toggle ) | |
540 | { | |
541 | #if wxOSX_USE_NATIVE_TOOLBAR | |
542 | if (m_toolbarItem != NULL ) | |
543 | { | |
544 | // the native toolbar item only has a 'selected' state (one for one toolbar) | |
545 | // so we emulate the toggle here | |
546 | if ( CanBeToggled() && toggle ) | |
547 | [m_toolbarItem setImage:m_alternateBitmap.GetNSImage()]; | |
548 | else | |
549 | [m_toolbarItem setImage:m_bmpNormal.GetNSImage()]; | |
550 | } | |
5e9897b4 | 551 | else |
0f9b48d1 | 552 | #endif |
5e9897b4 | 553 | { |
baac7154 SC |
554 | if ( IsButton() ) |
555 | [(NSButton*)m_controlHandle setState:(toggle ? NSOnState : NSOffState)]; | |
5e9897b4 | 556 | } |
0f9b48d1 SC |
557 | } |
558 | ||
559 | wxToolBarTool::wxToolBarTool( | |
560 | wxToolBar *tbar, | |
561 | int id, | |
562 | const wxString& label, | |
563 | const wxBitmap& bmpNormal, | |
564 | const wxBitmap& bmpDisabled, | |
565 | wxItemKind kind, | |
566 | wxObject *clientData, | |
567 | const wxString& shortHelp, | |
568 | const wxString& longHelp ) | |
569 | : | |
570 | wxToolBarToolBase( | |
571 | tbar, id, label, bmpNormal, bmpDisabled, kind, | |
572 | clientData, shortHelp, longHelp ) | |
573 | { | |
574 | Init(); | |
575 | } | |
576 | ||
577 | #pragma mark - | |
578 | #pragma mark Toolbar Implementation | |
579 | ||
580 | wxToolBarToolBase *wxToolBar::CreateTool( | |
581 | int id, | |
582 | const wxString& label, | |
583 | const wxBitmap& bmpNormal, | |
584 | const wxBitmap& bmpDisabled, | |
585 | wxItemKind kind, | |
586 | wxObject *clientData, | |
587 | const wxString& shortHelp, | |
588 | const wxString& longHelp ) | |
589 | { | |
590 | return new wxToolBarTool( | |
591 | this, id, label, bmpNormal, bmpDisabled, kind, | |
592 | clientData, shortHelp, longHelp ); | |
593 | } | |
594 | ||
595 | wxToolBarToolBase * | |
596 | wxToolBar::CreateTool(wxControl *control, const wxString& label) | |
597 | { | |
598 | return new wxToolBarTool(this, control, label); | |
599 | } | |
600 | ||
601 | void wxToolBar::Init() | |
602 | { | |
603 | m_maxWidth = -1; | |
604 | m_maxHeight = -1; | |
605 | m_defaultWidth = kwxMacToolBarToolDefaultWidth; | |
606 | m_defaultHeight = kwxMacToolBarToolDefaultHeight; | |
607 | ||
608 | #if wxOSX_USE_NATIVE_TOOLBAR | |
609 | m_macToolbar = NULL; | |
610 | m_macUsesNativeToolbar = false; | |
611 | #endif | |
612 | } | |
613 | ||
614 | // also for the toolbar we have the dual implementation: | |
615 | // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar | |
616 | ||
617 | bool wxToolBar::Create( | |
618 | wxWindow *parent, | |
619 | wxWindowID id, | |
620 | const wxPoint& pos, | |
621 | const wxSize& size, | |
622 | long style, | |
623 | const wxString& name ) | |
624 | { | |
625 | if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) ) | |
626 | return false; | |
627 | ||
628 | FixupStyle(); | |
629 | ||
630 | OSStatus err = noErr; | |
631 | ||
632 | #if wxOSX_USE_NATIVE_TOOLBAR | |
633 | ||
634 | if (parent->IsKindOf(CLASSINFO(wxFrame)) && wxSystemOptions::GetOptionInt(wxT("mac.toolbar.no-native")) != 1) | |
635 | { | |
636 | static wxNSToolbarDelegate* controller = nil; | |
03647350 | 637 | |
0f9b48d1 SC |
638 | if ( controller == nil ) |
639 | controller = [[wxNSToolbarDelegate alloc] init]; | |
640 | wxString identifier = wxString::Format( wxT("%p"), this ); | |
641 | wxCFStringRef cfidentifier(identifier); | |
642 | NSToolbar* tb = [[NSToolbar alloc] initWithIdentifier:cfidentifier.AsNSString()]; | |
03647350 | 643 | |
0f9b48d1 | 644 | m_macToolbar = tb ; |
03647350 | 645 | |
0f9b48d1 SC |
646 | if (m_macToolbar != NULL) |
647 | { | |
648 | [tb setDelegate:controller]; | |
03647350 | 649 | |
0f9b48d1 SC |
650 | NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault; |
651 | NSToolbarSizeMode displaySize = NSToolbarSizeModeSmall; | |
652 | ||
653 | if ( style & wxTB_NOICONS ) | |
654 | mode = NSToolbarDisplayModeLabelOnly; | |
655 | else if ( style & wxTB_TEXT ) | |
656 | mode = NSToolbarDisplayModeIconAndLabel; | |
657 | else | |
658 | mode = NSToolbarDisplayModeIconOnly; | |
659 | ||
660 | [tb setDisplayMode:mode]; | |
661 | [tb setSizeMode:displaySize]; | |
662 | } | |
663 | } | |
664 | #endif // wxOSX_USE_NATIVE_TOOLBAR | |
665 | ||
666 | return (err == noErr); | |
667 | } | |
668 | ||
669 | wxToolBar::~wxToolBar() | |
1665389a | 670 | { |
baac7154 SC |
671 | // removal only works while the toolbar is there |
672 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); | |
673 | if ( frame && frame->GetToolBar() == this ) | |
674 | { | |
675 | frame->SetToolBar(NULL); | |
676 | } | |
677 | ||
1665389a KO |
678 | [(NSToolbar*)m_macToolbar setDelegate:nil]; |
679 | [(NSToolbar*)m_macToolbar release]; | |
680 | m_macToolbar = NULL; | |
0f9b48d1 SC |
681 | } |
682 | ||
683 | bool wxToolBar::Show( bool show ) | |
684 | { | |
685 | WXWindow tlw = MacGetTopLevelWindowRef(); | |
686 | bool bResult = (tlw != NULL); | |
687 | ||
688 | if (bResult) | |
689 | { | |
690 | #if wxOSX_USE_NATIVE_TOOLBAR | |
691 | bool ownToolbarInstalled = false; | |
692 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
693 | if (ownToolbarInstalled) | |
694 | { | |
695 | bResult = ([(NSToolbar*)m_macToolbar isVisible] != show); | |
696 | if ( bResult ) | |
697 | [(NSToolbar*)m_macToolbar setVisible:show]; | |
698 | } | |
699 | else | |
700 | bResult = wxToolBarBase::Show( show ); | |
701 | #else | |
702 | ||
703 | bResult = wxToolBarBase::Show( show ); | |
704 | #endif | |
705 | } | |
706 | ||
707 | return bResult; | |
708 | } | |
709 | ||
710 | bool wxToolBar::IsShown() const | |
711 | { | |
712 | bool bResult; | |
713 | ||
714 | #if wxOSX_USE_NATIVE_TOOLBAR | |
715 | bool ownToolbarInstalled; | |
716 | ||
717 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
718 | if (ownToolbarInstalled) | |
719 | { | |
720 | bResult = [(NSToolbar*)m_macToolbar isVisible]; | |
721 | } | |
722 | else | |
723 | bResult = wxToolBarBase::IsShown(); | |
724 | #else | |
725 | ||
726 | bResult = wxToolBarBase::IsShown(); | |
727 | #endif | |
728 | ||
729 | return bResult; | |
730 | } | |
731 | ||
732 | void wxToolBar::DoGetSize( int *width, int *height ) const | |
733 | { | |
734 | #if wxOSX_USE_NATIVE_TOOLBAR | |
735 | bool ownToolbarInstalled; | |
736 | ||
737 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
738 | if ( ownToolbarInstalled ) | |
739 | { | |
740 | WXWindow tlw = MacGetTopLevelWindowRef(); | |
741 | float toolbarHeight = 0.0; | |
d8207702 | 742 | NSRect windowFrame = NSMakeRect(0, 0, 0, 0); |
03647350 | 743 | |
0f9b48d1 SC |
744 | if(m_macToolbar && [(NSToolbar*)m_macToolbar isVisible]) |
745 | { | |
746 | windowFrame = [NSWindow contentRectForFrameRect:[tlw frame] | |
747 | styleMask:[tlw styleMask]]; | |
748 | toolbarHeight = NSHeight(windowFrame) | |
749 | - NSHeight([[tlw contentView] frame]); | |
750 | } | |
03647350 | 751 | |
0f9b48d1 | 752 | if ( width != NULL ) |
e490b0d2 | 753 | *width = (int)windowFrame.size.width; |
0f9b48d1 | 754 | if ( height != NULL ) |
e490b0d2 | 755 | *height = (int)toolbarHeight; |
0f9b48d1 SC |
756 | } |
757 | else | |
758 | wxToolBarBase::DoGetSize( width, height ); | |
759 | ||
760 | #else | |
761 | wxToolBarBase::DoGetSize( width, height ); | |
762 | #endif | |
763 | } | |
764 | ||
765 | wxSize wxToolBar::DoGetBestSize() const | |
766 | { | |
baac7154 SC |
767 | // was updated in Realize() |
768 | ||
769 | wxSize size = GetMinSize(); | |
0f9b48d1 | 770 | |
baac7154 | 771 | return size; |
0f9b48d1 SC |
772 | } |
773 | ||
774 | void wxToolBar::SetWindowStyleFlag( long style ) | |
775 | { | |
776 | wxToolBarBase::SetWindowStyleFlag( style ); | |
777 | ||
778 | #if wxOSX_USE_NATIVE_TOOLBAR | |
779 | if (m_macToolbar != NULL) | |
780 | { | |
781 | NSToolbarDisplayMode mode = NSToolbarDisplayModeDefault; | |
782 | ||
783 | if ( style & wxTB_NOICONS ) | |
784 | mode = NSToolbarDisplayModeLabelOnly; | |
785 | else if ( style & wxTB_TEXT ) | |
786 | mode = NSToolbarDisplayModeIconAndLabel; | |
787 | else | |
788 | mode = NSToolbarDisplayModeIconOnly; | |
789 | ||
790 | [(NSToolbar*) m_macToolbar setDisplayMode:mode]; | |
791 | } | |
792 | #endif | |
793 | } | |
794 | ||
795 | #if wxOSX_USE_NATIVE_TOOLBAR | |
796 | bool wxToolBar::MacWantsNativeToolbar() | |
797 | { | |
798 | return m_macUsesNativeToolbar; | |
799 | } | |
800 | ||
801 | bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const | |
802 | { | |
803 | bool bResultV = false; | |
804 | ||
805 | if (ownToolbarInstalled != NULL) | |
806 | *ownToolbarInstalled = false; | |
807 | ||
808 | WXWindow tlw = MacGetTopLevelWindowRef(); | |
809 | if (tlw != NULL) | |
810 | { | |
811 | NSToolbar* curToolbarRef = [tlw toolbar]; | |
812 | bResultV = (curToolbarRef != NULL); | |
813 | if (bResultV && (ownToolbarInstalled != NULL)) | |
814 | *ownToolbarInstalled = (curToolbarRef == m_macToolbar); | |
815 | } | |
816 | ||
817 | return bResultV; | |
818 | } | |
819 | ||
820 | bool wxToolBar::MacInstallNativeToolbar(bool usesNative) | |
821 | { | |
822 | bool bResult = false; | |
823 | ||
824 | if (usesNative && (m_macToolbar == NULL)) | |
825 | return bResult; | |
826 | ||
baac7154 | 827 | if (usesNative && HasFlag(wxTB_LEFT|wxTB_RIGHT|wxTB_BOTTOM) ) |
0f9b48d1 SC |
828 | return bResult; |
829 | ||
830 | WXWindow tlw = MacGetTopLevelWindowRef(); | |
831 | if (tlw == NULL) | |
832 | return bResult; | |
833 | ||
834 | // check the existing toolbar | |
835 | NSToolbar* curToolbarRef = [tlw toolbar]; | |
836 | ||
837 | m_macUsesNativeToolbar = usesNative; | |
838 | ||
839 | if (m_macUsesNativeToolbar) | |
840 | { | |
841 | // only install toolbar if there isn't one installed already | |
842 | if (curToolbarRef == NULL) | |
843 | { | |
844 | bResult = true; | |
845 | [tlw setToolbar:(NSToolbar*) m_macToolbar]; | |
846 | [(NSToolbar*) m_macToolbar setVisible:YES]; | |
03647350 | 847 | |
22756322 | 848 | GetPeer()->Move(0,0,0,0 ); |
0f9b48d1 | 849 | SetSize( wxSIZE_AUTO_WIDTH, 0 ); |
22756322 | 850 | GetPeer()->SetVisibility( false ); |
0f9b48d1 SC |
851 | wxToolBarBase::Show( false ); |
852 | } | |
853 | } | |
854 | else | |
855 | { | |
856 | // only deinstall toolbar if this is the installed one | |
857 | if (m_macToolbar == curToolbarRef) | |
858 | { | |
859 | bResult = true; | |
860 | [(NSToolbar*) m_macToolbar setVisible:NO]; | |
1665389a | 861 | MacUninstallNativeToolbar(); |
22756322 | 862 | GetPeer()->SetVisibility( true ); |
0f9b48d1 SC |
863 | } |
864 | } | |
865 | ||
866 | if (bResult) | |
867 | InvalidateBestSize(); | |
868 | ||
869 | // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") ); | |
870 | return bResult; | |
871 | } | |
1665389a KO |
872 | |
873 | void wxToolBar::MacUninstallNativeToolbar() | |
874 | { | |
875 | if (!m_macToolbar) | |
876 | return; | |
877 | ||
878 | WXWindow tlw = MacGetTopLevelWindowRef(); | |
879 | if (tlw) | |
880 | [tlw setToolbar:nil]; | |
881 | } | |
0f9b48d1 SC |
882 | #endif |
883 | ||
baac7154 | 884 | void wxToolBar::DoLayout() |
0f9b48d1 | 885 | { |
0f9b48d1 SC |
886 | int maxToolWidth = 0; |
887 | int maxToolHeight = 0; | |
baac7154 | 888 | |
0f9b48d1 SC |
889 | int tw, th; |
890 | GetSize( &tw, &th ); | |
baac7154 | 891 | |
0f9b48d1 | 892 | // find the maximum tool width and height |
baac7154 | 893 | // and the number of stretchable items |
1cf59cb1 | 894 | int numStretchableSpaces = 0; |
0f9b48d1 SC |
895 | wxToolBarTool *tool; |
896 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
897 | while ( node ) | |
898 | { | |
899 | tool = (wxToolBarTool *) node->GetData(); | |
900 | if ( tool != NULL ) | |
901 | { | |
902 | wxSize sz = tool->GetSize(); | |
baac7154 | 903 | |
0f9b48d1 SC |
904 | if ( sz.x > maxToolWidth ) |
905 | maxToolWidth = sz.x; | |
906 | if ( sz.y > maxToolHeight ) | |
907 | maxToolHeight = sz.y; | |
baac7154 SC |
908 | if ( tool->IsStretchableSpace() ) |
909 | numStretchableSpaces++; | |
0f9b48d1 | 910 | } |
baac7154 | 911 | |
0f9b48d1 SC |
912 | node = node->GetNext(); |
913 | } | |
914 | ||
baac7154 SC |
915 | // layout non-native toolbar |
916 | ||
917 | bool isHorizontal = !IsVertical(); | |
918 | ||
919 | int maxWidth = 0; | |
920 | int maxHeight = 0; | |
921 | ||
922 | int x = m_xMargin + kwxMacToolBarLeftMargin; | |
923 | int y = m_yMargin + kwxMacToolBarTopMargin; | |
924 | ||
0f9b48d1 SC |
925 | node = m_tools.GetFirst(); |
926 | while ( node ) | |
927 | { | |
928 | tool = (wxToolBarTool*) node->GetData(); | |
929 | if ( tool == NULL ) | |
930 | { | |
931 | node = node->GetNext(); | |
932 | continue; | |
933 | } | |
baac7154 | 934 | |
0f9b48d1 SC |
935 | // set tool position: |
936 | // for the moment just perform a single row/column alignment | |
937 | wxSize cursize = tool->GetSize(); | |
938 | if ( x + cursize.x > maxWidth ) | |
939 | maxWidth = x + cursize.x; | |
940 | if ( y + cursize.y > maxHeight ) | |
941 | maxHeight = y + cursize.y; | |
baac7154 SC |
942 | |
943 | // update the item positioning state | |
944 | if ( !isHorizontal ) | |
945 | y += cursize.y + kwxMacToolSpacing; | |
946 | else | |
947 | x += cursize.x + kwxMacToolSpacing; | |
948 | ||
949 | node = node->GetNext(); | |
950 | } | |
951 | ||
952 | if ( isHorizontal ) | |
953 | { | |
954 | // if not set yet, only one row | |
955 | if ( m_maxRows <= 0 ) | |
956 | SetRows( 1 ); | |
957 | ||
958 | maxWidth += m_xMargin + kwxMacToolBarLeftMargin; | |
959 | m_minWidth = maxWidth; | |
960 | m_minHeight = m_maxHeight = maxToolHeight + 2 * (m_yMargin + kwxMacToolBarTopMargin); | |
961 | } | |
962 | else | |
963 | { | |
964 | // if not set yet, have one column | |
965 | if ( (GetToolsCount() > 0) && (m_maxRows <= 0) ) | |
966 | SetRows( GetToolsCount() ); | |
967 | ||
968 | maxHeight += m_yMargin + kwxMacToolBarTopMargin; | |
969 | m_minHeight = maxHeight; | |
970 | m_minWidth = m_maxWidth = maxToolWidth + 2 * (m_yMargin + kwxMacToolBarTopMargin); | |
971 | } | |
972 | ||
973 | int totalStretchableSpace = 0; | |
974 | int spacePerStretchable = 0; | |
975 | if ( numStretchableSpaces > 0 ) | |
976 | { | |
977 | if ( isHorizontal ) | |
978 | totalStretchableSpace = tw - maxWidth; | |
979 | else | |
980 | totalStretchableSpace = th - maxHeight; | |
981 | ||
982 | if ( totalStretchableSpace > 0 ) | |
983 | spacePerStretchable = totalStretchableSpace / numStretchableSpaces; | |
984 | } | |
985 | ||
986 | // perform real positioning | |
987 | ||
988 | x = m_xMargin + kwxMacToolBarLeftMargin; | |
989 | y = m_yMargin + kwxMacToolBarTopMargin; | |
990 | ||
991 | node = m_tools.GetFirst(); | |
992 | int currentStretchable = 0; | |
993 | while ( node ) | |
994 | { | |
995 | tool = (wxToolBarTool*) node->GetData(); | |
996 | if ( tool == NULL ) | |
997 | { | |
998 | node = node->GetNext(); | |
999 | continue; | |
1000 | } | |
1001 | ||
1002 | wxSize cursize = tool->GetSize(); | |
1003 | if ( tool->IsStretchableSpace() ) | |
1004 | { | |
1005 | ++currentStretchable; | |
1006 | int thisSpace = currentStretchable == numStretchableSpaces ? | |
1007 | totalStretchableSpace - (currentStretchable-1)*spacePerStretchable : | |
1008 | spacePerStretchable; | |
1009 | if ( isHorizontal ) | |
1010 | cursize.x += thisSpace; | |
1011 | else | |
1012 | cursize.y += thisSpace; | |
1013 | } | |
1014 | ||
1015 | if ( !isHorizontal ) | |
0f9b48d1 SC |
1016 | { |
1017 | int x1 = x + ( maxToolWidth - cursize.x ) / 2; | |
1018 | tool->SetPosition( wxPoint(x1, y) ); | |
1019 | } | |
1020 | else | |
1021 | { | |
1022 | int y1 = y + ( maxToolHeight - cursize.y ) / 2; | |
1023 | tool->SetPosition( wxPoint(x, y1) ); | |
1024 | } | |
baac7154 | 1025 | |
0f9b48d1 | 1026 | // update the item positioning state |
baac7154 | 1027 | if ( !isHorizontal ) |
0f9b48d1 SC |
1028 | y += cursize.y + kwxMacToolSpacing; |
1029 | else | |
1030 | x += cursize.x + kwxMacToolSpacing; | |
baac7154 SC |
1031 | |
1032 | node = node->GetNext(); | |
1033 | } | |
1034 | ||
1035 | } | |
1036 | ||
1037 | bool wxToolBar::Realize() | |
1038 | { | |
1039 | if ( !wxToolBarBase::Realize() ) | |
1040 | return false; | |
1041 | ||
1042 | wxToolBarTool *tool; | |
1043 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
0f9b48d1 SC |
1044 | |
1045 | #if wxOSX_USE_NATIVE_TOOLBAR | |
baac7154 SC |
1046 | CFIndex currentPosition = 0; |
1047 | bool insertAll = false; | |
1048 | ||
1049 | NSToolbar* refTB = (NSToolbar*)m_macToolbar; | |
1050 | wxFont f; | |
1051 | wxFontEncoding enc; | |
1052 | f = GetFont(); | |
1053 | if ( f.IsOk() ) | |
1054 | enc = f.GetEncoding(); | |
1055 | else | |
1056 | enc = wxFont::GetDefaultEncoding(); | |
1057 | ||
1058 | node = m_tools.GetFirst(); | |
1059 | while ( node ) | |
1060 | { | |
1061 | tool = (wxToolBarTool*) node->GetData(); | |
1062 | if ( tool == NULL ) | |
1063 | { | |
1064 | node = node->GetNext(); | |
1065 | continue; | |
1066 | } | |
1067 | ||
0f9b48d1 SC |
1068 | // install in native NSToolbar |
1069 | if ( refTB ) | |
1070 | { | |
1071 | NSToolbarItem* hiItemRef = tool->GetToolbarItemRef(); | |
1072 | if ( hiItemRef != NULL ) | |
1073 | { | |
1074 | // since setting the help texts is non-virtual we have to update | |
1075 | // the strings now | |
1076 | wxCFStringRef sh( tool->GetShortHelp(), enc); | |
1077 | [hiItemRef setToolTip:sh.AsNSString()]; | |
baac7154 | 1078 | |
0f9b48d1 SC |
1079 | if ( insertAll || (tool->GetIndex() != currentPosition) ) |
1080 | { | |
1081 | if ( !insertAll ) | |
1082 | { | |
1083 | insertAll = true; | |
baac7154 | 1084 | |
0f9b48d1 SC |
1085 | // if this is the first tool that gets newly inserted or repositioned |
1086 | // first remove all 'old' tools from here to the right, because of this | |
1087 | // all following tools will have to be reinserted (insertAll). | |
1088 | for ( wxToolBarToolsList::compatibility_iterator node2 = m_tools.GetLast(); | |
baac7154 SC |
1089 | node2 != node; |
1090 | node2 = node2->GetPrevious() ) | |
0f9b48d1 SC |
1091 | { |
1092 | wxToolBarTool *tool2 = (wxToolBarTool*) node2->GetData(); | |
baac7154 | 1093 | |
0f9b48d1 SC |
1094 | const long idx = tool2->GetIndex(); |
1095 | if ( idx != -1 ) | |
1096 | { | |
1097 | [refTB removeItemAtIndex:idx]; | |
1098 | tool2->SetIndex(-1); | |
1099 | } | |
1100 | } | |
1101 | } | |
baac7154 | 1102 | |
cc260109 | 1103 | wxCFStringRef cfidentifier; |
df04f800 | 1104 | NSString *nsItemId; |
60812bf6 | 1105 | if (tool->GetStyle() == wxTOOL_STYLE_SEPARATOR) |
cc260109 | 1106 | { |
f8251232 SC |
1107 | if ( tool->IsStretchable() ) |
1108 | nsItemId = NSToolbarFlexibleSpaceItemIdentifier; | |
1109 | else | |
1110 | { | |
1111 | if ( UMAGetSystemVersion() < 0x1070 ) | |
1112 | nsItemId = NSToolbarSeparatorItemIdentifier; | |
1113 | else | |
1114 | nsItemId = NSToolbarSpaceItemIdentifier; | |
1115 | } | |
cc260109 | 1116 | } |
60812bf6 SC |
1117 | else |
1118 | { | |
cc260109 VZ |
1119 | cfidentifier = wxCFStringRef(wxString::Format("%ld", (long)tool)); |
1120 | nsItemId = cfidentifier.AsNSString(); | |
60812bf6 | 1121 | } |
baac7154 | 1122 | |
cc260109 | 1123 | [refTB insertItemWithItemIdentifier:nsItemId atIndex:currentPosition]; |
0f9b48d1 SC |
1124 | tool->SetIndex( currentPosition ); |
1125 | } | |
baac7154 | 1126 | |
0f9b48d1 SC |
1127 | currentPosition++; |
1128 | } | |
1129 | } | |
baac7154 SC |
1130 | node = node->GetNext(); |
1131 | } | |
1132 | ||
0f9b48d1 | 1133 | #endif |
baac7154 SC |
1134 | |
1135 | DoLayout(); | |
1136 | ||
1137 | // adjust radio items | |
1138 | ||
1139 | bool lastIsRadio = false; | |
1140 | bool curIsRadio = false; | |
1141 | ||
1142 | node = m_tools.GetFirst(); | |
1143 | while ( node ) | |
1144 | { | |
1145 | tool = (wxToolBarTool*) node->GetData(); | |
1146 | if ( tool == NULL ) | |
1147 | { | |
1148 | node = node->GetNext(); | |
1149 | continue; | |
1150 | } | |
1151 | ||
0f9b48d1 SC |
1152 | // update radio button (and group) state |
1153 | lastIsRadio = curIsRadio; | |
1154 | curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) ); | |
1155 | ||
1156 | if ( !curIsRadio ) | |
1157 | { | |
1158 | if ( tool->IsToggled() ) | |
1159 | DoToggleTool( tool, true ); | |
1160 | } | |
1161 | else | |
1162 | { | |
1163 | if ( !lastIsRadio ) | |
1164 | { | |
1165 | if ( tool->Toggle( true ) ) | |
1166 | { | |
1167 | DoToggleTool( tool, true ); | |
1168 | } | |
1169 | } | |
1170 | else if ( tool->IsToggled() ) | |
1171 | { | |
1172 | if ( tool->IsToggled() ) | |
1173 | DoToggleTool( tool, true ); | |
1174 | ||
1175 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); | |
1176 | while ( nodePrev ) | |
1177 | { | |
1178 | wxToolBarToolBase *toggleTool = nodePrev->GetData(); | |
1179 | if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) ) | |
1180 | break; | |
1181 | ||
1182 | if ( toggleTool->Toggle( false ) ) | |
1183 | DoToggleTool( toggleTool, false ); | |
1184 | ||
1185 | nodePrev = nodePrev->GetPrevious(); | |
1186 | } | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | node = node->GetNext(); | |
1191 | } | |
1192 | ||
0f9b48d1 | 1193 | InvalidateBestSize(); |
baac7154 | 1194 | SetInitialSize( wxSize(m_minWidth, m_minHeight)); |
0f9b48d1 | 1195 | |
baac7154 SC |
1196 | SendSizeEventToParent(); |
1197 | ||
0f9b48d1 SC |
1198 | return true; |
1199 | } | |
1200 | ||
baac7154 SC |
1201 | void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
1202 | { | |
1203 | wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags); | |
1204 | ||
1205 | DoLayout(); | |
1206 | } | |
1207 | ||
0f9b48d1 SC |
1208 | void wxToolBar::SetToolBitmapSize(const wxSize& size) |
1209 | { | |
1210 | m_defaultWidth = size.x + kwxMacToolBorder; | |
1211 | m_defaultHeight = size.y + kwxMacToolBorder; | |
1212 | ||
1213 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1214 | if (m_macToolbar != NULL) | |
1215 | { | |
1216 | int maxs = wxMax( size.x, size.y ); | |
1217 | NSToolbarSizeMode sizeSpec; | |
1218 | if ( maxs > 32 ) | |
1219 | sizeSpec = NSToolbarSizeModeRegular; | |
1220 | else if ( maxs > 24 ) | |
1221 | sizeSpec = NSToolbarSizeModeDefault; | |
1222 | else | |
1223 | sizeSpec = NSToolbarSizeModeSmall; | |
1224 | ||
1225 | [(NSToolbar*) m_macToolbar setSizeMode:sizeSpec ]; | |
1226 | } | |
1227 | #endif | |
1228 | } | |
1229 | ||
1230 | // The button size is bigger than the bitmap size | |
1231 | wxSize wxToolBar::GetToolSize() const | |
1232 | { | |
1233 | return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder); | |
1234 | } | |
1235 | ||
1236 | void wxToolBar::SetRows(int nRows) | |
1237 | { | |
1238 | // avoid resizing the frame uselessly | |
1239 | if ( nRows != m_maxRows ) | |
1240 | m_maxRows = nRows; | |
1241 | } | |
1242 | ||
1243 | void wxToolBar::MacSuperChangedPosition() | |
1244 | { | |
1245 | wxWindow::MacSuperChangedPosition(); | |
1246 | ||
baac7154 | 1247 | /* |
0f9b48d1 SC |
1248 | #if wxOSX_USE_NATIVE_TOOLBAR |
1249 | if (! m_macUsesNativeToolbar ) | |
1250 | Realize(); | |
1251 | #else | |
1252 | ||
1253 | Realize(); | |
1254 | #endif | |
baac7154 | 1255 | */ |
0f9b48d1 SC |
1256 | } |
1257 | ||
1258 | void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap ) | |
1259 | { | |
5c33522f | 1260 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id)); |
0f9b48d1 SC |
1261 | if ( tool ) |
1262 | { | |
1263 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
1264 | ||
1265 | tool->SetNormalBitmap(bitmap); | |
1266 | ||
1267 | // a side-effect of the UpdateToggleImage function is that it always changes the bitmap used on the button. | |
1268 | tool->UpdateImages(); | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap ) | |
1273 | { | |
5c33522f | 1274 | wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id)); |
0f9b48d1 SC |
1275 | if ( tool ) |
1276 | { | |
1277 | wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools.")); | |
1278 | ||
1279 | tool->SetDisabledBitmap(bitmap); | |
1280 | ||
1281 | // TODO: what to do for this one? | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const | |
1286 | { | |
1287 | wxToolBarTool *tool; | |
1288 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
1289 | while ( node ) | |
1290 | { | |
1291 | tool = (wxToolBarTool *)node->GetData(); | |
1292 | if (tool != NULL) | |
1293 | { | |
1294 | wxRect2DInt r( tool->GetPosition(), tool->GetSize() ); | |
1295 | if ( r.Contains( wxPoint( x, y ) ) ) | |
1296 | return tool; | |
1297 | } | |
1298 | ||
1299 | node = node->GetNext(); | |
1300 | } | |
1301 | ||
d3b9f782 | 1302 | return NULL; |
0f9b48d1 SC |
1303 | } |
1304 | ||
1305 | wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) | |
1306 | { | |
1307 | wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y ); | |
1308 | if ( tool != NULL ) | |
1309 | return tool->GetShortHelp(); | |
1310 | ||
1311 | return wxEmptyString; | |
1312 | } | |
1313 | ||
35a11fc7 | 1314 | void wxToolBar::DoEnableTool(wxToolBarToolBase * WXUNUSED(t), bool WXUNUSED(enable)) |
0f9b48d1 | 1315 | { |
35a11fc7 | 1316 | // everything already done in the tool's Enable implementation |
0f9b48d1 SC |
1317 | } |
1318 | ||
1319 | void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) | |
1320 | { | |
1321 | wxToolBarTool *tool = (wxToolBarTool *)t; | |
1322 | if ( ( tool != NULL ) && tool->IsButton() ) | |
1323 | tool->UpdateToggleImage( toggle ); | |
1324 | } | |
1325 | ||
1326 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) | |
1327 | { | |
5c33522f | 1328 | wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase ); |
0f9b48d1 SC |
1329 | if (tool == NULL) |
1330 | return false; | |
1331 | ||
baac7154 SC |
1332 | long style = GetWindowStyleFlag(); |
1333 | ||
0f9b48d1 SC |
1334 | wxSize toolSize = GetToolSize(); |
1335 | WXWidget controlHandle = NULL; | |
1336 | NSRect toolrect = NSMakeRect(0, 0, toolSize.x, toolSize.y ); | |
1337 | ||
1338 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1339 | wxString label = tool->GetLabel(); | |
1340 | if (m_macToolbar && !label.empty() ) | |
1341 | { | |
1342 | // strip mnemonics from the label for compatibility | |
1343 | // with the usual labels in wxStaticText sense | |
1344 | label = wxStripMenuCodes(label); | |
1345 | } | |
1346 | #endif // wxOSX_USE_NATIVE_TOOLBAR | |
1347 | ||
1348 | switch (tool->GetStyle()) | |
1349 | { | |
1350 | case wxTOOL_STYLE_SEPARATOR: | |
1351 | { | |
1352 | wxASSERT( tool->GetControlHandle() == NULL ); | |
1353 | toolSize.x /= 4; | |
1354 | toolSize.y /= 4; | |
baac7154 | 1355 | if ( IsVertical() ) |
0f9b48d1 SC |
1356 | toolrect.size.height = toolSize.y; |
1357 | else | |
1358 | toolrect.size.width = toolSize.x; | |
1359 | ||
1360 | // in flat style we need a visual separator | |
1361 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1362 | if (m_macToolbar != NULL) | |
1363 | { | |
f8251232 SC |
1364 | NSString * nsItemId = nil; |
1365 | ||
1366 | if ( tool->IsStretchable() ) | |
1367 | nsItemId = NSToolbarFlexibleSpaceItemIdentifier; | |
1368 | else | |
1369 | { | |
1370 | if ( UMAGetSystemVersion() < 0x1070 ) | |
1371 | nsItemId = NSToolbarSeparatorItemIdentifier; | |
1372 | else | |
1373 | nsItemId = NSToolbarSpaceItemIdentifier; | |
1374 | } | |
1375 | ||
cc260109 | 1376 | NSToolbarItem* item = [[NSToolbarItem alloc] initWithItemIdentifier:nsItemId]; |
0f9b48d1 SC |
1377 | tool->SetToolbarItemRef( item ); |
1378 | } | |
1379 | #endif // wxOSX_USE_NATIVE_TOOLBAR | |
1380 | ||
1381 | NSBox* box = [[NSBox alloc] initWithFrame:toolrect]; | |
1382 | [box setBoxType:NSBoxSeparator]; | |
1383 | controlHandle = box; | |
1384 | tool->SetControlHandle( controlHandle ); | |
1385 | } | |
1386 | break; | |
1387 | ||
1388 | case wxTOOL_STYLE_BUTTON: | |
1389 | { | |
1390 | wxASSERT( tool->GetControlHandle() == NULL ); | |
1391 | ||
1392 | wxNSToolBarButton* v = [[wxNSToolBarButton alloc] initWithFrame:toolrect]; | |
1393 | ||
1394 | [v setBezelStyle:NSRegularSquareBezelStyle]; | |
1395 | [v setBordered:NO]; | |
5e9897b4 | 1396 | [v setButtonType: ( tool->CanBeToggled() ? NSToggleButton : NSMomentaryPushInButton )]; |
0f9b48d1 | 1397 | [v setImplementation:tool]; |
baac7154 | 1398 | |
0f9b48d1 | 1399 | controlHandle = v; |
03647350 | 1400 | |
0f9b48d1 SC |
1401 | #if wxOSX_USE_NATIVE_TOOLBAR |
1402 | if (m_macToolbar != NULL) | |
1403 | { | |
5bf56597 | 1404 | wxString identifier = wxString::Format(wxT("%ld"), (long) tool); |
0f9b48d1 SC |
1405 | wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() ); |
1406 | wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ]; | |
1407 | [item setImplementation:tool]; | |
1408 | tool->SetToolbarItemRef( item ); | |
1409 | } | |
1410 | ||
1411 | #endif // wxOSX_USE_NATIVE_TOOLBAR | |
1412 | tool->SetControlHandle( controlHandle ); | |
1413 | tool->UpdateImages(); | |
1414 | tool->UpdateLabel(); | |
8f2139d0 SC |
1415 | |
1416 | if ( style & wxTB_NOICONS ) | |
1417 | [v setImagePosition:NSNoImage]; | |
1418 | else if ( style & wxTB_TEXT ) | |
1419 | [v setImagePosition:NSImageAbove]; | |
1420 | else | |
1421 | [v setImagePosition:NSImageOnly]; | |
baac7154 SC |
1422 | [v sizeToFit]; |
1423 | ||
0f9b48d1 SC |
1424 | #if 0 |
1425 | InstallControlEventHandler( | |
1426 | (WXWidget) controlHandle, GetwxMacToolBarToolEventHandlerUPP(), | |
1427 | GetEventTypeCount(eventList), eventList, tool, NULL ); | |
1428 | #endif | |
1429 | } | |
1430 | break; | |
1431 | ||
1432 | case wxTOOL_STYLE_CONTROL: | |
1433 | ||
1434 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1435 | if (m_macToolbar != NULL) | |
1436 | { | |
1437 | WXWidget view = (WXWidget) tool->GetControl()->GetHandle() ; | |
9a83f860 | 1438 | wxCHECK_MSG( view, false, wxT("control must be non-NULL") ); |
0f9b48d1 | 1439 | |
5bf56597 | 1440 | wxString identifier = wxString::Format(wxT("%ld"), (long) tool); |
0f9b48d1 SC |
1441 | wxCFStringRef cfidentifier( identifier, wxFont::GetDefaultEncoding() ); |
1442 | wxNSToolbarItem* item = [[wxNSToolbarItem alloc] initWithItemIdentifier:cfidentifier.AsNSString() ]; | |
1443 | [item setImplementation:tool]; | |
1444 | tool->SetToolbarItemRef( item ); | |
1445 | } | |
1446 | #else | |
1447 | // right now there's nothing to do here | |
1448 | #endif | |
1449 | tool->UpdateLabel(); | |
1450 | break; | |
1451 | ||
1452 | default: | |
1453 | break; | |
1454 | } | |
1455 | ||
1456 | if ( controlHandle ) | |
1457 | { | |
1458 | WXWidget container = (WXWidget) GetHandle(); | |
1459 | wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") ); | |
1460 | ||
1461 | // SetControlVisibility( controlHandle, true, true ); | |
1462 | [container addSubview:controlHandle]; | |
1463 | } | |
1464 | ||
1465 | // nothing special to do here - we relayout in Realize() later | |
1466 | InvalidateBestSize(); | |
1467 | ||
1468 | return true; | |
1469 | ||
1470 | } | |
1471 | ||
1472 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) | |
1473 | { | |
1474 | wxFAIL_MSG( wxT("not implemented") ); | |
1475 | } | |
1476 | ||
1477 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) | |
1478 | { | |
5c33522f | 1479 | wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase ); |
0f9b48d1 SC |
1480 | wxToolBarToolsList::compatibility_iterator node; |
1481 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
1482 | { | |
1483 | wxToolBarToolBase *tool2 = node->GetData(); | |
1484 | if ( tool2 == tool ) | |
1485 | { | |
1486 | // let node point to the next node in the list | |
1487 | node = node->GetNext(); | |
1488 | ||
1489 | break; | |
1490 | } | |
1491 | } | |
1492 | ||
1493 | wxSize sz = ((wxToolBarTool*)tool)->GetSize(); | |
1494 | ||
1495 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1496 | CFIndex removeIndex = tool->GetIndex(); | |
1497 | #endif | |
1498 | ||
1499 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1500 | if (m_macToolbar != NULL) | |
1501 | { | |
1502 | if ( removeIndex != -1 && m_macToolbar ) | |
1503 | { | |
1504 | [(NSToolbar*) m_macToolbar removeItemAtIndex:removeIndex]; | |
1505 | tool->SetIndex( -1 ); | |
1506 | } | |
1507 | } | |
1508 | #endif | |
1509 | ||
1510 | tool->ClearControl(); | |
1511 | ||
1512 | // and finally reposition all the controls after this one | |
1513 | ||
1514 | for ( /* node -> first after deleted */; node; node = node->GetNext() ) | |
1515 | { | |
1516 | wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); | |
1517 | wxPoint pt = tool2->GetPosition(); | |
1518 | ||
baac7154 | 1519 | if ( IsVertical() ) |
0f9b48d1 SC |
1520 | pt.y -= sz.y; |
1521 | else | |
1522 | pt.x -= sz.x; | |
1523 | ||
1524 | tool2->SetPosition( pt ); | |
1525 | ||
1526 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1527 | if (m_macToolbar != NULL) | |
1528 | { | |
1529 | if ( removeIndex != -1 && tool2->GetIndex() > removeIndex ) | |
1530 | tool2->SetIndex( tool2->GetIndex() - 1 ); | |
1531 | } | |
1532 | #endif | |
1533 | } | |
1534 | ||
1535 | InvalidateBestSize(); | |
1536 | ||
1537 | return true; | |
1538 | } | |
1539 | ||
1540 | #include <Carbon/Carbon.h> | |
1541 | ||
1542 | void wxToolBar::OnPaint(wxPaintEvent& event) | |
1543 | { | |
1544 | #if wxOSX_USE_NATIVE_TOOLBAR | |
1545 | if ( m_macUsesNativeToolbar ) | |
1546 | { | |
baac7154 | 1547 | // nothing to do here |
0f9b48d1 | 1548 | } |
baac7154 | 1549 | else |
0f9b48d1 | 1550 | #endif |
baac7154 SC |
1551 | { |
1552 | int w, h; | |
1553 | GetSize( &w, &h ); | |
0f9b48d1 | 1554 | |
baac7154 | 1555 | bool drawMetalTheme = MacGetTopLevelWindow()->GetExtraStyle() & wxFRAME_EX_METAL; |
0f9b48d1 | 1556 | |
baac7154 SC |
1557 | if ( UMAGetSystemVersion() < 0x1050 ) |
1558 | { | |
1559 | if ( !drawMetalTheme ) | |
1560 | { | |
1561 | HIThemePlacardDrawInfo info; | |
1562 | memset( &info, 0, sizeof(info) ); | |
1563 | info.version = 0; | |
1564 | info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive; | |
1565 | ||
1566 | CGContextRef cgContext = (CGContextRef) MacGetCGContextRef(); | |
1567 | HIRect rect = CGRectMake( 0, 0, w, h ); | |
1568 | HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal ); | |
1569 | } | |
1570 | else | |
1571 | { | |
1572 | // leave the background as it is (striped or metal) | |
1573 | } | |
1574 | } | |
1575 | else | |
1576 | { | |
1577 | wxPaintDC dc(this); | |
1578 | ||
1579 | wxRect rect(0,0,w,h); | |
1580 | ||
1581 | dc.GradientFillLinear( rect , wxColour( 0xCC,0xCC,0xCC ), wxColour( 0xA8,0xA8,0xA8 ) , wxSOUTH ); | |
1582 | dc.SetPen( wxPen( wxColour( 0x51,0x51,0x51 ) ) ); | |
91b85d9b SC |
1583 | if ( HasFlag(wxTB_LEFT) ) |
1584 | dc.DrawLine(w-1, 0, w-1, h); | |
1585 | else if ( HasFlag(wxTB_RIGHT) ) | |
1586 | dc.DrawLine(0, 0, 0, h); | |
1587 | else if ( HasFlag(wxTB_BOTTOM) ) | |
1588 | dc.DrawLine(0, 0, w, 0); | |
1589 | else if ( HasFlag(wxTB_TOP) ) | |
1590 | dc.DrawLine(0, h-1, w, h-1); | |
baac7154 | 1591 | } |
0f9b48d1 | 1592 | } |
0f9b48d1 SC |
1593 | event.Skip(); |
1594 | } | |
1595 | ||
1596 | #endif // wxUSE_TOOLBAR |