2005-11-22 12:26:26 +00:00
|
|
|
/*
|
|
|
|
* win32.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2009-01-04 18:30:42 +00:00
|
|
|
* Copyright 2005-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2006-2009 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2005-11-22 12:26:26 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-07-24 23:56:50 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-11-22 12:26:26 +00:00
|
|
|
*
|
2005-12-29 19:50:50 +00:00
|
|
|
* $Id$
|
2005-11-22 12:26:26 +00:00
|
|
|
*/
|
|
|
|
|
2007-02-24 11:41:56 +00:00
|
|
|
/*
|
|
|
|
* Special functions for the win32 platform, to provide native dialogs.
|
|
|
|
*/
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2005-12-29 19:50:50 +00:00
|
|
|
#include "geany.h"
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-26 17:02:16 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2007-07-17 12:04:46 +00:00
|
|
|
#define VC_EXTRALEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2006-07-25 08:27:12 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <commdlg.h>
|
2007-02-19 18:58:32 +00:00
|
|
|
#include <shlobj.h>
|
2007-11-04 15:48:14 +00:00
|
|
|
#include <io.h>
|
2007-12-30 15:54:00 +00:00
|
|
|
#include <fcntl.h>
|
2006-07-25 08:27:12 +00:00
|
|
|
|
2006-01-03 12:33:27 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-02-19 18:58:32 +00:00
|
|
|
#include <gdk/gdkwin32.h>
|
|
|
|
|
2005-12-29 19:50:50 +00:00
|
|
|
#include "win32.h"
|
|
|
|
|
|
|
|
#include "document.h"
|
|
|
|
#include "support.h"
|
2006-01-03 12:33:27 +00:00
|
|
|
#include "utils.h"
|
2006-09-11 07:41:37 +00:00
|
|
|
#include "ui_utils.h"
|
2006-01-03 12:33:27 +00:00
|
|
|
#include "sciwrappers.h"
|
|
|
|
#include "dialogs.h"
|
2007-09-03 18:19:27 +00:00
|
|
|
#include "filetypes.h"
|
2008-04-23 16:47:42 +00:00
|
|
|
#include "project.h"
|
2008-05-15 15:58:28 +00:00
|
|
|
#include "editor.h"
|
2005-12-29 19:50:50 +00:00
|
|
|
|
2008-02-24 10:27:32 +00:00
|
|
|
#define BUFSIZE 4096
|
|
|
|
|
|
|
|
struct _geany_win32_spawn
|
|
|
|
{
|
|
|
|
HANDLE hChildStdinRd;
|
|
|
|
HANDLE hChildStdinWr;
|
|
|
|
HANDLE hChildStdoutRd;
|
|
|
|
HANDLE hChildStdoutWr;
|
|
|
|
HANDLE hChildStderrRd;
|
|
|
|
HANDLE hChildStderrWr;
|
|
|
|
HANDLE hInputFile;
|
|
|
|
HANDLE hStdout;
|
|
|
|
HANDLE hStderr;
|
2008-02-27 16:08:07 +00:00
|
|
|
HANDLE processId;
|
|
|
|
DWORD dwExitCode;
|
2008-02-24 10:27:32 +00:00
|
|
|
};
|
|
|
|
typedef struct _geany_win32_spawn geany_win32_spawn;
|
|
|
|
|
2008-02-27 16:08:07 +00:00
|
|
|
static gboolean GetContentFromHandle(HANDLE hFile, gchar **content, GError **error);
|
|
|
|
static HANDLE GetTempFileHandle(GError **error);
|
|
|
|
static gboolean CreateChildProcess(geany_win32_spawn *gw_spawn, TCHAR *szCmdline, const TCHAR *dir, GError **error);
|
|
|
|
static VOID ReadFromPipe(HANDLE hRead, HANDLE hWrite, HANDLE hFile, GError **error);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2007-02-19 18:58:32 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t *get_file_filters(void)
|
2007-02-19 18:58:32 +00:00
|
|
|
{
|
|
|
|
gchar *string;
|
2009-07-20 23:09:29 +00:00
|
|
|
guint i, j, len;
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t title[1024];
|
2007-02-19 18:58:32 +00:00
|
|
|
GString *str = g_string_sized_new(100);
|
2007-03-02 09:53:21 +00:00
|
|
|
GString *all_patterns = g_string_sized_new(100);
|
2007-02-19 18:58:32 +00:00
|
|
|
gchar *tmp;
|
|
|
|
|
2008-05-07 11:34:38 +00:00
|
|
|
for (i = 0; i < filetypes_array->len; i++)
|
2007-03-02 09:53:21 +00:00
|
|
|
{
|
|
|
|
tmp = g_strjoinv(";", filetypes[i]->pattern);
|
|
|
|
g_string_append_printf(str, "%s\t%s\t", filetypes[i]->title, tmp);
|
|
|
|
g_free(tmp);
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* create meta file filter "All Source" */
|
2008-05-07 11:34:38 +00:00
|
|
|
for (i = 0; i < filetypes_array->len; i++)
|
2007-02-19 18:58:32 +00:00
|
|
|
{
|
2007-03-02 09:53:21 +00:00
|
|
|
for (j = 0; filetypes[i]->pattern[j] != NULL; j++)
|
2007-02-19 18:58:32 +00:00
|
|
|
{
|
2007-03-02 09:53:21 +00:00
|
|
|
g_string_append(all_patterns, filetypes[i]->pattern[j]);
|
|
|
|
g_string_append_c(all_patterns, ';');
|
2007-02-19 18:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-02 09:53:21 +00:00
|
|
|
g_string_append_printf(str, "%s\t%s\t", _("All Source"), all_patterns->str);
|
|
|
|
g_string_free(all_patterns, TRUE);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
g_string_append_c(str, '\t'); /* the final \0 byte to mark the end of the string */
|
2007-02-19 18:58:32 +00:00
|
|
|
string = str->str;
|
|
|
|
g_string_free(str, FALSE);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* replace all "\t"s by \0 */
|
2007-02-19 18:58:32 +00:00
|
|
|
len = strlen(string);
|
|
|
|
for(i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (string[i] == '\t') string[i] = '\0';
|
|
|
|
}
|
2009-05-26 19:27:23 +00:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, string, len, title, sizeof(title));
|
|
|
|
g_free(string);
|
|
|
|
|
|
|
|
return title;
|
2007-02-19 18:58:32 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t *get_filters(gboolean project_files)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-02-19 18:58:32 +00:00
|
|
|
gchar *string;
|
2005-11-22 12:26:26 +00:00
|
|
|
gint i, len;
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t title[1024];
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2007-12-07 16:31:38 +00:00
|
|
|
if (project_files)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-12-07 16:31:38 +00:00
|
|
|
string = g_strconcat(_("Geany project files"), "\t", "*." GEANY_PROJECT_EXT, "\t",
|
2008-05-07 13:54:21 +00:00
|
|
|
filetypes[GEANY_FILETYPES_NONE]->title, "\t",
|
|
|
|
filetypes[GEANY_FILETYPES_NONE]->pattern[0], "\t", NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-12-07 16:31:38 +00:00
|
|
|
string = g_strconcat(_("Executables"), "\t", "*.exe;*.bat;*.cmd", "\t",
|
2008-05-07 13:54:21 +00:00
|
|
|
filetypes[GEANY_FILETYPES_NONE]->title, "\t",
|
|
|
|
filetypes[GEANY_FILETYPES_NONE]->pattern[0], "\t", NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* replace all "\t"s by \0 */
|
2005-11-22 12:26:26 +00:00
|
|
|
len = strlen(string);
|
|
|
|
for(i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if (string[i] == '\t') string[i] = '\0';
|
|
|
|
}
|
2009-05-26 19:27:23 +00:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, string, len, title, sizeof(title));
|
|
|
|
g_free(string);
|
|
|
|
|
|
|
|
return title;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
/* Converts the given UTF-8 filename or directory name into something usable for Windows and
|
2008-03-17 14:56:08 +00:00
|
|
|
* returns the directory part of the given filename. */
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t *get_dir_for_path(const gchar *utf8_filename)
|
2007-02-19 18:58:32 +00:00
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t w_dir[MAX_PATH];
|
2008-03-17 14:56:08 +00:00
|
|
|
gchar *result;
|
2009-05-27 18:00:05 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
if (g_file_test(utf8_filename, G_FILE_TEST_IS_DIR))
|
|
|
|
result = (gchar*) utf8_filename;
|
2007-02-19 18:58:32 +00:00
|
|
|
else
|
2009-05-27 18:00:05 +00:00
|
|
|
result = g_path_get_dirname(utf8_filename);
|
2008-03-21 14:02:59 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, result, -1, w_dir, sizeof(w_dir));
|
|
|
|
|
|
|
|
if (result != utf8_filename)
|
|
|
|
g_free(result);
|
2009-05-27 18:00:05 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
return w_dir;
|
2007-02-19 18:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Callback function for setting the initial directory of the folder open dialog. This could also
|
|
|
|
* be done with BROWSEINFO.pidlRoot and SHParseDisplayName but SHParseDisplayName is not available
|
|
|
|
* on systems below Windows XP. So, we go the hard way by creating a callback which will set up the
|
|
|
|
* folder when the dialog is initialised. Yeah, I like Windows. */
|
|
|
|
INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
|
|
|
|
{
|
|
|
|
switch(uMsg)
|
|
|
|
{
|
|
|
|
case BFFM_INITIALIZED:
|
2009-05-26 19:27:23 +00:00
|
|
|
{
|
|
|
|
SendMessageW(hwnd, BFFM_SETSELECTIONW, TRUE, pData);
|
2007-02-19 18:58:32 +00:00
|
|
|
break;
|
2009-05-26 19:27:23 +00:00
|
|
|
}
|
2007-02-19 18:58:32 +00:00
|
|
|
case BFFM_SELCHANGED:
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* set the status window to the currently selected path. */
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t szDir[MAX_PATH];
|
|
|
|
if (SHGetPathFromIDListW((LPITEMIDLIST) lp, szDir))
|
2007-02-19 18:58:32 +00:00
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
SendMessageW(hwnd, BFFM_SETSTATUSTEXTW, 0, (LPARAM) szDir);
|
2007-02-19 18:58:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Shows a folder selection dialog.
|
2008-03-17 14:56:08 +00:00
|
|
|
* initial_dir is expected in UTF-8
|
2007-02-19 18:58:32 +00:00
|
|
|
* The selected folder name is returned. */
|
2007-12-07 16:31:38 +00:00
|
|
|
gchar *win32_show_project_folder_dialog(GtkWidget *parent, const gchar *title,
|
|
|
|
const gchar *initial_dir)
|
2007-02-19 18:58:32 +00:00
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
BROWSEINFOW bi;
|
2007-02-19 18:58:32 +00:00
|
|
|
LPCITEMIDLIST pidl;
|
2008-03-17 14:56:08 +00:00
|
|
|
gchar *result = NULL;
|
2009-05-26 19:27:23 +00:00
|
|
|
wchar_t fname[MAX_PATH];
|
|
|
|
wchar_t w_title[512];
|
|
|
|
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, sizeof(w_title));
|
2007-02-19 18:58:32 +00:00
|
|
|
|
2007-12-07 16:31:38 +00:00
|
|
|
if (parent == NULL)
|
2008-05-22 14:41:28 +00:00
|
|
|
parent = main_widgets.window;
|
2007-12-07 16:31:38 +00:00
|
|
|
|
2007-02-19 18:58:32 +00:00
|
|
|
memset(&bi, 0, sizeof bi);
|
2007-12-07 16:31:38 +00:00
|
|
|
bi.hwndOwner = GDK_WINDOW_HWND(parent->window);
|
2007-02-19 18:58:32 +00:00
|
|
|
bi.pidlRoot = NULL;
|
2009-05-26 19:27:23 +00:00
|
|
|
bi.lpszTitle = w_title;
|
2007-02-19 18:58:32 +00:00
|
|
|
bi.lpfn = BrowseCallbackProc;
|
2009-05-26 19:27:23 +00:00
|
|
|
bi.lParam = (LPARAM) get_dir_for_path(initial_dir);
|
2007-02-19 18:58:32 +00:00
|
|
|
bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
|
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
pidl = SHBrowseForFolderW(&bi);
|
2007-02-19 18:58:32 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* convert the strange Windows folder list item something into an usual path string ;-) */
|
2008-03-17 14:56:08 +00:00
|
|
|
if (pidl != 0)
|
2008-02-08 11:19:45 +00:00
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
if (SHGetPathFromIDListW(pidl, fname))
|
2008-03-17 14:56:08 +00:00
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
result = g_malloc0(MAX_PATH * 2);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, fname, -1, result, MAX_PATH * 2, NULL, NULL);
|
2008-03-17 14:56:08 +00:00
|
|
|
}
|
|
|
|
/* SHBrowseForFolder() probably leaks memory here, but how to free the allocated memory? */
|
2008-02-08 11:19:45 +00:00
|
|
|
}
|
2008-03-17 14:56:08 +00:00
|
|
|
return result;
|
2007-02-19 18:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Shows a file open dialog.
|
|
|
|
* If allow_new_file is set, the file to be opened doesn't have to exist.
|
2008-03-17 14:56:08 +00:00
|
|
|
* initial_dir is expected in UTF-8
|
2007-12-07 16:31:38 +00:00
|
|
|
* The selected file name is returned.
|
|
|
|
* If project_file_filter is set, the file open dialog will have a file filter for Geany project
|
|
|
|
* files, a filter for executables otherwise. */
|
|
|
|
gchar *win32_show_project_open_dialog(GtkWidget *parent, const gchar *title,
|
|
|
|
const gchar *initial_dir, gboolean allow_new_file,
|
|
|
|
gboolean project_file_filter)
|
2007-02-19 18:58:32 +00:00
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
OPENFILENAMEW of;
|
2007-02-19 18:58:32 +00:00
|
|
|
gint retval;
|
2009-05-26 19:27:23 +00:00
|
|
|
wchar_t fname[MAX_PATH];
|
|
|
|
wchar_t w_title[512];
|
|
|
|
gchar *filename;
|
2007-02-19 18:58:32 +00:00
|
|
|
|
|
|
|
fname[0] = '\0';
|
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, title, -1, w_title, sizeof(w_title));
|
|
|
|
|
2007-12-07 16:31:38 +00:00
|
|
|
if (parent == NULL)
|
2008-05-22 14:41:28 +00:00
|
|
|
parent = main_widgets.window;
|
2007-12-07 16:31:38 +00:00
|
|
|
|
2007-02-19 18:58:32 +00:00
|
|
|
/* initialise file dialog info struct */
|
|
|
|
memset(&of, 0, sizeof of);
|
|
|
|
#ifdef OPENFILENAME_SIZE_VERSION_400
|
|
|
|
of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
|
|
|
|
#else
|
|
|
|
of.lStructSize = sizeof of;
|
|
|
|
#endif
|
2007-12-07 16:31:38 +00:00
|
|
|
of.hwndOwner = GDK_WINDOW_HWND(parent->window);
|
2009-05-26 19:27:23 +00:00
|
|
|
of.lpstrFilter = get_filters(project_file_filter);
|
2007-02-19 18:58:32 +00:00
|
|
|
|
|
|
|
of.lpstrCustomFilter = NULL;
|
|
|
|
of.nFilterIndex = 0;
|
|
|
|
of.lpstrFile = fname;
|
2009-05-26 19:27:23 +00:00
|
|
|
of.lpstrInitialDir = get_dir_for_path(initial_dir);
|
2007-02-19 18:58:32 +00:00
|
|
|
of.nMaxFile = 2048;
|
|
|
|
of.lpstrFileTitle = NULL;
|
2009-05-26 19:27:23 +00:00
|
|
|
of.lpstrTitle = w_title;
|
|
|
|
of.lpstrDefExt = L"";
|
2007-02-19 18:58:32 +00:00
|
|
|
of.Flags = OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_HIDEREADONLY;
|
|
|
|
if (! allow_new_file)
|
|
|
|
of.Flags |= OFN_FILEMUSTEXIST;
|
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
retval = GetOpenFileNameW(&of);
|
2007-02-19 18:58:32 +00:00
|
|
|
|
|
|
|
if (! retval)
|
|
|
|
{
|
|
|
|
if (CommDlgExtendedError())
|
|
|
|
{
|
|
|
|
gchar *error;
|
|
|
|
error = g_strdup_printf("File dialog box error (%x)", (int)CommDlgExtendedError());
|
2007-07-04 17:08:53 +00:00
|
|
|
win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
|
2007-02-19 18:58:32 +00:00
|
|
|
g_free(error);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* convert the resulting filename into UTF-8 (from whatever encoding it has at this moment) */
|
2009-05-26 19:27:23 +00:00
|
|
|
filename = g_malloc0(MAX_PATH * 2);
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, fname, -1, filename, MAX_PATH * 2, NULL, NULL);
|
|
|
|
|
|
|
|
return filename;
|
2007-02-19 18:58:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-21 17:25:58 +00:00
|
|
|
/* initial_dir can be NULL to use the current working directory.
|
|
|
|
* Returns: TRUE if the dialog was not cancelled. */
|
|
|
|
gboolean win32_show_file_dialog(gboolean file_open, const gchar *initial_dir)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
OPENFILENAMEW of;
|
2005-11-22 12:26:26 +00:00
|
|
|
gint retval;
|
2009-05-26 19:27:23 +00:00
|
|
|
gchar tmp[MAX_PATH];
|
|
|
|
wchar_t fname[MAX_PATH];
|
|
|
|
wchar_t w_dir[MAX_PATH];
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
fname[0] = '\0';
|
|
|
|
|
2009-06-17 18:18:46 +00:00
|
|
|
if (initial_dir != NULL)
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, initial_dir, -1, w_dir, sizeof(w_dir));
|
2009-05-26 19:27:23 +00:00
|
|
|
|
|
|
|
/* initialise file dialog info struct */
|
2005-11-22 12:26:26 +00:00
|
|
|
memset(&of, 0, sizeof of);
|
|
|
|
#ifdef OPENFILENAME_SIZE_VERSION_400
|
|
|
|
of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
|
|
|
|
#else
|
|
|
|
of.lStructSize = sizeof of;
|
|
|
|
#endif
|
2008-05-22 14:41:28 +00:00
|
|
|
of.hwndOwner = GDK_WINDOW_HWND(main_widgets.window->window);
|
2009-05-26 19:27:23 +00:00
|
|
|
of.lpstrFilter = get_file_filters();
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
of.lpstrCustomFilter = NULL;
|
2008-05-07 13:54:21 +00:00
|
|
|
of.nFilterIndex = GEANY_FILETYPES_NONE + 1;
|
2005-11-22 12:26:26 +00:00
|
|
|
of.lpstrFile = fname;
|
2009-06-17 18:18:46 +00:00
|
|
|
of.lpstrInitialDir = (initial_dir != NULL) ? w_dir : NULL;
|
2005-11-22 12:26:26 +00:00
|
|
|
of.nMaxFile = 2048;
|
|
|
|
of.lpstrFileTitle = NULL;
|
|
|
|
of.lpstrTitle = NULL;
|
2009-05-26 19:27:23 +00:00
|
|
|
of.lpstrDefExt = L"";
|
2005-11-22 12:26:26 +00:00
|
|
|
if (file_open)
|
|
|
|
{
|
2006-01-03 12:33:27 +00:00
|
|
|
of.Flags = OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_EXPLORER;
|
2009-05-26 19:27:23 +00:00
|
|
|
retval = GetOpenFileNameW(&of);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
of.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
|
2009-05-26 19:27:23 +00:00
|
|
|
retval = GetSaveFileNameW(&of);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!retval)
|
|
|
|
{
|
|
|
|
if (CommDlgExtendedError())
|
|
|
|
{
|
|
|
|
gchar error[100];
|
2008-02-24 10:27:32 +00:00
|
|
|
g_snprintf(error, sizeof error, "File dialog box error (%x)", (int)CommDlgExtendedError());
|
2007-07-04 17:08:53 +00:00
|
|
|
win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-11-07 11:24:22 +00:00
|
|
|
return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (file_open)
|
|
|
|
{
|
2009-07-20 23:09:29 +00:00
|
|
|
guint x;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
x = of.nFileOffset - 1;
|
2009-05-26 19:27:23 +00:00
|
|
|
if (x != wcslen(fname))
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* open a single file */
|
2009-05-26 19:27:23 +00:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, fname, -1, tmp, sizeof(tmp), NULL, NULL);
|
|
|
|
document_open_file(tmp, of.Flags & OFN_READONLY, NULL, NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* open multiple files */
|
2009-05-26 19:27:23 +00:00
|
|
|
gchar file_name[MAX_PATH];
|
|
|
|
gchar dir_name[MAX_PATH];
|
2009-05-27 18:00:05 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, fname, of.nFileOffset,
|
|
|
|
dir_name, sizeof(dir_name), NULL, NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
for (; ;)
|
|
|
|
{
|
|
|
|
if (! fname[x])
|
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
if (! fname[x+1])
|
|
|
|
break;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, fname + x + 1, -1,
|
|
|
|
tmp, sizeof(tmp), NULL, NULL);
|
|
|
|
g_snprintf(file_name, 511, "%s\\%s", dir_name, tmp);
|
2008-02-08 11:19:45 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* convert the resulting filename into UTF-8 */
|
2009-05-26 19:27:23 +00:00
|
|
|
document_open_file(file_name, of.Flags & OFN_READONLY, NULL, NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_current();
|
2008-06-03 17:22:04 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, fname, -1, tmp, sizeof(tmp), NULL, NULL);
|
|
|
|
document_save_file_as(doc, tmp);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-11-07 11:24:22 +00:00
|
|
|
return (retval != 0);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void win32_show_font_dialog(void)
|
|
|
|
{
|
|
|
|
gint retval;
|
2009-05-26 19:27:23 +00:00
|
|
|
CHOOSEFONT cf;
|
|
|
|
LOGFONT lf; /* logical font structure */
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
memset(&cf, 0, sizeof cf);
|
|
|
|
cf.lStructSize = sizeof cf;
|
2008-05-22 14:41:28 +00:00
|
|
|
cf.hwndOwner = GDK_WINDOW_HWND(main_widgets.window->window);
|
2005-11-22 12:26:26 +00:00
|
|
|
cf.lpLogFont = &lf;
|
|
|
|
cf.Flags = CF_APPLY | CF_NOSCRIPTSEL | CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
|
|
|
|
|
|
|
|
retval = ChooseFont(&cf);
|
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
if (retval)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
gchar *editorfont = g_strdup_printf("%s %d", lf.lfFaceName, (cf.iPointSize / 10));
|
|
|
|
ui_set_editor_font(editorfont);
|
|
|
|
g_free(editorfont);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-27 23:17:31 +00:00
|
|
|
void win32_show_color_dialog(const gchar *colour)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
CHOOSECOLOR cc;
|
|
|
|
static COLORREF acr_cust_clr[16];
|
|
|
|
static DWORD rgb_current;
|
|
|
|
gchar *hex = g_malloc0(12);
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_current();
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Initialize CHOOSECOLOR */
|
2005-11-22 12:26:26 +00:00
|
|
|
memset(&cc, 0, sizeof cc);
|
|
|
|
cc.lStructSize = sizeof(cc);
|
2008-05-22 14:41:28 +00:00
|
|
|
cc.hwndOwner = GDK_WINDOW_HWND(main_widgets.window->window);
|
2005-11-22 12:26:26 +00:00
|
|
|
cc.lpCustColors = (LPDWORD) acr_cust_clr;
|
2006-07-27 23:17:31 +00:00
|
|
|
cc.rgbResult = (colour != NULL) ? utils_strtod(colour, NULL, colour[0] == '#') : 0;
|
2005-11-22 12:26:26 +00:00
|
|
|
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
|
|
|
|
|
|
|
|
if (ChooseColor(&cc))
|
|
|
|
{
|
|
|
|
rgb_current = cc.rgbResult;
|
2007-07-17 09:14:13 +00:00
|
|
|
g_snprintf(hex, 11, "#%02X%02X%02X",
|
2005-11-22 12:26:26 +00:00
|
|
|
(guint) (utils_scale_round(GetRValue(rgb_current), 255)),
|
|
|
|
(guint) (utils_scale_round(GetGValue(rgb_current), 255)),
|
|
|
|
(guint) (utils_scale_round(GetBValue(rgb_current), 255)));
|
|
|
|
|
2008-10-15 14:14:31 +00:00
|
|
|
editor_insert_color(doc->editor, hex);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
g_free(hex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void win32_show_pref_file_dialog(GtkEntry *item)
|
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
OPENFILENAMEW of;
|
|
|
|
gint retval, len;
|
|
|
|
wchar_t fname[MAX_PATH];
|
|
|
|
gchar tmp[MAX_PATH];
|
|
|
|
gchar **field, *filename;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
fname[0] = '\0';
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* cut the options from the command line */
|
2005-11-22 12:26:26 +00:00
|
|
|
field = g_strsplit(gtk_entry_get_text(GTK_ENTRY(item)), " ", 2);
|
2009-05-26 19:27:23 +00:00
|
|
|
if (field[0])
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
filename = g_find_program_in_path(field[0]);
|
2009-05-26 19:27:23 +00:00
|
|
|
if (filename != NULL && g_file_test(filename, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, filename, -1, fname, sizeof(fname));
|
|
|
|
g_free(filename);
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize file dialog info struct */
|
|
|
|
memset(&of, 0, sizeof of);
|
|
|
|
#ifdef OPENFILENAME_SIZE_VERSION_400
|
|
|
|
of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
|
|
|
|
#else
|
|
|
|
of.lStructSize = sizeof of;
|
|
|
|
#endif
|
2007-11-01 12:53:00 +00:00
|
|
|
of.hwndOwner = GDK_WINDOW_HWND(ui_widgets.prefs_dialog->window);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
of.lpstrFilter = get_filters(FALSE);
|
2005-11-22 12:26:26 +00:00
|
|
|
of.lpstrCustomFilter = NULL;
|
|
|
|
of.nFilterIndex = 1;
|
|
|
|
|
|
|
|
of.lpstrFile = fname;
|
2009-05-26 19:27:23 +00:00
|
|
|
of.nMaxFile = 2048;
|
2005-11-22 12:26:26 +00:00
|
|
|
of.lpstrFileTitle = NULL;
|
2008-02-27 13:17:29 +00:00
|
|
|
/*of.lpstrInitialDir = g_get_home_dir();*/
|
2005-11-22 12:26:26 +00:00
|
|
|
of.lpstrInitialDir = NULL;
|
|
|
|
of.lpstrTitle = NULL;
|
2009-05-26 19:27:23 +00:00
|
|
|
of.lpstrDefExt = L"exe";
|
2008-02-08 11:19:45 +00:00
|
|
|
of.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER;
|
2009-05-26 19:27:23 +00:00
|
|
|
retval = GetOpenFileNameW(&of);
|
2007-02-19 18:58:32 +00:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
if (!retval)
|
|
|
|
{
|
|
|
|
if (CommDlgExtendedError())
|
|
|
|
{
|
|
|
|
gchar error[100];
|
2008-02-24 10:27:32 +00:00
|
|
|
g_snprintf(error, sizeof error, "File dialog box error (%x)", (int)CommDlgExtendedError());
|
2007-07-04 17:08:53 +00:00
|
|
|
win32_message_dialog(NULL, GTK_MESSAGE_ERROR, error);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
g_strfreev(field);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-05-26 19:27:23 +00:00
|
|
|
len = WideCharToMultiByte(CP_UTF8, 0, fname, -1, tmp, sizeof(tmp), NULL, NULL);
|
|
|
|
if ((of.nFileOffset - 1) != len)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
if (g_strv_length(field) > 1)
|
2008-02-27 13:17:29 +00:00
|
|
|
/* add the command line args of the old command */
|
|
|
|
/** TODO this fails badly when the old command contained spaces, we need quoting here */
|
2005-11-22 12:26:26 +00:00
|
|
|
filename = g_strconcat(tmp, " ", field[1], NULL);
|
|
|
|
else
|
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
filename = g_strdup(tmp);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(item), filename);
|
|
|
|
g_free(filename);
|
|
|
|
}
|
|
|
|
g_strfreev(field);
|
|
|
|
}
|
|
|
|
|
2006-07-24 23:56:50 +00:00
|
|
|
|
|
|
|
/* Creates a native Windows message box of the given type and returns always TRUE
|
|
|
|
* or FALSE representing th pressed Yes or No button.
|
|
|
|
* If type is not GTK_MESSAGE_QUESTION, it returns always TRUE. */
|
2007-07-04 17:08:53 +00:00
|
|
|
gboolean win32_message_dialog(GtkWidget *parent, GtkMessageType type, const gchar *msg)
|
2006-07-24 23:56:50 +00:00
|
|
|
{
|
|
|
|
gboolean ret = TRUE;
|
|
|
|
gint rc;
|
|
|
|
guint t;
|
2006-07-25 12:44:42 +00:00
|
|
|
const gchar *title;
|
2007-11-04 15:48:14 +00:00
|
|
|
HWND parent_hwnd = NULL;
|
2006-07-24 23:56:50 +00:00
|
|
|
static wchar_t w_msg[512];
|
|
|
|
static wchar_t w_title[512];
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2006-07-25 12:44:42 +00:00
|
|
|
case GTK_MESSAGE_ERROR:
|
|
|
|
{
|
|
|
|
t = MB_OK | MB_ICONERROR;
|
|
|
|
title = _("Error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GTK_MESSAGE_QUESTION:
|
|
|
|
{
|
|
|
|
t = MB_YESNO | MB_ICONQUESTION;
|
|
|
|
title = _("Question");
|
|
|
|
break;
|
|
|
|
}
|
2006-10-25 14:38:48 +00:00
|
|
|
case GTK_MESSAGE_WARNING:
|
|
|
|
{
|
|
|
|
t = MB_OK | MB_ICONWARNING;
|
|
|
|
title = _("Warning");
|
|
|
|
break;
|
|
|
|
}
|
2006-07-25 12:44:42 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
t = MB_OK | MB_ICONINFORMATION;
|
|
|
|
title = _("Information");
|
|
|
|
break;
|
|
|
|
}
|
2006-07-24 23:56:50 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* convert the Unicode chars to wide chars */
|
|
|
|
/** TODO test if LANG == C then possibly skip conversion => g_win32_getlocale() */
|
2007-09-12 16:25:19 +00:00
|
|
|
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));
|
2006-07-24 23:56:50 +00:00
|
|
|
|
2007-11-04 15:48:14 +00:00
|
|
|
if (parent != NULL)
|
|
|
|
parent_hwnd = GDK_WINDOW_HWND(parent->window);
|
2008-05-22 14:41:28 +00:00
|
|
|
else if (main_widgets.window != NULL)
|
|
|
|
parent_hwnd = GDK_WINDOW_HWND(main_widgets.window->window);
|
2007-07-04 17:08:53 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* display the message box */
|
2007-11-04 15:48:14 +00:00
|
|
|
rc = MessageBoxW(parent_hwnd, w_msg, w_title, t);
|
2006-07-25 08:27:12 +00:00
|
|
|
|
2006-07-24 23:56:50 +00:00
|
|
|
if (type == GTK_MESSAGE_QUESTION && rc != IDYES)
|
|
|
|
ret = FALSE;
|
2006-07-25 08:27:12 +00:00
|
|
|
|
2006-07-24 23:56:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-04 15:48:14 +00:00
|
|
|
/* Little wrapper for _waccess(), returns errno or 0 if there was no error */
|
|
|
|
gint win32_check_write_permission(const gchar *dir)
|
|
|
|
{
|
2009-05-26 19:27:23 +00:00
|
|
|
static wchar_t w_dir[MAX_PATH];
|
2007-12-30 15:54:00 +00:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, dir, -1, w_dir, sizeof w_dir);
|
2008-01-09 15:40:22 +00:00
|
|
|
if (_waccess(w_dir, R_OK | W_OK) != 0)
|
2008-01-06 19:59:01 +00:00
|
|
|
return errno;
|
|
|
|
else
|
|
|
|
return 0;
|
2007-11-04 15:48:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-24 23:56:50 +00:00
|
|
|
/* Special dialog to ask for an action when closing an unsaved file */
|
2006-07-25 12:44:42 +00:00
|
|
|
gint win32_message_dialog_unsaved(const gchar *msg)
|
2006-07-24 23:56:50 +00:00
|
|
|
{
|
|
|
|
static wchar_t w_msg[512];
|
|
|
|
static wchar_t w_title[512];
|
2007-11-04 15:48:14 +00:00
|
|
|
HWND parent_hwnd = NULL;
|
2006-07-24 23:56:50 +00:00
|
|
|
gint ret;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* convert the Unicode chars to wide chars */
|
2007-09-12 16:25:19 +00:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, msg, -1, w_msg, G_N_ELEMENTS(w_msg));
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, _("Question"), -1, w_title, G_N_ELEMENTS(w_title));
|
2006-07-24 23:56:50 +00:00
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
if (main_widgets.window != NULL)
|
|
|
|
parent_hwnd = GDK_WINDOW_HWND(main_widgets.window->window);
|
2007-12-07 16:31:38 +00:00
|
|
|
|
2007-11-04 15:48:14 +00:00
|
|
|
ret = MessageBoxW(parent_hwnd, w_msg, w_title, MB_YESNOCANCEL | MB_ICONQUESTION);
|
2006-07-24 23:56:50 +00:00
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case IDYES: ret = GTK_RESPONSE_YES; break;
|
|
|
|
case IDNO: ret = GTK_RESPONSE_NO; break;
|
|
|
|
case IDCANCEL: ret = GTK_RESPONSE_CANCEL; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-25 12:44:42 +00:00
|
|
|
/* Just a simple wrapper function to open a browser window */
|
|
|
|
void win32_open_browser(const gchar *uri)
|
|
|
|
{
|
2009-07-14 18:49:31 +00:00
|
|
|
if (strncmp(uri, "file://", 7) == 0)
|
|
|
|
uri += 7;
|
|
|
|
|
2006-07-25 12:44:42 +00:00
|
|
|
ShellExecute(NULL, "open", uri, NULL, NULL, SW_SHOWNORMAL);
|
|
|
|
}
|
|
|
|
|
2008-03-14 16:59:36 +00:00
|
|
|
|
|
|
|
/* Returns TRUE if the command, which child_pid refers to, returned with a successful exit code,
|
|
|
|
* otherwise FALSE. */
|
|
|
|
gboolean win32_get_exit_status(GPid child_pid)
|
|
|
|
{
|
|
|
|
DWORD exit_code;
|
|
|
|
GetExitCodeProcess(child_pid, &exit_code);
|
2008-03-21 14:02:59 +00:00
|
|
|
|
2008-03-14 16:59:36 +00:00
|
|
|
return (exit_code == 0);
|
|
|
|
}
|
|
|
|
|
2007-12-30 15:54:00 +00:00
|
|
|
|
|
|
|
static void debug_setup_console()
|
|
|
|
{
|
|
|
|
static const WORD MAX_CONSOLE_LINES = 500;
|
|
|
|
gint hConHandle;
|
|
|
|
glong lStdHandle;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO coninfo;
|
|
|
|
FILE *fp;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* allocate a console for this app */
|
2007-12-30 15:54:00 +00:00
|
|
|
AllocConsole();
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* set the screen buffer to be big enough to let us scroll text */
|
2007-12-30 15:54:00 +00:00
|
|
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
|
|
|
|
coninfo.dwSize.Y = MAX_CONSOLE_LINES;
|
|
|
|
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* redirect unbuffered STDOUT to the console */
|
2007-12-30 15:54:00 +00:00
|
|
|
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
|
|
|
|
fp = _fdopen(hConHandle, "w");
|
|
|
|
*stdout = *fp;
|
|
|
|
setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* redirect unbuffered STDERR to the console */
|
2007-12-30 15:54:00 +00:00
|
|
|
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
|
|
|
|
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
|
|
|
|
fp = _fdopen(hConHandle, "w");
|
|
|
|
*stderr = *fp;
|
|
|
|
setvbuf(stderr, NULL, _IONBF, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-24 18:26:16 +00:00
|
|
|
void win32_init_debug_code(void)
|
2007-12-30 15:54:00 +00:00
|
|
|
{
|
|
|
|
if (app->debug_mode)
|
2008-08-13 18:42:11 +00:00
|
|
|
{
|
|
|
|
/* create a console window to get log messages on Windows */
|
|
|
|
/** TODO remove me? */
|
2007-12-30 15:54:00 +00:00
|
|
|
debug_setup_console();
|
2008-08-13 18:42:11 +00:00
|
|
|
/* Enable GLib process spawn debug mode when Geany was started with the debug flag */
|
|
|
|
g_setenv("G_SPAWN_WIN32_DEBUG", "1", FALSE);
|
2007-12-30 15:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-24 18:26:16 +00:00
|
|
|
gchar *win32_get_hostname(void)
|
2008-02-10 17:53:18 +00:00
|
|
|
{
|
|
|
|
gchar hostname[100];
|
|
|
|
DWORD size = sizeof(hostname);
|
|
|
|
|
|
|
|
if (GetComputerName(hostname, &size))
|
|
|
|
return g_strdup(hostname);
|
|
|
|
else
|
|
|
|
return g_strdup("localhost");
|
|
|
|
}
|
|
|
|
|
2008-02-24 10:27:32 +00:00
|
|
|
|
|
|
|
/* Process spawning implementation for Windows, by Pierre Joye.
|
|
|
|
* Don't call this function directly, use utils_spawn_[a]sync() instead. */
|
|
|
|
gboolean win32_spawn(const gchar *dir, gchar **argv, gchar **env, GSpawnFlags flags,
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar **std_out, gchar **std_err, gint *exit_status, GError **error)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
|
|
|
TCHAR buffer[MAX_PATH]=TEXT("");
|
|
|
|
TCHAR cmdline[MAX_PATH] = TEXT("");
|
|
|
|
TCHAR* lpPart[MAX_PATH]={NULL};
|
|
|
|
DWORD retval=0;
|
|
|
|
gint argc = 0, i;
|
|
|
|
gint cmdpos=0;
|
|
|
|
|
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
|
|
|
BOOL fSuccess;
|
|
|
|
geany_win32_spawn gw_spawn;
|
|
|
|
|
|
|
|
/* Temp file */
|
|
|
|
HANDLE hStdoutTempFile = NULL;
|
|
|
|
HANDLE hStderrTempFile = NULL;
|
|
|
|
|
|
|
|
gchar *stdout_content = NULL;
|
|
|
|
gchar *stderr_content = NULL;
|
|
|
|
|
|
|
|
while (argv[argc])
|
|
|
|
{
|
|
|
|
++argc;
|
|
|
|
}
|
|
|
|
g_return_val_if_fail (std_out == NULL ||
|
|
|
|
!(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
|
|
|
|
g_return_val_if_fail (std_err == NULL ||
|
|
|
|
!(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
|
|
|
|
|
|
|
|
if (flags & G_SPAWN_SEARCH_PATH)
|
|
|
|
{
|
2009-05-27 18:00:05 +00:00
|
|
|
retval = SearchPath(NULL, argv[0], ".exe", MAX_PATH, buffer, lpPart);
|
|
|
|
if (retval > 0)
|
|
|
|
g_snprintf(cmdline, MAX_PATH, "\"%s\"", buffer);
|
|
|
|
else
|
|
|
|
g_strlcpy(cmdline, argv[0], sizeof(cmdline));
|
2008-02-24 10:27:32 +00:00
|
|
|
cmdpos = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = cmdpos; i < argc; i++)
|
|
|
|
{
|
|
|
|
g_snprintf(cmdline, MAX_PATH, "%s %s", cmdline, argv[i]);
|
|
|
|
/*MessageBox(NULL, cmdline, cmdline, MB_OK);*/
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std_err != NULL)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
hStderrTempFile = GetTempFileHandle(error);
|
2008-02-24 10:27:32 +00:00
|
|
|
if (hStderrTempFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("win32_spawn: Second CreateFile failed (%d)", (gint) GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std_out != NULL)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
hStdoutTempFile = GetTempFileHandle(error);
|
2008-02-24 10:27:32 +00:00
|
|
|
if (hStdoutTempFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("win32_spawn: Second CreateFile failed (%d)", (gint) GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the bInheritHandle flag so pipe handles are inherited. */
|
|
|
|
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
saAttr.bInheritHandle = TRUE;
|
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
/* Get the handle to the current STDOUT and STDERR. */
|
|
|
|
gw_spawn.hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
gw_spawn.hStderr = GetStdHandle(STD_ERROR_HANDLE);
|
2008-02-27 16:08:07 +00:00
|
|
|
gw_spawn.dwExitCode = 0;
|
2008-02-24 10:27:32 +00:00
|
|
|
|
|
|
|
/* Create a pipe for the child process's STDOUT. */
|
|
|
|
if (! CreatePipe(&(gw_spawn.hChildStdoutRd), &(gw_spawn.hChildStdoutWr), &saAttr, 0))
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("win32_spawn: Stdout pipe creation failed (%d)", (gint) GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that the read handle to the child process's pipe for STDOUT is not inherited.*/
|
|
|
|
SetHandleInformation(gw_spawn.hChildStdoutRd, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
|
|
|
|
/* Create a pipe for the child process's STDERR. */
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! CreatePipe(&(gw_spawn.hChildStderrRd), &(gw_spawn.hChildStderrWr), &saAttr, 0))
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("win32_spawn: Stderr pipe creation failed");
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that the read handle to the child process's pipe for STDOUT is not inherited.*/
|
|
|
|
SetHandleInformation(gw_spawn.hChildStderrRd, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
|
|
|
|
/* Create a pipe for the child process's STDIN. */
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! CreatePipe(&(gw_spawn.hChildStdinRd), &(gw_spawn.hChildStdinWr), &saAttr, 0))
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("win32_spawn: Stdin pipe creation failed");
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that the write handle to the child process's pipe for STDIN is not inherited. */
|
|
|
|
SetHandleInformation(gw_spawn.hChildStdinWr, HANDLE_FLAG_INHERIT, 0);
|
|
|
|
|
|
|
|
/* Now create the child process. */
|
2008-02-27 16:08:07 +00:00
|
|
|
fSuccess = CreateChildProcess(&gw_spawn, cmdline, dir, error);
|
|
|
|
if (exit_status)
|
|
|
|
{
|
|
|
|
*exit_status = gw_spawn.dwExitCode;
|
|
|
|
}
|
|
|
|
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! fSuccess)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
geany_debug("win32_spawn: Create process failed");
|
2008-03-21 18:29:25 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "Create process failed");
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read from pipe that is the standard output for child process. */
|
|
|
|
if (std_out != NULL)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
ReadFromPipe(gw_spawn.hChildStdoutRd, gw_spawn.hChildStdoutWr, hStdoutTempFile, error);
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! GetContentFromHandle(hStdoutTempFile, &stdout_content, error))
|
2008-02-27 16:08:07 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-02-24 10:27:32 +00:00
|
|
|
*std_out = stdout_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std_err != NULL)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
ReadFromPipe(gw_spawn.hChildStderrRd, gw_spawn.hChildStderrWr, hStderrTempFile, error);
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! GetContentFromHandle(hStderrTempFile, &stderr_content, error))
|
2008-02-27 16:08:07 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2008-02-24 10:27:32 +00:00
|
|
|
*std_err = stderr_content;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 16:08:07 +00:00
|
|
|
static gboolean GetContentFromHandle(HANDLE hFile, gchar **content, GError **error)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
|
|
|
DWORD filesize;
|
|
|
|
gchar * buffer;
|
|
|
|
DWORD dwRead;
|
|
|
|
|
2009-05-27 18:54:16 +00:00
|
|
|
filesize = GetFileSize(hFile, NULL);
|
2008-02-24 10:27:32 +00:00
|
|
|
if (filesize < 1)
|
|
|
|
{
|
|
|
|
*content = NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-05-27 18:54:16 +00:00
|
|
|
buffer = g_malloc(filesize + 1);
|
|
|
|
if (! buffer)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
2008-02-24 10:27:32 +00:00
|
|
|
geany_debug("GetContentFromHandle: Alloc failed");
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-05-27 18:54:16 +00:00
|
|
|
SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
|
|
|
|
if (! ReadFile(hFile, buffer, filesize, &dwRead, NULL) || dwRead == 0)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
2008-02-24 10:27:32 +00:00
|
|
|
geany_debug("GetContentFromHandle: Cannot read tempfile");
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_FAILED, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! CloseHandle(hFile))
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("GetContentFromHandle: CloseHandle failed (%d)", (gint) GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_FAILED, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
g_free(buffer);
|
|
|
|
*content = NULL;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-05-27 18:34:39 +00:00
|
|
|
buffer[filesize] = '\0';
|
2008-02-24 10:27:32 +00:00
|
|
|
*content = buffer;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-02 13:33:39 +00:00
|
|
|
gchar *win32_expand_environment_variables(const gchar *str)
|
|
|
|
{
|
|
|
|
gchar expCmdline[32768]; /* 32768 is the limit for ExpandEnvironmentStrings() */
|
|
|
|
|
|
|
|
if (ExpandEnvironmentStrings((LPCTSTR) str, (LPTSTR) expCmdline, sizeof(expCmdline)) != 0)
|
|
|
|
return g_strdup(expCmdline);
|
|
|
|
else
|
|
|
|
return g_strdup(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 16:08:07 +00:00
|
|
|
static gboolean CreateChildProcess(geany_win32_spawn *gw_spawn, TCHAR *szCmdline, const TCHAR *dir, GError **error)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
|
|
|
PROCESS_INFORMATION piProcInfo;
|
|
|
|
STARTUPINFO siStartInfo;
|
|
|
|
BOOL bFuncRetn = FALSE;
|
2009-08-02 13:33:39 +00:00
|
|
|
gchar *expandedCmdline;
|
2008-02-24 10:27:32 +00:00
|
|
|
|
|
|
|
/* Set up members of the PROCESS_INFORMATION structure. */
|
|
|
|
ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION) );
|
|
|
|
|
|
|
|
/* Set up members of the STARTUPINFO structure.*/
|
|
|
|
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO) );
|
|
|
|
|
|
|
|
siStartInfo.cb = sizeof(STARTUPINFO);
|
|
|
|
siStartInfo.hStdError = gw_spawn->hChildStderrWr;
|
|
|
|
siStartInfo.hStdOutput = gw_spawn->hChildStdoutWr;
|
|
|
|
siStartInfo.hStdInput = gw_spawn->hChildStdinRd;
|
|
|
|
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
|
|
|
|
|
2009-08-02 13:33:39 +00:00
|
|
|
/* Expand environment variables like %blah%. */
|
|
|
|
expandedCmdline = win32_expand_environment_variables(szCmdline);
|
|
|
|
|
2008-02-24 10:27:32 +00:00
|
|
|
/* Create the child process. */
|
|
|
|
bFuncRetn = CreateProcess(NULL,
|
2009-08-02 13:33:39 +00:00
|
|
|
expandedCmdline, /* command line */
|
2008-02-24 10:27:32 +00:00
|
|
|
NULL, /* process security attributes */
|
|
|
|
NULL, /* primary thread security attributes */
|
|
|
|
TRUE, /* handles are inherited */
|
|
|
|
CREATE_NO_WINDOW, /* creation flags */
|
|
|
|
NULL, /* use parent's environment */
|
|
|
|
dir, /* use parent's current directory */
|
|
|
|
&siStartInfo, /* STARTUPINFO pointer */
|
|
|
|
&piProcInfo); /* receives PROCESS_INFORMATION */
|
|
|
|
|
2009-08-02 13:33:39 +00:00
|
|
|
g_free(expandedCmdline);
|
|
|
|
|
2008-02-24 10:27:32 +00:00
|
|
|
if (bFuncRetn == 0)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
2008-03-21 18:29:25 +00:00
|
|
|
geany_debug("CreateChildProcess: CreateProcess failed (%s)", msg);
|
2008-03-14 16:19:53 +00:00
|
|
|
if (*error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-27 18:54:16 +00:00
|
|
|
gint i;
|
|
|
|
DWORD dwStatus;
|
2008-02-27 16:08:07 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 2 && (dwStatus = WaitForSingleObject(piProcInfo.hProcess, 30*1000)) == WAIT_TIMEOUT; i++)
|
|
|
|
{
|
|
|
|
geany_debug("CreateChildProcess: CreateProcess failed");
|
|
|
|
TerminateProcess(piProcInfo.hProcess, WAIT_TIMEOUT); /* NOTE: This will not kill grandkids. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetExitCodeProcess(piProcInfo.hProcess, &gw_spawn->dwExitCode) != 0)
|
|
|
|
{
|
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
geany_debug("GetExitCodeProcess failed: %s", msg);
|
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_FAILED, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
|
|
|
}
|
2008-02-24 10:27:32 +00:00
|
|
|
CloseHandle(piProcInfo.hProcess);
|
|
|
|
CloseHandle(piProcInfo.hThread);
|
|
|
|
return bFuncRetn;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 16:08:07 +00:00
|
|
|
static VOID ReadFromPipe(HANDLE hRead, HANDLE hWrite, HANDLE hFile, GError **error)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
|
|
|
DWORD dwRead, dwWritten;
|
|
|
|
CHAR chBuf[BUFSIZE];
|
|
|
|
|
|
|
|
/* Close the write end of the pipe before reading from the
|
|
|
|
read end of the pipe. */
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! CloseHandle(hWrite))
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
2008-02-24 10:27:32 +00:00
|
|
|
geany_debug("ReadFromPipe: Closing handle failed");
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR_PIPE, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read output from the child process, and write to parent's STDOUT. */
|
|
|
|
for (;;)
|
|
|
|
{
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! ReadFile(hRead, chBuf, BUFSIZE, &dwRead, NULL) || dwRead == 0)
|
|
|
|
break;
|
2008-02-24 10:27:32 +00:00
|
|
|
|
2009-05-27 18:54:16 +00:00
|
|
|
if (! WriteFile(hFile, chBuf, dwRead, &dwWritten, NULL))
|
2008-02-24 10:27:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 16:08:07 +00:00
|
|
|
static HANDLE GetTempFileHandle(GError **error)
|
2008-02-24 10:27:32 +00:00
|
|
|
{
|
|
|
|
/* Temp file */
|
|
|
|
DWORD dwBufSize=BUFSIZE;
|
|
|
|
UINT uRetVal;
|
|
|
|
TCHAR szTempName[BUFSIZE];
|
|
|
|
TCHAR lpPathBuffer[BUFSIZE];
|
|
|
|
DWORD dwRetVal;
|
|
|
|
HANDLE hTempFile;
|
|
|
|
|
|
|
|
/* Get the temp path. */
|
|
|
|
dwRetVal = GetTempPath(dwBufSize, /* length of the buffer*/
|
|
|
|
lpPathBuffer); /* buffer for path */
|
|
|
|
|
|
|
|
if (dwRetVal > dwBufSize || (dwRetVal == 0))
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("GetTempFileHandle: GetTempPath failed (%d)", (gint) GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a temporary file for STDOUT. */
|
|
|
|
uRetVal = GetTempFileName(lpPathBuffer, /* directory for tmp files */
|
|
|
|
TEXT("GEANY_VCDIFF_"), /* temp file name prefix */
|
|
|
|
0, /* create unique name */
|
|
|
|
szTempName); /* buffer for name */
|
|
|
|
if (uRetVal == 0)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("GetTempFileName failed (%d)", (gint) GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hTempFile = CreateFile((LPTSTR) szTempName, /* file name */
|
|
|
|
GENERIC_READ | GENERIC_WRITE, /* open r-w */
|
|
|
|
0, /* do not share */
|
|
|
|
NULL, /* default security */
|
|
|
|
CREATE_ALWAYS, /* overwrite existing */
|
|
|
|
FILE_ATTRIBUTE_NORMAL,/* normal file */
|
|
|
|
NULL); /* no template */
|
|
|
|
|
|
|
|
if (hTempFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-02-27 16:08:07 +00:00
|
|
|
gchar *msg = g_win32_error_message(GetLastError());
|
|
|
|
geany_debug("GetTempFileHandle: Second CreateFile failed (%d)", (gint) GetLastError());
|
2008-03-14 16:19:53 +00:00
|
|
|
if (error != NULL)
|
2009-07-20 23:09:29 +00:00
|
|
|
*error = g_error_new(G_SPAWN_ERROR, G_FILE_ERROR, "%s", msg);
|
2008-02-27 16:08:07 +00:00
|
|
|
g_free(msg);
|
2008-02-24 10:27:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return hTempFile;
|
|
|
|
}
|
|
|
|
|
2008-03-26 18:53:59 +00:00
|
|
|
|
|
|
|
/* From GDK (they got it from MS Knowledge Base article Q130698) */
|
|
|
|
static gboolean resolve_link(HWND hWnd, wchar_t *link, gchar **lpszPath)
|
|
|
|
{
|
|
|
|
WIN32_FILE_ATTRIBUTE_DATA wfad;
|
|
|
|
HRESULT hres;
|
|
|
|
IShellLinkW *pslW = NULL;
|
|
|
|
IPersistFile *ppf = NULL;
|
2008-04-08 15:00:06 +00:00
|
|
|
LPVOID pslWV = NULL;
|
|
|
|
LPVOID ppfV = NULL;
|
2008-03-26 18:53:59 +00:00
|
|
|
|
|
|
|
/* 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) ||
|
|
|
|
(wfad.nFileSizeHigh == 0 && wfad.nFileSizeLow == 0))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assume failure to start with: */
|
|
|
|
*lpszPath = 0;
|
|
|
|
|
|
|
|
CoInitialize(NULL);
|
|
|
|
|
|
|
|
hres = CoCreateInstance(
|
2008-04-08 15:00:06 +00:00
|
|
|
&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLinkW, &pslWV);
|
2008-03-26 18:53:59 +00:00
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
/* The IShellLink interface supports the IPersistFile interface.
|
|
|
|
* Get an interface pointer to it. */
|
2008-04-08 15:00:06 +00:00
|
|
|
pslW = (IShellLinkW*) pslWV;
|
|
|
|
hres = pslW->lpVtbl->QueryInterface(pslW, &IID_IPersistFile, &ppfV);
|
2008-04-23 16:47:42 +00:00
|
|
|
}
|
2008-03-26 18:53:59 +00:00
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
/* Load the file. */
|
2008-04-08 15:00:06 +00:00
|
|
|
ppf = (IPersistFile*) ppfV;
|
2008-03-26 18:53:59 +00:00
|
|
|
hres = ppf->lpVtbl->Load(ppf, link, STGM_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
/* Resolve the link by calling the Resolve() interface function. */
|
|
|
|
hres = pslW->lpVtbl->Resolve(pslW, hWnd, SLR_ANY_MATCH | SLR_NO_UI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SUCCEEDED(hres))
|
|
|
|
{
|
|
|
|
wchar_t 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ppf)
|
|
|
|
ppf->lpVtbl->Release(ppf);
|
|
|
|
|
|
|
|
if (pslW)
|
|
|
|
pslW->lpVtbl->Release(pslW);
|
|
|
|
|
|
|
|
return SUCCEEDED(hres);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Checks whether file_name is a Windows shortcut. file_name is expected in UTF-8 encoding.
|
|
|
|
* If file_name is a Windows shortcut, it returns the target in UTF-8 encoding.
|
|
|
|
* If it is not a shortcut, it returns a newly allocated copy of file_name. */
|
|
|
|
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);
|
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
resolve_link(GDK_WINDOW_HWND(main_widgets.window->window), wfilename, &path);
|
2008-03-26 18:53:59 +00:00
|
|
|
g_free(wfilename);
|
2008-04-23 16:47:42 +00:00
|
|
|
|
2008-03-26 18:53:59 +00:00
|
|
|
if (path == NULL)
|
|
|
|
return g_strdup(file_name);
|
|
|
|
else
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-02-24 18:26:16 +00:00
|
|
|
void win32_set_working_directory(const gchar *dir)
|
|
|
|
{
|
|
|
|
SetCurrentDirectory(dir);
|
|
|
|
}
|
2008-03-26 18:53:59 +00:00
|
|
|
|
2009-03-26 20:10:47 +00:00
|
|
|
gchar *win32_get_installation_dir(void)
|
|
|
|
{
|
|
|
|
#if GLIB_CHECK_VERSION(2, 16, 0)
|
|
|
|
return g_win32_get_package_installation_directory_of_module(NULL);
|
|
|
|
#else
|
|
|
|
return g_win32_get_package_installation_directory(NULL, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
#endif
|