]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: toolbar.cpp | |
3 | // Purpose: wxToolBar | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 SC |
5 | // Modified by: |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
84969af7 | 8 | // Copyright: (c) Stefan Csomor |
65571936 | 9 | // Licence: The wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
e9576ca5 SC |
13 | #pragma implementation "toolbar.h" |
14 | #endif | |
15 | ||
3d1a4878 | 16 | #include "wx/wxprec.h" |
519cb848 SC |
17 | |
18 | #if wxUSE_TOOLBAR | |
19 | ||
3d1a4878 | 20 | #include "wx/wx.h" |
72055702 | 21 | #include "wx/bitmap.h" |
e56d2520 | 22 | #include "wx/toolbar.h" |
e9576ca5 | 23 | |
2eb10e2a | 24 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) |
e9576ca5 SC |
25 | |
26 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
f3a65c3e | 27 | EVT_PAINT( wxToolBar::OnPaint ) |
e9576ca5 | 28 | END_EVENT_TABLE() |
e9576ca5 | 29 | |
d497dca4 | 30 | #include "wx/mac/uma.h" |
bfe9ffbc | 31 | #include "wx/geometry.h" |
ee799df7 SC |
32 | |
33 | #ifdef __WXMAC_OSX__ | |
17dfb611 SC |
34 | const short kwxMacToolBarToolDefaultWidth = 16 ; |
35 | const short kwxMacToolBarToolDefaultHeight = 16 ; | |
e56d2520 SC |
36 | const short kwxMacToolBarTopMargin = 4 ; // 1 ; // used to be 4 |
37 | const short kwxMacToolBarLeftMargin = 4 ; //1 ; // used to be 4 | |
38 | const short kwxMacToolBorder = 0 ; // used to be 0 | |
39 | const short kwxMacToolSpacing = 6 ; // 2 ; // used to be 6 | |
ee799df7 SC |
40 | #else |
41 | const short kwxMacToolBarToolDefaultWidth = 24 ; | |
42 | const short kwxMacToolBarToolDefaultHeight = 22 ; | |
43 | const short kwxMacToolBarTopMargin = 2 ; | |
44 | const short kwxMacToolBarLeftMargin = 2 ; | |
45 | const short kwxMacToolBorder = 4 ; | |
abbcdf3f | 46 | const short kwxMacToolSpacing = 0 ; |
ee799df7 SC |
47 | #endif |
48 | ||
e56d2520 SC |
49 | #pragma mark - |
50 | #pragma mark Tool Implementation | |
51 | ||
52 | ||
37e2cb08 SC |
53 | // ---------------------------------------------------------------------------- |
54 | // private classes | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
e56d2520 SC |
57 | // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef |
58 | ||
37e2cb08 SC |
59 | class wxToolBarTool : public wxToolBarToolBase |
60 | { | |
61 | public: | |
62 | wxToolBarTool(wxToolBar *tbar, | |
63 | int id, | |
27e2d606 GD |
64 | const wxString& label, |
65 | const wxBitmap& bmpNormal, | |
66 | const wxBitmap& bmpDisabled, | |
67 | wxItemKind kind, | |
37e2cb08 | 68 | wxObject *clientData, |
27e2d606 | 69 | const wxString& shortHelp, |
bfe9ffbc | 70 | const wxString& longHelp) ; |
f3a65c3e | 71 | |
37e2cb08 SC |
72 | wxToolBarTool(wxToolBar *tbar, wxControl *control) |
73 | : wxToolBarToolBase(tbar, control) | |
74 | { | |
bfe9ffbc | 75 | Init() ; |
e56d2520 SC |
76 | if (control != NULL) |
77 | SetControlHandle( (ControlRef) control->GetHandle() ) ; | |
37e2cb08 | 78 | } |
f3a65c3e | 79 | |
bfe9ffbc SC |
80 | ~wxToolBarTool() |
81 | { | |
e56d2520 | 82 | ClearControl() ; |
bfe9ffbc SC |
83 | if ( m_controlHandle ) |
84 | DisposeControl( m_controlHandle ) ; | |
e56d2520 SC |
85 | #if wxMAC_USE_NATIVE_TOOLBAR |
86 | if ( m_toolbarItemRef ) | |
87 | CFRelease( m_toolbarItemRef ) ; | |
88 | #endif | |
bfe9ffbc | 89 | } |
f3a65c3e VZ |
90 | |
91 | WXWidget GetControlHandle() | |
92 | { | |
93 | return (WXWidget) m_controlHandle ; | |
e56d2520 | 94 | } |
f3a65c3e VZ |
95 | |
96 | void SetControlHandle( ControlRef handle ) | |
97 | { | |
98 | m_controlHandle = handle ; | |
e56d2520 | 99 | } |
37e2cb08 | 100 | |
bfe9ffbc | 101 | void SetPosition( const wxPoint& position ) ; |
f3a65c3e VZ |
102 | |
103 | void ClearControl() | |
104 | { | |
105 | m_control = NULL ; | |
e56d2520 | 106 | #if wxMAC_USE_NATIVE_TOOLBAR |
f3a65c3e VZ |
107 | m_toolbarItemRef = NULL ; |
108 | #endif | |
e56d2520 | 109 | } |
f3a65c3e | 110 | |
bfe9ffbc SC |
111 | wxSize GetSize() const |
112 | { | |
113 | if ( IsControl() ) | |
114 | { | |
115 | return GetControl()->GetSize() ; | |
116 | } | |
117 | else if ( IsButton() ) | |
118 | { | |
119 | return GetToolBar()->GetToolSize() ; | |
120 | } | |
121 | else | |
122 | { | |
abbcdf3f | 123 | // separator size |
bfe9ffbc | 124 | wxSize sz = GetToolBar()->GetToolSize() ; |
5a904a32 SC |
125 | if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL ) |
126 | sz.y /= 4 ; | |
127 | else | |
128 | sz.x /= 4 ; | |
bfe9ffbc SC |
129 | return sz ; |
130 | } | |
131 | } | |
132 | wxPoint GetPosition() const | |
133 | { | |
134 | return wxPoint(m_x, m_y); | |
f3a65c3e | 135 | } |
a2fe01c9 | 136 | bool DoEnable( bool enable ) ; |
f3a65c3e | 137 | |
73fd9428 | 138 | void UpdateToggleImage( bool toggle ) ; |
f3a65c3e VZ |
139 | |
140 | #if wxMAC_USE_NATIVE_TOOLBAR | |
141 | void SetToolbarItemRef( HIToolbarItemRef ref ) | |
142 | { | |
e56d2520 SC |
143 | if ( m_controlHandle ) |
144 | HideControl( m_controlHandle ) ; | |
145 | if ( m_toolbarItemRef ) | |
146 | CFRelease( m_toolbarItemRef ) ; | |
f3a65c3e | 147 | m_toolbarItemRef = ref ; |
e56d2520 SC |
148 | if ( m_toolbarItemRef ) |
149 | { | |
150 | HIToolbarItemSetHelpText( | |
151 | m_toolbarItemRef, wxMacCFStringHolder( GetShortHelp() , GetToolBar()->GetFont().GetEncoding() ) , | |
152 | wxMacCFStringHolder( GetLongHelp() , GetToolBar()->GetFont().GetEncoding() ) ) ; | |
153 | } | |
154 | } | |
f3a65c3e VZ |
155 | HIToolbarItemRef GetToolbarItemRef() const |
156 | { | |
157 | return m_toolbarItemRef ; | |
e56d2520 | 158 | } |
2c1dbc95 SC |
159 | |
160 | void SetIndex( CFIndex idx ) | |
161 | { | |
162 | m_index = idx ; | |
163 | } | |
164 | ||
165 | CFIndex GetIndex() const | |
166 | { | |
167 | return m_index ; | |
168 | } | |
e56d2520 | 169 | #endif |
5d0bf05a | 170 | |
bfe9ffbc | 171 | private : |
f3a65c3e | 172 | void Init() |
bfe9ffbc SC |
173 | { |
174 | m_controlHandle = NULL ; | |
e56d2520 SC |
175 | #if wxMAC_USE_NATIVE_TOOLBAR |
176 | m_toolbarItemRef = NULL ; | |
2c1dbc95 | 177 | m_index = -1 ; |
e56d2520 | 178 | #endif |
bfe9ffbc | 179 | } |
facd6764 | 180 | ControlRef m_controlHandle ; |
e56d2520 SC |
181 | #if wxMAC_USE_NATIVE_TOOLBAR |
182 | HIToolbarItemRef m_toolbarItemRef ; | |
2c1dbc95 SC |
183 | // position in its toolbar, -1 means not inserted |
184 | CFIndex m_index ; | |
e56d2520 | 185 | #endif |
bfe9ffbc SC |
186 | wxCoord m_x; |
187 | wxCoord m_y; | |
37e2cb08 SC |
188 | }; |
189 | ||
facd6764 SC |
190 | static const EventTypeSpec eventList[] = |
191 | { | |
192 | { kEventClassControl , kEventControlHit } , | |
73fd9428 SC |
193 | #ifdef __WXMAC_OSX__ |
194 | { kEventClassControl , kEventControlHitTest } , | |
195 | #endif | |
facd6764 SC |
196 | } ; |
197 | ||
198 | static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
199 | { | |
200 | OSStatus result = eventNotHandledErr ; | |
201 | ||
202 | wxMacCarbonEvent cEvent( event ) ; | |
f3a65c3e | 203 | |
facd6764 SC |
204 | ControlRef controlRef ; |
205 | ||
206 | cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ; | |
207 | ||
208 | switch( GetEventKind( event ) ) | |
209 | { | |
210 | case kEventControlHit : | |
211 | { | |
212 | wxToolBarTool* tbartool = (wxToolBarTool*)data ; | |
e56d2520 SC |
213 | wxToolBar *tbar = tbartool != NULL ? ( wxToolBar * ) ( tbartool->GetToolBar() ) : NULL ; |
214 | if ((tbartool != NULL) && tbartool->CanBeToggled() ) | |
facd6764 | 215 | { |
e56d2520 | 216 | bool shouldToggle; |
64f553a4 | 217 | #ifdef __WXMAC_OSX__ |
e56d2520 | 218 | shouldToggle = !tbartool->IsToggled(); |
64f553a4 | 219 | #else |
e56d2520 | 220 | shouldToggle = ( GetControl32BitValue((ControlRef) tbartool->GetControlHandle()) != 0 ); |
64f553a4 | 221 | #endif |
e56d2520 | 222 | tbar->ToggleTool( tbartool->GetId(), shouldToggle ); |
facd6764 | 223 | } |
e56d2520 SC |
224 | if (tbartool != NULL) |
225 | tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() ); | |
f3a65c3e | 226 | result = noErr; |
facd6764 SC |
227 | } |
228 | break ; | |
5d0bf05a | 229 | |
73fd9428 SC |
230 | #ifdef __WXMAC_OSX__ |
231 | case kEventControlHitTest : | |
232 | { | |
233 | HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation) ; | |
234 | HIRect rect ; | |
235 | HIViewGetBounds( controlRef , &rect ) ; | |
f3a65c3e | 236 | |
73fd9428 SC |
237 | ControlPartCode pc = kControlNoPart ; |
238 | if ( CGRectContainsPoint( rect , pt ) ) | |
d60f41f2 | 239 | pc = kControlIconPart ; |
73fd9428 SC |
240 | cEvent.SetParameter( kEventParamControlPart , typeControlPartCode, pc ) ; |
241 | result = noErr ; | |
242 | } | |
243 | break ; | |
244 | #endif | |
5d0bf05a | 245 | |
facd6764 SC |
246 | default : |
247 | break ; | |
248 | } | |
249 | return result ; | |
250 | } | |
251 | ||
0aeeec99 | 252 | static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) |
facd6764 SC |
253 | { |
254 | OSStatus result = eventNotHandledErr ; | |
255 | ||
256 | switch ( GetEventClass( event ) ) | |
257 | { | |
258 | case kEventClassControl : | |
259 | result = wxMacToolBarToolControlEventHandler( handler, event, data ) ; | |
260 | break ; | |
5d0bf05a | 261 | |
facd6764 SC |
262 | default : |
263 | break ; | |
264 | } | |
265 | return result ; | |
266 | } | |
267 | ||
268 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler ) | |
269 | ||
e56d2520 SC |
270 | #if wxMAC_USE_NATIVE_TOOLBAR |
271 | ||
272 | // | |
273 | // native toolbar | |
274 | // | |
275 | ||
276 | static const EventTypeSpec toolBarEventList[] = | |
277 | { | |
278 | { kEventClassToolbarItem , kEventToolbarItemPerformAction } , | |
279 | } ; | |
280 | ||
281 | static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
282 | { | |
283 | OSStatus result = eventNotHandledErr ; | |
f3a65c3e | 284 | |
e56d2520 SC |
285 | switch( GetEventKind( event ) ) |
286 | { | |
287 | case kEventToolbarItemPerformAction : | |
288 | { | |
289 | wxToolBarTool* tbartool = (wxToolBarTool*) data ; | |
290 | if ( tbartool != NULL ) | |
291 | { | |
292 | int toolID = tbartool->GetId(); | |
293 | wxToolBar *tbar = ( wxToolBar * ) ( tbartool->GetToolBar() ); | |
294 | if ( tbartool->CanBeToggled() ) | |
295 | { | |
2c1dbc95 SC |
296 | if ( tbar ) |
297 | tbar->ToggleTool(toolID, !tbartool->IsToggled() ); | |
e56d2520 | 298 | } |
2c1dbc95 SC |
299 | if ( tbar ) |
300 | tbar->OnLeftClick( toolID , tbartool -> IsToggled() ) ; | |
f3a65c3e | 301 | result = noErr; |
e56d2520 SC |
302 | } |
303 | } | |
304 | break ; | |
5d0bf05a | 305 | |
e56d2520 SC |
306 | default : |
307 | break ; | |
308 | } | |
309 | return result ; | |
310 | } | |
311 | ||
312 | static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) | |
313 | { | |
314 | OSStatus result = eventNotHandledErr ; | |
315 | switch( GetEventClass( event ) ) | |
316 | { | |
317 | case kEventClassToolbarItem : | |
318 | result = wxMacToolBarCommandEventHandler( handler, event, data ) ; | |
319 | break ; | |
5d0bf05a | 320 | |
e56d2520 SC |
321 | default : |
322 | break ; | |
323 | } | |
324 | return result ; | |
325 | } | |
326 | ||
327 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler ) | |
328 | ||
329 | #endif | |
330 | ||
37e2cb08 SC |
331 | // ============================================================================ |
332 | // implementation | |
333 | // ============================================================================ | |
334 | ||
335 | // ---------------------------------------------------------------------------- | |
336 | // wxToolBarTool | |
337 | // ---------------------------------------------------------------------------- | |
338 | ||
a2fe01c9 SC |
339 | bool wxToolBarTool::DoEnable(bool enable) |
340 | { | |
341 | if ( IsControl() ) | |
342 | { | |
343 | GetControl()->Enable( enable ) ; | |
344 | } | |
345 | else if ( IsButton() ) | |
346 | { | |
f3a65c3e | 347 | #if wxMAC_USE_NATIVE_TOOLBAR |
e56d2520 SC |
348 | if ( m_toolbarItemRef ) |
349 | HIToolbarItemSetEnabled( m_toolbarItemRef , enable ) ; | |
350 | #endif | |
351 | ||
352 | if ( m_controlHandle ) | |
353 | { | |
a2fe01c9 | 354 | #if TARGET_API_MAC_OSX |
e56d2520 SC |
355 | if ( enable ) |
356 | EnableControl( m_controlHandle ) ; | |
357 | else | |
358 | DisableControl( m_controlHandle ) ; | |
a2fe01c9 | 359 | #else |
e56d2520 SC |
360 | if ( enable ) |
361 | ActivateControl( m_controlHandle ) ; | |
362 | else | |
363 | DeactivateControl( m_controlHandle ) ; | |
a2fe01c9 | 364 | #endif |
e56d2520 | 365 | } |
a2fe01c9 SC |
366 | } |
367 | return true ; | |
368 | } | |
bfe9ffbc SC |
369 | |
370 | void wxToolBarTool::SetPosition(const wxPoint& position) | |
371 | { | |
372 | m_x = position.x; | |
373 | m_y = position.y; | |
374 | ||
789ae0cf SC |
375 | int x , y ; |
376 | x = y = 0 ; | |
377 | int mac_x = position.x ; | |
378 | int mac_y = position.y ; | |
379 | ||
380 | if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() ) | |
bfe9ffbc | 381 | { |
bfe9ffbc | 382 | GetToolBar()->MacWindowToRootWindow( &x , &y ) ; |
facd6764 SC |
383 | mac_x += x; |
384 | mac_y += y; | |
789ae0cf SC |
385 | } |
386 | ||
387 | if ( IsButton() ) | |
388 | { | |
f3a65c3e VZ |
389 | Rect contrlRect ; |
390 | GetControlBounds( m_controlHandle , &contrlRect ) ; | |
bfe9ffbc SC |
391 | int former_mac_x = contrlRect.left ; |
392 | int former_mac_y = contrlRect.top ; | |
80e3f464 | 393 | GetToolBar()->GetToolSize() ; |
f3a65c3e | 394 | |
bfe9ffbc SC |
395 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) |
396 | { | |
bfe9ffbc | 397 | UMAMoveControl( m_controlHandle , mac_x , mac_y ) ; |
bfe9ffbc SC |
398 | } |
399 | } | |
400 | else if ( IsControl() ) | |
401 | { | |
402 | GetControl()->Move( position ) ; | |
403 | } | |
abbcdf3f SC |
404 | else |
405 | { | |
f3a65c3e | 406 | // separator |
abbcdf3f | 407 | #ifdef __WXMAC_OSX__ |
f3a65c3e VZ |
408 | Rect contrlRect ; |
409 | GetControlBounds( m_controlHandle , &contrlRect ) ; | |
abbcdf3f SC |
410 | int former_mac_x = contrlRect.left ; |
411 | int former_mac_y = contrlRect.top ; | |
f3a65c3e | 412 | |
abbcdf3f SC |
413 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) |
414 | { | |
415 | UMAMoveControl( m_controlHandle , mac_x , mac_y ) ; | |
416 | } | |
417 | #endif | |
418 | } | |
bfe9ffbc SC |
419 | } |
420 | ||
f3a65c3e | 421 | void wxToolBarTool::UpdateToggleImage( bool toggle ) |
73fd9428 | 422 | { |
f3a65c3e | 423 | #if wxMAC_USE_NATIVE_TOOLBAR |
e56d2520 SC |
424 | |
425 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4 | |
426 | #define kHIToolbarItemSelected (1 << 7) | |
427 | #endif | |
428 | ||
429 | // FIXME: this should be a OSX v10.4 runtime check | |
430 | if (m_toolbarItemRef != NULL) | |
431 | { | |
432 | OptionBits addAttrs, removeAttrs; | |
433 | OSStatus result; | |
434 | ||
435 | if (toggle) | |
436 | { | |
437 | addAttrs = kHIToolbarItemSelected; | |
438 | removeAttrs = kHIToolbarItemNoAttributes; | |
439 | } | |
440 | else | |
441 | { | |
442 | addAttrs = kHIToolbarItemNoAttributes; | |
443 | removeAttrs = kHIToolbarItemSelected; | |
444 | } | |
445 | ||
446 | result = HIToolbarItemChangeAttributes( m_toolbarItemRef, addAttrs, removeAttrs ); | |
447 | } | |
448 | #endif | |
5d0bf05a | 449 | |
73fd9428 SC |
450 | #ifdef __WXMAC_OSX__ |
451 | if ( toggle ) | |
452 | { | |
453 | int w = m_bmpNormal.GetWidth() ; | |
454 | int h = m_bmpNormal.GetHeight() ; | |
455 | wxBitmap bmp( w , h ) ; | |
456 | wxMemoryDC dc ; | |
457 | dc.SelectObject( bmp ) ; | |
458 | dc.SetPen( wxNullPen ) ; | |
459 | dc.SetBackground( *wxWHITE ) ; | |
460 | dc.DrawRectangle( 0 , 0 , w , h ) ; | |
461 | dc.DrawBitmap( m_bmpNormal , 0 , 0 , true) ; | |
462 | dc.SelectObject( wxNullBitmap ) ; | |
463 | ControlButtonContentInfo info ; | |
464 | wxMacCreateBitmapButton( &info , bmp ) ; | |
465 | SetControlData( m_controlHandle , 0, kControlIconContentTag, sizeof( info ), | |
466 | (Ptr)&info ); | |
467 | wxMacReleaseBitmapButton( &info ) ; | |
468 | } | |
469 | else | |
470 | { | |
471 | ControlButtonContentInfo info ; | |
472 | wxMacCreateBitmapButton( &info , m_bmpNormal ) ; | |
473 | SetControlData( m_controlHandle , 0, kControlIconContentTag, sizeof( info ), | |
474 | (Ptr)&info ); | |
475 | wxMacReleaseBitmapButton( &info ) ; | |
476 | } | |
477 | ||
478 | IconTransformType transform = toggle ? kTransformSelected : kTransformNone ; | |
479 | SetControlData( m_controlHandle, 0, kControlIconTransformTag, sizeof( transform ), | |
480 | (Ptr)&transform ); | |
481 | HIViewSetNeedsDisplay( m_controlHandle , true ) ; | |
482 | ||
483 | #else | |
484 | ::SetControl32BitValue( m_controlHandle , toggle ) ; | |
485 | #endif | |
486 | } | |
487 | ||
bfe9ffbc SC |
488 | wxToolBarTool::wxToolBarTool(wxToolBar *tbar, |
489 | int id, | |
490 | const wxString& label, | |
491 | const wxBitmap& bmpNormal, | |
492 | const wxBitmap& bmpDisabled, | |
493 | wxItemKind kind, | |
494 | wxObject *clientData, | |
495 | const wxString& shortHelp, | |
496 | const wxString& longHelp) | |
497 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind, | |
498 | clientData, shortHelp, longHelp) | |
499 | { | |
f4e0be4f | 500 | Init(); |
bfe9ffbc SC |
501 | } |
502 | ||
e56d2520 SC |
503 | #pragma mark - |
504 | #pragma mark Toolbar Implementation | |
2f1ae414 | 505 | |
37e2cb08 | 506 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
27e2d606 GD |
507 | const wxString& label, |
508 | const wxBitmap& bmpNormal, | |
509 | const wxBitmap& bmpDisabled, | |
510 | wxItemKind kind, | |
37e2cb08 | 511 | wxObject *clientData, |
27e2d606 GD |
512 | const wxString& shortHelp, |
513 | const wxString& longHelp) | |
37e2cb08 | 514 | { |
27e2d606 GD |
515 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, |
516 | clientData, shortHelp, longHelp); | |
37e2cb08 SC |
517 | } |
518 | ||
519 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
520 | { | |
521 | return new wxToolBarTool(this, control); | |
522 | } | |
523 | ||
37e2cb08 | 524 | void wxToolBar::Init() |
e9576ca5 | 525 | { |
e40298d5 JS |
526 | m_maxWidth = -1; |
527 | m_maxHeight = -1; | |
528 | m_defaultWidth = kwxMacToolBarToolDefaultWidth; | |
529 | m_defaultHeight = kwxMacToolBarToolDefaultHeight; | |
e56d2520 SC |
530 | #if wxMAC_USE_NATIVE_TOOLBAR |
531 | m_macHIToolbarRef = NULL ; | |
532 | m_macUsesNativeToolbar = false ; | |
533 | #endif | |
e9576ca5 SC |
534 | } |
535 | ||
e56d2520 SC |
536 | // also for the toolbar we have the dual implementation: |
537 | // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar | |
538 | // | |
e9576ca5 SC |
539 | bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, |
540 | long style, const wxString& name) | |
5d0bf05a | 541 | { |
2c1dbc95 | 542 | if ( !wxToolBarBase::Create( parent , id , pos , size , style, wxDefaultValidator, name ) ) |
3803c372 | 543 | return false ; |
e56d2520 SC |
544 | |
545 | OSStatus err = 0; | |
546 | ||
547 | #if wxMAC_USE_NATIVE_TOOLBAR | |
30962327 | 548 | wxString labelStr = wxString::Format(wxT("%xd"), (int)this); |
e56d2520 SC |
549 | err = HIToolbarCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding() ) , 0 , |
550 | (HIToolbarRef*) &m_macHIToolbarRef ); | |
551 | ||
552 | if (m_macHIToolbarRef != NULL) | |
f3a65c3e | 553 | { |
e56d2520 SC |
554 | HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault ; |
555 | HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall ; | |
556 | ||
557 | if ( style & wxTB_NOICONS ) | |
558 | mode = kHIToolbarDisplayModeLabelOnly ; | |
559 | else if ( style & wxTB_TEXT ) | |
560 | mode = kHIToolbarDisplayModeIconAndLabel ; | |
561 | else | |
562 | mode = kHIToolbarDisplayModeIconOnly ; | |
563 | ||
e56d2520 SC |
564 | HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef , mode ) ; |
565 | HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef , displaySize ) ; | |
566 | } | |
567 | #endif | |
568 | ||
569 | return (err == 0); | |
e9576ca5 SC |
570 | } |
571 | ||
572 | wxToolBar::~wxToolBar() | |
f3a65c3e | 573 | { |
e56d2520 SC |
574 | #if wxMAC_USE_NATIVE_TOOLBAR |
575 | if ( m_macHIToolbarRef ) | |
576 | { | |
577 | // if this is the installed toolbar, then deinstall it | |
578 | if (m_macUsesNativeToolbar) | |
579 | MacInstallNativeToolbar( false ); | |
580 | ||
581 | CFRelease( (HIToolbarRef) m_macHIToolbarRef ); | |
582 | m_macHIToolbarRef = NULL; | |
583 | } | |
584 | #endif | |
585 | } | |
586 | ||
e56d2520 SC |
587 | bool wxToolBar::Show( bool show ) |
588 | { | |
f3a65c3e | 589 | bool bResult; |
e56d2520 | 590 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); |
f3a65c3e | 591 | |
e56d2520 SC |
592 | bResult = (tlw != NULL); |
593 | if (bResult) | |
594 | { | |
595 | #if wxMAC_USE_NATIVE_TOOLBAR | |
f3a65c3e | 596 | bool ownToolbarInstalled = false; |
e56d2520 SC |
597 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); |
598 | if (ownToolbarInstalled) | |
599 | { | |
600 | bResult = (HIViewIsVisible( (HIViewRef)m_macHIToolbarRef ) != show); | |
601 | ShowHideWindowToolbar( tlw, show, false ); | |
602 | } | |
603 | else | |
604 | #endif | |
605 | bResult = wxToolBarBase::Show( show ); | |
606 | } | |
f3a65c3e | 607 | |
e56d2520 SC |
608 | return bResult; |
609 | } | |
610 | ||
611 | bool wxToolBar::IsShown() const | |
612 | { | |
613 | bool bResult; | |
614 | ||
615 | #if wxMAC_USE_NATIVE_TOOLBAR | |
616 | bool ownToolbarInstalled ; | |
617 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
618 | if (ownToolbarInstalled) | |
619 | bResult = HIViewIsVisible( (HIViewRef)m_macHIToolbarRef ); | |
620 | else | |
621 | #endif | |
622 | bResult = wxToolBarBase::IsShown(); | |
f3a65c3e | 623 | |
e56d2520 SC |
624 | return bResult; |
625 | } | |
626 | ||
e56d2520 SC |
627 | void wxToolBar::DoGetSize( int *width, int *height ) const |
628 | { | |
629 | #if wxMAC_USE_NATIVE_TOOLBAR | |
630 | Rect boundsR; | |
631 | bool ownToolbarInstalled; | |
f3a65c3e | 632 | |
e56d2520 SC |
633 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); |
634 | if ( ownToolbarInstalled ) | |
635 | { | |
636 | // TODO is this really a control ? | |
637 | GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR ); | |
638 | if ( width != NULL ) | |
639 | *width = boundsR.right - boundsR.left; | |
640 | if ( height != NULL ) | |
641 | *height = boundsR.bottom - boundsR.top; | |
642 | } | |
643 | else | |
644 | #endif | |
645 | wxToolBarBase::DoGetSize( width, height ); | |
e9576ca5 SC |
646 | } |
647 | ||
f3a65c3e | 648 | void wxToolBar::SetWindowStyleFlag( long style ) |
e56d2520 SC |
649 | { |
650 | wxToolBarBase::SetWindowStyleFlag( style ); | |
651 | #if wxMAC_USE_NATIVE_TOOLBAR | |
652 | if (m_macHIToolbarRef != NULL) | |
653 | { | |
654 | HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault; | |
655 | ||
656 | if ( style & wxTB_NOICONS ) | |
657 | mode = kHIToolbarDisplayModeLabelOnly; | |
658 | else if ( style & wxTB_TEXT ) | |
659 | mode = kHIToolbarDisplayModeIconAndLabel; | |
660 | else | |
661 | mode = kHIToolbarDisplayModeIconOnly; | |
f3a65c3e | 662 | |
e56d2520 SC |
663 | HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode ); |
664 | } | |
665 | #endif | |
666 | } | |
667 | ||
668 | #if wxMAC_USE_NATIVE_TOOLBAR | |
669 | bool wxToolBar::MacWantsNativeToolbar() | |
670 | { | |
671 | return m_macUsesNativeToolbar; | |
672 | } | |
673 | ||
674 | bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const | |
675 | { | |
676 | bool bResultV = false; | |
677 | ||
678 | if (ownToolbarInstalled != NULL) | |
679 | *ownToolbarInstalled = false; | |
680 | ||
681 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); | |
682 | if (tlw != NULL) | |
683 | { | |
684 | HIToolbarRef curToolbarRef = NULL; | |
685 | OSStatus err = GetWindowToolbar( tlw, &curToolbarRef ); | |
686 | bResultV = ((err == 0) && (curToolbarRef != NULL)); | |
687 | if (bResultV && (ownToolbarInstalled != NULL)) | |
688 | *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef); | |
689 | } | |
690 | ||
691 | return bResultV; | |
692 | } | |
693 | ||
f3a65c3e | 694 | bool wxToolBar::MacInstallNativeToolbar(bool usesNative) |
e56d2520 SC |
695 | { |
696 | bool bResult = false; | |
697 | ||
698 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); | |
699 | if (tlw == NULL) | |
700 | return bResult; | |
701 | ||
702 | if (usesNative && (m_macHIToolbarRef == NULL)) | |
703 | return bResult; | |
f3a65c3e | 704 | |
e56d2520 SC |
705 | if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0)) |
706 | return bResult; | |
f3a65c3e | 707 | |
e56d2520 SC |
708 | // check the existing toolbar |
709 | HIToolbarRef curToolbarRef = NULL; | |
710 | OSStatus err = GetWindowToolbar( tlw, &curToolbarRef ); | |
711 | if (err != 0) | |
712 | curToolbarRef = NULL; | |
713 | ||
714 | m_macUsesNativeToolbar = usesNative; | |
715 | ||
716 | if (m_macUsesNativeToolbar) | |
717 | { | |
718 | // only install toolbar if there isn't one installed already | |
719 | if (curToolbarRef == NULL) | |
720 | { | |
721 | bResult = true; | |
722 | ||
723 | SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef ); | |
724 | ShowHideWindowToolbar( tlw, true, false ); | |
725 | ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 ); | |
726 | SetAutomaticControlDragTrackingEnabledForWindow( tlw, true ); | |
f3a65c3e | 727 | |
e56d2520 SC |
728 | // FIXME: which is best, which is necessary? |
729 | // | |
730 | // m_peer->SetVisibility( false, true ); | |
731 | // | |
732 | // | |
733 | Rect r = { 0 , 0 , 0 , 0 }; | |
734 | // | |
735 | // | |
736 | m_peer->SetRect( &r ); | |
737 | // | |
738 | // FIXME: which is best, which is necessary? | |
739 | // | |
740 | SetSize( wxSIZE_AUTO_WIDTH, 0 ); | |
741 | // | |
742 | m_peer->SetVisibility( false, true ); | |
743 | wxToolBarBase::Show( false ); | |
744 | } | |
745 | } | |
746 | else | |
747 | { | |
748 | // only deinstall toolbar if this is the installed one | |
749 | if (m_macHIToolbarRef == curToolbarRef) | |
750 | { | |
751 | bResult = true; | |
752 | ||
753 | ShowHideWindowToolbar( tlw, false, false ); | |
754 | ChangeWindowAttributes( tlw, 0 , kWindowToolbarButtonAttribute ); | |
755 | SetWindowToolbar( tlw, NULL ); | |
f3a65c3e | 756 | |
e56d2520 SC |
757 | // FIXME: which is best, which is necessary? |
758 | m_peer->SetVisibility( true, true ); | |
f3a65c3e | 759 | |
e56d2520 SC |
760 | // |
761 | // wxToolBarBase::Show( true ); | |
762 | // | |
763 | } | |
764 | } | |
765 | ||
766 | if (bResult) | |
767 | InvalidateBestSize(); | |
768 | ||
769 | // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") ); | |
770 | return bResult; | |
771 | } | |
772 | #endif | |
773 | ||
37e2cb08 | 774 | bool wxToolBar::Realize() |
e9576ca5 | 775 | { |
eb22f2a6 | 776 | if (m_tools.GetCount() == 0) |
3803c372 | 777 | return false; |
0b7a8cd3 | 778 | |
2c1dbc95 SC |
779 | OSStatus err = noErr ; |
780 | #if wxMAC_USE_NATIVE_TOOLBAR | |
781 | // remove all tools, no way to determine how many there are in a toolbar, so just a high number :-( | |
782 | if ( m_macHIToolbarRef != NULL ) | |
783 | { | |
784 | for ( CFIndex i = 0 ; i < 100 ; ++i ) | |
785 | { | |
786 | err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef , i ) ; | |
787 | } | |
788 | } | |
789 | #endif | |
790 | ||
e56d2520 SC |
791 | int x = m_xMargin + kwxMacToolBarLeftMargin; |
792 | int y = m_yMargin + kwxMacToolBarTopMargin; | |
f3a65c3e | 793 | |
7c551d95 | 794 | int tw, th; |
e56d2520 | 795 | GetSize( &tw, &th ); |
f3a65c3e | 796 | |
e56d2520 SC |
797 | int maxWidth = 0; |
798 | int maxHeight = 0; | |
f3a65c3e | 799 | |
bfe9ffbc SC |
800 | int maxToolWidth = 0; |
801 | int maxToolHeight = 0; | |
f3a65c3e | 802 | |
e56d2520 SC |
803 | // find the maximum tool width and height |
804 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
805 | while ( node != NULL ) | |
bfe9ffbc | 806 | { |
e56d2520 | 807 | wxToolBarTool *tool = (wxToolBarTool *) node->GetData(); |
f3a65c3e | 808 | |
e56d2520 SC |
809 | if ( tool != NULL ) |
810 | { | |
811 | wxSize sz = tool->GetSize(); | |
f3a65c3e | 812 | |
e56d2520 SC |
813 | if ( sz.x > maxToolWidth ) |
814 | maxToolWidth = sz.x; | |
815 | if ( sz.y > maxToolHeight ) | |
816 | maxToolHeight = sz.y; | |
817 | } | |
f3a65c3e | 818 | |
bfe9ffbc SC |
819 | node = node->GetNext(); |
820 | } | |
f3a65c3e | 821 | |
e56d2520 SC |
822 | bool lastIsRadio = false; |
823 | bool curIsRadio = false; | |
824 | bool setChoiceInGroup = false; | |
f3a65c3e | 825 | |
bfe9ffbc | 826 | node = m_tools.GetFirst(); |
2c1dbc95 SC |
827 | |
828 | CFIndex currentPosition = 0 ; | |
829 | ||
e56d2520 | 830 | while ( node != NULL ) |
7810c95b | 831 | { |
e56d2520 | 832 | wxToolBarTool *tool = (wxToolBarTool *) node->GetData(); |
f3a65c3e | 833 | |
e56d2520 | 834 | if ( tool == NULL ) |
214b9484 | 835 | { |
e56d2520 SC |
836 | node = node->GetNext(); |
837 | continue; | |
214b9484 | 838 | } |
f3a65c3e | 839 | |
e56d2520 SC |
840 | // set tool position |
841 | // for the moment just perform a single row/column alignment | |
842 | wxSize cursize = tool->GetSize(); | |
bfe9ffbc | 843 | if ( x + cursize.x > maxWidth ) |
e56d2520 | 844 | maxWidth = x + cursize.x; |
bfe9ffbc | 845 | if ( y + cursize.y > maxHeight ) |
e56d2520 | 846 | maxHeight = y + cursize.y; |
f3a65c3e | 847 | |
73fd9428 SC |
848 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
849 | { | |
e56d2520 SC |
850 | int x1 = x + ( maxToolWidth - cursize.x ) / 2; |
851 | tool->SetPosition( wxPoint(x1, y) ); | |
73fd9428 SC |
852 | } |
853 | else | |
854 | { | |
e56d2520 SC |
855 | int y1 = y + ( maxToolHeight - cursize.y ) / 2; |
856 | tool->SetPosition( wxPoint(x, y1) ); | |
857 | } | |
f3a65c3e | 858 | |
e56d2520 | 859 | // update the item positioning state |
bfe9ffbc | 860 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
e56d2520 SC |
861 | y += cursize.y + kwxMacToolSpacing; |
862 | else | |
863 | x += cursize.x + kwxMacToolSpacing; | |
f3a65c3e | 864 | |
e56d2520 SC |
865 | #if wxMAC_USE_NATIVE_TOOLBAR |
866 | // install in native HIToolbar | |
867 | if ( m_macHIToolbarRef != NULL ) | |
bfe9ffbc | 868 | { |
e56d2520 SC |
869 | HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef(); |
870 | if ( hiItemRef != NULL ) | |
871 | { | |
2c1dbc95 | 872 | OSStatus result = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef , currentPosition ) ; |
e56d2520 SC |
873 | if ( result == 0 ) |
874 | { | |
2c1dbc95 SC |
875 | tool->SetIndex( currentPosition ) ; |
876 | currentPosition++ ; | |
e56d2520 SC |
877 | InstallEventHandler( HIObjectGetEventTarget(hiItemRef), GetwxMacToolBarEventHandlerUPP(), |
878 | GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL ); | |
879 | } | |
880 | } | |
881 | } | |
882 | #endif | |
f3a65c3e | 883 | |
e56d2520 SC |
884 | // update radio button (and group) state |
885 | lastIsRadio = curIsRadio; | |
886 | curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) ); | |
f3a65c3e | 887 | |
e56d2520 SC |
888 | if ( !curIsRadio ) |
889 | { | |
890 | if ( tool->IsToggled() ) | |
891 | DoToggleTool( tool, true ); | |
f3a65c3e | 892 | |
e56d2520 | 893 | setChoiceInGroup = false; |
0b7a8cd3 GD |
894 | } |
895 | else | |
896 | { | |
e56d2520 SC |
897 | if ( !lastIsRadio ) |
898 | { | |
899 | if ( tool->Toggle(true) ) | |
900 | { | |
901 | DoToggleTool( tool, true ); | |
902 | setChoiceInGroup = true; | |
903 | } | |
904 | } | |
905 | else if ( tool->IsToggled() ) | |
906 | { | |
907 | if ( tool->IsToggled() ) | |
908 | DoToggleTool( tool, true ); | |
f3a65c3e | 909 | |
e56d2520 SC |
910 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); |
911 | while ( nodePrev != NULL ) | |
912 | { | |
913 | wxToolBarToolBase *toggleTool = nodePrev->GetData(); | |
914 | if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) ) | |
915 | break; | |
f3a65c3e | 916 | |
e56d2520 SC |
917 | if ( toggleTool->Toggle(false) ) |
918 | DoToggleTool( toggleTool, false ); | |
f3a65c3e | 919 | |
e56d2520 SC |
920 | nodePrev = nodePrev->GetPrevious(); |
921 | } | |
922 | } | |
0b7a8cd3 | 923 | } |
f3a65c3e | 924 | |
eb22f2a6 | 925 | node = node->GetNext(); |
7810c95b | 926 | } |
f3a65c3e | 927 | |
0b7a8cd3 | 928 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) |
7810c95b | 929 | { |
e56d2520 SC |
930 | // if not set yet, only one row |
931 | if ( m_maxRows <= 0 ) | |
932 | SetRows( 1 ); | |
f3a65c3e | 933 | |
90d3f91a | 934 | m_minWidth = maxWidth; |
e56d2520 | 935 | maxWidth = tw; |
0b7a8cd3 | 936 | maxHeight += m_yMargin + kwxMacToolBarTopMargin; |
e56d2520 | 937 | m_minHeight = m_maxHeight = maxHeight; |
7810c95b | 938 | } |
0b7a8cd3 GD |
939 | else |
940 | { | |
e56d2520 SC |
941 | // if not set yet, have one column |
942 | if ( (GetToolsCount() > 0) && (m_maxRows <= 0) ) | |
943 | SetRows( GetToolsCount() ); | |
f3a65c3e | 944 | |
90d3f91a | 945 | m_minHeight = maxHeight; |
e56d2520 | 946 | maxHeight = th; |
0b7a8cd3 | 947 | maxWidth += m_xMargin + kwxMacToolBarLeftMargin; |
e56d2520 | 948 | m_minWidth = m_maxWidth = maxWidth; |
0b7a8cd3 | 949 | } |
e56d2520 | 950 | |
f3a65c3e | 951 | #if 0 |
e56d2520 SC |
952 | // FIXME: should this be OSX-only? |
953 | { | |
954 | bool wantNativeToolbar, ownToolbarInstalled; | |
955 | ||
956 | // attempt to install the native toolbar | |
957 | wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0); | |
958 | MacInstallNativeToolbar( wantNativeToolbar ); | |
959 | (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
960 | if (!ownToolbarInstalled) | |
961 | { | |
962 | SetSize( maxWidth, maxHeight ); | |
963 | InvalidateBestSize(); | |
964 | } | |
965 | } | |
f3a65c3e | 966 | #else |
facd6764 | 967 | SetSize( maxWidth, maxHeight ); |
9f884528 | 968 | InvalidateBestSize(); |
e56d2520 | 969 | #endif |
2c1dbc95 SC |
970 | |
971 | SetBestFittingSize(); | |
972 | ||
3803c372 | 973 | return true; |
e9576ca5 SC |
974 | } |
975 | ||
976 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
977 | { | |
e56d2520 SC |
978 | m_defaultWidth = size.x + kwxMacToolBorder; |
979 | m_defaultHeight = size.y + kwxMacToolBorder; | |
f3a65c3e | 980 | |
e56d2520 SC |
981 | #if wxMAC_USE_NATIVE_TOOLBAR |
982 | if (m_macHIToolbarRef != NULL) | |
983 | { | |
984 | int maxs = wxMax( size.x, size.y ); | |
328f4fee SC |
985 | HIToolbarDisplaySize sizeSpec ; |
986 | if ( maxs > 32 ) | |
328f4fee | 987 | sizeSpec = kHIToolbarDisplaySizeNormal ; |
00a7bd85 SC |
988 | else if ( maxs > 24 ) |
989 | sizeSpec = kHIToolbarDisplaySizeDefault ; | |
328f4fee SC |
990 | else |
991 | sizeSpec = kHIToolbarDisplaySizeSmall ; | |
f3a65c3e | 992 | |
e56d2520 SC |
993 | HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec ); |
994 | } | |
995 | #endif | |
e9576ca5 SC |
996 | } |
997 | ||
e9576ca5 SC |
998 | // The button size is bigger than the bitmap size |
999 | wxSize wxToolBar::GetToolSize() const | |
1000 | { | |
ee799df7 | 1001 | return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder); |
e9576ca5 SC |
1002 | } |
1003 | ||
37e2cb08 | 1004 | void wxToolBar::SetRows(int nRows) |
e9576ca5 | 1005 | { |
e56d2520 SC |
1006 | // avoid resizing the frame uselessly |
1007 | if ( nRows != m_maxRows ) | |
e9576ca5 | 1008 | { |
e56d2520 | 1009 | m_maxRows = nRows; |
e9576ca5 SC |
1010 | } |
1011 | } | |
1012 | ||
f3a65c3e | 1013 | void wxToolBar::MacSuperChangedPosition() |
c257d44d | 1014 | { |
e56d2520 SC |
1015 | wxWindow::MacSuperChangedPosition(); |
1016 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1017 | if (! m_macUsesNativeToolbar ) | |
1018 | #endif | |
1019 | { | |
1020 | Realize(); | |
1021 | } | |
c257d44d SC |
1022 | } |
1023 | ||
37e2cb08 | 1024 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
e9576ca5 | 1025 | { |
affd2611 | 1026 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
e56d2520 | 1027 | while ( node != NULL ) |
e044f600 | 1028 | { |
bfe9ffbc | 1029 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ; |
e56d2520 SC |
1030 | |
1031 | if (tool != NULL) | |
e044f600 | 1032 | { |
e56d2520 SC |
1033 | wxRect2DInt r( tool->GetPosition(), tool->GetSize() ); |
1034 | if ( r.Contains( wxPoint( x, y ) ) ) | |
1035 | return tool; | |
e044f600 | 1036 | } |
bfe9ffbc SC |
1037 | |
1038 | node = node->GetNext(); | |
e044f600 | 1039 | } |
37e2cb08 SC |
1040 | |
1041 | return (wxToolBarToolBase *)NULL; | |
e9576ca5 SC |
1042 | } |
1043 | ||
2f1ae414 SC |
1044 | wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) |
1045 | { | |
e044f600 | 1046 | wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ; |
e56d2520 | 1047 | if ( tool != NULL ) |
e044f600 | 1048 | return tool->GetShortHelp() ; |
e56d2520 | 1049 | |
427ff662 | 1050 | return wxEmptyString ; |
2f1ae414 SC |
1051 | } |
1052 | ||
37e2cb08 | 1053 | void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) |
e9576ca5 | 1054 | { |
e56d2520 SC |
1055 | if ( t != NULL ) |
1056 | ((wxToolBarTool*)t)->DoEnable( enable ) ; | |
e9576ca5 SC |
1057 | } |
1058 | ||
37e2cb08 | 1059 | void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) |
e9576ca5 | 1060 | { |
e044f600 | 1061 | wxToolBarTool *tool = (wxToolBarTool *)t; |
e56d2520 SC |
1062 | if ( ( tool != NULL ) && tool->IsButton() ) |
1063 | tool->UpdateToggleImage( toggle ); | |
37e2cb08 | 1064 | } |
7c551d95 | 1065 | |
37e2cb08 | 1066 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), |
be5fe3aa | 1067 | wxToolBarToolBase *toolBase) |
37e2cb08 | 1068 | { |
e56d2520 SC |
1069 | wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolBase ); |
1070 | if (tool == NULL) | |
1071 | return false; | |
1072 | ||
1073 | WindowRef window = (WindowRef) MacGetTopLevelWindowRef(); | |
1074 | wxSize toolSize = GetToolSize(); | |
1075 | Rect toolrect = { 0, 0 , toolSize.y , toolSize.x }; | |
1076 | ControlRef controlHandle = NULL; | |
1077 | OSStatus err = 0; | |
be5fe3aa | 1078 | |
e56d2520 | 1079 | switch (tool->GetStyle()) |
be5fe3aa SC |
1080 | { |
1081 | case wxTOOL_STYLE_SEPARATOR : | |
1082 | { | |
e56d2520 SC |
1083 | wxASSERT( tool->GetControlHandle() == NULL ); |
1084 | toolSize.x /= 4; | |
1085 | toolSize.y /= 4; | |
be5fe3aa | 1086 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
e56d2520 | 1087 | toolrect.bottom = toolSize.y; |
be5fe3aa | 1088 | else |
e56d2520 SC |
1089 | toolrect.right = toolSize.x; |
1090 | ||
be5fe3aa SC |
1091 | #ifdef __WXMAC_OSX__ |
1092 | // in flat style we need a visual separator | |
e56d2520 SC |
1093 | #if wxMAC_USE_NATIVE_TOOLBAR |
1094 | HIToolbarItemRef item; | |
1095 | err = HIToolbarItemCreate( kHIToolbarSeparatorIdentifier, kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates, &item ); | |
1096 | if (err == noErr) | |
1097 | tool->SetToolbarItemRef( item ); | |
1098 | #endif | |
1099 | CreateSeparatorControl( window, &toolrect, &controlHandle ); | |
1100 | tool->SetControlHandle( controlHandle ); | |
be5fe3aa SC |
1101 | #endif |
1102 | } | |
e56d2520 SC |
1103 | break; |
1104 | ||
be5fe3aa SC |
1105 | case wxTOOL_STYLE_BUTTON : |
1106 | { | |
1107 | wxASSERT( tool->GetControlHandle() == NULL ) ; | |
1108 | ControlButtonContentInfo info ; | |
1109 | wxMacCreateBitmapButton( &info , tool->GetNormalBitmap() , kControlContentIconRef ) ; | |
f3a65c3e | 1110 | |
e56d2520 SC |
1111 | if ( UMAGetSystemVersion() >= 0x1000) |
1112 | CreateIconControl( window , &toolrect , &info , false , &controlHandle ) ; | |
1113 | else | |
1114 | { | |
1115 | SInt16 behaviour = kControlBehaviorOffsetContents ; | |
1116 | if ( tool->CanBeToggled() ) | |
1117 | behaviour += kControlBehaviorToggles ; | |
f3a65c3e | 1118 | CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info , |
e56d2520 | 1119 | 0 , 0 , 0 , &controlHandle ) ; |
f3a65c3e | 1120 | } |
e56d2520 SC |
1121 | |
1122 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1123 | HIToolbarItemRef item ; | |
30962327 | 1124 | wxString labelStr = wxString::Format(wxT("%xd"), (int)tool); |
e56d2520 SC |
1125 | err = HIToolbarItemCreate( |
1126 | wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()), | |
1127 | kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item ); | |
1128 | if (err == noErr) | |
1129 | { | |
1130 | HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) ); | |
1131 | HIToolbarItemSetIconRef( item, info.u.iconRef ); | |
2c1dbc95 | 1132 | HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction ); |
e56d2520 SC |
1133 | tool->SetToolbarItemRef( item ); |
1134 | } | |
1135 | #endif | |
1136 | ||
be5fe3aa SC |
1137 | wxMacReleaseBitmapButton( &info ) ; |
1138 | /* | |
1139 | SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ; | |
1140 | UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ; | |
1141 | */ | |
f3a65c3e | 1142 | |
be5fe3aa | 1143 | InstallControlEventHandler( (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(), |
e56d2520 | 1144 | GetEventTypeCount(eventList), eventList, tool, NULL ); |
be5fe3aa | 1145 | |
e56d2520 | 1146 | tool->SetControlHandle( controlHandle ); |
be5fe3aa | 1147 | } |
e56d2520 SC |
1148 | break; |
1149 | ||
be5fe3aa | 1150 | case wxTOOL_STYLE_CONTROL : |
e56d2520 SC |
1151 | wxASSERT( tool->GetControl() != NULL ); |
1152 | #if 0 // wxMAC_USE_NATIVE_TOOLBAR | |
1153 | // FIXME: doesn't work yet... | |
1154 | { | |
1155 | HIToolbarItemRef item; | |
30962327 | 1156 | wxString labelStr = wxString::Format( wxT("%xd"), (int) tool ); |
e56d2520 SC |
1157 | result = HIToolbarItemCreate( wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()), |
1158 | kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, | |
1159 | &item ); | |
1160 | if ( result == 0 ) | |
1161 | { | |
1162 | HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) ); | |
1163 | HIToolbarItemSetCommandID( item, tool->GetId() ); | |
1164 | tool->SetToolbarItemRef( item ); | |
f3a65c3e | 1165 | |
e56d2520 SC |
1166 | controlHandle = ( ControlRef ) tool->GetControlHandle(); |
1167 | wxASSERT_MSG( controlHandle != NULL, wxT("NULL tool control") ); | |
f3a65c3e | 1168 | |
e56d2520 SC |
1169 | // FIXME: is this necessary ?? |
1170 | ::GetControlBounds( controlHandle, &toolrect ); | |
1171 | UMAMoveControl( controlHandle, -toolrect.left, -toolrect.top ); | |
f3a65c3e | 1172 | |
e56d2520 SC |
1173 | // FIXME: is this necessary ?? |
1174 | InstallControlEventHandler( controlHandle, GetwxMacToolBarToolEventHandlerUPP(), | |
1175 | GetEventTypeCount(eventList), eventList, tool, NULL ); | |
1176 | } | |
1177 | } | |
f3a65c3e | 1178 | |
e56d2520 SC |
1179 | #else |
1180 | // FIXME: right now there's nothing to do here | |
1181 | #endif | |
1182 | break; | |
1183 | ||
1184 | default : | |
1185 | break; | |
be5fe3aa | 1186 | } |
f3a65c3e | 1187 | |
e56d2520 | 1188 | if ( err == 0 ) |
be5fe3aa | 1189 | { |
e56d2520 SC |
1190 | if ( controlHandle ) |
1191 | { | |
1192 | ControlRef container = (ControlRef) GetHandle(); | |
1193 | wxASSERT_MSG( container != NULL, wxT("No valid mac container control") ); | |
be5fe3aa | 1194 | |
e56d2520 SC |
1195 | UMAShowControl( controlHandle ); |
1196 | ::EmbedControl( controlHandle, container ); | |
1197 | } | |
1198 | ||
1199 | if ( tool->CanBeToggled() && tool->IsToggled() ) | |
1200 | tool->UpdateToggleImage( true ); | |
be5fe3aa | 1201 | |
e56d2520 SC |
1202 | // nothing special to do here - we relayout in Realize() later |
1203 | tool->Attach(this); | |
1204 | InvalidateBestSize(); | |
1205 | } | |
1206 | else | |
be5fe3aa | 1207 | { |
30962327 | 1208 | wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long) err ); |
e56d2520 | 1209 | wxASSERT_MSG( false, errMsg.c_str() ); |
be5fe3aa | 1210 | } |
f3a65c3e | 1211 | |
e56d2520 | 1212 | return( err == 0 ); |
37e2cb08 | 1213 | } |
e9576ca5 | 1214 | |
5115c51a | 1215 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) |
37e2cb08 | 1216 | { |
5115c51a | 1217 | wxFAIL_MSG( _T("not implemented") ); |
e9576ca5 SC |
1218 | } |
1219 | ||
be5fe3aa | 1220 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) |
37e2cb08 | 1221 | { |
be5fe3aa | 1222 | wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolbase ) ; |
affd2611 | 1223 | wxToolBarToolsList::compatibility_iterator node; |
bfe9ffbc SC |
1224 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
1225 | { | |
1226 | wxToolBarToolBase *tool2 = node->GetData(); | |
1227 | if ( tool2 == tool ) | |
1228 | { | |
1229 | // let node point to the next node in the list | |
1230 | node = node->GetNext(); | |
1231 | ||
1232 | break; | |
1233 | } | |
1234 | } | |
1235 | ||
1236 | wxSize sz = ((wxToolBarTool*)tool)->GetSize() ; | |
1237 | ||
1238 | tool->Detach(); | |
2c1dbc95 SC |
1239 | |
1240 | CFIndex removeIndex = tool->GetIndex() ; | |
bfe9ffbc | 1241 | |
488b2c29 SC |
1242 | switch ( tool->GetStyle() ) |
1243 | { | |
1244 | case wxTOOL_STYLE_CONTROL: | |
be5fe3aa SC |
1245 | { |
1246 | tool->GetControl()->Destroy(); | |
e56d2520 | 1247 | tool->ClearControl() ; |
be5fe3aa | 1248 | } |
488b2c29 SC |
1249 | break; |
1250 | ||
1251 | case wxTOOL_STYLE_BUTTON: | |
1252 | case wxTOOL_STYLE_SEPARATOR: | |
be5fe3aa | 1253 | if ( tool->GetControlHandle() ) |
488b2c29 | 1254 | { |
be5fe3aa | 1255 | DisposeControl( (ControlRef) tool->GetControlHandle() ) ; |
e56d2520 | 1256 | #if wxMAC_USE_NATIVE_TOOLBAR |
2c1dbc95 SC |
1257 | if ( removeIndex != -1 && m_macHIToolbarRef ) |
1258 | { | |
1259 | HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef , removeIndex ) ; | |
1260 | tool->SetIndex( -1 ) ; | |
1261 | } | |
e56d2520 SC |
1262 | #endif |
1263 | tool->ClearControl() ; | |
488b2c29 SC |
1264 | } |
1265 | break; | |
e56d2520 SC |
1266 | |
1267 | default: | |
1268 | break; | |
488b2c29 SC |
1269 | } |
1270 | ||
bfe9ffbc | 1271 | // and finally reposition all the controls after this one |
f3a65c3e | 1272 | |
bfe9ffbc SC |
1273 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) |
1274 | { | |
1275 | wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); | |
1276 | wxPoint pt = tool2->GetPosition() ; | |
1277 | ||
1278 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
bfe9ffbc | 1279 | pt.y -= sz.y ; |
bfe9ffbc | 1280 | else |
bfe9ffbc | 1281 | pt.x -= sz.x ; |
e56d2520 | 1282 | |
bfe9ffbc | 1283 | tool2->SetPosition( pt ) ; |
2c1dbc95 SC |
1284 | |
1285 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1286 | if ( removeIndex != -1 && tool2->GetIndex() > removeIndex ) | |
1287 | tool2->SetIndex( tool2->GetIndex() - 1 ) ; | |
1288 | #endif | |
1289 | ||
bfe9ffbc | 1290 | } |
f3a65c3e | 1291 | |
9f884528 | 1292 | InvalidateBestSize(); |
3803c372 | 1293 | return true ; |
37e2cb08 | 1294 | } |
2f1ae414 SC |
1295 | |
1296 | void wxToolBar::OnPaint(wxPaintEvent& event) | |
1297 | { | |
e56d2520 SC |
1298 | #if wxMAC_USE_NATIVE_TOOLBAR |
1299 | if ( m_macUsesNativeToolbar ) | |
1300 | { | |
1301 | event.Skip(true) ; | |
1302 | return ; | |
1303 | } | |
1304 | #endif | |
f3a65c3e | 1305 | |
e40298d5 | 1306 | wxPaintDC dc(this) ; |
ddc548ec | 1307 | |
facd6764 SC |
1308 | int w, h ; |
1309 | GetSize( &w , &h ) ; | |
ddc548ec SC |
1310 | #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 |
1311 | if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() ) | |
1312 | { | |
1313 | if ( UMAGetSystemVersion() >= 0x1030 ) | |
1314 | { | |
1315 | HIThemePlacardDrawInfo info ; | |
1316 | memset( &info, 0 , sizeof( info ) ) ; | |
1317 | info.version = 0 ; | |
1318 | info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ; | |
f3a65c3e | 1319 | |
ddc548ec SC |
1320 | CGContextRef cgContext = (CGContextRef) MacGetCGContextRef() ; |
1321 | HIRect rect = CGRectMake( 0 , 0 , w , h ) ; | |
1322 | HIThemeDrawPlacard( &rect , & info , cgContext, kHIThemeOrientationNormal) ; | |
1323 | } | |
1324 | } | |
1325 | else | |
1326 | { | |
1327 | // leave the background as it is (striped or metal) | |
1328 | } | |
1329 | #else | |
1330 | wxMacPortSetter helper(&dc) ; | |
f3a65c3e VZ |
1331 | |
1332 | Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) , | |
facd6764 SC |
1333 | dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ; |
1334 | /* | |
1335 | if( toolbarrect.left < 0 ) | |
1336 | toolbarrect.left = 0 ; | |
1337 | if ( toolbarrect.top < 0 ) | |
1338 | toolbarrect.top = 0 ; | |
1339 | */ | |
01ffa8f7 SC |
1340 | if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() ) |
1341 | { | |
1342 | UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ; | |
1343 | } | |
1344 | else | |
1345 | { | |
1346 | #if TARGET_API_MAC_OSX | |
1347 | #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 | |
20b69855 SC |
1348 | if ( UMAGetSystemVersion() >= 0x1030 ) |
1349 | { | |
f3a65c3e | 1350 | HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) , |
20b69855 SC |
1351 | dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) ); |
1352 | CGContextRef cgContext ; | |
1353 | Rect bounds ; | |
1354 | GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ; | |
1355 | QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ; | |
1356 | CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ; | |
1357 | CGContextScaleCTM( cgContext , 1 , -1 ) ; | |
785f5eaa | 1358 | |
20b69855 SC |
1359 | { |
1360 | HIThemeBackgroundDrawInfo drawInfo ; | |
1361 | drawInfo.version = 0 ; | |
1362 | drawInfo.state = kThemeStateActive ; | |
1363 | drawInfo.kind = kThemeBackgroundMetal ; | |
1364 | HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ; | |
1365 | } | |
e56d2520 | 1366 | |
20b69855 SC |
1367 | QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ; |
1368 | } | |
1369 | else | |
1370 | #endif | |
01ffa8f7 | 1371 | { |
20b69855 | 1372 | UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ; |
01ffa8f7 | 1373 | } |
01ffa8f7 | 1374 | #endif |
01ffa8f7 SC |
1375 | } |
1376 | #endif | |
20b69855 | 1377 | |
facd6764 | 1378 | event.Skip() ; |
2f1ae414 | 1379 | } |
895f5af7 | 1380 | |
519cb848 SC |
1381 | #endif // wxUSE_TOOLBAR |
1382 |