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