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
;
95 wxToolBarTool::wxToolBarTool(wxControl
*control
)
97 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
99 m_index
= control
->GetId();
102 wxToolBarTool::~wxToolBarTool()
105 if (m_deleteSecondBitmap && m_bitmap2)
113 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER
)
115 gs_ToolBars
.Append(this);
122 m_defaultHeight
= 15;
126 m_toolSeparation
= 5;
129 m_xScrollPixelsPerLine
= 0;
130 m_yScrollPixelsPerLine
= 0;
131 m_xScrollingEnabled
= TRUE
;
132 m_yScrollingEnabled
= TRUE
;
133 m_xScrollPosition
= 0;
134 m_yScrollPosition
= 0;
135 m_calcScrolledOffset
= TRUE
;
138 m_xScrollLinesPerPage
= 0;
139 m_yScrollLinesPerPage
= 0;
142 wxToolBarBase::~wxToolBarBase ()
144 gs_ToolBars
.DeleteObject(this);
146 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
148 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
153 // Only allow toggle if returns TRUE
154 bool wxToolBarBase::OnLeftClick(int toolIndex
, bool toggleDown
)
156 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
157 event
.SetEventObject(this);
158 event
.SetExtraLong((long) toggleDown
);
160 // Send events to this toolbar instead (and thence up the window hierarchy)
161 GetEventHandler()->ProcessEvent(event
);
166 // Call when right button down.
167 void wxToolBarBase::OnRightClick(int toolIndex
,
171 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
172 event
.SetEventObject(this);
173 event
.SetInt(toolIndex
);
175 GetEventHandler()->ProcessEvent(event
);
178 // Called when the mouse cursor enters a tool bitmap (no button pressed).
179 // Argument is -1 if mouse is exiting the toolbar.
180 // Note that for this event, the id of the window is used,
181 // and the integer parameter of wxCommandEvent is used to retrieve
183 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
185 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
186 event
.SetEventObject(this);
187 event
.SetInt(toolIndex
);
189 GetEventHandler()->ProcessEvent(event
);
192 // If pushedBitmap is NULL, a reversed version of bitmap is
193 // created and used as the pushed/toggled image.
194 // If toggle is TRUE, the button toggles between the two states.
195 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
196 bool toggle
, wxCoord xPos
, wxCoord yPos
, wxObject
*clientData
,
197 const wxString
& helpString1
, const wxString
& helpString2
)
200 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
201 (wxObject
*) NULL
, helpString1
, helpString2
);
203 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
205 tool
->m_clientData
= clientData
;
210 tool
->m_x
= m_xMargin
;
215 tool
->m_y
= m_yMargin
;
217 // Calculate reasonable max size in case Layout() not called
218 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
219 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
221 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
222 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
224 m_tools
.Append((long)index
, tool
);
228 void wxToolBarBase::AddSeparator ()
230 wxToolBarTool
*tool
= new wxToolBarTool
;
232 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
233 m_tools
.Append(-1, tool
);
236 void wxToolBarBase::ClearTools()
238 m_pressedTool
= m_currentTool
= -1;
239 wxNode
*node
= m_tools
.First();
242 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
243 wxNode
*nextNode
= node
->Next();
250 void wxToolBarBase::EnableTool(int index
, bool enable
)
252 wxNode
*node
= m_tools
.Find((long)index
);
255 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
257 tool
->m_enabled
= enable
;
261 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
262 bool WXUNUSED(toggle
))
266 void wxToolBarBase::SetToggle(int index
, bool value
)
268 wxNode
*node
=m_tools
.Find((long)index
);
270 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
271 tool
->m_isToggle
= value
;
275 bool wxToolBarBase::GetToolState(int index
) const
277 wxNode
*node
= m_tools
.Find((long)index
);
280 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
283 return tool
->m_toggleState
;
290 bool wxToolBarBase::GetToolEnabled(int index
) const
292 wxNode
*node
= m_tools
.Find((long)index
);
295 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
298 return tool
->m_enabled
;
305 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
307 wxNode
*node
= m_tools
.Find((long)index
);
310 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
313 return tool
->m_clientData
;
320 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
322 wxNode
*node
=m_tools
.Find((long)index
);
325 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
326 tool
->m_shortHelpString
= helpString
;
330 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
332 wxNode
*node
=m_tools
.Find((long)index
);
335 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
336 return tool
->m_shortHelpString
;
342 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
344 wxNode
*node
=m_tools
.Find((long)index
);
347 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
348 tool
->m_longHelpString
= helpString
;
352 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
354 wxNode
*node
=m_tools
.Find((long)index
);
357 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
358 return tool
->m_longHelpString
;
364 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
366 wxNode
*node
= m_tools
.First();
369 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
370 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
371 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
372 (y
<= (tool
->m_y
+ tool
->GetHeight())))
380 wxSize
wxToolBarBase::GetMaxSize ( void ) const
382 return wxSize(m_maxWidth
, m_maxHeight
);
385 // Okay, so we've left the tool we're in ... we must check if
386 // the tool we're leaving was a 'sprung push button' and if so,
387 // spring it back to the up state.
389 void wxToolBarBase::SetMargins(int x
, int y
)
395 void wxToolBarBase::SetToolPacking(int packing
)
397 m_toolPacking
= packing
;
400 void wxToolBarBase::SetToolSeparation(int separation
)
402 m_toolSeparation
= separation
;
405 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
409 void wxToolBarBase::LayoutTools()
414 // SCROLLING IMPLEMENTATION
417 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
418 * noUnitsX/noUnitsY: : no. units per scrollbar
420 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
421 int noUnitsX
, int noUnitsY
,
424 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
425 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
426 m_xScrollLines
= noUnitsX
;
427 m_yScrollLines
= noUnitsY
;
432 // Recalculate scroll bar range and position
433 if (m_xScrollLines
> 0)
435 m_xScrollPosition
= xPos
;
436 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
440 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
441 m_xScrollPosition
= 0;
444 if (m_yScrollLines
> 0)
446 m_yScrollPosition
= yPos
;
447 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
451 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
452 m_yScrollPosition
= 0;
457 ::UpdateWindow ((HWND
) GetHWND());
462 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
464 int orient
= event
.GetOrientation();
466 int nScrollInc
= CalcScrollInc(event
);
470 if (orient
== wxHORIZONTAL
)
472 int newPos
= m_xScrollPosition
+ nScrollInc
;
473 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
477 int newPos
= m_yScrollPosition
+ nScrollInc
;
478 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
481 if (orient
== wxHORIZONTAL
)
483 if (m_xScrollingEnabled
)
484 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
490 if (m_yScrollingEnabled
)
491 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
496 if (orient
== wxHORIZONTAL
)
498 m_xScrollPosition
+= nScrollInc
;
502 m_yScrollPosition
+= nScrollInc
;
507 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
509 int pos
= event
.GetPosition();
510 int orient
= event
.GetOrientation();
513 switch (event
.GetEventType())
515 case wxEVT_SCROLL_TOP
:
517 if (orient
== wxHORIZONTAL
)
518 nScrollInc
= - m_xScrollPosition
;
520 nScrollInc
= - m_yScrollPosition
;
523 case wxEVT_SCROLL_BOTTOM
:
525 if (orient
== wxHORIZONTAL
)
526 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
528 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
531 case wxEVT_SCROLL_LINEUP
:
536 case wxEVT_SCROLL_LINEDOWN
:
541 case wxEVT_SCROLL_PAGEUP
:
543 if (orient
== wxHORIZONTAL
)
544 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
546 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
549 case wxEVT_SCROLL_PAGEDOWN
:
551 if (orient
== wxHORIZONTAL
)
552 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
554 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
557 case wxEVT_SCROLL_THUMBTRACK
:
559 if (orient
== wxHORIZONTAL
)
560 nScrollInc
= pos
- m_xScrollPosition
;
562 nScrollInc
= pos
- m_yScrollPosition
;
570 if (orient
== wxHORIZONTAL
)
573 GetClientSize(&w
, &h
);
575 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
576 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
580 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
581 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
582 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
583 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
590 GetClientSize(&w
, &h
);
592 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
593 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
597 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
598 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
599 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
600 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
606 // Adjust the scrollbars - new version.
607 void wxToolBarBase::AdjustScrollbars()
610 GetClientSize(&w
, &h
);
612 // Recalculate scroll bar range and position
613 if (m_xScrollLines
> 0)
615 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
616 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
620 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
622 // Calculate page size i.e. number of scroll units you get on the
623 // current client window
624 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
625 if (noPagePositions
< 1)
628 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
629 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
631 if (m_yScrollLines
> 0)
633 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
634 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
638 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
640 // Calculate page size i.e. number of scroll units you get on the
641 // current client window
642 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
643 if (noPagePositions
< 1)
646 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
647 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
651 // Default OnSize resets scrollbars, if any
652 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
654 #if wxUSE_CONSTRAINTS
662 // Prepare the DC by translating it according to the current scroll position
663 void wxToolBarBase::PrepareDC(wxDC
& dc
)
665 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
668 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
670 *x_unit
= m_xScrollPixelsPerLine
;
671 *y_unit
= m_yScrollPixelsPerLine
;
674 int wxToolBarBase::GetScrollPageSize(int orient
) const
676 if ( orient
== wxHORIZONTAL
)
677 return m_xScrollLinesPerPage
;
679 return m_yScrollLinesPerPage
;
682 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
684 if ( orient
== wxHORIZONTAL
)
685 m_xScrollLinesPerPage
= pageSize
;
687 m_yScrollLinesPerPage
= pageSize
;
691 * Scroll to given position (scroll position, not pixel position)
693 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
696 ViewStart (&old_x
, &old_y
);
697 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
702 m_xScrollPosition
= x_pos
;
703 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
707 m_yScrollPosition
= y_pos
;
708 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
712 UpdateWindow ((HWND
) GetHWND());
716 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
718 m_xScrollingEnabled
= x_scroll
;
719 m_yScrollingEnabled
= y_scroll
;
722 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
724 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
725 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
728 // Where the current view starts from
729 void wxToolBarBase::ViewStart (int *x
, int *y
) const
731 *x
= m_xScrollPosition
;
732 *y
= m_yScrollPosition
;
735 void wxToolBarBase::OnIdle(wxIdleEvent
&
744 wxWindow::OnIdle(event
);
750 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
751 void wxToolBarBase::DoToolbarUpdates()
753 wxEvtHandler
* evtHandler
= GetEventHandler() ;
755 wxNode
* node
= GetTools().First();
758 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
760 wxUpdateUIEvent
event(tool
->m_index
);
761 event
.SetEventObject(this);
763 if (evtHandler
->ProcessEvent(event
))
765 if (event
.GetSetEnabled())
766 EnableTool(tool
->m_index
, event
.GetEnabled());
767 if (event
.GetSetChecked())
768 ToggleTool(tool
->m_index
, event
.GetChecked());
770 if (event.GetSetText())