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
)
44 EVT_ENTER_WINDOW( wxToolBar::OnEnter
)
45 EVT_LEAVE_WINDOW( wxToolBar::OnLeave
)
48 bool wxToolBar::Create( wxWindow
*parent
, wxWindowID id
,
49 const wxPoint
& pos
, const wxSize
& size
,
50 long style
, const wxString
& name
)
52 bool ret
= wxToolBarBase::Create( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
);
57 void wxToolBar::Init()
59 // TODO: this is from tbarbase.cpp, but should be in
60 // wxToolbarBase::Init
61 // the list owns the pointers
62 m_tools
.DeleteContents(TRUE
);
63 m_xMargin
= m_yMargin
= 0;
64 m_maxRows
= m_maxCols
= 0;
73 SetToolBitmapSize( wxSize(16,15) );
76 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord x
, wxCoord y
) const
81 void wxToolBar::SetToolShortHelp(int id
, const wxString
& helpString
)
85 bool wxToolBar::DoInsertTool(size_t pos
, wxToolBarToolBase
*tool
)
90 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
95 void wxToolBar::DoEnableTool(wxToolBarToolBase
*tool
, bool enable
)
97 // Comment this out if you don't want the disabled look,
98 // which currently acts weirdly for the scissors icon
99 // in the toolbar sample. See src/common/tbarbase.cpp
100 // for the wxCreateGreyedImage function.
102 // Created disabled-state bitmap on demand
103 if (!enable
&& !tool
->GetBitmap2().Ok())
105 wxImage
image( tool
->GetBitmap1() );
107 unsigned char bg_red
= 180;
108 unsigned char bg_green
= 180;
109 unsigned char bg_blue
= 180;
111 unsigned char mask_red
= image
.GetMaskRed();
112 unsigned char mask_green
= image
.GetMaskGreen();
113 unsigned char mask_blue
= image
.GetMaskBlue();
115 bool has_mask
= image
.HasMask();
118 for (y
= 0; y
< image
.GetHeight(); y
++)
120 for (x
= 0; x
< image
.GetWidth(); x
++)
122 unsigned char red
= image
.GetRed(x
,y
);
123 unsigned char green
= image
.GetGreen(x
,y
);
124 unsigned char blue
= image
.GetBlue(x
,y
);
125 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
127 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
128 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
129 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
130 image
.SetRGB( x
, y
, red
, green
, blue
);
135 for (y
= 0; y
< image
.GetHeight(); y
++)
137 for (x
= y
% 2; x
< image
.GetWidth(); x
+= 2)
139 unsigned char red
= image
.GetRed(x
,y
);
140 unsigned char green
= image
.GetGreen(x
,y
);
141 unsigned char blue
= image
.GetBlue(x
,y
);
142 if (!has_mask
|| red
!= mask_red
|| green
!= mask_green
|| blue
!= mask_blue
)
144 red
= (((wxInt32
) red
- bg_red
) >> 1) + bg_red
;
145 green
= (((wxInt32
) green
- bg_green
) >> 1) + bg_green
;
146 blue
= (((wxInt32
) blue
- bg_blue
) >> 1) + bg_blue
;
147 image
.SetRGB( x
, y
, red
, green
, blue
);
152 tool
->SetBitmap2( image
.ConvertToBitmap() );
155 RefreshTool((wxToolBarTool
*) tool
);
159 void wxToolBar::DoToggleTool(wxToolBarToolBase
*tool
, bool toggle
)
161 wxToolBarTool
*my_tool
= (wxToolBarTool
*) tool
;
163 bool refresh
= (my_tool
->IsToggled() != toggle
);
165 my_tool
->m_isDown
= toggle
;
168 RefreshTool( my_tool
);
171 void wxToolBar::DoSetToggle(wxToolBarToolBase
*tool
, bool toggle
)
175 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
176 const wxBitmap
& bitmap1
,
177 const wxBitmap
& bitmap2
,
179 wxObject
*clientData
,
180 const wxString
& shortHelpString
,
181 const wxString
& longHelpString
)
183 return new wxToolBarTool( this, id
, bitmap1
, bitmap2
, toggle
,
184 clientData
, shortHelpString
, longHelpString
);
187 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
189 wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") );
194 void wxToolBar::RefreshTool( wxToolBarTool
*tool
)
196 wxRect
rect( tool
->m_x
, tool
->m_y
, m_defaultWidth
+6, m_defaultHeight
+6 );
197 Refresh( TRUE
, &rect
);
200 void wxToolBar::DrawToolBarTool( wxToolBarTool
*tool
, wxDC
&dc
, bool down
)
202 const wxBitmap
& bitmap
= (tool
->IsEnabled() || !tool
->GetBitmap2().Ok()) ? tool
->GetBitmap1() : tool
->GetBitmap2() ;
204 if (HasFlag(wxTB_FLAT
) && (tool
!= m_underMouse
))
207 dc
.DrawBitmap( bitmap
, tool
->m_x
+4, tool
->m_y
+4, TRUE
);
209 dc
.DrawBitmap( bitmap
, tool
->m_x
+3, tool
->m_y
+3, TRUE
);
216 dc
.DrawBitmap( bitmap
, tool
->m_x
+4, tool
->m_y
+4, TRUE
);
218 dc
.SetPen( *wxGREY_PEN
);
219 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
220 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
222 dc
.SetPen( *wxBLACK_PEN
);
223 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1 );
224 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4 );
226 dc
.SetPen( *wxWHITE_PEN
);
227 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
228 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
232 dc
.DrawBitmap( bitmap
, tool
->m_x
+3, tool
->m_y
+3, TRUE
);
234 dc
.SetPen( *wxWHITE_PEN
);
235 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
);
236 dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 );
237 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+2 );
238 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+2, tool
->m_y
+m_defaultHeight
+4 );
240 dc
.SetPen( *wxBLACK_PEN
);
241 dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 );
242 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 );
244 dc
.SetPen( *wxGREY_PEN
);
245 dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+4 );
246 dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+m_defaultHeight
+5 );
250 void wxToolBar::OnPaint(wxPaintEvent
&event
)
254 wxSize clientSize
= GetClientSize();
255 dc
.SetPen( *wxBLACK_PEN
);
256 dc
.DrawLine( 0,0, clientSize
.x
,0 );
258 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
260 node
= node
->GetNext() )
262 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
264 if (tool
->GetId() == -1) continue;
266 DrawToolBarTool( tool
, dc
, tool
->m_isDown
);
270 bool wxToolBar::Realize()
272 if (!wxToolBarBase::Realize())
278 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
280 m_xMargin
+= 1; // Cannot help it, but otherwise it look ugly
287 m_yMargin
+= 1; // Cannot help it, but otherwise it look ugly
293 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
295 node
= node
->GetNext() )
297 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
299 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
301 if (tool
->GetId() == -1)
306 tool
->m_x
= m_xMargin
;
308 y
+= m_defaultHeight
+ 6;
310 // Calculate the maximum height or width (depending on style)
311 // so we know how to size the toolbar in Realize.
312 // We could get the size of the tool instead of the
313 // default bitmap size
314 if (m_maxWidth
< (m_defaultWidth
+ 2*(m_xMargin
+ 2)))
315 m_maxWidth
= (m_defaultWidth
+ 2*(m_xMargin
+ 2)) ;
319 if (tool
->GetId() == -1)
325 tool
->m_y
= m_yMargin
;
326 x
+= m_defaultWidth
+ 6;
328 // Calculate the maximum height or width (depending on style)
329 // so we know how to size the toolbar in Realize.
330 // We could get the size of the tool instead of the
331 // default bitmap size
332 if (m_maxHeight
< (m_defaultHeight
+ 2*(m_yMargin
+ 2)))
333 m_maxHeight
= (m_defaultHeight
+ 2*(m_yMargin
+ 2)) ;
338 wxSize sz
= GetSize();
339 if (GetWindowStyleFlag() & wxTB_VERTICAL
)
341 SetSize(m_maxWidth
, sz
.y
);
345 SetSize(sz
.x
, m_maxHeight
);
351 void wxToolBar::OnLeave(wxMouseEvent
&event
)
353 if (HasFlag( wxTB_FLAT
))
355 wxToolBarTool
*oldMouseUnder
= m_underMouse
;
359 RefreshTool( oldMouseUnder
);
364 void wxToolBar::OnEnter(wxMouseEvent
&event
)
368 void wxToolBar::OnMouse(wxMouseEvent
&event
)
370 wxToolBarTool
*hit
= NULL
;
371 int x
= event
.GetX();
372 int y
= event
.GetY();
374 for ( wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
376 node
= node
->GetNext() )
378 wxToolBarTool
*tool
= (wxToolBarTool
*) node
->Data();
380 if ((x
> tool
->m_x
) && (x
< tool
->m_x
+m_defaultWidth
+5) &&
381 (y
> tool
->m_y
) && (y
< tool
->m_y
+m_defaultHeight
+5))
388 wxToolBarTool
*oldMouseUnder
= m_underMouse
;
389 if (HasFlag( wxTB_FLAT
))
391 if (m_underMouse
&& (m_underMouse
!= hit
))
394 RefreshTool( oldMouseUnder
);
399 if (event
.LeftDown() && (hit
) && hit
->IsEnabled())
404 m_captured
->m_isDown
= TRUE
;
405 RefreshTool( m_captured
);
410 if (event
.Dragging() && (m_captured
))
412 bool is_down
= (hit
== m_captured
);
413 if (is_down
!= m_captured
->m_isDown
)
415 m_captured
->m_isDown
= is_down
;
416 RefreshTool( m_captured
);
422 if (event
.LeftUp() && (m_captured
))
426 m_captured
->m_isDown
= FALSE
;
431 RefreshTool( m_captured
);
433 if (hit
== m_captured
)
435 wxCommandEvent
cevent( wxEVT_COMMAND_TOOL_CLICKED
, m_captured
->GetId() );
436 cevent
.SetEventObject( this );
437 // cevent.SetExtraLong((long) toggleDown);
438 GetEventHandler()->ProcessEvent( cevent
);
446 if (HasFlag(wxTB_FLAT
))
448 if (m_underMouse
&& (m_underMouse
!= oldMouseUnder
))
449 RefreshTool( m_underMouse
);