glade-parameter.c 5.67 KB
Newer Older
Jose Maria Celorio's avatar
Jose Maria Celorio committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2001 Ximian, Inc.
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors:
 *   Chema Celorio <chema@celorio.com>
 */

#include <stdlib.h> /* for atoi and atof */
24
#include <string.h>
Jose Maria Celorio's avatar
Jose Maria Celorio committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

#include "glade.h"
#include "glade-xml-utils.h"
#include "glade-parameter.h"

void
glade_parameter_get_integer (GList *parameters, const gchar *key, gint *value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
			*value = atoi (parameter->value);
			return;
		}
	}
}

void
glade_parameter_get_float (GList *parameters, const gchar *key, gfloat *value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
			*value = atof (parameter->value);
			return;
		}
	}
}


void
glade_parameter_get_boolean (GList *parameters, const gchar *key, gboolean *value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
			if (strcmp (parameter->value, GLADE_TAG_TRUE) == 0)
				*value = TRUE;
			else if (strcmp (parameter->value, GLADE_TAG_FALSE) == 0)
				*value = FALSE;
			else
				g_warning ("Invalid boolean parameter *%s* (%s/%s)",
					   parameter->value, GLADE_TAG_TRUE, GLADE_TAG_FALSE);
			return;
		}
	}
}

void
glade_parameter_get_string (GList *parameters, const gchar *key, gchar **value)
{
	GladeParameter *parameter;
	GList *list;

	list = parameters;
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		if (strcmp (key, parameter->key) == 0) {
			if (*value != NULL)
				g_free (*value);
			*value = g_strdup (parameter->value);
			return;
		}
	}
}
102 103 104 105 106 107 108 109 110 111 112 113 114 115

static void
glade_parameter_free (GladeParameter *parameter)
{
	g_return_if_fail (parameter->key);
	g_return_if_fail (parameter->value);

	g_free (parameter->key);
	g_free (parameter->value);
	parameter->key = NULL;
	parameter->value = NULL;
	g_free (parameter);
}

116
GladeParameter *
Jose Maria Celorio's avatar
Jose Maria Celorio committed
117 118 119 120 121 122 123 124 125 126
glade_parameter_new (void)
{
	GladeParameter *parameter;

	parameter = g_new0 (GladeParameter, 1);

	return parameter;
}

static GladeParameter *
Chema Celorio's avatar
Chema Celorio committed
127
glade_parameter_new_from_node (GladeXmlNode *node)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
128 129 130 131 132 133 134
{
	GladeParameter *parameter;

	if (!glade_xml_node_verify (node, GLADE_TAG_PARAMETER))
		return NULL;
	
	parameter = glade_parameter_new ();
Chema Celorio's avatar
Chema Celorio committed
135 136
	parameter->key   = glade_xml_get_property_string_required (node, GLADE_TAG_KEY, NULL);
	parameter->value = glade_xml_get_property_string_required (node, GLADE_TAG_VALUE, NULL);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
137 138 139 140 141 142 143 144

	if (!parameter->key || !parameter->value)
		return NULL;

	return parameter;
}

GList *
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
glade_parameter_list_find_by_key (GList *list, const gchar *key)
{
	GladeParameter *parameter;
	
	for (; list != NULL; list = list->next) {
		parameter = list->data;
		g_return_val_if_fail (parameter->key != NULL, NULL);
		if (strcmp (parameter->key, key) == 0)
			return list;
	}

	return NULL;
}
		

GList *
Chema Celorio's avatar
Chema Celorio committed
161
glade_parameter_list_new_from_node (GList *list, GladeXmlNode *node)
Jose Maria Celorio's avatar
Jose Maria Celorio committed
162 163
{
	GladeParameter *parameter;
Chema Celorio's avatar
Chema Celorio committed
164
	GladeXmlNode *child;
165
	GList *findme;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
166 167 168 169 170 171 172

	if (!glade_xml_node_verify (node, GLADE_TAG_PARAMETERS))
		return NULL;
	child = glade_xml_search_child (node, GLADE_TAG_PARAMETER);
	if (child == NULL)
		return NULL;

Chema Celorio's avatar
Chema Celorio committed
173
	child = glade_xml_node_get_children (node);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
174 175 176 177 178 179 180 181

	while (child != NULL) {
		skip_text (child);
		if (!glade_xml_node_verify (child, GLADE_TAG_PARAMETER))
			return NULL;
		parameter = glade_parameter_new_from_node (child);
		if (parameter == NULL)
			return NULL;
182 183 184 185 186 187 188 189
		/* Is this parameter already there ? just replace
		 * the pointer and free the old one
		 */
		findme = glade_parameter_list_find_by_key (list,
							   parameter->key);
		if (findme) {
			glade_parameter_free (findme->data);
			findme->data = parameter;
Chema Celorio's avatar
Chema Celorio committed
190
			child = glade_xml_node_next (child);
191 192
		}

Jose Maria Celorio's avatar
Jose Maria Celorio committed
193
		list = g_list_prepend (list, parameter);
Chema Celorio's avatar
Chema Celorio committed
194
		child = glade_xml_node_next (child);
Jose Maria Celorio's avatar
Jose Maria Celorio committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	}

	list = g_list_reverse (list);
	
	return list;
}
	

/**
 * glade_parameter_adjustment_new:
 * @parameter: 
 * 
 * Creates a GtkAdjustment from a list of parameters.
 * 
 * Return Value: A newly created GtkAdjustment
 **/
GtkAdjustment *
glade_parameter_adjustment_new (GList *parameters)
{
	GtkAdjustment *adjustment;
	gfloat value = 1;
	gfloat lower = 0;
	gfloat upper = 999999;

219
	gfloat step_increment = 1;
Jose Maria Celorio's avatar
Jose Maria Celorio committed
220 221 222 223 224 225 226 227 228
	gfloat page_increment = 265;
	gfloat climb_rate = 1;

	glade_parameter_get_float (parameters, "Default", &value);
	glade_parameter_get_float (parameters, "Min",     &lower);
	glade_parameter_get_float (parameters, "Max",     &upper);
	glade_parameter_get_float (parameters, "StepIncrement", &step_increment);
	glade_parameter_get_float (parameters, "PageIncrement", &page_increment);
	glade_parameter_get_float (parameters, "ClimbRate", &climb_rate);
229

Jose Maria Celorio's avatar
Jose Maria Celorio committed
230 231 232 233 234 235 236 237
	adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (value,
							 lower,
							 upper,
							 step_increment,
							 page_increment,
							 climb_rate));
	return adjustment;
}
238