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