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