missing NULL terminator in set_config_x
[geda-gaf.git] / gschem / src / gschem_macro_widget.c
blob3903f84ab488642ffee30185586089814151451a
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2010 Ales Hvezda
4 * Copyright (C) 1998-2020 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 /*!
21 * \file gschem_macro_widget.c
23 * \brief A widget for entering macros
26 #include <config.h>
28 #include <stdio.h>
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
35 #ifdef HAVE_MATH_H
36 #include <math.h>
37 #endif
39 #include <gdk/gdkkeysyms.h>
41 #include "gschem.h"
45 enum
47 PROP_0,
48 PROP_LABEL_TEXT,
49 PROP_MACRO_STRING
55 static gboolean
56 key_press_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data);
58 static void
59 activate_entry (GtkWidget *entry, GschemMacroWidget *widget);
61 static void
62 click_cancel (GtkWidget *button, GschemMacroWidget *widget);
64 static void
65 click_evaluate (GtkWidget *entry, GschemMacroWidget *widget);
67 static void
68 dispose (GObject *object);
70 static void
71 finalize (GObject *object);
73 static void
74 get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec);
76 static void
77 gschem_macro_widget_class_init (GschemMacroWidgetClass *klass);
79 static void
80 gschem_macro_widget_init (GschemMacroWidget *view);
82 static void
83 set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec);
85 static void
86 notify_entry_text (GtkWidget *entry, GParamSpec *pspec, GschemMacroWidget *widget);
90 static GObjectClass *gschem_macro_widget_parent_class = NULL;
94 /* Callback for when the user presses a key in the infobar
96 static gboolean
97 key_press_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
99 if (event->keyval == GDK_KEY_Escape &&
100 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0) {
101 gtk_info_bar_response (GTK_INFO_BAR (user_data), GTK_RESPONSE_CANCEL);
102 return TRUE;
105 return FALSE;
110 /* Callback for when the user presses enter in the entry widget
112 static void
113 activate_entry (GtkWidget *entry, GschemMacroWidget *widget)
115 g_return_if_fail (widget != NULL);
117 if (gtk_entry_get_text_length (GTK_ENTRY (widget->entry)) > 0) {
118 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_OK);
120 else {
121 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_CANCEL);
127 /* Callback for when the user clicks the cancel button
129 static void
130 click_cancel (GtkWidget *button, GschemMacroWidget *widget)
132 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_CANCEL);
137 /* Callback for when the user clicks the evaluate button
139 static void
140 click_evaluate (GtkWidget *entry, GschemMacroWidget *widget)
142 g_return_if_fail (widget != NULL);
144 if (gtk_entry_get_text_length (GTK_ENTRY (widget->entry)) > 0) {
145 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_OK);
151 /*! \brief Dispose of the object
153 static void
154 dispose (GObject *object)
156 /* lastly, chain up to the parent dispose */
158 g_return_if_fail (gschem_macro_widget_parent_class != NULL);
159 gschem_macro_widget_parent_class->dispose (object);
164 /*! \brief Finalize object
166 static void
167 finalize (GObject *object)
169 /* lastly, chain up to the parent finalize */
171 g_return_if_fail (gschem_macro_widget_parent_class != NULL);
172 gschem_macro_widget_parent_class->finalize (object);
177 /*! \brief Get a property
179 * \param [in] object
180 * \param [in] param_id
181 * \param [in,out] value
182 * \param [in] pspec
184 static void
185 get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec)
187 GschemMacroWidget *widget = GSCHEM_MACRO_WIDGET (object);
189 switch (param_id) {
190 case PROP_LABEL_TEXT:
191 g_value_set_string (value, gschem_macro_widget_get_label_text (widget));
192 break;
194 case PROP_MACRO_STRING:
195 g_value_set_string (value, gschem_macro_widget_get_macro_string (widget));
196 break;
198 default:
199 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
205 /*! \brief Initialize GschemMacroWidget class
207 * \param [in] klass The class for the GschemMacroWidget
209 static void
210 gschem_macro_widget_class_init (GschemMacroWidgetClass *klass)
212 gschem_macro_widget_parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
214 G_OBJECT_CLASS (klass)->dispose = dispose;
215 G_OBJECT_CLASS (klass)->finalize = finalize;
217 G_OBJECT_CLASS (klass)->get_property = get_property;
218 G_OBJECT_CLASS (klass)->set_property = set_property;
220 g_object_class_install_property (G_OBJECT_CLASS (klass),
221 PROP_LABEL_TEXT,
222 g_param_spec_string ("label-text",
223 "Label Text",
224 "Label Text",
225 _("Macro:"),
226 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
228 g_object_class_install_property (G_OBJECT_CLASS (klass),
229 PROP_MACRO_STRING,
230 g_param_spec_string ("macro-string",
231 "Macro String",
232 "Macro String",
234 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
239 /*! \brief Get the entry
241 * \param [in] widget This GschemMacroWidget
242 * \return The entry
244 GtkWidget*
245 gschem_macro_widget_get_entry (GschemMacroWidget *widget)
247 g_return_val_if_fail (widget != NULL, NULL);
249 return widget->entry;
254 /*! \brief Get the label text
256 * \param [in] widget This GschemMacroWidget
257 * \return The label text
259 const char*
260 gschem_macro_widget_get_label_text (GschemMacroWidget *widget)
262 g_return_val_if_fail (widget != NULL, NULL);
264 return gtk_label_get_text (GTK_LABEL (widget->label));
269 /*! \brief Get the macro string
271 * \param [in] widget This GschemMacroWidget
272 * \return The macro string
274 const char*
275 gschem_macro_widget_get_macro_string (GschemMacroWidget *widget)
277 g_return_val_if_fail (widget != NULL, NULL);
279 return gtk_entry_get_text (GTK_ENTRY (widget->entry));
284 /*! \brief Get/register GschemMacroWidget type.
286 GType
287 gschem_macro_widget_get_type ()
289 static GType type = 0;
291 if (type == 0) {
292 static const GTypeInfo info = {
293 sizeof(GschemMacroWidgetClass),
294 NULL, /* base_init */
295 NULL, /* base_finalize */
296 (GClassInitFunc) gschem_macro_widget_class_init,
297 NULL, /* class_finalize */
298 NULL, /* class_data */
299 sizeof(GschemMacroWidget),
300 0, /* n_preallocs */
301 (GInstanceInitFunc) gschem_macro_widget_init,
304 type = g_type_register_static (GTK_TYPE_INFO_BAR, "GschemMacroWidget", &info, 0);
307 return type;
312 /*! \brief Initialize GschemMacroWidget instance
314 * \param [in,out] view the GschemMacroWidget
316 static void
317 gschem_macro_widget_init (GschemMacroWidget *widget)
319 GtkWidget *action = gtk_info_bar_get_action_area (GTK_INFO_BAR (widget));
320 GtkWidget *button_box;
321 GtkWidget *cancel_button;
322 GtkWidget *content = gtk_info_bar_get_content_area (GTK_INFO_BAR (widget));
324 g_return_if_fail (widget != NULL);
326 gtk_widget_set_no_show_all (GTK_WIDGET (widget), TRUE);
328 widget->label = gtk_label_new (NULL);
329 gtk_widget_set_visible (widget->label, TRUE);
330 gtk_box_pack_start (GTK_BOX (content), widget->label, FALSE, FALSE, 0);
332 widget->entry = gtk_entry_new ();
333 gtk_widget_set_visible (widget->entry, TRUE);
334 gtk_box_pack_start (GTK_BOX (content), widget->entry, TRUE, TRUE, 0);
336 button_box = gtk_hbutton_box_new ();
337 gtk_widget_set_visible (button_box, TRUE);
338 gtk_box_pack_start (GTK_BOX (content), button_box, FALSE, FALSE, 0);
340 widget->evaluate_button = gtk_button_new_with_label (pgettext ("actuate", "Evaluate"));
341 gtk_widget_set_sensitive (widget->evaluate_button, FALSE);
342 gtk_widget_set_visible (widget->evaluate_button, TRUE);
343 gtk_box_pack_start (GTK_BOX (button_box), widget->evaluate_button, FALSE, FALSE, 0);
345 cancel_button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
346 gtk_widget_set_visible (cancel_button, TRUE);
347 gtk_box_pack_start (GTK_BOX (button_box), cancel_button, FALSE, FALSE, 0);
349 gtk_widget_set_no_show_all (action, TRUE);
350 gtk_widget_set_visible (action, FALSE);
352 g_signal_connect (G_OBJECT (widget),
353 "key-press-event",
354 G_CALLBACK (key_press_event),
355 widget);
357 g_signal_connect (G_OBJECT (widget->entry),
358 "activate",
359 G_CALLBACK (activate_entry),
360 widget);
362 g_signal_connect (G_OBJECT (cancel_button),
363 "clicked",
364 G_CALLBACK (click_cancel),
365 widget);
367 g_signal_connect (G_OBJECT (widget->evaluate_button),
368 "clicked",
369 G_CALLBACK (click_evaluate),
370 widget);
372 g_signal_connect (G_OBJECT (widget->entry),
373 "notify::text",
374 G_CALLBACK (notify_entry_text),
375 widget);
380 /*! \brief Set the label text
382 * \param [in,out] view This GschemMacroWidget
383 * \param [in] text The label text
385 void
386 gschem_macro_widget_set_label_text (GschemMacroWidget *widget, const char *text)
388 g_return_if_fail (widget != NULL);
390 gtk_label_set_text (GTK_LABEL (widget->label), text);
392 g_object_notify (G_OBJECT (widget), "label-text");
397 /*! \brief Set the macro string
399 * \param [in,out] view This GschemMacroWidget
400 * \param [in] str The macro string
402 void
403 gschem_macro_widget_set_macro_string (GschemMacroWidget *widget, const char *str)
405 g_return_if_fail (widget != NULL);
407 gtk_entry_set_text (GTK_ENTRY (widget->entry), str);
409 g_object_notify (G_OBJECT (widget), "macro-string");
414 /*! \brief Update the sensitivity of the evaluate button
416 static void
417 notify_entry_text (GtkWidget *entry, GParamSpec *pspec, GschemMacroWidget *widget)
419 g_return_if_fail (widget != NULL);
421 gtk_widget_set_sensitive (widget->evaluate_button,
422 (gtk_entry_get_text_length (GTK_ENTRY (widget->entry)) > 0));
427 /*! \brief Set a gobject property
429 static void
430 set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
432 GschemMacroWidget *widget = GSCHEM_MACRO_WIDGET (object);
434 switch (param_id) {
435 case PROP_LABEL_TEXT:
436 gschem_macro_widget_set_label_text (widget, g_value_get_string (value));
437 break;
439 case PROP_MACRO_STRING:
440 gschem_macro_widget_set_macro_string (widget, g_value_get_string (value));
441 break;
443 default:
444 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);