Re-enable SIGTERM handling
[geany-mirror.git] / src / win32.c
blob58775f498e5cd4b943ab6467aff01801b67d78c6
1 /*
2 * win32.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Special functions for the win32 platform, to provide native dialogs.
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 /* Need Windows XP for SHGetFolderPathAndSubDirW */
31 #define _WIN32_WINNT 0x0501
32 /* Needed for SHGFP_TYPE */
33 #define _WIN32_IE 0x0500
35 #include "win32.h"
37 #ifdef G_OS_WIN32
39 #include "dialogs.h"
40 #include "document.h"
41 #include "editor.h"
42 #include "filetypes.h"
43 #include "project.h"
44 #include "support.h"
45 #include "ui_utils.h"
46 #include "utils.h"
48 #include <ctype.h>
49 #include <fcntl.h>
50 #include <io.h>
51 #include <math.h>
52 #include <stdlib.h>
53 #include <string.h>
55 #include <windows.h>
56 #include <commdlg.h>
57 #include <shellapi.h>
58 #include <shlobj.h>
60 #include <glib/gstdio.h>
61 #include <gdk/gdkwin32.h>
64 /* The timer handle used to refresh windows below modal native dialogs. If
65 * ever more than one dialog can be shown at a time, this needs to be changed
66 * to be for specific dialogs. */
67 static UINT_PTR dialog_timer = 0;
70 G_INLINE_FUNC void win32_dialog_reset_timer(HWND hwnd)
72 if (G_UNLIKELY(dialog_timer != 0))
74 KillTimer(hwnd, dialog_timer);
75 dialog_timer = 0;
80 VOID CALLBACK
81 win32_dialog_update_main_window(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
83 gint i;
85 /* Pump the main window loop a bit, but not enough to lock-up.
86 * The typical `while(gtk_events_pending()) gtk_main_iteration();`
87 * loop causes the entire operating system to lock-up. */
88 for (i = 0; i < 4 && gtk_events_pending(); i++)
89 gtk_main_iteration();
91 /* Cancel any pending timers since we just did an update */
92 win32_dialog_reset_timer(hwnd);
96 G_INLINE_FUNC UINT_PTR win32_dialog_queue_main_window_redraw(HWND dlg, UINT msg,
97 WPARAM wParam, LPARAM lParam, gboolean postpone)
99 switch (msg)
101 /* Messages that likely mean the window below a dialog needs to be re-drawn. */
102 case WM_WINDOWPOSCHANGED:
103 case WM_MOVE:
104 case WM_SIZE:
105 case WM_THEMECHANGED:
106 if (postpone)
108 win32_dialog_reset_timer(dlg);
109 dialog_timer = SetTimer(dlg, 0, 33 /* around 30fps */, win32_dialog_update_main_window);
111 else
112 win32_dialog_update_main_window(dlg, msg, wParam, lParam);
113 break;
115 return 0; /* always let the default proc handle it */
119 /* This function is called for OPENFILENAME lpfnHook function and it establishes
120 * a timer that is reset each time which will update the main window loop eventually. */
121 UINT_PTR CALLBACK win32_dialog_explorer_hook_proc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
123 return win32_dialog_queue_main_window_redraw(dlg, msg, wParam, lParam, TRUE);
127 /* This function is called for old-school win32 dialogs that accept a proper
128 * lpfnHook function for all messages, it doesn't use a timer. */
129 UINT_PTR CALLBACK win32_dialog_hook_proc(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam)
131 return win32_dialog_queue_main_window_redraw(dlg, msg, wParam, lParam, FALSE);
135 static wchar_t *get_file_filters(void)
137 gchar *string;
138 guint i, j, len;
139 static wchar_t title[4096];
140 GString *str = g_string_sized_new(100);
141 GString *all_patterns = g_string_sized_new(100);
142 GSList *node;
143 gchar *tmp;
145 /* create meta file filter "All files" */
146 g_string_append_printf(str, "%s\t*\t", _("All files"));
147 /* create meta file filter "All Source" (skip GEANY_FILETYPES_NONE) */
148 for (i = GEANY_FILETYPES_NONE + 1; i < filetypes_array->len; i++)
150 for (j = 0; filetypes[i]->pattern[j] != NULL; j++)
152 g_string_append(all_patterns, filetypes[i]->pattern[j]);
153 g_string_append_c(all_patterns, ';');
156 g_string_append_printf(str, "%s\t%s\t", _("All Source"), all_patterns->str);
157 g_string_free(all_patterns, TRUE);
158 /* add 'usual' filetypes */
159 foreach_slist(node, filetypes_by_title)
161 GeanyFiletype *ft = node->data;
163 if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
164 continue;
165 tmp = g_strjoinv(";", ft->pattern);
166 g_string_append_printf(str, "%s\t%s\t", ft->title, tmp);
167 g_free(tmp);
169 g_string_append_c(str, '\t'); /* the final \0 byte to mark the end of the string */
170 string = str->str;
171 g_string_free(str, FALSE);
173 /* replace all "\t"s by \0 */
174 len = strlen(string);
175 g_strdelimit(string, "\t", '\0');
176 g_assert(string[len - 1] == 0x0);
177 MultiByteToWideChar(CP_UTF8, 0, string, len, title, G_N_ELEMENTS(title));
178 g_free(string);
180 return title;
184 static wchar_t *get_file_filter_all_files(void)
186 guint len;
187 static wchar_t title[4096];
188 gchar *filter;
190 /* create meta file filter "All files" */
191 filter = g_strdup_printf("%s\t*\t", _("All files"));
193 len = strlen(filter);
194 g_strdelimit(filter, "\t", '\0');
195 g_assert(filter[len - 1] == 0x0);
196 MultiByteToWideChar(CP_UTF8, 0, filter, len, title, G_N_ELEMENTS(title));
197 g_free(filter);
199 return title;
203 static wchar_t *get_filters(gboolean project_files)
205 gchar *string;
206 gint len;
207 static wchar_t title[1024];
209 if (project_files)
211 string = g_strconcat(_("Geany project files"), "\t", "*." GEANY_PROJECT_EXT, "\t",
212 _("All files"), "\t", "*", "\t", NULL);
214 else
216 string = g_strconcat(_("Executables"), "\t", "*.exe;*.bat;*.cmd", "\t",
217 _("All files"), "\t", "*", "\t", NULL);
220 /* replace all "\t"s by \0 */
221 len = strlen(string);
222 g_strdelimit(string, "\t", '\0');
223 g_assert(string[len - 1] == 0x0);
224 MultiByteToWideChar(CP_UTF8, 0, string, len, title, G_N_ELEMENTS(title));
225 g_free(string);
227 return title;
231 /* Converts the given UTF-8 filename or directory name into something usable for Windows and
232 * returns the directory part of the given filename. */
233 static wchar_t *get_dir_for_path(const gchar *utf8_filename)
235 static wchar_t w_dir[MAX_PATH];
236 gchar *result;
238 if (g_file_test(utf8_filename, G_FILE_TEST_IS_DIR))
239 result = (gchar*) utf8_filename;
240 else
241 result = g_path_get_dirname(utf8_filename);
243 MultiByteToWideChar(CP_UTF8, 0, result, -1, w_dir, G_N_ELEMENTS(w_dir));
245 if (result != utf8_filename)
246 g_free(result);
248 return w_dir;
252 /* Callback function for setting the initial directory of the folder open dialog. This could also
253 * be done with BROWSEINFO.pidlRoot and SHParseDisplayName but SHParseDisplayName is not available
254 * on systems below Windows XP. So, we go the hard way by creating a callback which will set up the
255 * folder when the dialog is initialised. Yeah, I like Windows. */
256 INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
258 win32_dialog_hook_proc(hwnd, uMsg, lp, pData);
259 switch (uMsg)
261 case BFFM_INITIALIZED:
263 SendMessageW(hwnd, BFFM_SETSELECTIONW, TRUE, pData);
264 break;
266 case BFFM_SELCHANGED:
268 /* set the status window to the currently selected path. */
269 static wchar_t szDir[MAX_PATH];
270 if (SHGetPathFromIDListW((LPITEMIDLIST) lp, szDir))
272 SendMessageW(hwnd, BFFM_SETSTATUSTEXTW, 0, (LPARAM) szDir);
274 break;
277 return 0;
281 /* Shows a folder selection dialog.
282 * initial_dir is expected in UTF-8
283 * The selected folder name is returned. */
284 gchar *win32_show_folder_dialog(GtkWidget *parent, const gchar *title, const gchar *initial_dir)
286 BROWSEINFOW bi;
287 LPITEMIDLIST pidl;
288 gchar *result = NULL;
289 wchar_t fname[MAX_PATH];
290 wchar_t w_title[512];
292 MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title));
294 if (parent == NULL)
295 parent = main_widgets.window;
297 memset(&bi, 0, sizeof bi);
298 bi.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(parent));
299 bi.pidlRoot = NULL;
300 bi.lpszTitle = w_title;
301 bi.lpfn = BrowseCallbackProc;
302 bi.lParam = (LPARAM) get_dir_for_path(initial_dir);
303 bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_USENEWUI;
305 pidl = SHBrowseForFolderW(&bi);
307 /* convert the strange Windows folder list item something into an usual path string ;-) */
308 if (pidl != NULL)
310 if (SHGetPathFromIDListW(pidl, fname))
312 result = g_malloc0(MAX_PATH * 2);
313 WideCharToMultiByte(CP_UTF8, 0, fname, -1, result, MAX_PATH * 2, NULL, NULL);
315 CoTaskMemFree(pidl);
317 return result;
321 /* Shows a file open dialog.
322 * If allow_new_file is set, the file to be opened doesn't have to exist.
323 * initial_dir is expected in UTF-8
324 * The selected file name is returned.
325 * If project_file_filter is set, the file open dialog will have a file filter for Geany project
326 * files, a filter for executables otherwise. */
327 gchar *win32_show_project_open_dialog(GtkWidget *parent, const gchar *title,
328 const gchar *initial_dir, gboolean allow_new_file,
329 gboolean project_file_filter)
331 OPENFILENAMEW of;
332 gint retval;
333 wchar_t fname[MAX_PATH];
334 wchar_t w_title[512];
335 gchar *filename;
337 fname[0] = '\0';
339 MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title));
341 if (parent == NULL)
342 parent = main_widgets.window;
344 /* initialise file dialog info struct */
345 memset(&of, 0, sizeof of);
346 of.lStructSize = sizeof of;
347 of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(parent));
348 of.lpstrFilter = get_filters(project_file_filter);
350 of.lpstrCustomFilter = NULL;
351 of.nFilterIndex = 0;
352 of.lpstrFile = fname;
353 of.lpstrInitialDir = get_dir_for_path(initial_dir);
354 of.nMaxFile = 2048;
355 of.lpstrFileTitle = NULL;
356 of.lpstrTitle = w_title;
357 of.lpstrDefExt = L"";
358 of.Flags = OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_HIDEREADONLY |
359 OFN_ENABLEHOOK | OFN_ENABLESIZING;
360 of.lpfnHook = win32_dialog_explorer_hook_proc;
361 if (! allow_new_file)
362 of.Flags |= OFN_FILEMUSTEXIST;
364 retval = GetOpenFileNameW(&of);
366 if (! retval)
368 if (CommDlgExtendedError())
370 gchar *error;
371 error = g_strdup_printf("File dialog box error (%x)", (int)CommDlgExtendedError());
372 win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
373 g_free(error);
375 return NULL;
377 /* convert the resulting filename into UTF-8 (from whatever encoding it has at this moment) */
378 filename = g_malloc0(MAX_PATH * 2);
379 WideCharToMultiByte(CP_UTF8, 0, fname, -1, filename, MAX_PATH * 2, NULL, NULL);
381 return filename;
385 /* initial_dir can be NULL to use the current working directory.
386 * Returns: TRUE if the dialog was not cancelled. */
387 gboolean win32_show_document_open_dialog(GtkWindow *parent, const gchar *title, const gchar *initial_dir)
389 OPENFILENAMEW of;
390 gint retval;
391 guint x;
392 gchar tmp[MAX_PATH];
393 wchar_t fname[MAX_PATH];
394 wchar_t w_dir[MAX_PATH];
395 wchar_t w_title[512];
397 fname[0] = '\0';
399 if (initial_dir != NULL)
400 MultiByteToWideChar(CP_UTF8, 0, initial_dir, -1, w_dir, G_N_ELEMENTS(w_dir));
402 MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title));
404 /* initialise file dialog info struct */
405 memset(&of, 0, sizeof of);
406 of.lStructSize = sizeof of;
407 of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent)));
408 of.lpstrFilter = get_file_filters();
410 of.lpstrCustomFilter = NULL;
411 of.nFilterIndex = GEANY_FILETYPES_NONE + 1;
412 of.lpstrFile = fname;
413 of.lpstrInitialDir = (initial_dir != NULL) ? w_dir : NULL;
414 of.nMaxFile = 2048;
415 of.lpstrFileTitle = NULL;
416 of.lpstrTitle = w_title;
417 of.lpstrDefExt = L"";
418 of.Flags = OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_EXPLORER |
419 OFN_ENABLEHOOK | OFN_ENABLESIZING;
420 of.lpfnHook = win32_dialog_explorer_hook_proc;
422 retval = GetOpenFileNameW(&of);
424 if (!retval)
426 if (CommDlgExtendedError())
428 gchar error[100];
429 g_snprintf(error, sizeof error, "File dialog box error (%x)", (int)CommDlgExtendedError());
430 win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
432 return FALSE;
435 x = of.nFileOffset - 1;
436 if (x != wcslen(fname))
437 { /* open a single file */
438 WideCharToMultiByte(CP_UTF8, 0, fname, -1, tmp, sizeof(tmp), NULL, NULL);
439 document_open_file(tmp, of.Flags & OFN_READONLY, NULL, NULL);
441 else
442 { /* open multiple files */
443 gchar file_name[MAX_PATH];
444 gchar dir_name[MAX_PATH];
446 WideCharToMultiByte(CP_UTF8, 0, fname, of.nFileOffset,
447 dir_name, sizeof(dir_name), NULL, NULL);
448 for (; ;)
450 if (! fname[x])
452 if (! fname[x + 1])
453 break;
455 WideCharToMultiByte(CP_UTF8, 0, fname + x + 1, -1,
456 tmp, sizeof(tmp), NULL, NULL);
457 g_snprintf(file_name, 511, "%s\\%s", dir_name, tmp);
459 /* convert the resulting filename into UTF-8 */
460 document_open_file(file_name, of.Flags & OFN_READONLY, NULL, NULL);
462 x++;
465 return (retval != 0);
469 gchar *win32_show_document_save_as_dialog(GtkWindow *parent, const gchar *title,
470 GeanyDocument *doc)
472 OPENFILENAMEW of;
473 gint retval;
474 gchar tmp[MAX_PATH];
475 wchar_t w_file[MAX_PATH];
476 wchar_t w_title[512];
477 int n;
479 w_file[0] = '\0';
481 /* Convert the name of the file for of.lpstrFile */
482 n = MultiByteToWideChar(CP_UTF8, 0, DOC_FILENAME(doc), -1, w_file, G_N_ELEMENTS(w_file));
484 /* If creating a new file name, convert and append the extension if any */
485 if (! doc->file_name && doc->file_type && doc->file_type->extension &&
486 n + 1 < (int)G_N_ELEMENTS(w_file))
488 w_file[n - 1] = L'.';
489 MultiByteToWideChar(CP_UTF8, 0, doc->file_type->extension, -1, &w_file[n],
490 G_N_ELEMENTS(w_file) - n - 1);
493 MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title));
495 /* initialise file dialog info struct */
496 memset(&of, 0, sizeof of);
497 of.lStructSize = sizeof of;
498 of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent)));
500 of.lpstrFilter = get_file_filter_all_files();
501 of.lpstrCustomFilter = NULL;
502 of.nFilterIndex = 0;
504 of.lpstrFile = w_file;
505 of.nMaxFile = 2048;
506 of.lpstrFileTitle = NULL;
507 of.lpstrTitle = w_title;
508 of.lpstrDefExt = L"";
509 of.Flags = OFN_OVERWRITEPROMPT | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING;
510 of.lpfnHook = win32_dialog_explorer_hook_proc;
511 retval = GetSaveFileNameW(&of);
513 if (! retval)
515 if (CommDlgExtendedError())
517 gchar *error = g_strdup_printf(
518 "File dialog box error (%x)", (gint) CommDlgExtendedError());
519 win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
520 g_free(error);
522 return NULL;
525 WideCharToMultiByte(CP_UTF8, 0, w_file, -1, tmp, sizeof(tmp), NULL, NULL);
527 return g_strdup(tmp);
531 /* initial_dir can be NULL to use the current working directory.
532 * Returns: the selected filename */
533 gchar *win32_show_file_dialog(GtkWindow *parent, const gchar *title, const gchar *initial_file)
535 OPENFILENAMEW of;
536 gint retval;
537 gchar tmp[MAX_PATH];
538 wchar_t w_file[MAX_PATH];
539 wchar_t w_title[512];
541 w_file[0] = '\0';
543 if (initial_file != NULL)
544 MultiByteToWideChar(CP_UTF8, 0, initial_file, -1, w_file, G_N_ELEMENTS(w_file));
546 MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title));
548 /* initialise file dialog info struct */
549 memset(&of, 0, sizeof of);
550 of.lStructSize = sizeof of;
551 of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent)));
553 of.lpstrFile = w_file;
554 of.nMaxFile = 2048;
555 of.lpstrFileTitle = NULL;
556 of.lpstrTitle = w_title;
557 of.lpstrDefExt = L"";
558 of.Flags = OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING;
559 of.lpfnHook = win32_dialog_explorer_hook_proc;
560 retval = GetOpenFileNameW(&of);
562 if (! retval)
564 if (CommDlgExtendedError())
566 gchar *error = g_strdup_printf(
567 "File dialog box error (%x)", (gint) CommDlgExtendedError());
568 win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
569 g_free(error);
571 return NULL;
574 WideCharToMultiByte(CP_UTF8, 0, w_file, -1, tmp, sizeof(tmp), NULL, NULL);
576 return g_strdup(tmp);
580 void win32_show_font_dialog(void)
582 gint retval;
583 CHOOSEFONT cf;
584 LOGFONT lf; /* logical font structure */
586 memset(&lf, 0, sizeof lf);
587 /* TODO: init lf members */
589 memset(&cf, 0, sizeof cf);
590 cf.lStructSize = sizeof cf;
591 cf.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets.window));
592 cf.lpLogFont = &lf;
593 /* support CF_APPLY? */
594 cf.Flags = CF_NOSCRIPTSEL | CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS | CF_ENABLEHOOK;
595 cf.lpfnHook = win32_dialog_hook_proc;
597 retval = ChooseFont(&cf);
599 if (retval)
601 gchar *editorfont = g_strdup_printf("%s %d", lf.lfFaceName, (cf.iPointSize / 10));
602 ui_set_editor_font(editorfont);
603 g_free(editorfont);
608 void win32_show_color_dialog(const gchar *colour)
610 CHOOSECOLOR cc;
611 static COLORREF acr_cust_clr[16];
612 static DWORD rgb_current;
613 gchar *hex = g_malloc0(12);
614 GeanyDocument *doc = document_get_current();
616 /* Initialize CHOOSECOLOR */
617 memset(&cc, 0, sizeof cc);
618 cc.lStructSize = sizeof(cc);
619 cc.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets.window));
620 cc.lpCustColors = (LPDWORD) acr_cust_clr;
621 cc.rgbResult = (colour != NULL) ? utils_parse_color_to_bgr(colour) : 0;
622 cc.Flags = CC_FULLOPEN | CC_RGBINIT | CC_ENABLEHOOK;
623 cc.lpfnHook = win32_dialog_hook_proc;
625 if (ChooseColor(&cc))
627 rgb_current = cc.rgbResult;
628 g_snprintf(hex, 11, "#%02X%02X%02X",
629 GetRValue(rgb_current), GetGValue(rgb_current), GetBValue(rgb_current));
630 editor_insert_color(doc->editor, hex);
632 g_free(hex);
636 void win32_show_pref_file_dialog(GtkEntry *item)
638 OPENFILENAMEW of;
639 gint retval, len;
640 wchar_t fname[MAX_PATH];
641 gchar tmp[MAX_PATH];
642 gchar **field, *filename;
644 fname[0] = '\0';
646 /* cut the options from the command line */
647 field = g_strsplit(gtk_entry_get_text(GTK_ENTRY(item)), " ", 2);
648 if (field[0])
650 filename = g_find_program_in_path(field[0]);
651 if (filename != NULL && g_file_test(filename, G_FILE_TEST_EXISTS))
653 MultiByteToWideChar(CP_UTF8, 0, filename, -1, fname, G_N_ELEMENTS(fname));
654 g_free(filename);
658 /* initialize file dialog info struct */
659 memset(&of, 0, sizeof of);
660 of.lStructSize = sizeof of;
661 of.hwndOwner = GDK_WINDOW_HWND(gtk_widget_get_window(ui_widgets.prefs_dialog));
663 of.lpstrFilter = get_filters(FALSE);
664 of.lpstrCustomFilter = NULL;
665 of.nFilterIndex = 1;
667 of.lpstrFile = fname;
668 of.nMaxFile = 2048;
669 of.lpstrFileTitle = NULL;
670 /*of.lpstrInitialDir = g_get_home_dir();*/
671 of.lpstrInitialDir = NULL;
672 of.lpstrTitle = NULL;
673 of.lpstrDefExt = L"exe";
674 of.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER |
675 OFN_ENABLEHOOK | OFN_ENABLESIZING;
676 of.lpfnHook = win32_dialog_explorer_hook_proc;
677 retval = GetOpenFileNameW(&of);
679 if (!retval)
681 if (CommDlgExtendedError())
683 gchar error[100];
684 g_snprintf(error, sizeof error, "File dialog box error (%x)", (int)CommDlgExtendedError());
685 win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
687 g_strfreev(field);
688 return;
691 len = WideCharToMultiByte(CP_UTF8, 0, fname, -1, tmp, sizeof(tmp), NULL, NULL);
692 if ((of.nFileOffset - 1) != len)
694 if (g_strv_length(field) > 1)
695 /* add the command line args of the old command */
696 /** TODO this fails badly when the old command contained spaces, we need quoting here */
697 filename = g_strconcat(tmp, " ", field[1], NULL);
698 else
700 filename = g_strdup(tmp);
702 gtk_entry_set_text(GTK_ENTRY(item), filename);
703 g_free(filename);
705 g_strfreev(field);
709 /* Creates a native Windows message box of the given type and returns always TRUE
710 * or FALSE representing th pressed Yes or No button.
711 * If type is not GTK_MESSAGE_QUESTION, it returns always TRUE. */
712 gboolean win32_message_dialog(GtkWidget *parent, GtkMessageType type, const gchar *msg)
714 gboolean ret = TRUE;
715 gint rc;
716 guint t;
717 const gchar *title;
718 HWND parent_hwnd = NULL;
719 static wchar_t w_msg[512];
720 static wchar_t w_title[512];
722 switch (type)
724 case GTK_MESSAGE_ERROR:
726 t = MB_OK | MB_ICONERROR;
727 title = _("Error");
728 break;
730 case GTK_MESSAGE_QUESTION:
732 t = MB_YESNO | MB_ICONQUESTION;
733 title = _("Question");
734 break;
736 case GTK_MESSAGE_WARNING:
738 t = MB_OK | MB_ICONWARNING;
739 title = _("Warning");
740 break;
742 default:
744 t = MB_OK | MB_ICONINFORMATION;
745 title = _("Information");
746 break;
750 /* convert the Unicode chars to wide chars */
751 /** TODO test if LANG == C then possibly skip conversion => g_win32_getlocale() */
752 MultiByteToWideChar(CP_UTF8, 0, msg, -1, w_msg, G_N_ELEMENTS(w_msg));
753 MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, G_N_ELEMENTS(w_title));
755 if (parent != NULL)
756 parent_hwnd = GDK_WINDOW_HWND(gtk_widget_get_window(parent));
757 else if (main_widgets.window != NULL)
758 parent_hwnd = GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets.window));
760 /* display the message box */
761 rc = MessageBoxW(parent_hwnd, w_msg, w_title, t);
763 if (type == GTK_MESSAGE_QUESTION && rc != IDYES)
764 ret = FALSE;
766 return ret;
770 /* Little wrapper for _waccess(), returns errno or 0 if there was no error */
771 gint win32_check_write_permission(const gchar *dir)
773 static wchar_t w_dir[MAX_PATH];
774 MultiByteToWideChar(CP_UTF8, 0, dir, -1, w_dir, G_N_ELEMENTS(w_dir));
775 if (_waccess(w_dir, R_OK | W_OK) != 0)
776 return errno;
777 else
778 return 0;
782 /* Just a simple wrapper function to open a browser window */
783 void win32_open_browser(const gchar *uri)
785 gint ret;
786 if (strncmp(uri, "file://", 7) == 0)
788 uri += 7;
789 if (strchr(uri, ':') != NULL)
791 while (*uri == '/')
792 uri++;
795 ret = (gint) ShellExecute(NULL, "open", uri, NULL, NULL, SW_SHOWNORMAL);
796 if (ret <= 32)
798 gchar *err = g_win32_error_message(GetLastError());
799 ui_set_statusbar(TRUE, _("Failed to open URI \"%s\": %s"), uri, err);
800 g_warning("ShellExecute failed opening \"%s\" (code %d): %s", uri, ret, err);
801 g_free(err);
806 static FILE *open_std_handle(DWORD handle, const char *mode)
808 HANDLE lStdHandle;
809 int hConHandle;
810 FILE *fp;
812 lStdHandle = GetStdHandle(handle);
813 if (lStdHandle == INVALID_HANDLE_VALUE)
815 gchar *err = g_win32_error_message(GetLastError());
816 g_warning("GetStdHandle(%ld) failed: %s", (long)handle, err);
817 g_free(err);
818 return NULL;
820 hConHandle = _open_osfhandle((intptr_t)lStdHandle, _O_TEXT);
821 if (hConHandle == -1)
823 gchar *err = g_win32_error_message(GetLastError());
824 g_warning("_open_osfhandle(handle(%ld), _O_TEXT) failed: %s", (long)handle, err);
825 g_free(err);
826 return NULL;
828 fp = _fdopen(hConHandle, mode);
829 if (! fp)
831 gchar *err = g_win32_error_message(GetLastError());
832 g_warning("_fdopen(%d, \"%s\") failed: %s", hConHandle, mode, err);
833 g_free(err);
834 return NULL;
836 if (setvbuf(fp, NULL, _IONBF, 0) != 0)
838 gchar *err = g_win32_error_message(GetLastError());
839 g_warning("setvbuf(%p, NULL, _IONBF, 0) failed: %s", fp, err);
840 g_free(err);
841 fclose(fp);
842 return NULL;
845 return fp;
849 static void debug_setup_console(void)
851 static const WORD MAX_CONSOLE_LINES = 500;
852 CONSOLE_SCREEN_BUFFER_INFO coninfo;
853 FILE *fp;
855 /* allocate a console for this app */
856 AllocConsole();
858 /* set the screen buffer to be big enough to let us scroll text */
859 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
860 coninfo.dwSize.Y = MAX_CONSOLE_LINES;
861 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
863 /* redirect unbuffered STDOUT to the console */
864 fp = open_std_handle(STD_OUTPUT_HANDLE, "w");
865 if (fp)
866 *stdout = *fp;
868 /* redirect unbuffered STDERR to the console */
869 fp = open_std_handle(STD_ERROR_HANDLE, "w");
870 if (fp)
871 *stderr = *fp;
873 /* redirect unbuffered STDIN to the console */
874 fp = open_std_handle(STD_INPUT_HANDLE, "r");
875 if (fp)
876 *stdin = *fp;
880 void win32_init_debug_code(void)
882 if (app->debug_mode)
884 /* create a console window to get log messages on Windows,
885 * especially useful when generating tags files */
886 debug_setup_console();
891 /* expands environment placeholders in @str. input and output is in UTF-8 */
892 gchar *win32_expand_environment_variables(const gchar *str)
894 wchar_t *cmdline = g_utf8_to_utf16(str, -1, NULL, NULL, NULL);
895 wchar_t expCmdline[32768]; /* 32768 is the limit for ExpandEnvironmentStrings() */
896 gchar *expanded = NULL;
898 if (cmdline && ExpandEnvironmentStringsW(cmdline, expCmdline, sizeof(expCmdline)) != 0)
899 expanded = g_utf16_to_utf8(expCmdline, -1, NULL, NULL, NULL);
901 g_free(cmdline);
903 return expanded ? expanded : g_strdup(str);
907 /* From GDK (they got it from MS Knowledge Base article Q130698) */
908 static gboolean resolve_link(HWND hWnd, wchar_t *link, gchar **lpszPath)
910 WIN32_FILE_ATTRIBUTE_DATA wfad;
911 HRESULT hres;
912 IShellLinkW *pslW = NULL;
913 IPersistFile *ppf = NULL;
914 LPVOID pslWV = NULL;
915 LPVOID ppfV = NULL;
917 /* Check if the file is empty first because IShellLink::Resolve for some reason succeeds
918 * with an empty file and returns an empty "link target". (#524151) */
919 if (!GetFileAttributesExW(link, GetFileExInfoStandard, &wfad) ||
920 (wfad.nFileSizeHigh == 0 && wfad.nFileSizeLow == 0))
922 return FALSE;
925 /* Assume failure to start with: */
926 *lpszPath = 0;
928 CoInitialize(NULL);
930 hres = CoCreateInstance(
931 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, &pslWV);
933 if (SUCCEEDED(hres))
935 /* The IShellLink interface supports the IPersistFile interface.
936 * Get an interface pointer to it. */
937 pslW = (IShellLinkW*) pslWV;
938 hres = pslW->lpVtbl->QueryInterface(pslW, &IID_IPersistFile, &ppfV);
941 if (SUCCEEDED(hres))
943 /* Load the file. */
944 ppf = (IPersistFile*) ppfV;
945 hres = ppf->lpVtbl->Load(ppf, link, STGM_READ);
948 if (SUCCEEDED(hres))
950 /* Resolve the link by calling the Resolve() interface function. */
951 hres = pslW->lpVtbl->Resolve(pslW, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
954 if (SUCCEEDED(hres))
956 wchar_t wtarget[MAX_PATH];
958 hres = pslW->lpVtbl->GetPath(pslW, wtarget, MAX_PATH, NULL, 0);
959 if (SUCCEEDED(hres))
960 *lpszPath = g_utf16_to_utf8(wtarget, -1, NULL, NULL, NULL);
963 if (ppf)
964 ppf->lpVtbl->Release(ppf);
966 if (pslW)
967 pslW->lpVtbl->Release(pslW);
969 return SUCCEEDED(hres);
973 /* Checks whether file_name is a Windows shortcut. file_name is expected in UTF-8 encoding.
974 * If file_name is a Windows shortcut, it returns the target in UTF-8 encoding.
975 * If it is not a shortcut, it returns a newly allocated copy of file_name. */
976 gchar *win32_get_shortcut_target(const gchar *file_name)
978 gchar *path = NULL;
979 wchar_t *wfilename = g_utf8_to_utf16(file_name, -1, NULL, NULL, NULL);
980 HWND hWnd = NULL;
982 if (main_widgets.window != NULL)
984 GdkWindow *window = gtk_widget_get_window(main_widgets.window);
985 if (window != NULL)
986 hWnd = GDK_WINDOW_HWND(window);
989 resolve_link(hWnd, wfilename, &path);
990 g_free(wfilename);
992 if (path == NULL)
993 return g_strdup(file_name);
994 else
995 return path;
999 void win32_set_working_directory(const gchar *dir)
1001 SetCurrentDirectory(dir);
1005 gchar *win32_get_installation_dir(void)
1007 return g_win32_get_package_installation_directory_of_module(NULL);
1011 gchar *win32_get_user_config_dir(void)
1013 HRESULT hr;
1014 wchar_t path[MAX_PATH];
1016 hr = SHGetFolderPathAndSubDirW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, L"geany", path);
1017 if (SUCCEEDED(hr))
1019 // GLib always uses UTF-8 for filename encoding on Windows
1020 int u8_size = WideCharToMultiByte(CP_UTF8, 0, path, -1, NULL, 0, NULL, NULL);
1021 if (u8_size > 0)
1023 gchar *u8_path = g_malloc0(u8_size + 1);
1024 if (u8_path != NULL &&
1025 WideCharToMultiByte(CP_UTF8, 0, path, -1, u8_path, u8_size, NULL, NULL))
1027 return u8_path;
1032 // glib fallback
1033 g_warning("Failed to retrieve Windows config dir, falling back to default");
1034 return g_build_filename(g_get_user_config_dir(), "geany", NULL);
1037 #endif