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