158 lines
4.2 KiB
C
158 lines
4.2 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-builder.h"
|
|
#include "vn-field.h"
|
|
#include <vn/field/field.h>
|
|
#include <vn/column/column.h>
|
|
|
|
/**
|
|
* SECTION: vn-builder
|
|
* @Short_description: functions to use with the GtkBuilder
|
|
* @Title: VnBuilder
|
|
*
|
|
* These are a set of functions that make it easy to create and bind interfaces
|
|
* to data using the #GtkBuilder and the #DbLib, part of the Hedera library.
|
|
**/
|
|
G_DEFINE_TYPE (VnBuilder, vn_builder, GTK_TYPE_BUILDER);
|
|
|
|
VnBuilder * vn_builder_new ()
|
|
{
|
|
return g_object_new (VN_TYPE_BUILDER, NULL);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public
|
|
|
|
/**
|
|
* vn_builder_load_file:
|
|
* @obj: a #VnBuilder
|
|
* @conn: a #DbConn
|
|
* @filename: the name of the file to parse
|
|
* @err: (out) (allow-none): return location for an error, or %NULL
|
|
*
|
|
* Parses a file containing a GtkBuilder UI definition and merges it with the
|
|
* current contents of builder. Also sets de conn property of objects that use
|
|
* the #DbConn
|
|
*
|
|
* Return value: A positive value on success, 0 if an error occurred
|
|
**/
|
|
guint vn_builder_load_file (VnBuilder * obj, DbConn * conn, const gchar * filename, GError ** err)
|
|
{
|
|
guint success;
|
|
GtkBuilder * builder;
|
|
|
|
g_return_val_if_fail (VN_IS_BUILDER (obj), 0);
|
|
g_return_val_if_fail (DB_IS_CONN (conn), 0);
|
|
|
|
builder = GTK_BUILDER (obj);
|
|
success = gtk_builder_add_from_file (builder, filename, err);
|
|
|
|
if (success)
|
|
{
|
|
GSList * n;
|
|
GSList * objects = gtk_builder_get_objects (builder);
|
|
|
|
for (n = objects; n; n = n->next)
|
|
if (VN_IS_COMBO (n->data)
|
|
|| VN_IS_COMPLETION (n->data)
|
|
|| VN_IS_COLUMN_COMBO (n->data)
|
|
|| DB_IS_MODEL (n->data)
|
|
|| DB_IS_ITERATOR (n->data))
|
|
g_object_set (n->data, "conn", conn, NULL);
|
|
|
|
g_slist_free (objects);
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
/**
|
|
* vn_builder_bind_fields:
|
|
* @obj: a #VnBuilder
|
|
* @iterator: where params are obtained.
|
|
* @...: pairs of column name and field id, terminated with -1
|
|
*
|
|
* Binds several iterator parameters to #VnEntry entries.
|
|
* The variable list argument should contain a string with the column name and
|
|
* a string the #VnField id.
|
|
**/
|
|
void vn_builder_bind_fields (VnBuilder * obj, DbIterator * iterator, ...)
|
|
{
|
|
va_list vl;
|
|
gchar * column;
|
|
const gchar * id;
|
|
VnField * field;
|
|
|
|
g_return_if_fail (VN_IS_BUILDER (obj));
|
|
g_return_if_fail (DB_IS_ITERATOR (iterator));
|
|
|
|
va_start (vl, iterator);
|
|
|
|
while ((column = va_arg (vl, gchar *)) != NULL)
|
|
{
|
|
id = va_arg (vl, gchar *);
|
|
field = vn_builder_get (obj, id);
|
|
|
|
if (VN_IS_FIELD (field))
|
|
vn_field_set_param (field, db_iterator_get_param (iterator, column));
|
|
else
|
|
g_warning ("VnBuilder: Object '%s' isn't a VnField!", id);
|
|
}
|
|
|
|
va_end (vl);
|
|
}
|
|
|
|
/**
|
|
* vn_builder_bind_columns:
|
|
* @obj: a #VnBuilder
|
|
* @...: pairs of column name and column id, terminated with -1
|
|
*
|
|
* Binds several model columns to #VnColumn columns.
|
|
* The variable list argument should contain a string with the column name and
|
|
* a string the #VnColumn id.
|
|
**/
|
|
void vn_builder_bind_columns (VnBuilder * obj, ...)
|
|
{
|
|
va_list vl;
|
|
gchar * column_name;
|
|
const gchar * id;
|
|
VnColumn * column;
|
|
|
|
g_return_if_fail (VN_IS_BUILDER (obj));
|
|
|
|
va_start (vl, obj);
|
|
|
|
while ((column_name = va_arg (vl, gchar *)) != NULL)
|
|
{
|
|
id = va_arg (vl, gchar *);
|
|
column = vn_builder_get (obj, id);
|
|
|
|
if (VN_IS_COLUMN (column))
|
|
vn_column_set_column_name (column, column_name);
|
|
else
|
|
g_warning ("VnBuilder: Object '%s' isn't a VnColumn!", id);
|
|
}
|
|
|
|
va_end (vl);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void vn_builder_init (VnBuilder * obj) {}
|
|
|
|
static void vn_builder_class_init (VnBuilderClass * k) {}
|