248 lines
5.7 KiB
C
248 lines
5.7 KiB
C
/*
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
*
|
|
* 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 "vn-form.h"
|
|
|
|
G_DEFINE_ABSTRACT_TYPE (VnForm, vn_form, GTK_TYPE_ALIGNMENT);
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
/**
|
|
* vn_form_open:
|
|
* @obj: the #VnForm
|
|
*
|
|
* Activates the form.
|
|
* Virtual: open
|
|
**/
|
|
void vn_form_open (VnForm * obj)
|
|
{
|
|
const gchar * dir;
|
|
gchar * file;
|
|
GError * err = NULL;
|
|
|
|
obj->builder = GTK_BUILDER (vn_builder_new ());
|
|
gtk_builder_set_translation_domain (obj->builder, vn_mod_get_text_domain (obj->mod));
|
|
|
|
dir = vn_mod_get_data_dir (obj->mod);
|
|
file = g_strdup_printf ("%s/%s.glade", dir, obj->name);
|
|
|
|
if (vn_builder_load_file (VN_BUILDER (obj->builder), obj->conn, file, &err))
|
|
{
|
|
gtk_builder_connect_signals (obj->builder, obj);
|
|
VN_FORM_GET_CLASS (obj)->open (obj, obj->builder, NULL);
|
|
gtk_container_add (GTK_CONTAINER (obj), vn_form_get (obj, "main"));
|
|
gtk_widget_show_all (GTK_WIDGET (obj));
|
|
|
|
// Loading actions from the .glade and the GtkUIManager definition
|
|
|
|
if ((obj->actions = vn_form_get (obj, "actions")))
|
|
{
|
|
gchar * buffer;
|
|
gchar * ui_file = g_strdup_printf ("%s/%s.ui", dir, obj->name);
|
|
|
|
if (g_file_get_contents (ui_file, &buffer, NULL, NULL))
|
|
{
|
|
obj->ui = buffer;
|
|
g_object_ref (obj->actions);
|
|
}
|
|
else
|
|
obj->actions = NULL;
|
|
|
|
g_free (ui_file);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
g_warning ("VnForm: %s", err->message);
|
|
g_error_free (err);
|
|
}
|
|
|
|
g_free (file);
|
|
}
|
|
|
|
/**
|
|
* vn_form_get:
|
|
* @obj: the #VnForm
|
|
* @name: the object name
|
|
*
|
|
* Gets an object from the form builder using its name.
|
|
*
|
|
* Return value: (transfer none): the object, or %NULL if there is no object
|
|
* with that name
|
|
**/
|
|
gpointer vn_form_get (VnForm * obj, const gchar * name)
|
|
{
|
|
return (gpointer) gtk_builder_get_object (obj->builder, name);
|
|
}
|
|
|
|
/**
|
|
* vn_form_get_name:
|
|
* @obj: the #VnForm
|
|
*
|
|
* Gets the name of the form.
|
|
**/
|
|
const gchar * vn_form_get_name (VnForm * obj)
|
|
{
|
|
g_return_val_if_fail (VN_IS_FORM (obj), NULL);
|
|
|
|
return obj->name;
|
|
}
|
|
|
|
/**
|
|
* vn_form_get_ui_manager:
|
|
* @obj: the #VnForm
|
|
*
|
|
* Returns the string containing the path of the #GtkUIManager UI definition
|
|
* file for the form @obj.
|
|
*
|
|
* Return value: a string
|
|
**/
|
|
const gchar * vn_form_get_ui_manager (VnForm * obj)
|
|
{
|
|
g_return_val_if_fail (VN_IS_FORM (obj), NULL);
|
|
|
|
return obj->ui;
|
|
}
|
|
|
|
/**
|
|
* vn_form_get_action_group:
|
|
* @obj: the #VnForm
|
|
*
|
|
* Returns the group actions implemented by @obj.
|
|
*
|
|
* Return value: (transfer none): a #GtkActionGroup
|
|
**/
|
|
GtkActionGroup * vn_form_get_action_group (VnForm * obj)
|
|
{
|
|
g_return_val_if_fail (VN_IS_FORM (obj), NULL);
|
|
|
|
return obj->actions;
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_NAME = 1
|
|
,PROP_GUI
|
|
,PROP_CONN
|
|
,PROP_MODULE
|
|
};
|
|
|
|
static void vn_form_set_property (VnForm * obj, guint id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_NAME:
|
|
obj->name = g_value_dup_string (value);
|
|
break;
|
|
case PROP_GUI:
|
|
obj->gui = g_value_dup_object (value);
|
|
obj->conn = vn_gui_get_conn (obj->gui);
|
|
break;
|
|
case PROP_MODULE:
|
|
obj->mod = g_value_dup_object (value);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
static void vn_form_get_property (VnForm * obj, guint id,
|
|
GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (id)
|
|
{
|
|
case PROP_NAME:
|
|
g_value_set_string (value, obj->name);
|
|
break;
|
|
case PROP_GUI:
|
|
g_value_set_object (value, obj->gui);
|
|
break;
|
|
case PROP_CONN:
|
|
g_value_set_object (value, obj->conn);
|
|
break;
|
|
case PROP_MODULE:
|
|
g_value_set_object (value, obj->mod);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void vn_form_init (VnForm * obj)
|
|
{
|
|
obj->name = NULL;
|
|
obj->gui = NULL;
|
|
obj->conn = NULL;
|
|
obj->builder = NULL;
|
|
obj->mod = NULL;
|
|
obj->actions = NULL;
|
|
obj->ui = NULL;
|
|
}
|
|
|
|
static void vn_form_finalize (VnForm * obj)
|
|
{
|
|
g_free (obj->name);
|
|
g_free (obj->ui);
|
|
g_clear_object (&obj->gui);
|
|
g_clear_object (&obj->builder);
|
|
g_clear_object (&obj->mod);
|
|
g_clear_object (&obj->actions);
|
|
G_OBJECT_CLASS (vn_form_parent_class)->finalize (G_OBJECT (obj));
|
|
}
|
|
|
|
static void vn_form_class_init (VnFormClass * k)
|
|
{
|
|
GObjectClass * klass = G_OBJECT_CLASS (k);
|
|
klass->finalize = (GObjectFinalizeFunc) vn_form_finalize;
|
|
klass->set_property = (GObjectSetPropertyFunc) vn_form_set_property;
|
|
klass->get_property = (GObjectGetPropertyFunc) vn_form_get_property;
|
|
|
|
g_object_class_install_property (klass, PROP_NAME,
|
|
g_param_spec_string ("name"
|
|
,_("Name")
|
|
,_("The form name")
|
|
,NULL
|
|
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
|
|
));
|
|
g_object_class_install_property (klass, PROP_GUI,
|
|
g_param_spec_object ("gui"
|
|
,_("Gui")
|
|
,_("The Gui object")
|
|
,VN_TYPE_GUI
|
|
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
|
|
));
|
|
g_object_class_install_property (klass, PROP_CONN,
|
|
g_param_spec_object ("conn"
|
|
,_("Connection")
|
|
,_("The connection used by the module")
|
|
,DB_TYPE_CONN
|
|
,G_PARAM_READABLE
|
|
));
|
|
g_object_class_install_property (klass, PROP_MODULE,
|
|
g_param_spec_object ("module"
|
|
,_("Module")
|
|
,_("The module")
|
|
,VN_TYPE_MOD
|
|
,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
|
|
));
|
|
}
|