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