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
|
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
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);
|
2016-11-04 11:14:47 +00:00
|
|
|
QAction *addAction(const QString &text, base::lambda_unique<void()> callback, const style::icon *icon = nullptr, const style::icon *iconOver = nullptr);
|
2016-10-26 16:43:13 +00:00
|
|
|
QAction *addSeparator();
|
|
|
|
void clearActions();
|
|
|
|
|
|
|
|
void clearSelection();
|
|
|
|
|
|
|
|
enum class TriggeredSource {
|
|
|
|
Mouse,
|
|
|
|
Keyboard,
|
|
|
|
};
|
|
|
|
void setChildShown(bool shown) {
|
|
|
|
_childShown = shown;
|
|
|
|
}
|
|
|
|
void setShowSource(TriggeredSource source);
|
|
|
|
|
|
|
|
using Actions = QList<QAction*>;
|
|
|
|
Actions &actions();
|
|
|
|
|
|
|
|
void setResizedCallback(base::lambda_unique<void()> callback) {
|
|
|
|
_resizedCallback = std_::move(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setActivatedCallback(base::lambda_unique<void(QAction *action, int actionTop, TriggeredSource source)> callback) {
|
|
|
|
_activatedCallback = std_::move(callback);
|
|
|
|
}
|
|
|
|
void setTriggeredCallback(base::lambda_unique<void(QAction *action, int actionTop, TriggeredSource source)> callback) {
|
|
|
|
_triggeredCallback = std_::move(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setKeyPressDelegate(base::lambda_unique<bool(int key)> delegate) {
|
|
|
|
_keyPressDelegate = std_::move(delegate);
|
|
|
|
}
|
|
|
|
void handleKeyPress(int key);
|
|
|
|
|
|
|
|
void setMouseMoveDelegate(base::lambda_unique<void(QPoint globalPosition)> delegate) {
|
|
|
|
_mouseMoveDelegate = std_::move(delegate);
|
|
|
|
}
|
|
|
|
void handleMouseMove(QPoint globalPosition);
|
|
|
|
|
|
|
|
void setMousePressDelegate(base::lambda_unique<void(QPoint globalPosition)> delegate) {
|
|
|
|
_mousePressDelegate = std_::move(delegate);
|
|
|
|
}
|
|
|
|
void handleMousePress(QPoint globalPosition);
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
void setMouseReleaseDelegate(base::lambda_unique<void(QPoint globalPosition)> delegate) {
|
|
|
|
_mouseReleaseDelegate = std_::move(delegate);
|
|
|
|
}
|
|
|
|
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;
|
2016-10-26 16:43:13 +00:00
|
|
|
void enterEvent(QEvent *e) override;
|
|
|
|
void leaveEvent(QEvent *e) override;
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void actionChanged();
|
|
|
|
|
|
|
|
private:
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
|
|
|
|
base::lambda_unique<void()> _resizedCallback;
|
|
|
|
base::lambda_unique<void(QAction *action, int actionTop, TriggeredSource source)> _activatedCallback;
|
|
|
|
base::lambda_unique<void(QAction *action, int actionTop, TriggeredSource source)> _triggeredCallback;
|
|
|
|
base::lambda_unique<bool(int key)> _keyPressDelegate;
|
|
|
|
base::lambda_unique<void(QPoint globalPosition)> _mouseMoveDelegate;
|
|
|
|
base::lambda_unique<void(QPoint globalPosition)> _mousePressDelegate;
|
2016-11-16 16:04:25 +00:00
|
|
|
base::lambda_unique<void(QPoint globalPosition)> _mouseReleaseDelegate;
|
2016-10-26 16:43:13 +00:00
|
|
|
|
|
|
|
struct ActionData {
|
|
|
|
bool hasSubmenu = false;
|
|
|
|
QString text;
|
|
|
|
QString shortcut;
|
|
|
|
const style::icon *icon = nullptr;
|
2016-11-01 12:46:34 +00:00
|
|
|
const style::icon *iconOver = nullptr;
|
2016-11-16 16:04:25 +00:00
|
|
|
QSharedPointer<RippleAnimation> ripple;
|
2016-10-26 16:43:13 +00:00
|
|
|
};
|
|
|
|
using ActionsData = QList<ActionData>;
|
|
|
|
|
|
|
|
QMenu *_wappedMenu = nullptr;
|
|
|
|
Actions _actions;
|
|
|
|
ActionsData _actionsData;
|
|
|
|
|
|
|
|
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
|