1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/toolbar.cpp
3 // Author: Robert Roebling
5 // Copyright: (c) 2001 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
18 #pragma implementation "univtoolbar.h"
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
33 #include "wx/toolbar.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 BEGIN_EVENT_TABLE(wxToolBar
,wxToolBarBase
)
41 EVT_MOUSE_EVENTS( wxToolBar::OnMouse
)
42 EVT_PAINT( wxToolBar::OnPaint
)
43 EVT_SIZE( wxToolBar::OnSize
)
46 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
47 const wxPoint
& pos
, const wxSize
& size
,
48 long style
, const wxString
& name
)
50 bool ret
= wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
55 void wxToolBar::Init()
57 // TODO: this is from tbarbase.cpp, but should be in
58 // wxToolbarBase::Init
59 // the list owns the pointers
60 m_tools
.DeleteContents(TRUE
);
61 m_xMargin
= m_yMargin
= 0;
62 m_maxRows
= m_maxCols
= 0;
69 SetToolBitmapSize( wxSize(16,15) );
72 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
77 void wxToolBar::SetToolShortHelp(int id
, const wxString
& helpString
)
81 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
)
86 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
91 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
93 // TODO: figure out why it gives a BadMatch when
94 // creating the bitmap (out.ConvertToBitmap).
95 // Possibly we should ensure all depths match.
97 // Created disabled-state bitmap on demand
98 if (!enable
&& !tool
->GetBitmap2().Ok())
100 wxImage
in(tool
->GetBitmap1());
101 wxImage
out(tool
->GetBitmap1());
103 wxCreateGreyedImage(in
, out
);
105 tool
->SetBitmap2(out
.ConvertToBitmap());
107 RefreshTool((wxToolBarTool
*) tool
);
111 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
113 wxToolBarTool
*my_tool
= (wxToolBarTool
*) tool
;
115 bool refresh
= (my_tool
->IsToggled() != toggle
);
117 my_tool
->m_isDown
= toggle
;
120 RefreshTool( my_tool
);
123 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
)
127 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
128 const wxBitmap
& bitmap1
,
129 const wxBitmap
& bitmap2
,
131 wxObject
*clientData
,
132 const wxString
& shortHelpString
,
133 const wxString
& longHelpString
)
135 return new wxToolBarTool( this, id
, bitmap1
, bitmap2
, toggle
,
136 clientData
, shortHelpString
, longHelpString
);
139 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
141 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
146 void wxToolBar::RefreshTool( wxToolBarTool
*tool
)
148 wxRect
rect( tool
->m_x
, tool
->m_y
, m_defaultWidth
+6, m_defaultHeight
+6 );
149 Refresh( TRUE
, &rect
);
152 void wxToolBar::DrawToolBarTool( wxToolBarTool
*tool
, wxDC
&dc
, bool down
)
154 const wxBitmap
& bitmap
= (tool
->IsEnabled() || !tool
->GetBitmap2().Ok()) ? tool
->GetBitmap1() : tool
->GetBitmap2() ;
157 dc
.DrawBitmap( bitmap
, tool
->m_x
+4, tool
->m_y
+4, TRUE
);
159 dc
.SetPen( *wxGREY_PEN
);
160 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
161 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
163 dc
.SetPen( *wxBLACK_PEN
);
164 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1 );
165 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4 );
167 dc
.SetPen( *wxWHITE_PEN
);
168 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
169 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
173 dc
.DrawBitmap( bitmap
, tool
->m_x
+3, tool
->m_y
+3, TRUE
);
175 dc
.SetPen( *wxWHITE_PEN
);
176 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
177 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
178 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+2 );
179 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+2, tool
->m_y
+m_defaultHeight
+4 );
181 dc
.SetPen( *wxBLACK_PEN
);
182 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
183 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
185 dc
.SetPen( *wxGREY_PEN
);
186 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+4 );
187 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+m_defaultHeight
+5 );
191 void wxToolBar::OnPaint(wxPaintEvent
&event
)
195 wxSize clientSize
= GetClientSize();
196 dc
.SetPen( *wxBLACK_PEN
);
197 dc
.DrawLine( 0,0, clientSize
.x
,0 );
199 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
201 node
= node
->GetNext() )
203 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
205 if (tool
->GetId() == -1) continue;
207 DrawToolBarTool( tool
, dc
, tool
->m_isDown
);
211 bool wxToolBar::Realize()
213 if (!wxToolBarBase::Realize())
219 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
230 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
232 node
= node
->GetNext() )
234 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
236 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
238 if (tool
->GetId() == -1)
243 tool
->m_x
= m_xMargin
;
245 y
+= m_defaultHeight
+ 6;
247 // Calculate the maximum height or width (depending on style)
248 // so we know how to size the toolbar in Realize.
249 // We could get the size of the tool instead of the
250 // default bitmap size
251 if (m_maxWidth
< (m_defaultWidth
+ 2*(m_xMargin
+ 2)))
252 m_maxWidth
= (m_defaultWidth
+ 2*(m_xMargin
+ 2)) ;
256 if (tool
->GetId() == -1)
262 tool
->m_y
= m_yMargin
;
263 x
+= m_defaultWidth
+ 6;
265 // Calculate the maximum height or width (depending on style)
266 // so we know how to size the toolbar in Realize.
267 // We could get the size of the tool instead of the
268 // default bitmap size
269 if (m_maxHeight
< (m_defaultHeight
+ 2*(m_yMargin
+ 2)))
270 m_maxHeight
= (m_defaultHeight
+ 2*(m_yMargin
+ 2)) ;
275 wxSize sz
= GetSize();
276 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
278 SetSize(m_maxWidth
, sz
.y
);
282 SetSize(sz
.x
, m_maxHeight
);
285 // Commenting out Robert's fix since the above should be
287 // SetSize( x+16, m_defaultHeight + 14 );
292 void wxToolBar::OnMouse(wxMouseEvent
&event
)
294 wxToolBarTool
*hit
= NULL
;
295 int x
= event
.GetX();
296 int y
= event
.GetY();
298 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
300 node
= node
->GetNext() )
302 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
304 if ((x
> tool
->m_x
) && (x
< tool
->m_x
+m_defaultWidth
+5) &&
305 (y
> tool
->m_y
) && (y
< tool
->m_y
+m_defaultHeight
+5))
312 if (event
.LeftDown() && (hit
) && hit
->IsEnabled())
317 m_captured
->m_isDown
= TRUE
;
318 RefreshTool( m_captured
);
323 if (event
.Dragging() && (m_captured
))
325 bool is_down
= (hit
== m_captured
);
326 if (is_down
!= m_captured
->m_isDown
)
328 m_captured
->m_isDown
= is_down
;
329 RefreshTool( m_captured
);
335 if (event
.LeftUp() && (m_captured
))
339 m_captured
->m_isDown
= FALSE
;
340 RefreshTool( m_captured
);
342 if (hit
== m_captured
)
344 wxCommandEvent
cevent( wxEVT_COMMAND_TOOL_CLICKED
, m_captured
->GetId() );
345 cevent
.SetEventObject( this );
346 // cevent.SetExtraLong((long) toggleDown);
347 GetEventHandler()->ProcessEvent( cevent
);