Commit fb940f78 authored by Juan Pablo Ugarte's avatar Juan Pablo Ugarte

Removed GladeBindingScript.

* bindings/python/glade-python.c, gladeui/glade-binding.c, 
  gladeui/glade-binding.h: Removed GladeBindingScript.

svn path=/trunk/; revision=1306
parent 4cedef0e
2007-05-08 Juan Pablo Ugarte <juanpablougarte@gmail.com>
* bindings/python/glade-python.c, gladeui/glade-binding.c,
gladeui/glade-binding.h: Removed GladeBindingScript.
* gladeui/glade-property-class.c, gladeui/glade-property-class.h:
added save_always member in GladePropertyClass.
......
......@@ -28,226 +28,7 @@
#include <gladeui/glade.h>
#include <gladeui/glade-binding.h>
static PyObject *glade, *glade_dict, *GladeError;
static PyObject *
glade_python_undo (PyObject *self, PyObject *args)
{
glade_app_command_undo ();
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_redo (PyObject *self, PyObject *args)
{
glade_app_command_redo ();
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_project_new (PyObject *self, PyObject *args)
{
glade_app_add_project (glade_project_new ());
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_project_open (PyObject *self, PyObject *args)
{
gchar *path;
Py_INCREF(Py_None);
if (PyArg_ParseTuple(args, "s", &path))
{
GladeProject *project;
if ((project = glade_app_get_project_by_path (path)))
glade_app_set_project (project);
else if ((project = glade_project_load (path)))
glade_app_add_project (project);
else
return Py_None;
}
return Py_None;
}
static PyObject *
glade_python_project_save (PyObject *self, PyObject *args)
{
gchar *path;
if (PyArg_ParseTuple(args, "s", &path))
glade_project_save (glade_app_get_project (), path, NULL);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_project_close (PyObject *self, PyObject *args)
{
gchar *path;
if (PyArg_ParseTuple(args, "s", &path))
{
GladeProject *project = glade_app_get_project_by_path (path);
if (project) glade_app_remove_project (project);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_project_get (PyObject *self, PyObject *args)
{
GladeProject *project = glade_app_get_project ();
return Py_BuildValue ("s", (project) ? glade_project_get_name (project) : "");
}
static PyObject *
glade_python_project_list (PyObject *self, PyObject *args)
{
GList *p, *projects = glade_app_get_projects ();
PyObject *list;
list = PyList_New (0);
for (p = projects; p && p->data; p = g_list_next (p))
{
GladeProject *project = p->data;
PyList_Append (list, Py_BuildValue ("s", glade_project_get_name (project)));
}
return list;
}
static PyObject *
glade_python_project_set (PyObject *self, PyObject *args)
{
gchar *path;
if (PyArg_ParseTuple(args, "s", &path))
{
GladeProject *project = glade_app_get_project_by_path (path);
if (project) glade_app_set_project (project);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_widget_new (PyObject *self, PyObject *args)
{
GladeProject *project = glade_app_get_project ();
GladeWidgetAdaptor *adaptor;
GladeWidget *widget = NULL;
gchar *class_name, *parent;
if (project && PyArg_ParseTuple(args, "ss", &class_name, &parent) &&
(adaptor = glade_widget_adaptor_get_by_name (class_name)))
widget = glade_command_create (adaptor,
glade_project_get_widget_by_name (project, parent),
NULL, project);
return Py_BuildValue ("s", (widget) ? widget->name : "");
}
static PyObject *
glade_python_widget_delete (PyObject *self, PyObject *args)
{
GladeProject *project = glade_app_get_project ();
gchar *name;
if (project && PyArg_ParseTuple(args, "s", &name))
{
GladeWidget *widget;
if ((widget = glade_project_get_widget_by_name (project, name)))
{
GList list;
list.data = widget;
list.next = list.prev = NULL;
glade_command_delete (&list);
}
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_widget_set (PyObject *self, PyObject *args)
{
GladeProject *project = glade_app_get_project ();
gchar *name, *id_property;
PyObject *val;
if (project && PyArg_ParseTuple(args, "ssO", &name, &id_property, &val))
{
GladeWidget *widget;
GladeProperty *property;
if ((widget = glade_project_get_widget_by_name (project, name)) &&
(property = glade_widget_get_property (widget, id_property)))
{
GValue value = {0,};
g_value_init (&value, G_VALUE_TYPE (property->value));
if (pyg_value_from_pyobject (&value, val) == 0)
glade_command_set_property_value (property, &value);
}
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_widget_get (PyObject *self, PyObject *args)
{
GladeProject *project = glade_app_get_project ();
gchar *name, *id_property;
if (project && PyArg_ParseTuple(args, "ss", &name, &id_property))
{
GladeWidget *widget;
GladeProperty *property;
if ((widget = glade_project_get_widget_by_name (project, name)) &&
(property = glade_widget_get_property (widget, id_property)))
return pyg_value_as_pyobject (property->value, TRUE);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
glade_python_widget_list (PyObject *self, PyObject *args)
{
GList *p;
GladeProject *project = glade_app_get_project ();
PyObject *list;
list = PyList_New (0);
for (p = (GList *) glade_project_get_objects (project); p && p->data; p = g_list_next (p))
{
GladeWidget *widget = glade_widget_get_from_gobject (p->data);
if (widget)
PyList_Append (list, Py_BuildValue ("s", glade_widget_get_name (widget)));
}
return list;
}
static PyObject *gladeui, *gladeui_dict, *GladeuiError;
extern PyTypeObject PyGladeWidgetAdaptor_Type;
......@@ -269,7 +50,7 @@ glade_python_register_class (GType type)
klass = pygobject_lookup_class (type);
pygobject_register_class (glade_dict, g_type_name (type), type, klass,
pygobject_register_class (gladeui_dict, g_type_name (type), type, klass,
Py_BuildValue("(O)", parent_class));
pyg_set_object_has_new_constructor (type);
......@@ -300,21 +81,7 @@ glade_python_get_adaptor_for_type (PyObject *self, PyObject *args)
return Py_None;
}
static PyMethodDef GladeMethods[] = {
{"undo", glade_python_undo, METH_VARARGS, "Execute undo command."},
{"redo", glade_python_redo, METH_VARARGS, "Execute redo command."},
{"project_new", glade_python_project_new, METH_VARARGS, "Create a new project."},
{"project_open", glade_python_project_open, METH_VARARGS, "Open an existing project."},
{"project_save", glade_python_project_save, METH_VARARGS, "Save the current project."},
{"project_close", glade_python_project_close, METH_VARARGS, "Close the current project."},
{"project_get", glade_python_project_get, METH_VARARGS, "Get the current project."},
{"project_set", glade_python_project_set, METH_VARARGS, "Set the current project."},
{"project_list", glade_python_project_list, METH_VARARGS, "List all projects."},
{"widget_new", glade_python_widget_new, METH_VARARGS, "Create a new GtkWidget."},
{"widget_delete", glade_python_widget_delete, METH_VARARGS, "Delete a GtkWidget."},
{"widget_set", glade_python_widget_set, METH_VARARGS, "Set a GtkWidget's property."},
{"widget_get", glade_python_widget_get, METH_VARARGS, "Get a GtkWidget's property."},
{"widget_list", glade_python_widget_list, METH_VARARGS, "List all widgets."},
static PyMethodDef GladeuiMethods[] = {
{"get_adaptor_for_type", glade_python_get_adaptor_for_type, METH_VARARGS, "Get the corresponding GladeWidgetAdaptor. Use this function to create your own derived Adaptor."},
{NULL, NULL, 0, NULL}
};
......@@ -334,35 +101,6 @@ glade_python_binding_library_load (const gchar *library)
g_free (str);
}
gint
glade_python_binding_run_script (const gchar *path, gchar **argv)
{
FILE *fp = fopen (path, "r");
gint retval, i;
if (fp == NULL) return -1;
PyRun_SimpleString ("sys.argv = [];");
if (argv)
{
GString *string = g_string_new ("");
for (i = 0; argv[i]; i++)
{
g_string_printf (string, "sys.argv += ['%s'];", argv[i]);
PyRun_SimpleString (string->str);
}
g_string_free (string, TRUE);
}
retval = PyRun_SimpleFile (fp, path);
fclose (fp);
return retval;
}
/*
Generated by pygtk-codegen-2.0
pygtk-codegen-2.0 -p glade_python_gwa -o glade-python-gwa.override glade-python-gwa.defs > glade-python-gwa.c
......@@ -438,14 +176,14 @@ glade_binding_init (GladeBindingCtrl *ctrl)
pyg_disable_warning_redirections ();
/* Create glade object */
glade = Py_InitModule ("glade", GladeMethods);
GladeError = PyErr_NewException ("glade.error", NULL, NULL);
Py_INCREF (GladeError);
PyModule_AddObject (glade, "error", GladeError);
gladeui = Py_InitModule ("glade", GladeuiMethods);
GladeuiError = PyErr_NewException ("glade.error", NULL, NULL);
Py_INCREF (GladeuiError);
PyModule_AddObject (gladeui, "error", GladeuiError);
/* Register GladeUI classes (GladeWidgetAdaptor) */
glade_dict = PyModule_GetDict (glade);
glade_python_gwa_register_classes (glade_dict);
gladeui_dict = PyModule_GetDict (gladeui);
glade_python_gwa_register_classes (gladeui_dict);
/* Create registered_classes hash table */
registered_classes = g_hash_table_new (g_direct_hash, g_direct_equal);
......@@ -465,7 +203,6 @@ glade_binding_init (GladeBindingCtrl *ctrl)
ctrl->name = "python";
ctrl->finalize = glade_python_binding_finalize;
ctrl->library_load = glade_python_binding_library_load;
ctrl->run_script = glade_python_binding_run_script;
return TRUE;
}
......@@ -43,129 +43,15 @@ GladeBindingInitFunc glade_binding_init() to initialize the binding and
GladeBindingLibraryLoadFunc to load a library written in language supported
by the binding.
You can add Scripting capability to the core by providing
GladeBindingRunScriptFunc and/or GladeBindingConsoleNewFunc
The core will search for script in two different directories,
datadir/package/scripts/ and g_get_user_config_dir()/package/scripts/
The hierarchy in those directories should be binding_name/gtype_name/ *
For example if you want to add a python script to GtkContainers you can do it by
installing the script file in
g_get_user_config_dir()/glade3/scripts/python/GtkContainer/Delete_Children.py
This will add a context menu item for any GtkContainer called "Delete Children"
that will trigger GtkContainer's widget adaptor "action-activated::Delete_Children" signal.
*/
static GHashTable *bindings = NULL;
static GList*
glade_binding_script_add (GList *list, GladeBinding *binding, gchar *path)
{
GladeBindingScript *script = g_new0 (GladeBindingScript, 1);
gchar *tmp;
script->binding = binding;
script->path = path;
script->name = g_path_get_basename (path);
if ((tmp = strrchr (script->name, '.'))) *tmp = '\0';
return g_list_prepend (list, script);
}
static GList *
glade_binding_get_script_list (GladeBinding *binding, gchar *directory)
{
const gchar *filename;
GList *list = NULL;
GDir *dir;
if ((dir = g_dir_open (directory, 0, NULL)) == NULL)
return NULL;
while ((filename = g_dir_read_name (dir)))
{
gchar *path = g_build_filename (directory, filename, NULL);
if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
list = glade_binding_script_add (list, binding, path);
else
g_free (path);
}
return list;
}
static void
glade_binding_script_free (gpointer data, gpointer user_data)
{
GladeBindingScript *script = data;
if (script->path) g_free (script->path);
if (script->name) g_free (script->name);
g_free (script);
}
static void
glade_binding_classes_destroy (gpointer value)
{
g_list_foreach (value, glade_binding_script_free, NULL);
g_list_free (value);
}
static void
glade_binding_script_load (GladeBinding *binding, gchar *rootdir)
{
const gchar *filename;
GDir *dir;
if ((dir = g_dir_open (rootdir, 0, NULL)) == NULL)
return;
while ((filename = g_dir_read_name (dir)))
{
gchar *key, *path = g_build_filename (rootdir, filename, NULL);
if (g_file_test (path, G_FILE_TEST_IS_DIR))
{
GList *list, *old_list;
list = glade_binding_get_script_list (binding, path);
if (g_hash_table_lookup_extended (binding->context_scripts,
filename,
(gpointer)&key,
(gpointer)&old_list))
{
g_hash_table_steal (binding->context_scripts, key);
g_free (key);
list = g_list_concat (old_list, list);
}
g_hash_table_insert (binding->context_scripts,
g_strdup (filename), list);
}
else if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
binding->scripts = glade_binding_script_add (binding->scripts, binding, path);
else
g_free (path);
}
}
/**
* glade_binding_load_all:
*
* Loads and initialize every binding plugin.
*
* Loads scripts for bindings that have GladeBindingRunScriptFunc implemented.
*
* This function will search for script in two different directories,
* datadir/package/scripts/ and g_get_user_config_dir()/package/scripts/
*
* The hierarchy in those directories should be binding_name/gtype_name/ *
* For example if you want to add a python script to GtkContainers you can do it
* by installing the script file in g_get_user_config_dir()/glade3/scripts/python/GtkContainer/Delete_Children.py
* This will add a context menu item for any GtkContainer called "Delete Children"
* that will trigger GtkContainer's widget adaptor "action-activated::Delete_Children" signal.
* This signal is proxied by GladeWidget.
*
*/
void
glade_binding_load_all (void)
......@@ -189,7 +75,7 @@ glade_binding_load_all (void)
GladeBindingInitFunc init;
GladeBinding *binding;
GModule *module;
gchar *path, *rootdir;
gchar *path;
if (g_str_has_suffix (filename, G_MODULE_SUFFIX) == FALSE)
continue;
......@@ -213,24 +99,6 @@ glade_binding_load_all (void)
}
g_hash_table_insert (bindings, binding->ctrl.name, binding);
/* Load Scripts */
binding->context_scripts = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
glade_binding_classes_destroy);
/* datadir/package/scripts/ */
rootdir = g_build_filename (glade_app_get_scripts_dir (), binding->ctrl.name, NULL);
glade_binding_script_load (binding, rootdir);
g_free (rootdir);
/* g_get_user_config_dir()/package/scripts/ */
rootdir = g_build_filename (g_get_user_config_dir (),
PACKAGE_NAME, GLADE_BINDING_SCRIPT_DIR,
binding->ctrl.name, NULL);
glade_binding_script_load (binding, rootdir);
g_free (rootdir);
g_free (path);
}
}
......@@ -244,12 +112,6 @@ glade_binding_remove (gpointer key, gpointer value, gpointer user_data)
g_module_close (binding->module);
if (binding->scripts)
glade_binding_classes_destroy (binding->scripts);
if (binding->context_scripts)
g_hash_table_remove_all (binding->context_scripts);
g_free (binding);
}
......@@ -309,7 +171,7 @@ glade_binding_get_all_foreach (gpointer key, gpointer value, gpointer user_data)
/**
* glade_binding_get_all:
*
* Return a newly alocated list of every laoded binding.
* Return a newly allocated list of every loaded binding.
*
*/
GList *
......@@ -339,24 +201,3 @@ glade_binding_library_load (GladeBinding *binding, const gchar *library)
if (binding->ctrl.library_load)
binding->ctrl.library_load (library);
}
/**
* glade_binding_run_script:
*
* @binding: a GladeBinding.
* @path: the library init function name.
*
* Run a script file.
*
*/
gint
glade_binding_run_script (GladeBinding *binding,
const gchar *path,
gchar **argv)
{
g_return_val_if_fail (binding != NULL && path != NULL, -1);
if (binding->ctrl.run_script)
return binding->ctrl.run_script (path, argv);
else
return -1;
}
......@@ -25,8 +25,6 @@
G_BEGIN_DECLS
#define GLADE_BINDING_SCRIPT_DIR "scripts"
typedef struct _GladeBindingCtrl GladeBindingCtrl;
/**
......@@ -66,45 +64,21 @@ typedef void (*GladeBindingFinalizeFunc) (GladeBindingCtrl *ctrl);
*/
typedef void (*GladeBindingLibraryLoadFunc) (const gchar *str);
/**
* GladeBindingRunScriptFunc:
*
* @path: the script path.
*
* Run the script @path.
* Define this function for the binding to support running scripts.
*
*/
typedef gint (*GladeBindingRunScriptFunc) (const gchar *path,
gchar **argv);
struct _GladeBindingCtrl {
gchar *name; /* the name of the module (ie: python) */
/* Module symbols */
GladeBindingFinalizeFunc finalize;
GladeBindingLibraryLoadFunc library_load;
GladeBindingRunScriptFunc run_script;
};
typedef struct _GladeBinding GladeBinding;
struct _GladeBinding {
GModule *module; /* The binding module */
GList *scripts; /* A list of GladeBindingScript */
GHashTable *context_scripts; /* A table of GladeBindingScript's list */
GladeBindingCtrl ctrl;
};
typedef struct _GladeBindingScript GladeBindingScript ;
struct _GladeBindingScript {
GladeBinding *binding;
gchar *name, *path;
};
void glade_binding_load_all (void);
void glade_binding_unload_all (void);
......@@ -118,11 +92,6 @@ GList *glade_binding_get_all ();
void glade_binding_library_load (GladeBinding *binding,
const gchar *library);
gint glade_binding_run_script (GladeBinding *script,
const gchar *path,
gchar **argv);
G_END_DECLS
#endif /* __GLADE_BINDING_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment