summaryrefslogtreecommitdiff
path: root/gui/help.c
blob: 553f0032dce5fc41e56ff56a5432a66026794451 (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
/*
 * gui/help.c - Online help
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * 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.
 */

#include <stdbool.h>

#include <gtk/gtk.h>

#ifdef USE_WEBKIT
#include <webkit2/webkit2.h>
#endif

#include "gui/help.h"


static GtkWidget *window = NULL;
static bool visible;


static void destroy_help(GtkWidget *object, gpointer user_data)
{
	gtk_widget_destroy(window);
	window = NULL;
}


static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
    gpointer data)
{
	switch (event->keyval) {
	case GDK_KEY_h:
	case GDK_KEY_Help:
	case GDK_KEY_q:
	case GDK_KEY_Escape:
		gtk_widget_hide(window);
		visible = 0;
		break;
	}
	return TRUE;
}


#ifdef USE_WEBKIT

static GtkWidget *help_content(void)
{
	GtkWidget *view;
	WebKitSettings *settings;

	view = webkit_web_view_new();

	settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(view));
	webkit_settings_set_default_font_size(settings, 10);

	webkit_web_view_load_html(WEBKIT_WEB_VIEW(view),
#include "../help.inc"
	, NULL);

	return view;
}

#else /* USE_WEBKIT */

static GtkWidget *help_content(void)
{
	GtkWidget *scroll, *label;

	scroll = gtk_scrolled_window_new(NULL, NULL);
	label = gtk_label_new(NULL);

	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
	    GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

	gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
	gtk_label_set_selectable(GTK_LABEL(label), TRUE);
	gtk_label_set_markup(GTK_LABEL(label),
#include "../help.inc"
	    );

	gtk_container_add(GTK_CONTAINER(scroll), label);

	return scroll;
}

#endif /* !USE_WEBKIT */


static void new_help_window(void)
{
	GtkWidget *content;

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	content = help_content();

	gtk_container_add(GTK_CONTAINER(window), content);
	gtk_window_set_default_size(GTK_WINDOW(window), 480, 360);
	gtk_widget_show_all(window);

	gtk_widget_set_can_focus(content, TRUE);
	gtk_widget_add_events(content, GDK_KEY_PRESS_MASK);

	g_signal_connect(G_OBJECT(content), "key_press_event",
	    G_CALLBACK(key_press_event), NULL);
	g_signal_connect(window, "destroy", G_CALLBACK(destroy_help), NULL);

	gtk_widget_show_all(window);
}


void help(void)
{
	if (!window) {
		new_help_window();
		visible = 1;
	} else {
		visible = !visible;
		if (visible)
			gtk_widget_show(window);
		else
			gtk_widget_hide(window);
	}
}