summaryrefslogtreecommitdiffstats
path: root/gedit/gedit-app-win32.c
blob: 61897a5d4a685cd283da1049be81e55cb6351fb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * This file is part of gedit
 *
 * Copyright (C) 2010 - Jesse van den Kieboom
 *
 * gedit 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.
 *
 * gedit 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 gedit; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include "gedit-app-win32.h"

#define SAVE_DATADIR DATADIR
#undef DATADIR

#include <io.h>
#include <conio.h>

#ifndef _WIN32_WINNT
#  define _WIN32_WINNT 0x0501
#endif

#include <windows.h>

#define DATADIR SAVE_DATADIR
#undef SAVE_DATADIR

struct _GeditAppWin32
{
	GeditApp parent_instance;
};

G_DEFINE_TYPE (GeditAppWin32, gedit_app_win32, GEDIT_TYPE_APP)

static gchar *
gedit_app_win32_get_help_uri_impl (GeditApp    *app,
                                   const gchar *name_of_user_manual,
                                   const gchar *link_id_within_user_manual)
{
	/* FIXME: name_of_user_manual is expected to be always "gedit" here. */

	if (link_id_within_user_manual != NULL)
	{
		return g_strdup_printf ("https://gedit-technology.net/user-manuals/gedit/%s",
		                        link_id_within_user_manual);
	}

	return g_strdup ("https://gedit-technology.net/user-manuals/gedit/");
}

static void
setup_path (void)
{
	gchar *path;
	gchar *installdir;
	gchar *bin;

	installdir = g_win32_get_package_installation_directory_of_module (NULL);

	bin = g_build_filename (installdir, "bin", NULL);
	g_free (installdir);

	/* Set PATH to include the gedit executable's folder */
	path = g_build_path (";", bin, g_getenv ("PATH"), NULL);
	g_free (bin);

	if (!g_setenv ("PATH", path, TRUE))
	{
		g_warning ("Could not set PATH for gedit");
	}

	g_free (path);
}

static void
prep_console (void)
{
	/* If we open gedit from a console get the stdout printing */
	if (fileno (stdout) != -1 &&
		_get_osfhandle (fileno (stdout)) != -1)
	{
		/* stdout is fine, presumably redirected to a file or pipe */
	}
	else
	{
		typedef BOOL (* WINAPI AttachConsole_t) (DWORD);

		AttachConsole_t p_AttachConsole =
			(AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"),
							  "AttachConsole");

		if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
		{
			freopen ("CONOUT$", "w", stdout);
			dup2 (fileno (stdout), 1);
			freopen ("CONOUT$", "w", stderr);
			dup2 (fileno (stderr), 2);
		}
	}
}

static void
gedit_app_win32_startup (GApplication *application)
{
	G_APPLICATION_CLASS (gedit_app_win32_parent_class)->startup (application);

	setup_path ();
	prep_console ();
}

static void
gedit_app_win32_class_init (GeditAppWin32Class *klass)
{
	GApplicationClass *gapp_class = G_APPLICATION_CLASS (klass);
	GeditAppClass *app_class = GEDIT_APP_CLASS (klass);

	gapp_class->startup = gedit_app_win32_startup;

	app_class->get_help_uri = gedit_app_win32_get_help_uri_impl;
}

static void
gedit_app_win32_init (GeditAppWin32 *self)
{
}

/* ex:set ts=8 noet: */