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