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_linecapcb.c
21  *
22  *  \brief A GtkComboBox for gschem line cap styles.
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 line cap styles for use in GtkComboBox
51  */
52 static GtkListStore* line_cap_list_store = NULL;
53 
54 
55 
56 static GtkListStore* create_line_cap_list_store();
57 
58 
59 
60 /*! \brief Create the GtkListStore for line cap styles.
61  */
62 static GtkListStore*
create_line_cap_list_store()63 create_line_cap_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,       _("Butt"),
75     COLUMN_INDEX,      END_NONE,
76     -1
77     );
78 
79   gtk_list_store_append (store, &iter);
80   gtk_list_store_set (store, &iter,
81     COLUMN_NAME,       _("Square"),
82     COLUMN_INDEX,      END_SQUARE,
83     -1
84     );
85 
86   gtk_list_store_append (store, &iter);
87   gtk_list_store_set (store, &iter,
88     COLUMN_NAME,       _("Round"),
89     COLUMN_INDEX,      END_ROUND,
90     -1
91     );
92 
93   return store;
94 }
95 
96 
97 
98 /*! \brief Create a ComboBox with the gschem line cap styles.
99  *
100  *  \return GtkWidget
101  */
102 GtkWidget*
x_linecapcb_new()103 x_linecapcb_new ()
104 {
105   GtkComboBox *combo;
106   GtkCellLayout *layout;
107   GtkCellRenderer *text_cell;
108 
109   if (line_cap_list_store == NULL) {
110     line_cap_list_store = create_line_cap_list_store ();
111   }
112 
113   combo = GTK_COMBO_BOX (gtk_combo_box_new_with_model (GTK_TREE_MODEL (line_cap_list_store)));
114   layout = GTK_CELL_LAYOUT (combo); /* For convenience */
115 
116   /* Renders the name of the fill style */
117   text_cell = GTK_CELL_RENDERER (gtk_cell_renderer_text_new());
118   g_object_set (text_cell, "xpad", 5, NULL);
119   gtk_cell_layout_pack_start (layout, text_cell, TRUE);
120   gtk_cell_layout_add_attribute (layout, text_cell, "text", COLUMN_NAME);
121 
122   return GTK_WIDGET (combo);
123 }
124 
125 
126 
127 /*! \brief Get the currently selected line cap style index
128  *
129  *  \param [in,out] widget The line cap style combo box
130  *  \return The currently selected line cap style index
131  */
132 int
x_linecapcb_get_index(GtkWidget * widget)133 x_linecapcb_get_index (GtkWidget *widget)
134 {
135   int index = -1;
136   GtkTreeIter iter;
137   GValue value = {0};
138 
139   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (widget), &iter)) {
140     gtk_tree_model_get_value (GTK_TREE_MODEL (line_cap_list_store), &iter, COLUMN_INDEX, &value);
141     index = g_value_get_int (&value);
142     g_value_unset (&value);
143   }
144 
145   return index;
146 }
147 
148 
149 
150 /*! \brief Select the given line cap style index
151  *
152  *  \param [in,out] widget The line cap style combo box
153  *  \param [in]     index  The line cap style index to select
154  */
155 void
x_linecapcb_set_index(GtkWidget * widget,int index)156 x_linecapcb_set_index (GtkWidget *widget, int index)
157 {
158   GtkTreeIter *active = NULL;
159   GtkTreeIter iter;
160 
161   g_return_if_fail (widget != NULL);
162   g_return_if_fail (line_cap_list_store != NULL);
163 
164   if (index >= 0) {
165     gboolean success;
166     GValue value = {0};
167 
168     success = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (line_cap_list_store), &iter);
169     while (success) {
170       gtk_tree_model_get_value (GTK_TREE_MODEL (line_cap_list_store), &iter, COLUMN_INDEX, &value);
171       if (g_value_get_int (&value) == index) {
172         g_value_unset (&value);
173         active = &iter;
174         break;
175       }
176       g_value_unset (&value);
177       success = gtk_tree_model_iter_next (GTK_TREE_MODEL(line_cap_list_store), &iter);
178     }
179   }
180 
181   gtk_combo_box_set_active_iter (GTK_COMBO_BOX(widget), active);
182 }
183