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