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
);
156 wxToolBar::~wxToolBar()
158 // we must refresh the frame size when the toolbar is deleted but the frame
159 // is not - otherwise toolbar leaves a hole in the place it used to occupy
162 PicHandle
MakePict(GWorldPtr wp
, GWorldPtr mask
) ;
163 PicHandle
MakePict(GWorldPtr wp
, GWorldPtr mask
)
168 PicHandle pict
; // this is the Picture we give back
170 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
171 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
172 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
174 unsigned char *maskimage
= NULL
;
176 GetPortBounds( wp
, &portRect
) ;
177 int width
= portRect
.right
- portRect
.left
;
178 int height
= portRect
.bottom
- portRect
.top
;
180 LockPixels( GetGWorldPixMap( wp
) ) ;
181 GetGWorld( &origPort
, &origDev
) ;
186 maskimage
= (unsigned char*) malloc( width
* height
) ;
187 SetGWorld( mask
, NULL
) ;
188 LockPixels( GetGWorldPixMap( mask
) ) ;
189 for ( int y
= 0 ; y
< height
; ++y
)
191 for( int x
= 0 ; x
< width
; ++x
)
195 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
196 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
199 UnlockPixels( GetGWorldPixMap( mask
) ) ;
202 SetGWorld( wp
, NULL
) ;
204 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
208 RGBBackColor( &gray
) ;
209 RGBForeColor( &black
) ;
210 EraseRect(&portRect
) ;
211 RGBBackColor( &white
) ;
215 for ( int y
= 0 ; y
< height
; ++y
)
217 for( int x
= 0 ; x
< width
; ++x
)
219 if ( maskimage
[y
*width
+ x
] )
223 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
224 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
233 CopyBits(GetPortBitMapForCopyBits(wp
), // src PixMap - we copy image over itself -
234 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs -
235 &portRect
, // srcRect - it will be recorded and compressed -
236 &portRect
, // dstRect - into the picture that is open -
237 srcCopy
,NULL
); // copyMode and no clip region
240 ClosePicture(); // We are done recording the picture
241 UnlockPixels( GetGWorldPixMap( wp
) ) ;
242 SetGWorld( origPort
, origDev
) ;
243 return pict
; // return our groovy pict handle
246 bool wxToolBar::Realize()
248 if (m_tools
.Number() == 0)
256 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
258 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
259 m_y
+ m_height
+ localOrigin
.v
, m_x
+ m_width
+ localOrigin
.h
} ;
260 ControlFontStyleRec controlstyle
;
262 controlstyle
.flags
= kControlUseFontMask
;
263 controlstyle
.font
= kControlFontSmallSystemFont
;
265 wxNode
*node
= m_tools
.First();
268 wxSize toolSize
= GetToolSize() ;
277 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
278 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
280 if( !tool
->IsSeparator() )
282 Rect toolrect
= { toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
283 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
284 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
286 PicHandle icon
= NULL
;
289 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
290 icon
= bmap
->m_hPict
;
291 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
293 if ( tool
->GetBitmap1().GetMask() )
295 icon
= MakePict( bmap
->m_hBitmap
, tool
->GetBitmap1().GetMask()->GetMaskBitmap() ) ;
299 icon
= MakePict( bmap
->m_hBitmap
, NULL
) ;
304 ControlHandle m_macToolHandle
;
306 SInt16 behaviour
= kControlBehaviorOffsetContents
;
307 if ( tool
->CanBeToggled() )
308 behaviour
+= kControlBehaviorToggles
;
312 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
313 behaviour
+ kControlContentPictHandle
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
314 ControlButtonContentInfo info
;
316 info
.contentType
= kControlContentPictHandle
;
317 info
.u
.picture
= icon
;
319 UMASetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
323 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
324 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
326 m_macToolHandles
.Add( m_macToolHandle
) ;
327 tool
->m_index
= m_macToolHandles
.Count() -1 ;
328 if ( !tool
->IsEnabled() )
330 UMADeactivateControl( m_macToolHandle
) ;
332 if ( tool
->CanBeToggled() && tool
->IsToggled() )
334 ::SetControlValue( m_macToolHandle
, 1 ) ;
336 UMASetControlFontStyle( m_macToolHandle
, &controlstyle
) ;
337 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
338 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
339 UMAEmbedControl( m_macToolHandle
, container
) ;
341 x
+= (int)toolSize
.x
;
346 m_macToolHandles
.Add( NULL
) ;
347 x
+= (int)toolSize
.x
/ 4;
349 if ( toolbarrect
.left
+ x
+ m_xMargin
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> maxWidth
)
350 maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
+ m_xMargin
- m_x
- localOrigin
.h
;
351 if (toolbarrect
.top
+ m_yMargin
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> maxHeight
)
352 maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
+ m_yMargin
- m_y
- localOrigin
.v
;
357 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
359 if ( m_maxRows
== 0 )
361 // if not set yet, only one row
365 maxHeight
+= toolSize
.y
;
366 maxHeight
+= m_yMargin
+ kwxMacToolBarTopMargin
;
367 m_maxHeight
= maxHeight
;
371 if ( noButtons
> 0 && m_maxRows
== 0 )
373 // if not set yet, have one column
377 maxWidth
+= toolSize
.x
;
378 maxWidth
+= m_xMargin
+ kwxMacToolBarLeftMargin
;
379 m_maxWidth
= maxWidth
;
382 SetSize(maxWidth
, maxHeight
);
387 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
389 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
392 // The button size is bigger than the bitmap size
393 wxSize
wxToolBar::GetToolSize() const
395 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
398 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
401 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
403 if ( m_macToolHandles
[index
] == (void*) control
)
405 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
406 if ( tool
->CanBeToggled() )
408 tool
->Toggle( GetControlValue( control
) ) ;
410 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
416 void wxToolBar::SetRows(int nRows
)
418 if ( nRows
== m_maxRows
)
420 // avoid resizing the frame uselessly
427 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
429 MacClientToRootWindow( &x
, &y
) ;
430 Point pt
= { y
,x
} ;
433 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
435 if ( m_macToolHandles
[index
] )
438 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
439 if ( PtInRect( pt
, &bounds
) )
441 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
446 return (wxToolBarToolBase
*)NULL
;
449 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
451 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
454 return tool
->GetShortHelp() ;
459 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
461 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
462 if ( tool
->m_index
< 0 )
465 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
468 UMAActivateControl( control
) ;
470 UMADeactivateControl( control
) ;
473 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
475 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
476 if ( tool
->m_index
< 0 )
479 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
480 ::SetControlValue( control
, toggle
) ;
483 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
484 wxToolBarToolBase
*tool
)
486 // nothing special to do here - we really create the toolbar buttons in
493 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
495 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
496 // TODO: set toggle state
499 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
504 void wxToolBar::OnPaint(wxPaintEvent
& event
)
511 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
514 wxMacDrawingHelper
help( win
) ;
515 // the mac control manager always assumes to have the origin at 0,0
518 bool hasTabBehind
= false ;
519 wxWindow
* parent
= GetParent() ;
522 if( parent
->MacGetWindowData() )
524 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
528 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
530 if ( ((wxControl
*)parent
)->GetMacControl() )
531 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
535 parent
= parent
->GetParent() ;
538 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
539 m_y
+ localOrigin
.v
+ m_height
, m_x
+ localOrigin
.h
+ m_width
} ;
541 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
544 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
546 if ( m_macToolHandles
[index
] )
548 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
552 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
553 wxDC::MacInvalidateSetup() ;
557 void wxToolBar::OnMouse( wxMouseEvent
&event
)
560 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
566 MacClientToRootWindow( &x
, &y
) ;
568 ControlHandle control
;
572 WindowRef window
= GetMacRootWindow() ;
579 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
580 modifiers
|= btnState
;
582 if ( event
.m_shiftDown
)
583 modifiers
|= shiftKey
;
585 if ( event
.m_controlDown
)
586 modifiers
|= controlKey
;
588 if ( event
.m_altDown
)
589 modifiers
|= optionKey
;
591 if ( event
.m_metaDown
)
592 modifiers
|= cmdKey
;
594 controlpart
= FindControl( localwhere
, window
, &control
) ;
596 if ( control
&& UMAIsControlActive( control
) )
599 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
600 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
602 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
603 wxTheApp
->s_lastMouseDown
= 0 ;
604 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
605 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
607 MacHandleControlClick( control
, controlpart
) ;
615 #endif // wxUSE_TOOLBAR