]>
git.saurik.com Git - wxWidgets.git/blob - utils/framelayout/src/dyntbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Contrib. demo
4 // Author: Aleksandras Gluchovas
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dyntbar.cpp"
14 #pragma interface "dyntbar.cpp"
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
30 #include "wx/utils.h" // import wxMin,wxMax macros
33 #include "newbmpbtn.h"
35 IMPLEMENT_DYNAMIC_CLASS(wxDynamicToolBar
, wxToolBarBase
)
37 BEGIN_EVENT_TABLE( wxDynamicToolBar
, wxToolBarBase
)
39 EVT_SIZE ( wxDynamicToolBar::OnSize
)
40 EVT_PAINT( wxDynamicToolBar::OnPaint
)
41 //EVT_ERASE_BACKGROUND( wxDynamicToolBar::OnEraseBackground )
45 /***** Implementation for class wxDynToolInfo *****/
47 IMPLEMENT_DYNAMIC_CLASS(wxDynToolInfo
, wxToolLayoutItem
)
49 /***** Implementation for class wxDynamicToolBar *****/
51 wxDynamicToolBar::wxDynamicToolBar()
52 : mpLayoutMan( NULL
),
59 wxDynamicToolBar::wxDynamicToolBar(wxWindow
*parent
, const wxWindowID id
,
60 const wxPoint
& pos
, const wxSize
& size
,
61 const long style
, const int orientation
,
62 const int RowsOrColumns
, const wxString
& name
)
63 : mpLayoutMan( NULL
),
68 Create(parent
, id
, pos
, size
, style
, orientation
, RowsOrColumns
, name
);
70 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE
) );
73 bool wxDynamicToolBar::Create(wxWindow
*parent
, const wxWindowID id
,
77 const int orientation
, const int RowsOrColumns
,
80 // cut&pasted from wxtbatsmpl.h
82 if ( ! wxWindow::Create(parent
, id
, pos
, size
, style
, name
) )
85 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE
));
90 bool wxDynamicToolBar::Realize(void)
96 wxDynamicToolBar::~wxDynamicToolBar(void)
98 if ( mpLayoutMan
) delete mpLayoutMan
;
100 for( size_t 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
,
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
)
191 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
193 if ( mTools
[i
]->mIndex
== toolIndex
) return mTools
[i
];
198 void wxDynamicToolBar::RemveTool( int toolIndex
)
200 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
202 if ( mTools
[i
]->mIndex
== toolIndex
)
204 if ( mTools
[i
]->mpToolWnd
)
206 mTools
[i
]->mpToolWnd
->Destroy();
215 // TODO:: if not found, should it be an assertion?
218 void wxDynamicToolBar::AddSeparator( wxWindow
* pSepartorWnd
)
220 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
222 pInfo
->mpToolWnd
= pSepartorWnd
;
224 pInfo
->mIsSeparator
= TRUE
;
228 pSepartorWnd
->Create( this, -1 );
231 pSepartorWnd
->GetSize( &x
, &y
);
232 pInfo
->mRealSize
.x
= x
;
233 pInfo
->mRealSize
.y
= y
;
235 pInfo
->mRect
.width
= x
;
236 pInfo
->mRect
.height
= y
;
240 pInfo
->mRealSize
.x
= mSepartorSize
;
241 pInfo
->mRealSize
.y
= 0;
243 pInfo
->mRect
.width
= mSepartorSize
;
244 pInfo
->mRect
.height
= 0;
250 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent
& event
)
255 void wxDynamicToolBar::OnSize( wxSizeEvent
& event
)
257 //SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE ) );
262 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo
& info
, wxDC
& dc
)
264 // check the orientation of separator
265 if ( info
.mRect
.width
< info
.mRect
.height
)
267 int midX
= info
.mRect
.x
+ info
.mRect
.width
/2 - 1;
269 dc
.SetPen( *wxGREY_PEN
);
270 dc
.DrawLine( midX
, info
.mRect
.y
,
271 midX
, info
.mRect
.y
+ info
.mRect
.height
+1 );
273 dc
.SetPen( *wxWHITE_PEN
);
274 dc
.DrawLine( midX
+1, info
.mRect
.y
,
275 midX
+1, info
.mRect
.y
+ info
.mRect
.height
+1 );
279 int midY
= info
.mRect
.y
+ info
.mRect
.height
/2 - 1;
281 dc
.SetPen( *wxGREY_PEN
);
282 dc
.DrawLine( info
.mRect
.x
, midY
,
283 info
.mRect
.x
+ info
.mRect
.width
+1, midY
);
285 dc
.SetPen( *wxWHITE_PEN
);
286 dc
.DrawLine( info
.mRect
.x
, midY
+ 1,
287 info
.mRect
.x
+ info
.mRect
.width
+1, midY
+ 1 );
291 void wxDynamicToolBar::OnPaint( wxPaintEvent
& event
)
293 // draw separators if any
297 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
299 if ( mTools
[i
]->mIsSeparator
)
301 // check if separator doesn't have it's own window
302 // if so, then draw it using built-in drawing method
304 if ( !mTools
[i
]->mpToolWnd
)
306 DrawSeparator( *mTools
[i
], dc
);
310 // FOR NOW:: quick fix
311 #include "wx/choice.h"
313 void wxDynamicToolBar::SizeToolWindows()
315 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
317 wxDynToolInfo
& info
= *mTools
[i
];
319 if ( !info
.mIsSeparator
)
322 // center real rectangle within the rectangle
323 // provided by the layout manager
325 int x
= info
.mRect
.x
;
326 int y
= info
.mRect
.y
+ (info
.mRect
.height
- info
.mRealSize
.y
)/2;
328 // FOR NOW FOR NOW:: quick & dirty fix
329 if ( info
.mpToolWnd
->IsKindOf( CLASSINFO( wxChoice
) ) )
331 info
.mpToolWnd
->SetSize( x
,y
,
332 info
.mRealSize
.x
- 3,
336 info
.mpToolWnd
->SetSize( x
,y
,
341 // TBD:: size separator window if present
345 bool wxDynamicToolBar::Layout()
347 if ( !mpLayoutMan
) mpLayoutMan
= CreateDefaulLayout();
354 wxLayoutItemArrayT items
;
357 for( size_t i
= 0; i
!= mTools
.Count(); ++i
) items
.Add( mTools
[i
] );
359 mpLayoutMan
->Layout( wndDim
, result
, items
, mVertGap
, mHorizGap
);;
365 void wxDynamicToolBar::GetPreferredDim( const wxSize
& givenDim
, wxSize
& prefDim
)
367 if ( !mpLayoutMan
) mpLayoutMan
= CreateDefaulLayout();
369 wxLayoutItemArrayT items
;
372 for( size_t i
= 0; i
!= mTools
.Count(); ++i
) items
.Add( mTools
[i
] );
374 mpLayoutMan
->Layout( givenDim
, prefDim
, items
, mVertGap
, mHorizGap
);;
377 void wxDynamicToolBar::SetLayout( LayoutManagerBase
* pLayout
)
379 if ( mpLayoutMan
) delete mpLayoutMan
;
381 mpLayoutMan
= pLayout
;
386 void wxDynamicToolBar::EnableTool(const int toolIndex
, const bool enable
)
388 wxDynToolInfo
* pInfo
= GetToolInfo( toolIndex
);
390 if ( !pInfo
) return;
392 if ( pInfo
->mIsSeparator
|| !pInfo
->mpToolWnd
) return;
394 pInfo
->mpToolWnd
->Enable( enable
);
397 /***** Implementation for class BagLayout *****/
399 void BagLayout::Layout( const wxSize
& parentDim
,
400 wxSize
& resultingDim
,
401 wxLayoutItemArrayT
& items
,
412 while( i
< items
.Count() )
421 if ( nRows
> 0 ) curY
+= vertGap
;
423 // step #1 - arrange horizontal positions of items in the row
427 if ( itemsInRow
> 0 ) curX
+= horizGap
;
429 wxRect
& r
= items
[i
]->mRect
;
431 if ( curX
+ r
.width
> parentDim
.x
)
433 if ( itemsInRow
> 0 ) break;
440 height
= wxMax( height
, r
.height
);
445 } while( i
< items
.Count() );
449 maxWidth
= wxMax( maxWidth
, curX
);
452 resultingDim
.x
= maxWidth
;
453 resultingDim
.y
= curY
;