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