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_clientData
= clientData
;
79 m_toggleState
= FALSE
;
81 m_bitmap1
= theBitmap1
;
82 m_bitmap2
= theBitmap2
;
83 m_width
= m_height
= 0;
84 m_deleteSecondBitmap
= FALSE
;
87 m_width
= m_bitmap1
.GetWidth()+2;
88 m_height
= m_bitmap1
.GetHeight()+2;
90 m_shortHelpString
= helpS1
;
91 m_longHelpString
= helpS2
;
94 wxToolBarTool::~wxToolBarTool()
97 if (m_deleteSecondBitmap && m_bitmap2)
105 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER
)
107 gs_ToolBars
.Append(this);
114 m_defaultHeight
= 15;
118 m_toolSeparation
= 5;
121 m_xScrollPixelsPerLine
= 0;
122 m_yScrollPixelsPerLine
= 0;
123 m_xScrollingEnabled
= TRUE
;
124 m_yScrollingEnabled
= TRUE
;
125 m_xScrollPosition
= 0;
126 m_yScrollPosition
= 0;
127 m_calcScrolledOffset
= TRUE
;
130 m_xScrollLinesPerPage
= 0;
131 m_yScrollLinesPerPage
= 0;
134 wxToolBarBase::~wxToolBarBase ()
136 gs_ToolBars
.DeleteObject(this);
138 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
140 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
145 // Only allow toggle if returns TRUE
146 bool wxToolBarBase::OnLeftClick(int toolIndex
, bool toggleDown
)
148 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
149 event
.SetEventObject(this);
150 event
.SetExtraLong((long) toggleDown
);
152 // Send events to this toolbar instead (and thence up the window hierarchy)
153 GetEventHandler()->ProcessEvent(event
);
158 // Call when right button down.
159 void wxToolBarBase::OnRightClick(int toolIndex
,
163 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
164 event
.SetEventObject(this);
165 event
.SetInt(toolIndex
);
167 GetEventHandler()->ProcessEvent(event
);
170 // Called when the mouse cursor enters a tool bitmap (no button pressed).
171 // Argument is -1 if mouse is exiting the toolbar.
172 // Note that for this event, the id of the window is used,
173 // and the integer parameter of wxCommandEvent is used to retrieve
175 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
177 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
178 event
.SetEventObject(this);
179 event
.SetInt(toolIndex
);
181 GetEventHandler()->ProcessEvent(event
);
184 // If pushedBitmap is NULL, a reversed version of bitmap is
185 // created and used as the pushed/toggled image.
186 // If toggle is TRUE, the button toggles between the two states.
187 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
188 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
,
189 const wxString
& helpString1
, const wxString
& helpString2
)
192 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
193 (wxObject
*) NULL
, helpString1
, helpString2
);
195 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
197 tool
->m_clientData
= clientData
;
202 tool
->m_x
= m_xMargin
;
207 tool
->m_y
= m_yMargin
;
209 // Calculate reasonable max size in case Layout() not called
210 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
211 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
213 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
214 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
216 m_tools
.Append((long)index
, tool
);
220 void wxToolBarBase::AddSeparator ()
222 wxToolBarTool
*tool
= new wxToolBarTool
;
224 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
225 m_tools
.Append(-1, tool
);
228 void wxToolBarBase::ClearTools()
230 m_pressedTool
= m_currentTool
= -1;
231 wxNode
*node
= m_tools
.First();
234 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
235 wxNode
*nextNode
= node
->Next();
242 void wxToolBarBase::EnableTool(int index
, bool enable
)
244 wxNode
*node
= m_tools
.Find((long)index
);
247 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
249 tool
->m_enabled
= enable
;
253 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
254 bool WXUNUSED(toggle
))
258 void wxToolBarBase::SetToggle(int index
, bool value
)
260 wxNode
*node
=m_tools
.Find((long)index
);
262 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
263 tool
->m_isToggle
= value
;
267 bool wxToolBarBase::GetToolState(int index
) const
269 wxNode
*node
= m_tools
.Find((long)index
);
272 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
275 return tool
->m_toggleState
;
282 bool wxToolBarBase::GetToolEnabled(int index
) const
284 wxNode
*node
= m_tools
.Find((long)index
);
287 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
290 return tool
->m_enabled
;
297 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
299 wxNode
*node
= m_tools
.Find((long)index
);
302 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
305 return tool
->m_clientData
;
312 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
314 wxNode
*node
=m_tools
.Find((long)index
);
317 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
318 tool
->m_shortHelpString
= helpString
;
322 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
324 wxNode
*node
=m_tools
.Find((long)index
);
327 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
328 return tool
->m_shortHelpString
;
334 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
336 wxNode
*node
=m_tools
.Find((long)index
);
339 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
340 tool
->m_longHelpString
= helpString
;
344 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
346 wxNode
*node
=m_tools
.Find((long)index
);
349 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
350 return tool
->m_longHelpString
;
356 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
358 wxNode
*node
= m_tools
.First();
361 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
362 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
363 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
364 (y
<= (tool
->m_y
+ tool
->GetHeight())))
372 wxSize
wxToolBarBase::GetMaxSize ( void ) const
374 return wxSize(m_maxWidth
, m_maxHeight
);
377 // Okay, so we've left the tool we're in ... we must check if
378 // the tool we're leaving was a 'sprung push button' and if so,
379 // spring it back to the up state.
381 void wxToolBarBase::SetMargins(int x
, int y
)
387 void wxToolBarBase::SetToolPacking(int packing
)
389 m_toolPacking
= packing
;
392 void wxToolBarBase::SetToolSeparation(int separation
)
394 m_toolSeparation
= separation
;
397 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
401 void wxToolBarBase::LayoutTools()
406 // SCROLLING IMPLEMENTATION
409 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
410 * noUnitsX/noUnitsY: : no. units per scrollbar
412 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
413 int noUnitsX
, int noUnitsY
,
416 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
417 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
418 m_xScrollLines
= noUnitsX
;
419 m_yScrollLines
= noUnitsY
;
424 // Recalculate scroll bar range and position
425 if (m_xScrollLines
> 0)
427 m_xScrollPosition
= xPos
;
428 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
432 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
433 m_xScrollPosition
= 0;
436 if (m_yScrollLines
> 0)
438 m_yScrollPosition
= yPos
;
439 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
443 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
444 m_yScrollPosition
= 0;
449 ::UpdateWindow ((HWND
) GetHWND());
454 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
456 int orient
= event
.GetOrientation();
458 int nScrollInc
= CalcScrollInc(event
);
462 if (orient
== wxHORIZONTAL
)
464 int newPos
= m_xScrollPosition
+ nScrollInc
;
465 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
469 int newPos
= m_yScrollPosition
+ nScrollInc
;
470 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
473 if (orient
== wxHORIZONTAL
)
475 if (m_xScrollingEnabled
)
476 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
482 if (m_yScrollingEnabled
)
483 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
488 if (orient
== wxHORIZONTAL
)
490 m_xScrollPosition
+= nScrollInc
;
494 m_yScrollPosition
+= nScrollInc
;
499 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
501 int pos
= event
.GetPosition();
502 int orient
= event
.GetOrientation();
505 switch (event
.GetEventType())
507 case wxEVT_SCROLL_TOP
:
509 if (orient
== wxHORIZONTAL
)
510 nScrollInc
= - m_xScrollPosition
;
512 nScrollInc
= - m_yScrollPosition
;
515 case wxEVT_SCROLL_BOTTOM
:
517 if (orient
== wxHORIZONTAL
)
518 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
520 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
523 case wxEVT_SCROLL_LINEUP
:
528 case wxEVT_SCROLL_LINEDOWN
:
533 case wxEVT_SCROLL_PAGEUP
:
535 if (orient
== wxHORIZONTAL
)
536 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
538 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
541 case wxEVT_SCROLL_PAGEDOWN
:
543 if (orient
== wxHORIZONTAL
)
544 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
546 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
549 case wxEVT_SCROLL_THUMBTRACK
:
551 if (orient
== wxHORIZONTAL
)
552 nScrollInc
= pos
- m_xScrollPosition
;
554 nScrollInc
= pos
- m_yScrollPosition
;
562 if (orient
== wxHORIZONTAL
)
565 GetClientSize(&w
, &h
);
567 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
568 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
572 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
573 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
574 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
575 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
582 GetClientSize(&w
, &h
);
584 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
585 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
589 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
590 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
591 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
592 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
598 // Adjust the scrollbars - new version.
599 void wxToolBarBase::AdjustScrollbars()
602 GetClientSize(&w
, &h
);
604 // Recalculate scroll bar range and position
605 if (m_xScrollLines
> 0)
607 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
608 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
612 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
614 // Calculate page size i.e. number of scroll units you get on the
615 // current client window
616 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
617 if (noPagePositions
< 1)
620 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
621 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
623 if (m_yScrollLines
> 0)
625 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
626 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
630 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
632 // Calculate page size i.e. number of scroll units you get on the
633 // current client window
634 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
635 if (noPagePositions
< 1)
638 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
639 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
643 // Default OnSize resets scrollbars, if any
644 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
646 #if wxUSE_CONSTRAINTS
654 // Prepare the DC by translating it according to the current scroll position
655 void wxToolBarBase::PrepareDC(wxDC
& dc
)
657 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
660 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
662 *x_unit
= m_xScrollPixelsPerLine
;
663 *y_unit
= m_yScrollPixelsPerLine
;
666 int wxToolBarBase::GetScrollPageSize(int orient
) const
668 if ( orient
== wxHORIZONTAL
)
669 return m_xScrollLinesPerPage
;
671 return m_yScrollLinesPerPage
;
674 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
676 if ( orient
== wxHORIZONTAL
)
677 m_xScrollLinesPerPage
= pageSize
;
679 m_yScrollLinesPerPage
= pageSize
;
683 * Scroll to given position (scroll position, not pixel position)
685 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
688 ViewStart (&old_x
, &old_y
);
689 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
694 m_xScrollPosition
= x_pos
;
695 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
699 m_yScrollPosition
= y_pos
;
700 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
704 UpdateWindow ((HWND
) GetHWND());
708 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
710 m_xScrollingEnabled
= x_scroll
;
711 m_yScrollingEnabled
= y_scroll
;
714 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
716 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
717 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
720 // Where the current view starts from
721 void wxToolBarBase::ViewStart (int *x
, int *y
) const
723 *x
= m_xScrollPosition
;
724 *y
= m_yScrollPosition
;
727 void wxToolBarBase::OnIdle(wxIdleEvent
& event
)
730 wxWindow::OnIdle(event
);
736 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
737 void wxToolBarBase::DoToolbarUpdates()
739 wxEvtHandler
* evtHandler
= GetEventHandler() ;
741 wxNode
* node
= GetTools().First();
744 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
746 wxUpdateUIEvent
event(tool
->m_index
);
747 event
.SetEventObject(this);
749 if (evtHandler
->ProcessEvent(event
))
751 if (event
.GetSetEnabled())
752 EnableTool(tool
->m_index
, event
.GetEnabled());
753 if (event
.GetSetChecked())
754 ToggleTool(tool
->m_index
, event
.GetChecked());
756 if (event.GetSetText())
766 // Circumvent wxControl::MSWOnMouseMove which doesn't set the cursor.
767 void wxToolBarBase::MSWOnMouseMove(int x
, int y
, WXUINT flags
)
769 wxWindow::MSWOnMouseMove(x
, y
, flags
);