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