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()
98 if (m_deleteSecondBitmap && m_bitmap2)
106 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER
)
108 gs_ToolBars
.Append(this);
115 m_defaultHeight
= 15;
119 m_toolSeparation
= 5;
122 m_xScrollPixelsPerLine
= 0;
123 m_yScrollPixelsPerLine
= 0;
124 m_xScrollingEnabled
= TRUE
;
125 m_yScrollingEnabled
= TRUE
;
126 m_xScrollPosition
= 0;
127 m_yScrollPosition
= 0;
128 m_calcScrolledOffset
= TRUE
;
131 m_xScrollLinesPerPage
= 0;
132 m_yScrollLinesPerPage
= 0;
135 wxToolBarBase::~wxToolBarBase ()
137 gs_ToolBars
.DeleteObject(this);
139 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
141 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
146 // Only allow toggle if returns TRUE
147 bool wxToolBarBase::OnLeftClick(int toolIndex
, bool toggleDown
)
149 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
150 event
.SetEventObject(this);
151 event
.SetExtraLong((long) toggleDown
);
153 // Send events to this toolbar instead (and thence up the window hierarchy)
154 GetEventHandler()->ProcessEvent(event
);
159 // Call when right button down.
160 void wxToolBarBase::OnRightClick(int toolIndex
,
164 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
165 event
.SetEventObject(this);
166 event
.SetInt(toolIndex
);
168 GetEventHandler()->ProcessEvent(event
);
171 // Called when the mouse cursor enters a tool bitmap (no button pressed).
172 // Argument is -1 if mouse is exiting the toolbar.
173 // Note that for this event, the id of the window is used,
174 // and the integer parameter of wxCommandEvent is used to retrieve
176 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
178 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
179 event
.SetEventObject(this);
180 event
.SetInt(toolIndex
);
182 GetEventHandler()->ProcessEvent(event
);
185 // If pushedBitmap is NULL, a reversed version of bitmap is
186 // created and used as the pushed/toggled image.
187 // If toggle is TRUE, the button toggles between the two states.
188 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
189 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
,
190 const wxString
& helpString1
, const wxString
& helpString2
)
193 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
194 (wxObject
*) NULL
, helpString1
, helpString2
);
196 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
198 tool
->m_clientData
= clientData
;
203 tool
->m_x
= m_xMargin
;
208 tool
->m_y
= m_yMargin
;
210 // Calculate reasonable max size in case Layout() not called
211 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
212 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
214 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
215 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
217 m_tools
.Append((long)index
, tool
);
221 void wxToolBarBase::AddSeparator ()
223 wxToolBarTool
*tool
= new wxToolBarTool
;
225 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
226 m_tools
.Append(-1, tool
);
229 void wxToolBarBase::ClearTools()
231 m_pressedTool
= m_currentTool
= -1;
232 wxNode
*node
= m_tools
.First();
235 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
236 wxNode
*nextNode
= node
->Next();
243 void wxToolBarBase::EnableTool(int index
, bool enable
)
245 wxNode
*node
= m_tools
.Find((long)index
);
248 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
250 tool
->m_enabled
= enable
;
254 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
255 bool WXUNUSED(toggle
))
259 void wxToolBarBase::SetToggle(int index
, bool value
)
261 wxNode
*node
=m_tools
.Find((long)index
);
263 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
264 tool
->m_isToggle
= value
;
268 bool wxToolBarBase::GetToolState(int index
) const
270 wxNode
*node
= m_tools
.Find((long)index
);
273 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
276 return tool
->m_toggleState
;
283 bool wxToolBarBase::GetToolEnabled(int index
) const
285 wxNode
*node
= m_tools
.Find((long)index
);
288 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
291 return tool
->m_enabled
;
298 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
300 wxNode
*node
= m_tools
.Find((long)index
);
303 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
306 return tool
->m_clientData
;
313 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
315 wxNode
*node
=m_tools
.Find((long)index
);
318 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
319 tool
->m_shortHelpString
= helpString
;
323 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
325 wxNode
*node
=m_tools
.Find((long)index
);
328 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
329 return tool
->m_shortHelpString
;
335 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
337 wxNode
*node
=m_tools
.Find((long)index
);
340 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
341 tool
->m_longHelpString
= helpString
;
345 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
347 wxNode
*node
=m_tools
.Find((long)index
);
350 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
351 return tool
->m_longHelpString
;
357 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
359 wxNode
*node
= m_tools
.First();
362 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
363 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
364 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
365 (y
<= (tool
->m_y
+ tool
->GetHeight())))
373 wxSize
wxToolBarBase::GetMaxSize ( void ) const
375 return wxSize(m_maxWidth
, m_maxHeight
);
378 // Okay, so we've left the tool we're in ... we must check if
379 // the tool we're leaving was a 'sprung push button' and if so,
380 // spring it back to the up state.
382 void wxToolBarBase::SetMargins(int x
, int y
)
388 void wxToolBarBase::SetToolPacking(int packing
)
390 m_toolPacking
= packing
;
393 void wxToolBarBase::SetToolSeparation(int separation
)
395 m_toolSeparation
= separation
;
398 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
402 void wxToolBarBase::LayoutTools()
407 // SCROLLING IMPLEMENTATION
410 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
411 * noUnitsX/noUnitsY: : no. units per scrollbar
413 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
414 int noUnitsX
, int noUnitsY
,
417 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
418 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
419 m_xScrollLines
= noUnitsX
;
420 m_yScrollLines
= noUnitsY
;
425 // Recalculate scroll bar range and position
426 if (m_xScrollLines
> 0)
428 m_xScrollPosition
= xPos
;
429 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
433 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
434 m_xScrollPosition
= 0;
437 if (m_yScrollLines
> 0)
439 m_yScrollPosition
= yPos
;
440 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
444 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
445 m_yScrollPosition
= 0;
450 ::UpdateWindow ((HWND
) GetHWND());
455 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
457 int orient
= event
.GetOrientation();
459 int nScrollInc
= CalcScrollInc(event
);
463 if (orient
== wxHORIZONTAL
)
465 int newPos
= m_xScrollPosition
+ nScrollInc
;
466 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
470 int newPos
= m_yScrollPosition
+ nScrollInc
;
471 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
474 if (orient
== wxHORIZONTAL
)
476 if (m_xScrollingEnabled
)
477 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
483 if (m_yScrollingEnabled
)
484 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
489 if (orient
== wxHORIZONTAL
)
491 m_xScrollPosition
+= nScrollInc
;
495 m_yScrollPosition
+= nScrollInc
;
500 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
502 int pos
= event
.GetPosition();
503 int orient
= event
.GetOrientation();
506 switch (event
.GetEventType())
508 case wxEVT_SCROLL_TOP
:
510 if (orient
== wxHORIZONTAL
)
511 nScrollInc
= - m_xScrollPosition
;
513 nScrollInc
= - m_yScrollPosition
;
516 case wxEVT_SCROLL_BOTTOM
:
518 if (orient
== wxHORIZONTAL
)
519 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
521 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
524 case wxEVT_SCROLL_LINEUP
:
529 case wxEVT_SCROLL_LINEDOWN
:
534 case wxEVT_SCROLL_PAGEUP
:
536 if (orient
== wxHORIZONTAL
)
537 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
539 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
542 case wxEVT_SCROLL_PAGEDOWN
:
544 if (orient
== wxHORIZONTAL
)
545 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
547 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
550 case wxEVT_SCROLL_THUMBTRACK
:
552 if (orient
== wxHORIZONTAL
)
553 nScrollInc
= pos
- m_xScrollPosition
;
555 nScrollInc
= pos
- m_yScrollPosition
;
563 if (orient
== wxHORIZONTAL
)
566 GetClientSize(&w
, &h
);
568 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
569 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
573 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
574 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
575 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
576 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
583 GetClientSize(&w
, &h
);
585 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
586 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
590 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
591 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
592 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
593 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
599 // Adjust the scrollbars - new version.
600 void wxToolBarBase::AdjustScrollbars()
603 GetClientSize(&w
, &h
);
605 // Recalculate scroll bar range and position
606 if (m_xScrollLines
> 0)
608 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
609 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
613 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
615 // Calculate page size i.e. number of scroll units you get on the
616 // current client window
617 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
618 if (noPagePositions
< 1)
621 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
622 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
624 if (m_yScrollLines
> 0)
626 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
627 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
631 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
633 // Calculate page size i.e. number of scroll units you get on the
634 // current client window
635 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
636 if (noPagePositions
< 1)
639 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
640 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
644 // Default OnSize resets scrollbars, if any
645 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
647 #if wxUSE_CONSTRAINTS
655 // Prepare the DC by translating it according to the current scroll position
656 void wxToolBarBase::PrepareDC(wxDC
& dc
)
658 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
661 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
663 *x_unit
= m_xScrollPixelsPerLine
;
664 *y_unit
= m_yScrollPixelsPerLine
;
667 int wxToolBarBase::GetScrollPageSize(int orient
) const
669 if ( orient
== wxHORIZONTAL
)
670 return m_xScrollLinesPerPage
;
672 return m_yScrollLinesPerPage
;
675 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
677 if ( orient
== wxHORIZONTAL
)
678 m_xScrollLinesPerPage
= pageSize
;
680 m_yScrollLinesPerPage
= pageSize
;
684 * Scroll to given position (scroll position, not pixel position)
686 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
689 ViewStart (&old_x
, &old_y
);
690 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
695 m_xScrollPosition
= x_pos
;
696 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
700 m_yScrollPosition
= y_pos
;
701 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
705 UpdateWindow ((HWND
) GetHWND());
709 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
711 m_xScrollingEnabled
= x_scroll
;
712 m_yScrollingEnabled
= y_scroll
;
715 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
717 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
718 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
721 // Where the current view starts from
722 void wxToolBarBase::ViewStart (int *x
, int *y
) const
724 *x
= m_xScrollPosition
;
725 *y
= m_yScrollPosition
;
728 void wxToolBarBase::OnIdle(wxIdleEvent
&
737 wxWindow::OnIdle(event
);
743 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
744 void wxToolBarBase::DoToolbarUpdates()
746 wxEvtHandler
* evtHandler
= GetEventHandler() ;
748 wxNode
* node
= GetTools().First();
751 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
753 wxUpdateUIEvent
event(tool
->m_index
);
754 event
.SetEventObject(this);
756 if (evtHandler
->ProcessEvent(event
))
758 if (event
.GetSetEnabled())
759 EnableTool(tool
->m_index
, event
.GetEnabled());
760 if (event
.GetSetChecked())
761 ToggleTool(tool
->m_index
, event
.GetChecked());
763 if (event.GetSetText())