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_show_hide_text_widget.c
23 * \brief A widget for showing or hiding text
35 #include <gdk/gdkkeysyms.h>
50 key_press_event (GtkWidget
*widget
, GdkEventKey
*event
, gpointer user_data
);
53 activate_entry (GtkWidget
*entry
, GschemShowHideTextWidget
*widget
);
56 click_cancel (GtkWidget
*button
, GschemShowHideTextWidget
*widget
);
59 click_ok (GtkWidget
*entry
, GschemShowHideTextWidget
*widget
);
62 dispose (GObject
*object
);
65 finalize (GObject
*object
);
68 get_property (GObject
*object
, guint param_id
, GValue
*value
, GParamSpec
*pspec
);
71 gschem_show_hide_text_widget_class_init (GschemShowHideTextWidgetClass
*klass
);
74 gschem_show_hide_text_widget_init (GschemShowHideTextWidget
*view
);
77 set_property (GObject
*object
, guint param_id
, const GValue
*value
, GParamSpec
*pspec
);
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
)
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
)
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
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
),
151 g_param_spec_string ("button-text",
154 pgettext ("actuate", "Hide"),
155 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
157 g_object_class_install_property (G_OBJECT_CLASS (klass
),
159 g_param_spec_string ("label-text",
162 _("Hide text starting with:"),
163 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
165 g_object_class_install_property (G_OBJECT_CLASS (klass
),
167 g_param_spec_string ("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
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
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
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
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.
237 gschem_show_hide_text_widget_get_type ()
239 static GType 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
),
251 (GInstanceInitFunc
) gschem_show_hide_text_widget_init
,
254 type
= g_type_register_static (GTK_TYPE_INFO_BAR
,
255 "GschemShowHideTextWidget",
265 /*! \brief Initialize GschemShowHideTextWidget instance
267 * \param [in,out] view the GschemShowHideTextWidget
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
),
307 G_CALLBACK (key_press_event
),
310 g_signal_connect (G_OBJECT (widget
->entry
),
312 G_CALLBACK (activate_entry
),
315 g_signal_connect (G_OBJECT (cancel_button
),
317 G_CALLBACK (click_cancel
),
320 g_signal_connect (G_OBJECT (widget
->ok_button
),
322 G_CALLBACK (click_ok
),
325 g_signal_connect (G_OBJECT (widget
->entry
),
327 G_CALLBACK (notify_entry_text
),
333 /*! \brief Set the ok button text
335 * \param [in,out] view This GschemShowHideTextWidget
336 * \param [in] text The button text
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
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
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
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
);
400 /* Callback for when the user presses enter in the entry widget
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
);
411 gtk_info_bar_response (GTK_INFO_BAR (widget
), GTK_RESPONSE_CANCEL
);
417 /* Callback for when the user clicks the cancel button
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
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
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
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
469 * \param [in] param_id
470 * \param [in,out] value
474 get_property (GObject
*object
, guint param_id
, GValue
*value
, GParamSpec
*pspec
)
476 GschemShowHideTextWidget
*widget
= GSCHEM_SHOW_HIDE_TEXT_WIDGET (object
);
479 case PROP_BUTTON_TEXT
:
480 g_value_set_string (value
, gschem_show_hide_text_widget_get_button_text (widget
));
483 case PROP_LABEL_TEXT
:
484 g_value_set_string (value
, gschem_show_hide_text_widget_get_label_text (widget
));
487 case PROP_TEXT_STRING
:
488 g_value_set_string (value
, gschem_show_hide_text_widget_get_text_string (widget
));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);
497 /*! \brief Update the sensitivity of the find button
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
513 set_property (GObject
*object
, guint param_id
, const GValue
*value
, GParamSpec
*pspec
)
515 GschemShowHideTextWidget
*widget
= GSCHEM_SHOW_HIDE_TEXT_WIDGET (object
);
518 case PROP_BUTTON_TEXT
:
519 gschem_show_hide_text_widget_set_button_text (widget
, g_value_get_string (value
));
522 case PROP_LABEL_TEXT
:
523 gschem_show_hide_text_widget_set_label_text (widget
, g_value_get_string (value
));
526 case PROP_TEXT_STRING
:
527 gschem_show_hide_text_widget_set_text_string (widget
, g_value_get_string (value
));
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, pspec
);