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(int index
, wxControl
*control
)
97 m_toolStyle
= wxTOOL_STYLE_CONTROL
;
101 wxToolBarTool::~wxToolBarTool()
104 if (m_deleteSecondBitmap && m_bitmap2)
112 wxToolBarBase::wxToolBarBase(void) : m_tools(wxKEY_INTEGER
)
114 gs_ToolBars
.Append(this);
121 m_defaultHeight
= 15;
125 m_toolSeparation
= 5;
128 m_xScrollPixelsPerLine
= 0;
129 m_yScrollPixelsPerLine
= 0;
130 m_xScrollingEnabled
= TRUE
;
131 m_yScrollingEnabled
= TRUE
;
132 m_xScrollPosition
= 0;
133 m_yScrollPosition
= 0;
134 m_calcScrolledOffset
= TRUE
;
137 m_xScrollLinesPerPage
= 0;
138 m_yScrollLinesPerPage
= 0;
141 wxToolBarBase::~wxToolBarBase ()
143 gs_ToolBars
.DeleteObject(this);
145 for ( wxNode
*node
= m_tools
.First(); node
; node
= node
->Next() )
147 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
152 // Only allow toggle if returns TRUE
153 bool wxToolBarBase::OnLeftClick(int toolIndex
, bool toggleDown
)
155 wxCommandEvent
event(wxEVT_COMMAND_TOOL_CLICKED
, toolIndex
);
156 event
.SetEventObject(this);
157 event
.SetExtraLong((long) toggleDown
);
159 // Send events to this toolbar instead (and thence up the window hierarchy)
160 GetEventHandler()->ProcessEvent(event
);
165 // Call when right button down.
166 void wxToolBarBase::OnRightClick(int toolIndex
,
170 wxCommandEvent
event(wxEVT_COMMAND_TOOL_RCLICKED
, toolIndex
);
171 event
.SetEventObject(this);
172 event
.SetInt(toolIndex
);
174 GetEventHandler()->ProcessEvent(event
);
177 // Called when the mouse cursor enters a tool bitmap (no button pressed).
178 // Argument is -1 if mouse is exiting the toolbar.
179 // Note that for this event, the id of the window is used,
180 // and the integer parameter of wxCommandEvent is used to retrieve
182 void wxToolBarBase::OnMouseEnter ( int toolIndex
)
184 wxCommandEvent
event(wxEVT_COMMAND_TOOL_ENTER
, GetId());
185 event
.SetEventObject(this);
186 event
.SetInt(toolIndex
);
188 GetEventHandler()->ProcessEvent(event
);
191 // If pushedBitmap is NULL, a reversed version of bitmap is
192 // created and used as the pushed/toggled image.
193 // If toggle is TRUE, the button toggles between the two states.
194 wxToolBarTool
*wxToolBarBase::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
195 bool toggle
, wxCoord xPos
, wxCoord yPos
, wxObject
*clientData
,
196 const wxString
& helpString1
, const wxString
& helpString2
)
199 wxToolBarTool
*tool
= new wxToolBarTool( (wxToolBar
*)this, index
, bitmap
, pushedBitmap
, toggle
,
200 (wxObject
*) NULL
, helpString1
, helpString2
);
202 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, pushedBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
204 tool
->m_clientData
= clientData
;
209 tool
->m_x
= m_xMargin
;
214 tool
->m_y
= m_yMargin
;
216 // Calculate reasonable max size in case Layout() not called
217 if ((tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
) > m_maxWidth
)
218 m_maxWidth
= (tool
->m_x
+ bitmap
.GetWidth() + m_xMargin
);
220 if ((tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
) > m_maxHeight
)
221 m_maxHeight
= (tool
->m_y
+ bitmap
.GetHeight() + m_yMargin
);
223 m_tools
.Append((long)index
, tool
);
227 void wxToolBarBase::AddSeparator ()
229 wxToolBarTool
*tool
= new wxToolBarTool
;
231 tool
->m_toolStyle
= wxTOOL_STYLE_SEPARATOR
;
232 m_tools
.Append(-1, tool
);
235 void wxToolBarBase::ClearTools()
237 m_pressedTool
= m_currentTool
= -1;
238 wxNode
*node
= m_tools
.First();
241 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
242 wxNode
*nextNode
= node
->Next();
249 void wxToolBarBase::EnableTool(int index
, bool enable
)
251 wxNode
*node
= m_tools
.Find((long)index
);
254 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
256 tool
->m_enabled
= enable
;
260 void wxToolBarBase::ToggleTool(int WXUNUSED(index
),
261 bool WXUNUSED(toggle
))
265 void wxToolBarBase::SetToggle(int index
, bool value
)
267 wxNode
*node
=m_tools
.Find((long)index
);
269 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
270 tool
->m_isToggle
= value
;
274 bool wxToolBarBase::GetToolState(int index
) const
276 wxNode
*node
= m_tools
.Find((long)index
);
279 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
282 return tool
->m_toggleState
;
289 bool wxToolBarBase::GetToolEnabled(int index
) const
291 wxNode
*node
= m_tools
.Find((long)index
);
294 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
297 return tool
->m_enabled
;
304 wxObject
*wxToolBarBase::GetToolClientData(int index
) const
306 wxNode
*node
= m_tools
.Find((long)index
);
309 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
312 return tool
->m_clientData
;
319 void wxToolBarBase::SetToolShortHelp(int index
, const wxString
& helpString
)
321 wxNode
*node
=m_tools
.Find((long)index
);
324 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
325 tool
->m_shortHelpString
= helpString
;
329 wxString
wxToolBarBase::GetToolShortHelp(int index
) const
331 wxNode
*node
=m_tools
.Find((long)index
);
334 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
335 return tool
->m_shortHelpString
;
341 void wxToolBarBase::SetToolLongHelp(int index
, const wxString
& helpString
)
343 wxNode
*node
=m_tools
.Find((long)index
);
346 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
347 tool
->m_longHelpString
= helpString
;
351 wxString
wxToolBarBase::GetToolLongHelp(int index
) const
353 wxNode
*node
=m_tools
.Find((long)index
);
356 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
357 return tool
->m_longHelpString
;
363 wxToolBarTool
*wxToolBarBase::FindToolForPosition(long x
, long y
) const
365 wxNode
*node
= m_tools
.First();
368 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
369 if ((x
>= tool
->m_x
) && (y
>= tool
->m_y
) &&
370 (x
<= (tool
->m_x
+ tool
->GetWidth())) &&
371 (y
<= (tool
->m_y
+ tool
->GetHeight())))
379 wxSize
wxToolBarBase::GetMaxSize ( void ) const
381 return wxSize(m_maxWidth
, m_maxHeight
);
384 // Okay, so we've left the tool we're in ... we must check if
385 // the tool we're leaving was a 'sprung push button' and if so,
386 // spring it back to the up state.
388 void wxToolBarBase::SetMargins(int x
, int y
)
394 void wxToolBarBase::SetToolPacking(int packing
)
396 m_toolPacking
= packing
;
399 void wxToolBarBase::SetToolSeparation(int separation
)
401 m_toolSeparation
= separation
;
404 void wxToolBarBase::Command(wxCommandEvent
& WXUNUSED(event
))
408 void wxToolBarBase::LayoutTools()
413 // SCROLLING IMPLEMENTATION
416 * pixelsPerUnitX/pixelsPerUnitY: number of pixels per unit (e.g. pixels per text line)
417 * noUnitsX/noUnitsY: : no. units per scrollbar
419 void wxToolBarBase::SetScrollbars (int pixelsPerUnitX
, int pixelsPerUnitY
,
420 int noUnitsX
, int noUnitsY
,
423 m_xScrollPixelsPerLine
= pixelsPerUnitX
;
424 m_yScrollPixelsPerLine
= pixelsPerUnitY
;
425 m_xScrollLines
= noUnitsX
;
426 m_yScrollLines
= noUnitsY
;
431 // Recalculate scroll bar range and position
432 if (m_xScrollLines
> 0)
434 m_xScrollPosition
= xPos
;
435 SetScrollPos (wxHORIZONTAL
, m_xScrollPosition
, TRUE
);
439 SetScrollbar(wxHORIZONTAL
, 0, 0, 0, FALSE
);
440 m_xScrollPosition
= 0;
443 if (m_yScrollLines
> 0)
445 m_yScrollPosition
= yPos
;
446 SetScrollPos (wxVERTICAL
, m_yScrollPosition
, TRUE
);
450 SetScrollbar(wxVERTICAL
, 0, 0, 0, FALSE
);
451 m_yScrollPosition
= 0;
456 ::UpdateWindow ((HWND
) GetHWND());
461 void wxToolBarBase::OnScroll(wxScrollEvent
& event
)
463 int orient
= event
.GetOrientation();
465 int nScrollInc
= CalcScrollInc(event
);
469 if (orient
== wxHORIZONTAL
)
471 int newPos
= m_xScrollPosition
+ nScrollInc
;
472 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
476 int newPos
= m_yScrollPosition
+ nScrollInc
;
477 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
480 if (orient
== wxHORIZONTAL
)
482 if (m_xScrollingEnabled
)
483 ScrollWindow(-m_xScrollPixelsPerLine
* nScrollInc
, 0, NULL
);
489 if (m_yScrollingEnabled
)
490 ScrollWindow(0, -m_yScrollPixelsPerLine
* nScrollInc
, NULL
);
495 if (orient
== wxHORIZONTAL
)
497 m_xScrollPosition
+= nScrollInc
;
501 m_yScrollPosition
+= nScrollInc
;
506 int wxToolBarBase::CalcScrollInc(wxScrollEvent
& event
)
508 int pos
= event
.GetPosition();
509 int orient
= event
.GetOrientation();
512 switch (event
.GetEventType())
514 case wxEVT_SCROLL_TOP
:
516 if (orient
== wxHORIZONTAL
)
517 nScrollInc
= - m_xScrollPosition
;
519 nScrollInc
= - m_yScrollPosition
;
522 case wxEVT_SCROLL_BOTTOM
:
524 if (orient
== wxHORIZONTAL
)
525 nScrollInc
= m_xScrollLines
- m_xScrollPosition
;
527 nScrollInc
= m_yScrollLines
- m_yScrollPosition
;
530 case wxEVT_SCROLL_LINEUP
:
535 case wxEVT_SCROLL_LINEDOWN
:
540 case wxEVT_SCROLL_PAGEUP
:
542 if (orient
== wxHORIZONTAL
)
543 nScrollInc
= -GetScrollPageSize(wxHORIZONTAL
);
545 nScrollInc
= -GetScrollPageSize(wxVERTICAL
);
548 case wxEVT_SCROLL_PAGEDOWN
:
550 if (orient
== wxHORIZONTAL
)
551 nScrollInc
= GetScrollPageSize(wxHORIZONTAL
);
553 nScrollInc
= GetScrollPageSize(wxVERTICAL
);
556 case wxEVT_SCROLL_THUMBTRACK
:
558 if (orient
== wxHORIZONTAL
)
559 nScrollInc
= pos
- m_xScrollPosition
;
561 nScrollInc
= pos
- m_yScrollPosition
;
569 if (orient
== wxHORIZONTAL
)
572 GetClientSize(&w
, &h
);
574 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
575 int noPositions
= (int) ( ((nMaxWidth
- w
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
579 if ( (m_xScrollPosition
+ nScrollInc
) < 0 )
580 nScrollInc
= -m_xScrollPosition
; // As -ve as we can go
581 else if ( (m_xScrollPosition
+ nScrollInc
) > noPositions
)
582 nScrollInc
= noPositions
- m_xScrollPosition
; // As +ve as we can go
589 GetClientSize(&w
, &h
);
591 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
592 int noPositions
= (int) ( ((nMaxHeight
- h
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
596 if ( (m_yScrollPosition
+ nScrollInc
) < 0 )
597 nScrollInc
= -m_yScrollPosition
; // As -ve as we can go
598 else if ( (m_yScrollPosition
+ nScrollInc
) > noPositions
)
599 nScrollInc
= noPositions
- m_yScrollPosition
; // As +ve as we can go
605 // Adjust the scrollbars - new version.
606 void wxToolBarBase::AdjustScrollbars()
609 GetClientSize(&w
, &h
);
611 // Recalculate scroll bar range and position
612 if (m_xScrollLines
> 0)
614 int nMaxWidth
= m_xScrollLines
*m_xScrollPixelsPerLine
;
615 int newRange
= (int) ( ((nMaxWidth
)/(float)m_xScrollPixelsPerLine
) + 0.5 );
619 m_xScrollPosition
= wxMin(newRange
, m_xScrollPosition
);
621 // Calculate page size i.e. number of scroll units you get on the
622 // current client window
623 int noPagePositions
= (int) ( (w
/(float)m_xScrollPixelsPerLine
) + 0.5 );
624 if (noPagePositions
< 1)
627 SetScrollbar(wxHORIZONTAL
, m_xScrollPosition
, noPagePositions
, newRange
);
628 SetScrollPageSize(wxHORIZONTAL
, noPagePositions
);
630 if (m_yScrollLines
> 0)
632 int nMaxHeight
= m_yScrollLines
*m_yScrollPixelsPerLine
;
633 int newRange
= (int) ( ((nMaxHeight
)/(float)m_yScrollPixelsPerLine
) + 0.5 );
637 m_yScrollPosition
= wxMin(newRange
, m_yScrollPosition
);
639 // Calculate page size i.e. number of scroll units you get on the
640 // current client window
641 int noPagePositions
= (int) ( (h
/(float)m_yScrollPixelsPerLine
) + 0.5 );
642 if (noPagePositions
< 1)
645 SetScrollbar(wxVERTICAL
, m_yScrollPosition
, noPagePositions
, newRange
);
646 SetScrollPageSize(wxVERTICAL
, noPagePositions
);
650 // Default OnSize resets scrollbars, if any
651 void wxToolBarBase::OnSize(wxSizeEvent
& WXUNUSED(event
))
653 #if wxUSE_CONSTRAINTS
661 // Prepare the DC by translating it according to the current scroll position
662 void wxToolBarBase::PrepareDC(wxDC
& dc
)
664 dc
.SetDeviceOrigin(- m_xScrollPosition
* m_xScrollPixelsPerLine
, - m_yScrollPosition
* m_yScrollPixelsPerLine
);
667 void wxToolBarBase::GetScrollPixelsPerUnit (int *x_unit
, int *y_unit
) const
669 *x_unit
= m_xScrollPixelsPerLine
;
670 *y_unit
= m_yScrollPixelsPerLine
;
673 int wxToolBarBase::GetScrollPageSize(int orient
) const
675 if ( orient
== wxHORIZONTAL
)
676 return m_xScrollLinesPerPage
;
678 return m_yScrollLinesPerPage
;
681 void wxToolBarBase::SetScrollPageSize(int orient
, int pageSize
)
683 if ( orient
== wxHORIZONTAL
)
684 m_xScrollLinesPerPage
= pageSize
;
686 m_yScrollLinesPerPage
= pageSize
;
690 * Scroll to given position (scroll position, not pixel position)
692 void wxToolBarBase::Scroll (int x_pos
, int y_pos
)
695 ViewStart (&old_x
, &old_y
);
696 if (((x_pos
== -1) || (x_pos
== old_x
)) && ((y_pos
== -1) || (y_pos
== old_y
)))
701 m_xScrollPosition
= x_pos
;
702 SetScrollPos (wxHORIZONTAL
, x_pos
, TRUE
);
706 m_yScrollPosition
= y_pos
;
707 SetScrollPos (wxVERTICAL
, y_pos
, TRUE
);
711 UpdateWindow ((HWND
) GetHWND());
715 void wxToolBarBase::EnableScrolling (bool x_scroll
, bool y_scroll
)
717 m_xScrollingEnabled
= x_scroll
;
718 m_yScrollingEnabled
= y_scroll
;
721 void wxToolBarBase::GetVirtualSize (int *x
, int *y
) const
723 *x
= m_xScrollPixelsPerLine
* m_xScrollLines
;
724 *y
= m_yScrollPixelsPerLine
* m_yScrollLines
;
727 // Where the current view starts from
728 void wxToolBarBase::ViewStart (int *x
, int *y
) const
730 *x
= m_xScrollPosition
;
731 *y
= m_yScrollPosition
;
734 void wxToolBarBase::OnIdle(wxIdleEvent
&
743 wxWindow::OnIdle(event
);
749 // Do the toolbar button updates (check for EVT_UPDATE_UI handlers)
750 void wxToolBarBase::DoToolbarUpdates()
752 wxEvtHandler
* evtHandler
= GetEventHandler() ;
754 wxNode
* node
= GetTools().First();
757 wxToolBarTool
* tool
= (wxToolBarTool
* ) node
->Data();
759 wxUpdateUIEvent
event(tool
->m_index
);
760 event
.SetEventObject(this);
762 if (evtHandler
->ProcessEvent(event
))
764 if (event
.GetSetEnabled())
765 EnableTool(tool
->m_index
, event
.GetEnabled());
766 if (event
.GetSetChecked())
767 ToggleTool(tool
->m_index
, event
.GetChecked());
769 if (event.GetSetText())