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 wxToolLayoutItem *****/
44 IMPLEMENT_DYNAMIC_CLASS(wxToolLayoutItem
, wxObject
)
47 /***** Implementation for class wxDynToolInfo *****/
49 IMPLEMENT_DYNAMIC_CLASS(wxDynToolInfo
, wxToolLayoutItem
)
51 /***** Implementation for class wxDynamicToolBar *****/
53 wxDynamicToolBar::wxDynamicToolBar()
54 : mpLayoutMan( NULL
),
61 wxDynamicToolBar::wxDynamicToolBar(wxWindow
*parent
, const wxWindowID id
,
62 const wxPoint
& pos
, const wxSize
& size
,
63 const long style
, const int orientation
,
64 const int RowsOrColumns
, const wxString
& name
)
65 : mpLayoutMan( NULL
),
70 Create(parent
, id
, pos
, size
, style
, orientation
, RowsOrColumns
, name
);
72 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
) );
75 bool wxDynamicToolBar::Create(wxWindow
*parent
, const wxWindowID id
,
79 const int orientation
, const int RowsOrColumns
,
82 // cut&pasted from wxtbatsmpl.h
84 if ( ! wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
87 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE
));
92 bool wxDynamicToolBar::Realize(void)
98 wxDynamicToolBar::~wxDynamicToolBar(void)
104 for( i
= 0; i
!= mTools
.Count(); ++i
)
110 void wxDynamicToolBar::AddTool( int toolIndex
,
111 wxWindow
* pToolWindow
,
115 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
117 pInfo
->mpToolWnd
= pToolWindow
;
118 pInfo
->mIndex
= toolIndex
;
119 pInfo
->mIsSeparator
= FALSE
;
122 pToolWindow
->GetSize( &x
, &y
);
123 pInfo
->mRealSize
.x
= x
;
124 pInfo
->mRealSize
.y
= y
;
125 pInfo
->mRect
.width
= x
;
126 pInfo
->mRect
.height
= y
;
131 void wxDynamicToolBar::AddTool( int toolIndex
,
132 const wxString
& imageFileName
,
133 wxBitmapType imageFileType
,
134 const wxString
& labelText
, bool alignTextRight
,
137 wxNewBitmapButton
* pBtn
=
139 new wxNewBitmapButton( imageFileName
, imageFileType
,
142 ? NB_ALIGN_TEXT_RIGHT
143 : NB_ALIGN_TEXT_BOTTOM
,
147 pBtn
->Create( this, toolIndex
);
151 AddTool( toolIndex
, pBtn
);
153 void wxDynamicToolBar::AddTool( int toolIndex
, wxBitmap labelBmp
,
154 const wxString
& labelText
, bool alignTextRight
,
157 wxNewBitmapButton
* pBtn
=
159 new wxNewBitmapButton( labelBmp
,
162 ? NB_ALIGN_TEXT_RIGHT
163 : NB_ALIGN_TEXT_BOTTOM
,
167 pBtn
->Create( this, toolIndex
);
171 AddTool( toolIndex
, pBtn
);
176 wxDynamicToolBar::AddTool(const int toolIndex
, const wxBitmap
& bitmap
,
177 const wxBitmap
& pushedBitmap
,
178 const bool toggle
, const long xPos
,
179 const long yPos
, wxObject
*clientData
,
180 const wxString
& helpString1
, const wxString
& helpString2
)
182 wxNewBitmapButton
* pBmpBtn
= new wxNewBitmapButton( bitmap
);
184 pBmpBtn
->Create( this, toolIndex
);
188 AddTool( toolIndex
, pBmpBtn
);
194 wxDynToolInfo
* wxDynamicToolBar::GetToolInfo( int toolIndex
)
197 for( i
= 0; i
!= mTools
.Count(); ++i
)
199 if ( mTools
[i
]->mIndex
== toolIndex
)
206 void wxDynamicToolBar::RemveTool( int toolIndex
)
209 for( i
= 0; i
!= mTools
.Count(); ++i
)
211 if ( mTools
[i
]->mIndex
== toolIndex
)
213 if ( mTools
[i
]->mpToolWnd
)
215 mTools
[i
]->mpToolWnd
->Destroy();
217 delete mTools
[i
]; // HVL To be tested!!!
218 #if wxCHECK_VERSION(2,3,2)
228 // TODO:: if not found, should it be an assertion?
231 void wxDynamicToolBar::AddSeparator( wxWindow
* pSepartorWnd
)
233 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
235 pInfo
->mpToolWnd
= pSepartorWnd
;
237 pInfo
->mIsSeparator
= TRUE
;
239 // Do we draw a separator or is a other object?
242 // hvl => Is there a way to know if it was already created?
243 // hvl => shouldn't the pSepartorWnd be created? (like one should expect?)
244 // pSepartorWnd->Create( this, -1 );
247 pSepartorWnd
->GetSize( &x
, &y
);
248 pInfo
->mRealSize
.x
= x
;
249 pInfo
->mRealSize
.y
= y
;
251 pInfo
->mRect
.width
= x
;
252 pInfo
->mRect
.height
= y
;
256 // Init x and y to the default.
257 pInfo
->mRealSize
.x
= 0;
258 pInfo
->mRealSize
.y
= 0;
260 // Init height and width to the normal size of a separator.
261 pInfo
->mRect
.width
= mSepartorSize
;
262 pInfo
->mRect
.height
= mSepartorSize
;
268 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent
& event
)
273 void wxDynamicToolBar::OnSize( wxSizeEvent
& event
)
275 //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) );
280 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo
& info
, wxDC
& dc
)
282 // check the orientation of separator
283 if ( info
.mRect
.width
< info
.mRect
.height
)
285 int midX
= info
.mRect
.x
+ info
.mRect
.width
/2 - 1;
287 dc
.SetPen( *wxGREY_PEN
);
288 dc
.DrawLine( midX
, info
.mRect
.y
,
289 midX
, info
.mRect
.y
+ info
.mRect
.height
+1 );
291 dc
.SetPen( *wxWHITE_PEN
);
292 dc
.DrawLine( midX
+1, info
.mRect
.y
,
293 midX
+1, info
.mRect
.y
+ info
.mRect
.height
+1 );
297 int midY
= info
.mRect
.y
+ info
.mRect
.height
/2 - 1;
299 dc
.SetPen( *wxGREY_PEN
);
300 dc
.DrawLine( info
.mRect
.x
, midY
,
301 info
.mRect
.x
+ info
.mRect
.width
+1, midY
);
303 dc
.SetPen( *wxWHITE_PEN
);
304 dc
.DrawLine( info
.mRect
.x
, midY
+ 1,
305 info
.mRect
.x
+ info
.mRect
.width
+1, midY
+ 1 );
309 void wxDynamicToolBar::OnPaint( wxPaintEvent
& event
)
311 // draw separators if any
315 for( i
= 0; i
!= mTools
.Count(); ++i
)
317 if ( mTools
[i
]->mIsSeparator
)
319 // check if separator doesn't have it's own window
320 // if so, then draw it using built-in drawing method
321 if ( !mTools
[i
]->mpToolWnd
)
322 DrawSeparator( *mTools
[i
], dc
);
327 // FOR NOW:: quick fix
328 #include "wx/choice.h"
330 void wxDynamicToolBar::SizeToolWindows()
332 bool bStateCheckDone
= FALSE
;
333 bool bHorzSeparator
= FALSE
;
338 for( i
= 0; i
!= mTools
.Count(); ++i
)
340 wxDynToolInfo
& info
= *mTools
[i
];
342 if ( !info
.mIsSeparator
)
344 // center real rectangle within the rectangle
345 // provided by the layout manager
347 int x
= info
.mRect
.x
;
348 int y
= info
.mRect
.y
+ (info
.mRect
.height
- info
.mRealSize
.y
)/2;
350 // FOR NOW FOR NOW:: quick & dirty fix
351 if ( info
.mpToolWnd
->IsKindOf( CLASSINFO( wxChoice
) ) )
353 info
.mpToolWnd
->SetSize( x
, y
,
354 info
.mRealSize
.x
- 3,
359 info
.mpToolWnd
->SetSize( x
, y
,
366 // We performer this code here, so we only execute it when we have
367 // separators and we do it only once (all to do with performance...)
368 if (!bStateCheckDone
)
370 bStateCheckDone
= TRUE
;
373 wxDynToolInfo
*pInfo
= NULL
;
374 wxDynToolInfo
*pPrevInfo
= NULL
;
375 int nVertSeparators
= 0;
377 for( j
= 0; j
!= mTools
.Count(); ++j
)
381 // Count all Vert Separators.
382 if ( pInfo
->mIsSeparator
)
385 // Check if the new row starts with a Separator.
386 if ( pPrevInfo
&& pInfo
->mIsSeparator
&&
387 // pPrevInfo->mRect.x >= pInfo->mRect.x &&
388 pPrevInfo
->mRect
.y
< pInfo
->mRect
.y
)
390 // If the Separator is shown on the next row and it's
391 // the only object on the row it would mean that the
392 // Separator should be shown as Horizontal one.
393 if (j
+1 != mTools
.Count())
395 if (pInfo
->mRect
.y
< mTools
[j
+1]->mRect
.y
)
406 maxWidth
= wxMax(pInfo
->mRect
.width
, maxWidth
);
407 maxHeight
= wxMax(pInfo
->mRect
.height
, maxHeight
);
410 bHorzSeparator
= nVertSeparators
== 0;
413 // Check if we should draw Horz or Vert...
414 if ( !bHorzSeparator
)
416 info
.mRect
.width
= mSepartorSize
;
417 info
.mRect
.height
= maxHeight
;
421 info
.mRect
.width
= maxWidth
;
422 info
.mRect
.height
= mSepartorSize
;
425 // Do we need to set a new size to a seperator object?
426 if ( info
.mpToolWnd
)
428 info
.mpToolWnd
->SetSize( info
.mRect
.x
,
438 bool wxDynamicToolBar::Layout()
445 wxDynToolInfo
*pInfo
;
447 // Reset the size of separators...
448 for( i
= 0; i
!= mTools
.Count(); ++i
)
452 if ( pInfo
->mIsSeparator
)
454 pInfo
->mRect
.width
= mSepartorSize
;
455 pInfo
->mRect
.height
= mSepartorSize
;
459 // Calc and set the best layout
460 GetPreferredDim( wndDim
, result
);
466 void wxDynamicToolBar::GetPreferredDim( const wxSize
& givenDim
, wxSize
& prefDim
)
469 mpLayoutMan
= CreateDefaultLayout();
471 wxLayoutItemArrayT items
;
475 for( i
= 0; i
!= mTools
.Count(); ++i
)
476 items
.Add( mTools
[i
] );
478 mpLayoutMan
->Layout( givenDim
, prefDim
, items
, mVertGap
, mHorizGap
);
481 void wxDynamicToolBar::SetLayout( LayoutManagerBase
* pLayout
)
486 mpLayoutMan
= pLayout
;
491 void wxDynamicToolBar::EnableTool(const int toolIndex
, const bool enable
)
493 wxDynToolInfo
* pInfo
= GetToolInfo( toolIndex
);
498 if ( pInfo
->mIsSeparator
|| !pInfo
->mpToolWnd
)
501 pInfo
->mpToolWnd
->Enable( enable
);
504 /***** Implementation for class BagLayout *****/
506 void BagLayout::Layout( const wxSize
& parentDim
,
507 wxSize
& resultingDim
,
508 wxLayoutItemArrayT
& items
,
519 while( i
< items
.Count() )
525 // int firstItem = i;
531 // step #1 - arrange horizontal positions of items in the row
535 if ( itemsInRow
> 0 )
538 wxRect
& r
= items
[i
]->mRect
;
540 if ( curX
+ r
.width
> parentDim
.x
)
542 if ( itemsInRow
> 0 )
550 height
= wxMax( height
, r
.height
);
555 } while( i
< items
.Count() );
559 maxWidth
= wxMax( maxWidth
, curX
);
562 resultingDim
.x
= maxWidth
;
563 resultingDim
.y
= curY
;
566 //////// stuff from 2.1.15 ///////////
568 wxToolBarToolBase
* wxDynamicToolBar::FindToolForPosition( wxCoord x
, wxCoord y
) const
573 bool wxDynamicToolBar::DoInsertTool( size_t pos
, wxToolBarToolBase
* tool
)
578 bool wxDynamicToolBar::DoDeleteTool( size_t pos
, wxToolBarToolBase
* tool
)
583 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase
* tool
, bool enable
)
587 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase
* tool
, bool toggle
)
591 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase
* tool
, bool toggle
)
595 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( int id
, const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
, bool toggle
, wxObject
* clientData
, const wxString
& shortHelpString
, const wxString
& longHelpString
)
600 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( wxControl
* control
)