2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2014-12-01 10:47:38 +00:00
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2016-02-08 10:56:18 +00:00
|
|
|
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "animation.h"
|
|
|
|
|
2015-12-28 21:20:04 +00:00
|
|
|
class UserData;
|
2016-04-04 21:09:46 +00:00
|
|
|
static UserData * const LookingUpInlineBot = SharedMemoryLocation<UserData, 0>();
|
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
class FlatTextarea : public QTextEdit {
|
2014-05-30 08:53:19 +00:00
|
|
|
Q_OBJECT
|
2015-09-16 13:04:08 +00:00
|
|
|
T_WIDGET
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
public:
|
2016-05-05 16:04:17 +00:00
|
|
|
struct Tag {
|
|
|
|
int offset, length;
|
|
|
|
QString id;
|
|
|
|
};
|
|
|
|
using TagList = QVector<Tag>;
|
|
|
|
struct TextWithTags {
|
|
|
|
using Tags = FlatTextarea::TagList;
|
|
|
|
QString text;
|
|
|
|
Tags tags;
|
|
|
|
};
|
|
|
|
|
|
|
|
static QByteArray serializeTagsList(const TagList &tags);
|
|
|
|
static TagList deserializeTagsList(QByteArray data, int textLength);
|
|
|
|
static QString tagsMimeType();
|
|
|
|
|
|
|
|
FlatTextarea(QWidget *parent, const style::flatTextarea &st, const QString &ph = QString(), const QString &val = QString(), const TagList &tags = TagList());
|
2015-04-04 20:01:34 +00:00
|
|
|
|
2015-09-16 21:15:13 +00:00
|
|
|
void setMaxLength(int32 maxLength);
|
2015-09-16 13:04:08 +00:00
|
|
|
void setMinHeight(int32 minHeight);
|
|
|
|
void setMaxHeight(int32 maxHeight);
|
|
|
|
|
2015-12-31 15:27:21 +00:00
|
|
|
void setPlaceholder(const QString &ph, int32 afterSymbols = 0);
|
2014-05-30 08:53:19 +00:00
|
|
|
void updatePlaceholder();
|
2015-12-31 15:27:21 +00:00
|
|
|
void finishPlaceholder();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
QRect getTextRect() const;
|
2014-10-25 15:40:20 +00:00
|
|
|
int32 fakeMargin() const;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-12-08 12:33:37 +00:00
|
|
|
void step_appearance(float64 ms, bool timer);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-04-14 13:03:03 +00:00
|
|
|
QSize sizeHint() const override;
|
|
|
|
QSize minimumSizeHint() const override;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
EmojiPtr getSingleEmoji() const;
|
2016-01-02 01:28:11 +00:00
|
|
|
QString getMentionHashtagBotCommandPart(bool &start) const;
|
2016-04-09 11:02:50 +00:00
|
|
|
|
|
|
|
// Get the current inline bot and request string for it.
|
|
|
|
// The *outInlineBot can be filled by LookingUpInlineBot shared ptr.
|
|
|
|
// In that case the caller should lookup the bot by *outInlineBotUsername.
|
|
|
|
QString getInlineBotQuery(UserData **outInlineBot, QString *outInlineBotUsername) const;
|
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
void removeSingleEmoji();
|
2014-05-30 08:53:19 +00:00
|
|
|
bool hasText() const;
|
|
|
|
|
2014-11-18 12:40:43 +00:00
|
|
|
bool isUndoAvailable() const;
|
|
|
|
bool isRedoAvailable() const;
|
|
|
|
|
2015-04-06 22:15:29 +00:00
|
|
|
void parseLinks();
|
|
|
|
QStringList linksList() const;
|
|
|
|
|
2016-04-14 13:03:03 +00:00
|
|
|
void insertFromMimeData(const QMimeData *source) override;
|
2015-04-06 22:15:29 +00:00
|
|
|
|
2016-04-14 13:03:03 +00:00
|
|
|
QMimeData *createMimeDataFromSelection() const override;
|
2016-04-09 11:02:50 +00:00
|
|
|
|
|
|
|
enum class SubmitSettings {
|
|
|
|
None,
|
|
|
|
Enter,
|
|
|
|
CtrlEnter,
|
|
|
|
Both,
|
|
|
|
};
|
|
|
|
void setSubmitSettings(SubmitSettings settings);
|
2015-08-18 16:10:01 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
const TextWithTags &getTextWithTags() const {
|
|
|
|
return _lastTextWithTags;
|
2016-04-30 17:04:14 +00:00
|
|
|
}
|
2016-05-05 16:04:17 +00:00
|
|
|
TextWithTags getTextWithTagsPart(int start, int end = -1);
|
2016-05-04 20:38:37 +00:00
|
|
|
void insertTag(const QString &text, QString tagId = QString());
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
bool isEmpty() const {
|
|
|
|
return _lastTextWithTags.text.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
enum UndoHistoryAction {
|
|
|
|
AddToUndoHistory,
|
|
|
|
MergeWithUndoHistory,
|
|
|
|
ClearUndoHistory
|
|
|
|
};
|
|
|
|
void setTextWithTags(const TextWithTags &textWithTags, UndoHistoryAction undoHistoryAction = AddToUndoHistory);
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
// If you need to make some preparations of tags before putting them to QMimeData
|
|
|
|
// (and then to clipboard or to drag-n-drop object), here is a strategy for that.
|
|
|
|
class TagMimeProcessor {
|
|
|
|
public:
|
|
|
|
virtual QString mimeTagFromTag(const QString &tagId) = 0;
|
|
|
|
virtual QString tagFromMimeTag(const QString &mimeTag) = 0;
|
2016-05-10 13:39:42 +00:00
|
|
|
virtual ~TagMimeProcessor() {
|
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
};
|
|
|
|
void setTagMimeProcessor(std_::unique_ptr<TagMimeProcessor> &&processor);
|
2016-04-29 13:46:16 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
public slots:
|
|
|
|
void onTouchTimer();
|
|
|
|
|
|
|
|
void onDocumentContentsChange(int position, int charsRemoved, int charsAdded);
|
|
|
|
void onDocumentContentsChanged();
|
|
|
|
|
2014-11-18 12:40:43 +00:00
|
|
|
void onUndoAvailable(bool avail);
|
|
|
|
void onRedoAvailable(bool avail);
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
signals:
|
2015-09-16 13:04:08 +00:00
|
|
|
void resized();
|
2014-05-30 08:53:19 +00:00
|
|
|
void changed();
|
2014-10-17 12:57:14 +00:00
|
|
|
void submitted(bool ctrlShiftEnter);
|
2014-05-30 08:53:19 +00:00
|
|
|
void cancelled();
|
|
|
|
void tabbed();
|
2015-04-06 22:15:29 +00:00
|
|
|
void spacedReturnedPasted();
|
|
|
|
void linksChanged();
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
protected:
|
2016-06-15 17:13:46 +00:00
|
|
|
void enterEventHook(QEvent *e) {
|
|
|
|
return QTextEdit::enterEvent(e);
|
|
|
|
}
|
|
|
|
void leaveEventHook(QEvent *e) {
|
|
|
|
return QTextEdit::leaveEvent(e);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
bool viewportEvent(QEvent *e) override;
|
|
|
|
void touchEvent(QTouchEvent *e);
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
void focusInEvent(QFocusEvent *e) override;
|
|
|
|
void focusOutEvent(QFocusEvent *e) override;
|
|
|
|
void keyPressEvent(QKeyEvent *e) override;
|
|
|
|
void resizeEvent(QResizeEvent *e) override;
|
|
|
|
void mousePressEvent(QMouseEvent *e) override;
|
|
|
|
void dropEvent(QDropEvent *e) override;
|
|
|
|
void contextMenuEvent(QContextMenuEvent *e) override;
|
|
|
|
|
|
|
|
virtual void correctValue(const QString &was, QString &now, TagList &nowTags) {
|
|
|
|
}
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void insertEmoji(EmojiPtr emoji, QTextCursor c);
|
|
|
|
|
2016-04-14 13:03:03 +00:00
|
|
|
QVariant loadResource(int type, const QUrl &name) override;
|
2015-08-18 16:10:01 +00:00
|
|
|
|
2015-09-16 13:04:08 +00:00
|
|
|
void checkContentHeight();
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
private:
|
2016-05-05 16:04:17 +00:00
|
|
|
// "start" and "end" are in coordinates of text where emoji are replaced
|
|
|
|
// by ObjectReplacementCharacter. If "end" = -1 means get text till the end.
|
|
|
|
QString getTextPart(int start, int end, TagList *outTagsList, bool *outTagsChanged = nullptr) const;
|
2016-04-30 17:04:14 +00:00
|
|
|
|
2015-01-02 14:55:24 +00:00
|
|
|
void getSingleEmojiFragment(QString &text, QTextFragment &fragment) const;
|
2016-05-04 16:46:24 +00:00
|
|
|
|
|
|
|
// After any characters added we must postprocess them. This includes:
|
|
|
|
// 1. Replacing font family to semibold for ~ characters, if we used Open Sans 13px.
|
|
|
|
// 2. Replacing font family from semibold for all non-~ characters, if we used ...
|
|
|
|
// 3. Replacing emoji code sequences by ObjectReplacementCharacters with emoji pics.
|
|
|
|
// 4. Interrupting tags in which the text was inserted by any char except a letter.
|
|
|
|
// 5. Applying tags from "_insertedTags" in case we pasted text with tags, not just text.
|
|
|
|
// Rule 4 applies only if we inserted chars not in the middle of a tag (but at the end).
|
|
|
|
void processFormatting(int changedPosition, int changedEnd);
|
|
|
|
|
2015-09-16 13:04:08 +00:00
|
|
|
bool heightAutoupdated();
|
|
|
|
|
2016-09-29 16:15:44 +00:00
|
|
|
int placeholderSkipWidth() const;
|
|
|
|
|
2016-04-09 11:02:50 +00:00
|
|
|
int _minHeight = -1; // < 0 - no autosize
|
|
|
|
int _maxHeight = -1;
|
|
|
|
int _maxLength = -1;
|
|
|
|
SubmitSettings _submitSettings = SubmitSettings::Enter;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
QString _ph, _phelided;
|
2016-04-09 11:02:50 +00:00
|
|
|
int _phAfter = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
bool _phVisible;
|
|
|
|
anim::ivalue a_phLeft;
|
|
|
|
anim::fvalue a_phAlpha;
|
|
|
|
anim::cvalue a_phColor;
|
2015-12-08 12:33:37 +00:00
|
|
|
Animation _a_appearance;
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
TextWithTags _lastTextWithTags;
|
|
|
|
|
2016-04-30 17:04:14 +00:00
|
|
|
// Tags list which we should apply while setText() call or insert from mime data.
|
2016-05-04 16:46:24 +00:00
|
|
|
TagList _insertedTags;
|
2016-05-05 16:04:17 +00:00
|
|
|
bool _insertedTagsAreFromMime;
|
2016-05-04 16:46:24 +00:00
|
|
|
|
|
|
|
// Override insert position and charsAdded from complex text editing
|
|
|
|
// (like drag-n-drop in the same text edit field).
|
|
|
|
int _realInsertPosition = -1;
|
|
|
|
int _realCharsAdded = 0;
|
2016-04-30 17:04:14 +00:00
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
std_::unique_ptr<TagMimeProcessor> _tagMimeProcessor;
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
style::flatTextarea _st;
|
|
|
|
|
2016-04-09 11:02:50 +00:00
|
|
|
bool _undoAvailable = false;
|
|
|
|
bool _redoAvailable = false;
|
|
|
|
bool _inDrop = false;
|
|
|
|
bool _inHeightCheck = false;
|
2014-11-18 12:40:43 +00:00
|
|
|
|
2016-04-09 11:02:50 +00:00
|
|
|
int _fakeMargin = 0;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
QTimer _touchTimer;
|
2016-04-09 11:02:50 +00:00
|
|
|
bool _touchPress = false;
|
|
|
|
bool _touchRightButton = false;
|
|
|
|
bool _touchMove = false;
|
2014-05-30 08:53:19 +00:00
|
|
|
QPoint _touchStart;
|
|
|
|
|
2016-04-09 11:02:50 +00:00
|
|
|
bool _correcting = false;
|
2015-04-06 22:15:29 +00:00
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
struct LinkRange {
|
|
|
|
int start;
|
|
|
|
int length;
|
|
|
|
};
|
|
|
|
friend bool operator==(const LinkRange &a, const LinkRange &b);
|
|
|
|
friend bool operator!=(const LinkRange &a, const LinkRange &b);
|
|
|
|
using LinkRanges = QVector<LinkRange>;
|
2015-04-06 22:15:29 +00:00
|
|
|
LinkRanges _links;
|
2014-05-30 08:53:19 +00:00
|
|
|
};
|
2016-04-30 17:04:14 +00:00
|
|
|
|
|
|
|
inline bool operator==(const FlatTextarea::Tag &a, const FlatTextarea::Tag &b) {
|
|
|
|
return (a.offset == b.offset) && (a.length == b.length) && (a.id == b.id);
|
|
|
|
}
|
|
|
|
inline bool operator!=(const FlatTextarea::Tag &a, const FlatTextarea::Tag &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
2016-05-04 20:38:37 +00:00
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
inline bool operator==(const FlatTextarea::TextWithTags &a, const FlatTextarea::TextWithTags &b) {
|
|
|
|
return (a.text == b.text) && (a.tags == b.tags);
|
|
|
|
}
|
|
|
|
inline bool operator!=(const FlatTextarea::TextWithTags &a, const FlatTextarea::TextWithTags &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2016-05-04 20:38:37 +00:00
|
|
|
inline bool operator==(const FlatTextarea::LinkRange &a, const FlatTextarea::LinkRange &b) {
|
|
|
|
return (a.start == b.start) && (a.length == b.length);
|
|
|
|
}
|
|
|
|
inline bool operator!=(const FlatTextarea::LinkRange &a, const FlatTextarea::LinkRange &b) {
|
|
|
|
return !(a == b);
|
|
|
|
}
|