GRAPHICS: MACGUI: move WM clipboard operations from mactext to macwindowmanager

This commit is contained in:
ysj1173886760 2021-06-02 21:29:00 +08:00 committed by Eugene Sandulenko
parent 43826c7839
commit c88b6e2e04
4 changed files with 75 additions and 71 deletions

View File

@ -1307,41 +1307,6 @@ Common::U32String MacText::cutSelection() {
return selection;
}
// this is refer to how we deal U32String in splitString
// maybe we can optimize this specifically
Common::U32String stripFormat(const Common::U32String &str) {
Common::U32String res;
// calc the size of str
const Common::U32String::value_type *l = str.c_str();
while (*l) {
if (*l == '\r') {
l++;
} else if (*l == '\n') {
l++;
} else if (*l == '\001') {
l++;
// if there are two \001, then we regard it as one character
if (*l == '\001') {
res += *l++;
}
} else if (*l == '\015') { // binary format
// we are skipping the formatting stuffs
// this number 12, and the number 23, is the size of our format
l += 12;
} else if (*l == '\016') { // human-readable format
l += 23;
} else {
res += *l++;
}
}
return res;
}
void MacText::setTextInClipboard(const Common::U32String &str) {
_wm->_clipboard = str;
g_system->setTextInClipboard(stripFormat(str));
}
bool MacText::processEvent(Common::Event &event) {
if (event.type == Common::EVENT_KEYDOWN) {
if (!_editable)
@ -1352,10 +1317,10 @@ bool MacText::processEvent(Common::Event &event) {
if (event.kbd.flags & (Common::KBD_ALT | Common::KBD_CTRL | Common::KBD_META)) {
switch (event.kbd.keycode) {
case Common::KEYCODE_x:
setTextInClipboard(cutSelection());
_wm->setTextInClipboard(cutSelection());
return true;
case Common::KEYCODE_c:
setTextInClipboard(getSelection(true, false));
_wm->setTextInClipboard(getSelection(true, false));
return true;
case Common::KEYCODE_v:
if (g_system->hasTextInClipboard()) {
@ -1760,34 +1725,10 @@ Common::U32String MacText::getTextChunk(int startRow, int startCol, int endRow,
return res;
}
Common::U32String MacText::getTextFromClipboard(int &size) {
Common::U32String global_str = g_system->getTextFromClipboard();
Common::U32String wm_str = _wm->_clipboard;
// str is what we need
Common::U32String str;
if (wm_str.empty()) {
// if wm clipboard is empty, then we use the global clipboard, which won't contain the format
str = global_str;
size = str.size();
} else {
Common::U32String tmp = stripFormat(_wm->_clipboard);
if (tmp == global_str) {
// if the text is equal, then we use wm one which contains the format
str = wm_str;
size = tmp.size();
} else {
// otherwise, we prefer the global one
str = global_str;
size = str.size();
}
}
return str;
}
// mostly, we refering reshuffleParagraph to implement this function
void MacText::insertTextFromClipboard() {
int ppos = 0;
Common::U32String str = getTextFromClipboard(ppos);
Common::U32String str = _wm->getTextFromClipboard(&ppos);
if (_textLines.empty()) {
splitString(str, 0);

View File

@ -192,13 +192,6 @@ private:
void appendText_(const Common::U32String &strWithFont, uint oldLen);
void deletePreviousCharInternal(int *row, int *col);
void insertTextFromClipboard();
void setTextInClipboard(const Common::U32String &str);
/**
* get text from WM clipboard or the global clipboard
* @param size will change to the length of real text in clipboard
* @return the text in clipboard, which may contained the format
*/
Common::U32String getTextFromClipboard(int &size);
public:
void appendTextDefault(const Common::U32String &str, bool skipAdd = false);

View File

@ -395,6 +395,68 @@ void MacWindowManager::disableScreenCopy() {
g_system->copyRectToScreen(_screenCopy->getBasePtr(0, 0), _screenCopy->pitch, 0, 0, _screenCopy->w, _screenCopy->h);
}
// this is refer to how we deal U32String in splitString in mactext
// maybe we can optimize this specifically
Common::U32String stripFormat(const Common::U32String &str) {
Common::U32String res;
// calc the size of str
const Common::U32String::value_type *l = str.c_str();
while (*l) {
if (*l == '\r') {
l++;
} else if (*l == '\n') {
l++;
} else if (*l == '\001') {
l++;
// if there are two \001, then we regard it as one character
if (*l == '\001') {
res += *l++;
}
} else if (*l == '\015') { // binary format
// we are skipping the formatting stuffs
// this number 12, and the number 23, is the size of our format
l += 12;
} else if (*l == '\016') { // human-readable format
l += 23;
} else {
res += *l++;
}
}
return res;
}
void MacWindowManager::setTextInClipboard(const Common::U32String &str) {
_clipboard = str;
g_system->setTextInClipboard(stripFormat(str));
}
Common::U32String MacWindowManager::getTextFromClipboard(int *size) {
Common::U32String global_str = g_system->getTextFromClipboard();
Common::U32String wm_str = _clipboard;
// str is what we need
Common::U32String str;
if (wm_str.empty()) {
// if wm clipboard is empty, then we use the global clipboard, which won't contain the format
str = global_str;
if (size)
*size = str.size();
} else {
Common::U32String tmp = stripFormat(_clipboard);
if (tmp == global_str) {
// if the text is equal, then we use wm one which contains the format
str = wm_str;
if (size)
*size = tmp.size();
} else {
// otherwise, we prefer the global one
str = global_str;
if (size)
*size = str.size();
}
}
return str;
}
bool MacWindowManager::isMenuActive() {
if (!_menu)
return false;

View File

@ -315,6 +315,14 @@ public:
Common::SeekableReadStream *getBorderFile(byte windowType, uint32 flags);
Common::SeekableReadStream *getFile(const Common::String &filename);
void setTextInClipboard(const Common::U32String &str);
/**
* get text from WM clipboard or the global clipboard
* @param size will change to the length of real text in clipboard
* @return the text in clipboard, which may contained the format
*/
Common::U32String getTextFromClipboard(int *size = nullptr);
public:
MacFontManager *_fontMan;
uint32 _mode;
@ -330,8 +338,6 @@ public:
MacWidget *_hoveredWidget;
Common::U32String _clipboard;
private:
void loadDesktop();
void drawDesktop();
@ -388,6 +394,8 @@ private:
Common::HashMap<uint32, uint> _colorHash;
Common::Archive *_dataBundle;
Common::U32String _clipboard;
};
} // End of namespace Graphics