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.
37 #include "filetypes.h"
51 #define WIN32_LEAN_AND_MEAN
57 #include <glib/gstdio.h>
58 #include <gdk/gdkwin32.h>
63 struct _geany_win32_spawn
67 HANDLE hChildStdoutRd
;
68 HANDLE hChildStdoutWr
;
69 HANDLE hChildStderrRd
;
70 HANDLE hChildStderrWr
;
77 typedef struct _geany_win32_spawn geany_win32_spawn
;
79 static gboolean
GetContentFromHandle(HANDLE hFile
, gchar
**content
, GError
**error
);
80 static HANDLE
GetTempFileHandle(GError
**error
);
81 static gboolean
CreateChildProcess(geany_win32_spawn
*gw_spawn
, TCHAR
*szCmdline
,
82 const TCHAR
*dir
, GError
**error
);
83 static VOID
ReadFromPipe(HANDLE hRead
, HANDLE hWrite
, HANDLE hFile
, GError
**error
);
86 static wchar_t *get_file_filters(void)
90 static wchar_t title
[4096];
91 GString
*str
= g_string_sized_new(100);
92 GString
*all_patterns
= g_string_sized_new(100);
96 /* create meta file filter "All files" */
97 g_string_append_printf(str
, "%s\t*\t", _("All files"));
98 /* create meta file filter "All Source" (skip GEANY_FILETYPES_NONE) */
99 for (i
= GEANY_FILETYPES_NONE
+ 1; i
< filetypes_array
->len
; i
++)
101 for (j
= 0; filetypes
[i
]->pattern
[j
] != NULL
; j
++)
103 g_string_append(all_patterns
, filetypes
[i
]->pattern
[j
]);
104 g_string_append_c(all_patterns
, ';');
107 g_string_append_printf(str
, "%s\t%s\t", _("All Source"), all_patterns
->str
);
108 g_string_free(all_patterns
, TRUE
);
109 /* add 'usual' filetypes */
110 foreach_slist(node
, filetypes_by_title
)
112 GeanyFiletype
*ft
= node
->data
;
114 if (G_UNLIKELY(ft
->id
== GEANY_FILETYPES_NONE
))
116 tmp
= g_strjoinv(";", ft
->pattern
);
117 g_string_append_printf(str
, "%s\t%s\t", ft
->title
, tmp
);
120 g_string_append_c(str
, '\t'); /* the final \0 byte to mark the end of the string */
122 g_string_free(str
, FALSE
);
124 /* replace all "\t"s by \0 */
125 len
= strlen(string
);
126 g_strdelimit(string
, "\t", '\0');
127 g_assert(string
[len
- 1] == 0x0);
128 MultiByteToWideChar(CP_UTF8
, 0, string
, len
, title
, G_N_ELEMENTS(title
));
135 static wchar_t *get_file_filter_all_files(void)
138 static wchar_t title
[4096];
141 /* create meta file filter "All files" */
142 filter
= g_strdup_printf("%s\t*\t", _("All files"));
144 len
= strlen(filter
);
145 g_strdelimit(filter
, "\t", '\0');
146 g_assert(filter
[len
- 1] == 0x0);
147 MultiByteToWideChar(CP_UTF8
, 0, filter
, len
, title
, G_N_ELEMENTS(title
));
154 static wchar_t *get_filters(gboolean project_files
)
158 static wchar_t title
[1024];
162 string
= g_strconcat(_("Geany project files"), "\t", "*." GEANY_PROJECT_EXT
, "\t",
163 _("All files"), "\t", "*", "\t", NULL
);
167 string
= g_strconcat(_("Executables"), "\t", "*.exe;*.bat;*.cmd", "\t",
168 _("All files"), "\t", "*", "\t", NULL
);
171 /* replace all "\t"s by \0 */
172 len
= strlen(string
);
173 g_strdelimit(string
, "\t", '\0');
174 g_assert(string
[len
- 1] == 0x0);
175 MultiByteToWideChar(CP_UTF8
, 0, string
, len
, title
, G_N_ELEMENTS(title
));
182 /* Converts the given UTF-8 filename or directory name into something usable for Windows and
183 * returns the directory part of the given filename. */
184 static wchar_t *get_dir_for_path(const gchar
*utf8_filename
)
186 static wchar_t w_dir
[MAX_PATH
];
189 if (g_file_test(utf8_filename
, G_FILE_TEST_IS_DIR
))
190 result
= (gchar
*) utf8_filename
;
192 result
= g_path_get_dirname(utf8_filename
);
194 MultiByteToWideChar(CP_UTF8
, 0, result
, -1, w_dir
, G_N_ELEMENTS(w_dir
));
196 if (result
!= utf8_filename
)
203 /* Callback function for setting the initial directory of the folder open dialog. This could also
204 * be done with BROWSEINFO.pidlRoot and SHParseDisplayName but SHParseDisplayName is not available
205 * on systems below Windows XP. So, we go the hard way by creating a callback which will set up the
206 * folder when the dialog is initialised. Yeah, I like Windows. */
207 INT CALLBACK
BrowseCallbackProc(HWND hwnd
, UINT uMsg
, LPARAM lp
, LPARAM pData
)
211 case BFFM_INITIALIZED
:
213 SendMessageW(hwnd
, BFFM_SETSELECTIONW
, TRUE
, pData
);
216 case BFFM_SELCHANGED
:
218 /* set the status window to the currently selected path. */
219 static wchar_t szDir
[MAX_PATH
];
220 if (SHGetPathFromIDListW((LPITEMIDLIST
) lp
, szDir
))
222 SendMessageW(hwnd
, BFFM_SETSTATUSTEXTW
, 0, (LPARAM
) szDir
);
231 /* Shows a folder selection dialog.
232 * initial_dir is expected in UTF-8
233 * The selected folder name is returned. */
234 gchar
*win32_show_folder_dialog(GtkWidget
*parent
, const gchar
*title
, const gchar
*initial_dir
)
238 gchar
*result
= NULL
;
239 wchar_t fname
[MAX_PATH
];
240 wchar_t w_title
[512];
242 MultiByteToWideChar(CP_UTF8
, 0, title
, -1, w_title
, G_N_ELEMENTS(w_title
));
245 parent
= main_widgets
.window
;
247 memset(&bi
, 0, sizeof bi
);
248 bi
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(parent
));
250 bi
.lpszTitle
= w_title
;
251 bi
.lpfn
= BrowseCallbackProc
;
252 bi
.lParam
= (LPARAM
) get_dir_for_path(initial_dir
);
253 bi
.ulFlags
= BIF_DONTGOBELOWDOMAIN
| BIF_RETURNONLYFSDIRS
| BIF_STATUSTEXT
;
255 pidl
= SHBrowseForFolderW(&bi
);
257 /* convert the strange Windows folder list item something into an usual path string ;-) */
260 if (SHGetPathFromIDListW(pidl
, fname
))
262 result
= g_malloc0(MAX_PATH
* 2);
263 WideCharToMultiByte(CP_UTF8
, 0, fname
, -1, result
, MAX_PATH
* 2, NULL
, NULL
);
265 /* SHBrowseForFolder() probably leaks memory here, but how to free the allocated memory? */
271 /* Shows a file open dialog.
272 * If allow_new_file is set, the file to be opened doesn't have to exist.
273 * initial_dir is expected in UTF-8
274 * The selected file name is returned.
275 * If project_file_filter is set, the file open dialog will have a file filter for Geany project
276 * files, a filter for executables otherwise. */
277 gchar
*win32_show_project_open_dialog(GtkWidget
*parent
, const gchar
*title
,
278 const gchar
*initial_dir
, gboolean allow_new_file
,
279 gboolean project_file_filter
)
283 wchar_t fname
[MAX_PATH
];
284 wchar_t w_title
[512];
289 MultiByteToWideChar(CP_UTF8
, 0, title
, -1, w_title
, G_N_ELEMENTS(w_title
));
292 parent
= main_widgets
.window
;
294 /* initialise file dialog info struct */
295 memset(&of
, 0, sizeof of
);
296 #ifdef OPENFILENAME_SIZE_VERSION_400
297 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
299 of
.lStructSize
= sizeof of
;
301 of
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(parent
));
302 of
.lpstrFilter
= get_filters(project_file_filter
);
304 of
.lpstrCustomFilter
= NULL
;
306 of
.lpstrFile
= fname
;
307 of
.lpstrInitialDir
= get_dir_for_path(initial_dir
);
309 of
.lpstrFileTitle
= NULL
;
310 of
.lpstrTitle
= w_title
;
311 of
.lpstrDefExt
= L
"";
312 of
.Flags
= OFN_PATHMUSTEXIST
| OFN_EXPLORER
| OFN_HIDEREADONLY
;
313 if (! allow_new_file
)
314 of
.Flags
|= OFN_FILEMUSTEXIST
;
316 retval
= GetOpenFileNameW(&of
);
320 if (CommDlgExtendedError())
323 error
= g_strdup_printf("File dialog box error (%x)", (int)CommDlgExtendedError());
324 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
329 /* convert the resulting filename into UTF-8 (from whatever encoding it has at this moment) */
330 filename
= g_malloc0(MAX_PATH
* 2);
331 WideCharToMultiByte(CP_UTF8
, 0, fname
, -1, filename
, MAX_PATH
* 2, NULL
, NULL
);
337 /* initial_dir can be NULL to use the current working directory.
338 * Returns: TRUE if the dialog was not cancelled. */
339 gboolean
win32_show_document_open_dialog(GtkWindow
*parent
, const gchar
*title
, const gchar
*initial_dir
)
345 wchar_t fname
[MAX_PATH
];
346 wchar_t w_dir
[MAX_PATH
];
347 wchar_t w_title
[512];
351 if (initial_dir
!= NULL
)
352 MultiByteToWideChar(CP_UTF8
, 0, initial_dir
, -1, w_dir
, G_N_ELEMENTS(w_dir
));
354 MultiByteToWideChar(CP_UTF8
, 0, title
, -1, w_title
, G_N_ELEMENTS(w_title
));
356 /* initialise file dialog info struct */
357 memset(&of
, 0, sizeof of
);
358 #ifdef OPENFILENAME_SIZE_VERSION_400
359 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
361 of
.lStructSize
= sizeof of
;
363 of
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent
)));
364 of
.lpstrFilter
= get_file_filters();
366 of
.lpstrCustomFilter
= NULL
;
367 of
.nFilterIndex
= GEANY_FILETYPES_NONE
+ 1;
368 of
.lpstrFile
= fname
;
369 of
.lpstrInitialDir
= (initial_dir
!= NULL
) ? w_dir
: NULL
;
371 of
.lpstrFileTitle
= NULL
;
372 of
.lpstrTitle
= w_title
;
373 of
.lpstrDefExt
= L
"";
374 of
.Flags
= OFN_ALLOWMULTISELECT
| OFN_FILEMUSTEXIST
| OFN_EXPLORER
;
376 retval
= GetOpenFileNameW(&of
);
380 if (CommDlgExtendedError())
383 g_snprintf(error
, sizeof error
, "File dialog box error (%x)", (int)CommDlgExtendedError());
384 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
389 x
= of
.nFileOffset
- 1;
390 if (x
!= wcslen(fname
))
391 { /* open a single file */
392 WideCharToMultiByte(CP_UTF8
, 0, fname
, -1, tmp
, sizeof(tmp
), NULL
, NULL
);
393 document_open_file(tmp
, of
.Flags
& OFN_READONLY
, NULL
, NULL
);
396 { /* open multiple files */
397 gchar file_name
[MAX_PATH
];
398 gchar dir_name
[MAX_PATH
];
400 WideCharToMultiByte(CP_UTF8
, 0, fname
, of
.nFileOffset
,
401 dir_name
, sizeof(dir_name
), NULL
, NULL
);
409 WideCharToMultiByte(CP_UTF8
, 0, fname
+ x
+ 1, -1,
410 tmp
, sizeof(tmp
), NULL
, NULL
);
411 g_snprintf(file_name
, 511, "%s\\%s", dir_name
, tmp
);
413 /* convert the resulting filename into UTF-8 */
414 document_open_file(file_name
, of
.Flags
& OFN_READONLY
, NULL
, NULL
);
419 return (retval
!= 0);
423 gchar
*win32_show_document_save_as_dialog(GtkWindow
*parent
, const gchar
*title
,
429 wchar_t w_file
[MAX_PATH
];
430 wchar_t w_title
[512];
435 /* Convert the name of the file for of.lpstrFile */
436 n
= MultiByteToWideChar(CP_UTF8
, 0, DOC_FILENAME(doc
), -1, w_file
, G_N_ELEMENTS(w_file
));
438 /* If creating a new file name, convert and append the extension if any */
439 if (! doc
->file_name
&& doc
->file_type
&& doc
->file_type
->extension
&&
440 n
+ 1 < (int)G_N_ELEMENTS(w_file
))
442 w_file
[n
- 1] = L
'.';
443 MultiByteToWideChar(CP_UTF8
, 0, doc
->file_type
->extension
, -1, &w_file
[n
],
444 G_N_ELEMENTS(w_file
) - n
- 1);
447 MultiByteToWideChar(CP_UTF8
, 0, title
, -1, w_title
, G_N_ELEMENTS(w_title
));
449 /* initialise file dialog info struct */
450 memset(&of
, 0, sizeof of
);
451 #ifdef OPENFILENAME_SIZE_VERSION_400
452 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
454 of
.lStructSize
= sizeof of
;
456 of
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent
)));
458 of
.lpstrFilter
= get_file_filter_all_files();
459 of
.lpstrCustomFilter
= NULL
;
462 of
.lpstrFile
= w_file
;
464 of
.lpstrFileTitle
= NULL
;
465 of
.lpstrTitle
= w_title
;
466 of
.lpstrDefExt
= L
"";
467 of
.Flags
= OFN_OVERWRITEPROMPT
| OFN_EXPLORER
;
468 retval
= GetSaveFileNameW(&of
);
472 if (CommDlgExtendedError())
474 gchar
*error
= g_strdup_printf(
475 "File dialog box error (%x)", (gint
) CommDlgExtendedError());
476 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
482 WideCharToMultiByte(CP_UTF8
, 0, w_file
, -1, tmp
, sizeof(tmp
), NULL
, NULL
);
484 return g_strdup(tmp
);
488 /* initial_dir can be NULL to use the current working directory.
489 * Returns: the selected filename */
490 gchar
*win32_show_file_dialog(GtkWindow
*parent
, const gchar
*title
, const gchar
*initial_file
)
495 wchar_t w_file
[MAX_PATH
];
496 wchar_t w_title
[512];
500 if (initial_file
!= NULL
)
501 MultiByteToWideChar(CP_UTF8
, 0, initial_file
, -1, w_file
, G_N_ELEMENTS(w_file
));
503 MultiByteToWideChar(CP_UTF8
, 0, title
, -1, w_title
, G_N_ELEMENTS(w_title
));
505 /* initialise file dialog info struct */
506 memset(&of
, 0, sizeof of
);
507 #ifdef OPENFILENAME_SIZE_VERSION_400
508 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
510 of
.lStructSize
= sizeof of
;
512 of
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(GTK_WIDGET(parent
)));
514 of
.lpstrFile
= w_file
;
516 of
.lpstrFileTitle
= NULL
;
517 of
.lpstrTitle
= w_title
;
518 of
.lpstrDefExt
= L
"";
519 of
.Flags
= OFN_FILEMUSTEXIST
| OFN_EXPLORER
;
520 retval
= GetOpenFileNameW(&of
);
524 if (CommDlgExtendedError())
526 gchar
*error
= g_strdup_printf(
527 "File dialog box error (%x)", (gint
) CommDlgExtendedError());
528 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
534 WideCharToMultiByte(CP_UTF8
, 0, w_file
, -1, tmp
, sizeof(tmp
), NULL
, NULL
);
536 return g_strdup(tmp
);
540 void win32_show_font_dialog(void)
544 LOGFONT lf
; /* logical font structure */
546 memset(&lf
, 0, sizeof lf
);
547 /* TODO: init lf members */
549 memset(&cf
, 0, sizeof cf
);
550 cf
.lStructSize
= sizeof cf
;
551 cf
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets
.window
));
553 /* support CF_APPLY? */
554 cf
.Flags
= CF_NOSCRIPTSEL
| CF_FORCEFONTEXIST
| CF_INITTOLOGFONTSTRUCT
| CF_SCREENFONTS
;
556 retval
= ChooseFont(&cf
);
560 gchar
*editorfont
= g_strdup_printf("%s %d", lf
.lfFaceName
, (cf
.iPointSize
/ 10));
561 ui_set_editor_font(editorfont
);
567 void win32_show_color_dialog(const gchar
*colour
)
570 static COLORREF acr_cust_clr
[16];
571 static DWORD rgb_current
;
572 gchar
*hex
= g_malloc0(12);
573 GeanyDocument
*doc
= document_get_current();
575 /* Initialize CHOOSECOLOR */
576 memset(&cc
, 0, sizeof cc
);
577 cc
.lStructSize
= sizeof(cc
);
578 cc
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets
.window
));
579 cc
.lpCustColors
= (LPDWORD
) acr_cust_clr
;
580 cc
.rgbResult
= (colour
!= NULL
) ? utils_parse_color_to_bgr(colour
) : 0;
581 cc
.Flags
= CC_FULLOPEN
| CC_RGBINIT
;
583 if (ChooseColor(&cc
))
585 rgb_current
= cc
.rgbResult
;
586 g_snprintf(hex
, 11, "#%02X%02X%02X",
587 (guint
) (utils_scale_round(GetRValue(rgb_current
), 255)),
588 (guint
) (utils_scale_round(GetGValue(rgb_current
), 255)),
589 (guint
) (utils_scale_round(GetBValue(rgb_current
), 255)));
591 editor_insert_color(doc
->editor
, hex
);
597 void win32_show_pref_file_dialog(GtkEntry
*item
)
601 wchar_t fname
[MAX_PATH
];
603 gchar
**field
, *filename
;
607 /* cut the options from the command line */
608 field
= g_strsplit(gtk_entry_get_text(GTK_ENTRY(item
)), " ", 2);
611 filename
= g_find_program_in_path(field
[0]);
612 if (filename
!= NULL
&& g_file_test(filename
, G_FILE_TEST_EXISTS
))
614 MultiByteToWideChar(CP_UTF8
, 0, filename
, -1, fname
, G_N_ELEMENTS(fname
));
619 /* initialize file dialog info struct */
620 memset(&of
, 0, sizeof of
);
621 #ifdef OPENFILENAME_SIZE_VERSION_400
622 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
624 of
.lStructSize
= sizeof of
;
626 of
.hwndOwner
= GDK_WINDOW_HWND(gtk_widget_get_window(ui_widgets
.prefs_dialog
));
628 of
.lpstrFilter
= get_filters(FALSE
);
629 of
.lpstrCustomFilter
= NULL
;
632 of
.lpstrFile
= fname
;
634 of
.lpstrFileTitle
= NULL
;
635 /*of.lpstrInitialDir = g_get_home_dir();*/
636 of
.lpstrInitialDir
= NULL
;
637 of
.lpstrTitle
= NULL
;
638 of
.lpstrDefExt
= L
"exe";
639 of
.Flags
= OFN_HIDEREADONLY
| OFN_FILEMUSTEXIST
| OFN_EXPLORER
;
640 retval
= GetOpenFileNameW(&of
);
644 if (CommDlgExtendedError())
647 g_snprintf(error
, sizeof error
, "File dialog box error (%x)", (int)CommDlgExtendedError());
648 win32_message_dialog(NULL
, GTK_MESSAGE_ERROR
, error
);
654 len
= WideCharToMultiByte(CP_UTF8
, 0, fname
, -1, tmp
, sizeof(tmp
), NULL
, NULL
);
655 if ((of
.nFileOffset
- 1) != len
)
657 if (g_strv_length(field
) > 1)
658 /* add the command line args of the old command */
659 /** TODO this fails badly when the old command contained spaces, we need quoting here */
660 filename
= g_strconcat(tmp
, " ", field
[1], NULL
);
663 filename
= g_strdup(tmp
);
665 gtk_entry_set_text(GTK_ENTRY(item
), filename
);
672 /* Creates a native Windows message box of the given type and returns always TRUE
673 * or FALSE representing th pressed Yes or No button.
674 * If type is not GTK_MESSAGE_QUESTION, it returns always TRUE. */
675 gboolean
win32_message_dialog(GtkWidget
*parent
, GtkMessageType type
, const gchar
*msg
)
681 HWND parent_hwnd
= NULL
;
682 static wchar_t w_msg
[512];
683 static wchar_t w_title
[512];
687 case GTK_MESSAGE_ERROR
:
689 t
= MB_OK
| MB_ICONERROR
;
693 case GTK_MESSAGE_QUESTION
:
695 t
= MB_YESNO
| MB_ICONQUESTION
;
696 title
= _("Question");
699 case GTK_MESSAGE_WARNING
:
701 t
= MB_OK
| MB_ICONWARNING
;
702 title
= _("Warning");
707 t
= MB_OK
| MB_ICONINFORMATION
;
708 title
= _("Information");
713 /* convert the Unicode chars to wide chars */
714 /** TODO test if LANG == C then possibly skip conversion => g_win32_getlocale() */
715 MultiByteToWideChar(CP_UTF8
, 0, msg
, -1, w_msg
, G_N_ELEMENTS(w_msg
));
716 MultiByteToWideChar(CP_UTF8
, 0, title
, -1, w_title
, G_N_ELEMENTS(w_title
));
719 parent_hwnd
= GDK_WINDOW_HWND(gtk_widget_get_window(parent
));
720 else if (main_widgets
.window
!= NULL
)
721 parent_hwnd
= GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets
.window
));
723 /* display the message box */
724 rc
= MessageBoxW(parent_hwnd
, w_msg
, w_title
, t
);
726 if (type
== GTK_MESSAGE_QUESTION
&& rc
!= IDYES
)
733 /* Little wrapper for _waccess(), returns errno or 0 if there was no error */
734 gint
win32_check_write_permission(const gchar
*dir
)
736 static wchar_t w_dir
[MAX_PATH
];
737 MultiByteToWideChar(CP_UTF8
, 0, dir
, -1, w_dir
, G_N_ELEMENTS(w_dir
));
738 if (_waccess(w_dir
, R_OK
| W_OK
) != 0)
745 /* Just a simple wrapper function to open a browser window */
746 void win32_open_browser(const gchar
*uri
)
748 if (strncmp(uri
, "file://", 7) == 0)
751 if (strchr(uri
, ':') != NULL
)
757 ShellExecute(NULL
, "open", uri
, NULL
, NULL
, SW_SHOWNORMAL
);
761 /* Returns TRUE if the command, which child_pid refers to, returned with a successful exit code,
762 * otherwise FALSE. */
763 gboolean
win32_get_exit_status(GPid child_pid
)
766 GetExitCodeProcess(child_pid
, &exit_code
);
768 return (exit_code
== 0);
772 static FILE *open_std_handle(DWORD handle
, const char *mode
)
778 lStdHandle
= GetStdHandle(handle
);
779 if (lStdHandle
== INVALID_HANDLE_VALUE
)
781 gchar
*err
= g_win32_error_message(GetLastError());
782 g_warning("GetStdHandle(%ld) failed: %s", (long)handle
, err
);
786 hConHandle
= _open_osfhandle((long)lStdHandle
, _O_TEXT
);
787 if (hConHandle
== -1)
789 gchar
*err
= g_win32_error_message(GetLastError());
790 g_warning("_open_osfhandle(%ld, _O_TEXT) failed: %s", (long)lStdHandle
, err
);
794 fp
= _fdopen(hConHandle
, mode
);
797 gchar
*err
= g_win32_error_message(GetLastError());
798 g_warning("_fdopen(%d, \"%s\") failed: %s", hConHandle
, mode
, err
);
802 if (setvbuf(fp
, NULL
, _IONBF
, 0) != 0)
804 gchar
*err
= g_win32_error_message(GetLastError());
805 g_warning("setvbuf(%p, NULL, _IONBF, 0) failed: %s", fp
, err
);
815 static void debug_setup_console(void)
817 static const WORD MAX_CONSOLE_LINES
= 500;
818 CONSOLE_SCREEN_BUFFER_INFO coninfo
;
821 /* allocate a console for this app */
824 /* set the screen buffer to be big enough to let us scroll text */
825 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &coninfo
);
826 coninfo
.dwSize
.Y
= MAX_CONSOLE_LINES
;
827 SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE
), coninfo
.dwSize
);
829 /* redirect unbuffered STDOUT to the console */
830 fp
= open_std_handle(STD_OUTPUT_HANDLE
, "w");
834 /* redirect unbuffered STDERR to the console */
835 fp
= open_std_handle(STD_ERROR_HANDLE
, "w");
839 /* redirect unbuffered STDIN to the console */
840 fp
= open_std_handle(STD_INPUT_HANDLE
, "r");
846 void win32_init_debug_code(void)
850 /* create a console window to get log messages on Windows,
851 * especially useful when generating tags files */
852 debug_setup_console();
853 /* Enable GLib process spawn debug mode when Geany was started with the debug flag */
854 g_setenv("G_SPAWN_WIN32_DEBUG", "1", FALSE
);
859 static gchar
*create_temp_file(void)
864 fd
= g_file_open_tmp("tmp_XXXXXX", &name
, NULL
);
874 /* Sometimes this blocks for 30s before aborting when there are several
875 * pages of (error) output and sometimes hangs - see the FIXME.
876 * Also gw_spawn.dwExitCode seems to be not set properly. */
877 /* Process spawning implementation for Windows, by Pierre Joye.
878 * Don't call this function directly, use utils_spawn_[a]sync() instead. */
880 gboolean
_broken_win32_spawn(const gchar
*dir
, gchar
**argv
, gchar
**env
, GSpawnFlags flags
,
881 gchar
**std_out
, gchar
**std_err
, gint
*exit_status
, GError
**error
)
883 TCHAR buffer
[CMDSIZE
]=TEXT("");
884 TCHAR cmdline
[CMDSIZE
] = TEXT("");
885 TCHAR
* lpPart
[CMDSIZE
]={NULL
};
890 SECURITY_ATTRIBUTES saAttr
;
892 geany_win32_spawn gw_spawn
;
895 HANDLE hStdoutTempFile
= NULL
;
896 HANDLE hStderrTempFile
= NULL
;
898 gchar
*stdout_content
= NULL
;
899 gchar
*stderr_content
= NULL
;
905 g_return_val_if_fail (std_out
== NULL
||
906 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
907 g_return_val_if_fail (std_err
== NULL
||
908 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
910 if (flags
& G_SPAWN_SEARCH_PATH
)
912 retval
= SearchPath(NULL
, argv
[0], ".exe", sizeof(buffer
), buffer
, lpPart
);
914 g_snprintf(cmdline
, sizeof(cmdline
), "\"%s\"", buffer
);
916 g_strlcpy(cmdline
, argv
[0], sizeof(cmdline
));
920 for (i
= cmdpos
; i
< argc
; i
++)
922 g_snprintf(cmdline
, sizeof(cmdline
), "%s %s", cmdline
, argv
[i
]);
923 /*MessageBox(NULL, cmdline, cmdline, MB_OK);*/
928 hStderrTempFile
= GetTempFileHandle(error
);
929 if (hStderrTempFile
== INVALID_HANDLE_VALUE
)
931 gchar
*msg
= g_win32_error_message(GetLastError());
932 geany_debug("win32_spawn: Second CreateFile failed (%d)", (gint
) GetLastError());
933 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR
, "%s", msg
);
941 hStdoutTempFile
= GetTempFileHandle(error
);
942 if (hStdoutTempFile
== INVALID_HANDLE_VALUE
)
944 gchar
*msg
= g_win32_error_message(GetLastError());
945 geany_debug("win32_spawn: Second CreateFile failed (%d)", (gint
) GetLastError());
946 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR
, "%s", msg
);
952 /* Set the bInheritHandle flag so pipe handles are inherited. */
953 saAttr
.nLength
= sizeof(SECURITY_ATTRIBUTES
);
954 saAttr
.bInheritHandle
= TRUE
;
955 saAttr
.lpSecurityDescriptor
= NULL
;
957 /* Get the handle to the current STDOUT and STDERR. */
958 gw_spawn
.hStdout
= GetStdHandle(STD_OUTPUT_HANDLE
);
959 gw_spawn
.hStderr
= GetStdHandle(STD_ERROR_HANDLE
);
960 gw_spawn
.dwExitCode
= 0;
962 /* Create a pipe for the child process's STDOUT. */
963 if (! CreatePipe(&(gw_spawn
.hChildStdoutRd
), &(gw_spawn
.hChildStdoutWr
), &saAttr
, 0))
965 gchar
*msg
= g_win32_error_message(GetLastError());
966 geany_debug("win32_spawn: Stdout pipe creation failed (%d)", (gint
) GetLastError());
967 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR_PIPE
, "%s", msg
);
972 /* Ensure that the read handle to the child process's pipe for STDOUT is not inherited.*/
973 SetHandleInformation(gw_spawn
.hChildStdoutRd
, HANDLE_FLAG_INHERIT
, 0);
975 /* Create a pipe for the child process's STDERR. */
976 if (! CreatePipe(&(gw_spawn
.hChildStderrRd
), &(gw_spawn
.hChildStderrWr
), &saAttr
, 0))
978 gchar
*msg
= g_win32_error_message(GetLastError());
979 geany_debug("win32_spawn: Stderr pipe creation failed");
980 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR_PIPE
, "%s", msg
);
985 /* Ensure that the read handle to the child process's pipe for STDOUT is not inherited.*/
986 SetHandleInformation(gw_spawn
.hChildStderrRd
, HANDLE_FLAG_INHERIT
, 0);
988 /* Create a pipe for the child process's STDIN. */
989 if (! CreatePipe(&(gw_spawn
.hChildStdinRd
), &(gw_spawn
.hChildStdinWr
), &saAttr
, 0))
991 gchar
*msg
= g_win32_error_message(GetLastError());
992 geany_debug("win32_spawn: Stdin pipe creation failed");
993 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR_PIPE
, "%s", msg
);
998 /* Ensure that the write handle to the child process's pipe for STDIN is not inherited. */
999 SetHandleInformation(gw_spawn
.hChildStdinWr
, HANDLE_FLAG_INHERIT
, 0);
1001 /* Now create the child process. */
1002 fSuccess
= CreateChildProcess(&gw_spawn
, cmdline
, dir
, error
);
1005 *exit_status
= gw_spawn
.dwExitCode
;
1010 geany_debug("win32_spawn: Create process failed");
1014 /* Read from pipe that is the standard output for child process. */
1015 if (std_out
!= NULL
)
1017 ReadFromPipe(gw_spawn
.hChildStdoutRd
, gw_spawn
.hChildStdoutWr
, hStdoutTempFile
, error
);
1018 if (! GetContentFromHandle(hStdoutTempFile
, &stdout_content
, error
))
1022 *std_out
= stdout_content
;
1025 if (std_err
!= NULL
)
1027 ReadFromPipe(gw_spawn
.hChildStderrRd
, gw_spawn
.hChildStderrWr
, hStderrTempFile
, error
);
1028 if (! GetContentFromHandle(hStderrTempFile
, &stderr_content
, error
))
1032 *std_err
= stderr_content
;
1038 /* Note: g_spawn is broken for receiving both stdio and stderr e.g. when
1039 * running make and there are compile errors. See glib/giowin32.c header
1040 * comment about Windows bugs, e.g. #338943 */
1041 /* Simple replacement for _broken_win32_spawn().
1042 * flags is ignored, G_SPAWN_SEARCH_PATH is implied.
1043 * Don't call this function directly, use utils_spawn_[a]sync() instead.
1044 * Adapted from tm_workspace_create_global_tags(). */
1045 gboolean
win32_spawn(const gchar
*dir
, gchar
**argv
, gchar
**env
, GSpawnFlags flags
,
1046 gchar
**std_out
, gchar
**std_err
, gint
*exit_status
, GError
**error
)
1050 gchar
*tmp_file
= create_temp_file();
1051 gchar
*tmp_errfile
= create_temp_file();
1053 gchar
*locale_command
;
1057 return _broken_win32_spawn(dir
, argv
, env
, flags
, std_out
, std_err
,
1058 exit_status
, error
);
1060 if (!tmp_file
|| !tmp_errfile
)
1062 g_warning("%s: Could not create temporary files!", G_STRFUNC
);
1065 command
= g_strjoinv(" ", argv
);
1066 SETPTR(command
, g_strdup_printf("cmd.exe /S /C \"%s >%s 2>%s\"",
1067 command
, tmp_file
, tmp_errfile
));
1068 locale_command
= g_locale_from_utf8(command
, -1, NULL
, NULL
, NULL
);
1069 if (! locale_command
)
1070 locale_command
= g_strdup(command
);
1071 geany_debug("WIN32: actually running command:\n%s", command
);
1074 ret
= system(locale_command
);
1075 /* the command can return -1 as an exit code, so check errno also */
1076 fail
= ret
== -1 && errno
;
1079 if (std_out
!= NULL
)
1080 g_file_get_contents(tmp_file
, std_out
, NULL
, NULL
);
1081 if (std_err
!= NULL
)
1082 g_file_get_contents(tmp_errfile
, std_err
, NULL
, NULL
);
1085 g_set_error_literal(error
, G_SPAWN_ERROR
, errno
, g_strerror(errno
));
1088 g_free(locale_command
);
1091 g_unlink(tmp_errfile
);
1092 g_free(tmp_errfile
);
1100 static gboolean
GetContentFromHandle(HANDLE hFile
, gchar
**content
, GError
**error
)
1106 filesize
= GetFileSize(hFile
, NULL
);
1113 buffer
= g_malloc(filesize
+ 1);
1116 gchar
*msg
= g_win32_error_message(GetLastError());
1117 geany_debug("GetContentFromHandle: Alloc failed");
1118 g_set_error(error
, G_SPAWN_ERROR
, G_SPAWN_ERROR
, "%s", msg
);
1123 SetFilePointer(hFile
, 0, NULL
, FILE_BEGIN
);
1124 if (! ReadFile(hFile
, buffer
, filesize
, &dwRead
, NULL
) || dwRead
== 0)
1126 gchar
*msg
= g_win32_error_message(GetLastError());
1127 geany_debug("GetContentFromHandle: Cannot read tempfile");
1128 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR_FAILED
, "%s", msg
);
1133 if (! CloseHandle(hFile
))
1135 gchar
*msg
= g_win32_error_message(GetLastError());
1136 geany_debug("GetContentFromHandle: CloseHandle failed (%d)", (gint
) GetLastError());
1137 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR_FAILED
, "%s", msg
);
1143 buffer
[filesize
] = '\0';
1149 gchar
*win32_expand_environment_variables(const gchar
*str
)
1151 gchar expCmdline
[32768]; /* 32768 is the limit for ExpandEnvironmentStrings() */
1153 if (ExpandEnvironmentStrings((LPCTSTR
) str
, (LPTSTR
) expCmdline
, sizeof(expCmdline
)) != 0)
1154 return g_strdup(expCmdline
);
1156 return g_strdup(str
);
1160 static gboolean
CreateChildProcess(geany_win32_spawn
*gw_spawn
, TCHAR
*szCmdline
,
1161 const TCHAR
*dir
, GError
**error
)
1163 PROCESS_INFORMATION piProcInfo
;
1164 STARTUPINFOW siStartInfo
;
1165 BOOL bFuncRetn
= FALSE
;
1166 gchar
*expandedCmdline
;
1167 wchar_t w_commandline
[CMDSIZE
];
1168 wchar_t w_dir
[MAX_PATH
];
1170 /* Set up members of the PROCESS_INFORMATION structure. */
1171 ZeroMemory(&piProcInfo
, sizeof(PROCESS_INFORMATION
));
1173 /* Set up members of the STARTUPINFO structure.*/
1174 ZeroMemory(&siStartInfo
, sizeof(STARTUPINFO
));
1176 siStartInfo
.cb
= sizeof(STARTUPINFO
);
1177 siStartInfo
.hStdError
= gw_spawn
->hChildStderrWr
;
1178 siStartInfo
.hStdOutput
= gw_spawn
->hChildStdoutWr
;
1179 siStartInfo
.hStdInput
= gw_spawn
->hChildStdinRd
;
1180 siStartInfo
.dwFlags
|= STARTF_USESTDHANDLES
;
1182 /* Expand environment variables like %blah%. */
1183 expandedCmdline
= win32_expand_environment_variables(szCmdline
);
1185 MultiByteToWideChar(CP_UTF8
, 0, expandedCmdline
, -1, w_commandline
, G_N_ELEMENTS(w_commandline
));
1186 MultiByteToWideChar(CP_UTF8
, 0, dir
, -1, w_dir
, G_N_ELEMENTS(w_dir
));
1188 /* Create the child process. */
1189 bFuncRetn
= CreateProcessW(NULL
,
1190 w_commandline
, /* command line */
1191 NULL
, /* process security attributes */
1192 NULL
, /* primary thread security attributes */
1193 TRUE
, /* handles are inherited */
1194 CREATE_NO_WINDOW
, /* creation flags */
1195 NULL
, /* use parent's environment */
1196 w_dir
, /* use parent's current directory */
1197 &siStartInfo
, /* STARTUPINFO pointer */
1198 &piProcInfo
); /* receives PROCESS_INFORMATION */
1200 g_free(expandedCmdline
);
1204 gchar
*msg
= g_win32_error_message(GetLastError());
1205 geany_debug("CreateChildProcess: CreateProcess failed (%s)", msg
);
1206 g_set_error(error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
, "%s", msg
);
1215 /* FIXME: this seems to timeout when there are many lines
1216 * to read - maybe because the child's pipe is full */
1217 for (i
= 0; i
< 2 &&
1218 WaitForSingleObject(piProcInfo
.hProcess
, ms
) == WAIT_TIMEOUT
; i
++)
1220 ui_set_statusbar(FALSE
, _("Process timed out after %.02f s!"), ms
/ 1000.0F
);
1221 geany_debug("CreateChildProcess: timed out");
1222 TerminateProcess(piProcInfo
.hProcess
, WAIT_TIMEOUT
); /* NOTE: This will not kill grandkids. */
1225 if (!GetExitCodeProcess(piProcInfo
.hProcess
, &gw_spawn
->dwExitCode
))
1227 gchar
*msg
= g_win32_error_message(GetLastError());
1228 geany_debug("GetExitCodeProcess failed: %s", msg
);
1229 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR_FAILED
, "%s", msg
);
1232 CloseHandle(piProcInfo
.hProcess
);
1233 CloseHandle(piProcInfo
.hThread
);
1240 static VOID
ReadFromPipe(HANDLE hRead
, HANDLE hWrite
, HANDLE hFile
, GError
**error
)
1242 DWORD dwRead
, dwWritten
;
1243 CHAR chBuf
[BUFSIZE
];
1245 /* Close the write end of the pipe before reading from the
1246 read end of the pipe. */
1247 if (! CloseHandle(hWrite
))
1249 gchar
*msg
= g_win32_error_message(GetLastError());
1250 geany_debug("ReadFromPipe: Closing handle failed");
1251 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR_PIPE
, "%s", msg
);
1256 /* Read output from the child process, and write to parent's STDOUT. */
1259 if (! ReadFile(hRead
, chBuf
, BUFSIZE
, &dwRead
, NULL
) || dwRead
== 0)
1262 if (! WriteFile(hFile
, chBuf
, dwRead
, &dwWritten
, NULL
))
1268 static HANDLE
GetTempFileHandle(GError
**error
)
1271 DWORD dwBufSize
= BUFSIZE
;
1273 TCHAR szTempName
[BUFSIZE
];
1274 TCHAR lpPathBuffer
[BUFSIZE
];
1278 /* Get the temp path. */
1279 dwRetVal
= GetTempPath(dwBufSize
, /* length of the buffer*/
1280 lpPathBuffer
); /* buffer for path */
1282 if (dwRetVal
> dwBufSize
|| (dwRetVal
== 0))
1284 gchar
*msg
= g_win32_error_message(GetLastError());
1285 geany_debug("GetTempFileHandle: GetTempPath failed (%d)", (gint
) GetLastError());
1286 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR
, "%s", msg
);
1291 /* Create a temporary file for STDOUT. */
1292 uRetVal
= GetTempFileName(lpPathBuffer
, /* directory for tmp files */
1293 TEXT("GEANY_VCDIFF_"), /* temp file name prefix */
1294 0, /* create unique name */
1295 szTempName
); /* buffer for name */
1298 gchar
*msg
= g_win32_error_message(GetLastError());
1299 geany_debug("GetTempFileName failed (%d)", (gint
) GetLastError());
1300 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR
, "%s", msg
);
1305 hTempFile
= CreateFile((LPTSTR
) szTempName
, /* file name */
1306 GENERIC_READ
| GENERIC_WRITE
, /* open r-w */
1307 0, /* do not share */
1308 NULL
, /* default security */
1309 CREATE_ALWAYS
, /* overwrite existing */
1310 FILE_ATTRIBUTE_NORMAL
,/* normal file */
1311 NULL
); /* no template */
1313 if (hTempFile
== INVALID_HANDLE_VALUE
)
1315 gchar
*msg
= g_win32_error_message(GetLastError());
1316 geany_debug("GetTempFileHandle: Second CreateFile failed (%d)", (gint
) GetLastError());
1317 g_set_error(error
, G_SPAWN_ERROR
, G_FILE_ERROR
, "%s", msg
);
1325 /* From GDK (they got it from MS Knowledge Base article Q130698) */
1326 static gboolean
resolve_link(HWND hWnd
, wchar_t *link
, gchar
**lpszPath
)
1328 WIN32_FILE_ATTRIBUTE_DATA wfad
;
1330 IShellLinkW
*pslW
= NULL
;
1331 IPersistFile
*ppf
= NULL
;
1332 LPVOID pslWV
= NULL
;
1335 /* Check if the file is empty first because IShellLink::Resolve for some reason succeeds
1336 * with an empty file and returns an empty "link target". (#524151) */
1337 if (!GetFileAttributesExW(link
, GetFileExInfoStandard
, &wfad
) ||
1338 (wfad
.nFileSizeHigh
== 0 && wfad
.nFileSizeLow
== 0))
1343 /* Assume failure to start with: */
1348 hres
= CoCreateInstance(
1349 &CLSID_ShellLink
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IShellLinkW
, &pslWV
);
1351 if (SUCCEEDED(hres
))
1353 /* The IShellLink interface supports the IPersistFile interface.
1354 * Get an interface pointer to it. */
1355 pslW
= (IShellLinkW
*) pslWV
;
1356 hres
= pslW
->lpVtbl
->QueryInterface(pslW
, &IID_IPersistFile
, &ppfV
);
1359 if (SUCCEEDED(hres
))
1361 /* Load the file. */
1362 ppf
= (IPersistFile
*) ppfV
;
1363 hres
= ppf
->lpVtbl
->Load(ppf
, link
, STGM_READ
);
1366 if (SUCCEEDED(hres
))
1368 /* Resolve the link by calling the Resolve() interface function. */
1369 hres
= pslW
->lpVtbl
->Resolve(pslW
, hWnd
, SLR_ANY_MATCH
| SLR_NO_UI
);
1372 if (SUCCEEDED(hres
))
1374 wchar_t wtarget
[MAX_PATH
];
1376 hres
= pslW
->lpVtbl
->GetPath(pslW
, wtarget
, MAX_PATH
, NULL
, 0);
1377 if (SUCCEEDED(hres
))
1378 *lpszPath
= g_utf16_to_utf8(wtarget
, -1, NULL
, NULL
, NULL
);
1382 ppf
->lpVtbl
->Release(ppf
);
1385 pslW
->lpVtbl
->Release(pslW
);
1387 return SUCCEEDED(hres
);
1391 /* Checks whether file_name is a Windows shortcut. file_name is expected in UTF-8 encoding.
1392 * If file_name is a Windows shortcut, it returns the target in UTF-8 encoding.
1393 * If it is not a shortcut, it returns a newly allocated copy of file_name. */
1394 gchar
*win32_get_shortcut_target(const gchar
*file_name
)
1397 wchar_t *wfilename
= g_utf8_to_utf16(file_name
, -1, NULL
, NULL
, NULL
);
1399 resolve_link(GDK_WINDOW_HWND(gtk_widget_get_window(main_widgets
.window
)), wfilename
, &path
);
1403 return g_strdup(file_name
);
1409 void win32_set_working_directory(const gchar
*dir
)
1411 SetCurrentDirectory(dir
);
1415 gchar
*win32_get_installation_dir(void)
1417 return g_win32_get_package_installation_directory_of_module(NULL
);