1 /* Lepton EDA Schematic Capture
2  * Copyright (C) 1998-2010 Ales Hvezda
3  * Copyright (C) 1998-2015 gEDA Contributors
4  * Copyright (C) 2017-2020 Lepton EDA Contributors
5  *
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.
10  *
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.
15  *
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
19  */
20 /*! \file x_linetypecb.c
21  *
22  *  \brief A GtkComboBox for gschem line types.
23  */
24 #include <config.h>
25 
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
33 
34 #include "gschem.h"
35 
36 
37 
38 
39 /*! \brief The columns in the GtkListStore
40  */
41 enum
42 {
43     COLUMN_NAME,
44     COLUMN_INDEX,
45     COLUMN_COUNT
46 };
47 
48 
49 
50 /*! \brief Stores the list of fill styles for use in GtkComboBox
51  */
52 static GtkListStore* line_type_list_store = NULL;
53 
54 
55 
56 static GtkListStore* create_line_type_list_store();
57 
58 
59 
60 /*! \brief Create the GtkListStore for fill styles.
61  */
62 static GtkListStore*
create_line_type_list_store()63 create_line_type_list_store ()
64 {
65   GtkTreeIter iter;
66   GtkListStore *store;
67 
68   store = gtk_list_store_new (COLUMN_COUNT,
69                               G_TYPE_STRING,
70                               G_TYPE_INT);
71 
72   gtk_list_store_append (store, &iter);
73   gtk_list_store_set (store, &iter,
74     COLUMN_NAME,       _("Solid"),
75     COLUMN_INDEX,      TYPE_SOLID,
76     -1
77     );
78 
79   gtk_list_store_append (store, &iter);
80   gtk_list_store_set (store, &iter,
81     COLUMN_NAME,       _("Dotted"),
82     COLUMN_INDEX,      TYPE_DOTTED,
83     -1
84     );
85 
86   gtk_list_store_append (store, &iter);
87   gtk_list_store_set (store, &iter,
88     COLUMN_NAME,       _("Dashed"),
89     COLUMN_INDEX,      TYPE_DASHED,
90     -1
91     );
92 
93   gtk_list_store_append (store, &iter);
94   gtk_list_store_set (store, &iter,
95     COLUMN_NAME,       _("Center"),
96     COLUMN_INDEX,      TYPE_CENTER,
97     -1
98     );
99 
100   gtk_list_store_append (store, &iter);
101   gtk_list_store_set (store, &iter,
102     COLUMN_NAME,       _("Phantom"),
103     COLUMN_INDEX,      TYPE_PHANTOM,
104     -1
105     );
106 
107   return store;
108 }
109 
110 
111 
112 /*! \brief Create a ComboBox with the gschem line types.
113  *
114  *  \return GtkWidget
115  */
116 GtkWidget*
x_linetypecb_new()117 x_linetypecb_new ()
118 {
119   GtkComboBox *combo;
120   GtkCellLayout *layout;
121   GtkCellRenderer *text_cell;
122 
123   if (line_type_list_store == NULL) {
124     line_type_list_store = create_line_type_list_store ();
125   }
126 
127   combo = GTK_COMBO_BOX (gtk_combo_box_new_with_model (GTK_TREE_MODEL (line_type_list_store)));
128   layout = GTK_CELL_LAYOUT (combo); /* For convenience */
129 
130   /* Renders the name of the fill style */
131   text_cell = GTK_CELL_RENDERER (gtk_cell_renderer_text_new());
132   g_object_set (text_cell, "xpad", 5, NULL);
133   gtk_cell_layout_pack_start (layout, text_cell, TRUE);
134   gtk_cell_layout_add_attribute (layout, text_cell, "text", COLUMN_NAME);
135 
136   return GTK_WIDGET (combo);
137 }
138 
139 
140 
141 /*! \brief Get the currently selected line type index
142  *
143  *  \param [in,out] widget The line type combo box
144  *  \return The currently selected line type index
145  */
146 int
x_linetypecb_get_index(GtkWidget * widget)147 x_linetypecb_get_index (GtkWidget *widget)
148 {
149   int index = -1;
150   GtkTreeIter iter;
151   GValue value = {0};
152 
153   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
154     gtk_tree_model_get_value (GTK_TREE_MODEL (line_type_list_store), &iter, COLUMN_INDEX, &value);
155     index = g_value_get_int (&value);
156     g_value_unset (&value);
157   }
158 
159   return index;
160 }
161 
162 
163 
164 /*! \brief Select the given line type index
165  *
166  *  \param [in,out] widget The line type combo box
167  *  \param [in]     style  The line type index to select
168  */
169 void
x_linetypecb_set_index(GtkWidget * widget,int index)170 x_linetypecb_set_index (GtkWidget *widget, int index)
171 {
172   GtkTreeIter *active = NULL;
173   GtkTreeIter iter;
174 
175   g_return_if_fail (widget != NULL);
176   g_return_if_fail (line_type_list_store != NULL);
177 
178   if (index >= 0) {
179     gboolean success;
180     GValue value = {0};
181 
182     success = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (line_type_list_store), &iter);
183     while (success) {
184       gtk_tree_model_get_value (GTK_TREE_MODEL (line_type_list_store), &iter, COLUMN_INDEX, &value);
185       if (g_value_get_int (&value) == index) {
186         g_value_unset (&value);
187         active = &iter;
188         break;
189       }
190       g_value_unset (&value);
191       success = gtk_tree_model_iter_next (GTK_TREE_MODEL(line_type_list_store), &iter);
192     }
193   }
194 
195   gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), active);
196 }
197