Commit a0ab60f0 authored by Bruno Coudoin's avatar Bruno Coudoin Committed by Bruno Coudoin

Missing files from Miguel

svn path=/trunk/; revision=3701
parent 0bee89d3
/* gcompris - board_config_combo.c
*
* Copyright (C) 2009 Miguel De Izarra
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "board_config.h"
#include "board_config_common.h"
#include <string.h>
/***********************************************/
/* L10n */
/***********************************************/
/** \brief return the list of locales in which GCompris has been translated
* even partialy.
*
* \note The list is calculated at the first call and must not be freed.
* Uppon next call, the same list is returned.
*
* \return a list containing the locales we suport
*/
GList*
gc_locale_gets_list(){
static GList *gcompris_locales_list = NULL;
GcomprisProperties *properties = gc_prop_get();
GDir *textdomain_dir;
GError **error = NULL;
GList *locales = NULL;
if(gcompris_locales_list)
return(gcompris_locales_list);
/* There is no english locale but it exists anyway */
locales = g_list_append(locales, g_strdup("en"));
textdomain_dir = g_dir_open (properties->package_locale_dir, 0, error);
const gchar *fname;
gchar *fname_abs;
gchar *catalog;
if(textdomain_dir) {
while ((fname = g_dir_read_name(textdomain_dir))) {
fname_abs = g_strdup_printf("%s/%s", properties->package_locale_dir, fname);
if (!g_file_test(fname_abs, G_FILE_TEST_IS_DIR))
continue;
catalog = g_strdup_printf("%s/LC_MESSAGES/gcompris.mo", fname_abs);
if (g_file_test(catalog, G_FILE_TEST_EXISTS)){
locales = g_list_append(locales, g_strdup(fname));
}
g_free (fname_abs);
g_free(catalog);
}
g_dir_close (textdomain_dir);
}
#if DEBUG /* to find locales list when run from src directory */
if(g_file_test("po", G_FILE_TEST_IS_DIR))
{
textdomain_dir= g_dir_open("po",0, error);
while((fname = g_dir_read_name(textdomain_dir)))
{
if(g_str_has_suffix(fname,".po"))
locales = g_list_append(locales, g_strndup(fname,2));
}
g_dir_close(textdomain_dir);
}
#endif
/* Save it for next call */
gcompris_locales_list = locales;
return locales;
}
void
gc_board_config_combo_locales_changed(GtkComboBox *combobox,
gpointer data)
{
_gc_boardconf_key*u = (_gc_boardconf_key*)data;
gchar *the_key = g_strdup((gchar *)u->key);
gchar *value;
gint index = gtk_combo_box_get_active (combobox);
if (index == 0)
/* Default value of gcompris selected */
value = g_strdup ("NULL");
else
value = _get_active_text (combobox);
g_hash_table_replace(u->config->hash_conf, (gpointer) the_key, (gpointer) value);
}
/* key = "locale" */
GtkComboBox*
gc_board_config_combo_locales(GcomprisBoardConf *config, gchar *init)
{
g_return_val_if_fail(config, NULL);
GtkWidget *combobox;
GtkWidget *hbox = gtk_hbox_new (FALSE, 8);
GList *list, *strings;
GtkWidget *label_combo;
gint init_index = 0;
strings = gc_locale_gets_list();
strings = g_list_prepend( strings, _("Default"));
if (init)
init_index = g_list_position(strings,
g_list_find_custom(strings,
(gconstpointer) init,
(GCompareFunc) strcmp));
if (init_index < 0)
init_index=0;
gtk_widget_show(hbox);
gtk_box_pack_start (GTK_BOX(config->main_conf_box),
hbox,
FALSE,
FALSE,
0);
/* Label */
label_combo = gtk_label_new ((gchar *)NULL);
gtk_widget_show(label_combo);
gtk_box_pack_start (GTK_BOX(hbox),
label_combo,
FALSE,
FALSE,
0);
gtk_label_set_justify (GTK_LABEL(label_combo),
GTK_JUSTIFY_RIGHT);
gtk_label_set_markup (GTK_LABEL(label_combo),
_("Select the language\n to use in the board"));
combobox = gtk_combo_box_new_text();
gtk_widget_show(combobox);
gtk_box_pack_start (GTK_BOX(hbox),
combobox,
FALSE,
FALSE,
0);
for (list = strings; list != NULL; list = list->next)
gtk_combo_box_append_text (GTK_COMBO_BOX(combobox),
list->data);
if (g_list_length(strings) > COMBOBOX_COL_MAX)
gtk_combo_box_set_wrap_width (GTK_COMBO_BOX(combobox),
g_list_length(strings) / COMBOBOX_COL_MAX +1 );
gtk_combo_box_set_active (GTK_COMBO_BOX(combobox),
init_index);
_gc_boardconf_key *u = g_malloc0(sizeof(_gc_boardconf_key));
u -> key = g_strdup("locale");
u -> config = config;
g_signal_connect(G_OBJECT(combobox),
"changed",
G_CALLBACK(gc_board_config_combo_locales_changed),
u);
g_signal_connect(G_OBJECT(combobox),
"destroy",
G_CALLBACK(_gc_destroy_boardconf_key),
u);
return GTK_COMBO_BOX(combobox);
}
void
gc_board_config_combo_drag_changed(GtkComboBox *combobox,
gpointer data)
{
_gc_boardconf_key *u = (_gc_boardconf_key*)data;
gchar *the_key = g_strdup((gchar *)u->key);
gchar *value;
gint index = gtk_combo_box_get_active (combobox);
if (index == 0)
/* Default value of gcompris selected */
value = g_strdup ("NULL");
else
value = g_strdup_printf("%d", index);
g_hash_table_replace(u->config->hash_conf, (gpointer) the_key, (gpointer) value);
}
/* key = "locale" */
GtkComboBox*
gc_board_config_combo_drag(GcomprisBoardConf *config, gint init)
{
g_return_val_if_fail(config, NULL);
GtkWidget *combobox;
GtkWidget *hbox = gtk_hbox_new (FALSE, 8);
GList *list, *strings;
GtkWidget *label_combo;
gint init_index;
strings = NULL;
strings = g_list_prepend( strings, _("Global GCompris mode"));
strings = g_list_append( strings, _("Normal"));
strings = g_list_append( strings, _("2 clicks"));
strings = g_list_append( strings, _("both modes"));
if (init < 0)
init_index =0;
else
init_index = init;
gtk_widget_show(hbox);
gtk_box_pack_start (GTK_BOX(config->main_conf_box),
hbox,
FALSE,
FALSE,
0);
/* Label */
label_combo = gtk_label_new ((gchar *)NULL);
gtk_widget_show(label_combo);
gtk_box_pack_start (GTK_BOX(hbox),
label_combo,
FALSE,
FALSE,
0);
gtk_label_set_justify (GTK_LABEL(label_combo),
GTK_JUSTIFY_RIGHT);
gtk_label_set_markup (GTK_LABEL(label_combo),
_("Select the drag and drop mode\n to use in the board"));
combobox = gtk_combo_box_new_text();
gtk_widget_show(combobox);
gtk_box_pack_start (GTK_BOX(hbox),
combobox,
FALSE,
FALSE,
0);
for (list = strings; list != NULL; list = list->next)
gtk_combo_box_append_text (GTK_COMBO_BOX(combobox),
list->data);
if (g_list_length(strings) > COMBOBOX_COL_MAX)
gtk_combo_box_set_wrap_width (GTK_COMBO_BOX(combobox),
g_list_length(strings) / COMBOBOX_COL_MAX +1 );
gtk_combo_box_set_active (GTK_COMBO_BOX(combobox),
init_index);
_gc_boardconf_key *u = g_malloc0(sizeof(_gc_boardconf_key));
u->key = g_strdup("drag_mode");
u->config = config;
g_signal_connect(G_OBJECT(combobox),
"changed",
G_CALLBACK(gc_board_config_combo_drag_changed),
u);
g_signal_connect(G_OBJECT(combobox),
"destroy",
G_CALLBACK(_gc_destroy_boardconf_key),
u);
return GTK_COMBO_BOX(combobox);
}
static gchar *current_locale = NULL;
void
gc_locale_change(gchar *locale)
{
if (!locale)
return;
if (strcmp(locale, "NULL") == 0){
gc_locale_reset();
return;
}
current_locale = g_strdup(gc_locale_get());
gc_locale_set(locale);
}
void
gc_locale_reset()
{
if (current_locale == NULL)
return;
gc_locale_change(current_locale);
g_free(current_locale);
current_locale = NULL;
}
/** \brief Search the given file for each locale and returns the locale list
*
* \param file: the file to search. In order to work, you need to provide a
* filename that includes a $LOCALE in it like:
* voices/$LOCALE/colors/blue.ogg
*
* \return a list of locale
*/
GList*
gc_locale_gets_asset_list(const gchar *filename)
{
GList *locales, *list, *locales_asset = NULL;
gchar *abs_filename;
locales = gc_locale_gets_list();
for (list = locales; list != NULL; list = list->next)
{
gchar **tmp;
/* Check there is a $LOCALE to replace */
if((tmp = g_strsplit(filename, "$LOCALE", -1)))
{
gchar locale[6];
gchar *filename2;
/* try with the locale */
g_strlcpy(locale, list->data, sizeof(locale));
filename2 = g_strjoinv(locale, tmp);
g_warning("trying locale file '%s'\n", filename2);
abs_filename = gc_file_find_absolute(filename2);
g_free(filename2);
g_strfreev(tmp);
}
else
{
abs_filename = gc_file_find_absolute(filename);
}
if(abs_filename)
{
/* It would be cleaner to provide the real locale name but then we need a way
* to get back the locale code from it's name and from the boards
*
* locales_asset = g_list_append(locales_asset, gc_locale_get_name(list->data));
*
*/
locales_asset = g_list_append(locales_asset, list->data);
g_free(abs_filename);
}
}
return locales_asset;
}
/* key = "locale_sound" */
GtkComboBox *gc_board_config_combo_locales_asset(GcomprisBoardConf *config, const gchar *label,
gchar *init,
const gchar *file)
{
g_return_val_if_fail(config, NULL);
GtkWidget *combobox;
GtkWidget *hbox = gtk_hbox_new (FALSE, 8);
GList *list, *strings;
GtkWidget *label_combo;
gint init_index = 0;
strings = gc_locale_gets_asset_list(file);
strings = g_list_prepend( strings, _("Default"));
if (init)
{
init_index = g_list_position(strings,
g_list_find_custom(strings,
(gconstpointer)init,
(GCompareFunc) strcmp));
}
if (init_index < 0)
init_index=0;
gtk_widget_show(hbox);
gtk_box_pack_start (GTK_BOX(config->main_conf_box),
hbox,
FALSE,
FALSE,
0);
/* Label */
label_combo = gtk_label_new ((gchar *)NULL);
gtk_widget_show(label_combo);
gtk_box_pack_start (GTK_BOX(hbox),
label_combo,
FALSE,
FALSE,
0);
gtk_label_set_justify (GTK_LABEL(label_combo),
GTK_JUSTIFY_RIGHT);
gtk_label_set_markup (GTK_LABEL(label_combo),
label);
combobox = gtk_combo_box_new_text();
gtk_widget_show(combobox);
gtk_box_pack_start (GTK_BOX(hbox),
combobox,
FALSE,
FALSE,
0);
for (list = strings; list != NULL; list = list->next)
gtk_combo_box_append_text(GTK_COMBO_BOX(combobox),
list->data);
if (g_list_length(strings) > COMBOBOX_COL_MAX)
gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(combobox),
g_list_length(strings) / COMBOBOX_COL_MAX +1 );
gtk_combo_box_set_active (GTK_COMBO_BOX(combobox),
init_index);
g_signal_connect(G_OBJECT(combobox),
"changed",
G_CALLBACK(gc_board_config_combo_locales_changed),
"locale_sound");
return GTK_COMBO_BOX(combobox);
}
static void
_combo_box_changed(GtkComboBox *combobox,
gpointer data)
{
_gc_boardconf_key *u = (_gc_boardconf_key*)data;
gchar *the_key = g_strdup(u->key);
gchar *value = g_strdup_printf("%s", _get_active_text (combobox));
g_hash_table_replace(u->config->hash_conf, (gpointer) the_key, (gpointer) value);
}
GtkComboBox *gc_board_config_combo_box(GcomprisBoardConf *config, const gchar *label, GList *strings, gchar *key, gchar *init)
{
g_return_val_if_fail(config, NULL);
check_key(key);
GtkWidget *combobox;
GtkWidget *hbox = gtk_hbox_new (FALSE, 8);
GList *list;
GtkWidget *label_combo;
gint init_index = 0;
if (init)
init_index = g_list_position ( strings, g_list_find_custom ( strings,(gconstpointer) init, (GCompareFunc) strcmp));
if (init_index < 0)
init_index=0;
gtk_widget_show(hbox);
gtk_box_pack_start (GTK_BOX(config->main_conf_box),
hbox,
FALSE,
FALSE,
0);
/* Label */
label_combo = gtk_label_new ((gchar *)NULL);
gtk_widget_show(label_combo);
gtk_box_pack_start (GTK_BOX(hbox),
label_combo,
FALSE,
FALSE,
0);
gtk_label_set_justify (GTK_LABEL(label_combo),
GTK_JUSTIFY_RIGHT);
gtk_label_set_markup (GTK_LABEL(label_combo),
(const gchar *)label);
combobox = gtk_combo_box_new_text();
gtk_widget_show(combobox);
gtk_box_pack_start (GTK_BOX(hbox),
combobox,
FALSE,
FALSE,
0);
for (list = strings; list != NULL; list = list->next)
gtk_combo_box_append_text (GTK_COMBO_BOX(combobox),
list->data);
if (g_list_length(strings) > COMBOBOX_COL_MAX)
gtk_combo_box_set_wrap_width (GTK_COMBO_BOX(combobox),
g_list_length(strings) / COMBOBOX_COL_MAX +1 );
gtk_combo_box_set_active (GTK_COMBO_BOX(combobox),
init_index);
_gc_boardconf_key *u = g_malloc0(sizeof(_gc_boardconf_key));
u -> key = g_strdup(key);
u -> config = config;
g_signal_connect(G_OBJECT(combobox),
"changed",
G_CALLBACK(_combo_box_changed),
u);
g_signal_connect(G_OBJECT(combobox),
"destroy",
G_CALLBACK(_gc_destroy_boardconf_key),
u);
return GTK_COMBO_BOX(combobox);
}
/* gcompris - board_config_common.c
*
* Copyright (C) 2009 Miguel De Izarra
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/* code get from gtk */
/* included here to not depend on gtk 2.6 */
#include "gcompris.h"
#include "board_config_common.h"
#include <string.h>
gchar *_get_active_text (GtkComboBox *combo_box)
{
GtkTreeIter iter;
gchar *text = NULL;
g_return_val_if_fail (GTK_IS_LIST_STORE (gtk_combo_box_get_model (combo_box)), NULL);
if (gtk_combo_box_get_active_iter (combo_box, &iter))
gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter,
0, &text, -1);
return text;
}
void check_key(gchar *key)
{
if ((strcmp(key, "locale") == 0) ||
(strcmp(key, "locale_sound") == 0) ||
(strcmp(key, "wordlist") == 0))
g_error(" Key %s forbiden ! Change !", key);
}
void _gc_destroy_boardconf_key(GtkWidget *widget, gpointer data)
{
_gc_boardconf_key *u = (_gc_boardconf_key*)data;
g_free(u->key);
g_free(u);
}
/* gcompris - board_config_common.h
*
* Copyright (C) 2009 Miguel De Izarra
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BOARD_CONFIG_COMMON_H
#define BOARD_CONFIG_COMMON_H
gchar *_get_active_text (GtkComboBox *combo_box);
void check_key(gchar *key);
typedef struct
{
gchar *key;
GcomprisBoardConf *config;
} _gc_boardconf_key;
void _gc_destroy_boardconf_key(GtkWidget*w, gpointer data);
#define COMBOBOX_COL_MAX 15
#endif
/* gcompris - board_config_radio.c
*
* Copyright (C) 2009 Miguel De Izarra
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "board_config.h"
#include "board_config_common.h"
#include <string.h>
typedef struct
{
GcomprisBoardConf *config;
GHashTable *hash_radio;
gchar *key;
GSList *radio_group ;
GtkWidget *radio_box;
gchar *radio_key;
gchar *radio_init;
} Gconfig_radio;
static void radio_changed(GtkToggleButton *togglebutton,
gpointer data)
{
gboolean state = gtk_toggle_button_get_active (togglebutton);
Gconfig_radio *u = (Gconfig_radio*)data;
gchar *h_key;
gchar *h_value;
if (state){
h_key = g_strdup (u->radio_key);
h_value = g_strdup((gchar *) u->key);
g_hash_table_replace (u->config->hash_conf, h_key, h_value);
}
}
static void radio_destroy(GtkWidget*w, gpointer data)
{
Gconfig_radio *u = (Gconfig_radio*)data;
/* simply free key and Gconfig_radio */
g_free(u->key);
g_free(u);
}
static void
create_radio_buttons(gpointer key,
gpointer value,
gpointer data)
{
Gconfig_radio * u = (Gconfig_radio*)data;
Gconfig_radio *u_copy;
GtkWidget *radio_button;
radio_button = gtk_radio_button_new_with_label (u->radio_group,
(const gchar *) g_strdup(value));
gtk_box_pack_start (GTK_BOX (u->radio_box), radio_button, TRUE, TRUE, 2);
gtk_widget_show (GTK_WIDGET (radio_button));
u->radio_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_button));
/* make a copy of Gconfig_radio, and set key */
u_copy = g_memdup(u,sizeof(Gconfig_radio));
u_copy->key = g_strdup ((gchar *)key);
if (strcmp(u_copy->key, u->radio_init)==0)
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(radio_button),TRUE);
g_signal_connect(G_OBJECT(radio_button),
"toggled",
G_CALLBACK(radio_changed),
(gpointer) u_copy);
g_signal_connect(G_OBJECT(radio_button),
"destroy",
G_CALLBACK(radio_destroy),
(gpointer) u_copy);