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.
30 /* Need Windows XP for SHGetFolderPathAndSubDirW */
31 #define _WIN32_WINNT 0x0501
32 /* Needed for SHGFP_TYPE */
33 #define _WIN32_IE 0x0500
42 #include "filetypes.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
);
81 win32_dialog_update_main_window(HWND hwnd
, UINT uMsg
, UINT_PTR idEvent
, DWORD dwTime
)
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
++)
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
)
101 /* Messages that likely mean the window below a dialog needs to be re-drawn. */
102 case WM_WINDOWPOSCHANGED
:
105 case WM_THEMECHANGED
:
108 win32_dialog_reset_timer(dlg
);
109 dialog_timer
= SetTimer(dlg
, 0, 33 /* around 30fps */, win32_dialog_update_main_window
);
112 win32_dialog_update_main_window(dlg
, msg
, wParam
, lParam
);
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)
139 static wchar_t title
[4096];
140 GString
*str
= g_string_sized_new(100);
141 GString
*all_patterns
= g_string_sized_new(100);
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
))
165 tmp
= g_strjoinv(";", ft
->pattern
);
166 g_string_append_printf(str
, "%s\t%s\t", ft
->title
, tmp
);
169 g_string_append_c(str
, '\t'); /* the final \0 byte to mark the end of the string */
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
));
184 static wchar_t *get_file_filter_all_files(void)
187 static wchar_t title
[4096];
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
));
203 static wchar_t *get_filters(gboolean project_files
)
207 static wchar_t title
[1024];
211 string
= g_strconcat(_("Geany project files"), "\t", "*." GEANY_PROJECT_EXT
, "\t",
212 _("All files"), "\t", "*", "\t", NULL
);
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
));
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
];
238 if (g_file_test(utf8_filename
, G_FILE_TEST_IS_DIR
))
239 result
= (gchar
*) utf8_filename
;
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
)
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
);
261 case BFFM_INITIALIZED
:
263 SendMessageW(hwnd
, BFFM_SETSELECTIONW
, TRUE
, pData
);
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
);
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
)
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
));
295 parent
= main_widgets
.window
;
297 memset(&bi
, 0, sizeof bi
);
298 bi
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(parent
));
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 ;-) */
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
);
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
)
333 wchar_t fname
[MAX_PATH
];
334 wchar_t w_title
[512];
339 MultiByteToWideChar(CP_UTF8
, 0, title
, -1, w_title
, G_N_ELEMENTS(w_title
));
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
;
352 of
.lpstrFile
= fname
;
353 of
.lpstrInitialDir
= get_dir_for_path(initial_dir
);
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
);
368 if (CommDlgExtendedError())
371 error
= g_strdup_printf("File dialog box error (%x)", (int)CommDlgExtendedError());
372 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
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
);
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
)
393 wchar_t fname
[MAX_PATH
];
394 wchar_t w_dir
[MAX_PATH
];
395 wchar_t w_title
[512];
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
;
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
);
426 if (CommDlgExtendedError())
429 g_snprintf(error
, sizeof error
, "File dialog box error (%x)", (int)CommDlgExtendedError());
430 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
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
);
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
);
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
);
465 return (retval
!= 0);
469 gchar
*win32_show_document_save_as_dialog(GtkWindow
*parent
, const gchar
*title
,
475 wchar_t w_file
[MAX_PATH
];
476 wchar_t w_title
[512];
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
;
504 of
.lpstrFile
= w_file
;
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
);
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
);
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
)
538 wchar_t w_file
[MAX_PATH
];
539 wchar_t w_title
[512];
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
;
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
);
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
);
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)
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
));
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
);
601 gchar
*editorfont
= g_strdup_printf("%s %d", lf
.lfFaceName
, (cf
.iPointSize
/ 10));
602 ui_set_editor_font(editorfont
);
608 void win32_show_color_dialog(const gchar
*colour
)
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
);
636 void win32_show_pref_file_dialog(GtkEntry
*item
)
640 wchar_t fname
[MAX_PATH
];
642 gchar
**field
, *filename
;
646 /* cut the options from the command line */
647 field
= g_strsplit(gtk_entry_get_text(GTK_ENTRY(item
)), " ", 2);
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
));
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
;
667 of
.lpstrFile
= fname
;
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
);
681 if (CommDlgExtendedError())
684 g_snprintf(error
, sizeof error
, "File dialog box error (%x)", (int)CommDlgExtendedError());
685 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
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
);
700 filename
= g_strdup(tmp
);
702 gtk_entry_set_text(GTK_ENTRY(item
), filename
);
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
)
718 HWND parent_hwnd
= NULL
;
719 static wchar_t w_msg
[512];
720 static wchar_t w_title
[512];
724 case GTK_MESSAGE_ERROR
:
726 t
= MB_OK
| MB_ICONERROR
;
730 case GTK_MESSAGE_QUESTION
:
732 t
= MB_YESNO
| MB_ICONQUESTION
;
733 title
= _("Question");
736 case GTK_MESSAGE_WARNING
:
738 t
= MB_OK
| MB_ICONWARNING
;
739 title
= _("Warning");
744 t
= MB_OK
| MB_ICONINFORMATION
;
745 title
= _("Information");
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
));
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
)
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)
782 /* Just a simple wrapper function to open a browser window */
783 void win32_open_browser(const gchar
*uri
)
786 if (strncmp(uri
, "file://", 7) == 0)
789 if (strchr(uri
, ':') != NULL
)
795 ret
= (gint
) ShellExecute(NULL
, "open", uri
, NULL
, NULL
, SW_SHOWNORMAL
);
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
);
806 static FILE *open_std_handle(DWORD handle
, const char *mode
)
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
);
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
);
828 fp
= _fdopen(hConHandle
, mode
);
831 gchar
*err
= g_win32_error_message(GetLastError());
832 g_warning("_fdopen(%d, \"%s\") failed: %s", hConHandle
, mode
, err
);
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
);
849 static void debug_setup_console(void)
851 static const WORD MAX_CONSOLE_LINES
= 500;
852 CONSOLE_SCREEN_BUFFER_INFO coninfo
;
855 /* allocate a console for this app */
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");
868 /* redirect unbuffered STDERR to the console */
869 fp
= open_std_handle(STD_ERROR_HANDLE
, "w");
873 /* redirect unbuffered STDIN to the console */
874 fp
= open_std_handle(STD_INPUT_HANDLE
, "r");
880 void win32_init_debug_code(void)
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
);
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
;
912 IShellLinkW
*pslW
= NULL
;
913 IPersistFile
*ppf
= 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))
925 /* Assume failure to start with: */
930 hres
= CoCreateInstance(
931 &CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLinkW
, &pslWV
);
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
);
944 ppf
= (IPersistFile
*) ppfV
;
945 hres
= ppf
->lpVtbl
->Load(ppf
, link
, STGM_READ
);
950 /* Resolve the link by calling the Resolve() interface function. */
951 hres
= pslW
->lpVtbl
->Resolve(pslW
, hWnd
, SLR_ANY_MATCH
| SLR_NO_UI
);
956 wchar_t wtarget
[MAX_PATH
];
958 hres
= pslW
->lpVtbl
->GetPath(pslW
, wtarget
, MAX_PATH
, NULL
, 0);
960 *lpszPath
= g_utf16_to_utf8(wtarget
, -1, NULL
, NULL
, NULL
);
964 ppf
->lpVtbl
->Release(ppf
);
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
)
979 wchar_t *wfilename
= g_utf8_to_utf16(file_name
, -1, NULL
, NULL
, NULL
);
982 if (main_widgets
.window
!= NULL
)
984 GdkWindow
*window
= gtk_widget_get_window(main_widgets
.window
);
986 hWnd
= GDK_WINDOW_HWND(window
);
989 resolve_link(hWnd
, wfilename
, &path
);
993 return g_strdup(file_name
);
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)
1014 wchar_t path
[MAX_PATH
];
1016 hr
= SHGetFolderPathAndSubDirW(NULL
, CSIDL_APPDATA
| CSIDL_FLAG_CREATE
, NULL
, SHGFP_TYPE_CURRENT
, L
"geany", path
);
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
);
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
))
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
);