libgeda, gschem: Limit colors usable as object colors
[geda-gaf.git] / gschem / src / x_colorcb.c
blob6a620bb3bd191a964b1b9211b1c1b8e14b5ba8ab
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 /*! \file x_colorcb.c
22 * \brief A GtkComboBox with the gschem colors.
24 #include <config.h>
26 #include <stdio.h>
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30 #ifdef HAVE_STRING_H
31 #include <string.h>
32 #endif
34 #include "gschem.h"
39 /*! \brief The columns in the GtkListStore
41 enum
43 COLUMN_NAME,
44 COLUMN_INDEX,
45 COLUMN_COLOR,
46 COLUMN_COUNT
51 /*! \brief Stores the list of colors for use in GtkComboBox
53 static GtkListStore* color_list_store = NULL;
57 static GtkListStore* create_color_list_store();
58 static const char* get_color_name(int index);
62 /*! \brief Create the GtkListStore of the avaialble colors.
64 static GtkListStore*
65 create_color_list_store ()
67 int color_index;
68 GtkTreeIter iter;
69 GtkListStore *store;
71 store = gtk_list_store_new (COLUMN_COUNT, G_TYPE_STRING, G_TYPE_INT, GDK_TYPE_COLOR);
73 for (color_index = 0; color_index < MAX_OBJECT_COLORS; color_index++) {
74 if (x_color_display_enabled (color_index)) {
75 gtk_list_store_append (store, &iter);
77 gtk_list_store_set (store, &iter,
78 COLUMN_NAME, get_color_name (color_index),
79 COLUMN_INDEX, color_index,
80 COLUMN_COLOR, x_get_color (color_index),
86 return store;
91 /*! \brief Given the color index, obtain a human readable name
93 static const char*
94 get_color_name (int index)
96 switch(index) {
97 case BACKGROUND_COLOR: return pgettext ("color", "Background");
98 case PIN_COLOR: return pgettext ("color", "Pin");
99 case NET_ENDPOINT_COLOR: return pgettext ("color", "Net endpoint");
100 case GRAPHIC_COLOR: return pgettext ("color", "Graphic");
101 case NET_COLOR: return pgettext ("color", "Net");
102 case ATTRIBUTE_COLOR: return pgettext ("color", "Attribute");
103 case LOGIC_BUBBLE_COLOR: return pgettext ("color", "Logic bubble");
104 case DOTS_GRID_COLOR: return pgettext ("color", "Grid point");
105 case DETACHED_ATTRIBUTE_COLOR:
106 return pgettext ("color", "Detached attribute");
107 case TEXT_COLOR: return pgettext ("color", "Text");
108 case BUS_COLOR: return pgettext ("color", "Bus");
109 case SELECT_COLOR: return pgettext ("color", "Selection");
110 case BOUNDINGBOX_COLOR: return pgettext ("color", "Bounding box");
111 case ZOOM_BOX_COLOR: return pgettext ("color", "Zoom box");
112 case STROKE_COLOR: return pgettext ("color", "Stroke");
113 case LOCK_COLOR: return pgettext ("color", "Lock");
114 case OUTPUT_BACKGROUND_COLOR:
115 return pgettext ("color", "Output background");
116 case FREESTYLE1_COLOR: return pgettext ("color", "User-defined #1");
117 case FREESTYLE2_COLOR: return pgettext ("color", "User-defined #2");
118 case FREESTYLE3_COLOR: return pgettext ("color", "User-defined #3");
119 case FREESTYLE4_COLOR: return pgettext ("color", "User-defined #4");
121 case JUNCTION_COLOR: return pgettext ("color", "Net junction");
122 case MESH_GRID_MAJOR_COLOR: return pgettext ("color", "Mesh grid major");
123 case MESH_GRID_MINOR_COLOR: return pgettext ("color", "Mesh grid minor");
124 case ORIGIN_COLOR: return pgettext ("color", "Origin marker");
125 case PLACE_ORIGIN_COLOR: return pgettext ("color", "Origin placement");
127 default:
128 break;
130 return pgettext ("color", "Unknown");
134 /*! \brief Create a ComboBox with the gschem colors.
136 * \return The currently selected color index
138 GtkWidget*
139 x_colorcb_new ()
141 GtkComboBox *combo;
142 GtkCellLayout *layout;
143 GtkCellRenderer *text_cell;
144 GtkCellRenderer *color_cell;
146 if (color_list_store == NULL) {
147 color_list_store = create_color_list_store ();
150 combo = GTK_COMBO_BOX (gtk_combo_box_new_with_model (GTK_TREE_MODEL (color_list_store)));
151 layout = GTK_CELL_LAYOUT (combo); /* For convenience */
153 /* Renders the color swatch. Since this won't contain text, set a
154 * minimum width. */
155 color_cell = GTK_CELL_RENDERER (gschem_swatch_column_renderer_new ());
156 g_object_set (color_cell, "width", 25, NULL);
157 gtk_cell_layout_pack_start (layout, color_cell, FALSE);
158 gtk_cell_layout_add_attribute (layout, color_cell, "color", COLUMN_COLOR);
160 /* Renders the name of the color */
161 text_cell = GTK_CELL_RENDERER (gtk_cell_renderer_text_new());
162 g_object_set (text_cell, "xpad", 5, NULL);
163 gtk_cell_layout_pack_start (layout, text_cell, TRUE);
164 gtk_cell_layout_add_attribute (layout, text_cell, "text", COLUMN_NAME);
166 return GTK_WIDGET (combo);
171 /*! \brief Get the currently selected color index
173 * \param [in,out] widget The color combo box
174 * \return The currently selected color index
177 x_colorcb_get_index (GtkWidget *widget)
179 int index = -1;
180 GtkTreeIter iter;
181 GValue value = {0};
183 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
184 gtk_tree_model_get_value (GTK_TREE_MODEL (color_list_store), &iter, COLUMN_INDEX, &value);
185 index = g_value_get_int (&value);
186 g_value_unset (&value);
189 return index;
194 /*! \brief Select the given color index
196 * \param [in,out] widget The color combo box
197 * \param [in] color_index The color index to select
199 void
200 x_colorcb_set_index (GtkWidget *widget, int color_index)
202 g_return_if_fail (color_list_store != NULL);
204 if (color_index >= 0) {
205 GtkTreeIter iter;
206 gboolean success = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (color_list_store), &iter);
207 GValue value = {0};
208 while (success) {
209 gtk_tree_model_get_value (GTK_TREE_MODEL (color_list_store), &iter, COLUMN_INDEX, &value);
210 if (g_value_get_int (&value) == color_index) {
211 g_value_unset (&value);
212 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), &iter);
213 break;
215 g_value_unset (&value);
216 success = gtk_tree_model_iter_next (GTK_TREE_MODEL(color_list_store), &iter);
219 else {
220 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), NULL);
226 /*! \brief Update the colors in the GtkListStore.
228 void
229 x_colorcb_update_store ()
231 if (color_list_store == NULL)
232 return;
234 GtkTreeIter iter;
235 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (color_list_store), &iter);
237 for (int i = 0; i < MAX_OBJECT_COLORS; i++) {
238 if (!x_color_display_enabled (i))
239 continue;
241 gtk_list_store_set (color_list_store, &iter,
242 COLUMN_COLOR, x_get_color (i),
243 -1);
244 gtk_tree_model_iter_next (GTK_TREE_MODEL (color_list_store), &iter);