1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
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"
28 #include "wx/utils.h" // import wxMin,wxMax macros
30 #include "wx/fl/dyntbar.h"
31 #include "wx/fl/newbmpbtn.h"
33 IMPLEMENT_DYNAMIC_CLASS(wxDynamicToolBar
, wxControl
)
35 BEGIN_EVENT_TABLE( wxDynamicToolBar
, wxControl
)
37 EVT_SIZE ( wxDynamicToolBar::OnSize
)
38 EVT_PAINT( wxDynamicToolBar::OnPaint
)
39 //EVT_ERASE_BACKGROUND( wxDynamicToolBar::OnEraseBackground )
43 /***** Implementation for class wxDynToolInfo *****/
45 IMPLEMENT_DYNAMIC_CLASS(wxDynToolInfo
, wxToolLayoutItem
)
47 /***** Implementation for class wxDynamicToolBar *****/
49 wxDynamicToolBar::wxDynamicToolBar()
50 : mpLayoutMan( NULL
),
57 wxDynamicToolBar::wxDynamicToolBar(wxWindow
*parent
, const wxWindowID id
,
58 const wxPoint
& pos
, const wxSize
& size
,
59 const long style
, const int orientation
,
60 const int RowsOrColumns
, const wxString
& name
)
61 : mpLayoutMan( NULL
),
66 Create(parent
, id
, pos
, size
, style
, orientation
, RowsOrColumns
, name
);
68 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE
) );
71 bool wxDynamicToolBar::Create(wxWindow
*parent
, const wxWindowID id
,
75 const int orientation
, const int RowsOrColumns
,
78 // cut&pasted from wxtbatsmpl.h
80 if ( ! wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
83 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE
));
88 bool wxDynamicToolBar::Realize(void)
94 wxDynamicToolBar::~wxDynamicToolBar(void)
100 for( i
= 0; i
!= mTools
.Count(); ++i
)
106 void wxDynamicToolBar::AddTool( int toolIndex
,
107 wxWindow
* pToolWindow
,
111 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
113 pInfo
->mpToolWnd
= pToolWindow
;
114 pInfo
->mIndex
= toolIndex
;
115 pInfo
->mIsSeparator
= FALSE
;
118 pToolWindow
->GetSize( &x
, &y
);
119 pInfo
->mRealSize
.x
= x
;
120 pInfo
->mRealSize
.y
= y
;
121 pInfo
->mRect
.width
= x
;
122 pInfo
->mRect
.height
= y
;
127 void wxDynamicToolBar::AddTool( int toolIndex
,
128 const wxString
& imageFileName
,
129 wxBitmapType imageFileType
,
130 const wxString
& labelText
, bool alignTextRight
,
133 wxNewBitmapButton
* pBtn
=
135 new wxNewBitmapButton( imageFileName
, imageFileType
,
138 ? NB_ALIGN_TEXT_RIGHT
139 : NB_ALIGN_TEXT_BOTTOM
,
143 pBtn
->Create( this, toolIndex
);
147 AddTool( toolIndex
, pBtn
);
149 void wxDynamicToolBar::AddTool( int toolIndex
, wxBitmap labelBmp
,
150 const wxString
& labelText
, bool alignTextRight
,
153 wxNewBitmapButton
* pBtn
=
155 new wxNewBitmapButton( labelBmp
,
158 ? NB_ALIGN_TEXT_RIGHT
159 : NB_ALIGN_TEXT_BOTTOM
,
163 pBtn
->Create( this, toolIndex
);
167 AddTool( toolIndex
, pBtn
);
172 wxDynamicToolBar::AddTool(const int toolIndex
, const wxBitmap
& bitmap
,
173 const wxBitmap
& pushedBitmap
,
174 const bool toggle
, const long xPos
,
175 const long yPos
, wxObject
*clientData
,
176 const wxString
& helpString1
, const wxString
& helpString2
)
178 wxNewBitmapButton
* pBmpBtn
= new wxNewBitmapButton( bitmap
);
180 pBmpBtn
->Create( this, toolIndex
);
184 AddTool( toolIndex
, pBmpBtn
);
190 wxDynToolInfo
* wxDynamicToolBar::GetToolInfo( int toolIndex
)
193 for( i
= 0; i
!= mTools
.Count(); ++i
)
195 if ( mTools
[i
]->mIndex
== toolIndex
)
202 void wxDynamicToolBar::RemveTool( int toolIndex
)
205 for( i
= 0; i
!= mTools
.Count(); ++i
)
207 if ( mTools
[i
]->mIndex
== toolIndex
)
209 if ( mTools
[i
]->mpToolWnd
)
211 mTools
[i
]->mpToolWnd
->Destroy();
213 delete mTools
[i
]; // HVL To be tested!!!
214 #if wxCHECK_VERSION(2,3,2)
224 // TODO:: if not found, should it be an assertion?
227 void wxDynamicToolBar::AddSeparator( wxWindow
* pSepartorWnd
)
229 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
231 pInfo
->mpToolWnd
= pSepartorWnd
;
233 pInfo
->mIsSeparator
= TRUE
;
237 pSepartorWnd
->Create( this, -1 );
240 pSepartorWnd
->GetSize( &x
, &y
);
241 pInfo
->mRealSize
.x
= x
;
242 pInfo
->mRealSize
.y
= y
;
244 pInfo
->mRect
.width
= x
;
245 pInfo
->mRect
.height
= y
;
249 pInfo
->mRealSize
.x
= mSepartorSize
;
250 pInfo
->mRealSize
.y
= 0;
252 pInfo
->mRect
.width
= mSepartorSize
;
253 pInfo
->mRect
.height
= 0;
259 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent
& event
)
264 void wxDynamicToolBar::OnSize( wxSizeEvent
& event
)
266 //SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE ) );
271 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo
& info
, wxDC
& dc
)
273 // check the orientation of separator
274 if ( info
.mRect
.width
< info
.mRect
.height
)
276 int midX
= info
.mRect
.x
+ info
.mRect
.width
/2 - 1;
278 dc
.SetPen( *wxGREY_PEN
);
279 dc
.DrawLine( midX
, info
.mRect
.y
,
280 midX
, info
.mRect
.y
+ info
.mRect
.height
+1 );
282 dc
.SetPen( *wxWHITE_PEN
);
283 dc
.DrawLine( midX
+1, info
.mRect
.y
,
284 midX
+1, info
.mRect
.y
+ info
.mRect
.height
+1 );
288 int midY
= info
.mRect
.y
+ info
.mRect
.height
/2 - 1;
290 dc
.SetPen( *wxGREY_PEN
);
291 dc
.DrawLine( info
.mRect
.x
, midY
,
292 info
.mRect
.x
+ info
.mRect
.width
+1, midY
);
294 dc
.SetPen( *wxWHITE_PEN
);
295 dc
.DrawLine( info
.mRect
.x
, midY
+ 1,
296 info
.mRect
.x
+ info
.mRect
.width
+1, midY
+ 1 );
300 void wxDynamicToolBar::OnPaint( wxPaintEvent
& event
)
302 // draw separators if any
307 for( i
= 0; i
!= mTools
.Count(); ++i
)
309 if ( mTools
[i
]->mIsSeparator
)
311 // check if separator doesn't have it's own window
312 // if so, then draw it using built-in drawing method
314 if ( !mTools
[i
]->mpToolWnd
)
316 DrawSeparator( *mTools
[i
], dc
);
320 // FOR NOW:: quick fix
321 #include "wx/choice.h"
323 void wxDynamicToolBar::SizeToolWindows()
326 for( i
= 0; i
!= mTools
.Count(); ++i
)
328 wxDynToolInfo
& info
= *mTools
[i
];
330 if ( !info
.mIsSeparator
)
333 // center real rectangle within the rectangle
334 // provided by the layout manager
336 int x
= info
.mRect
.x
;
337 int y
= info
.mRect
.y
+ (info
.mRect
.height
- info
.mRealSize
.y
)/2;
339 // FOR NOW FOR NOW:: quick & dirty fix
340 if ( info
.mpToolWnd
->IsKindOf( CLASSINFO( wxChoice
) ) )
342 info
.mpToolWnd
->SetSize( x
,y
,
343 info
.mRealSize
.x
- 3,
347 info
.mpToolWnd
->SetSize( x
,y
,
352 // TBD:: size separator window if present
356 bool wxDynamicToolBar::Layout()
359 mpLayoutMan
= CreateDefaulLayout();
366 wxLayoutItemArrayT items
;
370 for( i
= 0; i
!= mTools
.Count(); ++i
)
371 items
.Add( mTools
[i
] );
373 mpLayoutMan
->Layout( wndDim
, result
, items
, mVertGap
, mHorizGap
);;
379 void wxDynamicToolBar::GetPreferredDim( const wxSize
& givenDim
, wxSize
& prefDim
)
382 mpLayoutMan
= CreateDefaulLayout();
384 wxLayoutItemArrayT items
;
388 for( i
= 0; i
!= mTools
.Count(); ++i
)
389 items
.Add( mTools
[i
] );
391 mpLayoutMan
->Layout( givenDim
, prefDim
, items
, mVertGap
, mHorizGap
);;
394 void wxDynamicToolBar::SetLayout( LayoutManagerBase
* pLayout
)
399 mpLayoutMan
= pLayout
;
404 void wxDynamicToolBar::EnableTool(const int toolIndex
, const bool enable
)
406 wxDynToolInfo
* pInfo
= GetToolInfo( toolIndex
);
411 if ( pInfo
->mIsSeparator
|| !pInfo
->mpToolWnd
)
414 pInfo
->mpToolWnd
->Enable( enable
);
417 /***** Implementation for class BagLayout *****/
419 void BagLayout::Layout( const wxSize
& parentDim
,
420 wxSize
& resultingDim
,
421 wxLayoutItemArrayT
& items
,
432 while( i
< items
.Count() )
438 // int firstItem = i;
444 // step #1 - arrange horizontal positions of items in the row
448 if ( itemsInRow
> 0 )
451 wxRect
& r
= items
[i
]->mRect
;
453 if ( curX
+ r
.width
> parentDim
.x
)
455 if ( itemsInRow
> 0 )
463 height
= wxMax( height
, r
.height
);
468 } while( i
< items
.Count() );
472 maxWidth
= wxMax( maxWidth
, curX
);
475 resultingDim
.x
= maxWidth
;
476 resultingDim
.y
= curY
;
479 //////// stuff from 2.1.15 ///////////
481 wxToolBarToolBase
* wxDynamicToolBar::FindToolForPosition( wxCoord x
, wxCoord y
) const
486 bool wxDynamicToolBar::DoInsertTool( size_t pos
, wxToolBarToolBase
* tool
)
491 bool wxDynamicToolBar::DoDeleteTool( size_t pos
, wxToolBarToolBase
* tool
)
496 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase
* tool
, bool enable
)
500 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase
* tool
, bool toggle
)
504 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase
* tool
, bool toggle
)
508 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( int id
, const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
, bool toggle
, wxObject
* clientData
, const wxString
& shortHelpString
, const wxString
& longHelpString
)
513 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( wxControl
* control
)