]>
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( wxColour(192,192,192) );
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( wxColour( 192,192,192 ) );
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
);
151 wxDynamicToolBar::AddTool(const int toolIndex
, const wxBitmap
& bitmap
,
152 const wxBitmap
& pushedBitmap
,
153 const bool toggle
, const long xPos
,
154 const long yPos
, wxObject
*clientData
,
155 const wxString
& helpString1
, const wxString
& helpString2
)
157 wxNewBitmapButton
* pBmpBtn
= new wxNewBitmapButton( bitmap
);
159 pBmpBtn
->Create( this, toolIndex
);
163 AddTool( toolIndex
, pBmpBtn
);
169 wxDynToolInfo
* wxDynamicToolBar::GetToolInfo( int toolIndex
)
171 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
173 if ( mTools
[i
]->mIndex
== toolIndex
) return mTools
[i
];
178 void wxDynamicToolBar::RemveTool( int toolIndex
)
180 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
182 if ( mTools
[i
]->mIndex
== toolIndex
)
184 if ( mTools
[i
]->mpToolWnd
)
186 mTools
[i
]->mpToolWnd
->Destroy();
195 // TODO:: if not found, should it be an assertion?
198 void wxDynamicToolBar::AddSeparator( wxWindow
* pSepartorWnd
)
200 wxDynToolInfo
* pInfo
= new wxDynToolInfo();
202 pInfo
->mpToolWnd
= pSepartorWnd
;
204 pInfo
->mIsSeparator
= TRUE
;
208 pSepartorWnd
->Create( this, -1 );
211 pSepartorWnd
->GetSize( &x
, &y
);
212 pInfo
->mRealSize
.x
= x
;
213 pInfo
->mRealSize
.y
= y
;
215 pInfo
->mRect
.width
= x
;
216 pInfo
->mRect
.height
= y
;
220 pInfo
->mRealSize
.x
= mSepartorSize
;
221 pInfo
->mRealSize
.y
= 0;
223 pInfo
->mRect
.width
= mSepartorSize
;
224 pInfo
->mRect
.height
= 0;
230 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent
& event
)
235 void wxDynamicToolBar::OnSize( wxSizeEvent
& event
)
237 //SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_3DFACE ) );
242 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo
& info
, wxDC
& dc
)
244 // check the orientation of separator
245 if ( info
.mRect
.width
< info
.mRect
.height
)
247 int midX
= info
.mRect
.x
+ info
.mRect
.width
/2 - 1;
249 dc
.SetPen( *wxGREY_PEN
);
250 dc
.DrawLine( midX
, info
.mRect
.y
,
251 midX
, info
.mRect
.y
+ info
.mRect
.height
+1 );
253 dc
.SetPen( *wxWHITE_PEN
);
254 dc
.DrawLine( midX
+1, info
.mRect
.y
,
255 midX
+1, info
.mRect
.y
+ info
.mRect
.height
+1 );
259 int midY
= info
.mRect
.y
+ info
.mRect
.height
/2 - 1;
261 dc
.SetPen( *wxGREY_PEN
);
262 dc
.DrawLine( info
.mRect
.x
, midY
,
263 info
.mRect
.x
+ info
.mRect
.width
+1, midY
);
265 dc
.SetPen( *wxWHITE_PEN
);
266 dc
.DrawLine( info
.mRect
.x
, midY
+ 1,
267 info
.mRect
.x
+ info
.mRect
.width
+1, midY
+ 1 );
271 void wxDynamicToolBar::OnPaint( wxPaintEvent
& event
)
273 // draw separators if any
277 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
279 if ( mTools
[i
]->mIsSeparator
)
281 // check if separator doesn't have it's own window
282 // if so, then draw it using built-in drawing method
284 if ( !mTools
[i
]->mpToolWnd
)
286 DrawSeparator( *mTools
[i
], dc
);
290 // FOR NOW:: quick fix
291 #include "wx/choice.h"
293 void wxDynamicToolBar::SizeToolWindows()
295 for( size_t i
= 0; i
!= mTools
.Count(); ++i
)
297 wxDynToolInfo
& info
= *mTools
[i
];
299 if ( !info
.mIsSeparator
)
302 // center real rectangle within the rectangle
303 // provided by the layout manager
305 int x
= info
.mRect
.x
;
306 int y
= info
.mRect
.y
+ (info
.mRect
.height
- info
.mRealSize
.y
)/2;
308 // FOR NOW FOR NOW:: quick & dirty fix
309 if ( info
.mpToolWnd
->IsKindOf( CLASSINFO( wxChoice
) ) )
311 info
.mpToolWnd
->SetSize( x
,y
,
312 info
.mRealSize
.x
- 3,
316 info
.mpToolWnd
->SetSize( x
,y
,
321 // TBD:: size separator window if present
325 void wxDynamicToolBar::Layout()
327 if ( !mpLayoutMan
) mpLayoutMan
= CreateDefaulLayout();
334 wxLayoutItemArrayT items
;
337 for( size_t i
= 0; i
!= mTools
.Count(); ++i
) items
.Add( mTools
[i
] );
339 mpLayoutMan
->Layout( wndDim
, result
, items
, mVertGap
, mHorizGap
);;
344 void wxDynamicToolBar::GetPreferredDim( const wxSize
& givenDim
, wxSize
& prefDim
)
346 if ( !mpLayoutMan
) mpLayoutMan
= CreateDefaulLayout();
348 wxLayoutItemArrayT items
;
351 for( size_t i
= 0; i
!= mTools
.Count(); ++i
) items
.Add( mTools
[i
] );
353 mpLayoutMan
->Layout( givenDim
, prefDim
, items
, mVertGap
, mHorizGap
);;
356 void wxDynamicToolBar::SetLayout( LayoutManagerBase
* pLayout
)
358 if ( mpLayoutMan
) delete mpLayoutMan
;
360 mpLayoutMan
= pLayout
;
365 void wxDynamicToolBar::EnableTool(const int toolIndex
, const bool enable
)
367 wxDynToolInfo
* pInfo
= GetToolInfo( toolIndex
);
369 if ( !pInfo
) return;
371 if ( pInfo
->mIsSeparator
|| !pInfo
->mpToolWnd
) return;
373 pInfo
->mpToolWnd
->Enable( enable
);
376 /***** Implementation for class BagLayout *****/
378 void BagLayout::Layout( const wxSize
& parentDim
,
379 wxSize
& resultingDim
,
380 wxLayoutItemArrayT
& items
,
391 while( i
< items
.Count() )
400 if ( nRows
> 0 ) curY
+= vertGap
;
402 // step #1 - arrange horizontal positions of items in the row
406 if ( itemsInRow
> 0 ) curX
+= horizGap
;
408 wxRect
& r
= items
[i
]->mRect
;
410 if ( curX
+ r
.width
> parentDim
.x
)
412 if ( itemsInRow
> 0 ) break;
419 height
= wxMax( height
, r
.height
);
424 } while( i
< items
.Count() );
428 maxWidth
= wxMax( maxWidth
, curX
);
431 resultingDim
.x
= maxWidth
;
432 resultingDim
.y
= curY
;