]> git.saurik.com Git - wxWidgets.git/blob - src/common/tbarbase.cpp
Applied patch [ 619386 ] uxtheme.dll support
[wxWidgets.git] / src / common / tbarbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/tbarbase.cpp
3 // Purpose: wxToolBarBase implementation
4 // Author: Julian Smart
5 // Modified by: VZ at 11.12.99 (wxScrollableToolBar splitted off)
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "tbarbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_TOOLBAR
32
33 #ifndef WX_PRECOMP
34 #include "wx/control.h"
35 #endif
36
37 #include "wx/frame.h"
38 #include "wx/image.h"
39 #include "wx/settings.h"
40
41 #include "wx/tbarbase.h"
42
43 // ----------------------------------------------------------------------------
44 // wxWindows macros
45 // ----------------------------------------------------------------------------
46
47 IMPLEMENT_CLASS(wxToolBarBase, wxControl)
48
49 BEGIN_EVENT_TABLE(wxToolBarBase, wxControl)
50 EVT_IDLE(wxToolBarBase::OnIdle)
51 END_EVENT_TABLE()
52
53 #include "wx/listimpl.cpp"
54
55 WX_DEFINE_LIST(wxToolBarToolsList);
56
57 // ============================================================================
58 // implementation
59 // ============================================================================
60
61 // ----------------------------------------------------------------------------
62 // wxToolBarToolBase
63 // ----------------------------------------------------------------------------
64
65 bool wxToolBarToolBase::Enable(bool enable)
66 {
67 if ( m_enabled == enable )
68 return FALSE;
69
70 m_enabled = enable;
71
72 return TRUE;
73 }
74
75 bool wxToolBarToolBase::Toggle(bool toggle)
76 {
77 wxASSERT_MSG( CanBeToggled(), _T("can't toggle this tool") );
78
79 if ( m_toggled == toggle )
80 return FALSE;
81
82 m_toggled = toggle;
83
84 return TRUE;
85 }
86
87 bool wxToolBarToolBase::SetToggle(bool toggle)
88 {
89 wxItemKind kind = toggle ? wxITEM_CHECK : wxITEM_NORMAL;
90 if ( m_kind == kind )
91 return FALSE;
92
93 m_kind = kind;
94
95 return TRUE;
96 }
97
98 bool wxToolBarToolBase::SetShortHelp(const wxString& help)
99 {
100 if ( m_shortHelpString == help )
101 return FALSE;
102
103 m_shortHelpString = help;
104
105 return TRUE;
106 }
107
108 bool wxToolBarToolBase::SetLongHelp(const wxString& help)
109 {
110 if ( m_longHelpString == help )
111 return FALSE;
112
113 m_longHelpString = help;
114
115 return TRUE;
116 }
117
118 wxToolBarToolBase::~wxToolBarToolBase()
119 {
120 }
121
122 // ----------------------------------------------------------------------------
123 // wxToolBarBase adding/deleting items
124 // ----------------------------------------------------------------------------
125
126 wxToolBarBase::wxToolBarBase()
127 {
128 // the list owns the pointers
129 m_tools.DeleteContents(TRUE);
130
131 m_xMargin = m_yMargin = 0;
132
133 m_maxRows = m_maxCols = 0;
134 }
135
136 wxToolBarToolBase *wxToolBarBase::DoAddTool(int id,
137 const wxString& label,
138 const wxBitmap& bitmap,
139 const wxBitmap& bmpDisabled,
140 wxItemKind kind,
141 const wxString& shortHelp,
142 const wxString& longHelp,
143 wxObject *clientData,
144 wxCoord WXUNUSED(xPos),
145 wxCoord WXUNUSED(yPos))
146 {
147 return InsertTool(GetToolsCount(), id, label, bitmap, bmpDisabled,
148 kind, shortHelp, longHelp, clientData);
149 }
150
151 wxToolBarToolBase *wxToolBarBase::InsertTool(size_t pos,
152 int id,
153 const wxString& label,
154 const wxBitmap& bitmap,
155 const wxBitmap& bmpDisabled,
156 wxItemKind kind,
157 const wxString& shortHelp,
158 const wxString& longHelp,
159 wxObject *clientData)
160 {
161 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
162 _T("invalid position in wxToolBar::InsertTool()") );
163
164 wxToolBarToolBase *tool = CreateTool(id, label, bitmap, bmpDisabled, kind,
165 clientData, shortHelp, longHelp);
166
167 if ( !InsertTool(pos, tool) )
168 {
169 delete tool;
170
171 return NULL;
172 }
173
174 return tool;
175 }
176
177 wxToolBarToolBase *wxToolBarBase::AddTool(wxToolBarToolBase *tool)
178 {
179 return InsertTool(GetToolsCount(), tool);
180 }
181
182 wxToolBarToolBase *
183 wxToolBarBase::InsertTool(size_t pos, wxToolBarToolBase *tool)
184 {
185 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
186 _T("invalid position in wxToolBar::InsertTool()") );
187
188 if ( !tool || !DoInsertTool(pos, tool) )
189 {
190 return NULL;
191 }
192
193 m_tools.Insert(pos, tool);
194
195 return tool;
196 }
197
198 wxToolBarToolBase *wxToolBarBase::AddControl(wxControl *control)
199 {
200 return InsertControl(GetToolsCount(), control);
201 }
202
203 wxToolBarToolBase *wxToolBarBase::InsertControl(size_t pos, wxControl *control)
204 {
205 wxCHECK_MSG( control, (wxToolBarToolBase *)NULL,
206 _T("toolbar: can't insert NULL control") );
207
208 wxCHECK_MSG( control->GetParent() == this, (wxToolBarToolBase *)NULL,
209 _T("control must have toolbar as parent") );
210
211 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
212 _T("invalid position in wxToolBar::InsertControl()") );
213
214 wxToolBarToolBase *tool = CreateTool(control);
215
216 if ( !InsertTool(pos, tool) )
217 {
218 delete tool;
219
220 return NULL;
221 }
222
223 return tool;
224 }
225
226 wxControl *wxToolBarBase::FindControl( int id )
227 {
228 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
229 node;
230 node = node->GetNext() )
231 {
232 const wxToolBarToolBase * const tool = node->GetData();
233 if ( tool->IsControl() )
234 {
235 wxControl * const control = tool->GetControl();
236
237 if ( !control )
238 {
239 wxFAIL_MSG( _T("NULL control in toolbar?") );
240 }
241 else if ( control->GetId() == id )
242 {
243 // found
244 return control;
245 }
246 }
247 }
248
249 return NULL;
250 }
251
252 wxToolBarToolBase *wxToolBarBase::AddSeparator()
253 {
254 return InsertSeparator(GetToolsCount());
255 }
256
257 wxToolBarToolBase *wxToolBarBase::InsertSeparator(size_t pos)
258 {
259 wxCHECK_MSG( pos <= GetToolsCount(), (wxToolBarToolBase *)NULL,
260 _T("invalid position in wxToolBar::InsertSeparator()") );
261
262 wxToolBarToolBase *tool = CreateTool(wxID_SEPARATOR,
263 wxEmptyString,
264 wxNullBitmap, wxNullBitmap,
265 wxITEM_SEPARATOR, (wxObject *)NULL,
266 wxEmptyString, wxEmptyString);
267
268 if ( !tool || !DoInsertTool(pos, tool) )
269 {
270 delete tool;
271
272 return NULL;
273 }
274
275 m_tools.Insert(pos, tool);
276
277 return tool;
278 }
279
280 wxToolBarToolBase *wxToolBarBase::RemoveTool(int id)
281 {
282 size_t pos = 0;
283 wxToolBarToolsList::Node *node;
284 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
285 {
286 if ( node->GetData()->GetId() == id )
287 break;
288
289 pos++;
290 }
291
292 if ( !node )
293 {
294 // don't give any error messages - sometimes we might call RemoveTool()
295 // without knowing whether the tool is or not in the toolbar
296 return (wxToolBarToolBase *)NULL;
297 }
298
299 wxToolBarToolBase *tool = node->GetData();
300 if ( !DoDeleteTool(pos, tool) )
301 {
302 return (wxToolBarToolBase *)NULL;
303 }
304
305 // the node would delete the data, so set it to NULL to avoid this
306 node->SetData(NULL);
307
308 m_tools.DeleteNode(node);
309
310 return tool;
311 }
312
313 bool wxToolBarBase::DeleteToolByPos(size_t pos)
314 {
315 wxCHECK_MSG( pos < GetToolsCount(), FALSE,
316 _T("invalid position in wxToolBar::DeleteToolByPos()") );
317
318 wxToolBarToolsList::Node *node = m_tools.Item(pos);
319
320 if ( !DoDeleteTool(pos, node->GetData()) )
321 {
322 return FALSE;
323 }
324
325 m_tools.DeleteNode(node);
326
327 return TRUE;
328 }
329
330 bool wxToolBarBase::DeleteTool(int id)
331 {
332 size_t pos = 0;
333 wxToolBarToolsList::Node *node;
334 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
335 {
336 if ( node->GetData()->GetId() == id )
337 break;
338
339 pos++;
340 }
341
342 if ( !node || !DoDeleteTool(pos, node->GetData()) )
343 {
344 return FALSE;
345 }
346
347 m_tools.DeleteNode(node);
348
349 return TRUE;
350 }
351
352 wxToolBarToolBase *wxToolBarBase::FindById(int id) const
353 {
354 wxToolBarToolBase *tool = (wxToolBarToolBase *)NULL;
355
356 for ( wxToolBarToolsList::Node *node = m_tools.GetFirst();
357 node;
358 node = node->GetNext() )
359 {
360 tool = node->GetData();
361 if ( tool->GetId() == id )
362 {
363 // found
364 break;
365 }
366
367 tool = NULL;
368 }
369
370 return tool;
371 }
372
373 void wxToolBarBase::ClearTools()
374 {
375 m_tools.Clear();
376 }
377
378 bool wxToolBarBase::Realize()
379 {
380 return TRUE;
381 }
382
383 wxToolBarBase::~wxToolBarBase()
384 {
385 }
386
387 // ----------------------------------------------------------------------------
388 // wxToolBarBase tools state
389 // ----------------------------------------------------------------------------
390
391 void wxToolBarBase::EnableTool(int id, bool enable)
392 {
393 wxToolBarToolBase *tool = FindById(id);
394 if ( tool )
395 {
396 if ( tool->Enable(enable) )
397 {
398 DoEnableTool(tool, enable);
399 }
400 }
401 }
402
403 void wxToolBarBase::ToggleTool(int id, bool toggle)
404 {
405 wxToolBarToolBase *tool = FindById(id);
406 if ( tool && tool->CanBeToggled() )
407 {
408 if ( tool->Toggle(toggle) )
409 {
410 DoToggleTool(tool, toggle);
411 }
412 }
413 }
414
415 void wxToolBarBase::SetToggle(int id, bool toggle)
416 {
417 wxToolBarToolBase *tool = FindById(id);
418 if ( tool )
419 {
420 if ( tool->SetToggle(toggle) )
421 {
422 DoSetToggle(tool, toggle);
423 }
424 }
425 }
426
427 void wxToolBarBase::SetToolShortHelp(int id, const wxString& help)
428 {
429 wxToolBarToolBase *tool = FindById(id);
430 if ( tool )
431 {
432 (void)tool->SetShortHelp(help);
433 }
434 }
435
436 void wxToolBarBase::SetToolLongHelp(int id, const wxString& help)
437 {
438 wxToolBarToolBase *tool = FindById(id);
439 if ( tool )
440 {
441 (void)tool->SetLongHelp(help);
442 }
443 }
444
445 wxObject *wxToolBarBase::GetToolClientData(int id) const
446 {
447 wxToolBarToolBase *tool = FindById(id);
448
449 return tool ? tool->GetClientData() : (wxObject *)NULL;
450 }
451
452 void wxToolBarBase::SetToolClientData(int id, wxObject *clientData)
453 {
454 wxToolBarToolBase *tool = FindById(id);
455
456 wxCHECK_RET( tool, _T("no such tool in wxToolBar::SetToolClientData") );
457
458 tool->SetClientData(clientData);
459 }
460
461 int wxToolBarBase::GetToolPos(int id) const
462 {
463 size_t pos = 0;
464 wxToolBarToolsList::Node *node;
465
466 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
467 {
468 if ( node->GetData()->GetId() == id )
469 return pos;
470
471 pos++;
472 }
473
474 return wxNOT_FOUND;
475 }
476
477 bool wxToolBarBase::GetToolState(int id) const
478 {
479 wxToolBarToolBase *tool = FindById(id);
480 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
481
482 return tool->IsToggled();
483 }
484
485 bool wxToolBarBase::GetToolEnabled(int id) const
486 {
487 wxToolBarToolBase *tool = FindById(id);
488 wxCHECK_MSG( tool, FALSE, _T("no such tool") );
489
490 return tool->IsEnabled();
491 }
492
493 wxString wxToolBarBase::GetToolShortHelp(int id) const
494 {
495 wxToolBarToolBase *tool = FindById(id);
496 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
497
498 return tool->GetShortHelp();
499 }
500
501 wxString wxToolBarBase::GetToolLongHelp(int id) const
502 {
503 wxToolBarToolBase *tool = FindById(id);
504 wxCHECK_MSG( tool, _T(""), _T("no such tool") );
505
506 return tool->GetLongHelp();
507 }
508
509 // ----------------------------------------------------------------------------
510 // wxToolBarBase geometry
511 // ----------------------------------------------------------------------------
512
513 void wxToolBarBase::SetMargins(int x, int y)
514 {
515 m_xMargin = x;
516 m_yMargin = y;
517 }
518
519 void wxToolBarBase::SetRows(int WXUNUSED(nRows))
520 {
521 // nothing
522 }
523
524 // ----------------------------------------------------------------------------
525 // event processing
526 // ----------------------------------------------------------------------------
527
528 // Only allow toggle if returns TRUE
529 bool wxToolBarBase::OnLeftClick(int id, bool toggleDown)
530 {
531 wxCommandEvent event(wxEVT_COMMAND_TOOL_CLICKED, id);
532 event.SetEventObject(this);
533
534 // we use SetInt() to make wxCommandEvent::IsChecked() return toggleDown
535 event.SetInt((int)toggleDown);
536
537 // and SetExtraLong() for backwards compatibility
538 event.SetExtraLong((long)toggleDown);
539
540 // Send events to this toolbar instead (and thence up the window hierarchy)
541 GetEventHandler()->ProcessEvent(event);
542
543 return TRUE;
544 }
545
546 // Call when right button down.
547 void wxToolBarBase::OnRightClick(int id,
548 long WXUNUSED(x),
549 long WXUNUSED(y))
550 {
551 wxCommandEvent event(wxEVT_COMMAND_TOOL_RCLICKED, id);
552 event.SetEventObject(this);
553 event.SetInt(id);
554
555 GetEventHandler()->ProcessEvent(event);
556 }
557
558 // Called when the mouse cursor enters a tool bitmap (no button pressed).
559 // Argument is -1 if mouse is exiting the toolbar.
560 // Note that for this event, the id of the window is used,
561 // and the integer parameter of wxCommandEvent is used to retrieve
562 // the tool id.
563 void wxToolBarBase::OnMouseEnter(int id)
564 {
565 wxCommandEvent event(wxEVT_COMMAND_TOOL_ENTER, GetId());
566 event.SetEventObject(this);
567 event.SetInt(id);
568
569 wxFrame *frame = wxDynamicCast(GetParent(), wxFrame);
570 if( frame )
571 {
572 wxToolBarToolBase* tool = id == -1 ? (wxToolBarToolBase*)0 : FindById(id);
573 wxString help = tool ? tool->GetLongHelp() : wxString();
574 frame->DoGiveHelp( help, id != -1 );
575 }
576
577 (void)GetEventHandler()->ProcessEvent(event);
578 }
579
580 // ----------------------------------------------------------------------------
581 // UI updates
582 // ----------------------------------------------------------------------------
583
584 void wxToolBarBase::OnIdle(wxIdleEvent& event)
585 {
586 DoToolbarUpdates();
587
588 event.Skip();
589 }
590
591 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
592 void wxToolBarBase::DoToolbarUpdates()
593 {
594 wxWindow* parent = this;
595 while (parent->GetParent())
596 parent = parent->GetParent();
597
598 // This kind of #ifdef is a good way to annoy people. It breaks
599 // apps, but only on one platform and due to a hack in officially
600 // platform independent code. It took me hours to fix this. RR.
601 //
602 // #ifdef __WXMSW__
603 // wxWindow* focusWin = wxFindFocusDescendant(parent);
604 // #else
605 wxWindow* focusWin = (wxWindow*) NULL;
606 // #endif
607
608 wxEvtHandler* evtHandler = focusWin ? focusWin->GetEventHandler() : GetEventHandler() ;
609
610 for ( wxToolBarToolsList::Node* node = m_tools.GetFirst();
611 node;
612 node = node->GetNext() )
613 {
614 int id = node->GetData()->GetId();
615
616 wxUpdateUIEvent event(id);
617 event.SetEventObject(this);
618
619 if ( evtHandler->ProcessEvent(event) )
620 {
621 if ( event.GetSetEnabled() )
622 EnableTool(id, event.GetEnabled());
623 if ( event.GetSetChecked() )
624 ToggleTool(id, event.GetChecked());
625 #if 0
626 if ( event.GetSetText() )
627 // Set tooltip?
628 #endif // 0
629 }
630 }
631 }
632
633 // Helper function, used by wxCreateGreyedImage
634
635 static void wxGreyOutImage( const wxImage& src,
636 wxImage& dest,
637 const wxColour& darkCol,
638 const wxColour& lightCol,
639 const wxColour& bgCol )
640 {
641 // Second attempt, just making things monochrome
642 int width = src.GetWidth();
643 int height = src.GetHeight();
644
645 int redCur, greenCur, blueCur;
646 for ( int x = 0; x < width; x++ )
647 {
648 for ( int y = 1; y < height; y++ )
649 {
650 redCur = src.GetRed(x, y);
651 greenCur = src.GetGreen(x, y);
652 blueCur = src.GetBlue(x, y);
653
654 // Change light things to the background colour
655 if ( redCur >= (lightCol.Red() - 50) && greenCur >= (lightCol.Green() - 50) && blueCur >= (lightCol.Blue() - 50) )
656 {
657 dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
658 }
659 else if ( redCur == bgCol.Red() && greenCur == bgCol.Green() && blueCur == bgCol.Blue() )
660 {
661 // Leave the background colour as-is
662 // dest.SetRGB(x,y, bgCol.Red(), bgCol.Green(), bgCol.Blue());
663 }
664 else // if ( redCur <= darkCol.Red() && greenCur <= darkCol.Green() && blueCur <= darkCol.Blue() )
665 {
666 // Change dark things to really dark
667 dest.SetRGB(x,y, darkCol.Red(), darkCol.Green(), darkCol.Blue());
668 }
669 }
670 }
671 }
672
673 /*
674 * Make a greyed-out image suitable for disabled buttons.
675 * This code is adapted from wxNewBitmapButton in FL.
676 */
677
678 bool wxCreateGreyedImage(const wxImage& in, wxImage& out)
679 {
680 out = in.Copy();
681
682 // assuming the pixels along the edges are of the background color
683 wxColour bgCol(in.GetRed(0, 0), in.GetGreen(0, 0), in.GetBlue(0, 0));
684
685 wxColour darkCol = wxSystemSettings::GetColour(wxSYS_COLOUR_3DSHADOW) ;
686 wxColour lightCol = wxSystemSettings::GetColour(wxSYS_COLOUR_3DHIGHLIGHT) ;
687
688 wxGreyOutImage(in, out, darkCol, lightCol, bgCol);
689
690 return TRUE;
691 }
692
693 #endif // wxUSE_TOOLBAR