]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/fl/dyntbar.cpp
Ensure that endPos > startPos in a couple methods
[wxWidgets.git] / contrib / src / fl / dyntbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dyntbar.cpp
3 // Purpose: wxDynamicToolBar implementation
4 // Author: Aleksandras Gluchovas
5 // Modified by:
6 // Created: ??/10/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Aleksandras Gluchovas
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dyntbar.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/utils.h" // import wxMin,wxMax macros
28
29 #include "wx/fl/dyntbar.h"
30 #include "wx/fl/newbmpbtn.h"
31
32 IMPLEMENT_DYNAMIC_CLASS(wxDynamicToolBar, wxControl )
33
34 BEGIN_EVENT_TABLE( wxDynamicToolBar, wxControl )
35
36 EVT_SIZE ( wxDynamicToolBar::OnSize )
37 EVT_PAINT( wxDynamicToolBar::OnPaint )
38 //EVT_ERASE_BACKGROUND( wxDynamicToolBar::OnEraseBackground )
39
40 END_EVENT_TABLE()
41
42 /***** Implementation for class wxDynToolInfo *****/
43
44 IMPLEMENT_DYNAMIC_CLASS(wxDynToolInfo, wxToolLayoutItem)
45
46 /***** Implementation for class wxDynamicToolBar *****/
47
48 wxDynamicToolBar::wxDynamicToolBar()
49 : mpLayoutMan( NULL ),
50 mSepartorSize( 8 ),
51 mVertGap ( 0 ),
52 mHorizGap( 0 )
53 {
54 }
55
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 ),
61 mSepartorSize( 8 ),
62 mVertGap ( 0 ),
63 mHorizGap( 0 )
64 {
65 Create(parent, id, pos, size, style, orientation, RowsOrColumns, name);
66
67 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE) );
68 }
69
70 bool wxDynamicToolBar::Create(wxWindow *parent, const wxWindowID id,
71 const wxPoint& pos,
72 const wxSize& size,
73 const long style,
74 const int orientation, const int RowsOrColumns,
75 const wxString& name)
76 {
77 // cut&pasted from wxtbatsmpl.h
78
79 if ( ! wxWindow::Create(parent, id, pos, size, style, name) )
80 return FALSE;
81
82 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
83
84 return TRUE;
85 }
86
87 bool wxDynamicToolBar::Realize(void)
88 {
89 // FOR NOW:: nothing
90 return TRUE;
91 }
92
93 wxDynamicToolBar::~wxDynamicToolBar(void)
94 {
95 if ( mpLayoutMan )
96 delete mpLayoutMan;
97
98 size_t i;
99 for( i = 0; i != mTools.Count(); ++i )
100 {
101 delete mTools[i];
102 }
103 }
104
105 void wxDynamicToolBar::AddTool( int toolIndex,
106 wxWindow* pToolWindow,
107 const wxSize& size
108 )
109 {
110 wxDynToolInfo* pInfo = new wxDynToolInfo();
111
112 pInfo->mpToolWnd = pToolWindow;
113 pInfo->mIndex = toolIndex;
114 pInfo->mIsSeparator = FALSE;
115
116 int x,y;
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;
122
123 mTools.Add( pInfo );
124 }
125
126 void wxDynamicToolBar::AddTool( int toolIndex,
127 const wxString& imageFileName,
128 wxBitmapType imageFileType,
129 const wxString& labelText, bool alignTextRight,
130 bool isFlat )
131 {
132 wxNewBitmapButton* pBtn =
133
134 new wxNewBitmapButton( imageFileName, imageFileType,
135 labelText,
136 ( alignTextRight )
137 ? NB_ALIGN_TEXT_RIGHT
138 : NB_ALIGN_TEXT_BOTTOM,
139 isFlat
140 );
141
142 pBtn->Create( this, toolIndex );
143
144 pBtn->Reshape();
145
146 AddTool( toolIndex, pBtn );
147 }
148 void wxDynamicToolBar::AddTool( int toolIndex, wxBitmap labelBmp,
149 const wxString& labelText, bool alignTextRight,
150 bool isFlat )
151 {
152 wxNewBitmapButton* pBtn =
153
154 new wxNewBitmapButton( labelBmp,
155 labelText,
156 ( alignTextRight )
157 ? NB_ALIGN_TEXT_RIGHT
158 : NB_ALIGN_TEXT_BOTTOM,
159 isFlat
160 );
161
162 pBtn->Create( this, toolIndex );
163
164 pBtn->Reshape();
165
166 AddTool( toolIndex, pBtn );
167 }
168
169
170 wxToolBarToolBase*
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)
176 {
177 wxNewBitmapButton* pBmpBtn = new wxNewBitmapButton( bitmap );
178
179 pBmpBtn->Create( this, toolIndex );
180
181 pBmpBtn->Reshape();
182
183 AddTool( toolIndex, pBmpBtn );
184
185 return NULL;
186 }
187
188
189 wxDynToolInfo* wxDynamicToolBar::GetToolInfo( int toolIndex )
190 {
191 size_t i;
192 for( i = 0; i != mTools.Count(); ++i )
193 {
194 if ( mTools[i]->mIndex == toolIndex )
195 return mTools[i];
196 }
197
198 return NULL;
199 }
200
201 void wxDynamicToolBar::RemveTool( int toolIndex )
202 {
203 size_t i;
204 for( i = 0; i != mTools.Count(); ++i )
205 {
206 if ( mTools[i]->mIndex == toolIndex )
207 {
208 if ( mTools[i]->mpToolWnd )
209 {
210 mTools[i]->mpToolWnd->Destroy();
211 }
212 delete mTools[i]; // HVL To be tested!!!
213 #if wxCHECK_VERSION(2,3,2)
214 mTools.RemoveAt(i);
215 #else
216 mTools.Remove(i);
217 #endif
218 Layout();
219
220 return;
221 }
222 }
223 // TODO:: if not found, should it be an assertion?
224 }
225
226 void wxDynamicToolBar::AddSeparator( wxWindow* pSepartorWnd )
227 {
228 wxDynToolInfo* pInfo = new wxDynToolInfo();
229
230 pInfo->mpToolWnd = pSepartorWnd;
231 pInfo->mIndex = -1;
232 pInfo->mIsSeparator = TRUE;
233
234 if ( pSepartorWnd )
235 {
236 pSepartorWnd->Create( this, -1 );
237
238 int x,y;
239 pSepartorWnd->GetSize( &x, &y );
240 pInfo->mRealSize.x = x;
241 pInfo->mRealSize.y = y;
242
243 pInfo->mRect.width = x;
244 pInfo->mRect.height = y;
245 }
246 else
247 {
248 pInfo->mRealSize.x = mSepartorSize;
249 pInfo->mRealSize.y = 0;
250
251 pInfo->mRect.width = mSepartorSize;
252 pInfo->mRect.height = 0;
253 }
254
255 mTools.Add( pInfo );
256 }
257
258 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent& event )
259 {
260 // FOR NOW:: nothing
261 }
262
263 void wxDynamicToolBar::OnSize( wxSizeEvent& event )
264 {
265 //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) );
266
267 Layout();
268 }
269
270 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo& info, wxDC& dc )
271 {
272 // check the orientation of separator
273 if ( info.mRect.width < info.mRect.height )
274 {
275 int midX = info.mRect.x + info.mRect.width/2 - 1;
276
277 dc.SetPen( *wxGREY_PEN );
278 dc.DrawLine( midX, info.mRect.y,
279 midX, info.mRect.y + info.mRect.height+1 );
280
281 dc.SetPen( *wxWHITE_PEN );
282 dc.DrawLine( midX+1, info.mRect.y,
283 midX+1, info.mRect.y + info.mRect.height+1 );
284 }
285 else
286 {
287 int midY = info.mRect.y + info.mRect.height/2 - 1;
288
289 dc.SetPen( *wxGREY_PEN );
290 dc.DrawLine( info.mRect.x, midY,
291 info.mRect.x + info.mRect.width+1, midY );
292
293 dc.SetPen( *wxWHITE_PEN );
294 dc.DrawLine( info.mRect.x, midY + 1,
295 info.mRect.x + info.mRect.width+1, midY + 1 );
296 }
297 }
298
299 void wxDynamicToolBar::OnPaint( wxPaintEvent& event )
300 {
301 // draw separators if any
302
303 wxPaintDC dc(this);
304
305 size_t i;
306 for( i = 0; i != mTools.Count(); ++i )
307
308 if ( mTools[i]->mIsSeparator )
309 {
310 // check if separator doesn't have it's own window
311 // if so, then draw it using built-in drawing method
312
313 if ( !mTools[i]->mpToolWnd )
314
315 DrawSeparator( *mTools[i], dc );
316 }
317 }
318
319 // FOR NOW:: quick fix
320 #include "wx/choice.h"
321
322 void wxDynamicToolBar::SizeToolWindows()
323 {
324 size_t i;
325 for( i = 0; i != mTools.Count(); ++i )
326 {
327 wxDynToolInfo& info = *mTools[i];
328
329 if ( !info.mIsSeparator )
330 {
331
332 // center real rectangle within the rectangle
333 // provided by the layout manager
334
335 int x = info.mRect.x;
336 int y = info.mRect.y + (info.mRect.height - info.mRealSize.y)/2;
337
338 // FOR NOW FOR NOW:: quick & dirty fix
339 if ( info.mpToolWnd->IsKindOf( CLASSINFO( wxChoice ) ) )
340 {
341 info.mpToolWnd->SetSize( x,y,
342 info.mRealSize.x - 3,
343 info.mRealSize.y);
344 }
345 else
346 info.mpToolWnd->SetSize( x,y,
347 info.mRealSize.x,
348 info.mRealSize.y );
349 }
350
351 // TBD:: size separator window if present
352 }
353 }
354
355 bool wxDynamicToolBar::Layout()
356 {
357 if ( !mpLayoutMan )
358 mpLayoutMan = CreateDefaultLayout();
359
360 int x,y;
361 GetSize( &x, &y );
362 wxSize wndDim(x,y);
363 wxSize result;
364
365 wxLayoutItemArrayT items;
366
367 // safe conversion
368 size_t i;
369 for( i = 0; i != mTools.Count(); ++i )
370 items.Add( mTools[i] );
371
372 mpLayoutMan->Layout( wndDim, result, items, mVertGap, mHorizGap );;
373
374 SizeToolWindows();
375 return TRUE;
376 }
377
378 void wxDynamicToolBar::GetPreferredDim( const wxSize& givenDim, wxSize& prefDim )
379 {
380 if ( !mpLayoutMan )
381 mpLayoutMan = CreateDefaultLayout();
382
383 wxLayoutItemArrayT items;
384
385 // safe conversion
386 size_t i;
387 for( i = 0; i != mTools.Count(); ++i )
388 items.Add( mTools[i] );
389
390 mpLayoutMan->Layout( givenDim, prefDim, items, mVertGap, mHorizGap );;
391 }
392
393 void wxDynamicToolBar::SetLayout( LayoutManagerBase* pLayout )
394 {
395 if ( mpLayoutMan )
396 delete mpLayoutMan;
397
398 mpLayoutMan = pLayout;
399
400 Layout();
401 }
402
403 void wxDynamicToolBar::EnableTool(const int toolIndex, const bool enable )
404 {
405 wxDynToolInfo* pInfo = GetToolInfo( toolIndex );
406
407 if ( !pInfo )
408 return;
409
410 if ( pInfo->mIsSeparator || !pInfo->mpToolWnd )
411 return;
412
413 pInfo->mpToolWnd->Enable( enable );
414 }
415
416 /***** Implementation for class BagLayout *****/
417
418 void BagLayout::Layout( const wxSize& parentDim,
419 wxSize& resultingDim,
420 wxLayoutItemArrayT& items,
421 int horizGap,
422 int vertGap
423 )
424 {
425 int maxWidth = 0;
426 int curY = 0;
427 int nRows = 0;
428
429 size_t i = 0;
430
431 while( i < items.Count() )
432 {
433 int curX = 0;
434 int height = 0;
435 // int nItems = 0;
436
437 // int firstItem = i;
438 int itemsInRow = 0;
439
440 if ( nRows > 0 )
441 curY += vertGap;
442
443 // step #1 - arrange horizontal positions of items in the row
444
445 do
446 {
447 if ( itemsInRow > 0 )
448 curX += horizGap;
449
450 wxRect& r = items[i]->mRect;
451
452 if ( curX + r.width > parentDim.x )
453 {
454 if ( itemsInRow > 0 )
455 break;
456 }
457 r.x = curX;
458 r.y = curY;
459
460 curX += r.width;
461
462 height = wxMax( height, r.height );
463
464 ++itemsInRow;
465 ++i;
466
467 } while( i < items.Count() );
468
469 curY += height;
470
471 maxWidth = wxMax( maxWidth, curX );
472 }
473
474 resultingDim.x = maxWidth;
475 resultingDim.y = curY;
476 }
477
478 //////// stuff from 2.1.15 ///////////
479
480 wxToolBarToolBase* wxDynamicToolBar::FindToolForPosition( wxCoord x, wxCoord y ) const
481 {
482 return NULL;
483 }
484
485 bool wxDynamicToolBar::DoInsertTool( size_t pos, wxToolBarToolBase* tool )
486 {
487 return TRUE;
488 }
489
490 bool wxDynamicToolBar::DoDeleteTool( size_t pos, wxToolBarToolBase* tool )
491 {
492 return TRUE;
493 }
494
495 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase* tool, bool enable )
496 {
497 }
498
499 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase* tool, bool toggle )
500 {
501 }
502
503 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase* tool, bool toggle )
504 {
505 }
506
507 wxToolBarToolBase* wxDynamicToolBar::CreateTool( int id, const wxBitmap& bitmap1, const wxBitmap& bitmap2, bool toggle, wxObject* clientData, const wxString& shortHelpString, const wxString& longHelpString )
508 {
509 return NULL;
510 }
511
512 wxToolBarToolBase* wxDynamicToolBar::CreateTool( wxControl* control )
513 {
514 return NULL;
515 }
516