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