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