Commit 3d364e1b authored by Yves Combe's avatar Yves Combe

gcompris_configuration_window and gcompris_boolean_box: two usefull functions...

gcompris_configuration_window and gcompris_boolean_box: two usefull functions for boards configuration, with python bindings
samples in tuxpaint.py and colors.c
parent be19cd50
2005-07-24 Yves Combe,,, <yves@ycombe.net>
gcompris_configuration_window and gcompris_boolean_box: two usefull functions for boards configuration, with python bindings
samples in tuxpaint.py and colors.c
* src/boards/colors.c: (conf_apply), (colors_config_start),
(colors_config_stop):
* src/boards/py-gcompris-board.c: (pyGcomprisBoardType_getattr):
* src/boards/py-mod-admin.c: (py_board_config_start):
* src/boards/py-mod-gcompris.c: (pair_in_dict), (hash_to_dict),
(py_gcompris_get_board_conf), (py_gcompris_get_conf),
(py_gcompris_set_board_conf), (pyGcomprisConfCallback),
(py_gcompris_configuration_window), (py_gcompris_boolean_box):
* src/boards/python.c: (pythonboard_config_start):
* src/boards/python/admin/board_list.py:
* src/boards/python/tuxpaint.py:
* src/gcompris/board_config.c: (board_config_start),
(gcompris_close_board_conf), (gcompris_apply_board_conf),
(gcompris_configuration_window), (gcompris_boolean_box_toggled),
(gcompris_boolean_box):
* src/gcompris/board_config.h:
* src/gcompris/gcompris_db.c: (gcompris_get_conf),
(gcompris_get_board_conf):
* src/gcompris/gcompris_db.h:
* src/gcompris/plugin.h:
2005-07-23 Bruno coudoin <bruno.coudoin@free.fr>
Completed support for Nepal
......
......@@ -37,6 +37,9 @@ static void process_ok(void);
static void highlight_selected(int);
static void game_won(void);
static void repeat(void);
static void colors_config_start(GcomprisBoard *agcomprisBoard,
GcomprisProfile *aProfile);
static void colors_config_stop(void);
/* ================================================================ */
static GnomeCanvasGroup *boardRootItem = NULL;
......@@ -87,8 +90,8 @@ static BoardPlugin menu_bp =
NULL,//set_level,
NULL,
repeat,
NULL,
NULL
colors_config_start,
colors_config_stop
};
/* =====================================================================
......@@ -96,6 +99,37 @@ static BoardPlugin menu_bp =
* =====================================================================*/
GET_BPLUGIN_INFO(colors)
/* ======================= */
/* = config_start = */
/* ======================= */
static GtkVBox *config_vbox = NULL;
static GcomprisConfCallback conf_apply(GHashTable *table)
{
printf ("Config Hashtable size %d\n", g_hash_table_size(table));
}
static void
colors_config_start(GcomprisBoard *agcomprisBoard,
GcomprisProfile *aProfile)
{
gcompris_configuration_window(conf_apply);
gcompris_boolean_box("Test Check Box", "key1", TRUE);
}
/* ======================= */
/* = config_stop = */
/* ======================= */
static void
colors_config_stop()
{
}
/* =====================================================================
* in : boolean TRUE = PAUSE : FALSE = CONTINUE
* =====================================================================*/
......
......@@ -105,6 +105,7 @@ pyGcomprisBoardType_getattr(pyGcomprisBoardObject *self, char *name)
}
if (self->cdata->plugin->config_start && self->cdata->plugin->config_stop){
printf ("Board %s is configurable \n", self->cdata->name);
Py_INCREF(Py_True);
return Py_True;
} else {
......
......@@ -37,28 +37,25 @@ py_board_config_start (PyObject* self, PyObject* args)
{
PyObject* pyBoard;
PyObject* pyProfile;
PyObject* pyWindow;
pyGcomprisBoardObject* pyGcomprisBoard;
pyGcomprisProfileObject* pyGcomprisProfile;
GcomprisBoard* cGcomprisBoard;
GcomprisProfile* cGcomprisProfile;
GtkWindow *window;
/* Parse arguments */
if(!PyArg_ParseTuple(args,
"OOO:board_config_start",
"OO:board_config_start",
&pyBoard,
&pyProfile,
&pyWindow))
&pyProfile))
return NULL;
pyGcomprisBoard = (pyGcomprisBoardObject*) pyBoard;
pyGcomprisProfile = (pyGcomprisProfileObject*) pyProfile;
cGcomprisBoard = pyGcomprisBoard->cdata;
cGcomprisProfile = pyGcomprisProfile->cdata;
window = (GtkWindow*) pygobject_get(pyWindow);
/* Call the corresponding C function */
board_config_start(cGcomprisBoard, cGcomprisProfile, window);
board_config_start(cGcomprisBoard, cGcomprisProfile);
/* Create and return the result */
Py_INCREF(Py_None);
......
......@@ -16,6 +16,38 @@
#include "py-mod-anim.h"
#include "py-mod-admin.h"
void pair_in_dict(gpointer key,
gpointer value,
gpointer dict)
{
PyObject *pyValue;
/* key cannot be NULL. But value can */
if (value==NULL){
Py_INCREF(Py_None);
pyValue = Py_None;
} else
pyValue = PyString_FromString((gchar *)value);
PyDict_SetItem((PyObject *)dict, PyString_FromString((gchar *)key), pyValue);
}
/* Utility */
PyObject* hash_to_dict(GHashTable *table)
{
PyObject *pydict;
pydict = PyDict_New();
g_hash_table_foreach (table,
pair_in_dict,
(gpointer) pydict);
return pydict;;
}
/* All functions provided by this python module
* wraps a gcompris function. Each "py_*" function wraps the
* "*" C function.
......@@ -871,44 +903,30 @@ py_gcompris_spawn_async(PyObject *unused, PyObject *args, PyObject *kwargs)
static PyObject*
py_gcompris_get_board_conf(PyObject* self, PyObject* args)
{
GList *board_conf;
GList *list;
PyObject *pydict;
PyObject *value;
GcomprisConfPair *pair;
GHashTable *table;
/* Parse arguments */
if(!PyArg_ParseTuple(args, ":gcompris.get_board_conf"))
return NULL;
/* Call the corresponding C function */
board_conf = gcompris_get_board_conf();
pydict = PyDict_New();
for (list = board_conf; list != NULL; list = list->next){
pair = (GcomprisConfPair *) list->data;
if (pair->key==NULL)
continue;
if (pair->value==NULL){
Py_INCREF(Py_None);
value = Py_None;
} else
value = PyString_FromString(pair->value);
PyDict_SetItem(pydict, PyString_FromString(pair->key), value);
}
/* Create and return the result */
table = gcompris_get_board_conf();
pydict = hash_to_dict(table);
g_hash_table_destroy(table);
return pydict;;
}
static PyObject*
py_gcompris_get_conf(PyObject* self, PyObject* args)
{
GList *board_conf;
GList *list;
PyObject *pydict;
PyObject *value;
GcomprisConfPair *pair;
GHashTable *table;
PyObject* pyBoard;
PyObject* pyProfile;
......@@ -928,22 +946,12 @@ py_gcompris_get_conf(PyObject* self, PyObject* args)
cGcomprisBoard = pyGcomprisBoard->cdata;
/* Call the corresponding C function */
board_conf = gcompris_get_conf(cGcomprisProfile, cGcomprisBoard);
table = gcompris_get_conf(cGcomprisProfile, cGcomprisBoard);
pydict = hash_to_dict(table);
g_hash_table_destroy(table);
pydict = PyDict_New();
for (list = board_conf; list != NULL; list = list->next){
pair = (GcomprisConfPair *) list->data;
if (pair->key==NULL)
continue;
if (pair->value==NULL){
Py_INCREF(Py_None);
value = Py_None;
} else
value = PyString_FromString(pair->value);
PyDict_SetItem(pydict, PyString_FromString(pair->key), value);
}
/* Create and return the result */
return pydict;;
}
......@@ -985,8 +993,6 @@ py_gcompris_get_current_user(PyObject* self, PyObject* args)
static PyObject*
py_gcompris_set_board_conf (PyObject* self, PyObject* args)
{
GcomprisConfPair *pair;
PyObject* pyBoard;
PyObject* pyProfile;
pyGcomprisBoardObject* pyGcomprisBoard;
......@@ -1017,6 +1023,73 @@ py_gcompris_set_board_conf (PyObject* self, PyObject* args)
}
/* Some functions and variables needed to get the file selector working */
static PyObject* pyGcomprisConfCallbackFunc = NULL;
static GcomprisConfCallback pyGcomprisConfCallback(GHashTable* table){
PyObject* args;
PyObject* result;
if(pyGcomprisConfCallbackFunc==NULL) return;
/* Build arguments */
args = PyTuple_New(1);
PyTuple_SetItem(args, 0, Py_BuildValue("O", hash_to_dict(table)));
result = PyObject_CallObject(pyGcomprisConfCallbackFunc, args);
if(result==NULL){
PyErr_Print();
} else {
Py_DECREF(result);
}
}
static PyObject*
py_gcompris_configuration_window(PyObject* self, PyObject* args){
PyObject* pyCallback;
/* Parse arguments */
if(!PyArg_ParseTuple(args,
"O:gcompris_configuration_window",
&pyCallback))
return NULL;
if(!PyCallable_Check(pyCallback))
{
PyErr_SetString(PyExc_TypeError,
"gcompris_configuration_window argument must be callable");
return NULL;
}
pyGcomprisConfCallbackFunc = pyCallback;
return (PyObject *)pygobject_new((GObject*) gcompris_configuration_window(pyGcomprisConfCallback));
}
/* void gcompris_boolean_box (label, key, init); */
static PyObject*
py_gcompris_boolean_box(PyObject* self, PyObject* args)
{
PyObject *py_bool;
gchar *label;
gchar *key;
/* Parse arguments */
if(!PyArg_ParseTuple(args, "ssO:gcompris_boolean_box", &label, &key, &py_bool))
return NULL;
/* Call the corresponding C function */
gcompris_boolean_box((const gchar *)label, key, PyObject_IsTrue(py_bool));
/* Create and return the result */
Py_INCREF(Py_None);
return Py_None;
}
/****************************************************/
......@@ -1061,6 +1134,8 @@ static PyMethodDef PythonGcomprisModule[] = {
{ "set_board_conf", py_gcompris_set_board_conf, METH_VARARGS, "gcompris_set_board_conf" },
{ "get_current_profile", py_gcompris_get_current_profile, METH_VARARGS, "gcompris_get_current_profile" },
{ "get_current_user", py_gcompris_get_current_user, METH_VARARGS, "gcompris_get_current_user" },
{ "configuration_window", py_gcompris_configuration_window, METH_VARARGS, "gcompris_configuration_window" },
{ "boolean_box", py_gcompris_boolean_box, METH_VARARGS, "gcompris_boolean_box" },
{ NULL, NULL, 0, NULL}
};
......
......@@ -48,8 +48,7 @@ static void pythonboard_set_level (guint level);
static void pythonboard_config(void);
static void pythonboard_repeat (void);
static void pythonboard_config_start (GcomprisBoard *agcomprisBoard,
GcomprisProfile *aProfile,
GtkWindow *window);
GcomprisProfile *aProfile);
static void pythonboard_config_stop (void);
static gboolean pythonboard_is_ready = FALSE;
......@@ -529,8 +528,8 @@ static gboolean python_run_by_config = FALSE;
static void
pythonboard_config_start (GcomprisBoard *agcomprisBoard,
GcomprisProfile *aProfile,
GtkWindow *window){
GcomprisProfile *aProfile
){
PyObject* py_function_result;
PyObject* module_dict;
PyObject* py_boardclass;
......@@ -622,9 +621,8 @@ pythonboard_config_start (GcomprisBoard *agcomprisBoard,
py_function_result = PyObject_CallMethod(python_board_config_instance,
"config_start",
"OO",
gcompris_new_pyGcomprisProfileObject(aProfile),
pygobject_new((GObject*)window));
"O",
gcompris_new_pyGcomprisProfileObject(aProfile));
if( py_function_result != NULL){
Py_DECREF(py_function_result);
......
......@@ -343,17 +343,8 @@ class Board_list:
def configure_board(self, button):
print 'Oui, Oui un de ces jours a va lancer la conf de ', self.selected_board.title
window = gtk.Window()
window.set_title(_("Board %s configuration for %s") % (self.selected_board.name, self.active_profile.name))
window.set_border_width(8)
window.set_default_size(320, 350)
window.set_transient_for(self.frame.get_toplevel())
window.set_modal(True)
window.show()
gcompris.admin.board_config_start(self.selected_board,
self.active_profile,
window)
self.active_profile)
# gcompris.admin.board_config_stop(self.selected_board)
def filter_boards(self, button):
......
......@@ -153,41 +153,21 @@ class Gcompris_tuxpaint:
self.config_window.destroy()
pass
def config_start(self, profile, window):
def config_start(self, profile):
self.config_values = {}
self.configure_window = window
self.configure_profile = profile
print "config_start", self.gcomprisBoard.name, profile.name
button_close = gtk.Button(stock=gtk.STOCK_CLOSE)
button_close.connect("clicked", self.configuration_close)
button_close.show()
button_apply = gtk.Button(stock=gtk.STOCK_APPLY)
button_apply.connect("clicked", self.configuration_apply)
button_apply.show()
self.configure_main_box = gtk.VBox(False, 8)
self.configure_main_box.show()
window.add(self.configure_main_box)
box_bottom = gtk.HBox(False, 0)
box_bottom.show()
self.configure_main_box.pack_end(box_bottom, False, False, 0)
box_bottom.pack_end(button_close, False, False, 0)
box_bottom.pack_start(button_apply, False, False, 0)
self.main_vbox = gcompris.configuration_window(apply_callback)
self.actual_config = gcompris.get_conf(profile, self.gcomprisBoard)
self.boolean_box('Disable shape rotation', 'disable_shape_rotation')
self.boolean_box('Follow gcompris fullscreen', 'fullscreen')
self.boolean_box('Show Uppercase text only', 'uppercase_text')
self.boolean_box('Disable stamps', 'disable_stamps')
self.boolean_box('Disable stamps control', 'disable_stamps_control')
gcompris.boolean_box('Disable shape rotation', 'disable_shape_rotation', False)
gcompris.boolean_box('Follow gcompris fullscreen', 'fullscreen', True)
gcompris.boolean_box('Show Uppercase text only', 'uppercase_text', False)
gcompris.boolean_box('Disable stamps', 'disable_stamps', False)
gcompris.boolean_box('Disable stamps control', 'disable_stamps_control', False)
pass
def configuration_close(self, button):
self.configure_window.destroy()
......@@ -220,3 +200,7 @@ def child_callback(fd, cond, data):
def stop_board():
gcompris.bonus.board_finished(gcompris.bonus.FINISHED_RANDOM)
def apply_callback(table):
print table
/* gcompris - board_config.c
*
* Time-stamp: <2005/07/22 15:38:14 yves>
* Time-stamp: <2005/07/24 03:11:39 yves>
*
* Copyright (C) 2001 Pascal Georges
*
......@@ -19,14 +19,12 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef BOARD_CONFIG_H
#define BOARD_CONFIG_H
#include "gcompris.h"
static GcomprisBoard *config_board;
void board_config_start(GcomprisBoard *aBoard, GcomprisProfile *aProfile, GtkWindow *window)
void board_config_start(GcomprisBoard *aBoard, GcomprisProfile *aProfile)
{
/* if (config_board){ */
......@@ -46,7 +44,7 @@ void board_config_start(GcomprisBoard *aBoard, GcomprisProfile *aProfile, GtkWi
config_board = aBoard;
aBoard->plugin->config_start(aBoard, aProfile, window);
aBoard->plugin->config_start(aBoard, aProfile);
return;
}
......@@ -63,7 +61,128 @@ void board_config_stop()
return;
}
#endif
static GtkWindow *conf_window = NULL;
static GtkVBox *main_conf_box = NULL;
static GHashTable *hash_conf = NULL;
static GcomprisConfCallback Confcallback = NULL;
void gcompris_close_board_conf (GtkButton *button,
gpointer user_data)
{
gtk_object_destroy ((GtkObject *)conf_window);
}
void gcompris_apply_board_conf (GtkButton *button,
gpointer user_data)
{
if (Confcallback != NULL)
Confcallback(hash_conf);
}
GtkVBox *gcompris_configuration_window(GcomprisConfCallback callback)
{
GtkButton *button;
GtkHBox *footer;
Confcallback = callback;
hash_conf = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
conf_window = GTK_WINDOW(gtk_window_new (GTK_WINDOW_TOPLEVEL));
gtk_window_set_default_size (conf_window,
320,
300);
gtk_window_set_transient_for(conf_window,
GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(get_current_gcompris_board()->canvas))));
gtk_window_set_modal(conf_window, TRUE);
gtk_widget_show(GTK_WIDGET(conf_window));
main_conf_box = GTK_VBOX(gtk_vbox_new ( FALSE, 0));
gtk_widget_show(GTK_WIDGET(main_conf_box));
gtk_container_add(GTK_CONTAINER(conf_window), GTK_WIDGET(main_conf_box));
footer = GTK_HBOX(gtk_hbox_new (FALSE, 0));
gtk_widget_show(GTK_WIDGET(footer));
gtk_box_pack_end (GTK_BOX(main_conf_box),
GTK_WIDGET(footer),
FALSE,
FALSE,
0);
button = GTK_BUTTON(gtk_button_new_from_stock(GTK_STOCK_CLOSE));
gtk_widget_show(GTK_WIDGET(button));
gtk_box_pack_end (GTK_BOX(footer),
GTK_WIDGET(button),
FALSE,
FALSE,
0);
g_signal_connect(G_OBJECT(button),
"clicked",
G_CALLBACK(gcompris_close_board_conf),
NULL);
button = GTK_BUTTON(gtk_button_new_from_stock(GTK_STOCK_APPLY));
gtk_widget_show(GTK_WIDGET(button));
gtk_box_pack_start (GTK_BOX(footer),
GTK_WIDGET(button),
FALSE,
FALSE,
0);
g_signal_connect (G_OBJECT(button),
"clicked",
G_CALLBACK(gcompris_apply_board_conf),
NULL);
return main_conf_box;
}
void gcompris_boolean_box_toggled (GtkToggleButton *togglebutton,
gpointer key)
{
gchar *the_key = g_strdup((gchar *)key);
gchar *value;
if (gtk_toggle_button_get_active (togglebutton))
value = g_strdup("True");
else
value = g_strdup("False");
g_hash_table_replace(hash_conf, (gpointer) the_key, (gpointer) value);
}
void gcompris_boolean_box(const gchar *label, gchar *key, gboolean initial_value)
{
GtkWidget *CheckBox = gtk_check_button_new_with_label (label);
gtk_widget_show(CheckBox);
gtk_box_pack_start (GTK_BOX(main_conf_box),
CheckBox,
FALSE,
FALSE,
0);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(CheckBox),
initial_value);
g_signal_connect (G_OBJECT(CheckBox),
"toggled",
G_CALLBACK(gcompris_boolean_box_toggled),
key);
}
/* Local Variables: */
/* mode:c */
......
/* gcompris - board_config.h
*
* Time-stamp: <2005/07/22 15:11:00 yves>
* Time-stamp: <2005/07/24 01:53:50 yves>
*
* Copyright (C) 2001 Pascal Georges
*
......@@ -25,10 +25,22 @@
#include "gcompris.h"
gboolean board_config_check(GcomprisBoard *aBoard);
void board_config_start(GcomprisBoard *aBoard,
GcomprisProfile *aProfile,
GtkWindow *window);
GcomprisProfile *aProfile);
void board_config_stop();
/* Usefull fonctions for configuration of boards */
/* This fonction returns the main GtkVBox of the configuration window. */
/* You can add your own widget if you need */
/* the callback is called wish the hash key, value when the apply button is clicked */
/* the callback type */
typedef void (*GcomprisConfCallback) (GHashTable *table);
GtkVBox *gcompris_configuration_window(GcomprisConfCallback callback);
void gcompris_boolean_box(const gchar *label, gchar *key, gboolean initial_value);
#endif
/* Local Variables: */
......
/* gcompris - gcompris_db.c
*
<<<<<<< gcompris_db.c
* Time-stamp: <2005/07/23 01:39:25 bruno>
=======
* Time-stamp: <2005/07/22 21:10:04 yves>
>>>>>>> 1.24
* Time-stamp: <2005/07/23 16:12:09 yves>
*
* Copyright (C) 2000 Bruno Coudoin
*
......@@ -1128,7 +1124,7 @@ void gcompris_set_board_conf(GcomprisProfile *profile,
"SELECT key, value FROM board_profile_conf WHERE profile_id=%d AND board_id=%d;", p, b
GList *gcompris_get_conf(GcomprisProfile *profile, GcomprisBoard *board)
GHashTable *gcompris_get_conf(GcomprisProfile *profile, GcomprisBoard *board)
{
char *zErrMsg;
char **result;
......@@ -1138,7 +1134,7 @@ GList *gcompris_get_conf(GcomprisProfile *profile, GcomprisBoard *board)
gchar *request;
int i;
GList *list_conf = NULL;
GHashTable *hash_conf = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
request = g_strdup_printf(GET_CONF(profile->profile_id,
board->board_id));
......@@ -1160,30 +1156,28 @@ GList *gcompris_get_conf(GcomprisProfile *profile, GcomprisBoard *board)
i = ncolumn;
while (i < (nrow +1)*ncolumn){
GcomprisConfPair *pair = g_malloc0(sizeof(GcomprisConfPair));
pair->key = g_strdup(result[i++]);
pair->value = g_strdup(result[i++]);
list_conf = g_list_append(list_conf, pair);
g_hash_table_replace (hash_conf,
g_strdup(result[i++]),
g_strdup(result[i++]));
}
return list_conf;
sqlite3_free_table(result);
return hash_conf;
}
GList *gcompris_get_board_conf()
GHashTable *gcompris_get_board_conf()
{
GList *list_result;
GHashTable *hash_result;
list_result = gcompris_get_conf(gcompris_get_current_profile(),
hash_result = gcompris_get_conf(gcompris_get_current_profile(),
get_current_gcompris_board());
if (!list_result)
list_result = gcompris_get_conf(gcompris_get_profile_from_id(1),
if (g_hash_table_size(hash_result)==0)
hash_result = gcompris_get_conf(gcompris_get_profile_from_id(1),
get_current_gcompris_board());
return list_result;
return hash_result;
}
#define GET_ALL_PROFILES \
......
/* gcompris - gcompris_db.h
*
* Time-stamp: <2005/07/22 19:02:14 yves>
* Time-stamp: <2005/07/23 15:39:41 yves>
*
* Copyright (C) 2005 Bruno Coudoin
*
......@@ -97,17 +97,13 @@ void gcompris_set_board_conf(GcomprisProfile *profile,
gchar *value);
/* Return List of conf_cell */
typedef struct {
gchar *key;
gchar *value;
} GcomprisConfPair;
/* These two hash tables needs to be destroyed when not used more */
/* get conf for currents profile and board */
GList *gcompris_get_board_conf();
GHashTable *gcompris_get_board_conf();
/* get conf for specific profile and board */
GList *gcompris_get_conf(GcomprisProfile *profile, GcomprisBoard *board);
GHashTable *gcompris_get_conf(GcomprisProfile *profile, GcomprisBoard *board);