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