| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/univ/toolbar.cpp |
| 3 | // Purpose: implementation of wxToolBar for wxUniversal |
| 4 | // Author: Robert Roebling, Vadim Zeitlin (universalization) |
| 5 | // Modified by: |
| 6 | // Created: 20.02.02 |
| 7 | // Id: $Id$ |
| 8 | // Copyright: (c) 2001 Robert Roebling, |
| 9 | // (c) 2002 SciTech Software, Inc. (www.scitechsoft.com) |
| 10 | // Licence: wxWindows licence |
| 11 | ///////////////////////////////////////////////////////////////////////////// |
| 12 | |
| 13 | // ============================================================================ |
| 14 | // declarations |
| 15 | // ============================================================================ |
| 16 | |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | // headers |
| 19 | // ---------------------------------------------------------------------------- |
| 20 | |
| 21 | // For compilers that support precompilation, includes "wx.h". |
| 22 | #include "wx/wxprec.h" |
| 23 | |
| 24 | #ifdef __BORLANDC__ |
| 25 | #pragma hdrstop |
| 26 | #endif |
| 27 | |
| 28 | #if wxUSE_TOOLBAR |
| 29 | |
| 30 | #include "wx/toolbar.h" |
| 31 | |
| 32 | #ifndef WX_PRECOMP |
| 33 | #include "wx/utils.h" |
| 34 | #include "wx/app.h" |
| 35 | #include "wx/log.h" |
| 36 | #include "wx/frame.h" |
| 37 | #include "wx/dc.h" |
| 38 | #include "wx/image.h" |
| 39 | #endif |
| 40 | |
| 41 | #include "wx/univ/renderer.h" |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | // wxStdToolbarInputHandler: translates SPACE and ENTER keys and the left mouse |
| 45 | // click into button press/release actions |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | |
| 48 | class WXDLLEXPORT wxStdToolbarInputHandler : public wxStdInputHandler |
| 49 | { |
| 50 | public: |
| 51 | wxStdToolbarInputHandler(wxInputHandler *inphand); |
| 52 | |
| 53 | virtual bool HandleKey(wxInputConsumer *consumer, |
| 54 | const wxKeyEvent& event, |
| 55 | bool pressed); |
| 56 | virtual bool HandleMouse(wxInputConsumer *consumer, |
| 57 | const wxMouseEvent& event); |
| 58 | virtual bool HandleMouseMove(wxInputConsumer *consumer, const wxMouseEvent& event); |
| 59 | virtual bool HandleFocus(wxInputConsumer *consumer, const wxFocusEvent& event); |
| 60 | virtual bool HandleActivation(wxInputConsumer *consumer, bool activated); |
| 61 | |
| 62 | private: |
| 63 | wxWindow *m_winCapture; |
| 64 | wxToolBarToolBase *m_toolCapture; |
| 65 | wxToolBarToolBase *m_toolLast; |
| 66 | }; |
| 67 | |
| 68 | // ---------------------------------------------------------------------------- |
| 69 | // constants |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | |
| 72 | // value meaning that m_widthSeparator is not initialized |
| 73 | static const wxCoord INVALID_WIDTH = wxDefaultCoord; |
| 74 | |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | // wxToolBarTool: our implementation of wxToolBarToolBase |
| 77 | // ---------------------------------------------------------------------------- |
| 78 | |
| 79 | class WXDLLEXPORT wxToolBarTool : public wxToolBarToolBase |
| 80 | { |
| 81 | public: |
| 82 | wxToolBarTool(wxToolBar *tbar, |
| 83 | int id, |
| 84 | const wxString& label, |
| 85 | const wxBitmap& bmpNormal, |
| 86 | const wxBitmap& bmpDisabled, |
| 87 | wxItemKind kind, |
| 88 | wxObject *clientData, |
| 89 | const wxString& shortHelp, |
| 90 | const wxString& longHelp) |
| 91 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind, |
| 92 | clientData, shortHelp, longHelp) |
| 93 | { |
| 94 | // no position yet |
| 95 | m_x = |
| 96 | m_y = wxDefaultCoord; |
| 97 | m_width = |
| 98 | m_height = 0; |
| 99 | |
| 100 | // not pressed yet |
| 101 | m_isInverted = false; |
| 102 | |
| 103 | // mouse not here yet |
| 104 | m_underMouse = false; |
| 105 | } |
| 106 | |
| 107 | wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label) |
| 108 | : wxToolBarToolBase(tbar, control, label) |
| 109 | { |
| 110 | // no position yet |
| 111 | m_x = |
| 112 | m_y = wxDefaultCoord; |
| 113 | m_width = |
| 114 | m_height = 0; |
| 115 | |
| 116 | // not pressed yet |
| 117 | m_isInverted = false; |
| 118 | |
| 119 | // mouse not here yet |
| 120 | m_underMouse = false; |
| 121 | } |
| 122 | |
| 123 | // is this tool pressed, even temporarily? (this is different from being |
| 124 | // permanently toggled which is what IsToggled() returns) |
| 125 | bool IsPressed() const |
| 126 | { return CanBeToggled() ? IsToggled() != m_isInverted : m_isInverted; } |
| 127 | |
| 128 | // are we temporarily pressed/unpressed? |
| 129 | bool IsInverted() const { return m_isInverted; } |
| 130 | |
| 131 | // press the tool temporarily by inverting its toggle state |
| 132 | void Invert() { m_isInverted = !m_isInverted; } |
| 133 | |
| 134 | // Set underMouse |
| 135 | void SetUnderMouse( bool under = true ) { m_underMouse = under; } |
| 136 | bool IsUnderMouse() { return m_underMouse; } |
| 137 | |
| 138 | public: |
| 139 | // the tool position (for controls) |
| 140 | wxCoord m_x; |
| 141 | wxCoord m_y; |
| 142 | wxCoord m_width; |
| 143 | wxCoord m_height; |
| 144 | |
| 145 | private: |
| 146 | // true if the tool is pressed |
| 147 | bool m_isInverted; |
| 148 | |
| 149 | // true if the tool is under the mouse |
| 150 | bool m_underMouse; |
| 151 | }; |
| 152 | |
| 153 | // ============================================================================ |
| 154 | // wxToolBar implementation |
| 155 | // ============================================================================ |
| 156 | |
| 157 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) |
| 158 | |
| 159 | // ---------------------------------------------------------------------------- |
| 160 | // wxToolBar creation |
| 161 | // ---------------------------------------------------------------------------- |
| 162 | |
| 163 | void wxToolBar::Init() |
| 164 | { |
| 165 | // no tools yet |
| 166 | m_needsLayout = false; |
| 167 | |
| 168 | // unknown widths for the tools and separators |
| 169 | m_widthSeparator = INVALID_WIDTH; |
| 170 | |
| 171 | m_maxWidth = |
| 172 | m_maxHeight = 0; |
| 173 | |
| 174 | wxRenderer *renderer = GetRenderer(); |
| 175 | |
| 176 | SetToolBitmapSize(renderer->GetToolBarButtonSize(&m_widthSeparator)); |
| 177 | SetMargins(renderer->GetToolBarMargin()); |
| 178 | } |
| 179 | |
| 180 | bool wxToolBar::Create(wxWindow *parent, |
| 181 | wxWindowID id, |
| 182 | const wxPoint& pos, |
| 183 | const wxSize& size, |
| 184 | long style, |
| 185 | const wxString& name) |
| 186 | { |
| 187 | if ( !wxToolBarBase::Create(parent, id, pos, size, style, |
| 188 | wxDefaultValidator, name) ) |
| 189 | { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | FixupStyle(); |
| 194 | |
| 195 | CreateInputHandler(wxINP_HANDLER_TOOLBAR); |
| 196 | |
| 197 | SetInitialSize(size); |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | wxToolBar::~wxToolBar() |
| 203 | { |
| 204 | // Make sure the toolbar is removed from the parent. |
| 205 | SetSize(0,0); |
| 206 | } |
| 207 | |
| 208 | void wxToolBar::SetMargins(int x, int y) |
| 209 | { |
| 210 | // This required for similar visual effects under |
| 211 | // native platforms and wxUniv. |
| 212 | wxToolBarBase::SetMargins( x + 3, y + 3 ); |
| 213 | } |
| 214 | |
| 215 | // ---------------------------------------------------------------------------- |
| 216 | // wxToolBar tool-related methods |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | |
| 219 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
| 220 | { |
| 221 | // check the "other" direction first: it must be inside the toolbar or we |
| 222 | // don't risk finding anything |
| 223 | if ( IsVertical() ) |
| 224 | { |
| 225 | if ( x < 0 || x > m_maxWidth ) |
| 226 | return NULL; |
| 227 | |
| 228 | // we always use x, even for a vertical toolbar, this makes the code |
| 229 | // below simpler |
| 230 | x = y; |
| 231 | } |
| 232 | else // horizontal |
| 233 | { |
| 234 | if ( y < 0 || y > m_maxHeight ) |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
| 239 | node; |
| 240 | node = node->GetNext() ) |
| 241 | { |
| 242 | wxToolBarToolBase *tool = node->GetData(); |
| 243 | wxRect rectTool = GetToolRect(tool); |
| 244 | |
| 245 | wxCoord startTool, endTool; |
| 246 | GetRectLimits(rectTool, &startTool, &endTool); |
| 247 | |
| 248 | if ( x >= startTool && x <= endTool ) |
| 249 | { |
| 250 | // don't return the separators from here, they don't accept any |
| 251 | // input anyhow |
| 252 | return tool->IsSeparator() ? NULL : tool; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | void wxToolBar::SetToolShortHelp(int id, const wxString& help) |
| 260 | { |
| 261 | wxToolBarToolBase *tool = FindById(id); |
| 262 | |
| 263 | wxCHECK_RET( tool, _T("SetToolShortHelp: no such tool") ); |
| 264 | |
| 265 | tool->SetShortHelp(help); |
| 266 | } |
| 267 | |
| 268 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), |
| 269 | wxToolBarToolBase * WXUNUSED(tool)) |
| 270 | { |
| 271 | // recalculate the toolbar geometry before redrawing it the next time |
| 272 | m_needsLayout = true; |
| 273 | |
| 274 | // and ensure that we indeed are going to redraw |
| 275 | Refresh(); |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), |
| 281 | wxToolBarToolBase * WXUNUSED(tool)) |
| 282 | { |
| 283 | // as above |
| 284 | m_needsLayout = true; |
| 285 | |
| 286 | Refresh(); |
| 287 | |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | void wxToolBar::DoEnableTool(wxToolBarToolBase *tool, bool enable) |
| 292 | { |
| 293 | #if wxUSE_IMAGE |
| 294 | // created disabled-state bitmap on demand |
| 295 | if ( !enable && !tool->GetDisabledBitmap().Ok() ) |
| 296 | { |
| 297 | wxImage image(tool->GetNormalBitmap().ConvertToImage()); |
| 298 | |
| 299 | tool->SetDisabledBitmap(image.ConvertToGreyscale()); |
| 300 | } |
| 301 | #endif // wxUSE_IMAGE |
| 302 | |
| 303 | RefreshTool(tool); |
| 304 | } |
| 305 | |
| 306 | void wxToolBar::DoToggleTool(wxToolBarToolBase *tool, bool WXUNUSED(toggle)) |
| 307 | { |
| 308 | // note that if we're called the tool did change state (the base class |
| 309 | // checks for it), so it's not necessary to check for this again here |
| 310 | RefreshTool(tool); |
| 311 | } |
| 312 | |
| 313 | void wxToolBar::DoSetToggle(wxToolBarToolBase *tool, bool WXUNUSED(toggle)) |
| 314 | { |
| 315 | RefreshTool(tool); |
| 316 | } |
| 317 | |
| 318 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
| 319 | const wxString& label, |
| 320 | const wxBitmap& bmpNormal, |
| 321 | const wxBitmap& bmpDisabled, |
| 322 | wxItemKind kind, |
| 323 | wxObject *clientData, |
| 324 | const wxString& shortHelp, |
| 325 | const wxString& longHelp) |
| 326 | { |
| 327 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, |
| 328 | clientData, shortHelp, longHelp); |
| 329 | } |
| 330 | |
| 331 | wxToolBarToolBase * |
| 332 | wxToolBar::CreateTool(wxControl *control, const wxString& label) |
| 333 | { |
| 334 | return new wxToolBarTool(this, control, label); |
| 335 | } |
| 336 | |
| 337 | // ---------------------------------------------------------------------------- |
| 338 | // wxToolBar geometry |
| 339 | // ---------------------------------------------------------------------------- |
| 340 | |
| 341 | wxRect wxToolBar::GetToolRect(wxToolBarToolBase *toolBase) const |
| 342 | { |
| 343 | const wxToolBarTool *tool = (wxToolBarTool *)toolBase; |
| 344 | |
| 345 | wxRect rect; |
| 346 | |
| 347 | wxCHECK_MSG( tool, rect, _T("GetToolRect: NULL tool") ); |
| 348 | |
| 349 | // ensure that we always have the valid tool position |
| 350 | if ( m_needsLayout ) |
| 351 | { |
| 352 | wxConstCast(this, wxToolBar)->DoLayout(); |
| 353 | } |
| 354 | |
| 355 | rect.x = tool->m_x - m_xMargin; |
| 356 | rect.y = tool->m_y - m_yMargin; |
| 357 | |
| 358 | if ( IsVertical() ) |
| 359 | { |
| 360 | if (tool->IsButton()) |
| 361 | { |
| 362 | if(!HasFlag(wxTB_TEXT)) |
| 363 | { |
| 364 | rect.width = m_defaultWidth; |
| 365 | rect.height = m_defaultHeight; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | rect.width = m_defaultWidth + |
| 370 | GetFont().GetPointSize() * tool->GetLabel().length(); |
| 371 | rect.height = m_defaultHeight; |
| 372 | } |
| 373 | } |
| 374 | else if (tool->IsSeparator()) |
| 375 | { |
| 376 | rect.width = m_defaultWidth; |
| 377 | rect.height = m_widthSeparator; |
| 378 | } |
| 379 | else // control |
| 380 | { |
| 381 | rect.width = tool->m_width; |
| 382 | rect.height = tool->m_height; |
| 383 | } |
| 384 | } |
| 385 | else // horizontal |
| 386 | { |
| 387 | if (tool->IsButton()) |
| 388 | { |
| 389 | if(!HasFlag(wxTB_TEXT)) |
| 390 | { |
| 391 | rect.width = m_defaultWidth; |
| 392 | rect.height = m_defaultHeight; |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | rect.width = m_defaultWidth + |
| 397 | GetFont().GetPointSize() * tool->GetLabel().length(); |
| 398 | rect.height = m_defaultHeight; |
| 399 | } |
| 400 | } |
| 401 | else if (tool->IsSeparator()) |
| 402 | { |
| 403 | rect.width = m_widthSeparator; |
| 404 | rect.height = m_defaultHeight; |
| 405 | } |
| 406 | else // control |
| 407 | { |
| 408 | rect.width = tool->m_width; |
| 409 | rect.height = tool->m_height; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | rect.width += 2*m_xMargin; |
| 414 | rect.height += 2*m_yMargin; |
| 415 | |
| 416 | return rect; |
| 417 | } |
| 418 | |
| 419 | bool wxToolBar::Realize() |
| 420 | { |
| 421 | if ( !wxToolBarBase::Realize() ) |
| 422 | return false; |
| 423 | |
| 424 | m_needsLayout = true; |
| 425 | DoLayout(); |
| 426 | |
| 427 | // the first item in the radio group is checked by default to be consistent |
| 428 | // with wxGTK and the menu radio items |
| 429 | int radioGroupCount = 0; |
| 430 | |
| 431 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
| 432 | node; |
| 433 | node = node->GetNext() ) |
| 434 | { |
| 435 | wxToolBarTool *tool = (wxToolBarTool*) node->GetData(); |
| 436 | |
| 437 | if ( !tool->IsButton() || tool->GetKind() != wxITEM_RADIO ) |
| 438 | { |
| 439 | radioGroupCount = 0; |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | bool toggle = !radioGroupCount++; |
| 444 | if ( tool->Toggle(toggle) ) |
| 445 | { |
| 446 | DoToggleTool(tool, toggle); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | SetInitialSize(wxDefaultSize); |
| 451 | |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | void wxToolBar::SetWindowStyleFlag( long style ) |
| 456 | { |
| 457 | wxToolBarBase::SetWindowStyleFlag(style); |
| 458 | |
| 459 | m_needsLayout = true; |
| 460 | |
| 461 | Refresh(); |
| 462 | } |
| 463 | |
| 464 | void wxToolBar::DoLayout() |
| 465 | { |
| 466 | wxASSERT_MSG( m_needsLayout, _T("why are we called?") ); |
| 467 | |
| 468 | m_needsLayout = false; |
| 469 | |
| 470 | wxCoord x = m_xMargin, |
| 471 | y = m_yMargin; |
| 472 | |
| 473 | wxCoord widthTool = 0, maxWidthTool = 0; |
| 474 | wxCoord heightTool = 0, maxHeightTool = 0; |
| 475 | wxCoord margin = IsVertical() ? m_xMargin : m_yMargin; |
| 476 | wxCoord *pCur = IsVertical() ? &y : &x; |
| 477 | |
| 478 | // calculate the positions of all elements |
| 479 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
| 480 | node; |
| 481 | node = node->GetNext() ) |
| 482 | { |
| 483 | wxToolBarTool *tool = (wxToolBarTool *) node->GetData(); |
| 484 | |
| 485 | tool->m_x = x; |
| 486 | tool->m_y = y; |
| 487 | |
| 488 | // TODO ugly number fiddling |
| 489 | if (tool->IsButton()) |
| 490 | { |
| 491 | if (IsVertical()) |
| 492 | { |
| 493 | widthTool = m_defaultHeight; |
| 494 | heightTool = m_defaultWidth; |
| 495 | if(HasFlag(wxTB_TEXT)) |
| 496 | heightTool += GetFont().GetPointSize() * tool->GetLabel().length(); |
| 497 | } |
| 498 | else |
| 499 | { |
| 500 | widthTool = m_defaultWidth; |
| 501 | if(HasFlag(wxTB_TEXT)) |
| 502 | widthTool += GetFont().GetPointSize() * tool->GetLabel().length(); |
| 503 | |
| 504 | heightTool = m_defaultHeight; |
| 505 | } |
| 506 | |
| 507 | if(widthTool > maxWidthTool) // Record max width of tool |
| 508 | { |
| 509 | maxWidthTool = widthTool; |
| 510 | } |
| 511 | |
| 512 | if(heightTool > maxHeightTool) // Record max width of tool |
| 513 | { |
| 514 | maxHeightTool = heightTool; |
| 515 | } |
| 516 | |
| 517 | *pCur += widthTool; |
| 518 | } |
| 519 | else if (tool->IsSeparator()) |
| 520 | { |
| 521 | *pCur += m_widthSeparator; |
| 522 | } |
| 523 | else if (!IsVertical()) // horizontal control |
| 524 | { |
| 525 | wxControl *control = tool->GetControl(); |
| 526 | wxSize size = control->GetSize(); |
| 527 | tool->m_y += (m_defaultHeight - size.y)/2; |
| 528 | tool->m_width = size.x; |
| 529 | tool->m_height = size.y; |
| 530 | |
| 531 | *pCur += tool->m_width; |
| 532 | } |
| 533 | *pCur += margin; |
| 534 | } |
| 535 | |
| 536 | // calculate the total toolbar size |
| 537 | wxCoord xMin, yMin; |
| 538 | |
| 539 | if(!HasFlag(wxTB_TEXT)) |
| 540 | { |
| 541 | xMin = m_defaultWidth + 2*m_xMargin; |
| 542 | yMin = m_defaultHeight + 2*m_yMargin; |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | if (IsVertical()) |
| 547 | { |
| 548 | xMin = heightTool + 2*m_xMargin; |
| 549 | yMin = widthTool + 2*m_xMargin; |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | xMin = maxWidthTool + 2*m_xMargin; |
| 554 | yMin = heightTool + 2*m_xMargin; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | m_maxWidth = x < xMin ? xMin : x; |
| 559 | m_maxHeight = y < yMin ? yMin : y; |
| 560 | } |
| 561 | |
| 562 | wxSize wxToolBar::DoGetBestClientSize() const |
| 563 | { |
| 564 | return wxSize(m_maxWidth, m_maxHeight); |
| 565 | } |
| 566 | |
| 567 | void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags) |
| 568 | { |
| 569 | int old_width, old_height; |
| 570 | GetSize(&old_width, &old_height); |
| 571 | |
| 572 | wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags); |
| 573 | |
| 574 | // Correct width and height if needed. |
| 575 | if ( width == wxDefaultCoord || height == wxDefaultCoord ) |
| 576 | { |
| 577 | int tmp_width, tmp_height; |
| 578 | GetSize(&tmp_width, &tmp_height); |
| 579 | |
| 580 | if ( width == wxDefaultCoord ) |
| 581 | width = tmp_width; |
| 582 | if ( height == wxDefaultCoord ) |
| 583 | height = tmp_height; |
| 584 | } |
| 585 | |
| 586 | // We must refresh the frame size when the toolbar changes size |
| 587 | // otherwise the toolbar can be shown incorrectly |
| 588 | if ( old_width != width || old_height != height ) |
| 589 | { |
| 590 | // But before we send the size event check it |
| 591 | // we have a frame that is not being deleted. |
| 592 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); |
| 593 | if ( frame && !frame->IsBeingDeleted() ) |
| 594 | { |
| 595 | frame->SendSizeEvent(); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // ---------------------------------------------------------------------------- |
| 601 | // wxToolBar drawing |
| 602 | // ---------------------------------------------------------------------------- |
| 603 | |
| 604 | void wxToolBar::RefreshTool(wxToolBarToolBase *tool) |
| 605 | { |
| 606 | RefreshRect(GetToolRect(tool)); |
| 607 | } |
| 608 | |
| 609 | void wxToolBar::GetRectLimits(const wxRect& rect, |
| 610 | wxCoord *start, |
| 611 | wxCoord *end) const |
| 612 | { |
| 613 | wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") ); |
| 614 | |
| 615 | if ( IsVertical() ) |
| 616 | { |
| 617 | *start = rect.GetTop(); |
| 618 | *end = rect.GetBottom(); |
| 619 | } |
| 620 | else // horizontal |
| 621 | { |
| 622 | *start = rect.GetLeft(); |
| 623 | *end = rect.GetRight(); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void wxToolBar::DoDraw(wxControlRenderer *renderer) |
| 628 | { |
| 629 | // prepare the variables used below |
| 630 | wxDC& dc = renderer->GetDC(); |
| 631 | wxRenderer *rend = renderer->GetRenderer(); |
| 632 | dc.SetFont(GetFont()); |
| 633 | |
| 634 | // draw the border separating us from the menubar (if there is no menubar |
| 635 | // we probably shouldn't draw it?) |
| 636 | if ( !IsVertical() ) |
| 637 | { |
| 638 | rend->DrawHorizontalLine(dc, 0, 0, GetClientSize().x); |
| 639 | } |
| 640 | |
| 641 | // get the update rect and its limits depending on the orientation |
| 642 | wxRect rectUpdate = GetUpdateClientRect(); |
| 643 | wxCoord start, end; |
| 644 | GetRectLimits(rectUpdate, &start, &end); |
| 645 | |
| 646 | // and redraw all the tools intersecting it |
| 647 | for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
| 648 | node; |
| 649 | node = node->GetNext() ) |
| 650 | { |
| 651 | wxToolBarTool *tool = (wxToolBarTool*) node->GetData(); |
| 652 | wxRect rectTool = GetToolRect(tool); |
| 653 | wxCoord startTool, endTool; |
| 654 | GetRectLimits(rectTool, &startTool, &endTool); |
| 655 | |
| 656 | if ( endTool < start ) |
| 657 | { |
| 658 | // we're still to the left of the area to redraw |
| 659 | continue; |
| 660 | } |
| 661 | |
| 662 | if ( startTool > end ) |
| 663 | { |
| 664 | // we're beyond the area to redraw, nothing left to do |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | if (tool->IsSeparator() && !HasFlag(wxTB_FLAT)) |
| 669 | { |
| 670 | // Draw separators only in flat mode |
| 671 | continue; |
| 672 | } |
| 673 | |
| 674 | // deal with the flags |
| 675 | int flags = 0; |
| 676 | |
| 677 | if ( tool->IsEnabled() ) |
| 678 | { |
| 679 | // The toolbars without wxTB_FLAT don't react to the mouse hovering |
| 680 | if ( !HasFlag(wxTB_FLAT) || tool->IsUnderMouse() ) |
| 681 | flags |= wxCONTROL_CURRENT; |
| 682 | } |
| 683 | else // disabled tool |
| 684 | { |
| 685 | flags |= wxCONTROL_DISABLED; |
| 686 | } |
| 687 | |
| 688 | //if ( tool == m_toolCaptured ) |
| 689 | // flags |= wxCONTROL_FOCUSED; |
| 690 | |
| 691 | if ( tool->IsPressed() ) |
| 692 | flags = wxCONTROL_PRESSED; |
| 693 | |
| 694 | wxString label; |
| 695 | wxBitmap bitmap; |
| 696 | if ( !tool->IsSeparator() ) |
| 697 | { |
| 698 | label = tool->GetLabel(); |
| 699 | bitmap = tool->GetBitmap(); |
| 700 | } |
| 701 | //else: leave both the label and the bitmap invalid to draw a separator |
| 702 | |
| 703 | if ( !tool->IsControl() ) |
| 704 | { |
| 705 | int tbStyle = 0; |
| 706 | if(HasFlag(wxTB_TEXT)) |
| 707 | { |
| 708 | tbStyle |= wxTB_TEXT; |
| 709 | } |
| 710 | |
| 711 | if(HasFlag(wxTB_VERTICAL)) |
| 712 | { |
| 713 | tbStyle |= wxTB_VERTICAL; |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | tbStyle |= wxTB_HORIZONTAL; |
| 718 | } |
| 719 | rend->DrawToolBarButton(dc, label, bitmap, rectTool, flags, tool->GetStyle(), tbStyle); |
| 720 | } |
| 721 | else // control |
| 722 | { |
| 723 | wxControl *control = tool->GetControl(); |
| 724 | control->Move(tool->m_x, tool->m_y); |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | // ---------------------------------------------------------------------------- |
| 730 | // wxToolBar actions |
| 731 | // ---------------------------------------------------------------------------- |
| 732 | |
| 733 | bool wxToolBar::PerformAction(const wxControlAction& action, |
| 734 | long numArg, |
| 735 | const wxString& strArg) |
| 736 | { |
| 737 | wxToolBarTool *tool = (wxToolBarTool*) FindById(numArg); |
| 738 | if (!tool) |
| 739 | return false; |
| 740 | |
| 741 | if ( action == wxACTION_TOOLBAR_TOGGLE ) |
| 742 | { |
| 743 | PerformAction( wxACTION_BUTTON_RELEASE, numArg ); |
| 744 | |
| 745 | PerformAction( wxACTION_BUTTON_CLICK, numArg ); |
| 746 | |
| 747 | // Set mouse leave toolbar button range (If still in the range, |
| 748 | // toolbar button would get focus again |
| 749 | PerformAction( wxACTION_TOOLBAR_LEAVE, numArg ); |
| 750 | } |
| 751 | else if ( action == wxACTION_TOOLBAR_PRESS ) |
| 752 | { |
| 753 | wxLogTrace(_T("toolbar"), _T("Button '%s' pressed."), tool->GetShortHelp().c_str()); |
| 754 | |
| 755 | tool->Invert(); |
| 756 | |
| 757 | RefreshTool( tool ); |
| 758 | } |
| 759 | else if ( action == wxACTION_TOOLBAR_RELEASE ) |
| 760 | { |
| 761 | wxLogTrace(_T("toolbar"), _T("Button '%s' released."), tool->GetShortHelp().c_str()); |
| 762 | |
| 763 | wxASSERT_MSG( tool->IsInverted(), _T("release unpressed button?") ); |
| 764 | |
| 765 | if(tool->IsInverted()) |
| 766 | { |
| 767 | tool->Invert(); |
| 768 | } |
| 769 | |
| 770 | RefreshTool( tool ); |
| 771 | } |
| 772 | else if ( action == wxACTION_TOOLBAR_CLICK ) |
| 773 | { |
| 774 | bool isToggled; |
| 775 | if ( tool->CanBeToggled() ) |
| 776 | { |
| 777 | if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO ) |
| 778 | { |
| 779 | UnToggleRadioGroup(tool); |
| 780 | tool->Toggle(true); |
| 781 | } |
| 782 | else |
| 783 | { |
| 784 | tool->Toggle(); |
| 785 | } |
| 786 | |
| 787 | RefreshTool( tool ); |
| 788 | |
| 789 | isToggled = tool->IsToggled(); |
| 790 | } |
| 791 | else // simple non-checkable tool |
| 792 | { |
| 793 | isToggled = false; |
| 794 | } |
| 795 | OnLeftClick( tool->GetId(), isToggled ); |
| 796 | } |
| 797 | else if ( action == wxACTION_TOOLBAR_ENTER ) |
| 798 | { |
| 799 | wxCHECK_MSG( tool, false, _T("no tool to enter?") ); |
| 800 | |
| 801 | if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() ) |
| 802 | { |
| 803 | tool->SetUnderMouse( true ); |
| 804 | |
| 805 | if ( !tool->IsToggled() ) |
| 806 | RefreshTool( tool ); |
| 807 | } |
| 808 | } |
| 809 | else if ( action == wxACTION_TOOLBAR_LEAVE ) |
| 810 | { |
| 811 | wxCHECK_MSG( tool, false, _T("no tool to leave?") ); |
| 812 | |
| 813 | if ( HasFlag(wxTB_FLAT) && tool->IsEnabled() ) |
| 814 | { |
| 815 | tool->SetUnderMouse( false ); |
| 816 | |
| 817 | if ( !tool->IsToggled() ) |
| 818 | RefreshTool( tool ); |
| 819 | } |
| 820 | } |
| 821 | else |
| 822 | return wxControl::PerformAction(action, numArg, strArg); |
| 823 | |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | /* static */ |
| 828 | wxInputHandler *wxToolBar::GetStdInputHandler(wxInputHandler *handlerDef) |
| 829 | { |
| 830 | static wxStdToolbarInputHandler s_handler(handlerDef); |
| 831 | |
| 832 | return &s_handler; |
| 833 | } |
| 834 | |
| 835 | // ============================================================================ |
| 836 | // wxStdToolbarInputHandler implementation |
| 837 | // ============================================================================ |
| 838 | |
| 839 | wxStdToolbarInputHandler::wxStdToolbarInputHandler(wxInputHandler *handler) |
| 840 | : wxStdInputHandler(handler) |
| 841 | { |
| 842 | m_winCapture = NULL; |
| 843 | m_toolCapture = NULL; |
| 844 | m_toolLast = NULL; |
| 845 | } |
| 846 | |
| 847 | bool wxStdToolbarInputHandler::HandleKey(wxInputConsumer *consumer, |
| 848 | const wxKeyEvent& event, |
| 849 | bool pressed) |
| 850 | { |
| 851 | // TODO: when we have a current button we should allow the arrow |
| 852 | // keys to move it |
| 853 | return wxStdInputHandler::HandleKey(consumer, event, pressed); |
| 854 | } |
| 855 | |
| 856 | bool wxStdToolbarInputHandler::HandleMouse(wxInputConsumer *consumer, |
| 857 | const wxMouseEvent& event) |
| 858 | { |
| 859 | wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar); |
| 860 | wxToolBarToolBase *tool = tbar->FindToolForPosition(event.GetX(), event.GetY()); |
| 861 | |
| 862 | if ( event.Button(1) ) |
| 863 | { |
| 864 | |
| 865 | if ( event.LeftDown() || event.LeftDClick() ) |
| 866 | { |
| 867 | if ( !tool || !tool->IsEnabled() ) |
| 868 | return true; |
| 869 | |
| 870 | m_winCapture = tbar; |
| 871 | m_winCapture->CaptureMouse(); |
| 872 | |
| 873 | m_toolCapture = tool; |
| 874 | |
| 875 | consumer->PerformAction( wxACTION_BUTTON_PRESS, tool->GetId() ); |
| 876 | |
| 877 | return true; |
| 878 | } |
| 879 | else if ( event.LeftUp() ) |
| 880 | { |
| 881 | if ( m_winCapture ) |
| 882 | { |
| 883 | m_winCapture->ReleaseMouse(); |
| 884 | m_winCapture = NULL; |
| 885 | } |
| 886 | |
| 887 | if (m_toolCapture) |
| 888 | { |
| 889 | if ( tool == m_toolCapture ) |
| 890 | consumer->PerformAction( wxACTION_BUTTON_TOGGLE, m_toolCapture->GetId() ); |
| 891 | else |
| 892 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() ); |
| 893 | } |
| 894 | |
| 895 | m_toolCapture = NULL; |
| 896 | |
| 897 | return true; |
| 898 | } |
| 899 | //else: don't do anything special about the double click |
| 900 | } |
| 901 | |
| 902 | return wxStdInputHandler::HandleMouse(consumer, event); |
| 903 | } |
| 904 | |
| 905 | bool wxStdToolbarInputHandler::HandleMouseMove(wxInputConsumer *consumer, |
| 906 | const wxMouseEvent& event) |
| 907 | { |
| 908 | if ( !wxStdInputHandler::HandleMouseMove(consumer, event) ) |
| 909 | { |
| 910 | wxToolBar *tbar = wxStaticCast(consumer->GetInputWindow(), wxToolBar); |
| 911 | |
| 912 | wxToolBarTool *tool; |
| 913 | if ( event.Leaving() ) |
| 914 | { |
| 915 | // We cannot possibly be over a tool when |
| 916 | // leaving the toolbar |
| 917 | tool = NULL; |
| 918 | } |
| 919 | else |
| 920 | { |
| 921 | tool = (wxToolBarTool*) tbar->FindToolForPosition( event.GetX(), event.GetY() ); |
| 922 | } |
| 923 | |
| 924 | if (m_toolCapture) |
| 925 | { |
| 926 | // During capture we only care of the captured tool |
| 927 | if (tool && (tool != m_toolCapture)) |
| 928 | tool = NULL; |
| 929 | |
| 930 | if (tool == m_toolLast) |
| 931 | return true; |
| 932 | |
| 933 | if (tool) |
| 934 | consumer->PerformAction( wxACTION_BUTTON_PRESS, m_toolCapture->GetId() ); |
| 935 | else |
| 936 | consumer->PerformAction( wxACTION_BUTTON_RELEASE, m_toolCapture->GetId() ); |
| 937 | |
| 938 | m_toolLast = tool; |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | if (tool == m_toolLast) |
| 943 | return true; |
| 944 | |
| 945 | if (m_toolLast) |
| 946 | { |
| 947 | // Leave old tool if any |
| 948 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolLast->GetId() ); |
| 949 | } |
| 950 | |
| 951 | if (tool) |
| 952 | { |
| 953 | // Enter new tool if any |
| 954 | consumer->PerformAction( wxACTION_TOOLBAR_ENTER, tool->GetId() ); |
| 955 | } |
| 956 | |
| 957 | m_toolLast = tool; |
| 958 | } |
| 959 | |
| 960 | return true; |
| 961 | } |
| 962 | |
| 963 | return false; |
| 964 | } |
| 965 | |
| 966 | bool wxStdToolbarInputHandler::HandleFocus(wxInputConsumer *consumer, |
| 967 | const wxFocusEvent& WXUNUSED(event)) |
| 968 | { |
| 969 | if ( m_toolCapture ) |
| 970 | { |
| 971 | // We shouldn't be left with a highlighted button |
| 972 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() ); |
| 973 | } |
| 974 | |
| 975 | return true; |
| 976 | } |
| 977 | |
| 978 | bool wxStdToolbarInputHandler::HandleActivation(wxInputConsumer *consumer, |
| 979 | bool activated) |
| 980 | { |
| 981 | if (m_toolCapture && !activated) |
| 982 | { |
| 983 | // We shouldn't be left with a highlighted button |
| 984 | consumer->PerformAction( wxACTION_TOOLBAR_LEAVE, m_toolCapture->GetId() ); |
| 985 | } |
| 986 | |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | #endif // wxUSE_TOOLBAR |