2 * color-combo-history.c
3 * Copyright (C) 2008 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * gLabels is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
24 #include "color-combo-history.h"
29 /*========================================================*/
31 /*========================================================*/
33 /** GL_COLOR_COMBO_HISTORY Private fields */
34 struct _glColorComboHistoryPrivate {
43 /*========================================================*/
44 /* Private globals. */
45 /*========================================================*/
48 /*========================================================*/
49 /* Private function prototypes. */
50 /*========================================================*/
52 static void gl_color_combo_history_finalize (GObject *object);
55 /*****************************************************************************/
56 /* Object infrastructure. */
57 /*****************************************************************************/
58 G_DEFINE_TYPE (glColorComboHistory, gl_color_combo_history, G_TYPE_OBJECT);
61 /*****************************************************************************/
62 /* Class Init Function. */
63 /*****************************************************************************/
65 gl_color_combo_history_class_init (glColorComboHistoryClass *class)
67 GObjectClass *gobject_class = (GObjectClass *) class;
68 glColorComboHistoryClass *object_class = (glColorComboHistoryClass *) class;
70 gl_color_combo_history_parent_class = g_type_class_peek_parent (class);
72 gobject_class->finalize = gl_color_combo_history_finalize;
76 /*****************************************************************************/
77 /* Object Instance Init Function. */
78 /*****************************************************************************/
80 gl_color_combo_history_init (glColorComboHistory *this)
82 this->priv = g_new0 (glColorComboHistoryPrivate, 1);
86 /*****************************************************************************/
87 /* Finalize Method. */
88 /*****************************************************************************/
90 gl_color_combo_history_finalize (GObject *object)
92 glColorComboHistory *this;
94 g_return_if_fail (object && IS_GL_COLOR_COMBO_HISTORY (object));
95 this = GL_COLOR_COMBO_HISTORY (object);
97 g_free (this->priv->color);
100 G_OBJECT_CLASS (gl_color_combo_history_parent_class)->finalize (object);
104 /*****************************************************************************/
105 /** New Object Generator. */
106 /*****************************************************************************/
107 glColorComboHistory *
108 gl_color_combo_history_new (guint n)
110 glColorComboHistory *this;
112 this = g_object_new (TYPE_GL_COLOR_COMBO_HISTORY, NULL);
114 this->priv->max_n = n;
118 this->priv->color = g_new0 (guint, n);
125 /*****************************************************************************/
126 /* Add color to history. */
127 /*****************************************************************************/
129 gl_color_combo_history_add_color (glColorComboHistory *this,
135 * First check for duplicate color.
137 for ( i=0; i < this->priv->n; i++ )
139 if ( this->priv->color[i] == color )
148 if ( this->priv->n < this->priv->max_n )
150 this->priv->color[ this->priv->n ] = color;
156 /* Move colors down, dropping oldest. */
157 for ( i=0; i < (this->priv->n - 1); i++ )
159 this->priv->color[i] = this->priv->color[i+1];
161 this->priv->color[ this->priv->n - 1 ] = color;
164 /*****************************************************************************/
166 /*****************************************************************************/
168 gl_color_combo_history_get_color (glColorComboHistory *this,
171 return this->priv->color[i];
177 * Local Variables: -- emacs
179 * c-basic-offset: 8 -- emacs
180 * tab-width: 8 -- emacs
181 * indent-tabs-mode: nil -- emacs