1 /**************************************************************************/
2 /*  Klavaro - a flexible touch typing tutor                               */
3 /*  Copyright (C) from 2005 until 2008 Felipe Castro                      */
4 /*  Copyright (C) from 2009 until 2019 The Free Software Foundation       */
5 /*                                                                        */
6 /*  This program is free software, licensed under the terms of the GNU    */
7 /*  General Public License as published by the Free Software Foundation,  */
8 /*  either version 3 of the License, or (at your option) any later        */
9 /*  version. You should have received a copy of the GNU General Public    */
10 /*  License along with this program. If not,                              */
11 /*  see <https://www.gnu.org/licenses/>.                                  */
12 /**************************************************************************/
13 
14 /*
15  * Callbacks for widgets events
16  */
17 #include <string.h>
18 #include <stdlib.h>
19 #include <glib.h>
20 #include <glib/gstdio.h>
21 #include <gtk/gtk.h>
22 #include <gtkdatabox.h>
23 
24 #include "auxiliar.h"
25 #include "main.h"
26 #include "translation.h"
27 #include "keyboard.h"
28 #include "tutor.h"
29 #include "cursor.h"
30 #include "plot.h"
31 #include "basic.h"
32 #include "velocity.h"
33 #include "fluidness.h"
34 #include "accuracy.h"
35 #include "top10.h"
36 #include "callbacks.h"
37 
38 /*
39  * Variables
40  */
41 GtkClipboard *clipboard = NULL;
42 GtkClipboard *clipboard_2 = NULL;
43 
44 extern gchar *KEYB_CUSTOM;
45 extern gchar *KEYB_EDIT;
46 extern gchar *OTHER_DEFAULT;
47 
48 static gboolean callbacks_shield = FALSE;
49 static gboolean mesg_drag_drop = FALSE;
50 static gchar *other_renaming_undo = NULL;
51 
52 void
callbacks_shield_set(gboolean state)53 callbacks_shield_set (gboolean state)
54 {
55 	callbacks_shield = state;
56 }
57 
58 /**********************************************************************
59  * 1 - Main menu commands
60  **********************************************************************/
61 G_MODULE_EXPORT void
on_button_intro_clicked(GtkButton * button,gpointer user_data)62 on_button_intro_clicked (GtkButton *button, gpointer user_data)
63 {
64 	keyb_mode_intro ();
65 }
66 
67 G_MODULE_EXPORT void
on_button_basic_clicked(GtkButton * button,gpointer user_data)68 on_button_basic_clicked (GtkButton *button, gpointer user_data)
69 {
70 	window_save ("main");
71 	tutor_init (TT_BASIC);
72 }
73 
74 G_MODULE_EXPORT void
on_button_adapt_clicked(GtkButton * button,gpointer user_data)75 on_button_adapt_clicked (GtkButton *button, gpointer user_data)
76 {
77 	window_save ("main");
78 	tutor_init (TT_ADAPT);
79 }
80 
81 G_MODULE_EXPORT void
on_button_velo_clicked(GtkButton * button,gpointer user_data)82 on_button_velo_clicked (GtkButton *button, gpointer user_data)
83 {
84 	window_save ("main");
85 	tutor_init (TT_VELO);
86 }
87 
88 G_MODULE_EXPORT void
on_button_fluid_clicked(GtkButton * button,gpointer user_data)89 on_button_fluid_clicked (GtkButton *button, gpointer user_data)
90 {
91 	window_save ("main");
92 	tutor_init (TT_FLUID);
93 }
94 
95 G_MODULE_EXPORT void
on_button_help_clicked(GtkButton * button,gpointer user_data)96 on_button_help_clicked (GtkButton *button, gpointer user_data)
97 {
98 	gtk_widget_show (get_wg ("dialog_info"));
99 }
100 
101 G_MODULE_EXPORT void
on_button_about_clicked(GtkButton * button,gpointer user_data)102 on_button_about_clicked (GtkButton *button, gpointer user_data)
103 {
104 	gtk_widget_show (get_wg ("aboutdialog_klavaro"));
105 }
106 
107 G_MODULE_EXPORT void
on_aboutdialog_klavaro_close(GtkDialog * dialog,gint response_id,gpointer user_data)108 on_aboutdialog_klavaro_close (GtkDialog *dialog, gint response_id, gpointer user_data)
109 {
110 	gtk_widget_hide (GTK_WIDGET (dialog));
111 }
112 
113 G_MODULE_EXPORT void
on_aboutdialog_klavaro_response(GtkDialog * dialog,gint response_id,gpointer user_data)114 on_aboutdialog_klavaro_response (GtkDialog *dialog, gint response_id, gpointer user_data)
115 {
116 	gtk_widget_hide (GTK_WIDGET (dialog));
117 }
118 
119 G_MODULE_EXPORT void
on_checkbutton_speech_toggled(GtkToggleButton * togglebutton,gpointer user_data)120 on_checkbutton_speech_toggled (GtkToggleButton * togglebutton, gpointer user_data)
121 {
122 	if (callbacks_shield)
123 		return;
124 	main_preferences_set_boolean ("interface", "speech",
125 		       	gtk_toggle_button_get_active (togglebutton));
126 }
127 
128 G_MODULE_EXPORT void
on_combobox_language_changed(GtkComboBox * cmb,gpointer user_data)129 on_combobox_language_changed (GtkComboBox *cmb, gpointer user_data)
130 {
131 	gchar *tmp;
132 	gint active;
133 
134 	if (callbacks_shield)
135 		return;
136 	tmp = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (cmb));
137 	trans_change_language (tmp);
138 	g_free (tmp);
139 
140 	callbacks_shield_set (TRUE);
141 	active = gtk_combo_box_get_active (cmb);
142 	gtk_combo_box_set_active (GTK_COMBO_BOX (get_wg ("combobox_top10_language")), active);
143 	callbacks_shield_set (FALSE);
144 
145 	on_combobox_stat_module_changed (GTK_COMBO_BOX (get_wg ("combobox_stat_module")), (gpointer) -1);
146 }
147 
148 G_MODULE_EXPORT void
on_combobox_kbd_country_changed(GtkComboBox * cmb,gpointer user_data)149 on_combobox_kbd_country_changed (GtkComboBox *cmb, gpointer user_data)
150 {
151 	if (callbacks_shield)
152 		return;
153 	if (keyb_get_name ()) accur_close ();
154 	keyb_set_combo_kbd_variant ("combobox_kbd_country", "combobox_kbd_variant");
155 	accur_init ();
156 	main_preferences_set_string ("tutor", "keyboard", keyb_get_name ());
157 
158 	on_combobox_stat_module_changed (GTK_COMBO_BOX (get_wg ("combobox_stat_module")), (gpointer) -1);
159 
160 	//g_message ("kbd_country_changed: %s", keyb_get_name ());
161 }
162 
163 G_MODULE_EXPORT void
on_combobox_kbd_variant_changed(GtkComboBox * cmb,gpointer user_data)164 on_combobox_kbd_variant_changed (GtkComboBox *cmb, gpointer user_data)
165 {
166 	gchar *tmp;
167 
168 	if (callbacks_shield)
169 		return;
170 
171 	tmp = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (cmb));
172 	if (g_str_equal (tmp, KEYB_EDIT))
173 		keyb_mode_edit ();
174 	else
175 	{
176 		if (keyb_get_name ()) accur_close ();
177 		keyb_update_from_variant ("combobox_kbd_country", "combobox_kbd_variant");
178 		accur_init ();
179 		main_preferences_set_string ("tutor", "keyboard", keyb_get_name ());
180 	}
181 	g_free (tmp);
182 
183 	on_combobox_stat_module_changed (GTK_COMBO_BOX (get_wg ("combobox_stat_module")), (gpointer) -1);
184 
185 	//g_message ("kbd_variant_changed: %s", keyb_get_name ());
186 }
187 
188 G_MODULE_EXPORT void
on_window_main_destroy(GtkWidget * obj,gpointer data)189 on_window_main_destroy (GtkWidget *obj, gpointer data)
190 {
191 	if (callbacks_shield)
192 		return;
193 
194 	window_save ("main");
195 	main_preferences_set_boolean ("interface", "autostart", FALSE);
196 
197 	main_window_pass_away ();
198 }
199 
200 /**********************************************************************
201  * 2 - Little nohelp-dialog
202  **********************************************************************/
203 G_MODULE_EXPORT void
on_button_info_return_clicked(GtkButton * but,gpointer user_data)204 on_button_info_return_clicked (GtkButton *but, gpointer user_data)
205 {
206 	gtk_widget_hide (get_wg ("dialog_info"));
207 }
208 
209 /**********************************************************************
210  * 3 - Tutor window
211  **********************************************************************/
212 
213 static void
cb_color_tag(gchar * tag,gchar * fgcolor,gchar * bgcolor,GtkTextBuffer * tbuf)214 cb_color_tag(gchar *tag, gchar *fgcolor, gchar *bgcolor, GtkTextBuffer *tbuf)
215 {
216 	gchar *tmp;
217 	gchar *color_fg;
218 	gchar *color_bg;
219 
220 	tmp = g_strconcat (tag, "_fg", NULL);
221 	if (main_preferences_exist ("colors", tmp))
222 		color_fg = main_preferences_get_string ("colors", tmp);
223 	else
224 	{
225 		color_fg = g_strdup (fgcolor);
226 		main_preferences_set_string ("colors", tmp, color_fg);
227 	}
228 	if (main_altcolor_get_boolean ("colors", "altcolor"))
229 	{
230 		g_free (color_fg);
231 		color_fg = main_altcolor_get_string ("colors", tmp);
232 	}
233 	g_free (tmp);
234 
235 	tmp = g_strconcat (tag, "_bg", NULL);
236 	if (main_preferences_exist ("colors", tmp))
237 		color_bg = main_preferences_get_string ("colors", tmp);
238 	else
239 	{
240 		color_bg = g_strdup (bgcolor);
241 		main_preferences_set_string ("colors", tmp, color_bg);
242 	}
243 	if (main_altcolor_get_boolean ("colors", "altcolor"))
244 	{
245 		g_free (color_bg);
246 		color_bg = main_altcolor_get_string ("colors", tmp);
247 	}
248 	g_free (tmp);
249 
250 	gtk_text_buffer_create_tag (tbuf, tag,
251 		       	"foreground", color_fg,
252 		       	"background", color_bg,
253 		       	"underline", PANGO_UNDERLINE_NONE, NULL);
254 	g_free (color_fg);
255 	g_free (color_bg);
256 }
257 
258 G_MODULE_EXPORT void
on_text_tutor_realize(GtkWidget * widget,gpointer user_data)259 on_text_tutor_realize (GtkWidget * widget, gpointer user_data)
260 {
261 	gboolean beep;
262 	gchar *tmp;
263 	gchar *tmp_font;
264 	gchar *search;
265 	gchar *color_main_fg;
266 	gchar *color_main_bg;
267 	GtkWidget *wg;
268 	GtkTextBuffer *buf;
269 	PangoFontDescription *font_desc;
270 	PangoStyle pg_style;
271 	PangoWeight pg_weight;
272 	gint pg_size;
273 	gchar *pg_family;
274 	GdkRGBA color;
275 	GtkStyleContext *sc;
276 	static GtkCssProvider *css;
277 
278 	buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget));
279 
280 	/* Set main color for tutor text (char_untouched & CIA)
281 	 */
282 	if (main_preferences_exist ("colors", "char_untouched_bg"))
283 		color_main_bg = main_preferences_get_string ("colors", "char_untouched_bg");
284 	else
285 		color_main_bg = g_strdup (TUTOR_CREAM);
286 	if (main_preferences_exist ("colors", "char_untouched_fg"))
287 		color_main_fg = main_preferences_get_string ("colors", "char_untouched_fg");
288 	else
289 		color_main_fg = g_strdup (TUTOR_BLACK);
290 	/* Check if altcolor applies */
291 	if (main_altcolor_get_boolean ("colors", "altcolor") == TRUE)
292 	{
293 		g_free (color_main_fg);
294 		g_free (color_main_bg);
295 		color_main_fg = main_altcolor_get_string ("colors", "char_untouched_fg");
296 		color_main_bg = main_altcolor_get_string ("colors", "char_untouched_bg");
297 	}
298 
299 	/* Colors of text on the tutor window (note: ordering here matters, the first tag created is in the bottom!)
300 	 */
301 	gtk_text_buffer_create_tag (buf, "char_keep_wrap",
302 		       	"background", color_main_bg,
303 		       	"foreground", color_main_fg,
304 		       	"underline", PANGO_UNDERLINE_NONE, NULL);
305 
306 	/* This second one seems to not be needed. FIXME ?
307 	gtk_text_buffer_create_tag (buf, "char_keep_wrap2",
308 		       	"background", color_main_bg,
309 		       	"foreground", color_main_fg,
310 		       	"underline", PANGO_UNDERLINE_NONE, NULL);
311 	*/
312 
313 	cb_color_tag ("char_untouched",	TUTOR_BLACK,	TUTOR_CREAM,    buf);
314 	cb_color_tag ("char_wrong", 	TUTOR_RED,	TUTOR_RED_LITE, buf);
315 	cb_color_tag ("char_correct",	TUTOR_GREEN,	TUTOR_CREAM,    buf);
316 	cb_color_tag ("char_retouched",	TUTOR_BROWN,	TUTOR_GRAY,     buf);
317  	cb_color_tag ("cursor_blink",	TUTOR_BLUE,	TUTOR_YELLOW,   buf);
318  	cb_color_tag ("cursor_unblink",	TUTOR_BLUE_LITE,TUTOR_CREAM,    buf);
319 	cb_color_tag ("text_intro",	TUTOR_BLACK,	TUTOR_WHITE,    buf);
320 
321 	/* Tutor font */
322 	tmp_font = main_preferences_get_string ("tutor", "lesson_font");
323 	if (tmp_font == NULL)
324 	{
325 		if (tutor_is_tibetan() || g_str_has_prefix (main_preferences_get_string ("interface", "language"), "bo"))
326 			tmp_font = g_strdup (LESSON_FONT_BO);
327 		else
328 			tmp_font = g_strdup (LESSON_FONT);
329 		main_preferences_set_string ("tutor", "lesson_font", tmp_font);
330 	}
331 	gtk_text_buffer_create_tag (buf, "lesson_font", "font", tmp_font, NULL);
332 	gtk_font_chooser_set_font (GTK_FONT_CHOOSER (get_wg ("fontbutton_tutor")), tmp_font);
333 
334 	/* Change default font throughout the widget
335 	 */
336 	font_desc = pango_font_description_from_string (tmp_font);
337 	g_free (tmp_font);
338 	pg_style = pango_font_description_get_style (font_desc);
339 	pg_weight = pango_font_description_get_weight (font_desc);
340 	pg_size = pango_font_description_get_size (font_desc) / PANGO_SCALE;
341 	pg_family = (gchar *) pango_font_description_get_family (font_desc);
342 	tmp = g_strdup_printf (".tutor-font {color: %s; font: %s %s %ipt %s;}", color_main_fg,
343 			pg_style==PANGO_STYLE_NORMAL?"":"italic",
344 			pg_weight>PANGO_WEIGHT_MEDIUM?"bold":"",
345 		       	pg_size, pg_family);
346 	css = gtk_css_provider_new ();
347 	gtk_css_provider_load_from_data (css, tmp, -1, NULL);
348 	sc = gtk_widget_get_style_context (widget);
349 	gtk_style_context_add_provider (sc, GTK_STYLE_PROVIDER (css), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
350 	gtk_style_context_add_class (sc, "tutor-font");
351 	pango_font_description_free (font_desc);
352 	g_free (tmp);
353 
354 	g_free (color_main_bg);
355 	g_free (color_main_fg);
356 
357 	/* Turns on/off the beeps according to preferences
358 	 */
359 	if (main_preferences_exist ("tutor", "tutor_beep"))
360 		beep = main_preferences_get_boolean ("tutor", "tutor_beep");
361 	else
362 		beep = TRUE;
363 
364 	tutor_init_goals ();
365 }
366 
367 static void
cb_quick_restart()368 cb_quick_restart ()
369 {
370 	if (tutor_get_query () == QUERY_END)
371 		return;
372 
373 	tutor_set_query (QUERY_INTRO);
374 	tutor_process_touch (L'\0');
375 }
376 
377 G_MODULE_EXPORT void
on_button_tutor_other_clicked(GtkButton * button,gpointer user_data)378 on_button_tutor_other_clicked (GtkButton * button, gpointer user_data)
379 {
380 	GtkWidget *wg;
381 	GtkListStore *list;
382 
383 	gtk_widget_show (get_wg ("popup_other"));
384 
385 	wg = get_wg ("treeview_other");
386 	list = GTK_LIST_STORE ( gtk_tree_view_get_model (GTK_TREE_VIEW (wg)) );
387 	if (tutor_get_type () == TT_VELO)
388 	{
389 		if (main_velo_txt ())
390 			tutor_load_list_other (".paragraphs", list);
391 		else
392 			tutor_load_list_other (".words", list);
393 	}
394 	else if (tutor_get_type () == TT_FLUID)
395 		tutor_load_list_other (".paragraphs", list);
396 
397 	g_free (other_renaming_undo);
398 	other_renaming_undo = g_strdup ("*");
399 }
400 
401 G_MODULE_EXPORT void
on_spinbutton_lesson_value_changed(GtkSpinButton * spinbutton,gpointer user_data)402 on_spinbutton_lesson_value_changed (GtkSpinButton * spinbutton, gpointer user_data)
403 {
404 	gint tmp_int;
405 
406 	if (callbacks_shield)
407 		return;
408 
409 	if (tutor_get_type () == TT_BASIC)
410 	{
411 		basic_set_lesson_increased (FALSE);
412 		basic_set_lesson (gtk_spin_button_get_value_as_int (spinbutton));
413 		basic_init_char_set ();
414 	}
415 	else if (tutor_get_type () == TT_FLUID)
416 	{
417 		tmp_int = gtk_spin_button_get_value_as_int (spinbutton);
418 		main_preferences_set_int ("tutor", "fluid_paragraphs", tmp_int);
419 	}
420 	tutor_set_query (QUERY_INTRO);
421 	tutor_process_touch (L'\0');
422 }
423 
424 G_MODULE_EXPORT void
on_eventbox_lesson_plus(GtkWidget * widget,gpointer user_data)425 on_eventbox_lesson_plus (GtkWidget * widget, gpointer user_data)
426 {
427 	gint tmp_int;
428 	GtkSpinButton *spb;
429 
430 	spb = GTK_SPIN_BUTTON (get_wg ("spinbutton_lesson"));
431 	tmp_int = gtk_spin_button_get_value_as_int (spb);
432 	gtk_spin_button_set_value (spb, tmp_int + 1);
433 }
434 
435 G_MODULE_EXPORT void
on_eventbox_lesson_minus(GtkWidget * widget,gpointer user_data)436 on_eventbox_lesson_minus (GtkWidget * widget, gpointer user_data)
437 {
438 	gint tmp_int;
439 	GtkSpinButton *spb;
440 
441 	spb = GTK_SPIN_BUTTON (get_wg ("spinbutton_lesson"));
442 	tmp_int = gtk_spin_button_get_value_as_int (spb);
443 	if (tmp_int > 0)
444 		gtk_spin_button_set_value (spb, tmp_int - 1);
445 }
446 
447 G_MODULE_EXPORT void
on_eventbox_stat_plus(GtkWidget * widget,gpointer user_data)448 on_eventbox_stat_plus (GtkWidget * widget, gpointer user_data)
449 {
450 	gint tmp_int;
451 	GtkSpinButton *spb;
452 
453 	spb = GTK_SPIN_BUTTON (get_wg ("spinbutton_stat_lesson"));
454 	tmp_int = gtk_spin_button_get_value_as_int (spb);
455 	gtk_spin_button_set_value (spb, tmp_int + 1);
456 }
457 
458 G_MODULE_EXPORT void
on_eventbox_stat_minus(GtkWidget * widget,gpointer user_data)459 on_eventbox_stat_minus (GtkWidget * widget, gpointer user_data)
460 {
461 	gint tmp_int;
462 	GtkSpinButton *spb;
463 
464 	spb = GTK_SPIN_BUTTON (get_wg ("spinbutton_stat_lesson"));
465 	tmp_int = gtk_spin_button_get_value_as_int (spb);
466 	if (tmp_int > 0)
467 		gtk_spin_button_set_value (spb, tmp_int - 1);
468 }
469 
470 G_MODULE_EXPORT void
on_button_tutor_stat_clicked(GtkButton * button,gpointer user_data)471 on_button_tutor_stat_clicked (GtkButton * button, gpointer user_data)
472 {
473 	gint aux;
474 
475 	gtk_combo_box_set_active (GTK_COMBO_BOX (get_wg ("combobox_stat_module")), tutor_get_type ());
476 	if (tutor_get_type () == TT_BASIC)
477 	{
478 		callbacks_shield_set (TRUE);
479 		aux = basic_get_lesson () - (basic_get_lesson_increased () ? 1 : 0);
480 		aux += (aux == 0) ? 1 : 0;
481 		gtk_spin_button_set_value (GTK_SPIN_BUTTON (get_wg ("spinbutton_stat_lesson")), aux);
482 		callbacks_shield_set (FALSE);
483 	}
484 
485 	plot_draw_chart (gtk_combo_box_get_active (GTK_COMBO_BOX (get_wg ("combobox_stat_type"))) + 1);
486 	gtk_widget_show (get_wg ("window_stat"));
487 }
488 
489 G_MODULE_EXPORT void
on_togglebutton_edit_basic_lesson_toggled(GtkToggleButton * togglebutton,gpointer user_data)490 on_togglebutton_edit_basic_lesson_toggled (GtkToggleButton * togglebutton, gpointer user_data)
491 {
492 	gchar *tmp_name;
493 	GtkWidget *wg;
494 
495 	wg = get_wg ("entry_custom_basic_lesson");
496 	if (gtk_toggle_button_get_active (togglebutton))
497 	{
498 		callbacks_shield_set (TRUE);
499 		gtk_widget_show (wg);
500 		gtk_widget_grab_focus (wg);
501 
502 		tmp_name = g_ucs4_to_utf8 (basic_get_char_set (), -1, NULL, NULL, NULL);
503 		gtk_entry_set_text (GTK_ENTRY (wg), g_strstrip (tmp_name));
504 		gtk_editable_set_position (GTK_EDITABLE (wg), -1);
505 		g_free (tmp_name);
506 
507 		gtk_widget_set_sensitive (get_wg ("spinbutton_lesson"), FALSE);
508 		gtk_label_set_text (GTK_LABEL (get_wg ("label_heading")), _("Keys:"));
509 	}
510 	else
511 	{
512 		gtk_widget_hide (wg);
513 
514 		tmp_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (wg)));
515 		if (tutor_get_type () == TT_BASIC)
516 		{
517 			basic_save_lesson (tmp_name);
518 
519 			basic_init_char_set ();
520 			tutor_set_query (QUERY_INTRO);
521 			tutor_process_touch (L'\0');
522 
523 			gtk_widget_set_sensitive (get_wg ("spinbutton_lesson"), TRUE);
524 		}
525 		g_free (tmp_name);
526 
527 		callbacks_shield_set (FALSE);
528 		gtk_widget_grab_focus (get_wg ("entry_mesg"));
529 	}
530 }
531 
532 G_MODULE_EXPORT void
on_togglebutton_toomuch_errors_toggled(GtkToggleButton * togglebutton,gpointer user_data)533 on_togglebutton_toomuch_errors_toggled (GtkToggleButton * togglebutton, gpointer user_data)
534 {
535 	cb_quick_restart ();
536 }
537 
538 G_MODULE_EXPORT void
on_entry_custom_basic_lesson_activate(GtkEntry * entry,gpointer user_data)539 on_entry_custom_basic_lesson_activate (GtkEntry * entry, gpointer user_data)
540 {
541 	GtkWidget *wg;
542 
543 	wg = get_wg ("togglebutton_edit_basic_lesson");
544 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (wg), FALSE);
545 }
546 
547 G_MODULE_EXPORT void
on_button_tutor_top10_clicked(GtkButton * button,gpointer user_data)548 on_button_tutor_top10_clicked (GtkButton * button, gpointer user_data)
549 {
550 	GtkComboBox *cmb;
551 
552 	cmb = GTK_COMBO_BOX (get_wg ("combobox_top10"));
553 
554 	if (gtk_combo_box_get_active (cmb) == -1)
555 		gtk_combo_box_set_active (cmb, 0);
556 
557 	top10_message (NULL);
558 
559 	if (gtk_combo_box_get_active (GTK_COMBO_BOX (get_wg ("combobox_top10"))) == 0)
560 		top10_show_stats (LOCAL);
561 	else
562 		top10_show_stats (GLOBAL);
563 
564 	gtk_widget_show (get_wg ("window_top10"));
565 }
566 
567 G_MODULE_EXPORT void
on_button_tutor_show_keyb_clicked(GtkButton * button,gpointer user_data)568 on_button_tutor_show_keyb_clicked (GtkButton * button, gpointer user_data)
569 {
570 	if (gtk_widget_get_visible (get_wg ("window_hints")))
571 		window_save ("hints");
572 	keyb_mode_hint ();
573 }
574 
575 G_MODULE_EXPORT void
on_fontbutton_tutor_font_set(GtkFontButton * fbut,gpointer user_data)576 on_fontbutton_tutor_font_set (GtkFontButton * fbut, gpointer user_data)
577 {
578 	GtkWidget *wg;
579 	GtkTextBuffer *buf;
580 	GtkTextTagTable *ttab;
581 	GtkTextIter start;
582 	GtkTextIter end;
583 	gchar *tmp_font;
584 
585 	tmp_font = gtk_font_chooser_get_font (GTK_FONT_CHOOSER (fbut));
586 	if (tmp_font == NULL)
587 		tmp_font = g_strdup (LESSON_FONT);
588 	if (strlen (tmp_font) == 0)
589 	{
590 		g_free (tmp_font);
591 		tmp_font = g_strdup (LESSON_FONT);
592 	}
593 	main_preferences_set_string ("tutor", "lesson_font", tmp_font);
594 
595 	wg = get_wg ("text_tutor");
596 	buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (wg));
597 	ttab = gtk_text_buffer_get_tag_table (buf);
598 	gtk_text_buffer_get_bounds (buf, &start, &end);
599 
600 	gtk_text_tag_table_remove (ttab, gtk_text_tag_table_lookup (ttab, "lesson_font"));
601 	gtk_text_buffer_create_tag (buf, "lesson_font", "font", tmp_font, NULL);
602 	gtk_text_buffer_apply_tag_by_name (buf, "lesson_font", &start, &end);
603 
604 	g_free (tmp_font);
605 }
606 
607 G_MODULE_EXPORT void
on_togglebutton_altcolor_toggled(GtkToggleButton * togglebutton,gpointer user_data)608 on_togglebutton_altcolor_toggled (GtkToggleButton * togglebutton, gpointer user_data)
609 {
610 	gboolean acol;
611 
612 	if (callbacks_shield)
613 		return;
614 
615 	acol = gtk_toggle_button_get_active (togglebutton);
616 	main_altcolor_set_boolean ("colors", "altcolor", acol);
617 }
618 
619 G_MODULE_EXPORT void
on_togglebutton_tutor_intro_toggled(GtkToggleButton * button,gpointer user_data)620 on_togglebutton_tutor_intro_toggled (GtkToggleButton *button, gpointer user_data)
621 {
622 	if (callbacks_shield)
623 		return;
624 
625 	tutor_set_query (QUERY_INTRO);
626 	if (gtk_toggle_button_get_active (button))
627 		tutor_update ();
628 	else
629 		tutor_process_touch (UPSYM);
630 }
631 
632 G_MODULE_EXPORT void
on_button_tutor_back_clicked(GtkButton * button,gpointer user_data)633 on_button_tutor_back_clicked (GtkButton *button, gpointer user_data)
634 {
635 	window_save ("tutor");
636 	if (gtk_widget_get_visible (get_wg ("window_hints")))
637 		window_save ("hints");
638 	if (gtk_widget_get_visible (get_wg ("window_top10")))
639 		window_save ("top10");
640 	if (gtk_widget_get_visible (get_wg ("window_stat")))
641 		window_save ("stat");
642 	gtk_widget_hide (get_wg ("window_tutor"));
643 	gtk_widget_hide (get_wg ("window_hints"));
644 	gtk_widget_hide (get_wg ("window_top10"));
645 	gtk_widget_hide (get_wg ("window_stat"));
646 	gtk_widget_hide (get_wg ("filechooser_tutor"));
647 	gtk_widget_show (get_wg ("window_main"));
648 }
649 
650 G_MODULE_EXPORT void
on_window_tutor_destroy(GtkWidget * object,gpointer user_data)651 on_window_tutor_destroy (GtkWidget * object, gpointer user_data)
652 {
653 	if (callbacks_shield)
654 		return;
655 	window_save ("tutor");
656 	main_preferences_set_boolean ("interface", "autostart", TRUE);
657 	main_preferences_set_int ("interface", "exercise", tutor_get_type ());
658 	callbacks_shield_set (TRUE);
659 	main_window_pass_away ();
660 }
661 
662 G_MODULE_EXPORT void
on_button_tutor_close_clicked(GtkButton * button,gpointer user_data)663 on_button_tutor_close_clicked (GtkButton *button, gpointer user_data)
664 {
665 	on_window_tutor_destroy (NULL, NULL);
666 }
667 
668 G_MODULE_EXPORT void
on_eventbox_tutor_restart_grab_focus(GtkWidget * widget,gpointer user_data)669 on_eventbox_tutor_restart_grab_focus (GtkWidget * widget, gpointer user_data)
670 {
671 	cb_quick_restart ();
672 }
673 
674 G_MODULE_EXPORT void
on_entry_mesg_icon_release(GtkEntry * entry,GtkEntryIconPosition pos,GdkEvent * event,gpointer user_data)675 on_entry_mesg_icon_release (GtkEntry *entry, GtkEntryIconPosition pos, GdkEvent *event, gpointer user_data)
676 {
677 	cb_quick_restart ();
678 }
679 
680 /* Managing the keyboard/messages entry of the tutor window
681  */
682 
683 G_MODULE_EXPORT gboolean
on_entry_mesg_focus_out_event(GtkWidget * widget,GdkEventFocus * event,gpointer user_data)684 on_entry_mesg_focus_out_event (GtkWidget * widget, GdkEventFocus * event, gpointer user_data)
685 {
686 	if (callbacks_shield)
687 		return (FALSE);
688 
689 	gtk_widget_grab_focus (widget);
690 	return (FALSE);
691 }
692 
693 
694 G_MODULE_EXPORT void
on_entry_mesg_grab_focus(GtkWidget * widget,gpointer user_data)695 on_entry_mesg_grab_focus (GtkWidget * widget, gpointer user_data)
696 {
697 	if (callbacks_shield)
698 		return;
699 
700 	gtk_editable_select_region (GTK_EDITABLE (widget), 0, 0);
701 	gtk_editable_set_position (GTK_EDITABLE (widget), -1);
702 }
703 
704 
705 G_MODULE_EXPORT void
on_entry_mesg_activate(GtkEntry * entry,gpointer user_data)706 on_entry_mesg_activate (GtkEntry * entry, gpointer user_data)
707 {
708 	gchar *tmp1;
709 	gchar *tmp2;
710 
711 	if (callbacks_shield)
712 		return;
713 
714 	tmp1 = g_strdup (gtk_entry_get_text (entry));
715 	tmp2 = g_strconcat (tmp1, keyb_get_utf8_paragraph_symbol (), NULL);
716 	callbacks_shield_set (TRUE);
717 	gtk_entry_set_text (entry, tmp2);
718 	callbacks_shield_set (FALSE);
719 	g_free (tmp1);
720 	g_free (tmp2);
721 
722 	gtk_editable_set_position (GTK_EDITABLE (entry), -1);
723 
724 	tutor_process_touch (UPSYM);
725 }
726 
727 
728 G_MODULE_EXPORT void
on_entry_mesg_delete_text(GtkEditable * editable,gint start_pos,gint end_pos,gpointer user_data)729 on_entry_mesg_delete_text (GtkEditable * editable, gint start_pos, gint end_pos, gpointer user_data)
730 {
731 	if (callbacks_shield)
732 		return;
733 
734 	if (end_pos - start_pos == 1)
735 		tutor_process_touch (L'\b');
736 	else
737 		tutor_process_touch (L'\t');
738 }
739 
740 G_MODULE_EXPORT void
on_entry_mesg_insert_text(GtkEditable * editable,gchar * new_text,gint new_text_length,gpointer position,gpointer user_data)741 on_entry_mesg_insert_text (GtkEditable * editable,
742 			   gchar * new_text, gint new_text_length, gpointer position,
743 			   gpointer user_data)
744 {
745 	gchar *test;
746 	gchar *text;
747 
748 	if (callbacks_shield)
749 		return;
750 
751 	if (mesg_drag_drop)
752 	{
753 		mesg_drag_drop = FALSE;
754 
755 		callbacks_shield_set (TRUE);
756 		gtk_editable_delete_text (editable, 0, -1);
757 		callbacks_shield_set (FALSE);
758 
759 		if (g_str_has_prefix (new_text, "file://"))
760 			test = new_text + 7;
761 		else
762 			test = new_text;
763 
764 		if (g_file_test (test, G_FILE_TEST_IS_REGULAR))
765 			g_file_get_contents (test, &text, NULL, NULL);
766 		else
767 			text = g_strdup (new_text);
768 
769 		if (tutor_get_type () == TT_VELO)
770 			velo_text_write_to_file (text, TRUE);
771 		else if (tutor_get_type () == TT_FLUID)
772 			fluid_text_write_to_file (text);
773 
774 		g_free (text);
775 		return;
776 	}
777 
778 	tutor_process_touch (g_utf8_get_char_validated (new_text, new_text_length));
779 }
780 
781 /* For languages using input method - by Hodong Kim */
782 G_MODULE_EXPORT void
on_entry_mesg_preedit_changed(GtkEntry * entry,gchar * preedit,gpointer user_data)783 on_entry_mesg_preedit_changed (GtkEntry * entry,
784 			       gchar * preedit,
785 			       gpointer user_data)
786 {
787   gunichar real_char = cursor_get_char ();
788   gunichar preedit_char = g_utf8_get_char (preedit);
789 
790   if (real_char == preedit_char)
791   {
792     /* This trick sends 'focus change' to GtkEntry.
793        gtk_entry_focus_in() sets priv->need_im_reset = TRUE,
794        therefore gtk_entry_reset_im_context always executes gtk_im_context_reset,
795        as a result, pre-edit text is committed
796        */
797     GdkEvent *event = gdk_event_new (GDK_FOCUS_CHANGE);
798 
799     event->focus_change.type = GDK_FOCUS_CHANGE;
800     event->focus_change.in = TRUE;
801     event->focus_change.window = gtk_widget_get_window (GTK_WIDGET (entry));
802     if (event->focus_change.window != NULL)
803       g_object_ref (event->focus_change.window);
804 
805     gtk_widget_send_focus_change (GTK_WIDGET (entry), event);
806     gdk_event_free (event);
807 
808     gtk_entry_reset_im_context (entry);
809   }
810 }
811 
812 /* Tutor drag and drop handling
813  */
814 G_MODULE_EXPORT gboolean
on_entry_mesg_drag_drop(GtkWidget * widget,GdkDragContext * drag_context,gint x,gint y,guint time,gpointer user_data)815 on_entry_mesg_drag_drop (GtkWidget * widget, GdkDragContext * drag_context,
816 			 gint x, gint y, guint time, gpointer user_data)
817 {
818 	callbacks_shield_set (TRUE);
819 	gtk_entry_set_text (GTK_ENTRY (widget), "");
820 	callbacks_shield_set (FALSE);
821 	mesg_drag_drop = TRUE;
822 	return FALSE;
823 }
824 
825 G_MODULE_EXPORT void
on_entry_mesg_realize(GtkWidget * widget,gpointer user_data)826 on_entry_mesg_realize (GtkWidget * widget, gpointer user_data)
827 {
828 	GtkBindingSet *bs;
829 	GdkModifierType modmask = GDK_CONTROL_MASK;
830 
831 	bs = gtk_binding_set_by_class (G_OBJECT_GET_CLASS ( G_OBJECT (GTK_ENTRY (widget))));
832 	gtk_binding_entry_add_signal (bs, GDK_KEY_w, modmask,
833 			"delete-from-cursor", 2,
834 			G_TYPE_INT, GTK_DELETE_WORDS,
835 			G_TYPE_INT, -1); /* Delete -1 word: Ctrl+w */
836 	gtk_binding_entry_add_signal (bs, GDK_KEY_h, modmask,
837 			"delete-from-cursor", 2,
838 			G_TYPE_INT, GTK_DELETE_CHARS,
839 			G_TYPE_INT, -1); /* Delete -1 char: Ctrl+h */
840 }
841 
842 /**********************************************************************
843  * 4 - Top 10 management
844  **********************************************************************/
845 G_MODULE_EXPORT void
on_combobox_top10_changed(GtkComboBox * cmb,gpointer user_data)846 on_combobox_top10_changed (GtkComboBox *cmb, gpointer user_data)
847 {
848 	if (callbacks_shield)
849 		return;
850 
851 	top10_message (NULL);
852 
853 	if (gtk_combo_box_get_active (cmb) == 0)
854 		top10_show_stats (LOCAL);
855 	else
856 		top10_show_stats (GLOBAL);
857 }
858 
859 G_MODULE_EXPORT void
on_combobox_top10_language_changed(GtkComboBox * cmb,gpointer user_data)860 on_combobox_top10_language_changed (GtkComboBox *cmb, gpointer user_data)
861 {
862 	gchar *tmp;
863 	gint active;
864 
865 	if (callbacks_shield)
866 		return;
867 	tmp = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (cmb));
868 	trans_change_language (tmp);
869 	g_free (tmp);
870 
871 	callbacks_shield_set (TRUE);
872 	active = gtk_combo_box_get_active (cmb);
873 	gtk_combo_box_set_active (GTK_COMBO_BOX (get_wg ("combobox_language")), active);
874 	callbacks_shield_set (FALSE);
875 
876 	if (tutor_get_type () == TT_FLUID)
877 		tutor_init (tutor_get_type ());
878 
879 	on_combobox_top10_changed (GTK_COMBO_BOX (get_wg ("combobox_top10")), NULL);
880 }
881 
882 G_MODULE_EXPORT void
on_button_top10_go_www_clicked(GtkButton * button,gpointer user_data)883 on_button_top10_go_www_clicked         (GtkButton       *button,
884                                         gpointer         user_data)
885 {
886 	gchar *url;
887 	url = gtk_widget_get_tooltip_text (get_wg ("button_top10_go_www"));
888 	gtk_show_uri_on_window (get_win ("window_top10") , url, GDK_CURRENT_TIME, NULL);
889 	g_free (url);
890 }
891 
892 G_MODULE_EXPORT void
on_button_top10_publish_clicked(GtkButton * button,gpointer user_data)893 on_button_top10_publish_clicked (GtkButton * button, gpointer user_data)
894 {
895 	GtkWidget *wg;
896 
897 	top10_message (NULL);
898 
899 	main_preferences_save ();
900 	wg = get_wg ("image_top10_publish");
901 	gtk_image_set_from_icon_name (GTK_IMAGE (wg), "gtk-yes", GTK_ICON_SIZE_BUTTON);
902 
903 	top10_message (_("Connecting..."));
904 
905 	g_idle_add ((GSourceFunc) top10_global_publish, NULL);
906 }
907 
908 G_MODULE_EXPORT void
on_button_top10_update_clicked(GtkButton * button,gpointer user_data)909 on_button_top10_update_clicked (GtkButton * button, gpointer user_data)
910 {
911 	GtkWidget *wg;
912 
913 	top10_message (NULL);
914 
915 	main_preferences_save ();
916 	wg = get_wg ("image_top10_update");
917 	gtk_image_set_from_icon_name (GTK_IMAGE (wg), "gtk-yes", GTK_ICON_SIZE_BUTTON);
918 
919 	top10_message (_("Connecting..."));
920 
921 	g_idle_add ((GSourceFunc) top10_global_update, NULL);
922 }
923 
924 G_MODULE_EXPORT void
on_button_top10_expand_clicked(GtkButton * button,gpointer user_data)925 on_button_top10_expand_clicked (GtkButton * button, gpointer user_data)
926 {
927 	gtk_widget_hide (GTK_WIDGET (button));
928 	gtk_widget_show (get_wg ("button_top10_noexpand"));
929 	gtk_widget_show (get_wg ("scrolledwindow_top10_2"));
930 	gtk_widget_set_size_request (get_wg ("window_top10"), 630, 400);
931 }
932 
933 G_MODULE_EXPORT void
on_button_top10_noexpand_clicked(GtkButton * button,gpointer user_data)934 on_button_top10_noexpand_clicked (GtkButton * button, gpointer user_data)
935 {
936 	gtk_widget_hide (get_wg ("scrolledwindow_top10_2"));
937 	gtk_widget_show (get_wg ("button_top10_expand"));
938 	gtk_widget_hide (GTK_WIDGET (button));
939 	gtk_widget_set_size_request (get_wg ("window_top10"), 350, 400);
940 }
941 
942 G_MODULE_EXPORT void
on_button_top10_close_clicked(GtkButton * button,gpointer user_data)943 on_button_top10_close_clicked (GtkButton * button, gpointer user_data)
944 {
945 	top10_message (NULL);
946 	window_save ("top10");
947 	gtk_widget_hide (get_wg ("window_top10"));
948 }
949 
950 /**********************************************************************
951  * 5 - Keyboard window
952  **********************************************************************/
953 
954 G_MODULE_EXPORT void
on_combobox_keyboard_country_changed(GtkComboBox * cmb,gpointer user_data)955 on_combobox_keyboard_country_changed (GtkComboBox *cmb, gpointer user_data)
956 {
957 	gchar *tmp;
958 
959 	if (callbacks_shield)
960 		return;
961 
962 	keyb_set_combo_kbd_variant ("combobox_keyboard_country", "combobox_keyboard_variant");
963 	tmp = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (cmb));
964 	if (g_str_equal (tmp, KEYB_CUSTOM) &&
965 		gtk_combo_box_get_active ( GTK_COMBO_BOX (get_wg ("combobox_keyboard_variant")) ) > -1 )
966 	{
967 			gtk_widget_set_sensitive (get_wg ("button_kb_remove"), TRUE);
968 	}
969 	else
970 		gtk_widget_set_sensitive (get_wg ("button_kb_remove"), FALSE);
971 	g_free (tmp);
972 }
973 
974 G_MODULE_EXPORT void
on_combobox_keyboard_variant_changed(GtkComboBox * cmb,gpointer user_data)975 on_combobox_keyboard_variant_changed (GtkComboBox *cmb, gpointer user_data)
976 {
977 	gchar *tmp;
978 
979 	if (callbacks_shield)
980 		return;
981 
982 	keyb_update_from_variant ("combobox_keyboard_country", "combobox_keyboard_variant");
983 
984 	tmp = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_keyboard_country")));
985 	if (g_str_equal (tmp, KEYB_CUSTOM) &&
986 		gtk_combo_box_get_active (cmb) > -1 )
987 
988 		gtk_widget_set_sensitive (get_wg ("button_kb_remove"), TRUE);
989 	else
990 		gtk_widget_set_sensitive (get_wg ("button_kb_remove"), FALSE);
991 	g_free (tmp);
992 }
993 
994 G_MODULE_EXPORT void
on_button_kb_remove_clicked(GtkButton * button,gpointer user_data)995 on_button_kb_remove_clicked (GtkButton * button, gpointer user_data)
996 {
997 	gchar *tmp_path = NULL;
998 
999 	tmp_path = g_strconcat (main_path_user (), G_DIR_SEPARATOR_S, keyb_get_name (), ".kbd", NULL);
1000 	if (! g_file_test (tmp_path, G_FILE_TEST_IS_REGULAR))
1001 	{
1002 		gdk_display_beep (gdk_display_get_default());
1003 		g_message ("no valid keyboard to remove...");
1004 		g_free (tmp_path);
1005 		return;
1006 	}
1007 	g_free (tmp_path);
1008 
1009 	gtk_label_set_text (GTK_LABEL (get_wg ("label_confirm_action")), "REMOVE");
1010 	gtk_widget_show (get_wg ("dialog_confirm"));
1011 }
1012 
1013 G_MODULE_EXPORT void
on_button_kb_save_clicked(GtkButton * button,gpointer user_data)1014 on_button_kb_save_clicked (GtkButton * button, gpointer user_data)
1015 {
1016 	gchar *tmp;
1017 	gchar *tmp_path;
1018 
1019 	tmp = gtk_editable_get_chars (GTK_EDITABLE (get_wg ("entry_keyboard")), 0, -1);
1020 	if (strlen (tmp) == 0)
1021 	{
1022 		g_free (tmp);
1023 		tmp = g_strdup (KEYB_AUTO_SAVE);
1024 	}
1025 	tmp_path = g_strconcat (main_path_user (), G_DIR_SEPARATOR_S, tmp, ".kbd", NULL);
1026 	keyb_set_name (tmp);
1027 	g_free (tmp);
1028 
1029 	gtk_label_set_text (GTK_LABEL (get_wg ("label_confirm_action")), "OVERWRITE");
1030 	if (g_file_test (tmp_path, G_FILE_TEST_IS_REGULAR))
1031 		gtk_widget_show (get_wg ("dialog_confirm"));
1032 	else
1033 		on_button_confirm_yes_clicked (GTK_BUTTON (get_wg ("button_confirm_yes")), NULL);
1034 	g_free (tmp_path);
1035 }
1036 
1037 G_MODULE_EXPORT void
on_button_keyboard_previous_clicked(GtkButton * but,gpointer user_data)1038 on_button_keyboard_previous_clicked (GtkButton *but, gpointer user_data)
1039 {
1040 	keyb_intro_step_previous ();
1041 }
1042 
1043 G_MODULE_EXPORT void
on_button_keyboard_next_clicked(GtkButton * but,gpointer user_data)1044 on_button_keyboard_next_clicked (GtkButton *but, gpointer user_data)
1045 {
1046 	keyb_intro_step_next ();
1047 }
1048 
1049 G_MODULE_EXPORT void
on_button_keyboard_close_clicked(GtkButton * but,gpointer user_data)1050 on_button_keyboard_close_clicked (GtkButton *but, gpointer user_data)
1051 {
1052 	hints_demo_fingers (0);
1053 	gtk_widget_hide (get_wg ("window_keyboard"));
1054 }
1055 
1056 G_MODULE_EXPORT void
on_button_keyboard_hands_clicked(GtkButton * but,gpointer user_data)1057 on_button_keyboard_hands_clicked (GtkButton *but, gpointer user_data)
1058 {
1059 	if (gtk_widget_get_visible (get_wg ("window_tutor")))
1060 		if (gtk_widget_get_visible (get_wg ("hbox_keyboard_hints")))
1061 		{
1062 			gtk_widget_show (get_wg ("window_hints"));
1063 			hints_update_from_char (cursor_get_char ());
1064 		}
1065 
1066 	hints_demo_fingers (0);
1067 	gtk_widget_hide (get_wg ("window_keyboard"));
1068 }
1069 
1070 G_MODULE_EXPORT void
on_button_keyboard_cancel_clicked(GtkButton * button,gpointer user_data)1071 on_button_keyboard_cancel_clicked (GtkButton * button, gpointer user_data)
1072 {
1073 	gchar *tmp;
1074 
1075 	tmp = main_preferences_get_string ("tutor", "keyboard");
1076 	keyb_set_name (tmp);
1077 	keyb_set_chars ();
1078 	keyb_update_combos ("combobox_kbd_country", "combobox_kbd_variant");
1079 	gtk_widget_hide (get_wg ("window_keyboard"));
1080 	g_free (tmp);
1081 }
1082 
1083 /* Editing the keyboard.
1084  */
1085 G_MODULE_EXPORT void
on_toggle_shift1_toggled(GtkToggleButton * togglebutton,gpointer user_data)1086 on_toggle_shift1_toggled (GtkToggleButton * togglebutton, gpointer user_data)
1087 {
1088 	gboolean tog_state;
1089 	GtkWidget *tog;
1090 
1091 	tog = get_wg ("toggle_shift2");
1092 	tog_state = gtk_toggle_button_get_active (togglebutton);
1093 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tog), tog_state);
1094 	keyb_update_virtual_layout ();
1095 	keyb_edit_none ();
1096 
1097 	if (gtk_widget_get_visible (get_wg ("hbox_keyboard_hints")))
1098 		hints_update_from_button (GTK_BUTTON (togglebutton));
1099 }
1100 
1101 G_MODULE_EXPORT void
on_toggle_shift2_toggled(GtkToggleButton * togglebutton,gpointer user_data)1102 on_toggle_shift2_toggled (GtkToggleButton * togglebutton, gpointer user_data)
1103 {
1104 	gboolean tog_state;
1105 	GtkWidget *tog;
1106 
1107 	tog = get_wg ("toggle_shift1");
1108 	tog_state = gtk_toggle_button_get_active (togglebutton);
1109 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (tog), tog_state);
1110 	keyb_update_virtual_layout ();
1111 	keyb_edit_none ();
1112 
1113 	if (gtk_widget_get_visible (get_wg ("hbox_keyboard_hints")))
1114 		hints_update_from_button (GTK_BUTTON (togglebutton));
1115 }
1116 
1117 G_MODULE_EXPORT void
on_toggle_shift1_grab_focus(GtkToggleButton * tg,gpointer user_data)1118 on_toggle_shift1_grab_focus (GtkToggleButton *tg, gpointer user_data)
1119 {
1120 	if (callbacks_shield)
1121 		return;
1122 	keyb_edit_none ();
1123 }
1124 
1125 G_MODULE_EXPORT void
on_toggle_shift2_grab_focus(GtkToggleButton * tg,gpointer user_data)1126 on_toggle_shift2_grab_focus (GtkToggleButton *tg, gpointer user_data)
1127 {
1128 	if (callbacks_shield)
1129 		return;
1130 	keyb_edit_none ();
1131 }
1132 
1133 /* All the other keys
1134  */
1135 G_MODULE_EXPORT void
on_virtual_key_grab_focus(GtkWidget * wg,gpointer user_data)1136 on_virtual_key_grab_focus (GtkWidget *wg, gpointer user_data)
1137 {
1138 	if (callbacks_shield)
1139 		return;
1140 
1141 	if ( gtk_widget_get_visible (get_wg ("hbox_keyboard_hints")) )
1142 		hints_update_from_button (GTK_BUTTON (wg));
1143 	else
1144 		keyb_edit_button (GTK_BUTTON (wg));
1145 }
1146 
1147 G_MODULE_EXPORT void
on_virtual_key_clicked(GtkButton * button,gpointer user_data)1148 on_virtual_key_clicked (GtkButton * button, gpointer user_data)
1149 {
1150 	if (callbacks_shield)
1151 		return;
1152 
1153 	if ( gtk_widget_get_visible (get_wg ("hbox_keyboard_hints")) )
1154 		hints_update_from_button (button);
1155 	else
1156 		keyb_edit_button (button);
1157 }
1158 
1159 G_MODULE_EXPORT void
on_virtual_key_changed(GtkEditable * edit,gpointer user_data)1160 on_virtual_key_changed (GtkEditable *edit, gpointer user_data)
1161 {
1162 	gunichar ch;
1163 
1164 	if (callbacks_shield)
1165 		return;
1166 
1167 	ch = g_utf8_get_char_validated (gtk_entry_get_text (GTK_ENTRY (edit)), -1);
1168 	if (ch == (gunichar)-2 || ch == (gunichar)-1 || ch == 0)
1169 		ch = L' ';
1170 
1171 	keyb_change_key (ch);
1172 }
1173 
1174 /* Confirm-dialog shared tasks
1175  */
1176 G_MODULE_EXPORT void
on_dialog_confirm_show(GtkWidget * widget,gpointer user_data)1177 on_dialog_confirm_show (GtkWidget * widget, gpointer user_data)
1178 {
1179 	gchar *action;
1180 	gchar *msg;
1181 	gchar *tmp;
1182 	gint len;
1183 	GtkLabel *wg_label;
1184 
1185 	wg_label = GTK_LABEL (get_wg ("label_confirm_action"));
1186 	action = (gchar *) gtk_label_get_text (wg_label);
1187 
1188 	wg_label = GTK_LABEL (get_wg ("label_confirm"));
1189 
1190 	if (g_str_equal (action, "OVERWRITE"))
1191 	{
1192 		gtk_window_set_title (GTK_WINDOW (widget), _("Overwrite user layout"));
1193 		msg = g_strdup_printf ("%s\n\n %s",
1194 				_("This will OVERWRITE an existent keyboard layout."),
1195 			       	keyb_get_name ());
1196 		gtk_label_set_text (wg_label, msg);
1197 		g_free (msg);
1198 	}
1199 	else if (g_str_equal (action, "REMOVE"))
1200 	{
1201 		gtk_window_set_title (GTK_WINDOW (widget), _("Remove user layout"));
1202 		msg = g_strdup_printf ("%s\n\n %s",
1203 				_("This will REMOVE an existent keyboard layout."),
1204 			       	keyb_get_name ());
1205 		gtk_label_set_text (wg_label, msg);
1206 		g_free (msg);
1207 	}
1208 	else if (g_str_equal (action, "RESET_ALL"))
1209 	{
1210 		gtk_window_set_title (GTK_WINDOW (widget), _("Reset progress data"));
1211 		gtk_label_set_text (wg_label, _("This will DELETE all the progress data of all modules."));
1212 	}
1213 	else if (g_str_equal (action, "RESET"))
1214 	{
1215 		gtk_window_set_title (GTK_WINDOW (widget), _("Reset progress data"));
1216 		tmp = g_strdup (tutor_get_type_name ());
1217 		for (msg = tmp; tmp[0] != '\0'; tmp++) tmp[0] = g_ascii_toupper (tmp[0]);
1218 		tmp = msg;
1219 		if (g_str_equal (tmp, "ADAPT") && gtk_combo_box_get_active (GTK_COMBO_BOX (get_wg ("combobox_stat_type"))) > 1)
1220 		{
1221 			g_free (tmp);
1222 			tmp = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")));
1223 			msg = g_strdup_printf (_("This will delete the data of this chart. <%s>"), tmp);
1224 		}
1225 		else
1226 			msg = g_strdup_printf (_("This will delete the progress data of the module. (%s)"), tmp);
1227 		gtk_label_set_text (wg_label, msg);
1228 		g_free (tmp);
1229 		g_free (msg);
1230 	}
1231 	else
1232 	{
1233 		g_warning ("No valid action for this confirm-dialog: %s", action);
1234 		gtk_widget_hide (widget);
1235 	}
1236 }
1237 
1238 G_MODULE_EXPORT void
on_button_confirm_yes_clicked(GtkButton * button,gpointer user_data)1239 on_button_confirm_yes_clicked (GtkButton * button, gpointer user_data)
1240 {
1241 	gchar *file;
1242 	gchar *action;
1243 	gchar *msg;
1244 	gchar *tmp;
1245 	GtkWidget *wg;
1246 
1247 	wg = get_wg ("label_confirm_action");
1248 	action = (gchar *) gtk_label_get_text (GTK_LABEL (wg));
1249 
1250 	if (g_str_equal (action, "OVERWRITE"))
1251 	{
1252 		keyb_save_new_layout ();
1253 		keyb_set_keyboard_layouts ();
1254 		keyb_update_combos ("combobox_kbd_country", "combobox_kbd_variant");
1255 		gtk_widget_hide (get_wg ("window_keyboard"));
1256 	}
1257 
1258 	else if (g_str_equal (action, "REMOVE"))
1259 	{
1260 		keyb_remove_user_layout ();
1261 	}
1262 
1263 	else if (g_str_equal (action, "RESET_ALL"))
1264 	{
1265 		file = g_build_filename (main_path_stats (), "stat_basic.txt", NULL);
1266 		g_unlink (file);
1267 		g_free (file);
1268 		basic_set_lesson (1);
1269 		file = g_build_filename (main_path_stats (), "stat_adapt.txt", NULL);
1270 		g_unlink (file);
1271 		g_free (file);
1272 		file = g_build_filename (main_path_stats (), "stat_velo.txt", NULL);
1273 		g_unlink (file);
1274 		g_free (file);
1275 		file = g_build_filename (main_path_stats (), "stat_fluid.txt", NULL);
1276 		g_unlink (file);
1277 		g_free (file);
1278 		file = g_build_filename (main_path_stats (), "scores_fluid.txt", NULL);
1279 		g_unlink (file);
1280 		g_free (file);
1281 
1282 		accur_reset ();
1283 		accur_close ();
1284 		accur_init ();
1285 
1286 		on_combobox_stat_type_changed (NULL, NULL);
1287 	}
1288 	else if (g_str_equal (action, "RESET"))
1289 	{
1290 		tmp = g_strdup (tutor_get_type_name ());
1291 		for (msg = tmp; tmp[0] != '\0'; tmp++) tmp[0] = g_ascii_toupper (tmp[0]);
1292 		tmp = msg;
1293 
1294 		if (g_str_equal (tmp, "BASIC"))
1295 		{
1296 			file = g_build_filename (main_path_stats (), "stat_basic.txt", NULL);
1297 			g_unlink (file);
1298 			g_free (file);
1299 			basic_set_lesson (1);
1300 		}
1301 		else if (g_str_equal (tmp, "ADAPT"))
1302 		{
1303 			if (gtk_combo_box_get_active (GTK_COMBO_BOX (get_wg ("combobox_stat_type"))) == 2)
1304 			{
1305 				accur_terror_reset ();
1306 				accur_close ();
1307 				accur_init ();
1308 			}
1309 			else if (gtk_combo_box_get_active (GTK_COMBO_BOX (get_wg ("combobox_stat_type"))) == 3)
1310 			{
1311 				accur_ttime_reset ();
1312 				accur_close ();
1313 				accur_init ();
1314 			}
1315 			else
1316 			{
1317 				file = g_build_filename (main_path_stats (), "stat_adapt.txt", NULL);
1318 				g_unlink (file);
1319 				g_free (file);
1320 			}
1321 		}
1322 		else if (g_str_equal (tmp, "VELO"))
1323 		{
1324 			file = g_build_filename (main_path_stats (), "stat_velo.txt", NULL);
1325 			g_unlink (file);
1326 			g_free (file);
1327 		}
1328 		else if (g_str_equal (tmp, "FLUID"))
1329 		{
1330 			file = g_build_filename (main_path_stats (), "stat_fluid.txt", NULL);
1331 			g_unlink (file);
1332 			g_free (file);
1333 			file = g_build_filename (main_path_stats (), "scores_fluid.txt", NULL);
1334 			g_unlink (file);
1335 			g_free (file);
1336 		}
1337 		on_combobox_stat_type_changed (NULL, NULL);
1338 	}
1339 	else
1340 		g_warning ("No valid action selected for 'yes' confirm-button: %s", action);
1341 
1342 	gtk_widget_hide (gtk_widget_get_toplevel (GTK_WIDGET (button)));
1343 }
1344 
1345 G_MODULE_EXPORT void
on_button_confirm_no_clicked(GtkButton * button,gpointer user_data)1346 on_button_confirm_no_clicked (GtkButton * button, gpointer user_data)
1347 {
1348 	gchar *action;
1349 	GtkWidget *wg;
1350 
1351 	wg = get_wg ("label_confirm_action");
1352 	action = (gchar *) gtk_label_get_text (GTK_LABEL (wg));
1353 
1354 	if (g_str_equal (action, "OVERWRITE"))
1355 		g_printf ("Not overwriting file %s.kbd\n", keyb_get_name ());
1356 
1357 	else if (g_str_equal (action, "REMOVE"))
1358 		g_printf ("Not removing file %s.kbd\n", keyb_get_name ());
1359 
1360 	gtk_widget_hide (gtk_widget_get_toplevel (GTK_WIDGET (button)));
1361 }
1362 
1363 /*******************************************************************************
1364  * Window hints management
1365  */
1366 G_MODULE_EXPORT void
on_window_hints_check_resize(GtkContainer * cont,gpointer user_data)1367 on_window_hints_check_resize (GtkContainer *cont, gpointer user_data)
1368 {
1369 	static int i;
1370 	static GTimer *wait = NULL;
1371 	gint dx, dy;
1372 
1373 	//if (callbacks_shield)
1374 		return;
1375 
1376 	if (wait == NULL)
1377 		wait = g_timer_new ();
1378 
1379 	if (g_timer_elapsed (wait, NULL) > 0.2)
1380 	{
1381 		g_timer_start (wait);
1382 		gtk_window_get_size (get_win ("window_hints"), &dx, &dy);
1383 		g_printf ("hints_check_resize: %04i x %04i (%i)\n", dx, dy, i++);
1384 	}
1385 }
1386 
1387 G_MODULE_EXPORT void
on_button_hints_close_clicked(GtkButton * but,gpointer user_data)1388 on_button_hints_close_clicked (GtkButton *but, gpointer user_data)
1389 {
1390 	window_save ("hints");
1391 	gtk_widget_hide (get_wg ("window_hints"));
1392 }
1393 
1394 /**********************************************************************
1395  * 6 - Charts window
1396  **********************************************************************/
1397 
1398 G_MODULE_EXPORT void
on_combobox_stat_module_changed(GtkComboBox * cmb,gpointer user_data)1399 on_combobox_stat_module_changed (GtkComboBox *cmb, gpointer user_data)
1400 {
1401 	static gchar *stat_title = NULL;
1402 	gchar *win_title;
1403 	gint i;
1404 	gchar *tmp;
1405 	GtkWindow *win;
1406 
1407 	if (callbacks_shield == TRUE)
1408 		return;
1409 
1410 	if (gtk_combo_box_get_active (cmb) < 0)
1411 		return;
1412 
1413 	win = GTK_WINDOW (get_wg ("window_stat"));
1414 	if (stat_title == NULL)
1415 	{
1416 		plot_initialize ();
1417 		stat_title = g_strdup (gtk_window_get_title (win));
1418 	}
1419 
1420 	if (user_data == NULL)
1421 		tutor_init (gtk_combo_box_get_active (cmb));
1422 
1423 	callbacks_shield_set (TRUE);
1424 	for (i = 0; i < 4; i++)
1425 		gtk_combo_box_text_remove (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")), 0);
1426 	tmp = g_strdup_printf ("%s (%%)", _("Accuracy"));
1427 	gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")), 0, tmp);
1428 	g_free (tmp);
1429 	tmp = g_strdup_printf ("%s %s", _("Speed"), _("(WPM)"));
1430 	gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")), 1, tmp);
1431 	g_free (tmp);
1432 
1433 	switch (gtk_combo_box_get_active (cmb))
1434 	{
1435 	case 0:
1436 		win_title = g_strdup_printf ("%s (%s)", stat_title, keyb_mode_get_name ());
1437 		break;
1438 	case 1:
1439 		win_title = g_strdup_printf ("%s (%s)", stat_title, keyb_mode_get_name ());
1440 		tmp = g_strdup_printf ("%s", _("Errors"));
1441 		gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")), 2, tmp);
1442 		g_free (tmp);
1443 		tmp = g_strdup_printf ("%s", _("Touch times (s)"));
1444 		gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")), 3, tmp);
1445 		g_free (tmp);
1446 		break;
1447 	case 2:
1448 		win_title = g_strdup_printf ("%s (%s)", stat_title, trans_get_current_language ());
1449 		break;
1450 	case 3:
1451 		win_title = g_strdup_printf ("%s (%s)", stat_title, trans_get_current_language ());
1452 		tmp = g_strdup_printf ("%s (%%)", _("Fluidity"));
1453 		gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")), 2, tmp);
1454 		g_free (tmp);
1455 		tmp = g_strdup_printf ("%s (0-10)", _("Score"));
1456 		gtk_combo_box_text_insert_text (GTK_COMBO_BOX_TEXT (get_wg ("combobox_stat_type")), 3, tmp);
1457 		g_free (tmp);
1458 		break;
1459 	default:
1460 		win_title = g_strdup (stat_title);
1461 	}
1462 
1463 	gtk_combo_box_set_active (GTK_COMBO_BOX (get_wg ("combobox_stat_type")), 0);
1464 	callbacks_shield_set (FALSE);
1465 
1466 	gtk_window_set_title (win, win_title);
1467 	g_free (win_title);
1468 	plot_draw_chart (1);
1469 }
1470 
1471 G_MODULE_EXPORT void
on_combobox_stat_type_changed(GtkComboBox * cmb,gpointer user_data)1472 on_combobox_stat_type_changed (GtkComboBox *cmb, gpointer user_data)
1473 {
1474 	static gboolean init = TRUE;
1475 	gint active;
1476 
1477 	if (callbacks_shield == TRUE)
1478 		return;
1479 
1480 	if (init)
1481 	{
1482 		plot_initialize ();
1483 		init = FALSE;
1484 	}
1485 
1486 	active = gtk_combo_box_get_active ( GTK_COMBO_BOX (get_wg ("combobox_stat_type")) );
1487 	if (active < 0)
1488 		return;
1489 
1490 	/* Adapt module (1) has special charts
1491 	 */
1492 	if (gtk_combo_box_get_active (GTK_COMBO_BOX (get_wg ("combobox_stat_module"))) == 1)
1493 		plot_draw_chart (active + (active < 2 ? 1 : 4));
1494 	else
1495 		plot_draw_chart (active + 1);
1496 }
1497 
1498 G_MODULE_EXPORT void
on_spinbutton_stat_lesson_value_changed(GtkSpinButton * spinbutton,gpointer user_data)1499 on_spinbutton_stat_lesson_value_changed (GtkSpinButton * spinbutton, gpointer user_data)
1500 {
1501 	if (callbacks_shield)
1502 		return;
1503 
1504 	plot_draw_chart (gtk_combo_box_get_active (GTK_COMBO_BOX (get_wg ("combobox_stat_type"))) + 1);
1505 }
1506 
1507 G_MODULE_EXPORT void
on_button_stat_reset_clicked(GtkButton * button,gpointer check)1508 on_button_stat_reset_clicked (GtkButton * button, gpointer check)
1509 {
1510 	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check)))
1511 		gtk_label_set_text (GTK_LABEL (get_wg ("label_confirm_action")), "RESET_ALL");
1512 	else
1513 		gtk_label_set_text (GTK_LABEL (get_wg ("label_confirm_action")), "RESET");
1514 	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), FALSE);
1515 
1516 	gtk_widget_show (get_wg ("dialog_confirm"));
1517 }
1518 
1519 G_MODULE_EXPORT void
on_button_stat_close_clicked(GtkButton * button,gpointer user_data)1520 on_button_stat_close_clicked (GtkButton * button, gpointer user_data)
1521 {
1522 	callbacks_shield_set (TRUE);
1523 	gtk_combo_box_set_active (GTK_COMBO_BOX ( get_wg ("combobox_stat_type")), 0 );
1524 	callbacks_shield_set (FALSE);
1525 	window_save ("stat");
1526 	gtk_widget_hide (gtk_widget_get_toplevel (GTK_WIDGET (button)));
1527 }
1528 
1529 G_MODULE_EXPORT void
on_databox_hovered(GtkDatabox * dbox,GdkEventMotion * event,gpointer user_data)1530 on_databox_hovered (GtkDatabox *dbox, GdkEventMotion *event, gpointer user_data)
1531 {
1532 	plot_pointer_update (event->x);
1533 }
1534 
1535 /**********************************************************************
1536  * 7 - Other texts popup
1537  **********************************************************************/
1538 
1539 G_MODULE_EXPORT void
on_treeview_other_realize(GtkWidget * widget,gpointer user_data)1540 on_treeview_other_realize (GtkWidget * widget, gpointer user_data)
1541 {
1542 	GtkListStore *list;
1543 	GtkCellRenderer *renderer;
1544 	GtkTreeViewColumn *column;
1545 
1546 	list = gtk_list_store_new (1, G_TYPE_STRING);
1547 	gtk_tree_view_set_model (GTK_TREE_VIEW (widget), GTK_TREE_MODEL (list));
1548 	renderer = gtk_cell_renderer_text_new ();
1549 	column = gtk_tree_view_column_new_with_attributes (":-)", renderer, "text", 0, NULL);
1550 	gtk_tree_view_append_column (GTK_TREE_VIEW (widget), column);
1551 }
1552 
1553 G_MODULE_EXPORT void
on_button_other_new_clicked(GtkButton * button,gpointer user_data)1554 on_button_other_new_clicked (GtkButton * button, gpointer user_data)
1555 {
1556 	gtk_widget_show (GTK_WIDGET (get_wg ("filechooser_tutor")));
1557 }
1558 
1559 G_MODULE_EXPORT void
on_button_other_cancel_clicked(GtkButton * button,gpointer user_data)1560 on_button_other_cancel_clicked (GtkButton * button, gpointer user_data)
1561 {
1562 	g_free (other_renaming_undo);
1563 	other_renaming_undo = g_strdup ("*");
1564 	gtk_widget_hide (get_wg ("popup_other"));
1565 }
1566 
1567 G_MODULE_EXPORT void
on_button_other_apply_clicked(GtkButton * button,gpointer user_data)1568 on_button_other_apply_clicked (GtkButton *button, gpointer user_data)
1569 {
1570 	gchar *tmp_name;
1571 	GtkTreeSelection *sel;
1572 	GtkTreeModel *store;
1573 	GtkTreeIter iter;
1574 
1575 	gtk_widget_hide (get_wg ("popup_other"));
1576 
1577 	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (get_wg ("treeview_other")));
1578 	if (gtk_tree_selection_get_selected (sel, &store, &iter) == FALSE)
1579 	{
1580 		tutor_set_query (QUERY_INTRO);
1581 		tutor_process_touch ('\0');
1582 		return;
1583 	}
1584 
1585 	tutor_other_rename (gtk_entry_get_text (GTK_ENTRY (get_wg ("entry_other_rename"))), other_renaming_undo);
1586 	g_free (other_renaming_undo);
1587 	other_renaming_undo = g_strdup ("*");
1588 
1589 	gtk_tree_model_get (store, &iter, 0, &tmp_name, -1);
1590 
1591 	if (tutor_get_type () == TT_VELO)
1592 	{
1593 		if (main_velo_txt ())
1594 			fluid_init_paragraph_list (tmp_name);
1595 		else
1596 			velo_init_dict (tmp_name);
1597 	}
1598 	else if (tutor_get_type () == TT_FLUID)
1599 		fluid_init_paragraph_list (tmp_name);
1600 
1601 	tutor_set_query (QUERY_INTRO);
1602 	tutor_process_touch ('\0');
1603 }
1604 
1605 G_MODULE_EXPORT void
on_button_other_paste_clicked(GtkButton * button,gpointer user_data)1606 on_button_other_paste_clicked (GtkButton * button, gpointer user_data)
1607 {
1608 	gchar *text;
1609 	GtkWidget *wg;
1610 	GtkListStore *list;
1611 
1612 	if (clipboard == NULL)
1613 		clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
1614 	if (clipboard_2 == NULL)
1615 		clipboard_2 = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
1616 
1617 	text = gtk_clipboard_wait_for_text (clipboard);
1618 	if (text == NULL)
1619 		text = gtk_clipboard_wait_for_text (clipboard_2);
1620 	if (text == NULL)
1621 	{
1622 		gdk_display_beep (gdk_display_get_default());
1623 		g_message ("No text in the Clipboard");
1624 		return;
1625 	}
1626 
1627 	wg = get_wg ("treeview_other");
1628 	list = GTK_LIST_STORE ( gtk_tree_view_get_model (GTK_TREE_VIEW (wg)) );
1629 	if (tutor_get_type () == TT_VELO)
1630 	{
1631 		if (main_velo_txt ())
1632 		{
1633 			fluid_text_write_to_file (text);
1634 			tutor_load_list_other (".paragraphs", list);
1635 		}
1636 		else
1637 		{
1638 			velo_text_write_to_file (text, TRUE);
1639 			tutor_load_list_other (".words", list);
1640 		}
1641 	}
1642 	else if (tutor_get_type () == TT_FLUID)
1643 	{
1644 		fluid_text_write_to_file (text);
1645 		tutor_load_list_other (".paragraphs", list);
1646 	}
1647 	g_free (text);
1648 
1649 	g_free (other_renaming_undo);
1650 	other_renaming_undo = g_strdup ("*");
1651 	gtk_widget_hide (get_wg ("popup_other"));
1652 }
1653 
1654 G_MODULE_EXPORT void
on_button_other_remove_clicked(GtkButton * button,gpointer user_data)1655 on_button_other_remove_clicked (GtkButton * button, gpointer user_data)
1656 {
1657 	gchar *tmp_str;
1658 	gchar *tmp_name;
1659 	GtkTreeSelection *sel;
1660 	GtkTreeModel *store;
1661 	GtkTreeIter iter;
1662 
1663 	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (get_wg ("treeview_other")));
1664 	if (gtk_tree_selection_get_selected (sel, &store, &iter) == FALSE)
1665 	{
1666 		gdk_display_beep (gdk_display_get_default());
1667 		return;
1668 	}
1669 
1670 	gtk_tree_model_get (store, &iter, 0, &tmp_str, -1);
1671 
1672 	if (g_str_equal (tmp_str, OTHER_DEFAULT))
1673 	{
1674 		gdk_display_beep (gdk_display_get_default());
1675 		g_free (tmp_str);
1676 		return;
1677 	}
1678 
1679 	tmp_name = g_build_filename (main_path_user (), tmp_str, NULL);
1680 	g_free (tmp_str);
1681 
1682 	if (tutor_get_type () == TT_VELO)
1683 		if (main_velo_txt ())
1684 			tmp_str = g_strconcat (tmp_name, ".paragraphs", NULL);
1685 		else
1686 			tmp_str = g_strconcat (tmp_name, ".words", NULL);
1687 	else if (tutor_get_type () == TT_FLUID)
1688 		tmp_str = g_strconcat (tmp_name, ".paragraphs", NULL);
1689 	g_unlink (tmp_str);
1690 	g_free (tmp_str);
1691 	g_free (tmp_name);
1692 
1693 	gtk_list_store_remove (GTK_LIST_STORE (store), &iter);
1694 	gtk_widget_set_sensitive (get_wg ("button_other_remove"), FALSE);
1695 	gtk_widget_set_sensitive (get_wg ("label_other_rename"), FALSE);
1696 	gtk_widget_set_sensitive (get_wg ("entry_other_rename"), FALSE);
1697 }
1698 
1699 G_MODULE_EXPORT void
on_entry_other_rename_changed(GtkEditable * editable,gint start_pos,gint end_pos,gpointer user_data)1700 on_entry_other_rename_changed (GtkEditable *editable, gint start_pos, gint end_pos, gpointer user_data)
1701 {
1702 	gchar *str;
1703 	GtkTreeSelection *sel;
1704 	GtkTreeModel *store;
1705 	GtkTreeIter iter;
1706 
1707 	if (callbacks_shield)
1708 		return;
1709 
1710 	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (get_wg ("treeview_other")));
1711 	if (gtk_tree_selection_get_selected (sel, &store, &iter) == FALSE)
1712 	{
1713 		gtk_widget_set_sensitive (get_wg ("label_other_rename"), FALSE);
1714 		gtk_widget_set_sensitive (get_wg ("entry_other_rename"), FALSE);
1715 		return;
1716 	}
1717 	str = (gchar*) gtk_entry_get_text (GTK_ENTRY (get_wg ("entry_other_rename")));
1718 	if (strlen (str) > 0)
1719 		gtk_list_store_set (GTK_LIST_STORE (store), &iter, 0, str, -1);
1720 	else
1721 		gtk_list_store_set (GTK_LIST_STORE (store), &iter, 0, other_renaming_undo, -1);
1722 }
1723 
1724 G_MODULE_EXPORT void
on_entry_other_rename_activate(GtkEntry * entry,gpointer user_data)1725 on_entry_other_rename_activate (GtkEntry *entry, gpointer user_data)
1726 {
1727 	on_button_other_apply_clicked (NULL, NULL);
1728 }
1729 
1730 G_MODULE_EXPORT void
on_treeview_other_cursor_changed(GtkTreeView * treeview,gpointer user_data)1731 on_treeview_other_cursor_changed (GtkTreeView *treeview, gpointer user_data)
1732 {
1733 	gchar *tmp_str;
1734 	GtkTreeSelection *sel;
1735 	GtkTreeModel *store;
1736 	GtkTreeIter iter;
1737 	GtkEntry *entry;
1738 
1739 	sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (get_wg ("treeview_other")));
1740 	if (gtk_tree_selection_get_selected (sel, &store, &iter) == FALSE)
1741 	{
1742 		gtk_widget_set_sensitive (get_wg ("button_other_apply"), FALSE);
1743 		return;
1744 	}
1745 	gtk_widget_set_sensitive (get_wg ("button_other_apply"), TRUE);
1746 
1747 	entry = GTK_ENTRY (get_wg ("entry_other_rename"));
1748 
1749 	if (other_renaming_undo == NULL)
1750 		other_renaming_undo = g_strdup ("*");
1751 	else
1752 		tutor_other_rename (gtk_entry_get_text (entry), other_renaming_undo);
1753 
1754 	callbacks_shield_set (TRUE);
1755 	g_free (other_renaming_undo);
1756 	gtk_tree_model_get (store, &iter, 0, &tmp_str, -1);
1757 	if (g_str_equal (tmp_str, OTHER_DEFAULT))
1758 	{
1759 		gtk_entry_set_text (entry, "");
1760 		gtk_widget_set_sensitive (get_wg ("button_other_remove"), FALSE);
1761 		gtk_widget_set_sensitive (get_wg ("label_other_rename"), FALSE);
1762 		gtk_widget_set_sensitive (get_wg ("entry_other_rename"), FALSE);
1763 		other_renaming_undo = g_strdup ("*");
1764 	}
1765 	else
1766 	{
1767 		gtk_widget_set_sensitive (get_wg ("button_other_remove"), TRUE);
1768 		gtk_widget_set_sensitive (get_wg ("label_other_rename"), TRUE);
1769 		gtk_widget_set_sensitive (get_wg ("entry_other_rename"), TRUE);
1770 		gtk_entry_set_text (entry, tmp_str);
1771 		other_renaming_undo = g_strdup (tmp_str);
1772 	}
1773 	callbacks_shield_set (FALSE);
1774 
1775 }
1776 
1777 G_MODULE_EXPORT void
on_treeview_other_row_activated(GtkTreeView * treeview,GtkTreePath * path,GtkTreeViewColumn * column,gpointer user_data)1778 on_treeview_other_row_activated (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
1779 {
1780 	on_button_other_apply_clicked (NULL, NULL);
1781 }
1782 
1783 G_MODULE_EXPORT void
on_button_filechooser_open_clicked(GtkButton * button,gpointer user_data)1784 on_button_filechooser_open_clicked (GtkButton *button, gpointer user_data)
1785 {
1786 	gchar *tmp_path;
1787 	GtkWidget *wg;
1788 	GtkListStore *list;
1789 
1790 	tmp_path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (get_wg ("filechooser_tutor")));
1791 	if (tutor_get_type () == TT_VELO)
1792 		velo_create_dict (tmp_path, TRUE);
1793 	else if (tutor_get_type () == TT_FLUID)
1794 		fluid_copy_text_file (tmp_path);
1795 	g_free (tmp_path);
1796 
1797 	wg = get_wg ("treeview_other");
1798 	list = GTK_LIST_STORE ( gtk_tree_view_get_model (GTK_TREE_VIEW (wg)) );
1799 	if (tutor_get_type () == TT_VELO)
1800 		if (main_velo_txt ())
1801 			tutor_load_list_other (".paragraphs", list);
1802 		else
1803 			tutor_load_list_other (".words", list);
1804 	else if (tutor_get_type () == TT_FLUID)
1805 		tutor_load_list_other (".paragraphs", list);
1806 
1807 	g_free (other_renaming_undo);
1808 	other_renaming_undo = g_strdup ("*");
1809 	gtk_widget_hide (get_wg ("popup_other"));
1810 	gtk_widget_hide (get_wg ("filechooser_tutor"));
1811 }
1812 
1813 G_MODULE_EXPORT void
on_button_filechooser_cancel_clicked(GtkButton * button,gpointer user_data)1814 on_button_filechooser_cancel_clicked (GtkButton *button, gpointer user_data)
1815 {
1816 	gtk_widget_hide (get_wg ("filechooser_tutor"));
1817 }
1818 
1819 /**********************************************************************
1820  * 8 - Mangling with the windows positions
1821  **********************************************************************/
1822 
1823 G_MODULE_EXPORT void
on_window_main_show(GtkWidget * wg,gpointer user_data)1824 on_window_main_show (GtkWidget *wg, gpointer user_data)
1825 {
1826 	window_restore ("main");
1827 }
1828 
1829 G_MODULE_EXPORT void
on_window_tutor_show(GtkWidget * wg,gpointer user_data)1830 on_window_tutor_show (GtkWidget *wg, gpointer user_data)
1831 {
1832 	window_restore ("tutor");
1833 }
1834 
1835 G_MODULE_EXPORT void
on_window_hints_show(GtkWidget * wg,gpointer user_data)1836 on_window_hints_show (GtkWidget *wg, gpointer user_data)
1837 {
1838 	window_restore ("hints");
1839 }
1840 
1841 G_MODULE_EXPORT void
on_window_top10_show(GtkWidget * wg,gpointer user_data)1842 on_window_top10_show (GtkWidget *wg, gpointer user_data)
1843 {
1844 	window_restore ("top10");
1845 }
1846 
1847 G_MODULE_EXPORT void
on_window_stat_show(GtkWidget * wg,gpointer user_data)1848 on_window_stat_show (GtkWidget *wg, gpointer user_data)
1849 {
1850 	window_restore ("stat");
1851 }
1852 
1853 void
window_restore(gchar * who)1854 window_restore (gchar *who)
1855 {
1856 	gint x, y;
1857 	gchar *str;
1858 
1859 	str = g_strconcat (who, "_X", NULL);
1860 	if (main_preferences_exist ("windows", str))
1861 		x = main_preferences_get_int ("windows", str);
1862 	else
1863 	{
1864 		g_free (str);
1865 		return;
1866 	}
1867 	g_free (str);
1868 
1869 	str = g_strconcat (who, "_Y", NULL);
1870 	if (main_preferences_exist ("windows", str))
1871 		y = main_preferences_get_int ("windows", str);
1872 	else
1873 	{
1874 		g_free (str);
1875 		return;
1876 	}
1877 	g_free (str);
1878 
1879 	str = g_strconcat ("window_", who, NULL);
1880 	gtk_window_move (get_win (str), x, y);
1881 	g_free (str);
1882 }
1883 
1884 void
window_save(gchar * who)1885 window_save (gchar *who)
1886 {
1887 	gint x, y;
1888 	gchar *str;
1889 
1890 	str = g_strconcat ("window_", who, NULL);
1891 	gtk_window_get_position (get_win (str), &x, &y);
1892 	g_free (str);
1893 
1894 	str = g_strconcat (who, "_X", NULL);
1895 	main_preferences_set_int ("windows", str, x);
1896 	g_free (str);
1897 
1898 	str = g_strconcat (who, "_Y", NULL);
1899 	main_preferences_set_int ("windows", str, y);
1900 	g_free (str);
1901 }
1902