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
21 * \file gschem_macro_widget.c
23 * \brief A widget for entering macros
39 #include <gdk/gdkkeysyms.h>
56 key_press_event (GtkWidget
*widget
, GdkEventKey
*event
, gpointer user_data
);
59 activate_entry (GtkWidget
*entry
, GschemMacroWidget
*widget
);
62 click_cancel (GtkWidget
*button
, GschemMacroWidget
*widget
);
65 click_evaluate (GtkWidget
*entry
, GschemMacroWidget
*widget
);
68 dispose (GObject
*object
);
71 finalize (GObject
*object
);
74 get_property (GObject
*object
, guint param_id
, GValue
*value
, GParamSpec
*pspec
);
77 gschem_macro_widget_class_init (GschemMacroWidgetClass
*klass
);
80 gschem_macro_widget_init (GschemMacroWidget
*view
);
83 set_property (GObject
*object
, guint param_id
, const GValue
*value
, GParamSpec
*pspec
);
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
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
);
110 /* Callback for when the user presses enter in the entry widget
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
);
121 gtk_info_bar_response (GTK_INFO_BAR (widget
), GTK_RESPONSE_CANCEL
);
127 /* Callback for when the user clicks the cancel button
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
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
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
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
180 * \param [in] param_id
181 * \param [in,out] value
185 get_property (GObject
*object
, guint param_id
, GValue
*value
, GParamSpec
*pspec
)
187 GschemMacroWidget
*widget
= GSCHEM_MACRO_WIDGET (object
);
190 case PROP_LABEL_TEXT
:
191 g_value_set_string (value
, gschem_macro_widget_get_label_text (widget
));
194 case PROP_MACRO_STRING
:
195 g_value_set_string (value
, gschem_macro_widget_get_macro_string (widget
));
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
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
),
222 g_param_spec_string ("label-text",
226 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
228 g_object_class_install_property (G_OBJECT_CLASS (klass
),
230 g_param_spec_string ("macro-string",
234 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
239 /*! \brief Get the entry
241 * \param [in] widget This GschemMacroWidget
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
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
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.
287 gschem_macro_widget_get_type ()
289 static GType 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
),
301 (GInstanceInitFunc
) gschem_macro_widget_init
,
304 type
= g_type_register_static (GTK_TYPE_INFO_BAR
, "GschemMacroWidget", &info
, 0);
312 /*! \brief Initialize GschemMacroWidget instance
314 * \param [in,out] view the GschemMacroWidget
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
),
354 G_CALLBACK (key_press_event
),
357 g_signal_connect (G_OBJECT (widget
->entry
),
359 G_CALLBACK (activate_entry
),
362 g_signal_connect (G_OBJECT (cancel_button
),
364 G_CALLBACK (click_cancel
),
367 g_signal_connect (G_OBJECT (widget
->evaluate_button
),
369 G_CALLBACK (click_evaluate
),
372 g_signal_connect (G_OBJECT (widget
->entry
),
374 G_CALLBACK (notify_entry_text
),
380 /*! \brief Set the label text
382 * \param [in,out] view This GschemMacroWidget
383 * \param [in] text The label text
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
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
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
430 set_property (GObject
*object
, guint param_id
, const GValue
*value
, GParamSpec
*pspec
)
432 GschemMacroWidget
*widget
= GSCHEM_MACRO_WIDGET (object
);
435 case PROP_LABEL_TEXT
:
436 gschem_macro_widget_set_label_text (widget
, g_value_get_string (value
));
439 case PROP_MACRO_STRING
:
440 gschem_macro_widget_set_macro_string (widget
, g_value_get_string (value
));
444 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);