summaryrefslogtreecommitdiff
path: root/fluid/ExternalCodeEditor_WIN32.cxx
diff options
context:
space:
mode:
authorGreg Ercolano <erco@seriss.com>2020-07-13 12:43:50 -0700
committerAlbrecht Schlosser <albrechts.fltk@online.de>2020-07-14 13:14:37 +0200
commitda76085fe71d2271847f95f5aa0694a9373fbba0 (patch)
treea4fca1c4a8d0ccbe550efe243e8bb3a6c4102234 /fluid/ExternalCodeEditor_WIN32.cxx
parent845224f4756239963eeea896fe68539c95d71a6f (diff)
Import Albrecht's utf8 mods for FormatMessage() to fluid
Amended by Albrecht: - fix comments - add setlocale() to fluid Note: the latter is also necessary to honor locale in GTK filechooser.
Diffstat (limited to 'fluid/ExternalCodeEditor_WIN32.cxx')
-rw-r--r--fluid/ExternalCodeEditor_WIN32.cxx23
1 files changed, 14 insertions, 9 deletions
diff --git a/fluid/ExternalCodeEditor_WIN32.cxx b/fluid/ExternalCodeEditor_WIN32.cxx
index 05388d066..9be5a30aa 100644
--- a/fluid/ExternalCodeEditor_WIN32.cxx
+++ b/fluid/ExternalCodeEditor_WIN32.cxx
@@ -7,6 +7,7 @@
#include <FL/Fl.H> // Fl_Timeout_Handler..
#include <FL/fl_ask.H> // fl_alert()
+#include <FL/fl_utf8.h> // fl_utf8fromwc()
#include "ExternalCodeEditor_WIN32.h"
@@ -19,22 +20,26 @@ static Fl_Timeout_Handler L_update_timer_cb = 0; // app's update timer ca
// [Static/Local] Get error message string for last failed WIN32 function.
// Returns a string pointing to static memory.
//
-// TODO: Is more code needed here to convert returned string to utf8? -erco
-//
static const char *get_ms_errmsg() {
static char emsg[1024];
DWORD lastErr = GetLastError();
DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM;
- LPSTR mbuf = 0;
- DWORD size = FormatMessageA(flags, 0, lastErr, MAKELANGID(LANG_NEUTRAL,
- SUBLANG_DEFAULT), (LPSTR)&mbuf, 0, NULL);
- if ( size == 0 ) {
- _snprintf(emsg, sizeof(emsg), "Error Code %ld", long(lastErr));
+ DWORD langid = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
+ LPWSTR mbuf = 0;
+ DWORD msize = 0;
+
+ // Get error message from Windows
+ msize = FormatMessageW(flags, 0, lastErr, langid, (LPWSTR)&mbuf, 0, NULL);
+ if ( msize == 0 ) {
+ _snprintf(emsg, sizeof(emsg), "Error #%ld", (unsigned long)lastErr);
} else {
- // Copy mbuf -> emsg (with '\r's removed -- they screw up fl_alert())
- for ( char *src=mbuf, *dst=emsg; 1; src++ ) {
+ // Convert message to UTF-8
+ int mlen = fl_utf8fromwc(emsg, sizeof(emsg), mbuf, msize);
+ // Remove '\r's -- they screw up fl_alert()
+ char *src = emsg, *dst = emsg;
+ for ( ; 1; src++ ) {
if ( *src == '\0' ) { *dst = '\0'; break; }
if ( *src != '\r' ) { *dst++ = *src; }
}