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
22 * \brief A GtkComboBox with the gschem colors.
39 /*! \brief The columns in the GtkListStore
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.
65 create_color_list_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
),
91 /*! \brief Given the color index, obtain a human readable name
94 get_color_name (int 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");
130 return pgettext ("color", "Unknown");
134 /*! \brief Create a ComboBox with the gschem colors.
136 * \return The currently selected color index
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
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
)
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
);
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
200 x_colorcb_set_index (GtkWidget
*widget
, int color_index
)
202 g_return_if_fail (color_list_store
!= NULL
);
204 if (color_index
>= 0) {
206 gboolean success
= gtk_tree_model_get_iter_first (GTK_TREE_MODEL (color_list_store
), &iter
);
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
);
215 g_value_unset (&value
);
216 success
= gtk_tree_model_iter_next (GTK_TREE_MODEL(color_list_store
), &iter
);
220 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget
), NULL
);
226 /*! \brief Update the colors in the GtkListStore.
229 x_colorcb_update_store ()
231 if (color_list_store
== NULL
)
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
))
241 gtk_list_store_set (color_list_store
, &iter
,
242 COLUMN_COLOR
, x_get_color (i
),
244 gtk_tree_model_iter_next (GTK_TREE_MODEL (color_list_store
), &iter
);