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