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