/src/VBox/Frontends/VirtualBox/src/widgets/VBoxToolBar.h

https://gitlab.com/ufo/virtualbox-ose-3-1-8 · C Header · 137 lines · 89 code · 20 blank · 28 comment · 16 complexity · 53ff05af3379c67213faea0a63675a87 MD5 · raw file

  1. /** @file
  2. *
  3. * VBox frontends: Qt GUI ("VirtualBox"):
  4. * VBoxToolBar class declaration & implementation
  5. */
  6. /*
  7. * Copyright (C) 2006-2007 Sun Microsystems, Inc.
  8. *
  9. * This file is part of VirtualBox Open Source Edition (OSE), as
  10. * available from http://www.virtualbox.org. This file is free software;
  11. * you can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GPL) as published by the Free Software
  13. * Foundation, in version 2 as it comes in the "COPYING" file of the
  14. * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
  15. * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
  16. *
  17. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
  18. * Clara, CA 95054 USA or visit http://www.sun.com if you need
  19. * additional information or have any questions.
  20. */
  21. #ifndef ___VBoxToolBar_h___
  22. #define ___VBoxToolBar_h___
  23. #include <QGlobalStatic> /* for Q_WS_MAC */
  24. #ifdef Q_WS_MAC
  25. #include "VBoxUtils.h"
  26. #endif
  27. /* Qt includes */
  28. #include <QLayout>
  29. #include <QMainWindow>
  30. #include <QToolBar>
  31. /* Note: This styles are available on _all_ platforms. */
  32. #include <QCleanlooksStyle>
  33. #include <QWindowsStyle>
  34. /**
  35. * The VBoxToolBar class is a simple QToolBar reimplementation to disable
  36. * its built-in context menu and add some default behavior we need.
  37. */
  38. class VBoxToolBar : public QToolBar
  39. {
  40. public:
  41. VBoxToolBar (QWidget *aParent)
  42. : QToolBar (aParent)
  43. , mMainWindow (qobject_cast <QMainWindow*> (aParent))
  44. {
  45. setFloatable (false);
  46. setMovable (false);
  47. /* Remove that ugly frame panel around the toolbar.
  48. * Doing that currently for Cleanlooks & Windows styles. */
  49. if (qobject_cast <QCleanlooksStyle*> (QToolBar::style()) ||
  50. qobject_cast <QWindowsStyle*> (QToolBar::style()))
  51. setStyleSheet ("QToolBar { border: 0px none black; }");
  52. if (layout())
  53. layout()->setContentsMargins (0, 0, 0, 0);;
  54. setContextMenuPolicy (Qt::NoContextMenu);
  55. }
  56. #ifdef Q_WS_MAC
  57. void setMacToolbar()
  58. {
  59. if (mMainWindow)
  60. {
  61. mMainWindow->setUnifiedTitleAndToolBarOnMac (true);
  62. #ifndef QT_MAC_USE_COCOA
  63. WindowRef window = ::darwinToNativeWindow (this);
  64. EventHandlerUPP eventHandler = ::NewEventHandlerUPP (VBoxToolBar::macEventFilter);
  65. EventTypeSpec eventTypes[2];
  66. eventTypes[0].eventClass = kEventClassMouse;
  67. eventTypes[0].eventKind = kEventMouseDown;
  68. eventTypes[1].eventClass = kEventClassMouse;
  69. eventTypes[1].eventKind = kEventMouseUp;
  70. InstallWindowEventHandler (window, eventHandler,
  71. RT_ELEMENTS (eventTypes), eventTypes,
  72. NULL, NULL);
  73. #endif /* !QT_MAC_USE_COCOA */
  74. }
  75. }
  76. #ifndef QT_MAC_USE_COCOA
  77. static pascal OSStatus macEventFilter (EventHandlerCallRef aNextHandler,
  78. EventRef aEvent, void * /* aUserData */)
  79. {
  80. UInt32 eclass = GetEventClass (aEvent);
  81. if (eclass == kEventClassMouse)
  82. {
  83. WindowPartCode partCode;
  84. GetEventParameter (aEvent, kEventParamWindowPartCode, typeWindowPartCode, NULL, sizeof (WindowPartCode), NULL, &partCode);
  85. UInt32 ekind = GetEventKind (aEvent);
  86. if (partCode == 15 ||
  87. partCode == 4)
  88. if(ekind == kEventMouseDown || ekind == kEventMouseUp)
  89. {
  90. EventMouseButton button = 0;
  91. GetEventParameter (aEvent, kEventParamMouseButton, typeMouseButton, NULL, sizeof (button), NULL, &button);
  92. if (button != kEventMouseButtonPrimary)
  93. return noErr;
  94. }
  95. }
  96. return CallNextEventHandler (aNextHandler, aEvent);
  97. }
  98. #endif /* !QT_MAC_USE_COCOA */
  99. void setShowToolBarButton (bool aShow)
  100. {
  101. ::darwinSetShowsToolbarButton (this, aShow);
  102. }
  103. #endif /* Q_WS_MAC */
  104. void setUsesTextLabel (bool aEnable)
  105. {
  106. Qt::ToolButtonStyle tbs = Qt::ToolButtonTextUnderIcon;
  107. if (!aEnable)
  108. tbs = Qt::ToolButtonIconOnly;
  109. if (mMainWindow)
  110. mMainWindow->setToolButtonStyle (tbs);
  111. else
  112. setToolButtonStyle (tbs);
  113. }
  114. private:
  115. QMainWindow *mMainWindow;
  116. };
  117. #endif // !___VBoxToolBar_h___