| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QMENU_P_H |
| 5 | #define |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 19 | #include "QtWidgets/qmenu.h" |
| 20 | #if QT_CONFIG(menubar) |
| 21 | #include "QtWidgets/qmenubar.h" |
| 22 | #endif |
| 23 | #include "QtWidgets/qstyleoption.h" |
| 24 | #include "QtCore/qdatetime.h" |
| 25 | #include "QtCore/qmap.h" |
| 26 | #include "QtCore/qhash.h" |
| 27 | #include "QtCore/qbasictimer.h" |
| 28 | #include "private/qwidget_p.h" |
| 29 | |
| 30 | #include <qpa/qplatformmenu.h> |
| 31 | |
| 32 | #include <QtCore/qpointer.h> |
| 33 | |
| 34 | #include <functional> |
| 35 | |
| 36 | QT_REQUIRE_CONFIG(menu); |
| 37 | |
| 38 | QT_BEGIN_NAMESPACE |
| 39 | |
| 40 | static inline int pick(Qt::Orientation o, const QPoint &pos) |
| 41 | { return o == Qt::Horizontal ? pos.x() : pos.y(); } |
| 42 | |
| 43 | static inline int pick(Qt::Orientation o, const QSize &size) |
| 44 | { return o == Qt::Horizontal ? size.width() : size.height(); } |
| 45 | |
| 46 | static inline int &rpick(Qt::Orientation o, QPoint &pos) |
| 47 | { return o == Qt::Horizontal ? pos.rx() : pos.ry(); } |
| 48 | |
| 49 | static inline int &rpick(Qt::Orientation o, QSize &size) |
| 50 | { return o == Qt::Horizontal ? size.rwidth() : size.rheight(); } |
| 51 | |
| 52 | static inline QSizePolicy::Policy pick(Qt::Orientation o, const QSizePolicy &policy) |
| 53 | { return o == Qt::Horizontal ? policy.horizontalPolicy() : policy.verticalPolicy(); } |
| 54 | |
| 55 | static inline int perp(Qt::Orientation o, const QPoint &pos) |
| 56 | { return o == Qt::Vertical ? pos.x() : pos.y(); } |
| 57 | |
| 58 | static inline int perp(Qt::Orientation o, const QSize &size) |
| 59 | { return o == Qt::Vertical ? size.width() : size.height(); } |
| 60 | |
| 61 | static inline int &rperp(Qt::Orientation o, QPoint &pos) |
| 62 | { return o == Qt::Vertical ? pos.rx() : pos.ry(); } |
| 63 | |
| 64 | static inline int &rperp(Qt::Orientation o, QSize &size) |
| 65 | { return o == Qt::Vertical ? size.rwidth() : size.rheight(); } |
| 66 | |
| 67 | static inline int pick(Qt::Orientation o, const QMargins &m) |
| 68 | { return o == Qt::Horizontal ? (m.left() + m.right()) : (m.top() + m.bottom()); } |
| 69 | |
| 70 | static inline int perp(Qt::Orientation o, const QMargins &m) |
| 71 | { return o == Qt::Vertical ? (m.left() + m.right()) : (m.top() + m.bottom()); } |
| 72 | |
| 73 | class ; |
| 74 | class QEventLoop; |
| 75 | |
| 76 | template <typename T> |
| 77 | class QSetValueOnDestroy |
| 78 | { |
| 79 | public: |
| 80 | QSetValueOnDestroy(T &toSet, T value) |
| 81 | : toSet(toSet) |
| 82 | , value(value) |
| 83 | { } |
| 84 | |
| 85 | ~QSetValueOnDestroy() { toSet = value; } |
| 86 | private: |
| 87 | T &toSet; |
| 88 | T value; |
| 89 | }; |
| 90 | |
| 91 | class |
| 92 | { |
| 93 | Q_DISABLE_COPY_MOVE() |
| 94 | public: |
| 95 | () |
| 96 | : m_enabled(false) |
| 97 | , m_uni_directional(false) |
| 98 | , m_select_other_actions(false) |
| 99 | , m_use_reset_action(true) |
| 100 | { } |
| 101 | |
| 102 | () { reset(); } |
| 103 | |
| 104 | void (QMenu *) |
| 105 | { |
| 106 | const auto style = menu->style(); |
| 107 | m_menu = menu; |
| 108 | m_uni_directional = style->styleHint(stylehint: QStyle::SH_Menu_SubMenuUniDirection, opt: nullptr, widget: menu); |
| 109 | m_uni_dir_fail_at_count = short(style->styleHint(stylehint: QStyle::SH_Menu_SubMenuUniDirectionFailCount, opt: nullptr, widget: menu)); |
| 110 | m_select_other_actions = style->styleHint(stylehint: QStyle::SH_Menu_SubMenuSloppySelectOtherActions, opt: nullptr, widget: menu); |
| 111 | m_timeout = short(style->styleHint(stylehint: QStyle::SH_Menu_SubMenuSloppyCloseTimeout, opt: nullptr, widget: menu)); |
| 112 | m_discard_state_when_entering_parent = style->styleHint(stylehint: QStyle::SH_Menu_SubMenuResetWhenReenteringParent, opt: nullptr, widget: menu); |
| 113 | m_dont_start_time_on_leave = style->styleHint(stylehint: QStyle::SH_Menu_SubMenuDontStartSloppyOnLeave, opt: nullptr, widget: menu); |
| 114 | reset(); |
| 115 | } |
| 116 | |
| 117 | void (); |
| 118 | bool () const { return m_enabled; } |
| 119 | |
| 120 | enum { |
| 121 | , |
| 122 | , |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | void () |
| 127 | { |
| 128 | if (m_enabled) |
| 129 | m_time.start(msec: m_timeout, obj: m_menu); |
| 130 | } |
| 131 | |
| 132 | void () |
| 133 | { |
| 134 | if (!m_time.isActive()) |
| 135 | startTimer(); |
| 136 | } |
| 137 | |
| 138 | void () |
| 139 | { |
| 140 | m_time.stop(); |
| 141 | } |
| 142 | |
| 143 | void (); |
| 144 | void (); |
| 145 | |
| 146 | void (); |
| 147 | void (); |
| 148 | |
| 149 | static qreal (const QPointF &p1, const QPointF &p2) |
| 150 | { |
| 151 | const QPointF slope = p2 - p1; |
| 152 | if (qFuzzyIsNull(d: slope.x())) |
| 153 | return 9999; |
| 154 | return slope.y() / slope.x(); |
| 155 | } |
| 156 | |
| 157 | bool (qreal oldS, qreal newS, bool wantSteeper) |
| 158 | { |
| 159 | if (wantSteeper) |
| 160 | return oldS <= newS; |
| 161 | return newS <= oldS; |
| 162 | } |
| 163 | |
| 164 | MouseEventResult (const QPointF &mousePos, QAction *resetAction, QAction *currentAction) |
| 165 | { |
| 166 | if (m_parent) |
| 167 | m_parent->stopTimer(); |
| 168 | |
| 169 | if (!m_enabled) |
| 170 | return EventShouldBePropagated; |
| 171 | |
| 172 | startTimerIfNotRunning(); |
| 173 | |
| 174 | if (!m_sub_menu) { |
| 175 | reset(); |
| 176 | return EventShouldBePropagated; |
| 177 | } |
| 178 | |
| 179 | QSetValueOnDestroy<bool> setFirstMouse(m_first_mouse, false); |
| 180 | QSetValueOnDestroy<QPointF> setPreviousPoint(m_previous_point, mousePos); |
| 181 | |
| 182 | if (resetAction && resetAction->isSeparator()) { |
| 183 | m_reset_action = nullptr; |
| 184 | m_use_reset_action = true; |
| 185 | } else if (m_reset_action != resetAction) { |
| 186 | if (m_use_reset_action && resetAction) { |
| 187 | const QList<QAction *> actions = m_menu->actions(); |
| 188 | const int resetIdx = actions.indexOf(t: resetAction); |
| 189 | const int originIdx = actions.indexOf(t: m_origin_action); |
| 190 | if (resetIdx > -1 && originIdx > -1 && qAbs(t: resetIdx - originIdx) > 1) |
| 191 | m_use_reset_action = false; |
| 192 | } |
| 193 | m_reset_action = resetAction; |
| 194 | } |
| 195 | |
| 196 | if (m_action_rect.contains(p: mousePos)) { |
| 197 | startTimer(); |
| 198 | return currentAction == m_menu->menuAction() ? EventIsProcessed : EventShouldBePropagated; |
| 199 | } |
| 200 | |
| 201 | if (m_uni_directional && !m_first_mouse && resetAction != m_origin_action) { |
| 202 | bool left_to_right = m_menu->layoutDirection() == Qt::LeftToRight; |
| 203 | QRect = m_sub_menu->geometry(); |
| 204 | QPoint = |
| 205 | left_to_right? sub_menu_rect.topLeft() : sub_menu_rect.topRight(); |
| 206 | QPoint = |
| 207 | left_to_right? sub_menu_rect.bottomLeft() : sub_menu_rect.bottomRight(); |
| 208 | qreal prev_slope_top = slope(p1: m_previous_point, p2: sub_menu_top); |
| 209 | qreal prev_slope_bottom = slope(p1: m_previous_point, p2: sub_menu_bottom); |
| 210 | |
| 211 | qreal current_slope_top = slope(p1: mousePos, p2: sub_menu_top); |
| 212 | qreal current_slope_bottom = slope(p1: mousePos, p2: sub_menu_bottom); |
| 213 | |
| 214 | bool slopeTop = checkSlope(oldS: prev_slope_top, newS: current_slope_top, wantSteeper: sub_menu_top.y() < mousePos.y()); |
| 215 | bool slopeBottom = checkSlope(oldS: prev_slope_bottom, newS: current_slope_bottom, wantSteeper: sub_menu_bottom.y() > mousePos.y()); |
| 216 | bool rightDirection = false; |
| 217 | int mouseDir = int(m_previous_point.y() - mousePos.y()); |
| 218 | if (mouseDir >= 0) { |
| 219 | rightDirection = rightDirection || slopeTop; |
| 220 | } |
| 221 | if (mouseDir <= 0) { |
| 222 | rightDirection = rightDirection || slopeBottom; |
| 223 | } |
| 224 | |
| 225 | if (m_uni_dir_discarded_count >= m_uni_dir_fail_at_count && !rightDirection) { |
| 226 | m_uni_dir_discarded_count = 0; |
| 227 | return EventDiscardsSloppyState; |
| 228 | } |
| 229 | |
| 230 | if (!rightDirection) |
| 231 | m_uni_dir_discarded_count++; |
| 232 | else |
| 233 | m_uni_dir_discarded_count = 0; |
| 234 | |
| 235 | } |
| 236 | |
| 237 | return m_select_other_actions ? EventShouldBePropagated : EventIsProcessed; |
| 238 | } |
| 239 | |
| 240 | void (const QRect &actionRect, QAction *resetAction, QMenu *); |
| 241 | bool () const; |
| 242 | void (); |
| 243 | int () const { return m_timeout; } |
| 244 | |
| 245 | bool (int timerId) const { return m_time.timerId() == timerId; } |
| 246 | QMenu *() const { return m_sub_menu; } |
| 247 | |
| 248 | private: |
| 249 | QMenu * = nullptr; |
| 250 | QAction * = nullptr; |
| 251 | QAction * = nullptr; |
| 252 | QRectF ; |
| 253 | QPointF ; |
| 254 | QPointer<QMenu> ; |
| 255 | QMenuSloppyState * = nullptr; |
| 256 | QBasicTimer ; |
| 257 | short = 0; |
| 258 | short = 0; |
| 259 | short = 0; |
| 260 | bool = false; |
| 261 | bool = true; |
| 262 | |
| 263 | bool : 1; |
| 264 | bool : 1; |
| 265 | bool : 1; |
| 266 | bool : 1; |
| 267 | bool : 1; |
| 268 | bool : 1; |
| 269 | }; |
| 270 | |
| 271 | class : public QWidgetPrivate |
| 272 | { |
| 273 | Q_DECLARE_PUBLIC(QMenu) |
| 274 | public: |
| 275 | using = std::function<QPoint(const QSize &)>; |
| 276 | |
| 277 | () : |
| 278 | itemsDirty(false), |
| 279 | hasCheckableItems(false), |
| 280 | lastContextMenu(false), |
| 281 | collapsibleSeparators(true), |
| 282 | toolTipsVisible(false), |
| 283 | delayedPopupGuard(false), |
| 284 | hasReceievedEnter(false), |
| 285 | hasHadMouse(false), |
| 286 | aboutToHide(false), |
| 287 | tearoff(false), |
| 288 | tornoff(false), |
| 289 | tearoffHighlighted(false), |
| 290 | doChildEffects(false) |
| 291 | { } |
| 292 | |
| 293 | () |
| 294 | { |
| 295 | delete scroll; |
| 296 | if (!platformMenu.isNull() && !platformMenu->parent()) |
| 297 | delete platformMenu.data(); |
| 298 | } |
| 299 | void (); |
| 300 | QPlatformMenu *(); |
| 301 | void (QPlatformMenu *); |
| 302 | void (); |
| 303 | void (const QAction *action, QPlatformMenuItem *item); |
| 304 | QPlatformMenuItem *(const QAction *action, QPlatformMenuItem *beforeItem); |
| 305 | |
| 306 | #ifdef Q_OS_MACOS |
| 307 | void moveWidgetToPlatformItem(QWidget *w, QPlatformMenuItem* item); |
| 308 | #endif |
| 309 | |
| 310 | static QMenuPrivate *(QMenu *m) { return m->d_func(); } |
| 311 | int () const; |
| 312 | |
| 313 | bool () const; |
| 314 | |
| 315 | //item calculations |
| 316 | QRect (QAction *) const; |
| 317 | |
| 318 | mutable QList<QRect> ; |
| 319 | mutable QHash<QAction *, QWidget *> ; |
| 320 | void () const; |
| 321 | void (const QRect &screen) const; |
| 322 | QRect (QScreen *screen = nullptr) const; |
| 323 | bool () const; |
| 324 | int () const; |
| 325 | void (const QPoint &p, QAction *atAction, PositionFunction positionFunction = {}); |
| 326 | QAction *(const QPoint &p, QAction *action, PositionFunction positionFunction = {}); |
| 327 | |
| 328 | //selection |
| 329 | static QMenu *; |
| 330 | QPointF ; |
| 331 | |
| 332 | QAction * = nullptr; |
| 333 | #ifdef QT_KEYPAD_NAVIGATION |
| 334 | QAction *selectAction = nullptr; |
| 335 | QAction *cancelAction = nullptr; |
| 336 | #endif |
| 337 | struct { |
| 338 | () |
| 339 | { } |
| 340 | void (QMenu *parent) |
| 341 | { |
| 342 | this->parent = parent; |
| 343 | } |
| 344 | |
| 345 | void (int timeout, QAction *toStartAction) |
| 346 | { |
| 347 | if (timer.isActive() && toStartAction == action) |
| 348 | return; |
| 349 | action = toStartAction; |
| 350 | timer.start(msec: timeout,obj: parent); |
| 351 | } |
| 352 | void () |
| 353 | { |
| 354 | action = nullptr; |
| 355 | timer.stop(); |
| 356 | } |
| 357 | |
| 358 | QMenu * = nullptr; |
| 359 | QAction * = nullptr; |
| 360 | QBasicTimer ; |
| 361 | } ; |
| 362 | enum { |
| 363 | , |
| 364 | |
| 365 | }; |
| 366 | QWidget *() const; |
| 367 | QAction *(QPoint p) const; |
| 368 | void (); |
| 369 | void (QAction *, int = -1, SelectionReason reason = SelectedFromElsewhere, bool activateFirst = false); |
| 370 | void (QAction *, int, bool); |
| 371 | void (); |
| 372 | |
| 373 | //scrolling support |
| 374 | struct { |
| 375 | enum { , , , }; |
| 376 | enum { =0, =0x01, =0x02 }; |
| 377 | int = 0; |
| 378 | QBasicTimer ; |
| 379 | quint8 = ScrollNone; |
| 380 | quint8 = ScrollNone; |
| 381 | |
| 382 | () { } |
| 383 | () { } |
| 384 | } * = nullptr; |
| 385 | void (QMenuScroller::ScrollLocation location, bool active=false); |
| 386 | void (QMenuScroller::ScrollDirection direction, bool page=false, bool active=false); |
| 387 | void (QAction *action, QMenuScroller::ScrollLocation location, bool active=false); |
| 388 | |
| 389 | //synchronous operation (ie exec()) |
| 390 | QEventLoop * = nullptr; |
| 391 | QPointer<QAction> ; |
| 392 | |
| 393 | //search buffer |
| 394 | QString ; |
| 395 | QBasicTimer ; |
| 396 | |
| 397 | //passing of mouse events up the parent hierarchy |
| 398 | QPointer<QMenu> ; |
| 399 | bool (QMouseEvent *); |
| 400 | |
| 401 | //used to walk up the popup list |
| 402 | struct { |
| 403 | QPointer<QWidget> ; |
| 404 | QPointer<QAction> ; |
| 405 | }; |
| 406 | virtual QList<QPointer<QWidget>> () const; |
| 407 | QMenuCaused ; |
| 408 | void (); |
| 409 | void (QMenu *); |
| 410 | QWindow *() const; |
| 411 | |
| 412 | //index mappings |
| 413 | inline QAction *(int i) const { return q_func()->actions().at(i); } |
| 414 | inline int (QAction *act) const { return q_func()->actions().indexOf(t: act); } |
| 415 | |
| 416 | //tear off support |
| 417 | QPointer<QTornOffMenu> ; |
| 418 | |
| 419 | QMenuSloppyState ; |
| 420 | |
| 421 | //default action |
| 422 | QPointer<QAction> ; |
| 423 | |
| 424 | QAction * = nullptr; |
| 425 | QAction * = nullptr; |
| 426 | |
| 427 | void (QAction *); |
| 428 | void (); |
| 429 | |
| 430 | //firing of events |
| 431 | void (QAction *, QAction::ActionEvent, bool self=true); |
| 432 | void (const QList<QPointer<QWidget>> &, QAction *, QAction::ActionEvent, |
| 433 | bool); |
| 434 | |
| 435 | void (); |
| 436 | void (); |
| 437 | void (); |
| 438 | |
| 439 | bool (const QPoint &globalPos); |
| 440 | |
| 441 | void (); |
| 442 | |
| 443 | QPointer<QPlatformMenu> ; |
| 444 | |
| 445 | QPointer<QAction> ; |
| 446 | |
| 447 | QPointer<QWidget> ; |
| 448 | |
| 449 | class : public QWidget { |
| 450 | public: |
| 451 | enum { , }; |
| 452 | (Type type, QMenuPrivate *mPrivate, |
| 453 | QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); |
| 454 | void (QPaintEvent *e) override; |
| 455 | void (const QRect &rect); |
| 456 | |
| 457 | private: |
| 458 | QMenuPrivate *; |
| 459 | Type ; |
| 460 | }; |
| 461 | ScrollerTearOffItem * = nullptr; |
| 462 | ScrollerTearOffItem * = nullptr; |
| 463 | |
| 464 | void (QPainter *painter, ScrollerTearOffItem::Type type, const QRect &rect); |
| 465 | void (QPainter *painter, const QRect &rect); |
| 466 | QRect () const; |
| 467 | |
| 468 | bool (const QAction *action) const |
| 469 | { |
| 470 | Q_Q(const QMenu); |
| 471 | if (!action || action->isSeparator()) |
| 472 | return false; |
| 473 | if (action->isEnabled()) |
| 474 | return true; |
| 475 | return q->style()->styleHint(stylehint: QStyle::SH_Menu_AllowActiveAndDisabled, opt: nullptr, widget: q); |
| 476 | } |
| 477 | |
| 478 | mutable uint = 0; |
| 479 | mutable uint = 0; |
| 480 | int = 0; |
| 481 | int = 0; |
| 482 | |
| 483 | bool = false; |
| 484 | |
| 485 | mutable quint8 = 0; // "255cols ought to be enough for anybody." |
| 486 | |
| 487 | // Contains the screen of the popup point during popup(QPoint). |
| 488 | // This is to make sure the screen is remembered, |
| 489 | // when the menu contains many items on multiple screens |
| 490 | QPointer<QScreen> ; |
| 491 | |
| 492 | mutable bool : 1; |
| 493 | mutable bool : 1; |
| 494 | bool : 1; |
| 495 | bool : 1; |
| 496 | bool : 1; |
| 497 | bool : 1; |
| 498 | bool : 1; |
| 499 | // Selection |
| 500 | bool : 1; |
| 501 | bool : 1; |
| 502 | // Tear-off menus |
| 503 | bool : 1; |
| 504 | bool : 1; |
| 505 | bool : 1; |
| 506 | //menu fading/scrolling effects |
| 507 | bool : 1; |
| 508 | }; |
| 509 | |
| 510 | QT_END_NAMESPACE |
| 511 | |
| 512 | #endif // QMENU_P_H |
| 513 | |