]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: toolbar.cpp | |
3 | // Purpose: wxToolBar | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: The wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "toolbar.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/wx.h" | |
17 | ||
18 | #if wxUSE_TOOLBAR | |
19 | ||
20 | #include "wx/toolbar.h" | |
21 | #include "wx/notebook.h" | |
22 | #include "wx/tabctrl.h" | |
23 | #include "wx/bitmap.h" | |
24 | ||
25 | #if !USE_SHARED_LIBRARY | |
26 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) | |
27 | ||
28 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
29 | EVT_PAINT( wxToolBar::OnPaint ) | |
30 | END_EVENT_TABLE() | |
31 | #endif | |
32 | ||
33 | #include "wx/mac/uma.h" | |
34 | #include "wx/geometry.h" | |
35 | // ---------------------------------------------------------------------------- | |
36 | // private classes | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | class wxToolBarTool : public wxToolBarToolBase | |
40 | { | |
41 | public: | |
42 | wxToolBarTool(wxToolBar *tbar, | |
43 | int id, | |
44 | const wxString& label, | |
45 | const wxBitmap& bmpNormal, | |
46 | const wxBitmap& bmpDisabled, | |
47 | wxItemKind kind, | |
48 | wxObject *clientData, | |
49 | const wxString& shortHelp, | |
50 | const wxString& longHelp) ; | |
51 | ||
52 | wxToolBarTool(wxToolBar *tbar, wxControl *control) | |
53 | : wxToolBarToolBase(tbar, control) | |
54 | { | |
55 | Init() ; | |
56 | } | |
57 | ||
58 | ~wxToolBarTool() | |
59 | { | |
60 | if ( m_controlHandle ) | |
61 | DisposeControl( m_controlHandle ) ; | |
62 | } | |
63 | ||
64 | WXWidget GetControlHandle() { return (WXWidget) m_controlHandle ; } | |
65 | void SetControlHandle( ControlRef handle ) { m_controlHandle = handle ; } | |
66 | ||
67 | void SetSize(const wxSize& size) ; | |
68 | void SetPosition( const wxPoint& position ) ; | |
69 | wxSize GetSize() const | |
70 | { | |
71 | if ( IsControl() ) | |
72 | { | |
73 | return GetControl()->GetSize() ; | |
74 | } | |
75 | else if ( IsButton() ) | |
76 | { | |
77 | return GetToolBar()->GetToolSize() ; | |
78 | } | |
79 | else | |
80 | { | |
81 | wxSize sz = GetToolBar()->GetToolSize() ; | |
82 | sz.x /= 4 ; | |
83 | sz.y /= 4 ; | |
84 | return sz ; | |
85 | } | |
86 | } | |
87 | wxPoint GetPosition() const | |
88 | { | |
89 | return wxPoint(m_x, m_y); | |
90 | } | |
91 | private : | |
92 | void Init() | |
93 | { | |
94 | m_controlHandle = NULL ; | |
95 | } | |
96 | ControlRef m_controlHandle ; | |
97 | ||
98 | wxCoord m_x; | |
99 | wxCoord m_y; | |
100 | }; | |
101 | ||
102 | static const EventTypeSpec eventList[] = | |
103 | { | |
104 | { kEventClassControl , kEventControlHit } , | |
105 | } ; | |
106 | ||
107 | static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
108 | { | |
109 | OSStatus result = eventNotHandledErr ; | |
110 | ||
111 | wxMacCarbonEvent cEvent( event ) ; | |
112 | ||
113 | ControlRef controlRef ; | |
114 | ||
115 | cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ; | |
116 | ||
117 | switch( GetEventKind( event ) ) | |
118 | { | |
119 | case kEventControlHit : | |
120 | { | |
121 | wxToolBarTool* tbartool = (wxToolBarTool*)data ; | |
122 | if ( tbartool->CanBeToggled() ) | |
123 | { | |
124 | tbartool->Toggle( GetControl32BitValue( (ControlRef) tbartool->GetControlHandle() ) ) ; | |
125 | } | |
126 | ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ; | |
127 | ||
128 | result = noErr; | |
129 | } | |
130 | break ; | |
131 | default : | |
132 | break ; | |
133 | } | |
134 | return result ; | |
135 | } | |
136 | ||
137 | pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
138 | { | |
139 | OSStatus result = eventNotHandledErr ; | |
140 | ||
141 | switch ( GetEventClass( event ) ) | |
142 | { | |
143 | case kEventClassControl : | |
144 | result = wxMacToolBarToolControlEventHandler( handler, event, data ) ; | |
145 | break ; | |
146 | default : | |
147 | break ; | |
148 | } | |
149 | return result ; | |
150 | } | |
151 | ||
152 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler ) | |
153 | ||
154 | // ============================================================================ | |
155 | // implementation | |
156 | // ============================================================================ | |
157 | ||
158 | // ---------------------------------------------------------------------------- | |
159 | // wxToolBarTool | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
162 | void wxToolBarTool::SetSize(const wxSize& size) | |
163 | { | |
164 | if ( IsControl() ) | |
165 | { | |
166 | GetControl()->SetSize( size ) ; | |
167 | } | |
168 | } | |
169 | ||
170 | void wxToolBarTool::SetPosition(const wxPoint& position) | |
171 | { | |
172 | m_x = position.x; | |
173 | m_y = position.y; | |
174 | ||
175 | if ( IsButton() ) | |
176 | { | |
177 | int x , y ; | |
178 | x = y = 0 ; | |
179 | int mac_x = position.x ; | |
180 | int mac_y = position.y ; | |
181 | #if !TARGET_API_MAC_OSX | |
182 | WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetTopLevelWindowRef() ; | |
183 | GetToolBar()->MacWindowToRootWindow( &x , &y ) ; | |
184 | mac_x += x; | |
185 | mac_y += y; | |
186 | #endif | |
187 | Rect contrlRect ; | |
188 | GetControlBounds( m_controlHandle , &contrlRect ) ; | |
189 | int former_mac_x = contrlRect.left ; | |
190 | int former_mac_y = contrlRect.top ; | |
191 | wxSize sz = GetToolBar()->GetToolSize() ; | |
192 | ||
193 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) | |
194 | { | |
195 | UMAMoveControl( m_controlHandle , mac_x , mac_y ) ; | |
196 | } | |
197 | } | |
198 | else if ( IsControl() ) | |
199 | { | |
200 | GetControl()->Move( position ) ; | |
201 | } | |
202 | } | |
203 | ||
204 | const short kwxMacToolBarToolDefaultWidth = 24 ; | |
205 | const short kwxMacToolBarToolDefaultHeight = 22 ; | |
206 | const short kwxMacToolBarTopMargin = 2 ; | |
207 | const short kwxMacToolBarLeftMargin = 2 ; | |
208 | ||
209 | wxToolBarTool::wxToolBarTool(wxToolBar *tbar, | |
210 | int id, | |
211 | const wxString& label, | |
212 | const wxBitmap& bmpNormal, | |
213 | const wxBitmap& bmpDisabled, | |
214 | wxItemKind kind, | |
215 | wxObject *clientData, | |
216 | const wxString& shortHelp, | |
217 | const wxString& longHelp) | |
218 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind, | |
219 | clientData, shortHelp, longHelp) | |
220 | { | |
221 | Init(); | |
222 | ||
223 | if (id == wxID_SEPARATOR) return; | |
224 | ||
225 | WindowRef window = (WindowRef) tbar->MacGetTopLevelWindowRef() ; | |
226 | wxSize toolSize = tbar->GetToolSize() ; | |
227 | Rect toolrect = { 0, 0 , toolSize.y , toolSize.x } ; | |
228 | ||
229 | ControlButtonContentInfo info ; | |
230 | wxMacCreateBitmapButton( &info , GetNormalBitmap() ) ; | |
231 | ||
232 | SInt16 behaviour = kControlBehaviorOffsetContents ; | |
233 | if ( CanBeToggled() ) | |
234 | behaviour += kControlBehaviorToggles ; | |
235 | ||
236 | CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info , | |
237 | 0 , 0 , 0 , &m_controlHandle ) ; | |
238 | ||
239 | InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(), | |
240 | GetEventTypeCount(eventList), eventList, this,NULL); | |
241 | ||
242 | UMAShowControl( m_controlHandle ) ; | |
243 | if ( !IsEnabled() ) | |
244 | DisableControl( m_controlHandle ) ; | |
245 | ||
246 | if ( CanBeToggled() && IsToggled() ) | |
247 | ::SetControl32BitValue( m_controlHandle , 1 ) ; | |
248 | else | |
249 | ::SetControl32BitValue( m_controlHandle , 0 ) ; | |
250 | ||
251 | ControlRef container = (ControlRef) tbar->GetHandle() ; | |
252 | wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ; | |
253 | ::EmbedControl( m_controlHandle , container ) ; | |
254 | } | |
255 | ||
256 | ||
257 | wxToolBarToolBase *wxToolBar::CreateTool(int id, | |
258 | const wxString& label, | |
259 | const wxBitmap& bmpNormal, | |
260 | const wxBitmap& bmpDisabled, | |
261 | wxItemKind kind, | |
262 | wxObject *clientData, | |
263 | const wxString& shortHelp, | |
264 | const wxString& longHelp) | |
265 | { | |
266 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, | |
267 | clientData, shortHelp, longHelp); | |
268 | } | |
269 | ||
270 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
271 | { | |
272 | return new wxToolBarTool(this, control); | |
273 | } | |
274 | ||
275 | void wxToolBar::Init() | |
276 | { | |
277 | m_maxWidth = -1; | |
278 | m_maxHeight = -1; | |
279 | m_defaultWidth = kwxMacToolBarToolDefaultWidth; | |
280 | m_defaultHeight = kwxMacToolBarToolDefaultHeight; | |
281 | } | |
282 | ||
283 | bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, | |
284 | long style, const wxString& name) | |
285 | { | |
286 | if ( !wxToolBarBase::Create( parent , id , pos , size , style ) ) | |
287 | return FALSE ; | |
288 | ||
289 | return TRUE; | |
290 | } | |
291 | ||
292 | wxToolBar::~wxToolBar() | |
293 | { | |
294 | // we must refresh the frame size when the toolbar is deleted but the frame | |
295 | // is not - otherwise toolbar leaves a hole in the place it used to occupy | |
296 | } | |
297 | ||
298 | bool wxToolBar::Realize() | |
299 | { | |
300 | if (m_tools.GetCount() == 0) | |
301 | return FALSE; | |
302 | ||
303 | int x = m_xMargin + kwxMacToolBarLeftMargin ; | |
304 | int y = m_yMargin + kwxMacToolBarTopMargin ; | |
305 | ||
306 | int tw, th; | |
307 | GetSize(& tw, & th); | |
308 | ||
309 | int maxWidth = 0 ; | |
310 | int maxHeight = 0 ; | |
311 | ||
312 | int maxToolWidth = 0; | |
313 | int maxToolHeight = 0; | |
314 | ||
315 | // Find the maximum tool width and height | |
316 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
317 | while ( node ) | |
318 | { | |
319 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
320 | wxSize sz = tool->GetSize() ; | |
321 | ||
322 | if ( sz.x > maxToolWidth ) | |
323 | maxToolWidth = sz.x ; | |
324 | if (sz.y> maxToolHeight) | |
325 | maxToolHeight = sz.y; | |
326 | ||
327 | node = node->GetNext(); | |
328 | } | |
329 | ||
330 | node = m_tools.GetFirst(); | |
331 | while (node) | |
332 | { | |
333 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
334 | wxSize cursize = tool->GetSize() ; | |
335 | ||
336 | // for the moment we just do a single row/column alignement | |
337 | if ( x + cursize.x > maxWidth ) | |
338 | maxWidth = x + cursize.x ; | |
339 | if ( y + cursize.y > maxHeight ) | |
340 | maxHeight = y + cursize.y ; | |
341 | ||
342 | tool->SetPosition( wxPoint( x , y ) ) ; | |
343 | ||
344 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
345 | { | |
346 | y += cursize.y ; | |
347 | } | |
348 | else | |
349 | { | |
350 | x += cursize.x ; | |
351 | } | |
352 | ||
353 | node = node->GetNext(); | |
354 | } | |
355 | ||
356 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) | |
357 | { | |
358 | if ( m_maxRows == 0 ) | |
359 | { | |
360 | // if not set yet, only one row | |
361 | SetRows(1); | |
362 | } | |
363 | maxWidth = tw ; | |
364 | maxHeight += m_yMargin + kwxMacToolBarTopMargin; | |
365 | m_maxHeight = maxHeight ; | |
366 | } | |
367 | else | |
368 | { | |
369 | if ( GetToolsCount() > 0 && m_maxRows == 0 ) | |
370 | { | |
371 | // if not set yet, have one column | |
372 | SetRows(GetToolsCount()); | |
373 | } | |
374 | maxHeight = th ; | |
375 | maxWidth += m_xMargin + kwxMacToolBarLeftMargin; | |
376 | m_maxWidth = maxWidth ; | |
377 | } | |
378 | ||
379 | SetSize( maxWidth, maxHeight ); | |
380 | ||
381 | return TRUE; | |
382 | } | |
383 | ||
384 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
385 | { | |
386 | m_defaultWidth = size.x+4; m_defaultHeight = size.y+4; | |
387 | } | |
388 | ||
389 | // The button size is bigger than the bitmap size | |
390 | wxSize wxToolBar::GetToolSize() const | |
391 | { | |
392 | return wxSize(m_defaultWidth + 4, m_defaultHeight + 4); | |
393 | } | |
394 | ||
395 | void wxToolBar::SetRows(int nRows) | |
396 | { | |
397 | if ( nRows == m_maxRows ) | |
398 | { | |
399 | // avoid resizing the frame uselessly | |
400 | return; | |
401 | } | |
402 | ||
403 | m_maxRows = nRows; | |
404 | } | |
405 | ||
406 | void wxToolBar::MacSuperChangedPosition() | |
407 | { | |
408 | wxWindow::MacSuperChangedPosition() ; | |
409 | Realize() ; | |
410 | } | |
411 | ||
412 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const | |
413 | { | |
414 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
415 | while (node) | |
416 | { | |
417 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ; | |
418 | wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ; | |
419 | if ( r.Contains( wxPoint( x , y ) ) ) | |
420 | { | |
421 | return tool; | |
422 | } | |
423 | ||
424 | node = node->GetNext(); | |
425 | } | |
426 | ||
427 | return (wxToolBarToolBase *)NULL; | |
428 | } | |
429 | ||
430 | wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) | |
431 | { | |
432 | wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ; | |
433 | if ( tool ) | |
434 | { | |
435 | return tool->GetShortHelp() ; | |
436 | } | |
437 | return wxEmptyString ; | |
438 | } | |
439 | ||
440 | void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) | |
441 | { | |
442 | if (!IsShown()) | |
443 | return ; | |
444 | ||
445 | wxToolBarTool *tool = (wxToolBarTool *)t; | |
446 | if ( tool->IsControl() ) | |
447 | { | |
448 | tool->GetControl()->Enable( enable ) ; | |
449 | } | |
450 | else if ( tool->IsButton() ) | |
451 | { | |
452 | if ( enable ) | |
453 | UMAActivateControl( (ControlRef) tool->GetControlHandle() ) ; | |
454 | else | |
455 | UMADeactivateControl( (ControlRef) tool->GetControlHandle() ) ; | |
456 | } | |
457 | } | |
458 | ||
459 | void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) | |
460 | { | |
461 | if (!IsShown()) | |
462 | return ; | |
463 | ||
464 | wxToolBarTool *tool = (wxToolBarTool *)t; | |
465 | if ( tool->IsButton() ) | |
466 | { | |
467 | ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ; | |
468 | } | |
469 | } | |
470 | ||
471 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), | |
472 | wxToolBarToolBase *tool) | |
473 | { | |
474 | // nothing special to do here - we relayout in Realize() later | |
475 | tool->Attach(this); | |
476 | ||
477 | return TRUE; | |
478 | } | |
479 | ||
480 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) | |
481 | { | |
482 | wxFAIL_MSG( _T("not implemented") ); | |
483 | } | |
484 | ||
485 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) | |
486 | { | |
487 | wxToolBarToolsList::compatibility_iterator node; | |
488 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
489 | { | |
490 | wxToolBarToolBase *tool2 = node->GetData(); | |
491 | if ( tool2 == tool ) | |
492 | { | |
493 | // let node point to the next node in the list | |
494 | node = node->GetNext(); | |
495 | ||
496 | break; | |
497 | } | |
498 | } | |
499 | ||
500 | wxSize sz = ((wxToolBarTool*)tool)->GetSize() ; | |
501 | ||
502 | tool->Detach(); | |
503 | ||
504 | // and finally reposition all the controls after this one | |
505 | ||
506 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) | |
507 | { | |
508 | wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); | |
509 | wxPoint pt = tool2->GetPosition() ; | |
510 | ||
511 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
512 | { | |
513 | pt.y -= sz.y ; | |
514 | } | |
515 | else | |
516 | { | |
517 | pt.x -= sz.x ; | |
518 | } | |
519 | tool2->SetPosition( pt ) ; | |
520 | } | |
521 | ||
522 | return TRUE ; | |
523 | } | |
524 | ||
525 | void wxToolBar::OnPaint(wxPaintEvent& event) | |
526 | { | |
527 | wxPaintDC dc(this) ; | |
528 | wxMacPortSetter helper(&dc) ; | |
529 | int w, h ; | |
530 | GetSize( &w , &h ) ; | |
531 | ||
532 | Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) , | |
533 | dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ; | |
534 | /* | |
535 | if( toolbarrect.left < 0 ) | |
536 | toolbarrect.left = 0 ; | |
537 | if ( toolbarrect.top < 0 ) | |
538 | toolbarrect.top = 0 ; | |
539 | */ | |
540 | UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ; | |
541 | ||
542 | event.Skip() ; | |
543 | } | |
544 | ||
545 | #endif // wxUSE_TOOLBAR | |
546 |