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     // Created disabled-state bitmap on demand 
  94     if (!enable 
&& !tool
->GetBitmap2().Ok()) 
  96         wxImage 
in(tool
->GetBitmap1()); 
  97         wxImage 
out(tool
->GetBitmap1()); 
  99         wxCreateGreyedImage(in
, out
); 
 101         tool
->SetBitmap2(out
.ConvertToBitmap()); 
 103     RefreshTool((wxToolBarTool
*) tool
);     
 106 void wxToolBar::DoToggleTool(wxToolBarToolBase 
*tool
, bool toggle
) 
 108     wxToolBarTool 
*my_tool 
= (wxToolBarTool
*) tool
; 
 110     bool refresh 
= (my_tool
->IsToggled() != toggle
); 
 112     my_tool
->m_isDown 
= toggle
; 
 115         RefreshTool( my_tool 
); 
 118 void wxToolBar::DoSetToggle(wxToolBarToolBase 
*tool
, bool toggle
) 
 122 wxToolBarToolBase 
*wxToolBar::CreateTool(int id
, 
 123                                           const wxBitmap
& bitmap1
, 
 124                                           const wxBitmap
& bitmap2
, 
 126                                           wxObject 
*clientData
, 
 127                                           const wxString
& shortHelpString
, 
 128                                           const wxString
& longHelpString
) 
 130     return new wxToolBarTool( this, id
, bitmap1
, bitmap2
, toggle
, 
 131                               clientData
, shortHelpString
, longHelpString
); 
 134 wxToolBarToolBase 
*wxToolBar::CreateTool(wxControl 
*control
) 
 136     wxFAIL_MSG( wxT("Toolbar doesn't support controls yet.") ); 
 141 void wxToolBar::RefreshTool( wxToolBarTool 
*tool 
) 
 143     wxRect 
rect( tool
->m_x
, tool
->m_y
, m_defaultWidth
+6, m_defaultHeight
+6 ); 
 144     Refresh( TRUE
, &rect 
); 
 147 void wxToolBar::DrawToolBarTool( wxToolBarTool 
*tool
, wxDC 
&dc
, bool down 
) 
 149     const wxBitmap
& bitmap 
= (tool
->IsEnabled() || !tool
->GetBitmap1().Ok()) ? tool
->GetBitmap1() : tool
->GetBitmap2() ; 
 152         dc
.DrawBitmap( bitmap
, tool
->m_x
+4, tool
->m_y
+4, TRUE 
); 
 154         dc
.SetPen( *wxGREY_PEN 
); 
 155         dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y 
); 
 156         dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 ); 
 158         dc
.SetPen( *wxBLACK_PEN 
); 
 159         dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1 ); 
 160         dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+1, tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4 ); 
 162         dc
.SetPen( *wxWHITE_PEN 
); 
 163         dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 ); 
 164         dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 ); 
 168         dc
.DrawBitmap( bitmap
, tool
->m_x
+3, tool
->m_y
+3, TRUE 
); 
 170         dc
.SetPen( *wxWHITE_PEN 
); 
 171         dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y 
); 
 172         dc
.DrawLine( tool
->m_x
, tool
->m_y
, tool
->m_x
, tool
->m_y
+m_defaultHeight
+5 ); 
 173         dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+2 ); 
 174         dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+2, tool
->m_y
+m_defaultHeight
+4 ); 
 176         dc
.SetPen( *wxBLACK_PEN 
); 
 177         dc
.DrawLine( tool
->m_x
, tool
->m_y
+m_defaultHeight
+5, tool
->m_x
+m_defaultWidth
+6, tool
->m_y
+m_defaultHeight
+5 ); 
 178         dc
.DrawLine( tool
->m_x
+m_defaultWidth
+5, tool
->m_y
, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+6 ); 
 180         dc
.SetPen( *wxGREY_PEN 
); 
 181         dc
.DrawLine( tool
->m_x
+1, tool
->m_y
+m_defaultHeight
+4, tool
->m_x
+m_defaultWidth
+5, tool
->m_y
+m_defaultHeight
+4 ); 
 182         dc
.DrawLine( tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+1, tool
->m_x
+m_defaultWidth
+4, tool
->m_y
+m_defaultHeight
+5 ); 
 186 void wxToolBar::OnPaint(wxPaintEvent 
&event
) 
 190     wxSize clientSize 
= GetClientSize(); 
 191     dc
.SetPen( *wxBLACK_PEN 
); 
 192     dc
.DrawLine( 0,0, clientSize
.x
,0 ); 
 194     for ( wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 196           node 
= node
->GetNext() ) 
 198         wxToolBarTool 
*tool 
= (wxToolBarTool
*) node
->Data(); 
 200         if (tool
->GetId() == -1) continue; 
 202         DrawToolBarTool( tool
, dc
, tool
->m_isDown 
); 
 206 bool wxToolBar::Realize() 
 208     if (!wxToolBarBase::Realize()) 
 214     if (GetWindowStyleFlag() & wxTB_VERTICAL
) 
 225     for ( wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 227           node 
= node
->GetNext() ) 
 229         wxToolBarTool 
*tool 
= (wxToolBarTool
*) node
->Data(); 
 231         if (GetWindowStyleFlag() & wxTB_VERTICAL
) 
 233             if (tool
->GetId() == -1) 
 238             tool
->m_x 
= m_xMargin
; 
 240             y 
+= m_defaultHeight 
+ 6; 
 242             // Calculate the maximum height or width (depending on style) 
 243             // so we know how to size the toolbar in Realize. 
 244             // We could get the size of the tool instead of the 
 245             // default bitmap size 
 246             if (m_maxWidth 
< (m_defaultWidth 
+ 2*(m_xMargin 
+ 2))) 
 247                 m_maxWidth 
= (m_defaultWidth 
+ 2*(m_xMargin 
+ 2)) ; 
 251             if (tool
->GetId() == -1) 
 257             tool
->m_y 
= m_yMargin
; 
 258             x 
+= m_defaultWidth 
+ 6; 
 260             // Calculate the maximum height or width (depending on style) 
 261             // so we know how to size the toolbar in Realize. 
 262             // We could get the size of the tool instead of the 
 263             // default bitmap size 
 264             if (m_maxHeight 
< (m_defaultHeight 
+ 2*(m_yMargin 
+ 2))) 
 265                 m_maxHeight 
= (m_defaultHeight 
+ 2*(m_yMargin 
+ 2)) ; 
 270     wxSize sz 
= GetSize(); 
 271     if (GetWindowStyleFlag() & wxTB_VERTICAL
) 
 273         SetSize(m_maxWidth
, sz
.y
); 
 277         SetSize(sz
.x
, m_maxHeight
); 
 280     // Commenting out Robert's fix since the above should be 
 282     //    SetSize( x+16, m_defaultHeight + 14 ); 
 287 void wxToolBar::OnMouse(wxMouseEvent 
&event
) 
 289     wxToolBarTool 
*hit 
= NULL
; 
 290     int x 
= event
.GetX(); 
 291     int y 
= event
.GetY(); 
 293     for ( wxToolBarToolsList::Node 
*node 
= m_tools
.GetFirst(); 
 295           node 
= node
->GetNext() ) 
 297         wxToolBarTool 
*tool 
= (wxToolBarTool
*) node
->Data(); 
 299         if ((x 
> tool
->m_x
) && (x 
< tool
->m_x
+m_defaultWidth
+5)  && 
 300             (y 
> tool
->m_y
) && (y 
< tool
->m_y
+m_defaultHeight
+5)) 
 307     if (event
.LeftDown() && (hit
) && hit
->IsEnabled()) 
 312         m_captured
->m_isDown 
= TRUE
; 
 313         RefreshTool( m_captured 
); 
 318     if (event
.Dragging() && (m_captured
)) 
 320         bool is_down 
= (hit 
== m_captured
); 
 321         if (is_down 
!= m_captured
->m_isDown
) 
 323             m_captured
->m_isDown 
= is_down
; 
 324             RefreshTool( m_captured 
); 
 330     if (event
.LeftUp() && (m_captured
)) 
 334         m_captured
->m_isDown 
= FALSE
; 
 335         RefreshTool( m_captured 
); 
 337         if (hit 
== m_captured
) 
 339             wxCommandEvent 
cevent( wxEVT_COMMAND_TOOL_CLICKED
, m_captured
->GetId() ); 
 340             cevent
.SetEventObject( this ); 
 341             // cevent.SetExtraLong((long) toggleDown); 
 342             GetEventHandler()->ProcessEvent( cevent 
);