mirror of https://github.com/OpenTTD/OpenTTD
Codechange: replace buffer + strecpy with std::string for getting clipboard contents
parent
35f7f7e8dc
commit
d68b5c9162
|
@ -183,25 +183,21 @@ const char *GetCurrentLocale(const char *)
|
||||||
/**
|
/**
|
||||||
* Return the contents of the clipboard (COCOA).
|
* Return the contents of the clipboard (COCOA).
|
||||||
*
|
*
|
||||||
* @param buffer Clipboard content.
|
* @return The (optional) clipboard contents.
|
||||||
* @param last The pointer to the last element of the destination buffer
|
|
||||||
* @return Whether clipboard is empty or not.
|
|
||||||
*/
|
*/
|
||||||
bool GetClipboardContents(char *buffer, const char *last)
|
std::optional<std::string> GetClipboardContents()
|
||||||
{
|
{
|
||||||
NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
|
NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
|
||||||
NSArray *types = [ NSArray arrayWithObject:NSPasteboardTypeString ];
|
NSArray *types = [ NSArray arrayWithObject:NSPasteboardTypeString ];
|
||||||
NSString *bestType = [ pb availableTypeFromArray:types ];
|
NSString *bestType = [ pb availableTypeFromArray:types ];
|
||||||
|
|
||||||
/* Clipboard has no text data available. */
|
/* Clipboard has no text data available. */
|
||||||
if (bestType == nil) return false;
|
if (bestType == nil) return std::nullopt;
|
||||||
|
|
||||||
NSString *string = [ pb stringForType:NSPasteboardTypeString ];
|
NSString *string = [ pb stringForType:NSPasteboardTypeString ];
|
||||||
if (string == nil || [ string length ] == 0) return false;
|
if (string == nil || [ string length ] == 0) return std::nullopt;
|
||||||
|
|
||||||
strecpy(buffer, [ string UTF8String ], last);
|
return [ string UTF8String ];
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set the application's bundle directory.
|
/** Set the application's bundle directory.
|
||||||
|
|
|
@ -154,27 +154,25 @@ void ShowOSErrorBox(const char *buf, bool system)
|
||||||
WinTerminate(hab);
|
WinTerminate(hab);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GetClipboardContents(char *buffer, const char *last)
|
std::optional<std::string> GetClipboardContents()
|
||||||
{
|
{
|
||||||
/* XXX -- Currently no clipboard support implemented with GCC */
|
/* XXX -- Currently no clipboard support implemented with GCC */
|
||||||
#ifndef __INNOTEK_LIBC__
|
#ifndef __INNOTEK_LIBC__
|
||||||
HAB hab = 0;
|
HAB hab = 0;
|
||||||
|
|
||||||
if (WinOpenClipbrd(hab))
|
if (WinOpenClipbrd(hab)) {
|
||||||
{
|
const char *text = (const char *)WinQueryClipbrdData(hab, CF_TEXT);
|
||||||
const char *text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
|
|
||||||
|
|
||||||
if (text != nullptr)
|
if (text != nullptr) {
|
||||||
{
|
std::string result = text;
|
||||||
strecpy(buffer, text, last);
|
|
||||||
WinCloseClipbrd(hab);
|
WinCloseClipbrd(hab);
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
WinCloseClipbrd(hab);
|
WinCloseClipbrd(hab);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -217,22 +217,20 @@ void ShowOSErrorBox(const char *buf, bool system)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef WITH_COCOA
|
#ifndef WITH_COCOA
|
||||||
bool GetClipboardContents(char *buffer, const char *last)
|
std::optional<std::string> GetClipboardContents()
|
||||||
{
|
{
|
||||||
#ifdef WITH_SDL2
|
#ifdef WITH_SDL2
|
||||||
if (SDL_HasClipboardText() == SDL_FALSE) {
|
if (SDL_HasClipboardText() == SDL_FALSE) return std::nullopt;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *clip = SDL_GetClipboardText();
|
char *clip = SDL_GetClipboardText();
|
||||||
if (clip != nullptr) {
|
if (clip != nullptr) {
|
||||||
strecpy(buffer, clip, last);
|
std::string result = clip;
|
||||||
SDL_free(clip);
|
SDL_free(clip);
|
||||||
return true;
|
return result;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return false;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -433,26 +433,19 @@ void DetermineBasePaths(const char *exe)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool GetClipboardContents(char *buffer, const char *last)
|
std::optional<std::string> GetClipboardContents()
|
||||||
{
|
{
|
||||||
HGLOBAL cbuf;
|
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return std::nullopt;
|
||||||
const char *ptr;
|
|
||||||
|
|
||||||
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
|
OpenClipboard(nullptr);
|
||||||
OpenClipboard(nullptr);
|
HGLOBAL cbuf = GetClipboardData(CF_UNICODETEXT);
|
||||||
cbuf = GetClipboardData(CF_UNICODETEXT);
|
|
||||||
|
|
||||||
ptr = (const char*)GlobalLock(cbuf);
|
std::string result = FS2OTTD(static_cast<LPCWSTR>(GlobalLock(cbuf)));
|
||||||
int out_len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)ptr, -1, buffer, (last - buffer) + 1, nullptr, nullptr);
|
GlobalUnlock(cbuf);
|
||||||
GlobalUnlock(cbuf);
|
CloseClipboard();
|
||||||
CloseClipboard();
|
|
||||||
|
|
||||||
if (out_len == 0) return false;
|
if (result.empty()) return std::nullopt;
|
||||||
} else {
|
return result;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,11 +23,9 @@
|
||||||
* Try to retrieve the current clipboard contents.
|
* Try to retrieve the current clipboard contents.
|
||||||
*
|
*
|
||||||
* @note OS-specific function.
|
* @note OS-specific function.
|
||||||
* @param buffer Clipboard content.
|
* @return The (optional) clipboard contents.
|
||||||
* @param last The pointer to the last element of the destination buffer
|
|
||||||
* @return True if some text could be retrieved.
|
|
||||||
*/
|
*/
|
||||||
bool GetClipboardContents(char *buffer, const char *last);
|
std::optional<std::string> GetClipboardContents();
|
||||||
|
|
||||||
int _caret_timer;
|
int _caret_timer;
|
||||||
|
|
||||||
|
@ -223,11 +221,10 @@ bool Textbuf::InsertString(const char *str, bool marked, const char *caret, cons
|
||||||
*/
|
*/
|
||||||
bool Textbuf::InsertClipboard()
|
bool Textbuf::InsertClipboard()
|
||||||
{
|
{
|
||||||
char utf8_buf[512];
|
auto contents = GetClipboardContents();
|
||||||
|
if (!contents.has_value()) return false;
|
||||||
|
|
||||||
if (!GetClipboardContents(utf8_buf, lastof(utf8_buf))) return false;
|
return this->InsertString(contents.value().c_str(), false);
|
||||||
|
|
||||||
return this->InsertString(utf8_buf, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue