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