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 defwidth
= 24 ;
83 const short defheight
= 22 ;
85 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
86 const wxBitmap
& bitmap1
,
87 const wxBitmap
& bitmap2
,
90 const wxString
& shortHelpString
,
91 const wxString
& longHelpString
)
93 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
94 clientData
, shortHelpString
, longHelpString
);
97 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
99 return new wxToolBarTool(this, control
);
102 void wxToolBar::Init()
106 m_defaultWidth
= defwidth
;
107 m_defaultHeight
= defheight
;
111 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
112 long style
, const wxString
& name
)
115 m_defaultWidth
= defwidth
;
116 m_defaultHeight
= defheight
;
135 m_windowStyle
= style
;
136 parent
->AddChild(this);
138 m_backgroundColour
= parent
->GetBackgroundColour() ;
139 m_foregroundColour
= parent
->GetForegroundColour() ;
142 m_windowId
= NewControlId();
150 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
158 MacPreControlCreate( parent
, id
, "" , wxPoint( x
, y
) , wxSize( width
, height
) ,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
160 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, "\p" , true , 0 , 0 , 1,
161 kControlPlacardProc
, (long) this ) ;
162 MacPostControlCreate() ;
167 wxToolBar::~wxToolBar()
169 // we must refresh the frame size when the toolbar is deleted but the frame
170 // is not - otherwise toolbar leaves a hole in the place it used to occupy
173 PicHandle
MakePict(GWorldPtr wp
, GWorldPtr mask
) ;
174 PicHandle
MakePict(GWorldPtr wp
, GWorldPtr mask
)
179 PicHandle pict
; // this is the Picture we give back
181 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
182 RGBColor white
= { 0xffff ,0xffff , 0xffff } ;
183 RGBColor black
= { 0x0000 ,0x0000 , 0x0000 } ;
185 unsigned char *maskimage
= NULL
;
187 GetPortBounds( wp
, &portRect
) ;
188 int width
= portRect
.right
- portRect
.left
;
189 int height
= portRect
.bottom
- portRect
.top
;
191 LockPixels( GetGWorldPixMap( wp
) ) ;
192 GetGWorld( &origPort
, &origDev
) ;
197 maskimage
= (unsigned char*) malloc( width
* height
) ;
198 SetGWorld( mask
, NULL
) ;
199 LockPixels( GetGWorldPixMap( mask
) ) ;
200 for ( int y
= 0 ; y
< height
; ++y
)
202 for( int x
= 0 ; x
< width
; ++x
)
206 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
207 maskimage
[y
*width
+ x
] = ( col
.red
== 0 ) ; // for monochrome masks
210 UnlockPixels( GetGWorldPixMap( mask
) ) ;
213 SetGWorld( wp
, NULL
) ;
215 pict
= OpenPicture(&portRect
); // open a picture, this disables drawing
219 RGBBackColor( &gray
) ;
220 RGBForeColor( &black
) ;
221 EraseRect(&portRect
) ;
222 RGBBackColor( &white
) ;
226 for ( int y
= 0 ; y
< height
; ++y
)
228 for( int x
= 0 ; x
< width
; ++x
)
230 if ( maskimage
[y
*width
+ x
] )
234 GetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
235 SetCPixel( x
+ portRect
.left
, y
+ portRect
.top
, &col
) ;
244 CopyBits(GetPortBitMapForCopyBits(wp
), // src PixMap - we copy image over itself -
245 GetPortBitMapForCopyBits(wp
), // dst PixMap - no drawing occurs -
246 &portRect
, // srcRect - it will be recorded and compressed -
247 &portRect
, // dstRect - into the picture that is open -
248 srcCopy
,NULL
); // copyMode and no clip region
251 ClosePicture(); // We are done recording the picture
252 UnlockPixels( GetGWorldPixMap( wp
) ) ;
253 SetGWorld( origPort
, origDev
) ;
254 return pict
; // return our groovy pict handle
257 const short kwxMacToolBarTopMargin
= 2 ;
258 const short kwxMacToolBarLeftMargin
= 2 ;
260 bool wxToolBar::Realize()
262 if (m_tools
.Number() == 0)
270 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
272 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
273 m_y
+ m_height
+ localOrigin
.v
, m_x
+ m_width
+ localOrigin
.h
} ;
274 ControlFontStyleRec controlstyle
;
276 controlstyle
.flags
= kControlUseFontMask
;
277 controlstyle
.font
= kControlFontSmallSystemFont
;
279 wxNode
*node
= m_tools
.First();
282 wxSize toolSize
= GetToolSize() ;
285 m_maxWidth
= m_maxHeight
= 0 ;
288 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
289 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
291 if( !tool
->IsSeparator() )
293 Rect toolrect
= { toolbarrect
.top
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
294 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
295 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
297 PicHandle icon
= NULL
;
300 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
301 icon
= bmap
->m_hPict
;
302 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
304 if ( tool
->GetBitmap1().GetMask() )
306 icon
= MakePict( bmap
->m_hBitmap
, tool
->GetBitmap1().GetMask()->GetMaskBitmap() ) ;
310 icon
= MakePict( bmap
->m_hBitmap
, NULL
) ;
315 ControlHandle m_macToolHandle
;
317 SInt16 behaviour
= kControlBehaviorOffsetContents
;
318 if ( tool
->CanBeToggled() )
319 behaviour
+= kControlBehaviorToggles
;
323 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
324 behaviour
+ kControlContentPictHandle
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
325 ControlButtonContentInfo info
;
327 info
.contentType
= kControlContentPictHandle
;
328 info
.u
.picture
= icon
;
330 UMASetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
334 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
335 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
337 m_macToolHandles
.Add( m_macToolHandle
) ;
338 tool
->m_index
= m_macToolHandles
.Count() -1 ;
339 if ( !tool
->IsEnabled() )
341 UMADeactivateControl( m_macToolHandle
) ;
343 if ( tool
->CanBeToggled() && tool
->IsToggled() )
345 ::SetControlValue( m_macToolHandle
, 1 ) ;
347 UMASetControlFontStyle( m_macToolHandle
, &controlstyle
) ;
348 ControlHandle container
= GetParent()->MacGetContainerForEmbedding() ;
349 wxASSERT_MSG( container
!= NULL
, "No valid mac container control" ) ;
350 UMAEmbedControl( m_macToolHandle
, container
) ;
352 x
+= (int)toolSize
.x
;
357 m_macToolHandles
.Add( NULL
) ;
358 x
+= (int)toolSize
.x
/ 4;
360 if ( toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
> m_maxWidth
)
361 m_maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
- m_x
- localOrigin
.h
;
362 if (toolbarrect
.top
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
> m_maxHeight
)
363 m_maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
- m_y
- localOrigin
.v
;
368 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
370 if ( m_maxRows
== 0 )
372 // if not set yet, only one row
375 m_maxWidth
= clipRect
.right
- m_x
;
376 //m_maxWidth = tw ; // +=toolSize.x;
377 //m_maxWidth += m_xMargin ;
378 m_maxHeight
+= toolSize
.y
;
379 m_maxHeight
+= m_yMargin
;
383 if ( noButtons
> 0 && m_maxRows
== 0 )
385 // if not set yet, have one column
388 m_maxHeight
= clipRect
.bottom
- m_y
;
389 //m_maxHeight += m_yMargin ;
390 m_maxWidth
+= toolSize
.x
;
391 m_maxWidth
+= m_xMargin
;
394 SetSize(m_maxWidth
, m_maxHeight
);
399 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
401 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
404 // The button size is bigger than the bitmap size
405 wxSize
wxToolBar::GetToolSize() const
407 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
410 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
413 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
415 if ( m_macToolHandles
[index
] == (void*) control
)
417 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
418 if ( tool
->CanBeToggled() )
420 tool
->Toggle( GetControlValue( control
) ) ;
422 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
428 void wxToolBar::SetRows(int nRows
)
430 if ( nRows
== m_maxRows
)
432 // avoid resizing the frame uselessly
439 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
441 MacClientToRootWindow( &x
, &y
) ;
442 Point pt
= { y
,x
} ;
445 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
447 if ( m_macToolHandles
[index
] )
450 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
451 if ( PtInRect( pt
, &bounds
) )
453 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
458 return (wxToolBarToolBase
*)NULL
;
461 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
463 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
466 return tool
->GetShortHelp() ;
471 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
473 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
474 if ( tool
->m_index
< 0 )
477 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
480 UMAActivateControl( control
) ;
482 UMADeactivateControl( control
) ;
485 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
487 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
488 if ( tool
->m_index
< 0 )
491 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
492 ::SetControlValue( control
, toggle
) ;
495 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
496 wxToolBarToolBase
*tool
)
498 // nothing special to do here - we really create the toolbar buttons in
505 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
507 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
508 // TODO: set toggle state
511 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
516 void wxToolBar::OnPaint(wxPaintEvent
& event
)
523 GetParent()->MacGetPortParams( &localOrigin
, &clipRect
, &window
, &win
) ;
526 wxMacDrawingHelper
help( win
) ;
527 // the mac control manager always assumes to have the origin at 0,0
530 bool hasTabBehind
= false ;
531 wxWindow
* parent
= GetParent() ;
534 if( parent
->MacGetWindowData() )
536 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
540 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
542 if ( ((wxControl
*)parent
)->GetMacControl() )
543 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
547 parent
= parent
->GetParent() ;
552 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
554 w
= clipRect
.right
- m_x
;
558 h
= clipRect
.bottom
- m_y
;
561 Rect toolbarrect
= { m_y
+ localOrigin
.v
, m_x
+ localOrigin
.h
,
562 m_y
+ localOrigin
.v
+ h
, m_x
+ localOrigin
.h
+ w
} ;
564 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
567 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
569 if ( m_macToolHandles
[index
] )
571 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
575 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
576 wxDC::MacInvalidateSetup() ;
579 WindowRef window = GetMacRootWindow() ;
582 wxWindow* win = wxFindWinFromMacWindow( window ) ;
585 wxMacDrawingHelper help( win ) ;
586 // the mac control manager always assumes to have the origin at 0,0
589 bool hasTabBehind = false ;
590 wxWindow* parent = GetParent() ;
593 if( parent->MacGetWindowData() )
595 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , kThemeBrushDialogBackgroundActive , false ) ;
599 if( parent->IsKindOf( CLASSINFO( wxNotebook ) ) || parent->IsKindOf( CLASSINFO( wxTabCtrl ) ))
601 if ( ((wxControl*)parent)->GetMacControl() )
602 SetUpControlBackground( ((wxControl*)parent)->GetMacControl() , -1 , true ) ;
606 parent = parent->GetParent() ;
608 Rect toolbarrect = { m_y , m_x , m_y + m_height , m_x + m_width } ;
610 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
613 for ( index = 0 ; index < m_macToolHandles.Count() ; ++index )
615 if ( m_macToolHandles[index] )
617 UMADrawControl( (ControlHandle) m_macToolHandles[index] ) ;
621 UMASetThemeWindowBackground( win->MacGetWindowData()->m_macWindow , win->MacGetWindowData()->m_macWindowBackgroundTheme , false ) ;
622 wxDC::MacInvalidateSetup() ;
627 void wxToolBar::OnMouse( wxMouseEvent
&event
)
630 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
636 MacClientToRootWindow( &x
, &y
) ;
638 ControlHandle control
;
642 WindowRef window
= GetMacRootWindow() ;
649 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
650 modifiers
|= btnState
;
652 if ( event
.m_shiftDown
)
653 modifiers
|= shiftKey
;
655 if ( event
.m_controlDown
)
656 modifiers
|= controlKey
;
658 if ( event
.m_altDown
)
659 modifiers
|= optionKey
;
661 if ( event
.m_metaDown
)
662 modifiers
|= cmdKey
;
664 controlpart
= FindControl( localwhere
, window
, &control
) ;
666 if ( AcceptsFocus() && FindFocus() != this )
670 if ( control
&& UMAIsControlActive( control
) )
673 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
674 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
676 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
677 wxTheApp
->s_lastMouseDown
= 0 ;
678 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
679 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
681 MacHandleControlClick( control
, controlpart
) ;
689 #endif // wxUSE_TOOLBAR