1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDynamicToolBar implementation
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dyntbar.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/utils.h" // import wxMin,wxMax macros
29 #include "wx/fl/dyntbar.h"
30 #include "wx/fl/newbmpbtn.h"
32 IMPLEMENT_DYNAMIC_CLASS(wxDynamicToolBar
, wxControl
)
34 BEGIN_EVENT_TABLE( wxDynamicToolBar
, wxControl
)
36 EVT_SIZE ( wxDynamicToolBar::OnSize
)
37 EVT_PAINT( wxDynamicToolBar::OnPaint
)
38 //EVT_ERASE_BACKGROUND( wxDynamicToolBar::OnEraseBackground )
42 /***** Implementation for class wxDynToolInfo *****/
44 IMPLEMENT_DYNAMIC_CLASS(wxDynToolInfo
, wxToolLayoutItem
)
46 /***** Implementation for class wxDynamicToolBar *****/
48 wxDynamicToolBar::wxDynamicToolBar()
49 : mpLayoutMan( NULL
),
56 wxDynamicToolBar::wxDynamicToolBar(wxWindow
*parent
, const wxWindowID id
,
57 const wxPoint
& pos
, const wxSize
& size
,
58 const long style
, const int orientation
,
59 const int RowsOrColumns
, const wxString
& name
)
60 : mpLayoutMan( NULL
),
65 Create(parent
, id
, pos
, size
, style
, orientation
, RowsOrColumns
, name
);
67 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
) );
70 bool wxDynamicToolBar::Create(wxWindow
*parent
, const wxWindowID id
,
74 const int orientation
, const int RowsOrColumns
,
77 // cut&pasted from wxtbatsmpl.h
79 if ( ! wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
82 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
87 bool wxDynamicToolBar::Realize(void)
93 wxDynamicToolBar::~wxDynamicToolBar(void)
99 for( i
= 0; i
!= mTools
.Count(); ++i
)
105 void wxDynamicToolBar::AddTool( int toolIndex
,
106 wxWindow
* pToolWindow
,
110 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
112 pInfo
->mpToolWnd
= pToolWindow
;
113 pInfo
->mIndex
= toolIndex
;
114 pInfo
->mIsSeparator
= FALSE
;
117 pToolWindow
->GetSize( &x
, &y
);
118 pInfo
->mRealSize
.x
= x
;
119 pInfo
->mRealSize
.y
= y
;
120 pInfo
->mRect
.width
= x
;
121 pInfo
->mRect
.height
= y
;
126 void wxDynamicToolBar::AddTool( int toolIndex
,
127 const wxString
& imageFileName
,
128 wxBitmapType imageFileType
,
129 const wxString
& labelText
, bool alignTextRight
,
132 wxNewBitmapButton
* pBtn
=
134 new wxNewBitmapButton( imageFileName
, imageFileType
,
137 ? NB_ALIGN_TEXT_RIGHT
138 : NB_ALIGN_TEXT_BOTTOM
,
142 pBtn
->Create( this, toolIndex
);
146 AddTool( toolIndex
, pBtn
);
148 void wxDynamicToolBar::AddTool( int toolIndex
, wxBitmap labelBmp
,
149 const wxString
& labelText
, bool alignTextRight
,
152 wxNewBitmapButton
* pBtn
=
154 new wxNewBitmapButton( labelBmp
,
157 ? NB_ALIGN_TEXT_RIGHT
158 : NB_ALIGN_TEXT_BOTTOM
,
162 pBtn
->Create( this, toolIndex
);
166 AddTool( toolIndex
, pBtn
);
171 wxDynamicToolBar::AddTool(const int toolIndex
, const wxBitmap
& bitmap
,
172 const wxBitmap
& pushedBitmap
,
173 const bool toggle
, const long xPos
,
174 const long yPos
, wxObject
*clientData
,
175 const wxString
& helpString1
, const wxString
& helpString2
)
177 wxNewBitmapButton
* pBmpBtn
= new wxNewBitmapButton( bitmap
);
179 pBmpBtn
->Create( this, toolIndex
);
183 AddTool( toolIndex
, pBmpBtn
);
189 wxDynToolInfo
* wxDynamicToolBar::GetToolInfo( int toolIndex
)
192 for( i
= 0; i
!= mTools
.Count(); ++i
)
194 if ( mTools
[i
]->mIndex
== toolIndex
)
201 void wxDynamicToolBar::RemveTool( int toolIndex
)
204 for( i
= 0; i
!= mTools
.Count(); ++i
)
206 if ( mTools
[i
]->mIndex
== toolIndex
)
208 if ( mTools
[i
]->mpToolWnd
)
210 mTools
[i
]->mpToolWnd
->Destroy();
212 delete mTools
[i
]; // HVL To be tested!!!
213 #if wxCHECK_VERSION(2,3,2)
223 // TODO:: if not found, should it be an assertion?
226 void wxDynamicToolBar::AddSeparator( wxWindow
* pSepartorWnd
)
228 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
230 pInfo
->mpToolWnd
= pSepartorWnd
;
232 pInfo
->mIsSeparator
= TRUE
;
236 pSepartorWnd
->Create( this, -1 );
239 pSepartorWnd
->GetSize( &x
, &y
);
240 pInfo
->mRealSize
.x
= x
;
241 pInfo
->mRealSize
.y
= y
;
243 pInfo
->mRect
.width
= x
;
244 pInfo
->mRect
.height
= y
;
248 pInfo
->mRealSize
.x
= mSepartorSize
;
249 pInfo
->mRealSize
.y
= 0;
251 pInfo
->mRect
.width
= mSepartorSize
;
252 pInfo
->mRect
.height
= 0;
258 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent
& event
)
263 void wxDynamicToolBar::OnSize( wxSizeEvent
& event
)
265 //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) );
270 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo
& info
, wxDC
& dc
)
272 // check the orientation of separator
273 if ( info
.mRect
.width
< info
.mRect
.height
)
275 int midX
= info
.mRect
.x
+ info
.mRect
.width
/2 - 1;
277 dc
.SetPen( *wxGREY_PEN
);
278 dc
.DrawLine( midX
, info
.mRect
.y
,
279 midX
, info
.mRect
.y
+ info
.mRect
.height
+1 );
281 dc
.SetPen( *wxWHITE_PEN
);
282 dc
.DrawLine( midX
+1, info
.mRect
.y
,
283 midX
+1, info
.mRect
.y
+ info
.mRect
.height
+1 );
287 int midY
= info
.mRect
.y
+ info
.mRect
.height
/2 - 1;
289 dc
.SetPen( *wxGREY_PEN
);
290 dc
.DrawLine( info
.mRect
.x
, midY
,
291 info
.mRect
.x
+ info
.mRect
.width
+1, midY
);
293 dc
.SetPen( *wxWHITE_PEN
);
294 dc
.DrawLine( info
.mRect
.x
, midY
+ 1,
295 info
.mRect
.x
+ info
.mRect
.width
+1, midY
+ 1 );
299 void wxDynamicToolBar::OnPaint( wxPaintEvent
& event
)
301 // draw separators if any
306 for( i
= 0; i
!= mTools
.Count(); ++i
)
308 if ( mTools
[i
]->mIsSeparator
)
310 // check if separator doesn't have it's own window
311 // if so, then draw it using built-in drawing method
313 if ( !mTools
[i
]->mpToolWnd
)
315 DrawSeparator( *mTools
[i
], dc
);
319 // FOR NOW:: quick fix
320 #include "wx/choice.h"
322 void wxDynamicToolBar::SizeToolWindows()
325 for( i
= 0; i
!= mTools
.Count(); ++i
)
327 wxDynToolInfo
& info
= *mTools
[i
];
329 if ( !info
.mIsSeparator
)
332 // center real rectangle within the rectangle
333 // provided by the layout manager
335 int x
= info
.mRect
.x
;
336 int y
= info
.mRect
.y
+ (info
.mRect
.height
- info
.mRealSize
.y
)/2;
338 // FOR NOW FOR NOW:: quick & dirty fix
339 if ( info
.mpToolWnd
->IsKindOf( CLASSINFO( wxChoice
) ) )
341 info
.mpToolWnd
->SetSize( x
,y
,
342 info
.mRealSize
.x
- 3,
346 info
.mpToolWnd
->SetSize( x
,y
,
351 // TBD:: size separator window if present
355 bool wxDynamicToolBar::Layout()
358 mpLayoutMan
= CreateDefaultLayout();
365 wxLayoutItemArrayT items
;
369 for( i
= 0; i
!= mTools
.Count(); ++i
)
370 items
.Add( mTools
[i
] );
372 mpLayoutMan
->Layout( wndDim
, result
, items
, mVertGap
, mHorizGap
);;
378 void wxDynamicToolBar::GetPreferredDim( const wxSize
& givenDim
, wxSize
& prefDim
)
381 mpLayoutMan
= CreateDefaultLayout();
383 wxLayoutItemArrayT items
;
387 for( i
= 0; i
!= mTools
.Count(); ++i
)
388 items
.Add( mTools
[i
] );
390 mpLayoutMan
->Layout( givenDim
, prefDim
, items
, mVertGap
, mHorizGap
);;
393 void wxDynamicToolBar::SetLayout( LayoutManagerBase
* pLayout
)
398 mpLayoutMan
= pLayout
;
403 void wxDynamicToolBar::EnableTool(const int toolIndex
, const bool enable
)
405 wxDynToolInfo
* pInfo
= GetToolInfo( toolIndex
);
410 if ( pInfo
->mIsSeparator
|| !pInfo
->mpToolWnd
)
413 pInfo
->mpToolWnd
->Enable( enable
);
416 /***** Implementation for class BagLayout *****/
418 void BagLayout::Layout( const wxSize
& parentDim
,
419 wxSize
& resultingDim
,
420 wxLayoutItemArrayT
& items
,
431 while( i
< items
.Count() )
437 // int firstItem = i;
443 // step #1 - arrange horizontal positions of items in the row
447 if ( itemsInRow
> 0 )
450 wxRect
& r
= items
[i
]->mRect
;
452 if ( curX
+ r
.width
> parentDim
.x
)
454 if ( itemsInRow
> 0 )
462 height
= wxMax( height
, r
.height
);
467 } while( i
< items
.Count() );
471 maxWidth
= wxMax( maxWidth
, curX
);
474 resultingDim
.x
= maxWidth
;
475 resultingDim
.y
= curY
;
478 //////// stuff from 2.1.15 ///////////
480 wxToolBarToolBase
* wxDynamicToolBar::FindToolForPosition( wxCoord x
, wxCoord y
) const
485 bool wxDynamicToolBar::DoInsertTool( size_t pos
, wxToolBarToolBase
* tool
)
490 bool wxDynamicToolBar::DoDeleteTool( size_t pos
, wxToolBarToolBase
* tool
)
495 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase
* tool
, bool enable
)
499 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase
* tool
, bool toggle
)
503 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase
* tool
, bool toggle
)
507 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( int id
, const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
, bool toggle
, wxObject
* clientData
, const wxString
& shortHelpString
, const wxString
& longHelpString
)
512 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( wxControl
* control
)