win32_gui: encode copy-to-clipboard and save-to-file text in UTF-8

This commit is contained in:
Alex Kompel
2015-04-15 22:13:10 -07:00
parent 7c644c1507
commit c8679c8e0e
2 changed files with 31 additions and 22 deletions
+27 -19
View File
@@ -414,6 +414,7 @@ LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPar
} }
} }
} /* end switch */ } /* end switch */
} break; } break;
@@ -815,7 +816,7 @@ LRESULT onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
char* p; char* p;
size_t len; size_t len;
HANDLE hglbCopy; HANDLE hglbCopy;
TCHAR* p_copy; char* p_copy;
p = nh_compose_ascii_screenshot(); p = nh_compose_ascii_screenshot();
if( !p ) return 0; if( !p ) return 0;
@@ -828,18 +829,18 @@ LRESULT onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
EmptyClipboard(); EmptyClipboard();
hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(TCHAR)); hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(char));
if (hglbCopy == NULL) { if (hglbCopy == NULL) {
CloseClipboard(); CloseClipboard();
return FALSE; return FALSE;
} }
p_copy = (TCHAR*)GlobalLock(hglbCopy); p_copy = (char*)GlobalLock(hglbCopy);
NH_A2W( p, p_copy, len ); strncpy(p_copy, p, len);
p_copy[len] = (TCHAR) 0; // null character p_copy[len] = 0; // null character
GlobalUnlock(hglbCopy); GlobalUnlock(hglbCopy);
SetClipboardData(CF_TEXT, hglbCopy); SetClipboardData(SYMHANDLING(H_IBM)? CF_OEMTEXT : CF_TEXT, hglbCopy);
CloseClipboard(); CloseClipboard();
@@ -848,32 +849,34 @@ LRESULT onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
case IDM_SETTING_SCREEN_TO_FILE: { case IDM_SETTING_SCREEN_TO_FILE: {
OPENFILENAME ofn; OPENFILENAME ofn;
char filename[1024]; TCHAR filename[1024];
TCHAR whackdir[MAX_PATH];
FILE* pFile; FILE* pFile;
char* text; char* text;
wchar_t* wtext;
int tlen = 0;
ZeroMemory(filename, sizeof(filename)); ZeroMemory(filename, sizeof(filename));
ZeroMemory(&ofn, sizeof(ofn)); ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof (OPENFILENAME); ofn.lStructSize = sizeof (OPENFILENAME);
ofn.hwndOwner = hWnd; ofn.hwndOwner = hWnd;
ofn.hInstance = GetNHApp()->hApp; ofn.hInstance = GetNHApp()->hApp;
ofn.lpstrFilter = ofn.lpstrFilter = TEXT("Text Files (*.txt)\x0*.txt\x0")
"Text Files (*.txt)\x0*.txt\x0" TEXT("All Files (*.*)\x0*.*\x0")
"All Files (*.*)\x0*.*\x0" TEXT("\x0\x0");
"\x0\x0";
ofn.lpstrCustomFilter = NULL; ofn.lpstrCustomFilter = NULL;
ofn.nMaxCustFilter = 0; ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 1; ofn.nFilterIndex = 1;
ofn.lpstrFile = filename; ofn.lpstrFile = filename;
ofn.nMaxFile = sizeof(filename); ofn.nMaxFile = _countof(filename);
ofn.lpstrFileTitle = NULL; ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0; ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = hackdir; ofn.lpstrInitialDir = NH_A2W(hackdir, whackdir, MAX_PATH);
ofn.lpstrTitle = NULL; ofn.lpstrTitle = NULL;
ofn.Flags = OFN_LONGNAMES | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; ofn.Flags = OFN_LONGNAMES | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
ofn.nFileOffset = 0; ofn.nFileOffset = 0;
ofn.nFileExtension = 0; ofn.nFileExtension = 0;
ofn.lpstrDefExt = "txt"; ofn.lpstrDefExt = TEXT("txt");
ofn.lCustData = 0; ofn.lCustData = 0;
ofn.lpfnHook = 0; ofn.lpfnHook = 0;
ofn.lpTemplateName = 0; ofn.lpTemplateName = 0;
@@ -883,18 +886,23 @@ LRESULT onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
text = nh_compose_ascii_screenshot(); text = nh_compose_ascii_screenshot();
if( !text ) return FALSE; if( !text ) return FALSE;
pFile = fopen(filename, "wb"); pFile = _tfopen(filename, TEXT("wt+,ccs=UTF-8"));
if( !pFile ) { if( !pFile ) {
char buf[4096]; TCHAR buf[4096];
sprintf(buf, "Cannot open %s for writing!", filename); _stprintf_s(buf, _countof(buf), TEXT("Cannot open %s for writing!"), filename);
NHMessageBox(hWnd, buf, MB_OK | MB_ICONERROR); NHMessageBox(hWnd, buf, MB_OK | MB_ICONERROR);
free(text); free(text);
return FALSE; return FALSE;
} }
fwrite(text, strlen(text), 1, pFile); tlen = strlen(text);
wtext = (wchar_t*)malloc(tlen*sizeof(wchar_t));
if( !wtext ) panic("out of memory");
MultiByteToWideChar(NH_CODEPAGE,0,text,-1,wtext,tlen);
fwrite(wtext, tlen*sizeof(wchar_t), 1, pFile);
fclose(pFile); fclose(pFile);
free(text); free(text);
free(wtext);
} break; } break;
case IDM_NHMODE: case IDM_NHMODE:
@@ -908,7 +916,7 @@ LRESULT onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
mswin_destroy_reg(); mswin_destroy_reg();
/* Notify the user that windows settings will not be saved this time. */ /* Notify the user that windows settings will not be saved this time. */
NHMessageBox(GetNHApp()->hMainWnd, NHMessageBox(GetNHApp()->hMainWnd,
"Your Windows Settings will not be stored when you exit this time.", TEXT("Your Windows Settings will not be stored when you exit this time."),
MB_OK | MB_ICONINFORMATION); MB_OK | MB_ICONINFORMATION);
break; break;
} }
+4 -3
View File
@@ -211,9 +211,10 @@ extern COLORREF message_fg_color;
#define SYSCLR_TO_BRUSH(x) ((HBRUSH)((x) + 1)) #define SYSCLR_TO_BRUSH(x) ((HBRUSH)((x) + 1))
/* unicode stuff */ /* unicode stuff */
#ifdef UNICODE #define NH_CODEPAGE (SYMHANDLING(H_IBM)?GetOEMCP():GetACP())
#ifdef _UNICODE
#define NH_W2A(w, a, cb) ( WideCharToMultiByte( \ #define NH_W2A(w, a, cb) ( WideCharToMultiByte( \
CP_ACP, \ NH_CODEPAGE, \
0, \ 0, \
(w), \ (w), \
-1, \ -1, \
@@ -223,7 +224,7 @@ extern COLORREF message_fg_color;
NULL), (a) ) NULL), (a) )
#define NH_A2W(a, w, cb) ( MultiByteToWideChar( \ #define NH_A2W(a, w, cb) ( MultiByteToWideChar( \
CP_ACP, \ NH_CODEPAGE, \
0, \ 0, \
(a), \ (a), \
-1, \ -1, \