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/>.
23 #include "color-combo-history.h"
28 /*========================================================*/
30 /*========================================================*/
32 /** GL_COLOR_COMBO_HISTORY Private fields */
33 struct _glColorComboHistoryPrivate {
42 /*========================================================*/
43 /* Private globals. */
44 /*========================================================*/
47 /*========================================================*/
48 /* Private function prototypes. */
49 /*========================================================*/
51 static void gl_color_combo_history_finalize (GObject *object);
54 /*****************************************************************************/
55 /* Object infrastructure. */
56 /*****************************************************************************/
57 G_DEFINE_TYPE (glColorComboHistory, gl_color_combo_history, G_TYPE_OBJECT);
60 /*****************************************************************************/
61 /* Class Init Function. */
62 /*****************************************************************************/
64 gl_color_combo_history_class_init (glColorComboHistoryClass *class)
66 GObjectClass *gobject_class = (GObjectClass *) class;
68 gl_color_combo_history_parent_class = g_type_class_peek_parent (class);
70 gobject_class->finalize = gl_color_combo_history_finalize;
74 /*****************************************************************************/
75 /* Object Instance Init Function. */
76 /*****************************************************************************/
78 gl_color_combo_history_init (glColorComboHistory *this)
80 this->priv = g_new0 (glColorComboHistoryPrivate, 1);
84 /*****************************************************************************/
85 /* Finalize Method. */
86 /*****************************************************************************/
88 gl_color_combo_history_finalize (GObject *object)
90 glColorComboHistory *this;
92 g_return_if_fail (object && IS_GL_COLOR_COMBO_HISTORY (object));
93 this = GL_COLOR_COMBO_HISTORY (object);
95 g_free (this->priv->color);
98 G_OBJECT_CLASS (gl_color_combo_history_parent_class)->finalize (object);
102 /*****************************************************************************/
103 /** New Object Generator. */
104 /*****************************************************************************/
105 glColorComboHistory *
106 gl_color_combo_history_new (guint n)
108 glColorComboHistory *this;
110 this = g_object_new (TYPE_GL_COLOR_COMBO_HISTORY, NULL);
112 this->priv->max_n = n;
116 this->priv->color = g_new0 (guint, n);
123 /*****************************************************************************/
124 /* Add color to history. */
125 /*****************************************************************************/
127 gl_color_combo_history_add_color (glColorComboHistory *this,
133 * First check for duplicate color.
135 for ( i=0; i < this->priv->n; i++ )
137 if ( this->priv->color[i] == color )
146 if ( this->priv->n < this->priv->max_n )
148 this->priv->color[ this->priv->n ] = color;
154 /* Move colors down, dropping oldest. */
155 for ( i=0; i < (this->priv->n - 1); i++ )
157 this->priv->color[i] = this->priv->color[i+1];
159 this->priv->color[ this->priv->n - 1 ] = color;
162 /*****************************************************************************/
164 /*****************************************************************************/
166 gl_color_combo_history_get_color (glColorComboHistory *this,
169 return this->priv->color[i];
175 * Local Variables: -- emacs
177 * c-basic-offset: 8 -- emacs
178 * tab-width: 8 -- emacs
179 * indent-tabs-mode: nil -- emacs