From da76085fe71d2271847f95f5aa0694a9373fbba0 Mon Sep 17 00:00:00 2001 From: Greg Ercolano Date: Mon, 13 Jul 2020 12:43:50 -0700 Subject: 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. --- fluid/ExternalCodeEditor_WIN32.cxx | 23 ++++++++++++++--------- fluid/fluid.cxx | 4 +++- 2 files changed, 17 insertions(+), 10 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_Timeout_Handler.. #include // fl_alert() +#include // 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; } } diff --git a/fluid/fluid.cxx b/fluid/fluid.cxx index 5155c7fd7..0cd678827 100644 --- a/fluid/fluid.cxx +++ b/fluid/fluid.cxx @@ -39,7 +39,8 @@ #include #include #include -#include // time(), localtime(), etc. +#include // setlocale().. +#include // time(), localtime(), etc. #include "../src/flstring.h" #include "alignment_panel.h" @@ -1748,6 +1749,7 @@ static void sigint(SIGARG) { int main(int argc,char **argv) { int i = 1; + setlocale(LC_ALL, ""); // enable multilanguage errors in file chooser if (!Fl::args(argc,argv,i,arg) || i < argc-1) { static const char *msg = "usage: %s name.fl\n" -- cgit v1.2.3