1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Toolbar base classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "tbarbase.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
36 #include "wx/tbarbase.h"
38 #if !USE_SHARED_LIBRARY
39 IMPLEMENT_ABSTRACT_CLASS(wxToolBarBase
, wxControl
)
40 IMPLEMENT_DYNAMIC_CLASS(wxToolBarTool
, wxObject
)
42 BEGIN_EVENT_TABLE(wxToolBarBase
, wxControl
)
43 EVT_SCROLL(wxToolBarBase::OnScroll
)
44 EVT_SIZE(wxToolBarBase::OnSize
)
45 EVT_IDLE(wxToolBarBase::OnIdle
)
49 // Keep a list of all toolbars created, so you can tell whether a toolbar
50 // is still valid: a tool may have quit the toolbar.
51 static wxList gs_ToolBars
;
54 wxToolBarTool::wxToolBarTool(wxToolBar
*owner
, int theIndex
,
55 const wxBitmap
& theBitmap1
, const wxBitmap
& theBitmap2
,
56 bool toggle
, wxObject
*clientData
,
57 const wxString
& helpS1
, const wxString
& helpS2
,
60 wxToolBarTool::wxToolBarTool(int theIndex
,
61 const wxBitmap
& theBitmap1
, const wxBitmap
& theBitmap2
, bool toggle
,
62 long xPos
, long yPos
, const wxString
& helpS1
, const wxString
& helpS2
)
65 m_toolStyle
= wxTOOL_STYLE_BUTTON
;
69 m_item
= (GtkWidget
*) NULL
;
70 m_clientData
= clientData
;
80 m_toggleState
= FALSE
;
82 m_bitmap1
= theBitmap1
;
83 m_bitmap2
= theBitmap2
;
84 m_width
= m_height
= 0;
85 m_deleteSecondBitmap
= FALSE
;
88 m_width
= m_bitmap1
.GetWidth()+2;
89 m_height
= m_bitmap1
.GetHeight()+2;
91 m_shortHelpString
= helpS1
;
92 m_longHelpString
= helpS2
;
93 m_control
= (wxControl
*) NULL
;
96 wxToolBarTool::wxToolBarTool(wxControl
*control
)
98 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
100 m_index
= control
->GetId();
103 wxToolBarTool::~wxToolBarTool()
106 if (m_deleteSecondBitmap && m_bitmap2)
114 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER
)
116 gs_ToolBars
.Append(this);
123 m_defaultHeight
= 15;
127 m_toolSeparation
= 5;
130 m_xScrollPixelsPerLine
= 0;
131 m_yScrollPixelsPerLine
= 0;
132 m_xScrollingEnabled
= TRUE
;
133 m_yScrollingEnabled
= TRUE
;
134 m_xScrollPosition
= 0;
135 m_yScrollPosition
= 0;
136 m_calcScrolledOffset
= TRUE
;
139 m_xScrollLinesPerPage
= 0;
140 m_yScrollLinesPerPage
= 0;
143 wxToolBarBase::~wxToolBarBase ()
145 gs_ToolBars
.DeleteObject(this);
147 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
149 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
154 // Only allow toggle if returns TRUE
155 bool wxToolBarBase::OnLeftClick(int toolIndex
, bool toggleDown
)
157 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
158 event
.SetEventObject(this);
159 event
.SetExtraLong((long) toggleDown
);
161 // Send events to this toolbar instead (and thence up the window hierarchy)
162 GetEventHandler()->ProcessEvent(event
);
167 // Call when right button down.
168 void wxToolBarBase::OnRightClick(int toolIndex
,
172 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
173 event
.SetEventObject(this);
174 event
.SetInt(toolIndex
);
176 GetEventHandler()->ProcessEvent(event
);
179 // Called when the mouse cursor enters a tool bitmap (no button pressed).
180 // Argument is -1 if mouse is exiting the toolbar.
181 // Note that for this event, the id of the window is used,
182 // and the integer parameter of wxCommandEvent is used to retrieve
184 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
186 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
187 event
.SetEventObject(this);
188 event
.SetInt(toolIndex
);
190 GetEventHandler()->ProcessEvent(event
);
193 // If pushedBitmap is NULL, a reversed version of bitmap is
194 // created and used as the pushed/toggled image.
195 // If toggle is TRUE, the button toggles between the two states.
196 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
197 bool toggle
, wxCoord xPos
, wxCoord yPos
, wxObject
*clientData
,
198 const wxString
& helpString1
, const wxString
& helpString2
)
201 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
202 (wxObject
*) NULL
, helpString1
, helpString2
);
204 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
206 tool
->m_clientData
= clientData
;
211 tool
->m_x
= m_xMargin
;
216 tool
->m_y
= m_yMargin
;
218 // Calculate reasonable max size in case Layout() not called
219 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
220 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
222 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
223 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
225 m_tools
.Append((long)index
, tool
);
229 void wxToolBarBase::AddSeparator ()
231 wxToolBarTool
*tool
= new wxToolBarTool
;
233 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
234 m_tools
.Append(-1, tool
);
237 void wxToolBarBase::ClearTools()
239 m_pressedTool
= m_currentTool
= -1;
240 wxNode
*node
= m_tools
.First();
243 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
244 wxNode
*nextNode
= node
->Next();
251 void wxToolBarBase::EnableTool(int index
, bool enable
)
253 wxNode
*node
= m_tools
.Find((long)index
);
256 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
258 tool
->m_enabled
= enable
;
262 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
263 bool WXUNUSED(toggle
))
267 void wxToolBarBase::SetToggle(int index
, bool value
)
269 wxNode
*node
=m_tools
.Find((long)index
);
271 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
272 tool
->m_isToggle
= value
;
276 bool wxToolBarBase::GetToolState(int index
) const
278 wxNode
*node
= m_tools
.Find((long)index
);
281 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
284 return tool
->m_toggleState
;
291 bool wxToolBarBase::GetToolEnabled(int index
) const
293 wxNode
*node
= m_tools
.Find((long)index
);
296 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
299 return tool
->m_enabled
;
306 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
308 wxNode
*node
= m_tools
.Find((long)index
);
311 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
314 return tool
->m_clientData
;
321 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
323 wxNode
*node
=m_tools
.Find((long)index
);
326 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
327 tool
->m_shortHelpString
= helpString
;
331 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
333 wxNode
*node
=m_tools
.Find((long)index
);
336 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
337 return tool
->m_shortHelpString
;
343 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
345 wxNode
*node
=m_tools
.Find((long)index
);
348 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
349 tool
->m_longHelpString
= helpString
;
353 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
355 wxNode
*node
=m_tools
.Find((long)index
);
358 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
359 return tool
->m_longHelpString
;
365 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
367 wxNode
*node
= m_tools
.First();
370 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
371 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
372 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
373 (y
<= (tool
->m_y
+ tool
->GetHeight())))
381 wxSize
wxToolBarBase::GetMaxSize ( void ) const
383 return wxSize(m_maxWidth
, m_maxHeight
);
386 // Okay, so we've left the tool we're in ... we must check if
387 // the tool we're leaving was a 'sprung push button' and if so,
388 // spring it back to the up state.
390 void wxToolBarBase::SetMargins(int x
, int y
)
396 void wxToolBarBase::SetToolPacking(int packing
)
398 m_toolPacking
= packing
;
401 void wxToolBarBase::SetToolSeparation(int separation
)
403 m_toolSeparation
= separation
;
406 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
410 void wxToolBarBase::LayoutTools()
415 // SCROLLING IMPLEMENTATION
418 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
419 * noUnitsX/noUnitsY: : no. units per scrollbar
421 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
422 int noUnitsX
, int noUnitsY
,
425 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
426 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
427 m_xScrollLines
= noUnitsX
;
428 m_yScrollLines
= noUnitsY
;
433 // Recalculate scroll bar range and position
434 if (m_xScrollLines
> 0)
436 m_xScrollPosition
= xPos
;
437 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
441 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
442 m_xScrollPosition
= 0;
445 if (m_yScrollLines
> 0)
447 m_yScrollPosition
= yPos
;
448 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
452 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
453 m_yScrollPosition
= 0;
458 ::UpdateWindow ((HWND
) GetHWND());
463 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
465 int orient
= event
.GetOrientation();
467 int nScrollInc
= CalcScrollInc(event
);
471 if (orient
== wxHORIZONTAL
)
473 int newPos
= m_xScrollPosition
+ nScrollInc
;
474 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
478 int newPos
= m_yScrollPosition
+ nScrollInc
;
479 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
482 if (orient
== wxHORIZONTAL
)
484 if (m_xScrollingEnabled
)
485 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
491 if (m_yScrollingEnabled
)
492 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
497 if (orient
== wxHORIZONTAL
)
499 m_xScrollPosition
+= nScrollInc
;
503 m_yScrollPosition
+= nScrollInc
;
508 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
510 int pos
= event
.GetPosition();
511 int orient
= event
.GetOrientation();
514 switch (event
.GetEventType())
516 case wxEVT_SCROLL_TOP
:
518 if (orient
== wxHORIZONTAL
)
519 nScrollInc
= - m_xScrollPosition
;
521 nScrollInc
= - m_yScrollPosition
;
524 case wxEVT_SCROLL_BOTTOM
:
526 if (orient
== wxHORIZONTAL
)
527 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
529 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
532 case wxEVT_SCROLL_LINEUP
:
537 case wxEVT_SCROLL_LINEDOWN
:
542 case wxEVT_SCROLL_PAGEUP
:
544 if (orient
== wxHORIZONTAL
)
545 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
547 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
550 case wxEVT_SCROLL_PAGEDOWN
:
552 if (orient
== wxHORIZONTAL
)
553 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
555 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
558 case wxEVT_SCROLL_THUMBTRACK
:
560 if (orient
== wxHORIZONTAL
)
561 nScrollInc
= pos
- m_xScrollPosition
;
563 nScrollInc
= pos
- m_yScrollPosition
;
571 if (orient
== wxHORIZONTAL
)
574 GetClientSize(&w
, &h
);
576 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
577 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
581 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
582 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
583 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
584 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
591 GetClientSize(&w
, &h
);
593 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
594 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
598 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
599 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
600 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
601 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
607 // Adjust the scrollbars - new version.
608 void wxToolBarBase::AdjustScrollbars()
611 GetClientSize(&w
, &h
);
613 // Recalculate scroll bar range and position
614 if (m_xScrollLines
> 0)
616 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
617 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
621 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
623 // Calculate page size i.e. number of scroll units you get on the
624 // current client window
625 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
626 if (noPagePositions
< 1)
629 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
630 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
632 if (m_yScrollLines
> 0)
634 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
635 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
639 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
641 // Calculate page size i.e. number of scroll units you get on the
642 // current client window
643 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
644 if (noPagePositions
< 1)
647 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
648 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
652 // Default OnSize resets scrollbars, if any
653 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
655 #if wxUSE_CONSTRAINTS
663 // Prepare the DC by translating it according to the current scroll position
664 void wxToolBarBase::PrepareDC(wxDC
& dc
)
666 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
669 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
671 *x_unit
= m_xScrollPixelsPerLine
;
672 *y_unit
= m_yScrollPixelsPerLine
;
675 int wxToolBarBase::GetScrollPageSize(int orient
) const
677 if ( orient
== wxHORIZONTAL
)
678 return m_xScrollLinesPerPage
;
680 return m_yScrollLinesPerPage
;
683 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
685 if ( orient
== wxHORIZONTAL
)
686 m_xScrollLinesPerPage
= pageSize
;
688 m_yScrollLinesPerPage
= pageSize
;
692 * Scroll to given position (scroll position, not pixel position)
694 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
697 ViewStart (&old_x
, &old_y
);
698 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
703 m_xScrollPosition
= x_pos
;
704 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
708 m_yScrollPosition
= y_pos
;
709 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
713 UpdateWindow ((HWND
) GetHWND());
717 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
719 m_xScrollingEnabled
= x_scroll
;
720 m_yScrollingEnabled
= y_scroll
;
723 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
725 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
726 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
729 // Where the current view starts from
730 void wxToolBarBase::ViewStart (int *x
, int *y
) const
732 *x
= m_xScrollPosition
;
733 *y
= m_yScrollPosition
;
736 void wxToolBarBase::OnIdle(wxIdleEvent
&
745 wxWindow::OnIdle(event
);
751 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
752 void wxToolBarBase::DoToolbarUpdates()
754 wxEvtHandler
* evtHandler
= GetEventHandler() ;
756 wxNode
* node
= GetTools().First();
759 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
761 wxUpdateUIEvent
event(tool
->m_index
);
762 event
.SetEventObject(this);
764 if (evtHandler
->ProcessEvent(event
))
766 if (event
.GetSetEnabled())
767 EnableTool(tool
->m_index
, event
.GetEnabled());
768 if (event
.GetSetChecked())
769 ToggleTool(tool
->m_index
, event
.GetChecked());
771 if (event.GetSetText())