PageRenderTime 32ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/ExtLibs/wxWidgets/src/osx/cocoa/toolbar.mm

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