]> git.saurik.com Git - wxWidgets.git/blame - src/os2/toolbar.cpp
using new API (no visual difference)
[wxWidgets.git] / src / os2 / toolbar.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: toolbar.cpp
3// Purpose: wxToolBar
d90895ac 4// Author: David Webster
0e320a79 5// Modified by:
d697657f 6// Created: 06/30/02
0e320a79 7// RCS-ID: $Id$
d90895ac 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
d90895ac
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
0e320a79 14
d697657f
DW
15#if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE
16
d90895ac 17#ifndef WX_PRECOMP
d697657f
DW
18 #include "wx/settings.h"
19 #include "wx/window.h"
272ebf16
SN
20 #include "wx/frame.h"
21 #include "wx/app.h"
d697657f
DW
22 #include "wx/dcclient.h"
23 #include "wx/dcmemory.h"
d90895ac
DW
24#endif
25
05facebb 26#include "wx/tooltip.h"
b7c2b5c5 27#include "wx/toolbar.h"
d90895ac 28
d697657f 29bool wxToolBar::m_bInitialized = FALSE;
d90895ac 30
b7c2b5c5
DW
31// ----------------------------------------------------------------------------
32// private classes
33// ----------------------------------------------------------------------------
0e320a79 34
b7c2b5c5 35class wxToolBarTool : public wxToolBarToolBase
0e320a79 36{
b7c2b5c5
DW
37public:
38 inline wxToolBarTool( wxToolBar* pTbar
39 ,int vId
3437f881 40 ,const wxString& rsLabel
b7c2b5c5
DW
41 ,const wxBitmap& rBitmap1
42 ,const wxBitmap& rBitmap2
3437f881 43 ,wxItemKind vKind
b7c2b5c5 44 ,wxObject* pClientData
d697657f
DW
45 ,const wxString& rsShortHelpString
46 ,const wxString& rsLongHelpString
b7c2b5c5
DW
47 ) : wxToolBarToolBase( pTbar
48 ,vId
3437f881 49 ,rsLabel
b7c2b5c5
DW
50 ,rBitmap1
51 ,rBitmap2
3437f881 52 ,vKind
b7c2b5c5 53 ,pClientData
d697657f
DW
54 ,rsShortHelpString
55 ,rsLongHelpString
b7c2b5c5
DW
56 )
57 {
b7c2b5c5
DW
58 }
59
60 inline wxToolBarTool( wxToolBar* pTbar
61 ,wxControl* pControl
62 ) : wxToolBarToolBase( pTbar
63 ,pControl
64 )
65 {
b7c2b5c5
DW
66 }
67
d697657f
DW
68 void SetSize(const wxSize& rSize)
69 {
70 m_vWidth = rSize.x;
71 m_vHeight = rSize.y;
72 }
73
74 wxCoord GetWidth(void) const { return m_vWidth; }
75 wxCoord GetHeight(void) const { return m_vHeight; }
b7c2b5c5 76
d697657f
DW
77 wxCoord m_vX;
78 wxCoord m_vY;
79 wxCoord m_vWidth;
80 wxCoord m_vHeight;
81}; // end of CLASS wxToolBarTool
b7c2b5c5 82
d697657f
DW
83// ----------------------------------------------------------------------------
84// wxWin macros
85// ----------------------------------------------------------------------------
86
a04be86e 87IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
d697657f
DW
88
89BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
90 EVT_SIZE(wxToolBar::OnSize)
91 EVT_PAINT(wxToolBar::OnPaint)
92 EVT_KILL_FOCUS(wxToolBar::OnKillFocus)
93 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
233d6db5 94 EVT_TIMER(-1, wxToolBar::OnTimer)
d697657f 95END_EVENT_TABLE()
b7c2b5c5
DW
96
97// ============================================================================
98// implementation
99// ============================================================================
100
101// ----------------------------------------------------------------------------
d697657f 102// tool bar tools creation
b7c2b5c5
DW
103// ----------------------------------------------------------------------------
104
105wxToolBarToolBase* wxToolBar::CreateTool(
106 int nId
3437f881 107, const wxString& rsLabel
d697657f
DW
108, const wxBitmap& rBmpNormal
109, const wxBitmap& rBmpDisabled
110, wxItemKind eKind
b7c2b5c5 111, wxObject* pClientData
d697657f
DW
112, const wxString& rsShortHelp
113, const wxString& rsLongHelp
b7c2b5c5
DW
114)
115{
d697657f 116 return new wxToolBarTool( this
b7c2b5c5 117 ,nId
3437f881 118 ,rsLabel
d697657f
DW
119 ,rBmpNormal
120 ,rBmpDisabled
121 ,eKind
b7c2b5c5 122 ,pClientData
d697657f
DW
123 ,rsShortHelp
124 ,rsLongHelp
125 );
126} // end of wxToolBarSimple::CreateTool
0e320a79 127
d697657f 128wxToolBarToolBase *wxToolBar::CreateTool(
b7c2b5c5
DW
129 wxControl* pControl
130)
0e320a79 131{
d697657f 132 return new wxToolBarTool( this
b7c2b5c5 133 ,pControl
d697657f
DW
134 );
135} // end of wxToolBarSimple::CreateTool
b7c2b5c5
DW
136
137// ----------------------------------------------------------------------------
d697657f 138// wxToolBarSimple creation
b7c2b5c5
DW
139// ----------------------------------------------------------------------------
140
141void wxToolBar::Init()
142{
d697657f 143 m_nCurrentRowsOrColumns = 0;
b7c2b5c5 144
d697657f
DW
145 m_vLastX = m_vLastY = 0;
146 m_vMaxWidth = m_vMaxHeight = 0;
147 m_nPressedTool = m_nCurrentTool = -1;
148 m_vXPos = m_vYPos = -1;
149 m_vTextX = m_vTextY = 0;
d90895ac 150
d697657f
DW
151 m_toolPacking = 1;
152 m_toolSeparation = 5;
b7c2b5c5 153
d697657f
DW
154 m_defaultWidth = 16;
155 m_defaultHeight = 15;
05facebb
DW
156
157 m_pToolTip = NULL;
d697657f 158} // end of wxToolBar::Init
b7c2b5c5 159
d697657f
DW
160wxToolBarToolBase* wxToolBar::DoAddTool(
161 int vId
162, const wxString& rsLabel
163, const wxBitmap& rBitmap
164, const wxBitmap& rBmpDisabled
165, wxItemKind eKind
166, const wxString& rsShortHelp
167, const wxString& rsLongHelp
168, wxObject* pClientData
169, wxCoord vXPos
170, wxCoord vYPos
171)
172{
173 //
174 // Rememeber the position for DoInsertTool()
175 //
176 m_vXPos = vXPos;
177 m_vYPos = vYPos;
178
179 return wxToolBarBase::DoAddTool( vId
180 ,rsLabel
181 ,rBitmap
182 ,rBmpDisabled
183 ,eKind
184 ,rsShortHelp
185 ,rsLongHelp
186 ,pClientData
187 ,vXPos
188 ,vYPos
189 );
190} // end of wxToolBar::DoAddTool
b7c2b5c5 191
3029781e
DW
192bool wxToolBar::DeleteTool(
193 int nId
194)
195{
196 bool bOk = wxToolBarBase::DeleteTool(nId);
197
198 if (bOk)
199 {
200 Realize();
201 }
202 return bOk;
203} // end of wxToolBar::DeleteTool
204
205bool wxToolBar::DeleteToolByPos(
206 size_t nPos
207)
208{
209 bool bOk = wxToolBarBase::DeleteToolByPos(nPos);
210
211 if (bOk)
212 {
213 Realize();
214 }
215 return bOk;
216} // end of wxToolBar::DeleteTool
217
218wxToolBarToolBase* wxToolBar::InsertControl(
219 size_t nPos
220, wxControl* pControl
221)
222{
223 wxToolBarToolBase* pTool = wxToolBarBase::InsertControl( nPos
224 ,pControl
225 );
226 if (m_bInitialized)
227 {
228 Realize();
229 Refresh();
230 }
231 return pTool;
232} // end of wxToolBar::InsertControl
233
234wxToolBarToolBase* wxToolBar::InsertSeparator(
235 size_t nPos
236)
237{
238 wxToolBarToolBase* pTool = wxToolBarBase::InsertSeparator(nPos);
239
240 if (m_bInitialized)
241 {
242 Realize();
243 Refresh();
244 }
245 return pTool;
246} // end of wxToolBar::InsertSeparator
247
248wxToolBarToolBase* wxToolBar::InsertTool(
249 size_t nPos
250, int nId
251, const wxString& rsLabel
252, const wxBitmap& rBitmap
253, const wxBitmap& rBmpDisabled
254, wxItemKind eKind
255, const wxString& rsShortHelp
256, const wxString& rsLongHelp
257, wxObject* pClientData
258)
259{
260 wxToolBarToolBase* pTool = wxToolBarBase::InsertTool( nPos
261 ,nId
262 ,rsLabel
263 ,rBitmap
264 ,rBmpDisabled
265 ,eKind
266 ,rsShortHelp
267 ,rsLongHelp
268 ,pClientData
269 );
270 if (m_bInitialized)
271 {
272 Realize();
273 Refresh();
274 }
275 return pTool;
276} // end of wxToolBar::InsertTool
277
d697657f
DW
278bool wxToolBar::DoInsertTool(
279 size_t WXUNUSED(nPos)
280, wxToolBarToolBase* pToolBase
281)
282{
283 wxToolBarTool* pTool = (wxToolBarTool *)pToolBase;
b7c2b5c5 284
d697657f
DW
285 pTool->m_vX = m_vXPos;
286 if (pTool->m_vX == -1)
287 pTool->m_vX = m_xMargin;
b7c2b5c5 288
d697657f
DW
289 pTool->m_vY = m_vYPos;
290 if (pTool->m_vY == -1)
291 pTool->m_vX = m_yMargin;
b7c2b5c5 292
d697657f 293 pTool->SetSize(GetToolSize());
0e320a79 294
d697657f 295 if (pTool->IsButton())
b7c2b5c5 296 {
d697657f
DW
297 //
298 // Calculate reasonable max size in case Layout() not called
299 //
300 if ((pTool->m_vX + pTool->GetNormalBitmap().GetWidth() + m_xMargin) > m_vMaxWidth)
301 m_vMaxWidth = (wxCoord)((pTool->m_vX + pTool->GetWidth() + m_xMargin));
302
303 if ((pTool->m_vY + pTool->GetNormalBitmap().GetHeight() + m_yMargin) > m_vMaxHeight)
304 m_vMaxHeight = (wxCoord)((pTool->m_vY + pTool->GetHeight() + m_yMargin));
b7c2b5c5 305 }
d697657f
DW
306 return TRUE;
307} // end of wxToolBar::DoInsertTool
b7c2b5c5 308
d697657f 309bool wxToolBar::DoDeleteTool(
b7c2b5c5
DW
310 size_t WXUNUSED(nPos)
311, wxToolBarToolBase* pTool
312)
313{
d697657f
DW
314 pTool->Detach();
315 Refresh();
316 return TRUE;
317} // end of wxToolBar::DoDeleteTool
0e320a79 318
d697657f
DW
319bool wxToolBar::Create(
320 wxWindow* pParent
321, wxWindowID vId
322, const wxPoint& rPos
323, const wxSize& rSize
324, long lStyle
325, const wxString& rsName
b7c2b5c5 326)
0e320a79 327{
d697657f
DW
328 if ( !wxWindow::Create( pParent
329 ,vId
330 ,rPos
331 ,rSize
332 ,lStyle
333 ,rsName
334 ))
335 return FALSE;
d90895ac 336
d697657f
DW
337 // Set it to grey (or other 3D face colour)
338 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
339 SetFont(*wxSMALL_FONT);
d90895ac 340
d697657f 341 if (GetWindowStyleFlag() & wxTB_VERTICAL)
b7c2b5c5 342 {
d697657f
DW
343 m_vLastX = 7;
344 m_vLastY = 3;
d90895ac 345
d697657f
DW
346 m_maxRows = 32000; // a lot
347 m_maxCols = 1;
348 }
349 else
350 {
351 m_vLastX = 3;
352 m_vLastY = 7;
d90895ac 353
d697657f
DW
354 m_maxRows = 1;
355 m_maxCols = 32000; // a lot
356 }
357 SetCursor(*wxSTANDARD_CURSOR);
358
359 //
360 // The toolbar's tools, if they have labels and the winTB_TEXT
361 // style is set, then we need to take into account the size of
362 // the text when drawing tool bitmaps and the text
363 //
364 if (HasFlag(wxTB_TEXT))
b7c2b5c5 365 {
d697657f 366 wxClientDC vDC(this);
d90895ac 367
d697657f 368 vDC.SetFont(GetFont());
0fba44b4 369 vDC.GetTextExtent( wxT("XXXX")
d697657f
DW
370 ,&m_vTextX
371 ,&m_vTextY
372 );
b7c2b5c5
DW
373 }
374
d697657f
DW
375 //
376 // Position it
377 //
378 int nX = rPos.x;
379 int nY = rPos.y;
380 int nWidth = rSize.x;
381 int nHeight = rSize.y;
d697657f
DW
382
383 if (lStyle & wxTB_HORIZONTAL)
d90895ac 384 {
d697657f 385 if (nWidth <= 0)
b7c2b5c5 386 {
d697657f
DW
387 nWidth = pParent->GetClientSize().x;
388 }
389 if (nHeight <= 0)
390 {
391 if (lStyle & wxTB_TEXT)
3029781e 392 nHeight = m_defaultHeight + m_vTextY;
d697657f
DW
393 else
394 nHeight = m_defaultHeight;
b7c2b5c5 395 }
d90895ac 396 }
d697657f 397 else
d90895ac 398 {
d697657f 399 if (nHeight <= 0)
b7c2b5c5 400 {
d697657f
DW
401 nHeight = pParent->GetClientSize().y;
402 }
403 if (nWidth <= 0)
404 {
405 if (lStyle & wxTB_TEXT)
406 nWidth = m_vTextX + (int)(m_vTextX/2); // a little margin
407 else
408 nWidth = m_defaultWidth + (int)(m_defaultWidth/2); // a little margin
b7c2b5c5 409 }
d90895ac 410 }
d697657f
DW
411 if (nX < 0)
412 nX = 0;
413 if (nY < 0)
414 nY = 0;
415
416 SetSize( nX
417 ,nY
418 ,nWidth
419 ,nHeight
420 );
421 return TRUE;
422} // end of wxToolBar::Create
d90895ac 423
d697657f 424wxToolBar::~wxToolBar()
b7c2b5c5 425{
05facebb
DW
426 if (m_pToolTip)
427 {
428 delete m_pToolTip;
429 m_pToolTip = NULL;
430 }
d697657f 431} // end of wxToolBar::~wxToolBar
d90895ac 432
d697657f
DW
433bool wxToolBar::Realize()
434{
435 int nMaxToolWidth = 0;
436 int nMaxToolHeight = 0;
b7c2b5c5 437
d697657f
DW
438 m_nCurrentRowsOrColumns = 0;
439 m_vLastX = m_xMargin;
440 m_vLastY = m_yMargin;
441 m_vMaxWidth = 0;
442 m_vMaxHeight = 0;
b7c2b5c5 443
b7c2b5c5 444
d697657f
DW
445 //
446 // Find the maximum tool width and height
447 //
2461cfa0 448 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
b7c2b5c5 449
2461cfa0 450 while (node )
d90895ac 451 {
2461cfa0 452 wxToolBarTool* pTool = (wxToolBarTool *)node->GetData();
b7c2b5c5 453
6670f564 454 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().empty())
d697657f
DW
455 {
456 //
457 // Set the height according to the font and the border size
458 //
3029781e
DW
459 if (pTool->GetWidth() > m_vTextX)
460 nMaxToolWidth = pTool->GetWidth() + 4;
461 else
462 nMaxToolWidth = m_vTextX;
d697657f
DW
463 if (pTool->GetHeight() + m_vTextY > nMaxToolHeight)
464 nMaxToolHeight = pTool->GetHeight() + m_vTextY;
465 }
466 else
467 {
468 if (pTool->GetWidth() > nMaxToolWidth )
3029781e 469 nMaxToolWidth = pTool->GetWidth() + 4;
d697657f
DW
470 if (pTool->GetHeight() > nMaxToolHeight)
471 nMaxToolHeight = pTool->GetHeight();
472 }
2461cfa0 473 node = node->GetNext();
d90895ac 474 }
d90895ac 475
3029781e
DW
476 wxCoord vTbWidth = 0L;
477 wxCoord vTbHeight = 0L;
478
479 GetSize( &vTbWidth
480 ,&vTbHeight
481 );
482 if (vTbHeight < nMaxToolHeight)
483 {
484 SetSize( -1L
485 ,-1L
486 ,vTbWidth
487 ,nMaxToolHeight + 4
488 );
489 if (GetParent()->IsKindOf(CLASSINFO(wxFrame)))
490 {
491 wxFrame* pFrame = wxDynamicCast(GetParent(), wxFrame);
492
493 if (pFrame)
494 pFrame->PositionToolBar();
495 }
496 }
497
d697657f 498 int nSeparatorSize = m_toolSeparation;
b7c2b5c5 499
2461cfa0
SN
500 node = m_tools.GetFirst();
501 while (node)
d90895ac 502 {
2461cfa0 503 wxToolBarTool* pTool = (wxToolBarTool *)node->GetData();
d697657f
DW
504
505 if (pTool->IsSeparator())
b7c2b5c5 506 {
d697657f 507 if (GetWindowStyleFlag() & wxTB_HORIZONTAL)
b7c2b5c5 508 {
d697657f
DW
509 pTool->m_vX = m_vLastX + nSeparatorSize;
510 pTool->m_vHeight = m_defaultHeight + m_vTextY;
511 if (m_nCurrentRowsOrColumns >= m_maxCols)
512 m_vLastY += nSeparatorSize;
513 else
3029781e 514 m_vLastX += nSeparatorSize * 4;
b7c2b5c5
DW
515 }
516 else
517 {
d697657f
DW
518 pTool->m_vY = m_vLastY + nSeparatorSize;
519 pTool->m_vHeight = m_defaultHeight + m_vTextY;
520 if (m_nCurrentRowsOrColumns >= m_maxRows)
521 m_vLastX += nSeparatorSize;
522 else
3029781e 523 m_vLastY += nSeparatorSize * 4;
b7c2b5c5 524 }
b7c2b5c5 525 }
d697657f 526 else if (pTool->IsButton())
b7c2b5c5 527 {
d697657f
DW
528 if (GetWindowStyleFlag() & wxTB_HORIZONTAL)
529 {
530 if (m_nCurrentRowsOrColumns >= m_maxCols)
531 {
532 m_nCurrentRowsOrColumns = 0;
533 m_vLastX = m_xMargin;
534 m_vLastY += nMaxToolHeight + m_toolPacking;
535 }
536 pTool->m_vX = m_vLastX + (nMaxToolWidth - ((int)(nMaxToolWidth/2) + (int)(pTool->GetWidth()/2)));
537 if (HasFlag(wxTB_TEXT))
538 pTool->m_vY = m_vLastY + nSeparatorSize - 2; // just bit of adjustment
539 else
540 pTool->m_vY = m_vLastY + (nMaxToolHeight - (int)(pTool->GetHeight()/2));
541 m_vLastX += nMaxToolWidth + m_toolPacking + m_toolSeparation;
542 }
543 else
b7c2b5c5 544 {
d697657f
DW
545 if (m_nCurrentRowsOrColumns >= m_maxRows)
546 {
547 m_nCurrentRowsOrColumns = 0;
548 m_vLastX += (nMaxToolWidth + m_toolPacking);
549 m_vLastY = m_yMargin;
550 }
551 pTool->m_vX = m_vLastX + pTool->GetWidth();
552 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
553 pTool->m_vY = m_vLastY + (nMaxToolHeight - m_vTextY) + m_toolPacking;
554 else
555 pTool->m_vY = m_vLastY + (nMaxToolHeight - (int)(pTool->GetHeight()/2));
556 m_vLastY += nMaxToolHeight + m_toolPacking + m_toolSeparation;
b7c2b5c5 557 }
d697657f 558 m_nCurrentRowsOrColumns++;
b7c2b5c5 559 }
d697657f 560 else
b7c2b5c5 561 {
d697657f 562 // TODO: support the controls
b7c2b5c5 563 }
d697657f
DW
564
565 if (m_vLastX > m_maxWidth)
566 m_maxWidth = m_vLastX;
567 if (m_vLastY > m_maxHeight)
568 m_maxHeight = m_vLastY;
569
2461cfa0 570 node = node->GetNext();
d90895ac
DW
571 }
572
d697657f
DW
573 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
574 m_maxWidth += nMaxToolWidth;
575 else
576 m_maxHeight += nMaxToolHeight;
d90895ac 577
d697657f
DW
578 m_maxWidth += m_xMargin;
579 m_maxHeight += m_yMargin;
3029781e 580 m_bInitialized = TRUE;
d697657f
DW
581 return TRUE;
582} // end of wxToolBar::Realize
d90895ac 583
d697657f
DW
584// ----------------------------------------------------------------------------
585// event handlers
586// ----------------------------------------------------------------------------
d90895ac 587
d697657f
DW
588void wxToolBar::OnPaint (
589 wxPaintEvent& WXUNUSED(rEvent)
590)
591{
592 wxPaintDC vDc(this);
b7c2b5c5 593
d697657f 594 PrepareDC(vDc);
d90895ac 595
d697657f 596 static int nCount = 0;
b7c2b5c5 597
d697657f
DW
598 //
599 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
600 //
601 if (nCount > 0)
602 return;
603 nCount++;
b7c2b5c5 604
b856b0f5 605 ::WinFillRect(vDc.GetHPS(), &vDc.m_vRclPaint, GetBackgroundColour().GetPixel());
2461cfa0
SN
606 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
607 node;
608 node = node->GetNext() )
d697657f 609 {
2461cfa0 610 wxToolBarTool* pTool = (wxToolBarTool*)node->GetData();
b7c2b5c5 611
d697657f
DW
612 if (pTool->IsButton() )
613 DrawTool(vDc, pTool);
614 if (pTool->IsSeparator())
b7c2b5c5 615 {
6670f564
WS
616 wxColour gray85(85, 85, 85);
617 wxPen vDarkGreyPen( gray85, 1, wxSOLID );
618 int nX;
619 int nY;
620 int nHeight = 0;
621 int nWidth = 0;
d697657f
DW
622
623 vDc.SetPen(vDarkGreyPen);
97d74dd2 624 if (HasFlag(wxTB_TEXT))
d697657f 625 {
97d74dd2
DW
626 if (HasFlag(wxTB_HORIZONTAL))
627 {
628 nX = pTool->m_vX;
629 nY = pTool->m_vY - (m_vTextY - 6);
630 nHeight = (m_vTextY - 2) + pTool->GetHeight();
631 }
632 else
633 {
634 nX = pTool->m_vX + m_xMargin + 10;
635 nY = pTool->m_vY + m_vTextY + m_toolSeparation;
636 nWidth = pTool->GetWidth() > m_vTextX ? pTool->GetWidth() : m_vTextX;
637 }
d697657f
DW
638 }
639 else
640 {
641 nX = pTool->m_vX;
642 nY = pTool->m_vY;
97d74dd2
DW
643 if (HasFlag(wxTB_HORIZONTAL))
644 nHeight = pTool->GetHeight() - 2;
645 else
646 {
647 nX += m_xMargin + 10;
648 nY += m_yMargin + m_toolSeparation;
649 nWidth = pTool->GetWidth();
650 }
d697657f 651 }
97d74dd2 652 vDc.DrawLine(nX, nY, nX + nWidth, nY + nHeight);
d697657f
DW
653 }
654 }
655 nCount--;
656} // end of wxToolBar::OnPaint
b7c2b5c5 657
d697657f
DW
658void wxToolBar::OnSize (
659 wxSizeEvent& WXUNUSED(rEvent)
660)
661{
662#if wxUSE_CONSTRAINTS
663 if (GetAutoLayout())
664 Layout();
665#endif
666} // end of wxToolBar::OnSize
b7c2b5c5 667
d697657f
DW
668void wxToolBar::OnKillFocus(
669 wxFocusEvent& WXUNUSED(rEvent)
670)
671{
672 OnMouseEnter(m_nPressedTool = m_nCurrentTool = -1);
673} // end of wxToolBar::OnKillFocus
b7c2b5c5 674
d697657f
DW
675void wxToolBar::OnMouseEvent(
676 wxMouseEvent& rEvent
677)
678{
05facebb
DW
679 POINTL vPoint;
680 HWND hWnd;
d697657f
DW
681 wxCoord vX;
682 wxCoord vY;
683 HPOINTER hPtr = ::WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);
b7c2b5c5 684
d697657f 685 ::WinSetPointer(HWND_DESKTOP, hPtr);
05facebb
DW
686 ::WinQueryPointerPos(HWND_DESKTOP, &vPoint);
687 hWnd = ::WinWindowFromPoint(HWND_DESKTOP, &vPoint, TRUE);
688 if (hWnd != (HWND)GetHwnd())
689 {
690 m_vToolTimer.Stop();
691 return;
692 }
693
d697657f 694 rEvent.GetPosition(&vX, &vY);
b7c2b5c5 695
d697657f
DW
696 wxToolBarTool* pTool = (wxToolBarTool *)FindToolForPosition( vX
697 ,vY
698 );
b7c2b5c5 699
d697657f
DW
700 if (rEvent.LeftDown())
701 {
702 CaptureMouse();
b7c2b5c5 703 }
d697657f 704 if (rEvent.LeftUp())
b7c2b5c5 705 {
d697657f 706 ReleaseMouse();
b7c2b5c5
DW
707 }
708
d697657f 709 if (!pTool)
b7c2b5c5 710 {
05facebb 711 m_vToolTimer.Stop();
d697657f 712 if (m_nCurrentTool > -1)
b7c2b5c5 713 {
d697657f
DW
714 if (rEvent.LeftIsDown())
715 SpringUpButton(m_nCurrentTool);
716 pTool = (wxToolBarTool *)FindById(m_nCurrentTool);
3029781e 717 if (pTool && !pTool->IsToggled())
b7c2b5c5 718 {
d697657f
DW
719 RaiseTool( pTool
720 ,FALSE
721 );
b7c2b5c5 722 }
d697657f
DW
723 m_nCurrentTool = -1;
724 OnMouseEnter(-1);
725 }
726 return;
727 }
728 if (!rEvent.IsButton())
729 {
730 if (pTool->GetId() != m_nCurrentTool)
731 {
732 //
733 // If the left button is kept down and moved over buttons,
734 // press those buttons.
735 //
736 if (rEvent.LeftIsDown() && pTool->IsEnabled())
b7c2b5c5 737 {
d697657f
DW
738 SpringUpButton(m_nCurrentTool);
739 if (pTool->CanBeToggled())
b7c2b5c5 740 {
d697657f 741 pTool->Toggle();
b7c2b5c5 742 }
d697657f 743 DrawTool(pTool);
b7c2b5c5 744 }
3029781e
DW
745 wxToolBarTool* pOldTool = (wxToolBarTool*)FindById(m_nCurrentTool);
746
747 if (pOldTool && !pTool->IsToggled())
748 RaiseTool( pOldTool
749 ,FALSE
750 );
d697657f
DW
751 m_nCurrentTool = pTool->GetId();
752 OnMouseEnter(m_nCurrentTool);
6670f564 753 if (!pTool->GetShortHelp().empty())
05facebb 754 {
ff5802f3
DW
755 if (m_pToolTip)
756 delete m_pToolTip;
05facebb
DW
757 m_pToolTip = new wxToolTip(pTool->GetShortHelp());
758 m_vXMouse = (wxCoord)vPoint.x;
759 m_vYMouse = (wxCoord)vPoint.y;
0256cfeb 760 m_vToolTimer.Start(1000L, TRUE);
05facebb 761 }
d697657f
DW
762 if (!pTool->IsToggled())
763 RaiseTool(pTool);
b7c2b5c5 764 }
d697657f 765 return;
b7c2b5c5
DW
766 }
767
d697657f
DW
768 // Left button pressed.
769 if (rEvent.LeftDown() && pTool->IsEnabled())
b7c2b5c5 770 {
d697657f 771 if (pTool->CanBeToggled())
b7c2b5c5 772 {
d697657f 773 pTool->Toggle();
b7c2b5c5 774 }
d697657f
DW
775 DrawTool(pTool);
776 }
777 else if (rEvent.RightDown())
778 {
779 OnRightClick( pTool->GetId()
780 ,vX
781 ,vY
782 );
b7c2b5c5 783 }
d697657f
DW
784
785 //
786 // Left Button Released. Only this action confirms selection.
787 // If the button is enabled and it is not a toggle tool and it is
788 // in the pressed state, then raise the button and call OnLeftClick.
789 //
790 if (rEvent.LeftUp() && pTool->IsEnabled() )
b7c2b5c5 791 {
d697657f
DW
792 //
793 // Pass the OnLeftClick event to tool
794 //
795 if (!OnLeftClick( pTool->GetId()
796 ,pTool->IsToggled()) &&
797 pTool->CanBeToggled())
b7c2b5c5 798 {
d697657f
DW
799 //
800 // If it was a toggle, and OnLeftClick says No Toggle allowed,
801 // then change it back
802 //
803 pTool->Toggle();
b7c2b5c5 804 }
d697657f 805 DrawTool(pTool);
b7c2b5c5 806 }
d697657f 807} // end of wxToolBar::OnMouseEvent
d90895ac 808
b7c2b5c5 809// ----------------------------------------------------------------------------
d697657f 810// drawing
b7c2b5c5
DW
811// ----------------------------------------------------------------------------
812
d697657f
DW
813void wxToolBar::DrawTool(
814 wxToolBarToolBase* pTool
b7c2b5c5 815)
d90895ac 816{
d697657f 817 wxClientDC vDc(this);
b7c2b5c5 818
d697657f
DW
819 DrawTool( vDc
820 ,pTool
821 );
822} // end of wxToolBar::DrawTool
b7c2b5c5 823
d697657f
DW
824void wxToolBar::DrawTool(
825 wxDC& rDc
826, wxToolBarToolBase* pToolBase
b7c2b5c5 827)
d90895ac 828{
6670f564
WS
829 wxToolBarTool* pTool = (wxToolBarTool *)pToolBase;
830 wxColour gray85( 85,85,85 );
831 wxPen vDarkGreyPen( gray85, 1, wxSOLID );
832 wxBitmap vBitmap = pTool->GetNormalBitmap();
833 bool bUseMask = FALSE;
834 wxMask* pMask = NULL;
d90895ac 835
d697657f 836 PrepareDC(rDc);
d90895ac 837
d697657f
DW
838 if (!vBitmap.Ok())
839 return;
840 if ((pMask = vBitmap.GetMask()) != NULL)
841 if (pMask->GetMaskBitmap() != NULLHANDLE)
842 bUseMask = TRUE;
d90895ac 843
d697657f 844 if (!pTool->IsToggled())
d90895ac 845 {
d697657f
DW
846 LowerTool(pTool, FALSE);
847 if (!pTool->IsEnabled())
d90895ac 848 {
6670f564 849 wxColour vColor(wxT("GREY"));
d697657f
DW
850
851 rDc.SetTextForeground(vColor);
852 if (!pTool->GetDisabledBitmap().Ok())
853 pTool->SetDisabledBitmap(wxDisableBitmap( vBitmap
854 ,(long)GetBackgroundColour().GetPixel()
855 ));
856 rDc.DrawBitmap( pTool->GetDisabledBitmap()
857 ,pTool->m_vX
858 ,pTool->m_vY
859 ,bUseMask
860 );
d90895ac 861 }
d90895ac
DW
862 else
863 {
6670f564 864 rDc.SetTextForeground(*wxBLACK);
d697657f
DW
865 rDc.DrawBitmap( vBitmap
866 ,pTool->m_vX
867 ,pTool->m_vY
868 ,bUseMask
869 );
870 }
871 if (m_windowStyle & wxTB_3DBUTTONS)
872 {
873 RaiseTool(pTool);
874 }
875 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
876 {
877 wxCoord vX;
878 wxCoord vY;
879 wxCoord vLeft = pTool->m_vX - (int)(pTool->GetWidth()/2);
880
881 rDc.SetFont(GetFont());
882 rDc.GetTextExtent( pTool->GetLabel()
883 ,&vX
884 ,&vY
885 );
3029781e
DW
886 if (pTool->GetWidth() > vX) // large tools
887 {
888 vLeft = pTool->m_vX + (pTool->GetWidth() - vX);
889 GetSize(&vX, &vY);
890 rDc.DrawText( pTool->GetLabel()
891 ,vLeft
892 ,vY - (m_vTextY - 2)
893 );
894 }
895 else // normal tools
896 {
897 vLeft += (wxCoord)((m_vTextX - vX)/2);
898 rDc.DrawText( pTool->GetLabel()
899 ,vLeft
900 ,pTool->m_vY + m_vTextY + 4 // a bit of margin
901 );
902 }
d90895ac 903 }
d90895ac 904 }
d697657f
DW
905 else
906 {
6670f564 907 wxColour vColor(wxT("GREY"));
d697657f
DW
908
909 LowerTool(pTool);
910 rDc.SetTextForeground(vColor);
911 if (!pTool->GetDisabledBitmap().Ok())
912 pTool->SetDisabledBitmap(wxDisableBitmap( vBitmap
913 ,(long)GetBackgroundColour().GetPixel()
914 ));
915 rDc.DrawBitmap( pTool->GetDisabledBitmap()
916 ,pTool->m_vX
917 ,pTool->m_vY
918 ,bUseMask
919 );
920 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
921 {
922 wxCoord vX;
923 wxCoord vY;
924 wxCoord vLeft = pTool->m_vX - (int)(pTool->GetWidth()/2);
925
926 rDc.SetFont(GetFont());
927 rDc.GetTextExtent( pTool->GetLabel()
928 ,&vX
929 ,&vY
930 );
931 vLeft += (wxCoord)((m_vTextX - vX)/2);
932 rDc.DrawText( pTool->GetLabel()
933 ,vLeft
934 ,pTool->m_vY + m_vTextY + 4 // a bit of margin
935 );
936 }
937 }
938} // end of wxToolBar::DrawTool
0e320a79 939
b7c2b5c5
DW
940// ----------------------------------------------------------------------------
941// toolbar geometry
942// ----------------------------------------------------------------------------
d90895ac 943
b7c2b5c5
DW
944void wxToolBar::SetRows(
945 int nRows
946)
0e320a79 947{
d697657f 948 wxCHECK_RET( nRows != 0, _T("max number of rows must be > 0") );
b7c2b5c5 949
d697657f
DW
950 m_maxCols = (GetToolsCount() + nRows - 1) / nRows;
951 Refresh();
952} // end of wxToolBar::SetRows
0e320a79 953
d697657f 954wxToolBarToolBase* wxToolBar::FindToolForPosition(
b7c2b5c5
DW
955 wxCoord vX
956, wxCoord vY
957) const
0e320a79 958{
d697657f 959 wxCoord vTBarHeight = 0;
b7c2b5c5 960
3029781e
DW
961 GetSize( NULL
962 ,&vTBarHeight
963 );
964 vY = vTBarHeight - vY;
2461cfa0
SN
965 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
966 while (node)
b7c2b5c5 967 {
2461cfa0 968 wxToolBarTool* pTool = (wxToolBarTool *)node->GetData();
b7c2b5c5 969
d697657f 970 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
b7c2b5c5 971 {
d697657f
DW
972 if ((vX >= (pTool->m_vX - ((wxCoord)(pTool->GetWidth()/2) - 2))) &&
973 (vY >= (pTool->m_vY - 2)) &&
974 (vX <= (pTool->m_vX + pTool->GetWidth())) &&
975 (vY <= (pTool->m_vY + pTool->GetHeight() + m_vTextY + 2)))
976 {
977 return pTool;
978 }
b7c2b5c5 979 }
d697657f
DW
980 else
981 {
982 if ((vX >= pTool->m_vX) &&
983 (vY >= pTool->m_vY) &&
984 (vX <= (pTool->m_vX + pTool->GetWidth())) &&
985 (vY <= (pTool->m_vY + pTool->GetHeight())))
986 {
987 return pTool;
988 }
989 }
2461cfa0 990 node = node->GetNext();
0e320a79 991 }
d697657f
DW
992 return (wxToolBarToolBase *)NULL;
993} // end of wxToolBar::FindToolForPosition
d90895ac 994
b7c2b5c5 995// ----------------------------------------------------------------------------
d697657f 996// tool state change handlers
b7c2b5c5
DW
997// ----------------------------------------------------------------------------
998
999void wxToolBar::DoEnableTool(
1000 wxToolBarToolBase* pTool
d697657f 1001, bool WXUNUSED(bEnable)
b7c2b5c5 1002)
d90895ac 1003{
d697657f
DW
1004 DrawTool(pTool);
1005} // end of wxToolBar::DoEnableTool
0e320a79 1006
b7c2b5c5
DW
1007void wxToolBar::DoToggleTool(
1008 wxToolBarToolBase* pTool
d697657f 1009, bool WXUNUSED(bToggle)
b7c2b5c5 1010)
0e320a79 1011{
d697657f
DW
1012 DrawTool(pTool);
1013} // end of wxToolBar::DoToggleTool
0e320a79 1014
b7c2b5c5 1015void wxToolBar::DoSetToggle(
d697657f
DW
1016 wxToolBarToolBase* WXUNUSED(pTool)
1017, bool WXUNUSED(bToggle)
1018)
1019{
1020 // nothing to do
1021} // end of wxToolBar::DoSetToggle
1022
1023//
1024// Okay, so we've left the tool we're in ... we must check if the tool we're
1025// leaving was a 'sprung push button' and if so, spring it back to the up
1026// state.
1027//
1028void wxToolBar::SpringUpButton(
1029 int vId
b7c2b5c5 1030)
0e320a79 1031{
d697657f
DW
1032 wxToolBarToolBase* pTool = FindById(vId);
1033
1034 if (pTool && pTool->CanBeToggled())
1035 {
1036 if (pTool->IsToggled())
1037 pTool->Toggle();
1038
1039 DrawTool(pTool);
1040 }
1041} // end of wxToolBar::SpringUpButton
0e320a79 1042
b7c2b5c5 1043// ----------------------------------------------------------------------------
d697657f 1044// private helpers
b7c2b5c5
DW
1045// ----------------------------------------------------------------------------
1046
6670f564
WS
1047void wxToolBar::LowerTool ( wxToolBarToolBase* pToolBase,
1048 bool bLower )
d90895ac 1049{
d697657f 1050 wxToolBarTool* pTool = (wxToolBarTool*)pToolBase;
6670f564
WS
1051 wxCoord vX;
1052 wxCoord vY;
1053 wxCoord vWidth;
1054 wxCoord vHeight;
1055 wxColour gray85( 85,85,85 );
1056 wxPen vDarkGreyPen( gray85, 1, wxSOLID );
1057 wxPen vClearPen( GetBackgroundColour(), 1, wxSOLID );
1058 wxClientDC vDC(this);
b7c2b5c5 1059
d697657f
DW
1060 if (!pTool)
1061 return;
d90895ac 1062
d697657f
DW
1063 if (pTool->IsSeparator())
1064 return;
d90895ac 1065
d697657f
DW
1066 //
1067 // We only do this for flat toolbars
1068 //
1069 if (!HasFlag(wxTB_FLAT))
1070 return;
d90895ac 1071
6670f564 1072 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().empty())
d90895ac 1073 {
3029781e
DW
1074 if (pTool->GetWidth() > m_vTextX)
1075 {
1076 vX = pTool->m_vX - 2;
1077 vWidth = pTool->GetWidth() + 4;
1078 }
1079 else
1080 {
1081 vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
1082 vWidth = m_vTextX + 4;
1083 }
d697657f 1084 vY = pTool->m_vY - 2;
d697657f 1085 vHeight = pTool->GetHeight() + m_vTextY + 2;
d90895ac
DW
1086 }
1087 else
1088 {
3029781e 1089 vX = pTool->m_vX - 2;
d697657f
DW
1090 vY = pTool->m_vY - 2;
1091 vWidth = pTool->GetWidth() + 4;
1092 vHeight = pTool->GetHeight() + 4;
b7c2b5c5 1093 }
d697657f
DW
1094 if (bLower)
1095 {
6670f564 1096 vDC.SetPen(*wxWHITE_PEN);
d697657f
DW
1097 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1098 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
1099 vDC.SetPen(vDarkGreyPen);
1100 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1101 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1102 }
1103 else
1104 {
1105 vDC.SetPen(vClearPen);
1106 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1107 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
1108 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1109 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1110 }
1111} // end of WinGuiBase_CToolBarTool::LowerTool
b7c2b5c5 1112
6670f564
WS
1113void wxToolBar::RaiseTool ( wxToolBarToolBase* pToolBase,
1114 bool bRaise )
b7c2b5c5 1115{
6670f564
WS
1116 wxToolBarTool* pTool = (wxToolBarTool*)pToolBase;
1117 wxCoord vX;
1118 wxCoord vY;
1119 wxCoord vWidth;
1120 wxCoord vHeight;
1121 wxColour gray85( 85,85,85 );
1122 wxPen vDarkGreyPen( gray85, 1, wxSOLID );
1123 wxPen vClearPen( GetBackgroundColour(), 1, wxSOLID );
1124 wxClientDC vDC(this);
b7c2b5c5 1125
d697657f
DW
1126 if (!pTool)
1127 return;
b7c2b5c5 1128
d697657f
DW
1129 if (pTool->IsSeparator())
1130 return;
b7c2b5c5 1131
d697657f
DW
1132 if (!pTool->IsEnabled())
1133 return;
d90895ac 1134
d697657f
DW
1135 //
1136 // We only do this for flat toolbars
1137 //
1138 if (!HasFlag(wxTB_FLAT))
1139 return;
b7c2b5c5 1140
6670f564 1141 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().empty())
d90895ac 1142 {
3029781e
DW
1143 if (pTool->GetWidth() > m_vTextX)
1144 {
1145 vX = pTool->m_vX - 2;
1146 vWidth = pTool->GetWidth() + 4;
1147 }
1148 else
1149 {
1150 vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
1151 vWidth = m_vTextX + 4;
1152 }
d697657f 1153 vY = pTool->m_vY - 2;
d697657f 1154 vHeight = pTool->GetHeight() + m_vTextY + 2;
d90895ac 1155 }
d697657f
DW
1156 else
1157 {
3029781e 1158 vX = pTool->m_vX - 2;
d697657f
DW
1159 vY = pTool->m_vY - 2;
1160 vWidth = pTool->GetWidth() + 4;
1161 vHeight = pTool->GetHeight() + 4;
1162 }
1163 if (bRaise)
1164 {
1165 vDC.SetPen(vDarkGreyPen);
1166 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1167 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
6670f564 1168 vDC.SetPen(*wxWHITE_PEN);
d697657f
DW
1169 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1170 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1171 }
1172 else
1173 {
1174 vDC.SetPen(vClearPen);
1175 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1176 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
1177 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1178 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1179 }
1180} // end of wxToolBar::RaiseTool
d90895ac 1181
233d6db5
DW
1182void wxToolBar::OnTimer (
1183 wxTimerEvent& rEvent
1184)
1185{
5d644707 1186 if (rEvent.GetId() == m_vToolTimer.GetTimerId())
233d6db5 1187 {
05facebb
DW
1188 wxPoint vPos( m_vXMouse
1189 ,m_vYMouse
1190 );
1191
1192 m_pToolTip->DisplayToolTipWindow(vPos);
ff5802f3 1193 m_vToolTimer.Stop();
0256cfeb 1194 m_vToolExpTimer.Start(4000L, TRUE);
233d6db5 1195 }
5d644707 1196 else if (rEvent.GetId() == m_vToolExpTimer.GetTimerId())
233d6db5 1197 {
05facebb 1198 m_pToolTip->HideToolTipWindow();
ff5802f3
DW
1199 GetParent()->Refresh();
1200 m_vToolExpTimer.Stop();
233d6db5
DW
1201 }
1202} // end of wxToolBar::OnTimer
1203
d697657f 1204#endif // ndef for wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE