Changeset View
Changeset View
Standalone View
Standalone View
source/gui/ObjectTypes/CInput.cpp
/* Copyright (C) 2019 Wildfire Games. | /* Copyright (C) 2020 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 9 Lines | |||||
#include "CInput.h" | #include "CInput.h" | ||||
#include "graphics/FontMetrics.h" | #include "graphics/FontMetrics.h" | ||||
#include "graphics/ShaderManager.h" | #include "graphics/ShaderManager.h" | ||||
#include "graphics/TextRenderer.h" | #include "graphics/TextRenderer.h" | ||||
#include "gui/CGUI.h" | #include "gui/CGUI.h" | ||||
#include "gui/CGUIScrollBarVertical.h" | #include "gui/CGUIScrollBarVertical.h" | ||||
#include "lib/sysdep/clipboard.h" | |||||
#include "lib/timer.h" | #include "lib/timer.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||
#include "ps/GameSetup/Config.h" | #include "ps/GameSetup/Config.h" | ||||
#include "ps/Globals.h" | #include "ps/Globals.h" | ||||
#include "ps/Hotkey.h" | #include "ps/Hotkey.h" | ||||
#include "renderer/Renderer.h" | #include "renderer/Renderer.h" | ||||
▲ Show 20 Lines • Show All 559 Lines • ▼ Show 20 Lines | InReaction CInput::ManuallyHandleHotkeyEvent(const SDL_Event_* ev) | ||||
if (hotkey == "paste") | if (hotkey == "paste") | ||||
{ | { | ||||
if (m_Readonly) | if (m_Readonly) | ||||
return IN_PASS; | return IN_PASS; | ||||
m_WantedX = 0.0f; | m_WantedX = 0.0f; | ||||
wchar_t* text = sys_clipboard_get(); | char* utf8_text = SDL_GetClipboardText(); | ||||
Stan: Is there a way to limit the number of objects we create? | |||||
Done Inline ActionsThis is rare enough, not worth optimising for one less allocation for now. But we should eventually migrate from std::wstring to always using UTF-8-encoded strings. linkmauve: This is rare enough, not worth optimising for one less allocation for now. But we should… | |||||
Done Inline ActionsI don't think that we need the text variable after that line. I suggest to write it like: std::wstring text; if (char* clipboard_text = SDL_GetClipboardText()) { text = wstring_from_utf8(clipboard_text); SDL_free(text); } if (!text.empty()) { // ... } vladislavbelov: I don't think that we need the `text` variable after that line. I suggest to write it like… | |||||
Done Inline ActionsI originally did that to minimise the amount of changes per commit, since this is already quite a lot to review, but I’ll change it in the next revision. linkmauve: I originally did that to minimise the amount of changes per commit, since this is already quite… | |||||
if (text) | if (!utf8_text) | ||||
{ | return IN_HANDLED; | ||||
std::wstring text = wstring_from_utf8(utf8_text); | |||||
Done Inline ActionsWe can use std::wstring directly here and below. vladislavbelov: We can use `std::wstring` directly here and below. | |||||
SDL_free(utf8_text); | |||||
if (SelectingText()) | if (SelectingText()) | ||||
DeleteCurSelection(); | DeleteCurSelection(); | ||||
if (m_iBufferPos == static_cast<int>(m_Caption.length())) | if (m_iBufferPos == static_cast<int>(m_Caption.length())) | ||||
Done Inline Actionsstatic_cast<int>(expr) In the new coding conventions :) Stan: static_cast<int>(expr) In the new coding conventions :) | |||||
Done Inline ActionsAre you sure that wcslen(text.c_str()) != text.size()? vladislavbelov: Are you sure that `wcslen(text.c_str()) != text.size()`? | |||||
Done Inline ActionsI’m still very uneasy with the difference between LP64 and LLP64 in this area, but it may give the correct result. linkmauve: I’m still very uneasy with the difference between LP64 and LLP64 in this area, but it may give… | |||||
m_Caption += text; | m_Caption += text; | ||||
else | else | ||||
m_Caption = | m_Caption = | ||||
m_Caption.Left(m_iBufferPos) + text + | m_Caption.Left(m_iBufferPos) + text + | ||||
m_Caption.Right(static_cast<long>(m_Caption.length()) - m_iBufferPos); | m_Caption.Right(static_cast<long>(m_Caption.length()) - m_iBufferPos); | ||||
UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1); | UpdateText(m_iBufferPos, m_iBufferPos, m_iBufferPos+1); | ||||
m_iBufferPos += (int)wcslen(text); | m_iBufferPos += static_cast<int>(text.size()); | ||||
UpdateAutoScroll(); | UpdateAutoScroll(); | ||||
UpdateBufferPositionSetting(); | UpdateBufferPositionSetting(); | ||||
sys_clipboard_free(text); | |||||
SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); | SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); | ||||
} | |||||
return IN_HANDLED; | return IN_HANDLED; | ||||
} | } | ||||
else if (hotkey == "copy" || hotkey == "cut") | else if (hotkey == "copy" || hotkey == "cut") | ||||
{ | { | ||||
if (m_Readonly && hotkey == "cut") | if (m_Readonly && hotkey == "cut") | ||||
return IN_PASS; | return IN_PASS; | ||||
Show All 12 Lines | if (SelectingText()) | ||||
else | else | ||||
{ | { | ||||
virtualFrom = m_iBufferPos_Tail; | virtualFrom = m_iBufferPos_Tail; | ||||
virtualTo = m_iBufferPos; | virtualTo = m_iBufferPos; | ||||
} | } | ||||
CStrW text = m_Caption.Left(virtualTo).Right(virtualTo - virtualFrom); | CStrW text = m_Caption.Left(virtualTo).Right(virtualTo - virtualFrom); | ||||
sys_clipboard_set(&text[0]); | SDL_SetClipboardText(text.ToUTF8().c_str()); | ||||
if (hotkey == "cut") | if (hotkey == "cut") | ||||
{ | { | ||||
DeleteCurSelection(); | DeleteCurSelection(); | ||||
UpdateAutoScroll(); | UpdateAutoScroll(); | ||||
SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); | SendEvent(GUIM_TEXTEDIT, EventNameTextEdit); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,393 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
Is there a way to limit the number of objects we create?