]> git.sur5r.net Git - glabels/blob - glabels2/src/color-combo-history.c
fa62c0d8dc41d91088031b739ce5bc271569dc16
[glabels] / glabels2 / src / color-combo-history.c
1 /*
2  *  color-combo-history.c
3  *  Copyright (C) 2008  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include <config.h>
22
23
24 #include "color-combo-history.h"
25
26 #include "color.h"
27
28
29 /*========================================================*/
30 /* Private types.                                         */
31 /*========================================================*/
32
33 /** GL_COLOR_COMBO_HISTORY Private fields */
34 struct _glColorComboHistoryPrivate {
35
36         guint       max_n;
37
38         guint       n;
39         guint      *color;
40 };
41
42
43 /*========================================================*/
44 /* Private globals.                                       */
45 /*========================================================*/
46
47
48 /*========================================================*/
49 /* Private function prototypes.                           */
50 /*========================================================*/
51
52 static void gl_color_combo_history_finalize      (GObject             *object);
53
54
55 /*****************************************************************************/
56 /* Object infrastructure.                                                    */
57 /*****************************************************************************/
58 G_DEFINE_TYPE (glColorComboHistory, gl_color_combo_history, G_TYPE_OBJECT);
59
60
61 /*****************************************************************************/
62 /* Class Init Function.                                                      */
63 /*****************************************************************************/
64 static void
65 gl_color_combo_history_class_init (glColorComboHistoryClass *class)
66 {
67         GObjectClass              *gobject_class = (GObjectClass *) class;
68         glColorComboHistoryClass  *object_class  = (glColorComboHistoryClass *) class;
69
70         gl_color_combo_history_parent_class = g_type_class_peek_parent (class);
71
72         gobject_class->finalize = gl_color_combo_history_finalize;
73 }
74
75
76 /*****************************************************************************/
77 /* Object Instance Init Function.                                            */
78 /*****************************************************************************/
79 static void
80 gl_color_combo_history_init (glColorComboHistory *this)
81 {
82         this->priv = g_new0 (glColorComboHistoryPrivate, 1);
83 }
84
85
86 /*****************************************************************************/
87 /* Finalize Method.                                                          */
88 /*****************************************************************************/
89 static void
90 gl_color_combo_history_finalize (GObject *object)
91 {
92         glColorComboHistory    *this;
93
94         g_return_if_fail (object && IS_GL_COLOR_COMBO_HISTORY (object));
95         this = GL_COLOR_COMBO_HISTORY (object);
96
97         g_free (this->priv->color);
98         g_free (this->priv);
99
100         G_OBJECT_CLASS (gl_color_combo_history_parent_class)->finalize (object);
101 }
102
103
104 /*****************************************************************************/
105 /** New Object Generator.                                                    */
106 /*****************************************************************************/
107 glColorComboHistory *
108 gl_color_combo_history_new (guint n)
109 {
110         glColorComboHistory *this;
111
112         this = g_object_new (TYPE_GL_COLOR_COMBO_HISTORY, NULL);
113
114         this->priv->max_n = n;
115         this->priv->n     = 0;
116         if (n > 0)
117         {
118                 this->priv->color = g_new0 (guint, n);
119         }
120
121         return this;
122 }
123
124
125 /*****************************************************************************/
126 /* Add color to history.                                                     */
127 /*****************************************************************************/
128 void
129 gl_color_combo_history_add_color (glColorComboHistory *this,
130                                   guint                color)
131 {
132         guint i;
133
134         /*
135          * First check for duplicate color.
136          */
137         for ( i=0; i < this->priv->n; i++ )
138         {
139                 if ( this->priv->color[i] == color )
140                 {
141                         return;
142                 }
143         }
144
145         /*
146          * Simple case.
147          */
148         if ( this->priv->n < this->priv->max_n )
149         {
150                 this->priv->color[ this->priv->n ] = color;
151                 this->priv->n++;
152
153                 return;
154         }
155
156         /* Move colors down, dropping oldest. */
157         for ( i=0; i < (this->priv->n - 1); i++ )
158         {
159                 this->priv->color[i] = this->priv->color[i+1];
160         }
161         this->priv->color[ this->priv->n - 1 ] = color;
162 }
163
164 /*****************************************************************************/
165 /* Get color.                                                                */
166 /*****************************************************************************/
167 guint
168 gl_color_combo_history_get_color (glColorComboHistory *this,
169                                   guint                i)
170 {
171         return this->priv->color[i];
172 }
173
174
175
176 /*
177  * Local Variables:       -- emacs
178  * mode: C                -- emacs
179  * c-basic-offset: 8      -- emacs
180  * tab-width: 8           -- emacs
181  * indent-tabs-mode: nil  -- emacs
182  * End:                   -- emacs
183  */