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
, wxObject
)
34 BEGIN_EVENT_TABLE( wxDynamicToolBar
, wxToolBarBase
)
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 WXUNUSED(orientation
), const int WXUNUSED(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
,
112 const wxSize
& WXUNUSED(size
)
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
& WXUNUSED(pushedBitmap
),
178 const bool WXUNUSED(toggle
), const long WXUNUSED(xPos
),
179 const long WXUNUSED(yPos
), wxObject
*WXUNUSED(clientData
),
180 const wxString
& helpString1
, const wxString
& WXUNUSED(helpString2
))
182 wxNewBitmapButton
* pBmpBtn
= new wxNewBitmapButton( bitmap
);
184 pBmpBtn
->Create( this, toolIndex
);
189 pBmpBtn
->SetToolTip( helpString1
);
191 wxUnusedVar( helpString1
);
192 #endif // wxUSE_TOOLTIPS
194 AddTool( toolIndex
, pBmpBtn
);
200 wxDynToolInfo
* wxDynamicToolBar::GetToolInfo( int toolIndex
)
203 for( i
= 0; i
!= mTools
.Count(); ++i
)
205 if ( mTools
[i
]->mIndex
== toolIndex
)
212 void wxDynamicToolBar::RemveTool( int toolIndex
)
215 for( i
= 0; i
!= mTools
.Count(); ++i
)
217 if ( mTools
[i
]->mIndex
== toolIndex
)
219 if ( mTools
[i
]->mpToolWnd
)
221 mTools
[i
]->mpToolWnd
->Destroy();
223 delete mTools
[i
]; // HVL To be tested!!!
224 #if wxCHECK_VERSION(2,3,2)
234 // TODO:: if not found, should it be an assertion?
237 void wxDynamicToolBar::AddSeparator( wxWindow
* pSepartorWnd
)
239 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
241 pInfo
->mpToolWnd
= pSepartorWnd
;
243 pInfo
->mIsSeparator
= true;
245 // Do we draw a separator or is a other object?
248 // hvl => Is there a way to know if it was already created?
249 // hvl => shouldn't the pSepartorWnd be created? (like one should expect?)
250 // pSepartorWnd->Create( this, -1 );
253 pSepartorWnd
->GetSize( &x
, &y
);
254 pInfo
->mRealSize
.x
= x
;
255 pInfo
->mRealSize
.y
= y
;
257 pInfo
->mRect
.width
= x
;
258 pInfo
->mRect
.height
= y
;
262 // Init x and y to the default.
263 pInfo
->mRealSize
.x
= 0;
264 pInfo
->mRealSize
.y
= 0;
266 // Init height and width to the normal size of a separator.
267 pInfo
->mRect
.width
= mSepartorSize
;
268 pInfo
->mRect
.height
= mSepartorSize
;
274 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent
& WXUNUSED(event
) )
279 void wxDynamicToolBar::OnSize( wxSizeEvent
& WXUNUSED(event
) )
281 //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) );
286 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo
& info
, wxDC
& dc
)
288 // check the orientation of separator
289 if ( info
.mRect
.width
< info
.mRect
.height
)
291 int midX
= info
.mRect
.x
+ info
.mRect
.width
/2 - 1;
293 dc
.SetPen( *wxGREY_PEN
);
294 dc
.DrawLine( midX
, info
.mRect
.y
,
295 midX
, info
.mRect
.y
+ info
.mRect
.height
+1 );
297 dc
.SetPen( *wxWHITE_PEN
);
298 dc
.DrawLine( midX
+1, info
.mRect
.y
,
299 midX
+1, info
.mRect
.y
+ info
.mRect
.height
+1 );
303 int midY
= info
.mRect
.y
+ info
.mRect
.height
/2 - 1;
305 dc
.SetPen( *wxGREY_PEN
);
306 dc
.DrawLine( info
.mRect
.x
, midY
,
307 info
.mRect
.x
+ info
.mRect
.width
+1, midY
);
309 dc
.SetPen( *wxWHITE_PEN
);
310 dc
.DrawLine( info
.mRect
.x
, midY
+ 1,
311 info
.mRect
.x
+ info
.mRect
.width
+1, midY
+ 1 );
315 void wxDynamicToolBar::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
317 // draw separators if any
321 for( i
= 0; i
!= mTools
.Count(); ++i
)
323 if ( mTools
[i
]->mIsSeparator
)
325 // check if separator doesn't have it's own window
326 // if so, then draw it using built-in drawing method
327 if ( !mTools
[i
]->mpToolWnd
)
328 DrawSeparator( *mTools
[i
], dc
);
333 // FOR NOW:: quick fix
334 #include "wx/choice.h"
336 void wxDynamicToolBar::SizeToolWindows()
338 bool bStateCheckDone
= false;
339 bool bHorzSeparator
= false;
344 for( i
= 0; i
!= mTools
.Count(); ++i
)
346 wxDynToolInfo
& info
= *mTools
[i
];
348 if ( !info
.mIsSeparator
)
350 // center real rectangle within the rectangle
351 // provided by the layout manager
353 int x
= info
.mRect
.x
;
354 int y
= info
.mRect
.y
+ (info
.mRect
.height
- info
.mRealSize
.y
)/2;
356 // FOR NOW FOR NOW:: quick & dirty fix
357 if ( info
.mpToolWnd
->IsKindOf( CLASSINFO( wxChoice
) ) )
359 info
.mpToolWnd
->SetSize( x
, y
,
360 info
.mRealSize
.x
- 3,
365 info
.mpToolWnd
->SetSize( x
, y
,
372 // We performer this code here, so we only execute it when we have
373 // separators and we do it only once (all to do with performance...)
374 if (!bStateCheckDone
)
376 bStateCheckDone
= true;
379 wxDynToolInfo
*pInfo
;
380 wxDynToolInfo
*pPrevInfo
= NULL
;
381 int nVertSeparators
= 0;
383 for( j
= 0; j
!= mTools
.Count(); ++j
)
387 // Count all Vert Separators.
388 if ( pInfo
->mIsSeparator
)
391 // Check if the new row starts with a Separator.
392 if ( pPrevInfo
&& pInfo
->mIsSeparator
&&
393 // pPrevInfo->mRect.x >= pInfo->mRect.x &&
394 pPrevInfo
->mRect
.y
< pInfo
->mRect
.y
)
396 // If the Separator is shown on the next row and it's
397 // the only object on the row it would mean that the
398 // Separator should be shown as Horizontal one.
399 if (j
+1 != mTools
.Count())
401 if (pInfo
->mRect
.y
< mTools
[j
+1]->mRect
.y
)
412 maxWidth
= wxMax(pInfo
->mRect
.width
, maxWidth
);
413 maxHeight
= wxMax(pInfo
->mRect
.height
, maxHeight
);
416 bHorzSeparator
= nVertSeparators
== 0;
419 // Check if we should draw Horz or Vert...
420 if ( !bHorzSeparator
)
422 info
.mRect
.width
= mSepartorSize
;
423 info
.mRect
.height
= maxHeight
;
427 info
.mRect
.width
= maxWidth
;
428 info
.mRect
.height
= mSepartorSize
;
431 // Do we need to set a new size to a separator object?
432 if ( info
.mpToolWnd
)
434 info
.mpToolWnd
->SetSize( info
.mRect
.x
,
444 bool wxDynamicToolBar::Layout()
451 wxDynToolInfo
*pInfo
;
453 // Reset the size of separators...
454 for( i
= 0; i
!= mTools
.Count(); ++i
)
458 if ( pInfo
->mIsSeparator
)
460 pInfo
->mRect
.width
= mSepartorSize
;
461 pInfo
->mRect
.height
= mSepartorSize
;
465 // Calc and set the best layout
466 GetPreferredDim( wndDim
, result
);
472 void wxDynamicToolBar::GetPreferredDim( const wxSize
& givenDim
, wxSize
& prefDim
)
475 mpLayoutMan
= CreateDefaultLayout();
477 wxLayoutItemArrayT items
;
481 for( i
= 0; i
!= mTools
.Count(); ++i
)
482 items
.Add( mTools
[i
] );
484 mpLayoutMan
->Layout( givenDim
, prefDim
, items
, mVertGap
, mHorizGap
);
487 void wxDynamicToolBar::SetLayout( LayoutManagerBase
* pLayout
)
492 mpLayoutMan
= pLayout
;
497 void wxDynamicToolBar::EnableTool(int toolIndex
, bool enable
)
499 wxDynToolInfo
* pInfo
= GetToolInfo( toolIndex
);
504 if ( pInfo
->mIsSeparator
|| !pInfo
->mpToolWnd
)
507 pInfo
->mpToolWnd
->Enable( enable
);
510 /***** Implementation for class BagLayout *****/
512 void BagLayout::Layout( const wxSize
& parentDim
,
513 wxSize
& resultingDim
,
514 wxLayoutItemArrayT
& items
,
525 while( i
< items
.Count() )
531 // int firstItem = i;
537 // step #1 - arrange horizontal positions of items in the row
541 if ( itemsInRow
> 0 )
544 wxRect
& r
= items
[i
]->mRect
;
546 if ( curX
+ r
.width
> parentDim
.x
)
548 if ( itemsInRow
> 0 )
556 height
= wxMax( height
, r
.height
);
561 } while( i
< items
.Count() );
565 maxWidth
= wxMax( maxWidth
, curX
);
568 resultingDim
.x
= maxWidth
;
569 resultingDim
.y
= curY
;
572 //////// stuff from 2.1.15 ///////////
574 wxToolBarToolBase
* wxDynamicToolBar::FindToolForPosition( wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
) ) const
579 bool wxDynamicToolBar::DoInsertTool( size_t WXUNUSED(pos
), wxToolBarToolBase
* WXUNUSED(tool
) )
584 bool wxDynamicToolBar::DoDeleteTool( size_t WXUNUSED(pos
), wxToolBarToolBase
* WXUNUSED(tool
) )
589 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase
* WXUNUSED(tool
), bool WXUNUSED(enable
) )
593 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase
* WXUNUSED(tool
), bool WXUNUSED(toggle
) )
597 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase
* WXUNUSED(tool
), bool WXUNUSED(toggle
) )
601 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( int WXUNUSED(id
),
602 const wxString
& WXUNUSED(label
),
603 const wxBitmap
& WXUNUSED(bmpNormal
),
604 const wxBitmap
& WXUNUSED(bmpDisabled
),
605 wxItemKind
WXUNUSED(kind
),
606 wxObject
*WXUNUSED(clientData
),
607 const wxString
& WXUNUSED(shortHelp
),
608 const wxString
& WXUNUSED(longHelp
)
614 wxToolBarToolBase
* wxDynamicToolBar::CreateTool( wxControl
* WXUNUSED(control
) )