missing NULL terminator in set_config_x
[geda-gaf.git] / gschem / src / gschem_show_hide_text_widget.c
blob96d0867c4e9fb34456e063dc908532bd9ff44edc
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_show_hide_text_widget.c
23 * \brief A widget for showing or hiding text
26 #include <config.h>
28 #ifdef HAVE_STDLIB_H
29 #include <stdlib.h>
30 #endif
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
35 #include <gdk/gdkkeysyms.h>
37 #include "gschem.h"
40 enum
42 PROP_0,
43 PROP_BUTTON_TEXT,
44 PROP_LABEL_TEXT,
45 PROP_TEXT_STRING
49 static gboolean
50 key_press_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data);
52 static void
53 activate_entry (GtkWidget *entry, GschemShowHideTextWidget *widget);
55 static void
56 click_cancel (GtkWidget *button, GschemShowHideTextWidget *widget);
58 static void
59 click_ok (GtkWidget *entry, GschemShowHideTextWidget *widget);
61 static void
62 dispose (GObject *object);
64 static void
65 finalize (GObject *object);
67 static void
68 get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec);
70 static void
71 gschem_show_hide_text_widget_class_init (GschemShowHideTextWidgetClass *klass);
73 static void
74 gschem_show_hide_text_widget_init (GschemShowHideTextWidget *view);
76 static void
77 set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec);
79 static void
80 notify_entry_text (GtkWidget *entry, GParamSpec *pspec, GschemShowHideTextWidget *widget);
83 static GObjectClass *gschem_show_hide_text_widget_parent_class = NULL;
86 /*! \brief Show the hide text widget
88 void hide_text_dialog (GschemToplevel *w_current)
90 OBJECT *object;
92 g_return_if_fail (w_current != NULL);
93 g_return_if_fail (w_current->toplevel != NULL);
95 object = o_select_return_first_object (w_current);
97 if ((object != NULL) && (object->type == OBJ_TEXT)) {
98 gschem_show_hide_text_widget_set_text_string(
99 GSCHEM_SHOW_HIDE_TEXT_WIDGET (w_current->hide_text_widget),
100 o_text_get_string (w_current->toplevel, object)
104 gtk_widget_show (GTK_WIDGET (w_current->hide_text_widget));
105 gtk_widget_grab_focus (gschem_show_hide_text_widget_get_entry (GSCHEM_SHOW_HIDE_TEXT_WIDGET (w_current->hide_text_widget)));
106 gtk_editable_select_region (GTK_EDITABLE (gschem_show_hide_text_widget_get_entry (GSCHEM_SHOW_HIDE_TEXT_WIDGET (w_current->hide_text_widget))), 0, -1);
110 /*! \brief show the show text widget
112 void show_text_dialog (GschemToplevel *w_current)
114 OBJECT *object;
116 g_return_if_fail (w_current != NULL);
117 g_return_if_fail (w_current->toplevel != NULL);
119 object = o_select_return_first_object (w_current);
121 if ((object != NULL) && (object->type == OBJ_TEXT)) {
122 gschem_show_hide_text_widget_set_text_string(
123 GSCHEM_SHOW_HIDE_TEXT_WIDGET (w_current->show_text_widget),
124 o_text_get_string (w_current->toplevel, object)
128 gtk_widget_show (GTK_WIDGET (w_current->show_text_widget));
129 gtk_widget_grab_focus (gschem_show_hide_text_widget_get_entry (GSCHEM_SHOW_HIDE_TEXT_WIDGET (w_current->show_text_widget)));
130 gtk_editable_select_region (GTK_EDITABLE (gschem_show_hide_text_widget_get_entry (GSCHEM_SHOW_HIDE_TEXT_WIDGET (w_current->show_text_widget))), 0, -1);
134 /*! \brief Initialize GschemShowHideTextWidget class
136 * \param [in] klass The class for the GschemShowHideTextWidget
138 static void
139 gschem_show_hide_text_widget_class_init (GschemShowHideTextWidgetClass *klass)
141 gschem_show_hide_text_widget_parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
143 G_OBJECT_CLASS (klass)->dispose = dispose;
144 G_OBJECT_CLASS (klass)->finalize = finalize;
146 G_OBJECT_CLASS (klass)->get_property = get_property;
147 G_OBJECT_CLASS (klass)->set_property = set_property;
149 g_object_class_install_property (G_OBJECT_CLASS (klass),
150 PROP_BUTTON_TEXT,
151 g_param_spec_string ("button-text",
152 "Button Text",
153 "Button Text",
154 pgettext ("actuate", "Hide"),
155 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
157 g_object_class_install_property (G_OBJECT_CLASS (klass),
158 PROP_LABEL_TEXT,
159 g_param_spec_string ("label-text",
160 "Label Text",
161 "Label Text",
162 _("Hide text starting with:"),
163 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
165 g_object_class_install_property (G_OBJECT_CLASS (klass),
166 PROP_TEXT_STRING,
167 g_param_spec_string ("text-string",
168 "Text String",
169 "Text String",
171 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
175 /*! \brief Get the button label text
177 * \param [in] widget This GschemShowHideTextWidget
178 * \return The button label text
180 const char*
181 gschem_show_hide_text_widget_get_button_text (GschemShowHideTextWidget *widget)
183 g_return_val_if_fail (widget != NULL, FALSE);
185 return gtk_button_get_label (GTK_BUTTON (widget->ok_button));
189 /*! \brief Get the entry
191 * \param [in] widget This GschemShowHideTextWidget
192 * \return The entry
194 GtkWidget*
195 gschem_show_hide_text_widget_get_entry (GschemShowHideTextWidget *widget)
197 g_return_val_if_fail (widget != NULL, NULL);
199 return widget->entry;
204 /*! \brief Get the label text
206 * \param [in] widget This GschemShowHideTextWidget
207 * \return The label text
209 const char*
210 gschem_show_hide_text_widget_get_label_text (GschemShowHideTextWidget *widget)
212 g_return_val_if_fail (widget != NULL, NULL);
214 return gtk_label_get_label (GTK_LABEL (widget->label));
219 /*! \brief Get the find text string
221 * \param [in] widget This GschemShowHideTextWidget
222 * \return The find text string
224 const char*
225 gschem_show_hide_text_widget_get_text_string (GschemShowHideTextWidget *widget)
227 g_return_val_if_fail (widget != NULL, NULL);
229 return gtk_entry_get_text (GTK_ENTRY (widget->entry));
234 /*! \brief Get/register GschemShowHideTextWidget type.
236 GType
237 gschem_show_hide_text_widget_get_type ()
239 static GType type = 0;
241 if (type == 0) {
242 static const GTypeInfo info = {
243 sizeof(GschemShowHideTextWidgetClass),
244 NULL, /* base_init */
245 NULL, /* base_finalize */
246 (GClassInitFunc) gschem_show_hide_text_widget_class_init,
247 NULL, /* class_finalize */
248 NULL, /* class_data */
249 sizeof(GschemShowHideTextWidget),
250 0, /* n_preallocs */
251 (GInstanceInitFunc) gschem_show_hide_text_widget_init,
254 type = g_type_register_static (GTK_TYPE_INFO_BAR,
255 "GschemShowHideTextWidget",
256 &info,
260 return type;
265 /*! \brief Initialize GschemShowHideTextWidget instance
267 * \param [in,out] view the GschemShowHideTextWidget
269 static void
270 gschem_show_hide_text_widget_init (GschemShowHideTextWidget *widget)
272 GtkWidget *action = gtk_info_bar_get_action_area (GTK_INFO_BAR (widget));
273 GtkWidget *button_box;
274 GtkWidget *cancel_button;
275 GtkWidget *content = gtk_info_bar_get_content_area (GTK_INFO_BAR (widget));
277 g_return_if_fail (widget != NULL);
279 gtk_widget_set_no_show_all (GTK_WIDGET (widget), TRUE);
281 widget->label = gtk_label_new (NULL);
282 gtk_widget_set_visible (widget->label, TRUE);
283 gtk_box_pack_start (GTK_BOX (content), widget->label, FALSE, FALSE, 0);
285 widget->entry = gtk_entry_new ();
286 gtk_widget_set_visible (widget->entry, TRUE);
287 gtk_box_pack_start (GTK_BOX (content), widget->entry, TRUE, TRUE, 0);
289 button_box = gtk_hbutton_box_new ();
290 gtk_widget_set_visible (button_box, TRUE);
291 gtk_box_pack_start (GTK_BOX (content), button_box, FALSE, FALSE, 0);
293 widget->ok_button = gtk_button_new ();
294 gtk_widget_set_sensitive (widget->ok_button, FALSE);
295 gtk_widget_set_visible (widget->ok_button, TRUE);
296 gtk_box_pack_start (GTK_BOX (button_box), widget->ok_button, FALSE, FALSE, 0);
298 cancel_button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
299 gtk_widget_set_visible (cancel_button, TRUE);
300 gtk_box_pack_start (GTK_BOX (button_box), cancel_button, FALSE, FALSE, 0);
302 gtk_widget_set_no_show_all (action, TRUE);
303 gtk_widget_set_visible (action, FALSE);
305 g_signal_connect (G_OBJECT (widget),
306 "key-press-event",
307 G_CALLBACK (key_press_event),
308 widget);
310 g_signal_connect (G_OBJECT (widget->entry),
311 "activate",
312 G_CALLBACK (activate_entry),
313 widget);
315 g_signal_connect (G_OBJECT (cancel_button),
316 "clicked",
317 G_CALLBACK (click_cancel),
318 widget);
320 g_signal_connect (G_OBJECT (widget->ok_button),
321 "clicked",
322 G_CALLBACK (click_ok),
323 widget);
325 g_signal_connect (G_OBJECT (widget->entry),
326 "notify::text",
327 G_CALLBACK (notify_entry_text),
328 widget);
333 /*! \brief Set the ok button text
335 * \param [in,out] view This GschemShowHideTextWidget
336 * \param [in] text The button text
338 void
339 gschem_show_hide_text_widget_set_button_text (GschemShowHideTextWidget *widget, const char *text)
341 g_return_if_fail (widget != NULL);
343 gtk_button_set_label (GTK_BUTTON (widget->ok_button), text);
345 g_object_notify (G_OBJECT (widget), "button-text");
350 /*! \brief Set the label text
352 * \param [in,out] view This GschemShowHideTextWidget
353 * \param [in] text The label text
355 void
356 gschem_show_hide_text_widget_set_label_text (GschemShowHideTextWidget *widget, const char *text)
358 g_return_if_fail (widget != NULL);
360 gtk_label_set_text (GTK_LABEL (widget->label), text);
362 g_object_notify (G_OBJECT (widget), "label-text");
367 /*! \brief Set the find text string
369 * \param [in,out] view This GschemShowHideTextWidget
370 * \param [in] str The find text string
372 void
373 gschem_show_hide_text_widget_set_text_string (GschemShowHideTextWidget *widget, const char *str)
375 g_return_if_fail (widget != NULL);
377 gtk_entry_set_text (GTK_ENTRY (widget->entry), str);
379 g_object_notify (G_OBJECT (widget), "text-string");
384 /* Callback for when the user presses a key in the infobar
386 static gboolean
387 key_press_event (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
389 if (event->keyval == GDK_KEY_Escape &&
390 (event->state & gtk_accelerator_get_default_mod_mask ()) == 0) {
391 gtk_info_bar_response (GTK_INFO_BAR (user_data), GTK_RESPONSE_CANCEL);
392 return TRUE;
395 return FALSE;
400 /* Callback for when the user presses enter in the entry widget
402 static void
403 activate_entry (GtkWidget *entry, GschemShowHideTextWidget *widget)
405 g_return_if_fail (widget != NULL);
407 if (gtk_entry_get_text_length (GTK_ENTRY (widget->entry)) > 0) {
408 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_OK);
410 else {
411 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_CANCEL);
417 /* Callback for when the user clicks the cancel button
419 static void
420 click_cancel (GtkWidget *button, GschemShowHideTextWidget *widget)
422 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_CANCEL);
427 /* Callback for when the user clicks the find button
429 static void
430 click_ok (GtkWidget *entry, GschemShowHideTextWidget *widget)
432 g_return_if_fail (widget != NULL);
434 if (gtk_entry_get_text_length (GTK_ENTRY (widget->entry)) > 0) {
435 gtk_info_bar_response (GTK_INFO_BAR (widget), GTK_RESPONSE_OK);
441 /*! \brief Dispose of the object
443 static void
444 dispose (GObject *object)
446 /* lastly, chain up to the parent dispose */
448 g_return_if_fail (gschem_show_hide_text_widget_parent_class != NULL);
449 gschem_show_hide_text_widget_parent_class->dispose (object);
454 /*! \brief Finalize object
456 static void
457 finalize (GObject *object)
459 /* lastly, chain up to the parent finalize */
461 g_return_if_fail (gschem_show_hide_text_widget_parent_class != NULL);
462 gschem_show_hide_text_widget_parent_class->finalize (object);
466 /*! \brief Get a property
468 * \param [in] object
469 * \param [in] param_id
470 * \param [in,out] value
471 * \param [in] pspec
473 static void
474 get_property (GObject *object, guint param_id, GValue *value, GParamSpec *pspec)
476 GschemShowHideTextWidget *widget = GSCHEM_SHOW_HIDE_TEXT_WIDGET (object);
478 switch (param_id) {
479 case PROP_BUTTON_TEXT:
480 g_value_set_string (value, gschem_show_hide_text_widget_get_button_text (widget));
481 break;
483 case PROP_LABEL_TEXT:
484 g_value_set_string (value, gschem_show_hide_text_widget_get_label_text (widget));
485 break;
487 case PROP_TEXT_STRING:
488 g_value_set_string (value, gschem_show_hide_text_widget_get_text_string (widget));
489 break;
491 default:
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
497 /*! \brief Update the sensitivity of the find button
499 static void
500 notify_entry_text (GtkWidget *entry, GParamSpec *pspec, GschemShowHideTextWidget *widget)
502 g_return_if_fail (widget != NULL);
504 gtk_widget_set_sensitive (widget->ok_button,
505 (gtk_entry_get_text_length (GTK_ENTRY (widget->entry)) > 0));
510 /*! \brief Set a gobject property
512 static void
513 set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec)
515 GschemShowHideTextWidget *widget = GSCHEM_SHOW_HIDE_TEXT_WIDGET (object);
517 switch (param_id) {
518 case PROP_BUTTON_TEXT:
519 gschem_show_hide_text_widget_set_button_text (widget, g_value_get_string (value));
520 break;
522 case PROP_LABEL_TEXT:
523 gschem_show_hide_text_widget_set_label_text (widget, g_value_get_string (value));
524 break;
526 case PROP_TEXT_STRING:
527 gschem_show_hide_text_widget_set_text_string (widget, g_value_get_string (value));
528 break;
530 default:
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);