summaryrefslogtreecommitdiffstats
path: root/litosaccels.c
blob: db7126c019bfd85092f64302b418ae3d3a2027f9 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <gtk/gtk.h>

#include "litosapp.h"
#include "litosappprefs.h"
#include "litosfile.h"

gboolean litos_file_load (GFile *file, GtkTextBuffer *buffer, GError **error);
void litos_file_highlight_buffer(LitosFile *file);
GtkTextBuffer *litos_file_get_buffer(LitosFile *file);
GFile *litos_file_get_gfile(LitosFile* file);
void litos_file_reset_gfile(LitosFile *file);
gchar *litos_file_get_name(LitosFile *file);

gboolean litos_app_window_remove_child(LitosAppWindow *win);
void litos_app_window_save(LitosAppWindow *win, LitosFile *file);
void litos_app_window_save_as(LitosAppWindow *app);
LitosFile * litos_app_window_open(LitosAppWindow *win, GFile *gf);
LitosFile * litos_app_window_current_file(LitosAppWindow *win);
LitosFile * litos_app_window_new_file(LitosAppWindow *win);
guint litos_app_window_get_array_len(LitosAppWindow *win);
gboolean litos_app_window_quit (GtkWindow *window, gpointer user_data);
void ctrl_f(LitosAppWindow *win);
void Esc(LitosAppWindow *win);

void litos_app_error_dialog(GtkWindow *window, GError *error, char *filename);

gboolean litos_app_check_duplicate(char *filename, LitosAppWindow *win);
void set_search_entry(LitosAppWindow *win);

static void
open_cb (GtkWidget *dialog, gint response, gpointer window)
{
	if (response == GTK_RESPONSE_ACCEPT)
	{
		GFile *gfile = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));

		LitosAppWindow *win = LITOS_APP_WINDOW(window);

		if (gfile != NULL)
		{
			char *gfile_name = g_file_get_path(gfile);

			if (!litos_app_check_duplicate(gfile_name,win))
			{
				LitosFile * file = litos_app_window_open(win, gfile);
			}

			g_free(gfile_name);
		}
	}

	gtk_window_destroy (GTK_WINDOW (dialog));
}

static void
esc_activated(GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));
	LitosAppWindow *win = LITOS_APP_WINDOW(window);
	Esc(win);
}

static void
open_activated(GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWidget *dialog;

	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));
	LitosAppWindow *win = LITOS_APP_WINDOW(window);

	dialog = gtk_file_chooser_dialog_new ("Open File",
		NULL,
		GTK_FILE_CHOOSER_ACTION_OPEN,
		"Cancel",
		GTK_RESPONSE_CANCEL,
		"Open",
		GTK_RESPONSE_ACCEPT,
		NULL);

	if (litos_app_window_get_array_len(win) != 0)
	{
		GError *error = NULL;
		LitosFile *file = litos_app_window_current_file(win);

		if (litos_file_get_gfile(file) != NULL)
		{
			if (!gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), g_file_get_parent(litos_file_get_gfile(file)), &error))
				litos_app_error_dialog(GTK_WINDOW(win), error, "Current Folder");
		}
	}

	gtk_window_set_transient_for(GTK_WINDOW(dialog), window);

	gtk_widget_show(dialog);

	g_signal_connect (dialog, "response", G_CALLBACK (open_cb), window);
}

static void
open_tmpl_cb (GtkWidget *dialog, gint response, gpointer window)
{
	if (response == GTK_RESPONSE_ACCEPT)
	{
		GFile *gfile = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));

		LitosAppWindow *win = LITOS_APP_WINDOW(window);

		if (gfile != NULL) /*gfile could be NULL in maybe some (unknown) edge case so you check gfile != NULL just in case so that it doesn't cause some error/crash */
		{
			LitosFile * file = litos_app_window_open(win, gfile);
			litos_file_reset_gfile(file); /*we don't want to save over the template but on a new file */
		}
	}

	gtk_window_destroy (GTK_WINDOW (dialog));
}

static void
open_tmpl (GSimpleAction *action,
                       GVariant      *parameter,
                       gpointer       app)
{
	GtkWidget *dialog;

	GError *error = NULL;

	GFile *gfile = g_file_new_for_path(g_get_user_special_dir (G_USER_DIRECTORY_TEMPLATES));

	GtkWindow *win = gtk_application_get_active_window (GTK_APPLICATION (app));

	dialog = gtk_file_chooser_dialog_new ("Open File",
		NULL,
		GTK_FILE_CHOOSER_ACTION_OPEN,
		"Cancel",
		GTK_RESPONSE_CANCEL,
		"Open",
		GTK_RESPONSE_ACCEPT,
		NULL);

	if (!gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), gfile, &error))
		litos_app_error_dialog(GTK_WINDOW(win), error, "Templates");

	gtk_window_set_transient_for(GTK_WINDOW(dialog), win);

	gtk_widget_show(dialog);

	g_signal_connect (dialog, "response", G_CALLBACK (open_tmpl_cb), win);
}

static void
save(GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));
	LitosAppWindow *win = LITOS_APP_WINDOW(window);

	LitosFile *file = litos_app_window_current_file(win);
	litos_app_window_save(win, file);
}

static void
save_as_dialog (GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *win = gtk_application_get_active_window (GTK_APPLICATION (app));
	litos_app_window_save_as(LITOS_APP_WINDOW(win));
}

static void
preferences_activated (GSimpleAction *action,
                       GVariant      *parameter,
                       gpointer       app)
{
	LitosAppPrefs *prefs;
	GtkWindow *win;

	win = gtk_application_get_active_window (GTK_APPLICATION (app));
	prefs = litos_app_prefs_new (LITOS_APP_WINDOW (win));
	gtk_window_present (GTK_WINDOW (prefs));
}

static void
close_activated (GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));

	LitosAppWindow *win = LITOS_APP_WINDOW(window);

	litos_app_window_remove_child(win);
}

static void
quit_activated (GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));
	LitosAppWindow *win = LITOS_APP_WINDOW(window);

	litos_app_window_quit(window, win);
}

static void
new_file (GSimpleAction *action,
                GVariant *parameter,
                gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));
	LitosAppWindow *win = LITOS_APP_WINDOW(window);
	LitosFile * file = litos_app_window_new_file(win);
}

/* Called when Ctrl+B, Ctrl+i, etc is toggled */
static void
insertChar (GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));

	LitosAppWindow *win = LITOS_APP_WINDOW(window);

	LitosFile *file = litos_app_window_current_file(win);

	GtkTextBuffer *buffer = litos_file_get_buffer(file);

	gchar *insertChar;

	g_variant_get (parameter, "s", &insertChar);

	gtk_text_buffer_insert_at_cursor (buffer, insertChar, (gint)strlen(insertChar));

	g_free(insertChar);
}

/* Called when Ctrl+B, Ctrl+i, etc is toggled */
static void
insertHtmlTags (GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));

	LitosAppWindow *win = LITOS_APP_WINDOW(window);

	LitosFile *file = litos_app_window_current_file(win);

	GtkTextBuffer *buffer = litos_file_get_buffer(file);

	char *string = NULL;

	char replaceString[650] = { 0 };

	gchar *tag;

	g_variant_get (parameter, "s", &tag);

	GtkTextIter start_sel, end_sel;

	if (gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel))
	{
		string = gtk_text_buffer_get_text (buffer,
							&start_sel,
							&end_sel,
							FALSE);

		snprintf(replaceString, sizeof(replaceString), tag, string);
		gtk_text_buffer_delete (buffer, &start_sel, &end_sel);
		gtk_text_buffer_insert (buffer, &start_sel, replaceString, (gint)strlen(replaceString));
	}

	else
	{
		snprintf(replaceString, sizeof(replaceString), "</%c>", tag[1]);	/*e.g. enters </l> if Ctrl-l is pressed*/
		gtk_text_buffer_insert_at_cursor (buffer, replaceString,(gint)strlen(replaceString));
	}

	g_free(tag);
}

static void
find_selection (GSimpleAction *action, GVariant *parameter, gpointer app)
{
	GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (app));

	LitosAppWindow *win = LITOS_APP_WINDOW(window);	

	ctrl_f(win);
}

void setAccels (GApplication *app)
{
	long unsigned int i;

	/* map actions to callbacks */
	const GActionEntry app_entries[] = {
		{"preferences", preferences_activated, NULL, NULL, NULL },
		{"insert_html", insertHtmlTags, "s", NULL, NULL, {0,0,0}},
		{"insert_char", insertChar, "s", NULL, NULL, {0,0,0}},
		{"open", open_activated, NULL, NULL, NULL},
		{"esc", esc_activated, NULL, NULL, NULL},
		{"open_tmpl", open_tmpl, NULL, NULL, NULL},
		{"new", new_file, NULL, NULL, NULL},
		{"save", save, NULL, NULL, NULL, {0,0,0}},
		{"save_as", save_as_dialog, NULL, NULL, NULL, {0,0,0}},
		{"close", close_activated, NULL, NULL, NULL},
		{"find", find_selection, NULL, NULL, NULL, {0,0,0}},
		{"quit", quit_activated, NULL, NULL, NULL }
	};

	/* define keyboard accelerators*/
	struct {
	  const gchar *action;
	  const gchar *accels[2];
	} action_accels[] = {
		{ "app.open", { "<Control>o", NULL} },
		{ "app.new", { "<Control>n", NULL} },
		{ "app.save", { "<Control>s", NULL} },
		{ "app.save_as", { "<Shift><Control>s", NULL} },
		{ "app.close", { "<Control>w", NULL} },
		{ "app.quit", { "<Control>q", NULL} },
		{ "app.esc", { "Escape", NULL} },
		{ "app.find", { "<Control>f", NULL} },
		{ "app.insert_html(\"<b>%s</b>\")", { "<Control>b", NULL} },
		{ "app.insert_html(\"<i>%s</i>\")", { "<Control>i", NULL} },
		{ "app.insert_html(\"<h2>%s</h2>\")", { "<Control>2", NULL} },
		{ "app.insert_html(\"<h3>%s</h3>\")", { "<Control>3", NULL} },
		{ "app.insert_html('<a href=\"this.html\">%s</a>')", { "<Control>h", NULL} },
		{ "app.insert_html(\"<p>%s</p>\")", { "<Control>p", NULL} },
		{ "app.insert_html(\"<li><p>%s</p></li>\")", { "<Control>l", NULL} },
		{ "app.insert_html(\"<sup>%s</sup>\")", { "<Control>e", NULL} },
		{ "app.insert_html(\"<sub>%s</sub>\")", { "<Control>u", NULL} },
		{ "app.insert_char('<div class=\"eq\">\n<p>this</p>\n</div>\')", { "<Control>g", NULL} },
		{ "app.insert_char(\"−\")", { "<Control>m", NULL} },
		{ "app.insert_char(\"⋅\")", { "<Control>d", NULL} },
		{ "app.insert_char(\"⟶⟼⇒\")", { "<Control>y", NULL} },
		{ "app.insert_char(\"⇌⟵⇐\")", { "<Control><Shift>y", NULL} },
		{ "app.insert_char(\"<br>\")", { "<Control>r", NULL} },
		{ "app.insert_char(\"&emsp;■□\")", { "<Control>t", NULL} },
	};

	g_action_map_add_action_entries(G_ACTION_MAP(app), app_entries, G_N_ELEMENTS(app_entries), app);

	for (i = 0; i < G_N_ELEMENTS(action_accels); i++)
		gtk_application_set_accels_for_action(GTK_APPLICATION(app), action_accels[i].action, action_accels[i].accels);
}