diff options
| author | Greg Ercolano <erco@seriss.com> | 2020-07-13 12:43:50 -0700 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2020-07-14 13:14:37 +0200 |
| commit | da76085fe71d2271847f95f5aa0694a9373fbba0 (patch) | |
| tree | a4fca1c4a8d0ccbe550efe243e8bb3a6c4102234 /fluid | |
| parent | 845224f4756239963eeea896fe68539c95d71a6f (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')
| -rw-r--r-- | fluid/ExternalCodeEditor_WIN32.cxx | 23 | ||||
| -rw-r--r-- | 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/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; } } 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 <stdio.h> #include <stdlib.h> #include <errno.h> -#include <time.h> // time(), localtime(), etc. +#include <locale.h> // setlocale().. +#include <time.h> // 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 <switches> name.fl\n" |
