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