]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/fl/dyntbar.cpp
Better fix for 'int i' used twice in the same block.
[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, wxObject )
33
34 BEGIN_EVENT_TABLE( wxDynamicToolBar, wxToolBarBase )
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 wxToolLayoutItem *****/
43
44 IMPLEMENT_DYNAMIC_CLASS(wxToolLayoutItem, wxObject)
45
46
47 /***** Implementation for class wxDynToolInfo *****/
48
49 IMPLEMENT_DYNAMIC_CLASS(wxDynToolInfo, wxToolLayoutItem)
50
51 /***** Implementation for class wxDynamicToolBar *****/
52
53 wxDynamicToolBar::wxDynamicToolBar()
54 : mpLayoutMan( NULL ),
55 mSepartorSize( 8 ),
56 mVertGap ( 0 ),
57 mHorizGap( 0 )
58 {
59 }
60
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 ),
66 mSepartorSize( 8 ),
67 mVertGap ( 0 ),
68 mHorizGap( 0 )
69 {
70 Create(parent, id, pos, size, style, orientation, RowsOrColumns, name);
71
72 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE) );
73 }
74
75 bool wxDynamicToolBar::Create(wxWindow *parent, const wxWindowID id,
76 const wxPoint& pos,
77 const wxSize& size,
78 const long style,
79 const int WXUNUSED(orientation), const int WXUNUSED(RowsOrColumns),
80 const wxString& name)
81 {
82 // cut&pasted from wxtbatsmpl.h
83
84 if ( ! wxWindow::Create(parent, id, pos, size, style, name) )
85 return false;
86
87 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
88
89 return true;
90 }
91
92 bool wxDynamicToolBar::Realize(void)
93 {
94 // FOR NOW:: nothing
95 return true;
96 }
97
98 wxDynamicToolBar::~wxDynamicToolBar(void)
99 {
100 if ( mpLayoutMan )
101 delete mpLayoutMan;
102
103 size_t i;
104 for( i = 0; i != mTools.Count(); ++i )
105 {
106 delete mTools[i];
107 }
108 }
109
110 void wxDynamicToolBar::AddTool( int toolIndex,
111 wxWindow* pToolWindow,
112 const wxSize& WXUNUSED(size)
113 )
114 {
115 wxDynToolInfo* pInfo = new wxDynToolInfo();
116
117 pInfo->mpToolWnd = pToolWindow;
118 pInfo->mIndex = toolIndex;
119 pInfo->mIsSeparator = false;
120
121 int x,y;
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;
127
128 mTools.Add( pInfo );
129 }
130
131 void wxDynamicToolBar::AddTool( int toolIndex,
132 const wxString& imageFileName,
133 wxBitmapType imageFileType,
134 const wxString& labelText, bool alignTextRight,
135 bool isFlat )
136 {
137 wxNewBitmapButton* pBtn =
138
139 new wxNewBitmapButton( imageFileName, imageFileType,
140 labelText,
141 ( alignTextRight )
142 ? NB_ALIGN_TEXT_RIGHT
143 : NB_ALIGN_TEXT_BOTTOM,
144 isFlat
145 );
146
147 pBtn->Create( this, toolIndex );
148
149 pBtn->Reshape();
150
151 AddTool( toolIndex, pBtn );
152 }
153 void wxDynamicToolBar::AddTool( int toolIndex, wxBitmap labelBmp,
154 const wxString& labelText, bool alignTextRight,
155 bool isFlat )
156 {
157 wxNewBitmapButton* pBtn =
158
159 new wxNewBitmapButton( labelBmp,
160 labelText,
161 ( alignTextRight )
162 ? NB_ALIGN_TEXT_RIGHT
163 : NB_ALIGN_TEXT_BOTTOM,
164 isFlat
165 );
166
167 pBtn->Create( this, toolIndex );
168
169 pBtn->Reshape();
170
171 AddTool( toolIndex, pBtn );
172 }
173
174
175 wxToolBarToolBase*
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))
181 {
182 wxNewBitmapButton* pBmpBtn = new wxNewBitmapButton( bitmap );
183
184 pBmpBtn->Create( this, toolIndex );
185
186 pBmpBtn->Reshape();
187
188 #if wxUSE_TOOLTIPS
189 pBmpBtn->SetToolTip( helpString1 );
190 #else
191 wxUnusedVar( helpString1 );
192 #endif // wxUSE_TOOLTIPS
193
194 AddTool( toolIndex, pBmpBtn );
195
196 return NULL;
197 }
198
199
200 wxDynToolInfo* wxDynamicToolBar::GetToolInfo( int toolIndex )
201 {
202 size_t i;
203 for( i = 0; i != mTools.Count(); ++i )
204 {
205 if ( mTools[i]->mIndex == toolIndex )
206 return mTools[i];
207 }
208
209 return NULL;
210 }
211
212 void wxDynamicToolBar::RemveTool( int toolIndex )
213 {
214 size_t i;
215 for( i = 0; i != mTools.Count(); ++i )
216 {
217 if ( mTools[i]->mIndex == toolIndex )
218 {
219 if ( mTools[i]->mpToolWnd )
220 {
221 mTools[i]->mpToolWnd->Destroy();
222 }
223 delete mTools[i]; // HVL To be tested!!!
224 #if wxCHECK_VERSION(2,3,2)
225 mTools.RemoveAt(i);
226 #else
227 mTools.Remove(i);
228 #endif
229 Layout();
230
231 return;
232 }
233 }
234 // TODO:: if not found, should it be an assertion?
235 }
236
237 void wxDynamicToolBar::AddSeparator( wxWindow* pSepartorWnd )
238 {
239 wxDynToolInfo* pInfo = new wxDynToolInfo();
240
241 pInfo->mpToolWnd = pSepartorWnd;
242 pInfo->mIndex = -1;
243 pInfo->mIsSeparator = true;
244
245 // Do we draw a separator or is a other object?
246 if ( pSepartorWnd )
247 {
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 );
251
252 int x,y;
253 pSepartorWnd->GetSize( &x, &y );
254 pInfo->mRealSize.x = x;
255 pInfo->mRealSize.y = y;
256
257 pInfo->mRect.width = x;
258 pInfo->mRect.height = y;
259 }
260 else
261 {
262 // Init x and y to the default.
263 pInfo->mRealSize.x = 0;
264 pInfo->mRealSize.y = 0;
265
266 // Init height and width to the normal size of a separator.
267 pInfo->mRect.width = mSepartorSize;
268 pInfo->mRect.height = mSepartorSize;
269 }
270
271 mTools.Add( pInfo );
272 }
273
274 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent& WXUNUSED(event) )
275 {
276 // FOR NOW:: nothing
277 }
278
279 void wxDynamicToolBar::OnSize( wxSizeEvent& WXUNUSED(event) )
280 {
281 //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) );
282
283 Layout();
284 }
285
286 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo& info, wxDC& dc )
287 {
288 // check the orientation of separator
289 if ( info.mRect.width < info.mRect.height )
290 {
291 int midX = info.mRect.x + info.mRect.width/2 - 1;
292
293 dc.SetPen( *wxGREY_PEN );
294 dc.DrawLine( midX, info.mRect.y,
295 midX, info.mRect.y + info.mRect.height+1 );
296
297 dc.SetPen( *wxWHITE_PEN );
298 dc.DrawLine( midX+1, info.mRect.y,
299 midX+1, info.mRect.y + info.mRect.height+1 );
300 }
301 else
302 {
303 int midY = info.mRect.y + info.mRect.height/2 - 1;
304
305 dc.SetPen( *wxGREY_PEN );
306 dc.DrawLine( info.mRect.x, midY,
307 info.mRect.x + info.mRect.width+1, midY );
308
309 dc.SetPen( *wxWHITE_PEN );
310 dc.DrawLine( info.mRect.x, midY + 1,
311 info.mRect.x + info.mRect.width+1, midY + 1 );
312 }
313 }
314
315 void wxDynamicToolBar::OnPaint( wxPaintEvent& WXUNUSED(event) )
316 {
317 // draw separators if any
318 wxPaintDC dc(this);
319
320 size_t i;
321 for( i = 0; i != mTools.Count(); ++i )
322 {
323 if ( mTools[i]->mIsSeparator )
324 {
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 );
329 }
330 }
331 }
332
333 // FOR NOW:: quick fix
334 #include "wx/choice.h"
335
336 void wxDynamicToolBar::SizeToolWindows()
337 {
338 bool bStateCheckDone = false;
339 bool bHorzSeparator = false;
340 int maxWidth = 0;
341 int maxHeight = 0;
342
343 size_t i;
344 for( i = 0; i != mTools.Count(); ++i )
345 {
346 wxDynToolInfo& info = *mTools[i];
347
348 if ( !info.mIsSeparator )
349 {
350 // center real rectangle within the rectangle
351 // provided by the layout manager
352
353 int x = info.mRect.x;
354 int y = info.mRect.y + (info.mRect.height - info.mRealSize.y)/2;
355
356 // FOR NOW FOR NOW:: quick & dirty fix
357 if ( info.mpToolWnd->IsKindOf( CLASSINFO( wxChoice ) ) )
358 {
359 info.mpToolWnd->SetSize( x, y,
360 info.mRealSize.x - 3,
361 info.mRealSize.y);
362 }
363 else
364 {
365 info.mpToolWnd->SetSize( x, y,
366 info.mRealSize.x,
367 info.mRealSize.y );
368 }
369 }
370 else
371 {
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)
375 {
376 bStateCheckDone = true;
377
378 size_t j;
379 wxDynToolInfo *pInfo;
380 wxDynToolInfo *pPrevInfo = NULL;
381 int nVertSeparators = 0;
382
383 for( j = 0; j != mTools.Count(); ++j )
384 {
385 pInfo = mTools[j];
386
387 // Count all Vert Separators.
388 if ( pInfo->mIsSeparator )
389 nVertSeparators++;
390
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)
395 {
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())
400 {
401 if (pInfo->mRect.y < mTools[j+1]->mRect.y)
402 nVertSeparators--;
403 }
404 else
405 {
406 nVertSeparators--;
407 }
408 }
409
410 pPrevInfo = pInfo;
411
412 maxWidth = wxMax(pInfo->mRect.width, maxWidth);
413 maxHeight = wxMax(pInfo->mRect.height, maxHeight);
414 }
415
416 bHorzSeparator = nVertSeparators == 0;
417 }
418
419 // Check if we should draw Horz or Vert...
420 if ( !bHorzSeparator )
421 {
422 info.mRect.width = mSepartorSize;
423 info.mRect.height = maxHeight;
424 }
425 else
426 {
427 info.mRect.width = maxWidth;
428 info.mRect.height = mSepartorSize;
429 }
430
431 // Do we need to set a new size to a separator object?
432 if ( info.mpToolWnd )
433 {
434 info.mpToolWnd->SetSize( info.mRect.x,
435 info.mRect.y,
436 info.mRect.width,
437 info.mRect.height);
438 }
439
440 }
441 }
442 }
443
444 bool wxDynamicToolBar::Layout()
445 {
446 int x,y;
447 GetSize( &x, &y );
448 wxSize wndDim(x,y);
449 wxSize result;
450 size_t i;
451 wxDynToolInfo *pInfo;
452
453 // Reset the size of separators...
454 for( i = 0; i != mTools.Count(); ++i )
455 {
456 pInfo = mTools[i];
457
458 if ( pInfo->mIsSeparator )
459 {
460 pInfo->mRect.width = mSepartorSize;
461 pInfo->mRect.height = mSepartorSize;
462 }
463 }
464
465 // Calc and set the best layout
466 GetPreferredDim( wndDim, result );
467
468 SizeToolWindows();
469 return true;
470 }
471
472 void wxDynamicToolBar::GetPreferredDim( const wxSize& givenDim, wxSize& prefDim )
473 {
474 if ( !mpLayoutMan )
475 mpLayoutMan = CreateDefaultLayout();
476
477 wxLayoutItemArrayT items;
478
479 // safe conversion
480 size_t i;
481 for( i = 0; i != mTools.Count(); ++i )
482 items.Add( mTools[i] );
483
484 mpLayoutMan->Layout( givenDim, prefDim, items, mVertGap, mHorizGap );
485 }
486
487 void wxDynamicToolBar::SetLayout( LayoutManagerBase* pLayout )
488 {
489 if ( mpLayoutMan )
490 delete mpLayoutMan;
491
492 mpLayoutMan = pLayout;
493
494 Layout();
495 }
496
497 void wxDynamicToolBar::EnableTool(int toolIndex, bool enable )
498 {
499 wxDynToolInfo* pInfo = GetToolInfo( toolIndex );
500
501 if ( !pInfo )
502 return;
503
504 if ( pInfo->mIsSeparator || !pInfo->mpToolWnd )
505 return;
506
507 pInfo->mpToolWnd->Enable( enable );
508 }
509
510 /***** Implementation for class BagLayout *****/
511
512 void BagLayout::Layout( const wxSize& parentDim,
513 wxSize& resultingDim,
514 wxLayoutItemArrayT& items,
515 int horizGap,
516 int vertGap
517 )
518 {
519 int maxWidth = 0;
520 int curY = 0;
521 int nRows = 0;
522
523 size_t i = 0;
524
525 while( i < items.Count() )
526 {
527 int curX = 0;
528 int height = 0;
529 // int nItems = 0;
530
531 // int firstItem = i;
532 int itemsInRow = 0;
533
534 if ( nRows > 0 )
535 curY += vertGap;
536
537 // step #1 - arrange horizontal positions of items in the row
538
539 do
540 {
541 if ( itemsInRow > 0 )
542 curX += horizGap;
543
544 wxRect& r = items[i]->mRect;
545
546 if ( curX + r.width > parentDim.x )
547 {
548 if ( itemsInRow > 0 )
549 break;
550 }
551 r.x = curX;
552 r.y = curY;
553
554 curX += r.width;
555
556 height = wxMax( height, r.height );
557
558 ++itemsInRow;
559 ++i;
560
561 } while( i < items.Count() );
562
563 curY += height;
564
565 maxWidth = wxMax( maxWidth, curX );
566 }
567
568 resultingDim.x = maxWidth;
569 resultingDim.y = curY;
570 }
571
572 //////// stuff from 2.1.15 ///////////
573
574 wxToolBarToolBase* wxDynamicToolBar::FindToolForPosition( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) ) const
575 {
576 return NULL;
577 }
578
579 bool wxDynamicToolBar::DoInsertTool( size_t WXUNUSED(pos), wxToolBarToolBase* WXUNUSED(tool) )
580 {
581 return true;
582 }
583
584 bool wxDynamicToolBar::DoDeleteTool( size_t WXUNUSED(pos), wxToolBarToolBase* WXUNUSED(tool) )
585 {
586 return true;
587 }
588
589 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase* WXUNUSED(tool), bool WXUNUSED(enable) )
590 {
591 }
592
593 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase* WXUNUSED(tool), bool WXUNUSED(toggle) )
594 {
595 }
596
597 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase* WXUNUSED(tool), bool WXUNUSED(toggle) )
598 {
599 }
600
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)
609 )
610 {
611 return NULL;
612 }
613
614 wxToolBarToolBase* wxDynamicToolBar::CreateTool( wxControl* WXUNUSED(control) )
615 {
616 return NULL;
617 }
618