1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHORy
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "toolbar.h"
19 #include "wx/toolbar.h"
20 #include "wx/notebook.h"
21 #include "wx/tabctrl.h"
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
26 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
27 EVT_MOUSE_EVENTS( wxToolBar::OnMouse
)
28 EVT_PAINT( wxToolBar::OnPaint
)
32 #include "wx/mac/uma.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 class wxToolBarTool
: public wxToolBarToolBase
41 wxToolBarTool(wxToolBar
*tbar
,
43 const wxBitmap
& bitmap1
,
44 const wxBitmap
& bitmap2
,
47 const wxString
& shortHelpString
,
48 const wxString
& longHelpString
)
49 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
50 clientData
, shortHelpString
, longHelpString
)
56 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
57 : wxToolBarToolBase(tbar
, control
)
63 // set/get the number of separators which we use to cover the space used by
64 // a control in the toolbar
65 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
66 size_t GetSeparatorsCount() const { return m_nSepCount
; }
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 const short kwxMacToolBarToolDefaultWidth
= 24 ;
83 const short kwxMacToolBarToolDefaultHeight
= 22 ;
84 const short kwxMacToolBarTopMargin
= 2 ;
85 const short kwxMacToolBarLeftMargin
= 2 ;
88 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
89 const wxBitmap
& bitmap1
,
90 const wxBitmap
& bitmap2
,
93 const wxString
& shortHelpString
,
94 const wxString
& longHelpString
)
96 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
97 clientData
, shortHelpString
, longHelpString
);
100 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
102 return new wxToolBarTool(this, control
);
105 void wxToolBar::Init()
109 m_defaultWidth
= kwxMacToolBarToolDefaultWidth
;
110 m_defaultHeight
= kwxMacToolBarToolDefaultHeight
;
113 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
114 long style
, const wxString
& name
)
133 m_windowStyle
= style
;
134 parent
->AddChild(this);
136 m_backgroundColour
= parent
->GetBackgroundColour() ;
137 m_foregroundColour
= parent
->GetForegroundColour() ;
140 m_windowId
= NewControlId();
149 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
157 wxToolBar::~wxToolBar()
159 // we must refresh the frame size when the toolbar is deleted but the frame
160 // is not - otherwise toolbar leaves a hole in the place it used to occupy
163 PicHandle
MakePict(GWorldPtr wp
, GWorldPtr mask
) ;
164 PicHandle
MakePict(GWorldPtr wp
, GWorldPtr mask
)
169 PicHandle pict
; // this is the Picture we give back
171 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
172 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
173 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
175 unsigned char *maskimage
= NULL
;
177 GetPortBounds( wp
, &portRect
) ;
178 int width
= portRect
.right
- portRect
.left
;
179 int height
= portRect
.bottom
- portRect
.top
;
181 LockPixels( GetGWorldPixMap( wp
) ) ;
182 GetGWorld( &origPort
, &origDev
) ;
187 maskimage
= (unsigned char*) malloc( width
* height
) ;
188 SetGWorld( mask
, NULL
) ;
189 LockPixels( GetGWorldPixMap( mask
) ) ;
190 for ( int y
= 0 ; y
< height
; ++y
)
192 for( int x
= 0 ; x
< width
; ++x
)
196 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
197 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
200 UnlockPixels( GetGWorldPixMap( mask
) ) ;
203 SetGWorld( wp
, NULL
) ;
205 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
209 RGBBackColor( &gray
) ;
210 RGBForeColor( &black
) ;
211 EraseRect(&portRect
) ;
212 RGBBackColor( &white
) ;
216 for ( int y
= 0 ; y
< height
; ++y
)
218 for( int x
= 0 ; x
< width
; ++x
)
220 if ( maskimage
[y
*width
+ x
] )
224 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
225 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
234 CopyBits(GetPortBitMapForCopyBits(wp
), // src PixMap - we copy image over itself -
235 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs -
236 &portRect
, // srcRect - it will be recorded and compressed -
237 &portRect
, // dstRect - into the picture that is open -
238 srcCopy
,NULL
); // copyMode and no clip region
241 ClosePicture(); // We are done recording the picture
242 UnlockPixels( GetGWorldPixMap( wp
) ) ;
243 SetGWorld( origPort
, origDev
) ;
244 return pict
; // return our groovy pict handle
247 bool wxToolBar::Realize()
249 if (m_tools
.Number() == 0)
257 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
259 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
260 m_y
+ m_height
+ localOrigin
.v
, m_x
+ m_width
+ localOrigin
.h
} ;
261 ControlFontStyleRec controlstyle
;
263 controlstyle
.flags
= kControlUseFontMask
;
264 controlstyle
.font
= kControlFontSmallSystemFont
;
266 wxNode
*node
= m_tools
.First();
269 wxSize toolSize
= GetToolSize() ;
278 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
279 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
281 if( !tool
->IsSeparator() )
283 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
284 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
285 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
287 PicHandle icon
= NULL
;
290 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
291 icon
= bmap
->m_hPict
;
292 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
294 if ( tool
->GetBitmap1().GetMask() )
296 icon
= MakePict( bmap
->m_hBitmap
, tool
->GetBitmap1().GetMask()->GetMaskBitmap() ) ;
300 icon
= MakePict( bmap
->m_hBitmap
, NULL
) ;
305 ControlHandle m_macToolHandle
;
307 SInt16 behaviour
= kControlBehaviorOffsetContents
;
308 if ( tool
->CanBeToggled() )
309 behaviour
+= kControlBehaviorToggles
;
313 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , false , 0 ,
314 behaviour
+ kControlContentPictHandle
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
315 ControlButtonContentInfo info
;
317 info
.contentType
= kControlContentPictHandle
;
318 info
.u
.picture
= icon
;
320 UMASetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
324 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , false , 0 ,
325 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
327 UMAShowControl( m_macToolHandle
) ;
328 m_macToolHandles
.Add( m_macToolHandle
) ;
329 tool
->m_index
= m_macToolHandles
.Count() -1 ;
330 if ( !tool
->IsEnabled() )
332 UMADeactivateControl( m_macToolHandle
) ;
334 if ( tool
->CanBeToggled() && tool
->IsToggled() )
336 ::SetControlValue( m_macToolHandle
, 1 ) ;
338 UMASetControlFontStyle( m_macToolHandle
, &controlstyle
) ;
339 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
340 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
341 UMAEmbedControl( m_macToolHandle
, container
) ;
343 x
+= (int)toolSize
.x
;
348 m_macToolHandles
.Add( NULL
) ;
349 x
+= (int)toolSize
.x
/ 4;
351 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
352 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
353 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
354 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
359 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
361 if ( m_maxRows
== 0 )
363 // if not set yet, only one row
367 maxHeight
+= toolSize
.y
;
368 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
369 m_maxHeight
= maxHeight
;
373 if ( noButtons
> 0 && m_maxRows
== 0 )
375 // if not set yet, have one column
379 maxWidth
+= toolSize
.x
;
380 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
381 m_maxWidth
= maxWidth
;
384 SetSize(maxWidth
, maxHeight
);
389 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
391 m_defaultWidth
= size
.x
+4; m_defaultHeight
= size
.y
+4;
394 // The button size is bigger than the bitmap size
395 wxSize
wxToolBar::GetToolSize() const
397 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
400 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
403 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
405 if ( m_macToolHandles
[index
] == (void*) control
)
407 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
408 if ( tool
->CanBeToggled() )
410 tool
->Toggle( GetControlValue( control
) ) ;
412 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
418 void wxToolBar::SetRows(int nRows
)
420 if ( nRows
== m_maxRows
)
422 // avoid resizing the frame uselessly
429 void wxToolBar::MacSuperChangedPosition()
431 if (m_tools
.Number() > 0)
439 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
441 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
442 m_y
+ m_height
+ localOrigin
.v
, m_x
+ m_width
+ localOrigin
.h
} ;
443 ControlFontStyleRec controlstyle
;
445 controlstyle
.flags
= kControlUseFontMask
;
446 controlstyle
.font
= kControlFontSmallSystemFont
;
448 wxNode
*node
= m_tools
.First();
451 wxSize toolSize
= GetToolSize() ;
459 WindowRef rootwindow
= GetMacRootWindow() ;
460 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
461 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDialogBackgroundActive
, false ) ;
462 wxMacDrawingHelper
focus( wxrootwindow
) ;
465 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
466 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
468 if( !tool
->IsSeparator() )
470 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
471 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
472 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
474 ControlHandle m_macToolHandle
= (ControlHandle
) m_macToolHandles
[toolcount
++] ;
478 GetControlBounds( m_macToolHandle
, &contrlRect
) ;
479 int former_mac_x
= contrlRect
.left
;
480 int former_mac_y
= contrlRect
.top
;
481 int mac_x
= toolrect
.left
;
482 int mac_y
= toolrect
.top
;
484 if ( mac_x
!= former_mac_x
|| mac_y
!= former_mac_y
)
487 Rect inval
= { former_mac_y
, former_mac_x
, former_mac_y
+ toolSize
.y
, former_mac_x
+ toolSize
.y
} ;
488 InvalWindowRect( rootwindow
, &inval
) ;
490 UMAMoveControl( m_macToolHandle
, mac_x
, mac_y
) ;
492 Rect inval
= { mac_y
, mac_x
, mac_y
+ toolSize
.y
, mac_x
+ toolSize
.y
} ;
493 InvalWindowRect( rootwindow
, &inval
) ;
498 x
+= (int)toolSize
.x
;
504 x
+= (int)toolSize
.x
/ 4;
506 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
507 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
508 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
509 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
513 if ( wxrootwindow
->IsKindOf( CLASSINFO( wxDialog
) ) )
518 UMASetThemeWindowBackground( rootwindow
, kThemeBrushDocumentWindowBackground
, false ) ;
522 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
524 if ( m_maxRows
== 0 )
526 // if not set yet, only one row
530 maxHeight
+= toolSize
.y
;
531 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
532 m_maxHeight
= maxHeight
;
536 if ( noButtons
> 0 && m_maxRows
== 0 )
538 // if not set yet, have one column
542 maxWidth
+= toolSize
.x
;
543 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
544 m_maxWidth
= maxWidth
;
547 SetSize(maxWidth
, maxHeight
);
550 wxWindow::MacSuperChangedPosition() ;
553 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
555 MacClientToRootWindow( &x
, &y
) ;
556 Point pt
= { y
,x
} ;
559 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
561 if ( m_macToolHandles
[index
] )
564 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
565 if ( PtInRect( pt
, &bounds
) )
567 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
572 return (wxToolBarToolBase
*)NULL
;
575 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
577 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
580 return tool
->GetShortHelp() ;
585 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
587 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
588 if ( tool
->m_index
< 0 )
591 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
594 UMAActivateControl( control
) ;
596 UMADeactivateControl( control
) ;
599 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
601 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
602 if ( tool
->m_index
< 0 )
605 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
606 ::SetControlValue( control
, toggle
) ;
609 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
610 wxToolBarToolBase
*tool
)
612 // nothing special to do here - we really create the toolbar buttons in
619 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
621 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
622 // TODO: set toggle state
625 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
630 void wxToolBar::OnPaint(wxPaintEvent
& event
)
637 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
640 wxMacDrawingHelper
help( win
) ;
641 // the mac control manager always assumes to have the origin at 0,0
644 bool hasTabBehind
= false ;
645 wxWindow
* parent
= GetParent() ;
648 if( parent
->MacGetWindowData() )
650 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
654 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
656 if ( ((wxControl
*)parent
)->GetMacControl() )
657 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
661 parent
= parent
->GetParent() ;
664 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
665 m_y
+ localOrigin
.v
+ m_height
, m_x
+ localOrigin
.h
+ m_width
} ;
667 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
670 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
672 if ( m_macToolHandles
[index
] )
674 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
678 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
682 void wxToolBar::OnMouse( wxMouseEvent
&event
)
685 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
691 MacClientToRootWindow( &x
, &y
) ;
693 ControlHandle control
;
697 WindowRef window
= GetMacRootWindow() ;
704 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
705 modifiers
|= btnState
;
707 if ( event
.m_shiftDown
)
708 modifiers
|= shiftKey
;
710 if ( event
.m_controlDown
)
711 modifiers
|= controlKey
;
713 if ( event
.m_altDown
)
714 modifiers
|= optionKey
;
716 if ( event
.m_metaDown
)
717 modifiers
|= cmdKey
;
719 controlpart
= FindControl( localwhere
, window
, &control
) ;
721 if ( control
&& UMAIsControlActive( control
) )
724 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
725 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
727 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
728 wxTheApp
->s_lastMouseDown
= 0 ;
729 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
730 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
732 MacHandleControlClick( control
, controlpart
) ;
740 #endif // wxUSE_TOOLBAR