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