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
)
114 m_maxWidth
= m_maxHeight
= 0;
116 m_defaultWidth
= defwidth
;
117 m_defaultHeight
= defheight
;
136 m_windowStyle
= style
;
137 parent
->AddChild(this);
139 m_backgroundColour
= parent
->GetBackgroundColour() ;
140 m_foregroundColour
= parent
->GetForegroundColour() ;
143 m_windowId
= NewControlId();
151 AdjustForParentClientOrigin(x
, y
, wxSIZE_USE_EXISTING
);
159 MacPreControlCreate( parent
, id
, "" , wxPoint( x
, y
) , wxSize( width
, height
) ,style
, wxDefaultValidator
, name
, &bounds
, title
) ;
161 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, "\p" , true , 0 , 0 , 1,
162 kControlPlacardProc
, (long) this ) ;
163 MacPostControlCreate() ;
168 wxToolBar::~wxToolBar()
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)
265 Rect toolbarrect
= { m_y
, m_x
, m_y
+ m_height
, m_x
+ m_width
} ;
266 ControlFontStyleRec controlstyle
;
267 WindowPtr window
= GetMacRootWindow() ;
268 controlstyle
.flags
= kControlUseFontMask
;
269 controlstyle
.font
= kControlFontSmallSystemFont
;
271 wxNode
*node
= m_tools
.First();
274 wxSize toolSize
= GetToolSize() ;
279 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
280 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
282 if( !tool
->IsSeparator() )
284 Rect toolrect
= { toolbarrect
.top
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
285 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
286 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
288 PicHandle icon
= NULL
;
291 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
292 icon
= bmap
->m_hPict
;
293 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
295 if ( tool
->GetBitmap1().GetMask() )
297 icon
= MakePict( bmap
->m_hBitmap
, tool
->GetBitmap1().GetMask()->GetMaskBitmap() ) ;
301 icon
= MakePict( bmap
->m_hBitmap
, NULL
) ;
306 ControlHandle m_macToolHandle
;
308 SInt16 behaviour
= kControlBehaviorOffsetContents
;
309 if ( tool
->CanBeToggled() )
310 behaviour
+= kControlBehaviorToggles
;
314 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
315 behaviour
+ kControlContentPictHandle
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
316 ControlButtonContentInfo info
;
318 info
.contentType
= kControlContentPictHandle
;
319 info
.u
.picture
= icon
;
321 UMASetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
325 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
326 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
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
+ kwxMacToolBarLeftMargin
> m_maxWidth
)
352 m_maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
;
353 if (toolbarrect
.top
+ kwxMacToolBarTopMargin
+ toolSize
.y
> m_maxHeight
)
354 m_maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
;
359 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
361 m_maxWidth
= tw
; // +=toolSize.x;
362 m_maxHeight
+= toolSize
.y
;
363 m_maxHeight
+= m_yMargin
;
367 m_maxHeight
= th
;// += toolSize.y;
368 m_maxWidth
+= toolSize
.x
;
369 m_maxWidth
+= m_xMargin
;
372 SetSize(m_maxWidth
, m_maxHeight
);
377 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
379 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
382 // The button size is bigger than the bitmap size
383 wxSize
wxToolBar::GetToolSize() const
385 return wxSize(m_defaultWidth
+ 4, m_defaultHeight
+ 4);
388 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
391 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
393 if ( m_macToolHandles
[index
] == (void*) control
)
395 wxToolBarTool
*tool
= (wxToolBarTool
*)m_tools
.Nth( index
)->Data();
396 if ( tool
->CanBeToggled() )
398 tool
->Toggle( GetControlValue( control
) ) ;
400 OnLeftClick( tool
->GetId() , tool
-> IsToggled() ) ;
406 void wxToolBar::SetRows(int nRows
)
408 if ( nRows
== m_maxRows
)
410 // avoid resizing the frame uselessly
417 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
419 MacClientToRootWindow( &x
, &y
) ;
420 Point pt
= { y
,x
} ;
423 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
425 if ( m_macToolHandles
[index
] )
428 GetControlBounds((ControlHandle
) m_macToolHandles
[index
], &bounds
) ;
429 if ( PtInRect( pt
, &bounds
) )
431 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
436 return (wxToolBarToolBase
*)NULL
;
439 wxString
wxToolBar::MacGetToolTipString( wxPoint
&pt
)
441 wxToolBarToolBase
* tool
= FindToolForPosition( pt
.x
, pt
.y
) ;
444 return tool
->GetShortHelp() ;
449 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
451 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
452 if ( tool
->m_index
< 0 )
455 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
458 UMAActivateControl( control
) ;
460 UMADeactivateControl( control
) ;
463 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
465 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
466 if ( tool
->m_index
< 0 )
469 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
470 ::SetControlValue( control
, toggle
) ;
473 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
474 wxToolBarToolBase
*tool
)
476 // nothing special to do here - we really create the toolbar buttons in
483 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
485 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
486 // TODO: set toggle state
489 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
494 void wxToolBar::OnPaint(wxPaintEvent
& event
)
496 WindowRef window
= GetMacRootWindow() ;
499 wxWindow
* win
= wxFindWinFromMacWindow( window
) ;
502 wxMacDrawingHelper
help( win
) ;
503 // the mac control manager always assumes to have the origin at 0,0
506 bool hasTabBehind
= false ;
507 wxWindow
* parent
= GetParent() ;
510 if( parent
->MacGetWindowData() )
512 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, kThemeBrushDialogBackgroundActive
, false ) ;
516 if( parent
->IsKindOf( CLASSINFO( wxNotebook
) ) || parent
->IsKindOf( CLASSINFO( wxTabCtrl
) ))
518 if ( ((wxControl
*)parent
)->GetMacControl() )
519 SetUpControlBackground( ((wxControl
*)parent
)->GetMacControl() , -1 , true ) ;
523 parent
= parent
->GetParent() ;
525 Rect toolbarrect
= { m_y
, m_x
, m_y
+ m_height
, m_x
+ m_width
} ;
527 UMADrawThemePlacard( &toolbarrect
, IsEnabled() ? kThemeStateActive
: kThemeStateInactive
) ;
530 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
532 if ( m_macToolHandles
[index
] )
534 UMADrawControl( (ControlHandle
) m_macToolHandles
[index
] ) ;
538 UMASetThemeWindowBackground( win
->MacGetWindowData()->m_macWindow
, win
->MacGetWindowData()->m_macWindowBackgroundTheme
, false ) ;
542 void wxToolBar::OnMouse( wxMouseEvent
&event
)
545 if (event
.GetEventType() == wxEVT_LEFT_DOWN
|| event
.GetEventType() == wxEVT_LEFT_DCLICK
)
551 MacClientToRootWindow( &x
, &y
) ;
553 ControlHandle control
;
557 WindowRef window
= GetMacRootWindow() ;
564 if ( !event
.m_leftDown
&& !event
.m_rightDown
)
565 modifiers
|= btnState
;
567 if ( event
.m_shiftDown
)
568 modifiers
|= shiftKey
;
570 if ( event
.m_controlDown
)
571 modifiers
|= controlKey
;
573 if ( event
.m_altDown
)
574 modifiers
|= optionKey
;
576 if ( event
.m_metaDown
)
577 modifiers
|= cmdKey
;
579 controlpart
= FindControl( localwhere
, window
, &control
) ;
581 if ( AcceptsFocus() && FindFocus() != this )
585 if ( control
&& UMAIsControlActive( control
) )
588 if ( controlpart
== kControlIndicatorPart
&& !UMAHasAppearance() )
589 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) NULL
) ;
591 controlpart
= UMAHandleControlClick( control
, localwhere
, modifiers
, (ControlActionUPP
) -1 ) ;
592 wxTheApp
->s_lastMouseDown
= 0 ;
593 if ( controlpart
&& ! ( ( UMAHasAppearance() || (controlpart
!= kControlIndicatorPart
) )
594 && (IsKindOf( CLASSINFO( wxScrollBar
) ) ) ) ) // otherwise we will get the event twice
596 MacHandleControlClick( control
, controlpart
) ;
604 #endif // wxUSE_TOOLBAR