]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/fl/dyntbar.cpp
Patch #514729 applied - adds missing RTTIs to wxToolLayoutItem
[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 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 orientation, const int 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& 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& pushedBitmap,
178 const bool toggle, const long xPos,
179 const long yPos, wxObject *clientData,
180 const wxString& helpString1, const wxString& helpString2)
181 {
182 wxNewBitmapButton* pBmpBtn = new wxNewBitmapButton( bitmap );
183
184 pBmpBtn->Create( this, toolIndex );
185
186 pBmpBtn->Reshape();
187
188 AddTool( toolIndex, pBmpBtn );
189
190 return NULL;
191 }
192
193
194 wxDynToolInfo* wxDynamicToolBar::GetToolInfo( int toolIndex )
195 {
196 size_t i;
197 for( i = 0; i != mTools.Count(); ++i )
198 {
199 if ( mTools[i]->mIndex == toolIndex )
200 return mTools[i];
201 }
202
203 return NULL;
204 }
205
206 void wxDynamicToolBar::RemveTool( int toolIndex )
207 {
208 size_t i;
209 for( i = 0; i != mTools.Count(); ++i )
210 {
211 if ( mTools[i]->mIndex == toolIndex )
212 {
213 if ( mTools[i]->mpToolWnd )
214 {
215 mTools[i]->mpToolWnd->Destroy();
216 }
217 delete mTools[i]; // HVL To be tested!!!
218 #if wxCHECK_VERSION(2,3,2)
219 mTools.RemoveAt(i);
220 #else
221 mTools.Remove(i);
222 #endif
223 Layout();
224
225 return;
226 }
227 }
228 // TODO:: if not found, should it be an assertion?
229 }
230
231 void wxDynamicToolBar::AddSeparator( wxWindow* pSepartorWnd )
232 {
233 wxDynToolInfo* pInfo = new wxDynToolInfo();
234
235 pInfo->mpToolWnd = pSepartorWnd;
236 pInfo->mIndex = -1;
237 pInfo->mIsSeparator = TRUE;
238
239 // Do we draw a separator or is a other object?
240 if ( pSepartorWnd )
241 {
242 // hvl => Is there a way to know if it was already created?
243 // hvl => shouldn't the pSepartorWnd be created? (like one should expect?)
244 // pSepartorWnd->Create( this, -1 );
245
246 int x,y;
247 pSepartorWnd->GetSize( &x, &y );
248 pInfo->mRealSize.x = x;
249 pInfo->mRealSize.y = y;
250
251 pInfo->mRect.width = x;
252 pInfo->mRect.height = y;
253 }
254 else
255 {
256 // Init x and y to the default.
257 pInfo->mRealSize.x = 0;
258 pInfo->mRealSize.y = 0;
259
260 // Init height and width to the normal size of a separator.
261 pInfo->mRect.width = mSepartorSize;
262 pInfo->mRect.height = mSepartorSize;
263 }
264
265 mTools.Add( pInfo );
266 }
267
268 void wxDynamicToolBar::OnEraseBackground( wxEraseEvent& event )
269 {
270 // FOR NOW:: nothing
271 }
272
273 void wxDynamicToolBar::OnSize( wxSizeEvent& event )
274 {
275 //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) );
276
277 Layout();
278 }
279
280 void wxDynamicToolBar::DrawSeparator( wxDynToolInfo& info, wxDC& dc )
281 {
282 // check the orientation of separator
283 if ( info.mRect.width < info.mRect.height )
284 {
285 int midX = info.mRect.x + info.mRect.width/2 - 1;
286
287 dc.SetPen( *wxGREY_PEN );
288 dc.DrawLine( midX, info.mRect.y,
289 midX, info.mRect.y + info.mRect.height+1 );
290
291 dc.SetPen( *wxWHITE_PEN );
292 dc.DrawLine( midX+1, info.mRect.y,
293 midX+1, info.mRect.y + info.mRect.height+1 );
294 }
295 else
296 {
297 int midY = info.mRect.y + info.mRect.height/2 - 1;
298
299 dc.SetPen( *wxGREY_PEN );
300 dc.DrawLine( info.mRect.x, midY,
301 info.mRect.x + info.mRect.width+1, midY );
302
303 dc.SetPen( *wxWHITE_PEN );
304 dc.DrawLine( info.mRect.x, midY + 1,
305 info.mRect.x + info.mRect.width+1, midY + 1 );
306 }
307 }
308
309 void wxDynamicToolBar::OnPaint( wxPaintEvent& event )
310 {
311 // draw separators if any
312 wxPaintDC dc(this);
313
314 size_t i;
315 for( i = 0; i != mTools.Count(); ++i )
316 {
317 if ( mTools[i]->mIsSeparator )
318 {
319 // check if separator doesn't have it's own window
320 // if so, then draw it using built-in drawing method
321 if ( !mTools[i]->mpToolWnd )
322 DrawSeparator( *mTools[i], dc );
323 }
324 }
325 }
326
327 // FOR NOW:: quick fix
328 #include "wx/choice.h"
329
330 void wxDynamicToolBar::SizeToolWindows()
331 {
332 bool bStateCheckDone = FALSE;
333 bool bHorzSeparator = FALSE;
334 int maxWidth = 0;
335 int maxHeight = 0;
336
337 size_t i;
338 for( i = 0; i != mTools.Count(); ++i )
339 {
340 wxDynToolInfo& info = *mTools[i];
341
342 if ( !info.mIsSeparator )
343 {
344 // center real rectangle within the rectangle
345 // provided by the layout manager
346
347 int x = info.mRect.x;
348 int y = info.mRect.y + (info.mRect.height - info.mRealSize.y)/2;
349
350 // FOR NOW FOR NOW:: quick & dirty fix
351 if ( info.mpToolWnd->IsKindOf( CLASSINFO( wxChoice ) ) )
352 {
353 info.mpToolWnd->SetSize( x, y,
354 info.mRealSize.x - 3,
355 info.mRealSize.y);
356 }
357 else
358 {
359 info.mpToolWnd->SetSize( x, y,
360 info.mRealSize.x,
361 info.mRealSize.y );
362 }
363 }
364 else
365 {
366 // We performer this code here, so we only execute it when we have
367 // separators and we do it only once (all to do with performance...)
368 if (!bStateCheckDone)
369 {
370 bStateCheckDone = TRUE;
371
372 size_t j;
373 wxDynToolInfo *pInfo = NULL;
374 wxDynToolInfo *pPrevInfo = NULL;
375 int nVertSeparators = 0;
376
377 for( j = 0; j != mTools.Count(); ++j )
378 {
379 pInfo = mTools[j];
380
381 // Count all Vert Separators.
382 if ( pInfo->mIsSeparator )
383 nVertSeparators++;
384
385 // Check if the new row starts with a Separator.
386 if ( pPrevInfo && pInfo->mIsSeparator &&
387 // pPrevInfo->mRect.x >= pInfo->mRect.x &&
388 pPrevInfo->mRect.y < pInfo->mRect.y)
389 {
390 // If the Separator is shown on the next row and it's
391 // the only object on the row it would mean that the
392 // Separator should be shown as Horizontal one.
393 if (j+1 != mTools.Count())
394 {
395 if (pInfo->mRect.y < mTools[j+1]->mRect.y)
396 nVertSeparators--;
397 }
398 else
399 {
400 nVertSeparators--;
401 }
402 }
403
404 pPrevInfo = pInfo;
405
406 maxWidth = wxMax(pInfo->mRect.width, maxWidth);
407 maxHeight = wxMax(pInfo->mRect.height, maxHeight);
408 }
409
410 bHorzSeparator = nVertSeparators == 0;
411 }
412
413 // Check if we should draw Horz or Vert...
414 if ( !bHorzSeparator )
415 {
416 info.mRect.width = mSepartorSize;
417 info.mRect.height = maxHeight;
418 }
419 else
420 {
421 info.mRect.width = maxWidth;
422 info.mRect.height = mSepartorSize;
423 }
424
425 // Do we need to set a new size to a seperator object?
426 if ( info.mpToolWnd )
427 {
428 info.mpToolWnd->SetSize( info.mRect.x,
429 info.mRect.y,
430 info.mRect.width,
431 info.mRect.height);
432 }
433
434 }
435 }
436 }
437
438 bool wxDynamicToolBar::Layout()
439 {
440 int x,y;
441 GetSize( &x, &y );
442 wxSize wndDim(x,y);
443 wxSize result;
444 size_t i;
445 wxDynToolInfo *pInfo;
446
447 // Reset the size of separators...
448 for( i = 0; i != mTools.Count(); ++i )
449 {
450 pInfo = mTools[i];
451
452 if ( pInfo->mIsSeparator )
453 {
454 pInfo->mRect.width = mSepartorSize;
455 pInfo->mRect.height = mSepartorSize;
456 }
457 }
458
459 // Calc and set the best layout
460 GetPreferredDim( wndDim, result );
461
462 SizeToolWindows();
463 return TRUE;
464 }
465
466 void wxDynamicToolBar::GetPreferredDim( const wxSize& givenDim, wxSize& prefDim )
467 {
468 if ( !mpLayoutMan )
469 mpLayoutMan = CreateDefaultLayout();
470
471 wxLayoutItemArrayT items;
472
473 // safe conversion
474 size_t i;
475 for( i = 0; i != mTools.Count(); ++i )
476 items.Add( mTools[i] );
477
478 mpLayoutMan->Layout( givenDim, prefDim, items, mVertGap, mHorizGap );
479 }
480
481 void wxDynamicToolBar::SetLayout( LayoutManagerBase* pLayout )
482 {
483 if ( mpLayoutMan )
484 delete mpLayoutMan;
485
486 mpLayoutMan = pLayout;
487
488 Layout();
489 }
490
491 void wxDynamicToolBar::EnableTool(const int toolIndex, const bool enable )
492 {
493 wxDynToolInfo* pInfo = GetToolInfo( toolIndex );
494
495 if ( !pInfo )
496 return;
497
498 if ( pInfo->mIsSeparator || !pInfo->mpToolWnd )
499 return;
500
501 pInfo->mpToolWnd->Enable( enable );
502 }
503
504 /***** Implementation for class BagLayout *****/
505
506 void BagLayout::Layout( const wxSize& parentDim,
507 wxSize& resultingDim,
508 wxLayoutItemArrayT& items,
509 int horizGap,
510 int vertGap
511 )
512 {
513 int maxWidth = 0;
514 int curY = 0;
515 int nRows = 0;
516
517 size_t i = 0;
518
519 while( i < items.Count() )
520 {
521 int curX = 0;
522 int height = 0;
523 // int nItems = 0;
524
525 // int firstItem = i;
526 int itemsInRow = 0;
527
528 if ( nRows > 0 )
529 curY += vertGap;
530
531 // step #1 - arrange horizontal positions of items in the row
532
533 do
534 {
535 if ( itemsInRow > 0 )
536 curX += horizGap;
537
538 wxRect& r = items[i]->mRect;
539
540 if ( curX + r.width > parentDim.x )
541 {
542 if ( itemsInRow > 0 )
543 break;
544 }
545 r.x = curX;
546 r.y = curY;
547
548 curX += r.width;
549
550 height = wxMax( height, r.height );
551
552 ++itemsInRow;
553 ++i;
554
555 } while( i < items.Count() );
556
557 curY += height;
558
559 maxWidth = wxMax( maxWidth, curX );
560 }
561
562 resultingDim.x = maxWidth;
563 resultingDim.y = curY;
564 }
565
566 //////// stuff from 2.1.15 ///////////
567
568 wxToolBarToolBase* wxDynamicToolBar::FindToolForPosition( wxCoord x, wxCoord y ) const
569 {
570 return NULL;
571 }
572
573 bool wxDynamicToolBar::DoInsertTool( size_t pos, wxToolBarToolBase* tool )
574 {
575 return TRUE;
576 }
577
578 bool wxDynamicToolBar::DoDeleteTool( size_t pos, wxToolBarToolBase* tool )
579 {
580 return TRUE;
581 }
582
583 void wxDynamicToolBar::DoEnableTool( wxToolBarToolBase* tool, bool enable )
584 {
585 }
586
587 void wxDynamicToolBar::DoToggleTool( wxToolBarToolBase* tool, bool toggle )
588 {
589 }
590
591 void wxDynamicToolBar::DoSetToggle( wxToolBarToolBase* tool, bool toggle )
592 {
593 }
594
595 wxToolBarToolBase* wxDynamicToolBar::CreateTool( int id, const wxBitmap& bitmap1, const wxBitmap& bitmap2, bool toggle, wxObject* clientData, const wxString& shortHelpString, const wxString& longHelpString )
596 {
597 return NULL;
598 }
599
600 wxToolBarToolBase* wxDynamicToolBar::CreateTool( wxControl* control )
601 {
602 return NULL;
603 }
604