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
;
234 // Do we draw a separator or is a other object?
237 // hvl => Is there a way to know if it was already created?
238 // hvl => shouldn't the pSepartorWnd be created? (like one should expect?)
239 // pSepartorWnd->Create( this, -1 );
242 pSepartorWnd
->GetSize( &x
, &y
);
243 pInfo
->mRealSize
.x
= x
;
244 pInfo
->mRealSize
.y
= y
;
246 pInfo
->mRect
.width
= x
;
247 pInfo
->mRect
.height
= y
;
251 // Init x and y to the default.
252 pInfo
->mRealSize
.x
= 0;
253 pInfo
->mRealSize
.y
= 0;
255 // Init height and width to the normal size of a separator.
256 pInfo
->mRect
.width
= mSepartorSize
;
257 pInfo
->mRect
.height
= mSepartorSize
;
263 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent
& event
)
268 void wxDynamicToolBar::OnSize( wxSizeEvent
& event
)
270 //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) );
275 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo
& info
, wxDC
& dc
)
277 // check the orientation of separator
278 if ( info
.mRect
.width
< info
.mRect
.height
)
280 int midX
= info
.mRect
.x
+ info
.mRect
.width
/2 - 1;
282 dc
.SetPen( *wxGREY_PEN
);
283 dc
.DrawLine( midX
, info
.mRect
.y
,
284 midX
, info
.mRect
.y
+ info
.mRect
.height
+1 );
286 dc
.SetPen( *wxWHITE_PEN
);
287 dc
.DrawLine( midX
+1, info
.mRect
.y
,
288 midX
+1, info
.mRect
.y
+ info
.mRect
.height
+1 );
292 int midY
= info
.mRect
.y
+ info
.mRect
.height
/2 - 1;
294 dc
.SetPen( *wxGREY_PEN
);
295 dc
.DrawLine( info
.mRect
.x
, midY
,
296 info
.mRect
.x
+ info
.mRect
.width
+1, midY
);
298 dc
.SetPen( *wxWHITE_PEN
);
299 dc
.DrawLine( info
.mRect
.x
, midY
+ 1,
300 info
.mRect
.x
+ info
.mRect
.width
+1, midY
+ 1 );
304 void wxDynamicToolBar::OnPaint( wxPaintEvent
& event
)
306 // draw separators if any
310 for( i
= 0; i
!= mTools
.Count(); ++i
)
312 if ( mTools
[i
]->mIsSeparator
)
314 // check if separator doesn't have it's own window
315 // if so, then draw it using built-in drawing method
316 if ( !mTools
[i
]->mpToolWnd
)
317 DrawSeparator( *mTools
[i
], dc
);
322 // FOR NOW:: quick fix
323 #include "wx/choice.h"
325 void wxDynamicToolBar::SizeToolWindows()
327 bool bStateCheckDone
= FALSE
;
328 bool bHorzSeparator
= FALSE
;
333 for( i
= 0; i
!= mTools
.Count(); ++i
)
335 wxDynToolInfo
& info
= *mTools
[i
];
337 if ( !info
.mIsSeparator
)
339 // center real rectangle within the rectangle
340 // provided by the layout manager
342 int x
= info
.mRect
.x
;
343 int y
= info
.mRect
.y
+ (info
.mRect
.height
- info
.mRealSize
.y
)/2;
345 // FOR NOW FOR NOW:: quick & dirty fix
346 if ( info
.mpToolWnd
->IsKindOf( CLASSINFO( wxChoice
) ) )
348 info
.mpToolWnd
->SetSize( x
, y
,
349 info
.mRealSize
.x
- 3,
354 info
.mpToolWnd
->SetSize( x
, y
,
361 // We performer this code here, so we only execute it when we have
362 // separators and we do it only once (all to do with performance...)
363 if (!bStateCheckDone
)
365 bStateCheckDone
= TRUE
;
368 wxDynToolInfo
*pInfo
= NULL
;
369 wxDynToolInfo
*pPrevInfo
= NULL
;
370 int nVertSeparators
= 0;
372 for( j
= 0; j
!= mTools
.Count(); ++j
)
376 // Count all Vert Separators.
377 if ( pInfo
->mIsSeparator
)
380 // Check if the new row starts with a Separator.
381 if ( pPrevInfo
&& pInfo
->mIsSeparator
&&
382 // pPrevInfo->mRect.x >= pInfo->mRect.x &&
383 pPrevInfo
->mRect
.y
< pInfo
->mRect
.y
)
385 // If the Separator is shown on the next row and it's
386 // the only object on the row it would mean that the
387 // Separator should be shown as Horizontal one.
388 if (j
+1 != mTools
.Count())
390 if (pInfo
->mRect
.y
< mTools
[j
+1]->mRect
.y
)
401 maxWidth
= wxMax(pInfo
->mRect
.width
, maxWidth
);
402 maxHeight
= wxMax(pInfo
->mRect
.height
, maxHeight
);
405 bHorzSeparator
= nVertSeparators
== 0;
408 // Check if we should draw Horz or Vert...
409 if ( !bHorzSeparator
)
411 info
.mRect
.width
= mSepartorSize
;
412 info
.mRect
.height
= maxHeight
;
416 info
.mRect
.width
= maxWidth
;
417 info
.mRect
.height
= mSepartorSize
;
420 // Do we need to set a new size to a seperator object?
421 if ( info
.mpToolWnd
)
423 info
.mpToolWnd
->SetSize( info
.mRect
.x
,
433 bool wxDynamicToolBar::Layout()
440 wxDynToolInfo
*pInfo
;
442 // Reset the size of separators...
443 for( i
= 0; i
!= mTools
.Count(); ++i
)
447 if ( pInfo
->mIsSeparator
)
449 pInfo
->mRect
.width
= mSepartorSize
;
450 pInfo
->mRect
.height
= mSepartorSize
;
454 // Calc and set the best layout
455 GetPreferredDim( wndDim
, result
);
461 void wxDynamicToolBar::GetPreferredDim( const wxSize
& givenDim
, wxSize
& prefDim
)
464 mpLayoutMan
= CreateDefaultLayout();
466 wxLayoutItemArrayT items
;
470 for( i
= 0; i
!= mTools
.Count(); ++i
)
471 items
.Add( mTools
[i
] );
473 mpLayoutMan
->Layout( givenDim
, prefDim
, items
, mVertGap
, mHorizGap
);
476 void wxDynamicToolBar::SetLayout( LayoutManagerBase
* pLayout
)
481 mpLayoutMan
= pLayout
;
486 void wxDynamicToolBar::EnableTool(const int toolIndex
, const bool enable
)
488 wxDynToolInfo
* pInfo
= GetToolInfo( toolIndex
);
493 if ( pInfo
->mIsSeparator
|| !pInfo
->mpToolWnd
)
496 pInfo
->mpToolWnd
->Enable( enable
);
499 /***** Implementation for class BagLayout *****/
501 void BagLayout::Layout( const wxSize
& parentDim
,
502 wxSize
& resultingDim
,
503 wxLayoutItemArrayT
& items
,
514 while( i
< items
.Count() )
520 // int firstItem = i;
526 // step #1 - arrange horizontal positions of items in the row
530 if ( itemsInRow
> 0 )
533 wxRect
& r
= items
[i
]->mRect
;
535 if ( curX
+ r
.width
> parentDim
.x
)
537 if ( itemsInRow
> 0 )
545 height
= wxMax( height
, r
.height
);
550 } while( i
< items
.Count() );
554 maxWidth
= wxMax( maxWidth
, curX
);
557 resultingDim
.x
= maxWidth
;
558 resultingDim
.y
= curY
;
561 //////// stuff from 2.1.15 ///////////
563 wxToolBarToolBase
* wxDynamicToolBar::FindToolForPosition( wxCoord x
, wxCoord y
) const
568 bool wxDynamicToolBar::DoInsertTool( size_t pos
, wxToolBarToolBase
* tool
)
573 bool wxDynamicToolBar::DoDeleteTool( size_t pos
, wxToolBarToolBase
* tool
)
578 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase
* tool
, bool enable
)
582 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase
* tool
, bool toggle
)
586 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase
* tool
, bool toggle
)
590 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( int id
, const wxBitmap
& bitmap1
, const wxBitmap
& bitmap2
, bool toggle
, wxObject
* clientData
, const wxString
& shortHelpString
, const wxString
& longHelpString
)
595 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( wxControl
* control
)