]>
Commit | Line | Data |
---|---|---|
8e08b761 | 1 | ///////////////////////////////////////////////////////////////////////////// |
4cbc57f0 JS |
2 | // Name: dyntbar.cpp |
3 | // Purpose: wxDynamicToolBar implementation | |
8e08b761 JS |
4 | // Author: Aleksandras Gluchovas |
5 | // Modified by: | |
6 | // Created: ??/10/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Aleksandras Gluchovas | |
4cbc57f0 | 9 | // Licence: wxWindows license |
8e08b761 JS |
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 | ||
8e08b761 JS |
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 | ||
4cbc57f0 JS |
36 | EVT_SIZE ( wxDynamicToolBar::OnSize ) |
37 | EVT_PAINT( wxDynamicToolBar::OnPaint ) | |
38 | //EVT_ERASE_BACKGROUND( wxDynamicToolBar::OnEraseBackground ) | |
8e08b761 JS |
39 | |
40 | END_EVENT_TABLE() | |
41 | ||
42 | /***** Implementation for class wxDynToolInfo *****/ | |
4cbc57f0 | 43 | |
8e08b761 JS |
44 | IMPLEMENT_DYNAMIC_CLASS(wxDynToolInfo, wxToolLayoutItem) |
45 | ||
46 | /***** Implementation for class wxDynamicToolBar *****/ | |
47 | ||
48 | wxDynamicToolBar::wxDynamicToolBar() | |
4cbc57f0 JS |
49 | : mpLayoutMan( NULL ), |
50 | mSepartorSize( 8 ), | |
51 | mVertGap ( 0 ), | |
52 | mHorizGap( 0 ) | |
8e08b761 JS |
53 | { |
54 | } | |
55 | ||
56 | wxDynamicToolBar::wxDynamicToolBar(wxWindow *parent, const wxWindowID id, | |
4cbc57f0 JS |
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 ) | |
8e08b761 | 64 | { |
4cbc57f0 | 65 | Create(parent, id, pos, size, style, orientation, RowsOrColumns, name); |
8e08b761 | 66 | |
4cbc57f0 | 67 | SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE) ); |
8e08b761 JS |
68 | } |
69 | ||
70 | bool wxDynamicToolBar::Create(wxWindow *parent, const wxWindowID id, | |
4cbc57f0 JS |
71 | const wxPoint& pos, |
72 | const wxSize& size, | |
73 | const long style, | |
74 | const int orientation, const int RowsOrColumns, | |
75 | const wxString& name) | |
8e08b761 | 76 | { |
4cbc57f0 | 77 | // cut&pasted from wxtbatsmpl.h |
8e08b761 | 78 | |
4cbc57f0 JS |
79 | if ( ! wxWindow::Create(parent, id, pos, size, style, name) ) |
80 | return FALSE; | |
8e08b761 | 81 | |
4cbc57f0 | 82 | SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE )); |
8e08b761 | 83 | |
4cbc57f0 | 84 | return TRUE; |
8e08b761 JS |
85 | } |
86 | ||
87 | bool wxDynamicToolBar::Realize(void) | |
88 | { | |
4cbc57f0 JS |
89 | // FOR NOW:: nothing |
90 | return TRUE; | |
8e08b761 JS |
91 | } |
92 | ||
93 | wxDynamicToolBar::~wxDynamicToolBar(void) | |
94 | { | |
4cbc57f0 | 95 | if ( mpLayoutMan ) |
8e08b761 JS |
96 | delete mpLayoutMan; |
97 | ||
98 | size_t i; | |
4cbc57f0 | 99 | for( i = 0; i != mTools.Count(); ++i ) |
8e08b761 | 100 | { |
4cbc57f0 | 101 | delete mTools[i]; |
8e08b761 JS |
102 | } |
103 | } | |
104 | ||
105 | void wxDynamicToolBar::AddTool( int toolIndex, | |
4cbc57f0 JS |
106 | wxWindow* pToolWindow, |
107 | const wxSize& size | |
108 | ) | |
8e08b761 | 109 | { |
4cbc57f0 | 110 | wxDynToolInfo* pInfo = new wxDynToolInfo(); |
8e08b761 | 111 | |
4cbc57f0 JS |
112 | pInfo->mpToolWnd = pToolWindow; |
113 | pInfo->mIndex = toolIndex; | |
114 | pInfo->mIsSeparator = FALSE; | |
8e08b761 | 115 | |
4cbc57f0 JS |
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; | |
8e08b761 | 122 | |
4cbc57f0 | 123 | mTools.Add( pInfo ); |
8e08b761 JS |
124 | } |
125 | ||
126 | void wxDynamicToolBar::AddTool( int toolIndex, | |
8252c9ca GD |
127 | const wxString& imageFileName, |
128 | wxBitmapType imageFileType, | |
129 | const wxString& labelText, bool alignTextRight, | |
130 | bool isFlat ) | |
8e08b761 | 131 | { |
4cbc57f0 | 132 | wxNewBitmapButton* pBtn = |
8e08b761 | 133 | |
4cbc57f0 JS |
134 | new wxNewBitmapButton( imageFileName, imageFileType, |
135 | labelText, | |
136 | ( alignTextRight ) | |
137 | ? NB_ALIGN_TEXT_RIGHT | |
138 | : NB_ALIGN_TEXT_BOTTOM, | |
139 | isFlat | |
140 | ); | |
8e08b761 | 141 | |
4cbc57f0 | 142 | pBtn->Create( this, toolIndex ); |
8e08b761 | 143 | |
4cbc57f0 JS |
144 | pBtn->Reshape(); |
145 | ||
146 | AddTool( toolIndex, pBtn ); | |
8e08b761 JS |
147 | } |
148 | void wxDynamicToolBar::AddTool( int toolIndex, wxBitmap labelBmp, | |
4cbc57f0 JS |
149 | const wxString& labelText, bool alignTextRight, |
150 | bool isFlat ) | |
8e08b761 | 151 | { |
4cbc57f0 | 152 | wxNewBitmapButton* pBtn = |
8e08b761 | 153 | |
4cbc57f0 JS |
154 | new wxNewBitmapButton( labelBmp, |
155 | labelText, | |
156 | ( alignTextRight ) | |
157 | ? NB_ALIGN_TEXT_RIGHT | |
158 | : NB_ALIGN_TEXT_BOTTOM, | |
159 | isFlat | |
160 | ); | |
8e08b761 | 161 | |
4cbc57f0 | 162 | pBtn->Create( this, toolIndex ); |
8e08b761 | 163 | |
4cbc57f0 JS |
164 | pBtn->Reshape(); |
165 | ||
166 | AddTool( toolIndex, pBtn ); | |
8e08b761 JS |
167 | } |
168 | ||
169 | ||
4cbc57f0 JS |
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) | |
8e08b761 | 176 | { |
4cbc57f0 | 177 | wxNewBitmapButton* pBmpBtn = new wxNewBitmapButton( bitmap ); |
8e08b761 | 178 | |
4cbc57f0 | 179 | pBmpBtn->Create( this, toolIndex ); |
8e08b761 | 180 | |
4cbc57f0 | 181 | pBmpBtn->Reshape(); |
8e08b761 | 182 | |
4cbc57f0 | 183 | AddTool( toolIndex, pBmpBtn ); |
8e08b761 | 184 | |
4cbc57f0 | 185 | return NULL; |
8e08b761 JS |
186 | } |
187 | ||
188 | ||
189 | wxDynToolInfo* wxDynamicToolBar::GetToolInfo( int toolIndex ) | |
190 | { | |
191 | size_t i; | |
4cbc57f0 | 192 | for( i = 0; i != mTools.Count(); ++i ) |
8e08b761 | 193 | { |
4cbc57f0 | 194 | if ( mTools[i]->mIndex == toolIndex ) |
8e08b761 JS |
195 | return mTools[i]; |
196 | } | |
197 | ||
4cbc57f0 | 198 | return NULL; |
8e08b761 JS |
199 | } |
200 | ||
201 | void wxDynamicToolBar::RemveTool( int toolIndex ) | |
202 | { | |
203 | size_t i; | |
4cbc57f0 | 204 | for( i = 0; i != mTools.Count(); ++i ) |
8e08b761 | 205 | { |
4cbc57f0 JS |
206 | if ( mTools[i]->mIndex == toolIndex ) |
207 | { | |
208 | if ( mTools[i]->mpToolWnd ) | |
209 | { | |
210 | mTools[i]->mpToolWnd->Destroy(); | |
211 | } | |
8e08b761 JS |
212 | delete mTools[i]; // HVL To be tested!!! |
213 | #if wxCHECK_VERSION(2,3,2) | |
4cbc57f0 | 214 | mTools.RemoveAt(i); |
8e08b761 | 215 | #else |
4cbc57f0 | 216 | mTools.Remove(i); |
8e08b761 | 217 | #endif |
4cbc57f0 | 218 | Layout(); |
8e08b761 | 219 | |
4cbc57f0 JS |
220 | return; |
221 | } | |
8e08b761 | 222 | } |
4cbc57f0 | 223 | // TODO:: if not found, should it be an assertion? |
8e08b761 JS |
224 | } |
225 | ||
226 | void wxDynamicToolBar::AddSeparator( wxWindow* pSepartorWnd ) | |
227 | { | |
4cbc57f0 JS |
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 ); | |
8e08b761 JS |
256 | } |
257 | ||
258 | void wxDynamicToolBar::OnEraseBackground( wxEraseEvent& event ) | |
259 | { | |
4cbc57f0 | 260 | // FOR NOW:: nothing |
8e08b761 JS |
261 | } |
262 | ||
263 | void wxDynamicToolBar::OnSize( wxSizeEvent& event ) | |
264 | { | |
4cbc57f0 | 265 | //SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ) ); |
8e08b761 | 266 | |
4cbc57f0 | 267 | Layout(); |
8e08b761 JS |
268 | } |
269 | ||
270 | void wxDynamicToolBar::DrawSeparator( wxDynToolInfo& info, wxDC& dc ) | |
271 | { | |
4cbc57f0 JS |
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 | } | |
8e08b761 JS |
297 | } |
298 | ||
299 | void wxDynamicToolBar::OnPaint( wxPaintEvent& event ) | |
300 | { | |
4cbc57f0 | 301 | // draw separators if any |
8e08b761 | 302 | |
4cbc57f0 | 303 | wxPaintDC dc(this); |
8e08b761 JS |
304 | |
305 | size_t i; | |
4cbc57f0 JS |
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 | |
8e08b761 | 312 | |
4cbc57f0 | 313 | if ( !mTools[i]->mpToolWnd ) |
8e08b761 | 314 | |
4cbc57f0 JS |
315 | DrawSeparator( *mTools[i], dc ); |
316 | } | |
8e08b761 JS |
317 | } |
318 | ||
319 | // FOR NOW:: quick fix | |
320 | #include "wx/choice.h" | |
321 | ||
322 | void wxDynamicToolBar::SizeToolWindows() | |
323 | { | |
324 | size_t i; | |
4cbc57f0 JS |
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 | } | |
8e08b761 JS |
353 | } |
354 | ||
355 | bool wxDynamicToolBar::Layout() | |
356 | { | |
4cbc57f0 JS |
357 | if ( !mpLayoutMan ) |
358 | mpLayoutMan = CreateDefaultLayout(); | |
8e08b761 | 359 | |
4cbc57f0 JS |
360 | int x,y; |
361 | GetSize( &x, &y ); | |
362 | wxSize wndDim(x,y); | |
363 | wxSize result; | |
8e08b761 | 364 | |
4cbc57f0 | 365 | wxLayoutItemArrayT items; |
8e08b761 | 366 | |
4cbc57f0 | 367 | // safe conversion |
8e08b761 | 368 | size_t i; |
4cbc57f0 | 369 | for( i = 0; i != mTools.Count(); ++i ) |
8e08b761 JS |
370 | items.Add( mTools[i] ); |
371 | ||
4cbc57f0 | 372 | mpLayoutMan->Layout( wndDim, result, items, mVertGap, mHorizGap );; |
8e08b761 | 373 | |
4cbc57f0 | 374 | SizeToolWindows(); |
8e08b761 JS |
375 | return TRUE; |
376 | } | |
377 | ||
378 | void wxDynamicToolBar::GetPreferredDim( const wxSize& givenDim, wxSize& prefDim ) | |
379 | { | |
4cbc57f0 JS |
380 | if ( !mpLayoutMan ) |
381 | mpLayoutMan = CreateDefaultLayout(); | |
8e08b761 | 382 | |
4cbc57f0 | 383 | wxLayoutItemArrayT items; |
8e08b761 | 384 | |
4cbc57f0 | 385 | // safe conversion |
8e08b761 | 386 | size_t i; |
4cbc57f0 | 387 | for( i = 0; i != mTools.Count(); ++i ) |
8e08b761 JS |
388 | items.Add( mTools[i] ); |
389 | ||
4cbc57f0 | 390 | mpLayoutMan->Layout( givenDim, prefDim, items, mVertGap, mHorizGap );; |
8e08b761 JS |
391 | } |
392 | ||
393 | void wxDynamicToolBar::SetLayout( LayoutManagerBase* pLayout ) | |
394 | { | |
4cbc57f0 | 395 | if ( mpLayoutMan ) |
8e08b761 JS |
396 | delete mpLayoutMan; |
397 | ||
4cbc57f0 | 398 | mpLayoutMan = pLayout; |
8e08b761 | 399 | |
4cbc57f0 | 400 | Layout(); |
8e08b761 JS |
401 | } |
402 | ||
403 | void wxDynamicToolBar::EnableTool(const int toolIndex, const bool enable ) | |
404 | { | |
4cbc57f0 | 405 | wxDynToolInfo* pInfo = GetToolInfo( toolIndex ); |
8e08b761 | 406 | |
4cbc57f0 | 407 | if ( !pInfo ) |
8e08b761 JS |
408 | return; |
409 | ||
4cbc57f0 | 410 | if ( pInfo->mIsSeparator || !pInfo->mpToolWnd ) |
8e08b761 JS |
411 | return; |
412 | ||
4cbc57f0 | 413 | pInfo->mpToolWnd->Enable( enable ); |
8e08b761 JS |
414 | } |
415 | ||
416 | /***** Implementation for class BagLayout *****/ | |
417 | ||
418 | void BagLayout::Layout( const wxSize& parentDim, | |
4cbc57f0 JS |
419 | wxSize& resultingDim, |
420 | wxLayoutItemArrayT& items, | |
421 | int horizGap, | |
422 | int vertGap | |
423 | ) | |
8e08b761 | 424 | { |
4cbc57f0 JS |
425 | int maxWidth = 0; |
426 | int curY = 0; | |
427 | int nRows = 0; | |
8e08b761 | 428 | |
4cbc57f0 | 429 | size_t i = 0; |
8e08b761 | 430 | |
4cbc57f0 JS |
431 | while( i < items.Count() ) |
432 | { | |
433 | int curX = 0; | |
434 | int height = 0; | |
435 | // int nItems = 0; | |
8e08b761 | 436 | |
4cbc57f0 JS |
437 | // int firstItem = i; |
438 | int itemsInRow = 0; | |
8e08b761 | 439 | |
4cbc57f0 | 440 | if ( nRows > 0 ) |
8e08b761 JS |
441 | curY += vertGap; |
442 | ||
4cbc57f0 | 443 | // step #1 - arrange horizontal positions of items in the row |
8e08b761 | 444 | |
4cbc57f0 JS |
445 | do |
446 | { | |
447 | if ( itemsInRow > 0 ) | |
8e08b761 JS |
448 | curX += horizGap; |
449 | ||
4cbc57f0 | 450 | wxRect& r = items[i]->mRect; |
8e08b761 | 451 | |
4cbc57f0 | 452 | if ( curX + r.width > parentDim.x ) |
8e08b761 | 453 | { |
4cbc57f0 | 454 | if ( itemsInRow > 0 ) |
8e08b761 JS |
455 | break; |
456 | } | |
4cbc57f0 JS |
457 | r.x = curX; |
458 | r.y = curY; | |
8e08b761 | 459 | |
4cbc57f0 | 460 | curX += r.width; |
8e08b761 | 461 | |
4cbc57f0 | 462 | height = wxMax( height, r.height ); |
8e08b761 | 463 | |
4cbc57f0 JS |
464 | ++itemsInRow; |
465 | ++i; | |
8e08b761 | 466 | |
4cbc57f0 | 467 | } while( i < items.Count() ); |
8e08b761 | 468 | |
4cbc57f0 | 469 | curY += height; |
8e08b761 | 470 | |
4cbc57f0 JS |
471 | maxWidth = wxMax( maxWidth, curX ); |
472 | } | |
8e08b761 | 473 | |
4cbc57f0 JS |
474 | resultingDim.x = maxWidth; |
475 | resultingDim.y = curY; | |
8e08b761 JS |
476 | } |
477 | ||
478 | //////// stuff from 2.1.15 /////////// | |
479 | ||
480 | wxToolBarToolBase* wxDynamicToolBar::FindToolForPosition( wxCoord x, wxCoord y ) const | |
481 | { | |
4cbc57f0 | 482 | return NULL; |
8e08b761 JS |
483 | } |
484 | ||
485 | bool wxDynamicToolBar::DoInsertTool( size_t pos, wxToolBarToolBase* tool ) | |
486 | { | |
4cbc57f0 | 487 | return TRUE; |
8e08b761 JS |
488 | } |
489 | ||
490 | bool wxDynamicToolBar::DoDeleteTool( size_t pos, wxToolBarToolBase* tool ) | |
491 | { | |
4cbc57f0 | 492 | return TRUE; |
8e08b761 JS |
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 | { | |
4cbc57f0 | 509 | return NULL; |
8e08b761 JS |
510 | } |
511 | ||
512 | wxToolBarToolBase* wxDynamicToolBar::CreateTool( wxControl* control ) | |
513 | { | |
4cbc57f0 | 514 | return NULL; |
8e08b761 JS |
515 | } |
516 |