1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * Copyright (C) 2009 Shaun McCance <shaunm@gnome.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Shaun McCance <shaunm@gnome.org>
22 #include <webkit2/webkit2.h>
24 #include "yelp-settings.h"
26 static YelpSettings
*settings
;
27 static WebKitSettings
*websettings
;
28 static GtkWidget
*webview
;
29 static GtkWidget
*color_table
;
30 static GtkWidget
*color_buttons
[YELP_SETTINGS_NUM_COLORS
];
31 static GtkWidget
*icon_table
;
32 static GtkWidget
*icon_choosers
[YELP_SETTINGS_NUM_ICONS
];
33 static GtkWidget
*icon_images
[YELP_SETTINGS_NUM_ICONS
];
34 static GtkWidget
*font_choosers
[YELP_SETTINGS_NUM_FONTS
];
36 #define FORMAT_TMPL "<html><head>" \
38 "body { background-color: %s; color: %s; }" \
40 " margin: 0.5em; padding: 0.2em;" \
41 " border: solid 3px %s;" \
45 "<pre>SOME\nMONOSPACE\nTEXT</pre>" \
47 "<tr><td>YELP_SETTINGS_COLOR_BASE:</td><td>%s</td></tr>" \
48 "<tr style='color: %s'><td>YELP_SETTINGS_COLOR_TEXT:</td><td>%s</td></tr>" \
49 "<tr style='color: %s'><td>YELP_SETTINGS_COLOR_TEXT_LIGHT:</td><td>%s</td></tr>" \
50 "<tr style='color: %s'><td>YELP_SETTINGS_COLOR_LINK:</td><td>%s</td></tr>" \
51 "<tr style='color: %s'><td>YELP_SETTINGS_COLOR_LINK_VISITED:</td><td>%s</td></tr>" \
53 "<div style='background-color:%s;border-color:%s'>" \
54 "<table><tr><td>YELP_SETTINGS_COLOR_GRAY_BASE:</td><td>%s</td></tr>" \
55 "<tr><td>YELP_SETTINGS_COLOR_GRAY_BORDER:</td><td>%s</tr></table></div>" \
56 "<div style='background-color:%s;border-color:%s'>" \
57 "<table><tr><td>YELP_SETTINGS_COLOR_BLUE_BASE:</td><td>%s</td></tr>" \
58 "<tr><td>YELP_SETTINGS_COLOR_BLUE_BORDER:</td><td>%s</tr></table></div>" \
59 "<div style='background-color:%s;border-color:%s'>" \
60 "<table><tr><td>YELP_SETTINGS_COLOR_RED_BASE:</td><td>%s</td></tr>" \
61 "<tr><td>YELP_SETTINGS_COLOR_RED_BORDER:</td><td>%s</tr></table></div>" \
62 "<div style='background-color:%s;border-color:%s'>" \
63 "<table><tr><td>YELP_SETTINGS_COLOR_YELLOW_BASE:</td><td>%s</td></tr>" \
64 "<tr><td>YELP_SETTINGS_COLOR_YELLOW_BORDER:</td><td>%s</tr></table></div>" \
68 colors_changed (YelpSettings
*unused_settings
, gpointer user_data
)
70 gchar
**colors
= yelp_settings_get_colors (settings
);
72 page
= g_strdup_printf (FORMAT_TMPL
,
73 colors
[YELP_SETTINGS_COLOR_BASE
], colors
[YELP_SETTINGS_COLOR_TEXT
],
74 colors
[YELP_SETTINGS_COLOR_BASE
], colors
[YELP_SETTINGS_COLOR_BASE
],
75 colors
[YELP_SETTINGS_COLOR_TEXT
], colors
[YELP_SETTINGS_COLOR_TEXT
],
76 colors
[YELP_SETTINGS_COLOR_TEXT_LIGHT
], colors
[YELP_SETTINGS_COLOR_TEXT_LIGHT
],
77 colors
[YELP_SETTINGS_COLOR_LINK
], colors
[YELP_SETTINGS_COLOR_LINK
],
78 colors
[YELP_SETTINGS_COLOR_LINK_VISITED
], colors
[YELP_SETTINGS_COLOR_LINK_VISITED
],
79 colors
[YELP_SETTINGS_COLOR_GRAY_BASE
], colors
[YELP_SETTINGS_COLOR_GRAY_BORDER
],
80 colors
[YELP_SETTINGS_COLOR_GRAY_BASE
], colors
[YELP_SETTINGS_COLOR_GRAY_BORDER
],
81 colors
[YELP_SETTINGS_COLOR_BLUE_BASE
], colors
[YELP_SETTINGS_COLOR_BLUE_BORDER
],
82 colors
[YELP_SETTINGS_COLOR_BLUE_BASE
], colors
[YELP_SETTINGS_COLOR_BLUE_BORDER
],
83 colors
[YELP_SETTINGS_COLOR_RED_BASE
], colors
[YELP_SETTINGS_COLOR_RED_BORDER
],
84 colors
[YELP_SETTINGS_COLOR_RED_BASE
], colors
[YELP_SETTINGS_COLOR_RED_BORDER
],
85 colors
[YELP_SETTINGS_COLOR_YELLOW_BASE
], colors
[YELP_SETTINGS_COLOR_YELLOW_BORDER
],
86 colors
[YELP_SETTINGS_COLOR_YELLOW_BASE
], colors
[YELP_SETTINGS_COLOR_YELLOW_BORDER
]);
87 webkit_web_view_load_html (WEBKIT_WEB_VIEW (webview
),
95 icons_changed (YelpSettings
*unused_settings
, gpointer user_data
)
98 for (i
= 0; i
< YELP_SETTINGS_NUM_ICONS
; i
++) {
99 gchar
*filename
= yelp_settings_get_icon (settings
, i
);
100 if (filename
!= NULL
) {
101 gtk_image_set_from_file (GTK_IMAGE (icon_images
[i
]), filename
);
108 fonts_changed (YelpSettings
*unused_settings
, gpointer user_data
)
110 g_object_set (websettings
,
111 "default-font-family", yelp_settings_get_font_family (settings
, YELP_SETTINGS_FONT_VARIABLE
),
112 "default-font-size", yelp_settings_get_font_size (settings
, YELP_SETTINGS_FONT_VARIABLE
),
113 "monospace-font-family", yelp_settings_get_font_family (settings
, YELP_SETTINGS_FONT_FIXED
),
114 "default-monospace-font-size", yelp_settings_get_font_size (settings
, YELP_SETTINGS_FONT_FIXED
),
119 color_set (GtkColorButton
*button
,
125 for (i
= 0; i
< YELP_SETTINGS_NUM_COLORS
; i
++)
126 if ((gpointer
) color_buttons
[i
] == (gpointer
) button
)
128 g_return_if_fail (i
< YELP_SETTINGS_NUM_COLORS
);
130 gtk_color_chooser_get_rgba (GTK_COLOR_CHOOSER (button
), &rgba
);
131 g_snprintf (str
, 8, "#%02X%02X%02X",
132 (guint
)(0.5 + CLAMP (rgba
.red
, 0., 1.) * 255),
133 (guint
)(0.5 + CLAMP (rgba
.green
, 0., 1.) * 255),
134 (guint
)(0.5 + CLAMP (rgba
.blue
, 0., 1.) * 255));
135 yelp_settings_set_colors (settings
, i
, str
, -1);
139 font_set (GtkFontButton
*button
,
144 for (i
= 0; i
< YELP_SETTINGS_NUM_FONTS
; i
++)
145 if ((gpointer
) font_choosers
[i
] == (gpointer
) button
)
147 g_return_if_fail (i
< YELP_SETTINGS_NUM_FONTS
);
149 yelp_settings_set_fonts (settings
, i
, gtk_font_button_get_font_name (button
), -1);
153 icon_file_set (GtkFileChooserButton
*button
,
158 for (i
= 0; i
< YELP_SETTINGS_NUM_ICONS
; i
++)
159 if ((gpointer
) icon_choosers
[i
] == (gpointer
) button
)
161 g_return_if_fail (i
< YELP_SETTINGS_NUM_ICONS
);
163 filename
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (button
));
164 yelp_settings_set_icons (settings
, i
, filename
, -1);
169 use_gtk_settings_toggled (GtkToggleButton
*button
,
172 if (gtk_toggle_button_get_active (button
)) {
173 g_object_set (settings
, "gtk-settings", gtk_settings_get_default (), NULL
);
174 gtk_widget_set_sensitive (color_table
, FALSE
);
177 g_object_set (settings
, "gtk-settings", NULL
, NULL
);
178 gtk_widget_set_sensitive (color_table
, TRUE
);
183 use_gtk_icon_theme_toggled (GtkToggleButton
*button
,
186 if (gtk_toggle_button_get_active (button
)) {
187 g_object_set (settings
, "gtk-icon-theme", gtk_icon_theme_get_default (), NULL
);
188 gtk_widget_set_sensitive (icon_table
, FALSE
);
191 g_object_set (settings
, "gtk-icon-theme", NULL
, NULL
);
192 gtk_widget_set_sensitive (icon_table
, TRUE
);
197 use_small_icons_toggled (GtkToggleButton
*button
,
200 yelp_settings_set_icon_size (settings
, gtk_toggle_button_get_active (button
) ? 24 : 48);
204 main (int argc
, char **argv
)
206 GtkWidget
*window
, *hbox
, *vbox
, *widget
, *scroll
, *table
;
209 gtk_init (&argc
, &argv
);
211 settings
= yelp_settings_get_default ();
213 window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
214 gtk_window_set_default_size (GTK_WINDOW (window
), 1024, 600);
215 g_signal_connect (window
, "destroy", G_CALLBACK (gtk_main_quit
), NULL
);
217 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 12);
218 g_object_set (hbox
, "border-width", 6, NULL
);
219 gtk_container_add (GTK_CONTAINER (window
), hbox
);
221 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 6);
222 gtk_box_pack_start (GTK_BOX (hbox
), vbox
, TRUE
, TRUE
, 0);
224 widget
= gtk_check_button_new_with_label ("Use GtkSettings");
225 g_object_set (widget
, "active", TRUE
, NULL
);
226 g_signal_connect (widget
, "toggled", G_CALLBACK (use_gtk_settings_toggled
), NULL
);
227 gtk_box_pack_start (GTK_BOX (vbox
), widget
, FALSE
, FALSE
, 0);
229 color_table
= gtk_grid_new ();
230 gtk_widget_set_sensitive (color_table
, FALSE
);
231 gtk_box_pack_start (GTK_BOX (vbox
), color_table
, FALSE
, FALSE
, 0);
233 for (i
= 0; i
< YELP_SETTINGS_NUM_COLORS
; i
++) {
234 color_buttons
[i
] = gtk_color_button_new ();
235 g_signal_connect (color_buttons
[i
], "color-set", G_CALLBACK (color_set
), NULL
);
237 gtk_grid_attach (GTK_GRID (color_table
), color_buttons
[i
],
241 gtk_grid_attach (GTK_GRID (color_table
), color_buttons
[i
],
249 scroll
= gtk_scrolled_window_new (NULL
, NULL
);
250 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll
), GTK_SHADOW_IN
);
251 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll
),
252 GTK_POLICY_AUTOMATIC
,
253 GTK_POLICY_AUTOMATIC
);
254 gtk_box_pack_start (GTK_BOX (vbox
), scroll
, TRUE
, TRUE
, 0);
256 webview
= webkit_web_view_new ();
257 websettings
= webkit_web_view_get_settings (WEBKIT_WEB_VIEW (webview
));
258 gtk_container_add (GTK_CONTAINER (scroll
), webview
);
260 vbox
= gtk_box_new (GTK_ORIENTATION_VERTICAL
, 6);
261 gtk_box_pack_start (GTK_BOX (hbox
), vbox
, TRUE
, TRUE
, 0);
263 table
= gtk_grid_new ();
264 gtk_box_pack_start (GTK_BOX (vbox
), table
, FALSE
, FALSE
, 0);
266 widget
= gtk_label_new ("Variable");
267 g_object_set (widget
, "xalign", 0.0, NULL
);
268 gtk_grid_attach (GTK_GRID (table
), widget
, 0, 0, 1, 1);
269 font_choosers
[YELP_SETTINGS_FONT_VARIABLE
] = gtk_font_button_new ();
270 g_signal_connect (font_choosers
[YELP_SETTINGS_FONT_VARIABLE
], "notify::font-name",
271 G_CALLBACK (font_set
), NULL
);
272 gtk_font_button_set_font_name (GTK_FONT_BUTTON (font_choosers
[YELP_SETTINGS_FONT_VARIABLE
]),
274 gtk_grid_attach (GTK_GRID (table
), font_choosers
[YELP_SETTINGS_FONT_VARIABLE
],
277 widget
= gtk_label_new ("Fixed");
278 g_object_set (widget
, "xalign", 0.0, NULL
);
279 gtk_grid_attach (GTK_GRID (table
), widget
, 0, 1, 1, 1);
280 font_choosers
[YELP_SETTINGS_FONT_FIXED
] = gtk_font_button_new ();
281 g_signal_connect (font_choosers
[YELP_SETTINGS_FONT_FIXED
], "notify::font-name",
282 G_CALLBACK (font_set
), NULL
);
283 gtk_font_button_set_font_name (GTK_FONT_BUTTON (font_choosers
[YELP_SETTINGS_FONT_FIXED
]),
285 gtk_grid_attach (GTK_GRID (table
), font_choosers
[YELP_SETTINGS_FONT_FIXED
],
288 widget
= gtk_check_button_new_with_label ("Use GtkIconTheme");
289 g_object_set (widget
, "active", TRUE
, NULL
);
290 g_signal_connect (widget
, "toggled", G_CALLBACK (use_gtk_icon_theme_toggled
), NULL
);
291 gtk_box_pack_start (GTK_BOX (vbox
), widget
, FALSE
, FALSE
, 0);
293 widget
= gtk_check_button_new_with_label ("Use small icons");
294 g_signal_connect (widget
, "toggled", G_CALLBACK (use_small_icons_toggled
), NULL
);
295 gtk_box_pack_start (GTK_BOX (vbox
), widget
, FALSE
, FALSE
, 0);
297 icon_table
= gtk_grid_new ();
298 gtk_widget_set_sensitive (icon_table
, FALSE
);
299 gtk_box_pack_start (GTK_BOX (vbox
), icon_table
, FALSE
, FALSE
, 0);
301 for (i
= 0; i
< YELP_SETTINGS_NUM_ICONS
; i
++) {
302 const gchar
*labels
[YELP_SETTINGS_NUM_ICONS
] =
303 {"BUG", "IMPORTANT", "NOTE", "TIP", "WARNING"};
304 widget
= gtk_label_new (labels
[i
]);
305 g_object_set (widget
, "xalign", 0.0, NULL
);
306 gtk_grid_attach (GTK_GRID (icon_table
), widget
, 0, i
, 1, 1);
307 icon_choosers
[i
] = gtk_file_chooser_button_new (labels
[i
], GTK_FILE_CHOOSER_ACTION_OPEN
);
308 gtk_grid_attach (GTK_GRID (icon_table
), icon_choosers
[i
],
310 g_signal_connect (icon_choosers
[i
], "file-set", G_CALLBACK (icon_file_set
), NULL
);
313 hbox
= gtk_box_new (GTK_ORIENTATION_HORIZONTAL
, 6);
314 gtk_box_set_homogeneous (GTK_BOX (hbox
), TRUE
);
315 gtk_box_pack_start (GTK_BOX (vbox
), hbox
, FALSE
, FALSE
, 0);
316 for (i
= 0; i
< YELP_SETTINGS_NUM_ICONS
; i
++) {
317 icon_images
[i
] = gtk_image_new ();
318 gtk_box_pack_start (GTK_BOX (hbox
), icon_images
[i
], FALSE
, FALSE
, 0);
321 g_signal_connect (settings
, "fonts-changed", G_CALLBACK (fonts_changed
), NULL
);
322 fonts_changed (settings
, NULL
);
323 g_signal_connect (settings
, "colors-changed", G_CALLBACK (colors_changed
), NULL
);
324 colors_changed (settings
, NULL
);
325 g_signal_connect (settings
, "icons-changed", G_CALLBACK (icons_changed
), NULL
);
326 icons_changed (settings
, NULL
);
328 gtk_widget_show_all (GTK_WIDGET (window
));