2016-10-26 16:43:13 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-10-26 16:43:13 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
2017-06-29 12:29:00 +00:00
|
|
|
class ToggleView;
|
2016-11-16 16:04:25 +00:00
|
|
|
class RippleAnimation;
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
class Menu : public TWidget {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
Menu(QWidget *parent, const style::Menu &st = st::defaultMenu);
|
|
|
|
Menu(QWidget *parent, QMenu *menu, const style::Menu &st = st::defaultMenu);
|
|
|
|
|
2016-11-01 12:46:34 +00:00
|
|
|
QAction *addAction(const QString &text, const QObject *receiver, const char* member, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
2017-02-26 11:32:13 +00:00
|
|
|
QAction *addAction(const QString &text, base::lambda<void()> callback, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
2016-10-26 16:43:13 +00:00
|
|
|
QAction *addSeparator();
|
|
|
|
void clearActions();
|
2017-06-29 19:09:10 +00:00
|
|
|
void finishAnimations();
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
void clearSelection();
|
|
|
|
|
|
|
|
enum class TriggeredSource {
|
|
|
|
Mouse,
|
|
|
|
Keyboard,
|
|
|
|
};
|
|
|
|
void setChildShown(bool shown) {
|
|
|
|
_childShown = shown;
|
|
|
|
}
|
|
|
|
void setShowSource(TriggeredSource source);
|
2017-06-29 12:29:00 +00:00
|
|
|
void setForceWidth(int forceWidth);
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
using Actions = QList<QAction*>;
|
|
|
|
Actions &actions();
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void setResizedCallback(base::lambda<void()> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_resizedCallback = std::move(callback);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void setActivatedCallback(base::lambda<void(QAction *action, int actionTop, TriggeredSource source)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_activatedCallback = std::move(callback);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
2017-02-26 11:32:13 +00:00
|
|
|
void setTriggeredCallback(base::lambda<void(QAction *action, int actionTop, TriggeredSource source)> callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_triggeredCallback = std::move(callback);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void setKeyPressDelegate(base::lambda<bool(int key)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_keyPressDelegate = std::move(delegate);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
void handleKeyPress(int key);
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void setMouseMoveDelegate(base::lambda<void(QPoint globalPosition)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_mouseMoveDelegate = std::move(delegate);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
void handleMouseMove(QPoint globalPosition);
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void setMousePressDelegate(base::lambda<void(QPoint globalPosition)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_mousePressDelegate = std::move(delegate);
|
2016-10-26 16:43:13 +00:00
|
|
|
}
|
|
|
|
void handleMousePress(QPoint globalPosition);
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
void setMouseReleaseDelegate(base::lambda<void(QPoint globalPosition)> delegate) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_mouseReleaseDelegate = std::move(delegate);
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
void handleMouseRelease(QPoint globalPosition);
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void keyPressEvent(QKeyEvent *e) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
2016-11-16 16:04:25 +00:00
|
|
|
void mouseReleaseEvent(QMouseEvent *e) override;
|
2017-02-11 11:24:37 +00:00
|
|
|
void enterEventHook(QEvent *e) override;
|
|
|
|
void leaveEventHook(QEvent *e) override;
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void actionChanged();
|
|
|
|
|
|
|
|
private:
|
2017-07-22 08:35:18 +00:00
|
|
|
struct ActionData {
|
|
|
|
ActionData() = default;
|
|
|
|
ActionData(const ActionData &other) = delete;
|
|
|
|
ActionData &operator=(const ActionData &other) = delete;
|
|
|
|
ActionData(ActionData &&other) = default;
|
|
|
|
ActionData &operator=(ActionData &&other) = default;
|
|
|
|
~ActionData();
|
|
|
|
|
|
|
|
bool hasSubmenu = false;
|
|
|
|
QString text;
|
|
|
|
QString shortcut;
|
|
|
|
const style::icon *icon = nullptr;
|
|
|
|
const style::icon *iconOver = nullptr;
|
|
|
|
std::unique_ptr<RippleAnimation> ripple;
|
|
|
|
std::unique_ptr<ToggleView> toggle;
|
|
|
|
};
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void updateSelected(QPoint globalPosition);
|
|
|
|
void init();
|
|
|
|
|
|
|
|
// Returns the new width.
|
|
|
|
int processAction(QAction *action, int index, int width);
|
2016-11-01 12:46:34 +00:00
|
|
|
QAction *addAction(QAction *a, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
void setSelected(int selected);
|
2017-06-29 19:09:10 +00:00
|
|
|
void setPressed(int pressed);
|
2016-10-26 16:43:13 +00:00
|
|
|
void clearMouseSelection();
|
|
|
|
|
|
|
|
int itemTop(int index);
|
2016-11-16 16:04:25 +00:00
|
|
|
void updateItem(int index);
|
2016-10-26 16:43:13 +00:00
|
|
|
void updateSelectedItem();
|
|
|
|
void itemPressed(TriggeredSource source);
|
2016-11-16 16:04:25 +00:00
|
|
|
void itemReleased(TriggeredSource source);
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
const style::Menu &_st;
|
|
|
|
|
2016-11-20 12:54:07 +00:00
|
|
|
base::lambda<void()> _resizedCallback;
|
|
|
|
base::lambda<void(QAction *action, int actionTop, TriggeredSource source)> _activatedCallback;
|
|
|
|
base::lambda<void(QAction *action, int actionTop, TriggeredSource source)> _triggeredCallback;
|
|
|
|
base::lambda<bool(int key)> _keyPressDelegate;
|
|
|
|
base::lambda<void(QPoint globalPosition)> _mouseMoveDelegate;
|
|
|
|
base::lambda<void(QPoint globalPosition)> _mousePressDelegate;
|
|
|
|
base::lambda<void(QPoint globalPosition)> _mouseReleaseDelegate;
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
QMenu *_wappedMenu = nullptr;
|
|
|
|
Actions _actions;
|
2017-06-29 12:29:00 +00:00
|
|
|
std::vector<ActionData> _actionsData;
|
2016-10-26 16:43:13 +00:00
|
|
|
|
2017-06-29 12:29:00 +00:00
|
|
|
int _forceWidth = 0;
|
2016-10-26 16:43:13 +00:00
|
|
|
int _itemHeight, _separatorHeight;
|
|
|
|
|
|
|
|
bool _mouseSelection = false;
|
|
|
|
|
|
|
|
int _selected = -1;
|
2016-11-16 16:04:25 +00:00
|
|
|
int _pressed = -1;
|
2016-10-26 16:43:13 +00:00
|
|
|
bool _childShown = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Ui
|