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