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