Last active
December 13, 2015 23:10
-
-
Save codebrainz/fcadf6400f5f4c7263d3 to your computer and use it in GitHub Desktop.
Make Geany agnostic to Win32 API text encoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/win32.c b/src/win32.c | |
index 34b7d93..f5ffc9a 100644 | |
--- a/src/win32.c | |
+++ b/src/win32.c | |
@@ -32,6 +32,10 @@ | |
/* Needed for SHGFP_TYPE */ | |
#define _WIN32_IE 0x0500 | |
+#ifndef UNICODE | |
+# define UNICODE | |
+#endif | |
+ | |
#include "win32.h" | |
#ifdef G_OS_WIN32 | |
@@ -67,6 +71,71 @@ | |
static UINT_PTR dialog_timer = 0; | |
+// converts a UTF8 string into a TCHAR (wchar_t when UNICODE is defined) | |
+static TCHAR *win32_utf8_to_tchar_len(const gchar *utf8_string, gssize len) | |
+{ | |
+ TCHAR *out_str = NULL; | |
+#ifdef UNICODE | |
+ // utf8 to wide | |
+ int size = MultiByteToWideChar(CP_UTF8, 0, utf8_string, len, NULL, 0); | |
+ if (size == 0) | |
+ return NULL; | |
+ out_str = g_malloc(size * sizeof(TCHAR)); | |
+ if (out_str == NULL) | |
+ return NULL; | |
+ MultiByteToWideChar(CP_UTF8, 0, utf8_string, len, out_str, size); | |
+#else | |
+ // utf8 to ascii | |
+ if (len < 0) | |
+ len = strlen(utf8_string); | |
+ out_str = g_strndup(utf8_string, len); | |
+ for (gsize i = 0; i < len; i++) | |
+ { | |
+ if (out_str[i] > 127) | |
+ out_str[i] = '?'; | |
+ } | |
+#endif | |
+ return out_str; | |
+} | |
+ | |
+ | |
+static inline TCHAR *win32_utf8_to_tchar(const gchar *utf8_string) | |
+{ | |
+ return win32_utf8_to_tchar_len(utf8_string, -1); | |
+} | |
+ | |
+ | |
+// converts a TCHAR string (wchar_t when UNICODE is defined) to a UTF8 string | |
+static gchar *win32_tchar_to_utf8_len(const TCHAR *tchar_string, gssize len) | |
+{ | |
+ gchar *out_str = NULL; | |
+#ifdef UNICODE | |
+ // wide to utf8 | |
+ int size = WideCharToMultiByte(CP_UTF8, 0, tchar_string, len, | |
+ NULL, 0, NULL, NULL); | |
+ if (size == 0) | |
+ return NULL; | |
+ out_str = g_malloc(size); | |
+ if (out_str == NULL) | |
+ return NULL; | |
+ WideCharToMultiByte(CP_UTF8, 0, tchar_string, len, out_str, size, | |
+ NULL, NULL); | |
+#else | |
+ // ascii to utf8 | |
+ if (len < 0) | |
+ len = strlen(tchar_string); | |
+ out_str = g_strndup(tchar_string, len); | |
+#endif | |
+ return out_str; | |
+} | |
+ | |
+ | |
+static inline gchar *win32_tchar_to_utf8(const TCHAR *tchar_string) | |
+{ | |
+ return win32_tchar_to_utf8_len(tchar_string, -1); | |
+} | |
+ | |
+ | |
G_INLINE_FUNC void win32_dialog_reset_timer(HWND hwnd) | |
{ | |
if (G_UNLIKELY(dialog_timer != 0)) | |
@@ -132,11 +201,11 @@ UINT_PTR CALLBACK win32_dialog_hook_proc(HWND dlg, UINT msg, WPARAM wParam, LPAR | |
} | |
-static wchar_t *get_file_filters(void) | |
+static TCHAR *get_file_filters(void) | |
{ | |
gchar *string; | |
guint i, j, len; | |
- static wchar_t title[4096]; | |
+ TCHAR *title; | |
GString *str = g_string_sized_new(100); | |
GString *all_patterns = g_string_sized_new(100); | |
GSList *node; | |
@@ -175,16 +244,17 @@ static wchar_t *get_file_filters(void) | |
g_strdelimit(string, "\t", '\0'); | |
g_assert(string[len - 1] == 0x0); | |
MultiByteToWideChar(CP_UTF8, 0, string, len, title, G_N_ELEMENTS(title)); | |
+ title = win32_utf8_to_tchar_len(string, len); | |
g_free(string); | |
return title; | |
} | |
-static wchar_t *get_file_filter_all_files(void) | |
+static TCHAR *get_file_filter_all_files(void) | |
{ | |
guint len; | |
- static wchar_t title[4096]; | |
+ TCHAR *title; | |
gchar *filter; | |
/* create meta file filter "All files" */ | |
@@ -193,18 +263,18 @@ static wchar_t *get_file_filter_all_files(void) | |
len = strlen(filter); | |
g_strdelimit(filter, "\t", '\0'); | |
g_assert(filter[len - 1] == 0x0); | |
- MultiByteToWideChar(CP_UTF8, 0, filter, len, title, G_N_ELEMENTS(title)); | |
+ title = win32_utf8_to_tchar_len(filter, len); | |
g_free(filter); | |
return title; | |
} | |
-static wchar_t *get_filters(gboolean project_files) | |
+static TCHAR *get_filters(gboolean project_files) | |
{ | |
gchar *string; | |
gint len; | |
- static wchar_t title[1024]; | |
+ TCHAR *title; | |
if (project_files) | |
{ | |
@@ -221,7 +291,7 @@ static wchar_t *get_filters(gboolean project_files) | |
len = strlen(string); | |
g_strdelimit(string, "\t", '\0'); | |
g_assert(string[len - 1] == 0x0); | |
- MultiByteToWideChar(CP_UTF8, 0, string, len, title, G_N_ELEMENTS(title)); | |
+ title = win32_utf8_to_tchar_len(string, len); | |
g_free(string); | |
return title; | |
@@ -230,20 +300,18 @@ static wchar_t *get_filters(gboolean project_files) | |
/* Converts the given UTF-8 filename or directory name into something usable for Windows and | |
* returns the directory part of the given filename. */ | |
-static wchar_t *get_dir_for_path(const gchar *utf8_filename) | |
+static TCHAR *get_dir_for_path(const gchar *utf8_filename) | |
{ | |
- static wchar_t w_dir[MAX_PATH]; | |
+ TCHAR *w_dir; | |
gchar *result; | |
if (g_file_test(utf8_filename, G_FILE_TEST_IS_DIR)) | |
- result = (gchar*) utf8_filename; | |
+ result = g_strdup(utf8_filename); | |
else | |
result = g_path_get_dirname(utf8_filename); | |
- MultiByteToWideChar(CP_UTF8, 0, result, -1, w_dir, G_N_ELEMENTS(w_dir)); | |
- | |
- if (result != utf8_filename) | |
- g_free(result); | |
+ w_dir = win32_utf8_to_tchar(result); | |
+ g_free(result); | |
return w_dir; | |
} | |
@@ -260,16 +328,16 @@ INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) | |
{ | |
case BFFM_INITIALIZED: | |
{ | |
- SendMessageW(hwnd, BFFM_SETSELECTIONW, TRUE, pData); | |
+ SendMessage(hwnd, BFFM_SETSELECTIONW, TRUE, pData); | |
break; | |
} | |
case BFFM_SELCHANGED: | |
{ | |
/* set the status window to the currently selected path. */ | |
- static wchar_t szDir[MAX_PATH]; | |
- if (SHGetPathFromIDListW((LPITEMIDLIST) lp, szDir)) | |
+ TCHAR szDir[MAX_PATH]; | |
+ if (SHGetPathFromIDList((LPITEMIDLIST) lp, szDir)) | |
{ | |
- SendMessageW(hwnd, BFFM_SETSTATUSTEXTW, 0, (LPARAM) szDir); | |
+ SendMessage(hwnd, BFFM_SETSTATUSTEXTW, 0, (LPARAM) szDir); | |
} | |
break; | |
} | |
@@ -286,10 +354,10 @@ gchar *win32_show_folder_dialog(GtkWidget *parent, const gchar *title, const gch | |
BROWSEINFOW bi; | |
LPCITEMIDLIST pidl; | |
gchar *result = NULL; | |
- wchar_t fname[MAX_PATH]; | |
- wchar_t w_title[512]; | |
+ TCHAR fname[MAX_PATH]; | |
+ TCHAR *w_title; | |
- MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title)); | |
+ w_title = win32_utf8_to_tchar(title); | |
if (parent == NULL) | |
parent = main_widgets.window; | |
@@ -302,16 +370,16 @@ gchar *win32_show_folder_dialog(GtkWidget *parent, const gchar *title, const gch | |
bi.lParam = (LPARAM) get_dir_for_path(initial_dir); | |
bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_USENEWUI; | |
- pidl = SHBrowseForFolderW(&bi); | |
+ pidl = SHBrowseForFolder(&bi); | |
+ | |
+ g_free(w_title); | |
+ g_free((gchar*) bi.lParam); | |
/* convert the strange Windows folder list item something into an usual path string ;-) */ | |
if (pidl != 0) | |
{ | |
- if (SHGetPathFromIDListW(pidl, fname)) | |
- { | |
- result = g_malloc0(MAX_PATH * 2); | |
- WideCharToMultiByte(CP_UTF8, 0, fname, -1, result, MAX_PATH * 2, NULL, NULL); | |
- } | |
+ if (SHGetPathFromIDList(pidl, fname)) | |
+ result = win32_tchar_to_utf8(fname); | |
/* SHBrowseForFolder() probably leaks memory here, but how to free the allocated memory? */ | |
} | |
return result; | |
@@ -330,13 +398,17 @@ gchar *win32_show_project_open_dialog(GtkWidget *parent, const gchar *title, | |
{ | |
OPENFILENAMEW of; | |
gint retval; | |
- wchar_t fname[MAX_PATH]; | |
- wchar_t w_title[512]; | |
+ TCHAR fname[MAX_PATH]; | |
+ TCHAR *w_title; | |
+ TCHAR *w_filter; | |
+ TCHAR *w_dir; | |
gchar *filename; | |
fname[0] = '\0'; | |
- MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title)); | |
+ w_title = win32_utf8_to_tchar(title); | |
+ w_filter = get_filters(project_file_filter); | |
+ w_dir = get_dir_for_path(initial_dir); | |
if (parent == NULL) | |
parent = main_widgets.window; | |
@@ -345,23 +417,26 @@ gchar *win32_show_project_open_dialog(GtkWidget *parent, const gchar *title, | |
memset(&of, 0, sizeof of); | |
of.lStructSize = sizeof of; | |
of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(parent)); | |
- of.lpstrFilter = get_filters(project_file_filter); | |
+ of.lpstrFilter = w_filter; | |
of.lpstrCustomFilter = NULL; | |
of.nFilterIndex = 0; | |
of.lpstrFile = fname; | |
- of.lpstrInitialDir = get_dir_for_path(initial_dir); | |
+ of.lpstrInitialDir = w_dir; | |
of.nMaxFile = 2048; | |
of.lpstrFileTitle = NULL; | |
of.lpstrTitle = w_title; | |
- of.lpstrDefExt = L""; | |
+ of.lpstrDefExt = TEXT(""); | |
of.Flags = OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_HIDEREADONLY | | |
OFN_ENABLEHOOK | OFN_ENABLESIZING; | |
of.lpfnHook = win32_dialog_explorer_hook_proc; | |
if (! allow_new_file) | |
of.Flags |= OFN_FILEMUSTEXIST; | |
- retval = GetOpenFileNameW(&of); | |
+ retval = GetOpenFileName(&of); | |
+ g_free(w_title); | |
+ g_free(w_filter); | |
+ g_free(w_dir); | |
if (! retval) | |
{ | |
@@ -374,9 +449,8 @@ gchar *win32_show_project_open_dialog(GtkWidget *parent, const gchar *title, | |
} | |
return NULL; | |
} | |
- /* convert the resulting filename into UTF-8 (from whatever encoding it has at this moment) */ | |
- filename = g_malloc0(MAX_PATH * 2); | |
- WideCharToMultiByte(CP_UTF8, 0, fname, -1, filename, MAX_PATH * 2, NULL, NULL); | |
+ | |
+ filename = win32_tchar_to_utf8(fname); | |
return filename; | |
} | |
@@ -389,23 +463,26 @@ gboolean win32_show_document_open_dialog(GtkWindow *parent, const gchar *title, | |
OPENFILENAMEW of; | |
gint retval; | |
guint x; | |
- gchar tmp[MAX_PATH]; | |
- wchar_t fname[MAX_PATH]; | |
- wchar_t w_dir[MAX_PATH]; | |
- wchar_t w_title[512]; | |
+ TCHAR fname[MAX_PATH]; | |
+ TCHAR *w_dir; | |
+ TCHAR *w_title; | |
+ TCHAR *w_filter; | |
fname[0] = '\0'; | |
if (initial_dir != NULL) | |
- MultiByteToWideChar(CP_UTF8, 0, initial_dir, -1, w_dir, G_N_ELEMENTS(w_dir)); | |
+ w_dir = win32_utf8_to_tchar(initial_dir); | |
+ else | |
+ w_dir = g_malloc0(sizeof(TCHAR)); | |
- MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title)); | |
+ w_title = win32_utf8_to_tchar(title); | |
+ w_filter = get_file_filters(); | |
/* initialise file dialog info struct */ | |
memset(&of, 0, sizeof of); | |
of.lStructSize = sizeof of; | |
of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent))); | |
- of.lpstrFilter = get_file_filters(); | |
+ of.lpstrFilter = w_filter; | |
of.lpstrCustomFilter = NULL; | |
of.nFilterIndex = GEANY_FILETYPES_NONE + 1; | |
@@ -414,12 +491,15 @@ gboolean win32_show_document_open_dialog(GtkWindow *parent, const gchar *title, | |
of.nMaxFile = 2048; | |
of.lpstrFileTitle = NULL; | |
of.lpstrTitle = w_title; | |
- of.lpstrDefExt = L""; | |
+ of.lpstrDefExt = TEXT(""); | |
of.Flags = OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_EXPLORER | | |
OFN_ENABLEHOOK | OFN_ENABLESIZING; | |
of.lpfnHook = win32_dialog_explorer_hook_proc; | |
- retval = GetOpenFileNameW(&of); | |
+ retval = GetOpenFileName(&of); | |
+ g_free(w_dir); | |
+ g_free(w_title); | |
+ g_free(w_filter); | |
if (!retval) | |
{ | |
@@ -429,22 +509,23 @@ gboolean win32_show_document_open_dialog(GtkWindow *parent, const gchar *title, | |
g_snprintf(error, sizeof error, "File dialog box error (%x)", (int)CommDlgExtendedError()); | |
win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error); | |
} | |
+ g_free(w_dir); | |
+ g_free(w_title); | |
return FALSE; | |
} | |
x = of.nFileOffset - 1; | |
if (x != wcslen(fname)) | |
{ /* open a single file */ | |
- WideCharToMultiByte(CP_UTF8, 0, fname, -1, tmp, sizeof(tmp), NULL, NULL); | |
+ gchar *tmp = win32_tchar_to_utf8(fname); | |
document_open_file(tmp, of.Flags & OFN_READONLY, NULL, NULL); | |
+ g_free(tmp); | |
} | |
else | |
{ /* open multiple files */ | |
gchar file_name[MAX_PATH]; | |
- gchar dir_name[MAX_PATH]; | |
+ gchar *dir_name = win32_tchar_to_utf8(fname); | |
- WideCharToMultiByte(CP_UTF8, 0, fname, of.nFileOffset, | |
- dir_name, sizeof(dir_name), NULL, NULL); | |
for (; ;) | |
{ | |
if (! fname[x]) | |
@@ -452,15 +533,17 @@ gboolean win32_show_document_open_dialog(GtkWindow *parent, const gchar *title, | |
if (! fname[x + 1]) | |
break; | |
- WideCharToMultiByte(CP_UTF8, 0, fname + x + 1, -1, | |
- tmp, sizeof(tmp), NULL, NULL); | |
+ gchar *tmp = win32_tchar_to_utf8(fname + x + 1); | |
g_snprintf(file_name, 511, "%s\\%s", dir_name, tmp); | |
+ g_free(tmp); | |
/* convert the resulting filename into UTF-8 */ | |
document_open_file(file_name, of.Flags & OFN_READONLY, NULL, NULL); | |
} | |
x++; | |
} | |
+ | |
+ g_free(dir_name); | |
} | |
return (retval != 0); | |
} | |
@@ -471,33 +554,30 @@ gchar *win32_show_document_save_as_dialog(GtkWindow *parent, const gchar *title, | |
{ | |
OPENFILENAMEW of; | |
gint retval; | |
- gchar tmp[MAX_PATH]; | |
- wchar_t w_file[MAX_PATH]; | |
- wchar_t w_title[512]; | |
+ TCHAR *w_file; | |
+ TCHAR *w_title; | |
+ TCHAR *w_filter; | |
int n; | |
- w_file[0] = '\0'; | |
- | |
- /* Convert the name of the file for of.lpstrFile */ | |
- n = MultiByteToWideChar(CP_UTF8, 0, DOC_FILENAME(doc), -1, w_file, G_N_ELEMENTS(w_file)); | |
- | |
/* If creating a new file name, convert and append the extension if any */ | |
- if (! doc->file_name && doc->file_type && doc->file_type->extension && | |
- n + 1 < (int)G_N_ELEMENTS(w_file)) | |
+ if (! doc->file_name && doc->file_type && doc->file_type->extension) | |
{ | |
- w_file[n - 1] = L'.'; | |
- MultiByteToWideChar(CP_UTF8, 0, doc->file_type->extension, -1, &w_file[n], | |
- G_N_ELEMENTS(w_file) - n - 1); | |
+ gchar *file = g_strdup_printf("%s.%s", DOC_FILENAME(doc), doc->file_type->extension); | |
+ w_file = win32_utf8_to_tchar(file); | |
+ g_free(file); | |
} | |
+ else | |
+ w_file = win32_utf8_to_tchar(DOC_FILENAME(doc)); | |
- MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title)); | |
+ w_title = win32_utf8_to_tchar(title); | |
+ w_filter = get_file_filter_all_files(); | |
/* initialise file dialog info struct */ | |
memset(&of, 0, sizeof of); | |
of.lStructSize = sizeof of; | |
of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent))); | |
- of.lpstrFilter = get_file_filter_all_files(); | |
+ of.lpstrFilter = w_filter; | |
of.lpstrCustomFilter = NULL; | |
of.nFilterIndex = 0; | |
@@ -505,10 +585,13 @@ gchar *win32_show_document_save_as_dialog(GtkWindow *parent, const gchar *title, | |
of.nMaxFile = 2048; | |
of.lpstrFileTitle = NULL; | |
of.lpstrTitle = w_title; | |
- of.lpstrDefExt = L""; | |
+ of.lpstrDefExt = TEXT(""); | |
of.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING; | |
of.lpfnHook = win32_dialog_explorer_hook_proc; | |
- retval = GetSaveFileNameW(&of); | |
+ retval = GetSaveFileName(&of); | |
+ | |
+ g_free(w_title); | |
+ g_free(w_filter); | |
if (! retval) | |
{ | |
@@ -519,12 +602,13 @@ gchar *win32_show_document_save_as_dialog(GtkWindow *parent, const gchar *title, | |
win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error); | |
g_free(error); | |
} | |
+ g_free(w_file); | |
return NULL; | |
} | |
- WideCharToMultiByte(CP_UTF8, 0, w_file, -1, tmp, sizeof(tmp), NULL, NULL); | |
- | |
- return g_strdup(tmp); | |
+ gchar *result = win32_tchar_to_utf8(w_file); | |
+ g_free(w_file); | |
+ return result; | |
} | |
@@ -534,16 +618,15 @@ gchar *win32_show_file_dialog(GtkWindow *parent, const gchar *title, const gchar | |
{ | |
OPENFILENAMEW of; | |
gint retval; | |
- gchar tmp[MAX_PATH]; | |
- wchar_t w_file[MAX_PATH]; | |
- wchar_t w_title[512]; | |
- | |
- w_file[0] = '\0'; | |
+ TCHAR *w_file; | |
+ TCHAR *w_title; | |
if (initial_file != NULL) | |
- MultiByteToWideChar(CP_UTF8, 0, initial_file, -1, w_file, G_N_ELEMENTS(w_file)); | |
+ w_file = win32_utf8_to_tchar(initial_file); | |
+ else | |
+ w_file = g_malloc0(sizeof(TCHAR)); | |
- MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title)); | |
+ w_title = win32_utf8_to_tchar(title); | |
/* initialise file dialog info struct */ | |
memset(&of, 0, sizeof of); | |
@@ -554,10 +637,12 @@ gchar *win32_show_file_dialog(GtkWindow *parent, const gchar *title, const gchar | |
of.nMaxFile = 2048; | |
of.lpstrFileTitle = NULL; | |
of.lpstrTitle = w_title; | |
- of.lpstrDefExt = L""; | |
+ of.lpstrDefExt = TEXT(""); | |
of.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING; | |
of.lpfnHook = win32_dialog_explorer_hook_proc; | |
- retval = GetOpenFileNameW(&of); | |
+ retval = GetOpenFileName(&of); | |
+ | |
+ g_free(w_title); | |
if (! retval) | |
{ | |
@@ -568,12 +653,13 @@ gchar *win32_show_file_dialog(GtkWindow *parent, const gchar *title, const gchar | |
win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error); | |
g_free(error); | |
} | |
+ g_free(w_file); | |
return NULL; | |
} | |
- WideCharToMultiByte(CP_UTF8, 0, w_file, -1, tmp, sizeof(tmp), NULL, NULL); | |
- | |
- return g_strdup(tmp); | |
+ gchar *result = win32_tchar_to_utf8(w_file); | |
+ g_free(w_file); | |
+ return result; | |
} | |
@@ -640,30 +726,32 @@ void win32_show_pref_file_dialog(GtkEntry *item) | |
{ | |
OPENFILENAMEW of; | |
gint retval, len; | |
- wchar_t fname[MAX_PATH]; | |
+ TCHAR *fname; | |
+ TCHAR *w_filter; | |
gchar tmp[MAX_PATH]; | |
- gchar **field, *filename; | |
+ gchar **field; | |
- fname[0] = '\0'; | |
+ w_filter = get_filters(FALSE); | |
/* cut the options from the command line */ | |
field = g_strsplit(gtk_entry_get_text(GTK_ENTRY(item)), " ", 2); | |
if (field[0]) | |
{ | |
- filename = g_find_program_in_path(field[0]); | |
+ gchar *filename = g_find_program_in_path(field[0]); | |
if (filename != NULL && g_file_test(filename, G_FILE_TEST_EXISTS)) | |
- { | |
- MultiByteToWideChar(CP_UTF8, 0, filename, -1, fname, G_N_ELEMENTS(fname)); | |
- g_free(filename); | |
- } | |
+ fname = win32_utf8_to_tchar(filename); | |
+ g_free(filename); | |
} | |
+ if (fname == NULL) | |
+ fname = g_malloc0(sizeof(TCHAR)); | |
+ | |
/* initialize file dialog info struct */ | |
memset(&of, 0, sizeof of); | |
of.lStructSize = sizeof of; | |
of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(ui_widgets.prefs_dialog)); | |
- of.lpstrFilter = get_filters(FALSE); | |
+ of.lpstrFilter = w_filter; | |
of.lpstrCustomFilter = NULL; | |
of.nFilterIndex = 1; | |
@@ -673,11 +761,13 @@ void win32_show_pref_file_dialog(GtkEntry *item) | |
/*of.lpstrInitialDir = g_get_home_dir();*/ | |
of.lpstrInitialDir = NULL; | |
of.lpstrTitle = NULL; | |
- of.lpstrDefExt = L"exe"; | |
+ of.lpstrDefExt = TEXT("exe"); | |
of.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER | | |
OFN_ENABLEHOOK | OFN_ENABLESIZING; | |
of.lpfnHook = win32_dialog_explorer_hook_proc; | |
- retval = GetOpenFileNameW(&of); | |
+ retval = GetOpenFileName(&of); | |
+ | |
+ g_free(w_filter); | |
if (!retval) | |
{ | |
@@ -688,16 +778,21 @@ void win32_show_pref_file_dialog(GtkEntry *item) | |
win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error); | |
} | |
g_strfreev(field); | |
+ g_free(fname); | |
return; | |
} | |
+ // FIXME: use win32_tchar_to_utf8() here | |
len = WideCharToMultiByte(CP_UTF8, 0, fname, -1, tmp, sizeof(tmp), NULL, NULL); | |
if ((of.nFileOffset - 1) != len) | |
{ | |
+ gchar *filename; | |
if (g_strv_length(field) > 1) | |
+ { | |
/* add the command line args of the old command */ | |
/** TODO this fails badly when the old command contained spaces, we need quoting here */ | |
filename = g_strconcat(tmp, " ", field[1], NULL); | |
+ } | |
else | |
{ | |
filename = g_strdup(tmp); | |
@@ -706,6 +801,7 @@ void win32_show_pref_file_dialog(GtkEntry *item) | |
g_free(filename); | |
} | |
g_strfreev(field); | |
+ g_free(fname); | |
} | |
@@ -719,8 +815,8 @@ gboolean win32_message_dialog(GtkWidget *parent, GtkMessageType type, const gcha | |
guint t; | |
const gchar *title; | |
HWND parent_hwnd = NULL; | |
- static wchar_t w_msg[512]; | |
- static wchar_t w_title[512]; | |
+ TCHAR *w_msg; | |
+ TCHAR *w_title; | |
switch (type) | |
{ | |
@@ -751,9 +847,8 @@ gboolean win32_message_dialog(GtkWidget *parent, GtkMessageType type, const gcha | |
} | |
/* convert the Unicode chars to wide chars */ | |
- /** TODO test if LANG == C then possibly skip conversion => g_win32_getlocale() */ | |
- MultiByteToWideChar(CP_UTF8, 0, msg, -1, w_msg, G_N_ELEMENTS(w_msg)); | |
- MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title)); | |
+ w_msg = win32_utf8_to_tchar(msg); | |
+ w_title = win32_utf8_to_tchar(title); | |
if (parent != NULL) | |
parent_hwnd = GDK_WINDOW_HWND(gtk_widget_get_window(parent)); | |
@@ -761,7 +856,9 @@ gboolean win32_message_dialog(GtkWidget *parent, GtkMessageType type, const gcha | |
parent_hwnd = GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets.window)); | |
/* display the message box */ | |
- rc = MessageBoxW(parent_hwnd, w_msg, w_title, t); | |
+ rc = MessageBox(parent_hwnd, w_msg, w_title, t); | |
+ g_free(w_msg); | |
+ g_free(w_title); | |
if (type == GTK_MESSAGE_QUESTION && rc != IDYES) | |
ret = FALSE; | |
@@ -773,7 +870,7 @@ gboolean win32_message_dialog(GtkWidget *parent, GtkMessageType type, const gcha | |
/* Little wrapper for _waccess(), returns errno or 0 if there was no error */ | |
gint win32_check_write_permission(const gchar *dir) | |
{ | |
- static wchar_t w_dir[MAX_PATH]; | |
+ TCHAR w_dir[MAX_PATH]; | |
MultiByteToWideChar(CP_UTF8, 0, dir, -1, w_dir, G_N_ELEMENTS(w_dir)); | |
if (_waccess(w_dir, R_OK | W_OK) != 0) | |
return errno; | |
@@ -794,7 +891,9 @@ void win32_open_browser(const gchar *uri) | |
uri++; | |
} | |
} | |
- ShellExecute(NULL, "open", uri, NULL, NULL, SW_SHOWNORMAL); | |
+ TCHAR *w_uri = win32_utf8_to_tchar(uri); | |
+ ShellExecute(NULL, TEXT("open"), w_uri, NULL, NULL, SW_SHOWNORMAL); | |
+ g_free(w_uri); | |
} | |
@@ -895,7 +994,7 @@ gchar *win32_expand_environment_variables(const gchar *str) | |
/* From GDK (they got it from MS Knowledge Base article Q130698) */ | |
-static gboolean resolve_link(HWND hWnd, wchar_t *link, gchar **lpszPath) | |
+static gboolean resolve_link(HWND hWnd, TCHAR *link, gchar **lpszPath) | |
{ | |
WIN32_FILE_ATTRIBUTE_DATA wfad; | |
HRESULT hres; | |
@@ -906,7 +1005,7 @@ static gboolean resolve_link(HWND hWnd, wchar_t *link, gchar **lpszPath) | |
/* Check if the file is empty first because IShellLink::Resolve for some reason succeeds | |
* with an empty file and returns an empty "link target". (#524151) */ | |
- if (!GetFileAttributesExW(link, GetFileExInfoStandard, &wfad) || | |
+ if (!GetFileAttributesEx(link, GetFileExInfoStandard, &wfad) || | |
(wfad.nFileSizeHigh == 0 && wfad.nFileSizeLow == 0)) | |
{ | |
return FALSE; | |
@@ -943,11 +1042,11 @@ static gboolean resolve_link(HWND hWnd, wchar_t *link, gchar **lpszPath) | |
if (SUCCEEDED(hres)) | |
{ | |
- wchar_t wtarget[MAX_PATH]; | |
+ TCHAR wtarget[MAX_PATH]; | |
hres = pslW->lpVtbl->GetPath(pslW, wtarget, MAX_PATH, NULL, 0); | |
if (SUCCEEDED(hres)) | |
- *lpszPath = g_utf16_to_utf8(wtarget, -1, NULL, NULL, NULL); | |
+ *lpszPath = win32_tchar_to_utf8(wtarget); | |
} | |
if (ppf) | |
@@ -966,7 +1065,7 @@ static gboolean resolve_link(HWND hWnd, wchar_t *link, gchar **lpszPath) | |
gchar *win32_get_shortcut_target(const gchar *file_name) | |
{ | |
gchar *path = NULL; | |
- wchar_t *wfilename = g_utf8_to_utf16(file_name, -1, NULL, NULL, NULL); | |
+ TCHAR *wfilename = win32_utf8_to_tchar(file_name); | |
HWND hWnd = NULL; | |
if (main_widgets.window != NULL) | |
@@ -988,7 +1087,10 @@ gchar *win32_get_shortcut_target(const gchar *file_name) | |
void win32_set_working_directory(const gchar *dir) | |
{ | |
- SetCurrentDirectory(dir); | |
+ g_return_if_fail(dir != NULL); | |
+ TCHAR *w_dir = win32_utf8_to_tchar(dir); | |
+ SetCurrentDirectory(w_dir); | |
+ g_free(w_dir); | |
} | |
@@ -1001,22 +1103,14 @@ gchar *win32_get_installation_dir(void) | |
gchar *win32_get_user_config_dir(void) | |
{ | |
HRESULT hr; | |
- wchar_t path[MAX_PATH]; | |
+ TCHAR path[MAX_PATH]; | |
- hr = SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, L"geany", path); | |
+ hr = SHGetFolderPathAndSubDir(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, TEXT("geany"), path); | |
if (SUCCEEDED(hr)) | |
{ | |
- // GLib always uses UTF-8 for filename encoding on Windows | |
- int u8_size = WideCharToMultiByte(CP_UTF8, 0, path, -1, NULL, 0, NULL, NULL); | |
- if (u8_size > 0) | |
- { | |
- gchar *u8_path = g_malloc0(u8_size + 1); | |
- if (u8_path != NULL && | |
- WideCharToMultiByte(CP_UTF8, 0, path, -1, u8_path, u8_size, NULL, NULL)) | |
- { | |
- return u8_path; | |
- } | |
- } | |
+ gchar *utf8_str = win32_tchar_to_utf8(path); | |
+ if (utf8_str != NULL) | |
+ return utf8_str; | |
} | |
// glib fallback |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment