1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "toolbar.h"
20 #include "wx/toolbar.h"
22 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
24 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
27 #include <wx/mac/uma.h>
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 class wxToolBarTool
: public wxToolBarToolBase
36 wxToolBarTool(wxToolBar
*tbar
,
38 const wxBitmap
& bitmap1
,
39 const wxBitmap
& bitmap2
,
42 const wxString
& shortHelpString
,
43 const wxString
& longHelpString
)
44 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
45 clientData
, shortHelpString
, longHelpString
)
51 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
52 : wxToolBarToolBase(tbar
, control
)
58 // set/get the number of separators which we use to cover the space used by
59 // a control in the toolbar
60 void SetSeparatorsCount(size_t count
) { m_nSepCount
= count
; }
61 size_t GetSeparatorsCount() const { return m_nSepCount
; }
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
78 const wxBitmap
& bitmap1
,
79 const wxBitmap
& bitmap2
,
82 const wxString
& shortHelpString
,
83 const wxString
& longHelpString
)
85 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
86 clientData
, shortHelpString
, longHelpString
);
89 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
91 return new wxToolBarTool(this, control
);
94 // ----------------------------------------------------------------------------
95 // wxToolBar construction
96 // ----------------------------------------------------------------------------
98 void wxToolBar::Init()
103 m_defaultHeight
= 22;
107 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
108 long style
, const wxString
& name
)
110 m_maxWidth
= m_maxHeight
= 0;
113 m_defaultHeight
= 22;
132 MacPreControlCreate( parent
, id
, "" , wxPoint( x
, y
) , wxSize( width
, height
) ,style
, *((wxValidator
*)NULL
) , name
, &bounds
, title
) ;
134 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, "\p" , true , 0 , 0 , 1,
135 kControlPlacardProc
, (long) this ) ;
136 MacPostControlCreate() ;
141 wxToolBar::~wxToolBar()
146 PicHandle
MakePict(GWorldPtr wp
)
151 PicHandle pict
; // this is the Picture we give back
153 RGBColor gray
= { 0xCCCC ,0xCCCC , 0xCCCC } ;
155 GetGWorld( &origPort
, &origDev
) ;
156 SetGWorld( wp
, NULL
) ;
158 pict
= OpenPicture(&wp
->portRect
); // open a picture, this disables drawing
162 RGBBackColor( &gray
) ;
163 EraseRect(&wp
->portRect
) ;
164 CopyBits((BitMap
*)*wp
->portPixMap
, // src PixMap - we copy image over itself -
165 (BitMap
*)*wp
->portPixMap
, // dst PixMap - no drawing occurs -
166 &wp
->portRect
, // srcRect - it will be recorded and compressed -
167 &wp
->portRect
, // dstRect - into the picture that is open -
168 srcCopy
,NULL
); // copyMode and no clip region
170 ClosePicture(); // We are done recording the picture
171 SetGWorld( origPort
, origDev
) ;
172 return pict
; // return our groovy pict handle
175 PicHandle
MakePictWhite(GWorldPtr wp
)
180 PicHandle pict
; // this is the Picture we give back
182 RGBColor white
= { 0xFFFF ,0xFFFF , 0xFFFF } ;
184 GetGWorld( &origPort
, &origDev
) ;
185 SetGWorld( wp
, NULL
) ;
187 pict
= OpenPicture(&wp
->portRect
); // open a picture, this disables drawing
191 RGBBackColor( &white
) ;
192 EraseRect(&wp
->portRect
) ;
193 CopyBits((BitMap
*)*wp
->portPixMap
, // src PixMap - we copy image over itself -
194 (BitMap
*)*wp
->portPixMap
, // dst PixMap - no drawing occurs -
195 &wp
->portRect
, // srcRect - it will be recorded and compressed -
196 &wp
->portRect
, // dstRect - into the picture that is open -
197 srcCopy
,NULL
); // copyMode and no clip region
199 ClosePicture(); // We are done recording the picture
200 SetGWorld( origPort
, origDev
) ;
201 return pict
; // return our groovy pict handle
204 const short kwxMacToolBarTopMargin
= 2 ;
205 const short kwxMacToolBarLeftMargin
= 2 ;
207 bool wxToolBar::Realize()
209 if (m_tools
.Number() == 0)
212 Rect toolbarrect
= { m_y
, m_x
, m_y
+ m_height
, m_x
+ m_width
} ;
213 ControlFontStyleRec controlstyle
;
214 WindowPtr window
= GetMacRootWindow() ;
215 controlstyle
.flags
= kControlUseFontMask
;
216 controlstyle
.font
= kControlFontSmallSystemFont
;
218 wxNode
*node
= m_tools
.First();
221 wxSize toolSize
= GetToolSize() ;
226 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
227 wxBitmapRefData
* bmap
= (wxBitmapRefData
*) ( tool
->GetBitmap1().GetRefData()) ;
229 if( !tool
->IsSeparator() )
231 Rect toolrect
= { toolbarrect
.top
+ kwxMacToolBarTopMargin
, toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
, 0 , 0 } ;
232 toolrect
.right
= toolrect
.left
+ toolSize
.x
;
233 toolrect
.bottom
= toolrect
.top
+ toolSize
.y
;
235 PicHandle icon
= NULL
;
238 if ( bmap
->m_bitmapType
== kMacBitmapTypePict
)
239 icon
= bmap
->m_hPict
;
240 else if ( bmap
->m_bitmapType
== kMacBitmapTypeGrafWorld
)
242 icon
= MakePict( bmap
->m_hBitmap
) ;
246 ControlHandle m_macToolHandle
;
248 SInt16 behaviour
= kControlBehaviorOffsetContents
;
249 if ( tool
->CanBeToggled() )
250 behaviour
+= kControlBehaviorToggles
;
254 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
255 behaviour
+ kControlContentPictHandle
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
256 ControlButtonContentInfo info
;
258 info
.contentType
= kControlContentPictHandle
;
259 info
.u
.picture
= icon
;
261 UMASetControlData( m_macToolHandle
, kControlButtonPart
, kControlBevelButtonContentTag
, sizeof(info
) , (char*) &info
) ;
265 m_macToolHandle
= UMANewControl( window
, &toolrect
, "\p" , true , 0 ,
266 behaviour
, 0 , kControlBevelButtonNormalBevelProc
, (long) this ) ;
268 m_macToolHandles
.Add( m_macToolHandle
) ;
269 UMASetControlFontStyle( m_macToolHandle
, &controlstyle
) ;
270 UMAEmbedControl( m_macToolHandle
, m_macControl
) ;
272 x
+= (int)toolSize
.x
;
277 m_macToolHandles
.Add( NULL
) ;
278 x
+= (int)toolSize
.x
/ 4;
280 if ( toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
> m_maxWidth
)
281 m_maxWidth
= toolbarrect
.left
+ x
+ kwxMacToolBarLeftMargin
;
282 if (toolbarrect
.top
+ kwxMacToolBarTopMargin
+ toolSize
.y
> m_maxHeight
)
283 m_maxHeight
= toolbarrect
.top
+ kwxMacToolBarTopMargin
;
288 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL
)
290 m_maxWidth
= tw
; // +=toolSize.x;
291 m_maxHeight
+= toolSize
.y
;
292 m_maxHeight
+= m_yMargin
;
296 m_maxHeight
= th
;// += toolSize.y;
297 m_maxWidth
+= toolSize
.x
;
298 m_maxWidth
+= m_xMargin
;
301 SetSize(m_maxWidth
, m_maxHeight
);
306 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
308 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
311 // The button size is bigger than the bitmap size
312 wxSize
wxToolBar::GetToolSize() const
314 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
317 void wxToolBar::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
320 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
322 if ( m_macToolHandles
[index
] == (void*) control
)
324 OnLeftClick( ( (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ) ->m_index
, ( (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ) ->IsToggled() ) ;
329 void wxToolBar::SetRows(int nRows
)
331 if ( nRows
== m_maxRows
)
333 // avoid resizing the frame uselessly
340 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
342 MacClientToRootWindow( &x
, &y
) ;
343 Point pt
= { x
,y
} ;
346 for ( index
= 0 ; index
< m_macToolHandles
.Count() ; ++index
)
348 if ( PtInRect( pt
, &(**(ControlHandle
)(m_macToolHandles
[index
])).contrlRect
) )
350 return (wxToolBarTool
*) (m_tools
.Nth( index
)->Data() ) ;
354 return (wxToolBarToolBase
*)NULL
;
357 void wxToolBar::DoEnableTool(wxToolBarToolBase
*t
, bool enable
)
359 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
360 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
361 if ( UMAHasAppearance() )
364 ::ActivateControl( control
) ;
366 ::DeactivateControl( control
) ;
371 ::HiliteControl( control
, 0 ) ;
373 ::HiliteControl( control
, 255 ) ;
377 void wxToolBar::DoToggleTool(wxToolBarToolBase
*t
, bool toggle
)
379 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
381 ControlHandle control
= (ControlHandle
) m_macToolHandles
[ tool
->m_index
] ;
382 ::SetControlValue( control
, toggle
) ;
385 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
),
386 wxToolBarToolBase
*tool
)
388 // nothing special to do here - we really create the toolbar buttons in
395 void wxToolBar::DoSetToggle(wxToolBarToolBase
*t
, bool toggle
)
397 wxToolBarTool
*tool
= (wxToolBarTool
*)t
;
398 // TODO: set toggle state
401 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
405 #endif // wxUSE_TOOLBAR