Bumped versions to 1.1.2/20070818/30:2:0 for the next development snapshot
[geda-gaf/whiteaudio.git] / gschem / src / x_multiattrib.c
blobff5a8ff70a702ee0ece965b82da2968a51e670fe
1 /* gEDA - GPL Electronic Design Automation
2 * gschem - gEDA Schematic Capture
3 * Copyright (C) 1998-2007 Ales Hvezda
4 * Copyright (C) 1998-2007 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., 59 Temple Place, Suite 330, Boston, MA 02111 USA
20 #include <config.h>
22 #include <stdio.h>
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
30 #include <libgeda/libgeda.h>
32 #include "../include/i_vars.h"
33 #include "../include/globals.h"
34 #include "../include/prototype.h"
36 #ifdef HAVE_LIBDMALLOC
37 #include <dmalloc.h>
38 #endif
40 #include <gdk/gdkkeysyms.h>
41 #include "../include/gschem_dialog.h"
42 #include "../include/x_multiattrib.h"
44 /*! \brief Process the response returned by the multi-attribte dialog.
45 * \par Function Description
46 * This function handles the response <B>arg1</B> of the multi-attribute
47 * editor dialog <B>dialog</B>.
49 * \param [in] dialog The multi-attribute editor dialog.
50 * \param [in] arg1 The response ID.
51 * \param [in] user_data A pointer on the toplevel environment.
53 static void
54 multiattrib_callback_response (GtkDialog *dialog,
55 gint arg1,
56 gpointer user_data)
58 TOPLEVEL *toplevel = (TOPLEVEL*)user_data;
60 switch (arg1) {
61 case GTK_RESPONSE_CLOSE:
62 case GTK_RESPONSE_DELETE_EVENT:
63 gtk_widget_destroy (GTK_WIDGET (dialog));
64 toplevel->mawindow = NULL;
65 break;
69 /*! \brief Open multiple attribute editor dialog.
70 * \par Function Description
71 * Opens the multiple attribute editor dialog for objects in this <B>toplevel</B>.
73 * \param [in] toplevel The TOPLEVEL object.
75 void x_multiattrib_open (TOPLEVEL *toplevel)
77 if ( toplevel->mawindow == NULL ) {
78 toplevel->mawindow = GTK_WIDGET (g_object_new (TYPE_MULTIATTRIB,
79 "selection", toplevel->page_current->selection_list,
80 /* GschemDialog */
81 "settings-name", "multiattrib",
82 "toplevel", toplevel,
83 NULL));
85 g_signal_connect (toplevel->mawindow,
86 "response",
87 G_CALLBACK (multiattrib_callback_response),
88 toplevel);
90 gtk_window_set_transient_for (GTK_WINDOW(toplevel->mawindow),
91 GTK_WINDOW(toplevel->main_window));
93 gtk_widget_show (toplevel->mawindow);
94 } else {
95 gtk_window_present (GTK_WINDOW(toplevel->mawindow));
100 /*! \brief Close the multiattrib dialog.
102 * \par Function Description
104 * Closes the multiattrib dialog associated with <B>toplevel</B>.
106 * \param [in] toplevel The TOPLEVEL object.
108 void x_multiattrib_close (TOPLEVEL *toplevel)
110 if (toplevel->mawindow != NULL) {
111 gtk_widget_destroy (toplevel->mawindow);
112 toplevel->mawindow = NULL;
117 /*! \brief Update the multiattrib editor dialog for a TOPLEVEL.
119 * \par Function Description
121 * If the TOPLEVEL has an open multiattrib dialog, switch to
122 * watching the current page's SELECTION object for changes.
124 * \param [in] toplevel The TOPLEVEL object.
126 void x_multiattrib_update( TOPLEVEL *toplevel )
128 if (toplevel->mawindow != NULL) {
129 g_object_set (G_OBJECT (toplevel->mawindow), "selection",
130 toplevel->page_current->selection_list, NULL);
135 /*! \section celltextview-widget Cell TextView Widget Code.
136 * This widget makes a 'GtkTextView' widget implements the 'GtkCellEditable'
137 * interface. It can then be used to renderer multi-line texts inside
138 * tree views ('GtkTreeView').
140 static void celltextview_class_init (CellTextViewClass *klass);
141 static void celltextview_init (CellTextView *self);
142 static void celltextview_cell_editable_init (GtkCellEditableIface *iface);
144 /*! \todo Finish function documentation
145 * \brief
146 * \par Function Description
149 static gboolean celltextview_key_press_event (GtkWidget *widget,
150 GdkEventKey *key_event,
151 gpointer data)
153 CellTextView *celltextview = (CellTextView*)widget;
155 /* If the Escape key is pressed, we flag the edit as canceled */
156 if (key_event->keyval == GDK_Escape)
157 celltextview->editing_canceled = TRUE;
159 /* ends editing of cell if one of these keys are pressed or editing is canceled */
160 if (celltextview->editing_canceled == TRUE ||
161 /* the Enter key without the Control modifier */
162 (!(key_event->state & GDK_CONTROL_MASK) &&
163 (key_event->keyval == GDK_Return ||
164 key_event->keyval == GDK_KP_Enter))) {
165 gtk_cell_editable_editing_done (GTK_CELL_EDITABLE (celltextview));
166 gtk_cell_editable_remove_widget (GTK_CELL_EDITABLE (celltextview));
167 return TRUE;
170 return FALSE;
173 /*! \todo Finish function documentation
174 * \brief
175 * \par Function Description
178 static void celltextview_start_editing (GtkCellEditable *cell_editable,
179 GdkEvent *event)
181 g_signal_connect (cell_editable,
182 "key_press_event",
183 G_CALLBACK (celltextview_key_press_event),
184 NULL);
188 /*! \todo Finish function documentation
189 * \brief
190 * \par Function Description
193 GType celltextview_get_type()
195 static GType celltextview_type = 0;
197 if (!celltextview_type) {
198 static const GTypeInfo celltextview_info = {
199 sizeof(CellTextViewClass),
200 NULL, /* base_init */
201 NULL, /* base_finalize */
202 (GClassInitFunc) celltextview_class_init,
203 NULL, /* class_finalize */
204 NULL, /* class_data */
205 sizeof(CellTextView),
206 0, /* n_preallocs */
207 (GInstanceInitFunc) celltextview_init,
210 static const GInterfaceInfo cell_editable_info = {
211 (GInterfaceInitFunc) celltextview_cell_editable_init,
212 NULL, /* interface_finalize */
213 NULL /* interface_data */
216 celltextview_type = g_type_register_static(GTK_TYPE_TEXT_VIEW,
217 "CellTextView",
218 &celltextview_info, 0);
219 g_type_add_interface_static(celltextview_type,
220 GTK_TYPE_CELL_EDITABLE,
221 &cell_editable_info);
224 return celltextview_type;
227 /*! \todo Finish function documentation
228 * \brief
229 * \par Function Description
232 static void celltextview_class_init(CellTextViewClass *klass)
234 /* GObjectClass *gobject_class = G_OBJECT_CLASS (klass); */
237 /*! \todo Finish function documentation
238 * \brief
239 * \par Function Description
242 static void celltextview_init(CellTextView *celltextview)
244 celltextview->editing_canceled = FALSE;
247 /*! \todo Finish function documentation
248 * \brief
249 * \par Function Description
252 static void celltextview_cell_editable_init(GtkCellEditableIface *iface)
254 iface->start_editing = celltextview_start_editing;
257 /*! \section multi-line-text-cell-renderer Multi-line Text Cell Renderer
258 * GTK has no multi-line text cell renderer. This code adds one to be used
259 * in gschem code. It is inspired by the 'GtkCellRendererCombo' renderer
260 * of GTK 2.4 (LGPL).
262 static void cellrenderermultilinetext_class_init(CellRendererMultiLineTextClass *klass);
263 static void cellrenderermultilinetext_init (CellRendererMultiLineText *self);
265 static void cellrenderermultilinetext_editing_done (GtkCellEditable *cell_editable,
266 gpointer user_data);
267 static gboolean cellrenderermultilinetext_focus_out_event (GtkWidget *widget,
268 GdkEvent *event,
269 gpointer user_data);
272 #define CELL_RENDERER_MULTI_LINE_TEXT_PATH "cell-renderer-multi-line-text-path"
275 /*! \todo Finish function documentation
276 * \brief
277 * \par Function Description
280 static GtkCellEditable* cellrenderermultilinetext_start_editing(GtkCellRenderer *cell,
281 GdkEvent *event,
282 GtkWidget *widget,
283 const gchar *path,
284 GdkRectangle *background_area,
285 GdkRectangle *cell_area,
286 GtkCellRendererState flags)
288 GtkCellRendererText *cell_text;
289 CellRendererMultiLineText *cell_mlt;
290 GtkWidget *textview;
291 GtkTextBuffer *textbuffer;
293 cell_text = GTK_CELL_RENDERER_TEXT (cell);
294 if (cell_text->editable == FALSE) {
295 return NULL;
298 cell_mlt = CELL_RENDERER_MULTI_LINE_TEXT (cell);
300 textbuffer = GTK_TEXT_BUFFER (g_object_new (GTK_TYPE_TEXT_BUFFER,
301 NULL));
302 gtk_text_buffer_set_text (textbuffer,
303 cell_text->text,
304 strlen (cell_text->text));
306 textview = GTK_WIDGET (g_object_new (TYPE_CELL_TEXT_VIEW,
307 /* GtkTextView */
308 "buffer", textbuffer,
309 "editable", TRUE,
310 /* GtkWidget */
311 "height-request", cell_area->height,
312 NULL));
313 g_object_set_data_full (G_OBJECT (textview),
314 CELL_RENDERER_MULTI_LINE_TEXT_PATH,
315 g_strdup (path), g_free);
317 gtk_widget_show (textview);
319 g_signal_connect (GTK_CELL_EDITABLE (textview),
320 "editing_done",
321 G_CALLBACK (cellrenderermultilinetext_editing_done),
322 cell_mlt);
323 cell_mlt->focus_out_id =
324 g_signal_connect (textview,
325 "focus_out_event",
326 G_CALLBACK (cellrenderermultilinetext_focus_out_event),
327 cell_mlt);
329 return GTK_CELL_EDITABLE (textview);
332 /*! \todo Finish function documentation
333 * \brief
334 * \par Function Description
337 static void cellrenderermultilinetext_editing_done(GtkCellEditable *cell_editable,
338 gpointer user_data)
340 CellRendererMultiLineText *cell = CELL_RENDERER_MULTI_LINE_TEXT (user_data);
341 GtkTextBuffer *buffer;
342 GtkTextIter start, end;
343 gchar *new_text;
344 const gchar *path;
346 if (cell->focus_out_id > 0) {
347 g_signal_handler_disconnect (cell_editable,
348 cell->focus_out_id);
349 cell->focus_out_id = 0;
352 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (cell),
353 CELL_TEXT_VIEW (cell_editable)->editing_canceled);
354 if (CELL_TEXT_VIEW (cell_editable)->editing_canceled)
355 return;
357 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (cell_editable));
358 gtk_text_buffer_get_start_iter (buffer, &start);
359 gtk_text_buffer_get_end_iter (buffer, &end);
360 new_text = gtk_text_buffer_get_text (buffer, &start, &end, TRUE);
362 path = g_object_get_data (G_OBJECT (cell_editable),
363 CELL_RENDERER_MULTI_LINE_TEXT_PATH);
364 g_signal_emit_by_name (cell, "edited", path, new_text);
366 g_free (new_text);
370 /*! \todo Finish function documentation
371 * \brief
372 * \par Function Description
375 static gboolean cellrenderermultilinetext_focus_out_event(GtkWidget *widget,
376 GdkEvent *event,
377 gpointer user_data)
379 cellrenderermultilinetext_editing_done (GTK_CELL_EDITABLE (widget),
380 user_data);
382 return FALSE;
385 /*! \todo Finish function documentation
386 * \brief
387 * \par Function Description
390 GType cellrenderermultilinetext_get_type()
392 static GType cellrenderermultilinetext_type = 0;
394 if (!cellrenderermultilinetext_type) {
395 static const GTypeInfo cellrenderermultilinetext_info = {
396 sizeof(CellRendererMultiLineTextClass),
397 NULL, /* base_init */
398 NULL, /* base_finalize */
399 (GClassInitFunc) cellrenderermultilinetext_class_init,
400 NULL, /* class_finalize */
401 NULL, /* class_data */
402 sizeof(CellRendererMultiLineText),
403 0, /* n_preallocs */
404 (GInstanceInitFunc) cellrenderermultilinetext_init,
407 cellrenderermultilinetext_type = g_type_register_static (
408 GTK_TYPE_CELL_RENDERER_TEXT,
409 "CellRendererMultiLineText",
410 &cellrenderermultilinetext_info, 0);
413 return cellrenderermultilinetext_type;
416 /*! \todo Finish function documentation
417 * \brief
418 * \par Function Description
421 static void cellrenderermultilinetext_class_init(CellRendererMultiLineTextClass *klass)
423 /* GObjectClass *gobject_class = G_OBJECT_CLASS (klass); */
424 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
426 cell_class->start_editing = cellrenderermultilinetext_start_editing;
430 /*! \todo Finish function documentation
431 * \brief
432 * \par Function Description
435 static void cellrenderermultilinetext_init(CellRendererMultiLineText *self)
440 enum {
441 PROP_SELECTION = 1
444 enum {
445 COLUMN_ATTRIBUTE,
446 NUM_COLUMNS
449 static GObjectClass *multiattrib_parent_class = NULL;
451 static void multiattrib_class_init (MultiattribClass *class);
452 static void multiattrib_init (Multiattrib *multiattrib);
453 static void multiattrib_set_property (GObject *object,
454 guint property_id,
455 const GValue *value,
456 GParamSpec *pspec);
457 static void multiattrib_get_property (GObject *object,
458 guint property_id,
459 GValue *value,
460 GParamSpec *pspec);
462 static void multiattrib_popup_menu (Multiattrib *multiattrib,
463 GdkEventButton *event);
466 /*! \todo Finish function documentation
467 * \brief
468 * \par Function Description
471 static void multiattrib_action_add_attribute(TOPLEVEL *toplevel,
472 OBJECT *object,
473 const gchar *name,
474 const gchar *value,
475 gint visible,
476 gint show_name_value)
478 OBJECT *o_attrib;
479 gchar *newtext;
481 newtext = g_strdup_printf ("%s=%s", name, value);
483 /* create a new attribute and link it */
484 o_attrib = o_attrib_add_attrib (toplevel, newtext,
485 visible, show_name_value, object);
487 toplevel->page_current->CHANGED = 1;
488 o_undo_savestate (toplevel, UNDO_ALL);
490 g_free (newtext);
494 /*! \todo Finish function documentation
495 * \brief
496 * \par Function Description
499 static void multiattrib_action_duplicate_attribute(TOPLEVEL *toplevel,
500 OBJECT *object,
501 OBJECT *o_attrib)
503 OBJECT *o_new;
505 o_new = o_attrib_add_attrib (toplevel,
506 o_attrib->text->string,
507 o_attrib->visibility,
508 o_attrib->show_name_value,
509 object);
510 toplevel->page_current->CHANGED = 1;
511 o_undo_savestate (toplevel, UNDO_ALL);
515 /*! \todo Finish function documentation
516 * \brief
517 * \par Function Description
520 static void multiattrib_action_delete_attribute(TOPLEVEL *toplevel,
521 OBJECT *o_attrib)
523 /* actually deletes the attribute */
524 o_selection_remove ( toplevel->page_current->selection_list, o_attrib);
525 o_delete_text (toplevel, o_attrib);
526 toplevel->page_current->CHANGED=1;
527 o_undo_savestate (toplevel, UNDO_ALL);
531 /*! \todo Finish function documentation
532 * \brief
533 * \par Function Description
536 static void multiattrib_column_set_data_name(GtkTreeViewColumn *tree_column,
537 GtkCellRenderer *cell,
538 GtkTreeModel *tree_model,
539 GtkTreeIter *iter,
540 gpointer data)
542 OBJECT *o_attrib;
543 gchar *name, *value;
545 gtk_tree_model_get (tree_model, iter,
546 COLUMN_ATTRIBUTE, &o_attrib,
547 -1);
548 g_assert (o_attrib->type == OBJ_TEXT);
550 o_attrib_get_name_value (o_attrib->text->string, &name, &value);
551 g_object_set (cell,
552 "text", name,
553 NULL);
554 g_free (name);
555 g_free (value);
559 /*! \todo Finish function documentation
560 * \brief
561 * \par Function Description
564 static void multiattrib_column_set_data_value(GtkTreeViewColumn *tree_column,
565 GtkCellRenderer *cell,
566 GtkTreeModel *tree_model,
567 GtkTreeIter *iter,
568 gpointer data)
570 OBJECT *o_attrib;
571 gchar *name, *value;
573 gtk_tree_model_get (tree_model, iter,
574 COLUMN_ATTRIBUTE, &o_attrib,
575 -1);
576 g_assert (o_attrib->type == OBJ_TEXT);
578 o_attrib_get_name_value (o_attrib->text->string, &name, &value);
579 g_object_set (cell,
580 "text", value,
581 NULL);
582 g_free (name);
583 g_free (value);
587 /*! \todo Finish function documentation
588 * \brief
589 * \par Function Description
592 static void multiattrib_column_set_data_visible(GtkTreeViewColumn *tree_column,
593 GtkCellRenderer *cell,
594 GtkTreeModel *tree_model,
595 GtkTreeIter *iter,
596 gpointer data)
598 OBJECT *o_attrib;
600 gtk_tree_model_get (tree_model, iter,
601 COLUMN_ATTRIBUTE, &o_attrib,
602 -1);
603 g_assert (o_attrib->type == OBJ_TEXT);
605 g_object_set (cell,
606 "active", (o_attrib->visibility == VISIBLE),
607 NULL);
611 /*! \todo Finish function documentation
612 * \brief
613 * \par Function Description
616 static void multiattrib_column_set_data_show_name(GtkTreeViewColumn *tree_column,
617 GtkCellRenderer *cell,
618 GtkTreeModel *tree_model,
619 GtkTreeIter *iter,
620 gpointer data)
622 OBJECT *o_attrib;
624 gtk_tree_model_get (tree_model, iter,
625 COLUMN_ATTRIBUTE, &o_attrib,
626 -1);
627 g_assert (o_attrib->type == OBJ_TEXT);
629 g_object_set (cell,
630 "active", (o_attrib->show_name_value == SHOW_NAME_VALUE ||
631 o_attrib->show_name_value == SHOW_NAME),
632 NULL);
636 /*! \todo Finish function documentation
637 * \brief
638 * \par Function Description
641 static void multiattrib_column_set_data_show_value(GtkTreeViewColumn *tree_column,
642 GtkCellRenderer *cell,
643 GtkTreeModel *tree_model,
644 GtkTreeIter *iter,
645 gpointer data)
647 OBJECT *o_attrib;
649 gtk_tree_model_get (tree_model, iter,
650 COLUMN_ATTRIBUTE, &o_attrib,
651 -1);
652 g_assert (o_attrib->type == OBJ_TEXT);
654 g_object_set (cell,
655 "active", (o_attrib->show_name_value == SHOW_NAME_VALUE ||
656 o_attrib->show_name_value == SHOW_VALUE),
657 NULL);
661 /*! \brief Requests an update of the display of a row.
662 * \par Function Description
663 * This is an helper function to update the display of a row when
664 * data for this row have been modified in the model.
666 * It emits the 'row_changed' signal on the pointed row.
668 * \param [in] model A GtkTreeModel.
669 * \param [in] iter A valid GtkTreeIter pointing to the changed row.
671 static void
672 update_row_display (GtkTreeModel *model, GtkTreeIter *iter)
674 GtkTreePath *path;
676 path = gtk_tree_model_get_path (model, iter);
677 gtk_tree_model_row_changed (model, path, iter);
678 gtk_tree_path_free (path);
682 /*! \todo Finish function documentation
683 * \brief
684 * \par Function Description
687 static void multiattrib_callback_edited_name(GtkCellRendererText *cellrenderertext,
688 gchar *arg1,
689 gchar *arg2,
690 gpointer user_data)
692 Multiattrib *multiattrib = (Multiattrib*)user_data;
693 GtkTreeModel *model;
694 GtkTreeIter iter;
695 OBJECT *o_attrib;
696 TOPLEVEL *toplevel;
697 gchar *name, *value, *newtext;
699 model = gtk_tree_view_get_model (multiattrib->treeview);
700 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
702 if (!gtk_tree_model_get_iter_from_string (model, &iter, arg1)) {
703 return;
706 if (g_ascii_strcasecmp (arg2, "") == 0) {
707 GtkWidget *dialog = gtk_message_dialog_new (
708 GTK_WINDOW (multiattrib),
709 GTK_DIALOG_MODAL,
710 GTK_MESSAGE_ERROR,
711 GTK_BUTTONS_OK,
712 _("Attributes with empty name are not allowed. Please set a name."));
714 gtk_dialog_run (GTK_DIALOG (dialog));
715 gtk_widget_destroy (dialog);
716 return;
719 gtk_tree_model_get (model, &iter,
720 COLUMN_ATTRIBUTE, &o_attrib,
721 -1);
722 g_assert (o_attrib->type == OBJ_TEXT);
724 o_attrib_get_name_value (o_attrib->text->string, &name, &value);
725 newtext = g_strdup_printf ("%s=%s", arg2, value);
727 /* actually modifies the attribute */
728 o_text_change (toplevel, o_attrib,
729 newtext, o_attrib->visibility, o_attrib->show_name_value);
731 g_free (name);
732 g_free (value);
733 g_free (newtext);
737 /*! \todo Finish function documentation
738 * \brief
739 * \par Function Description
742 static void multiattrib_callback_edited_value(GtkCellRendererText *cell_renderer,
743 gchar *arg1,
744 gchar *arg2,
745 gpointer user_data)
747 Multiattrib *multiattrib = (Multiattrib*)user_data;
748 GtkTreeModel *model;
749 GtkTreeIter iter;
750 OBJECT *o_attrib;
751 TOPLEVEL *toplevel;
752 gchar *name, *value, *newtext;
754 model = gtk_tree_view_get_model (multiattrib->treeview);
755 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
757 if (!gtk_tree_model_get_iter_from_string (model, &iter, arg1)) {
758 return;
761 gtk_tree_model_get (model, &iter,
762 COLUMN_ATTRIBUTE, &o_attrib,
763 -1);
764 g_assert (o_attrib->type == OBJ_TEXT);
766 o_attrib_get_name_value (o_attrib->text->string, &name, &value);
767 newtext = g_strdup_printf ("%s=%s", name, arg2);
769 /* actually modifies the attribute */
770 o_text_change (toplevel, o_attrib,
771 newtext, o_attrib->visibility, o_attrib->show_name_value);
773 /* request an update of display for this row */
774 update_row_display (model, &iter);
776 g_free (name);
777 g_free (value);
778 g_free (newtext);
782 /*! \todo Finish function documentation
783 * \brief
784 * \par Function Description
787 static void multiattrib_callback_toggled_visible(GtkCellRendererToggle *cell_renderer,
788 gchar *path,
789 gpointer user_data)
791 Multiattrib *multiattrib = (Multiattrib*)user_data;
792 GtkTreeModel *model;
793 GtkTreeIter iter;
794 OBJECT *o_attrib;
795 TOPLEVEL *toplevel;
796 gint visibility;
798 model = gtk_tree_view_get_model (multiattrib->treeview);
799 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
801 if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
802 return;
805 gtk_tree_model_get (model, &iter,
806 COLUMN_ATTRIBUTE, &o_attrib,
807 -1);
808 g_assert (o_attrib->type == OBJ_TEXT);
809 o_text_erase (toplevel, o_attrib);
811 visibility = o_attrib->visibility == VISIBLE ? INVISIBLE : VISIBLE;
813 /* actually modifies the attribute */
814 o_attrib->visibility = visibility;
815 o_text_recreate (toplevel, o_attrib);
816 o_text_draw (toplevel, o_attrib);
817 o_undo_savestate (toplevel, UNDO_ALL);
819 /* request an update of display for this row */
820 update_row_display (model, &iter);
824 /*! \todo Finish function documentation
825 * \brief
826 * \par Function Description
829 static void multiattrib_callback_toggled_show_name(GtkCellRendererToggle *cell_renderer,
830 gchar *path,
831 gpointer user_data)
833 Multiattrib *multiattrib = (Multiattrib*)user_data;
834 GtkTreeModel *model;
835 GtkTreeIter iter;
836 OBJECT *o_attrib;
837 TOPLEVEL *toplevel;
838 gint new_snv;
840 model = gtk_tree_view_get_model (multiattrib->treeview);
841 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
843 if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
844 return;
847 gtk_tree_model_get (model, &iter,
848 COLUMN_ATTRIBUTE, &o_attrib,
849 -1);
850 g_assert (o_attrib->type == OBJ_TEXT);
851 o_text_erase (toplevel, o_attrib);
853 switch (o_attrib->show_name_value) {
854 case SHOW_NAME_VALUE: new_snv = SHOW_VALUE; break;
855 case SHOW_NAME: new_snv = SHOW_VALUE; break;
856 case SHOW_VALUE: new_snv = SHOW_NAME_VALUE; break;
857 default:
858 g_assert_not_reached ();
859 new_snv = SHOW_NAME_VALUE;
862 /* actually modifies the attribute */
863 o_attrib->show_name_value = new_snv;
864 o_text_recreate (toplevel, o_attrib);
865 o_text_draw (toplevel, o_attrib);
866 o_undo_savestate (toplevel, UNDO_ALL);
868 /* request an update of display for this row */
869 update_row_display (model, &iter);
873 /*! \todo Finish function documentation
874 * \brief
875 * \par Function Description
878 static void multiattrib_callback_toggled_show_value(GtkCellRendererToggle *cell_renderer,
879 gchar *path,
880 gpointer user_data)
882 Multiattrib *multiattrib = (Multiattrib*)user_data;
883 GtkTreeModel *model;
884 GtkTreeIter iter;
885 OBJECT *o_attrib;
886 TOPLEVEL *toplevel;
887 gint new_snv;
889 model = gtk_tree_view_get_model (multiattrib->treeview);
890 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
892 if (!gtk_tree_model_get_iter_from_string (model, &iter, path)) {
893 return;
896 gtk_tree_model_get (model, &iter,
897 COLUMN_ATTRIBUTE, &o_attrib,
898 -1);
899 g_assert (o_attrib->type == OBJ_TEXT);
900 o_text_erase (toplevel, o_attrib);
902 switch (o_attrib->show_name_value) {
903 case SHOW_NAME_VALUE: new_snv = SHOW_NAME; break;
904 case SHOW_NAME: new_snv = SHOW_NAME_VALUE; break;
905 case SHOW_VALUE: new_snv = SHOW_NAME; break;
906 default:
907 g_assert_not_reached ();
908 new_snv = SHOW_NAME_VALUE;
911 /* actually modifies the attribute */
912 o_attrib->show_name_value = new_snv;
913 o_text_recreate (toplevel, o_attrib);
914 o_text_draw (toplevel, o_attrib);
915 o_undo_savestate (toplevel, UNDO_ALL);
917 /* request an update of display for this row */
918 update_row_display (model, &iter);
922 /*! \todo Finish function documentation
923 * \brief
924 * \par Function Description
927 static gboolean multiattrib_callback_key_pressed(GtkWidget *widget,
928 GdkEventKey *event,
929 gpointer user_data)
931 Multiattrib *multiattrib = (Multiattrib*)user_data;
933 if (event->state == 0 &&
934 (event->keyval == GDK_Delete || event->keyval == GDK_KP_Delete)) {
935 GtkTreeModel *model;
936 GtkTreeIter iter;
937 OBJECT *o_attrib;
938 /* delete the currently selected attribute */
940 if (!gtk_tree_selection_get_selected (
941 gtk_tree_view_get_selection (multiattrib->treeview),
942 &model, &iter)) {
943 /* nothing selected, nothing to do */
944 return FALSE;
947 gtk_tree_model_get (model, &iter,
948 COLUMN_ATTRIBUTE, &o_attrib,
949 -1);
950 g_assert (o_attrib->type == OBJ_TEXT);
952 multiattrib_action_delete_attribute (GSCHEM_DIALOG (multiattrib)->toplevel,
953 o_attrib);
955 /* update the treeview contents */
956 multiattrib_update (multiattrib);
959 return FALSE;
962 /*! \todo Finish function documentation
963 * \brief
964 * \par Function Description
967 static gboolean multiattrib_callback_button_pressed(GtkWidget *widget,
968 GdkEventButton *event,
969 gpointer user_data)
971 Multiattrib *multiattrib = (Multiattrib*)user_data;
972 gboolean ret = FALSE;
974 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
975 multiattrib_popup_menu (multiattrib, event);
976 ret = TRUE;
979 return ret;
982 /*! \todo Finish function documentation
983 * \brief
984 * \par Function Description
987 static gboolean multiattrib_callback_popup_menu(GtkWidget *widget,
988 gpointer user_data)
990 Multiattrib *multiattrib = (Multiattrib*)user_data;
992 multiattrib_popup_menu (multiattrib, NULL);
994 return TRUE;
997 /*! \todo Finish function documentation
998 * \brief
999 * \par Function Description
1002 static void multiattrib_callback_popup_duplicate(GtkMenuItem *menuitem,
1003 gpointer user_data)
1005 Multiattrib *multiattrib = (Multiattrib*)user_data;
1006 GtkTreeModel *model;
1007 GtkTreeIter iter;
1008 TOPLEVEL *toplevel;
1009 OBJECT *object, *o_attrib;
1011 if (!gtk_tree_selection_get_selected (
1012 gtk_tree_view_get_selection (multiattrib->treeview),
1013 &model, &iter)) {
1014 /* nothing selected, nothing to do */
1015 return;
1018 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
1019 object = multiattrib->object;
1021 gtk_tree_model_get (model, &iter,
1022 COLUMN_ATTRIBUTE, &o_attrib,
1023 -1);
1024 g_assert (o_attrib->type == OBJ_TEXT);
1026 multiattrib_action_duplicate_attribute (toplevel, object, o_attrib);
1028 /* update the treeview contents */
1029 multiattrib_update (multiattrib);
1033 /*! \todo Finish function documentation
1034 * \brief
1035 * \par Function Description
1038 static void multiattrib_callback_popup_delete(GtkMenuItem *menuitem,
1039 gpointer user_data)
1041 Multiattrib *multiattrib = (Multiattrib*)user_data;
1042 GtkTreeModel *model;
1043 GtkTreeIter iter;
1044 TOPLEVEL *toplevel;
1045 OBJECT *o_attrib;
1047 if (!gtk_tree_selection_get_selected (
1048 gtk_tree_view_get_selection (multiattrib->treeview),
1049 &model, &iter)) {
1050 /* nothing selected, nothing to do */
1051 return;
1054 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
1056 gtk_tree_model_get (model, &iter,
1057 COLUMN_ATTRIBUTE, &o_attrib,
1058 -1);
1059 g_assert (o_attrib->type == OBJ_TEXT);
1061 multiattrib_action_delete_attribute (toplevel, o_attrib);
1063 /* update the treeview contents */
1064 multiattrib_update (multiattrib);
1068 /*! \todo Finish function documentation
1069 * \brief
1070 * \par Function Description
1073 static gboolean multiattrib_callback_value_key_pressed(GtkWidget *widget,
1074 GdkEventKey *event,
1075 gpointer user_data)
1077 Multiattrib *multiattrib = (Multiattrib*)widget;
1078 gboolean retval = FALSE;
1080 /* ends editing of cell if one of these keys are pressed: */
1081 /* - the Return key without the Control modifier */
1082 /* - the Tab key without the Control modifier */
1083 if ((event->keyval == GDK_Return || event->keyval == GDK_KP_Enter) ||
1084 (event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab)) {
1085 /* Control modifier activated? */
1086 if (event->state & GDK_CONTROL_MASK) {
1087 /* yes the modifier in event structure and let event propagate */
1088 event->state ^= GDK_CONTROL_MASK;
1089 retval = FALSE;
1090 } else {
1091 /* change focus and stop propagation */
1092 g_signal_emit_by_name (multiattrib,
1093 "move_focus",
1094 (event->state & GDK_SHIFT_MASK) ?
1095 GTK_DIR_TAB_BACKWARD : GTK_DIR_TAB_FORWARD);
1096 retval = TRUE;
1100 return retval;
1104 /*! \brief GtkWidget "grab-focus" signal handler
1106 * \par Function Description
1107 * Select the text in the GtkTextView so it may be over-typed quickly
1109 static void multiattrib_callback_value_grab_focus (GtkWidget *widget,
1110 gpointer user_data)
1112 GtkTextView *textview = GTK_TEXT_VIEW (widget);
1113 GtkTextBuffer *textbuffer;
1114 GtkTextIter startiter, enditer;
1116 textbuffer = gtk_text_view_get_buffer (textview);
1117 gtk_text_buffer_get_iter_at_offset (textbuffer, &startiter, 0);
1118 gtk_text_buffer_get_iter_at_offset (textbuffer, &enditer, -1);
1119 gtk_text_buffer_select_range (textbuffer, &enditer, &startiter);
1123 /*! \todo Finish function documentation
1124 * \brief
1125 * \par Function Description
1128 static void multiattrib_callback_button_add(GtkButton *button,
1129 gpointer user_data)
1131 Multiattrib *multiattrib = (Multiattrib*)user_data;
1132 GtkTextBuffer *buffer;
1133 GtkTextIter start, end;
1134 const gchar *name;
1135 gchar *value;
1136 TOPLEVEL *toplevel;
1137 OBJECT *object;
1138 gboolean visible;
1139 gint shownv;
1141 toplevel = GSCHEM_DIALOG (multiattrib)->toplevel;
1142 object = multiattrib->object;
1143 buffer = gtk_text_view_get_buffer (multiattrib->textview_value);
1145 /* retrieve information from the Add/Edit frame */
1146 /* - attribute's name */
1147 name = gtk_entry_get_text (
1148 GTK_ENTRY (GTK_COMBO (multiattrib->combo_name)->entry));
1149 /* - attribute's value */
1150 gtk_text_buffer_get_bounds (buffer, &start, &end);
1151 value = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
1152 /* - attribute's visibility status */
1153 visible = gtk_toggle_button_get_active (
1154 (GtkToggleButton*)multiattrib->button_visible);
1155 /* - visibility type */
1156 shownv = (gint)gtk_option_menu_get_history (multiattrib->optionmenu_shownv);
1158 if (name[0] == '\0' || name[0] == ' ') {
1159 /* name not allowed for an attribute */
1160 g_free (value);
1161 return;
1164 multiattrib_action_add_attribute (toplevel, object,
1165 name, value,
1166 visible, shownv);
1167 g_free (value);
1169 multiattrib_update (multiattrib);
1172 /*! \todo Finish function documentation
1173 * \brief
1174 * \par Function Description
1177 static void multiattrib_init_attrib_names(GtkCombo *combo)
1179 GList *items = NULL;
1180 const gchar *string;
1181 gint i;
1183 for (i = 0, string = s_attrib_get (i);
1184 string != NULL;
1185 i++, string = s_attrib_get (i)) {
1186 items = g_list_append (items, (gpointer)string);
1189 gtk_combo_set_popdown_strings (GTK_COMBO (combo), items);
1191 g_list_free (items);
1195 /*! \todo Finish function documentation
1196 * \brief
1197 * \par Function Description
1200 static void multiattrib_init_visible_types(GtkOptionMenu *optionmenu)
1202 GtkWidget *menu, *item;
1204 menu = gtk_menu_new ();
1205 item = gtk_menu_item_new_with_label (_("Show Name & Value"));
1206 gtk_menu_append (menu, item);
1207 item = gtk_menu_item_new_with_label (_("Show Value only"));
1208 gtk_menu_append (menu, item);
1209 item = gtk_menu_item_new_with_label (_("Show Name only"));
1210 gtk_menu_append (menu, item);
1212 gtk_option_menu_set_menu (optionmenu, menu);
1217 /*! \brief Popup a context-sensitive menu.
1218 * \par Function Description
1219 * Pops up a context-sensitive menu.
1220 * <B>event</B> can be NULL if the popup is triggered by a key binding
1221 * instead of a mouse click.
1223 * \param [in] multiattrib The Multiattrib object.
1224 * \param [in] event Mouse event.
1226 static void multiattrib_popup_menu(Multiattrib *multiattrib,
1227 GdkEventButton *event)
1229 GtkTreePath *path;
1230 GtkWidget *menu;
1231 struct menuitem_t {
1232 gchar *label;
1233 GCallback callback;
1235 struct menuitem_t menuitems[] = {
1236 { N_("Duplicate"), G_CALLBACK (multiattrib_callback_popup_duplicate) },
1237 { N_("Delete"), G_CALLBACK (multiattrib_callback_popup_delete) },
1238 { NULL, NULL } };
1239 struct menuitem_t *tmp;
1241 if (event != NULL &&
1242 gtk_tree_view_get_path_at_pos (multiattrib->treeview,
1243 (gint)event->x,
1244 (gint)event->y,
1245 &path, NULL, NULL, NULL)) {
1246 GtkTreeSelection *selection;
1247 selection = gtk_tree_view_get_selection (multiattrib->treeview);
1248 gtk_tree_selection_unselect_all (selection);
1249 gtk_tree_selection_select_path (selection, path);
1250 gtk_tree_path_free (path);
1253 /* create the context menu */
1254 menu = gtk_menu_new();
1255 for (tmp = menuitems; tmp->label != NULL; tmp++) {
1256 GtkWidget *menuitem;
1257 if (g_strcasecmp (tmp->label, "-") == 0) {
1258 menuitem = gtk_separator_menu_item_new ();
1259 } else {
1260 menuitem = gtk_menu_item_new_with_label (_(tmp->label));
1261 g_signal_connect (menuitem,
1262 "activate",
1263 tmp->callback,
1264 multiattrib);
1266 gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
1268 gtk_widget_show_all (menu);
1269 /* make menu a popup menu */
1270 gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
1271 (event != NULL) ? event->button : 0,
1272 gdk_event_get_time ((GdkEvent*)event));
1277 /*! \brief Function to retrieve Multiattrib's GType identifier.
1279 * \par Function Description
1281 * Function to retrieve Multiattrib's GType identifier.
1282 * Upon first call, this registers Multiattrib in the GType system.
1283 * Subsequently it returns the saved value from its first execution.
1285 * \return the GType identifier associated with Multiattrib.
1287 GType multiattrib_get_type()
1289 static GType multiattrib_type = 0;
1291 if (!multiattrib_type) {
1292 static const GTypeInfo multiattrib_info = {
1293 sizeof(MultiattribClass),
1294 NULL, /* base_init */
1295 NULL, /* base_finalize */
1296 (GClassInitFunc) multiattrib_class_init,
1297 NULL, /* class_finalize */
1298 NULL, /* class_data */
1299 sizeof(Multiattrib),
1300 0, /* n_preallocs */
1301 (GInstanceInitFunc) multiattrib_init,
1304 multiattrib_type = g_type_register_static (GSCHEM_TYPE_DIALOG,
1305 "Multiattrib",
1306 &multiattrib_info, 0);
1309 return multiattrib_type;
1313 /*! \brief Update the multiattrib editor dialog when the page's selection changes.
1315 * \par Function Description
1317 * When the page's selection changes this function identifies how many objects
1318 * which can have attributes are currently selected. If this number is 1, the
1319 * dialog is set to edit its attributes.
1321 * \todo The dialog doesn't currently support editing multiple objects at once
1323 * \param [in] selection The SELECTION object of page being edited.
1324 * \param [in] multiattrib The multi-attribute editor dialog.
1326 static void selection_changed_cb (SELECTION *selection, Multiattrib *multiattrib)
1328 int object_count = 0;
1329 GList *selection_glist;
1330 GList *iter;
1331 OBJECT *object;
1333 selection_glist = geda_list_get_glist (selection);
1335 for ( iter = selection_glist;
1336 iter != NULL;
1337 iter = g_list_next (iter) ) {
1338 object = (OBJECT *)iter->data;
1339 g_assert( object != NULL );
1341 if (object->type == OBJ_COMPLEX ||
1342 object->type == OBJ_PLACEHOLDER ||
1343 object->type == OBJ_NET ||
1344 object->type == OBJ_BUS ||
1345 object->type == OBJ_PIN) {
1346 object_count++;
1350 if (object_count == 0) {
1351 /* TODO: If the user selects a single attribute which is
1352 * not floating, should we find its parent object and
1353 * display the multi-attribute editor for that?
1354 * Bonus marks for making it jump to the correct attrib.
1356 multiattrib->object = NULL;
1357 } else if (object_count == 1) {
1358 multiattrib->object = (OBJECT *)selection_glist->data;
1359 } else {
1360 /* TODO: Something clever with multiple objects selected */
1361 multiattrib->object = NULL;
1364 multiattrib_update (multiattrib);
1368 /*! \brief Update the dialog when the current page's SELECTION object is destroyed
1370 * \par Function Description
1372 * This handler is called when the g_object_weak_ref() on the SELECTION object
1373 * we're watching expires. We reset our multiattrib->selection pointer to NULL
1374 * to avoid attempting to access the destroyed object. NB: Our signal handlers
1375 * were automatically disconnected during the destruction process.
1377 * \param [in] data Pointer to the multi-attrib dialog
1378 * \param [in] where_the_object_was Pointer to where the object was just destroyed
1380 static void selection_weak_ref_cb (gpointer data, GObject *where_the_object_was)
1382 Multiattrib *multiattrib = (Multiattrib *)data;
1384 multiattrib->selection = NULL;
1385 multiattrib_update (multiattrib);
1389 /*! \brief Connect signal handler and weak_ref on the SELECTION object
1391 * \par Function Description
1393 * Connect the "changed" signal and add a weak reference
1394 * on the SELECTION object we are going to watch.
1396 * \param [in] multiattrib The Multiattrib dialog.
1397 * \param [in] selection The SELECTION object to watch.
1399 static void connect_selection( Multiattrib *multiattrib, SELECTION *selection )
1401 multiattrib->selection = selection;
1402 if (multiattrib->selection != NULL) {
1403 g_object_weak_ref (G_OBJECT (multiattrib->selection),
1404 selection_weak_ref_cb,
1405 multiattrib);
1406 multiattrib->selection_changed_id =
1407 g_signal_connect (G_OBJECT (multiattrib->selection),
1408 "changed",
1409 G_CALLBACK (selection_changed_cb),
1410 multiattrib);
1411 /* Synthesise a selection changed update to refresh the view */
1412 selection_changed_cb (multiattrib->selection, multiattrib);
1413 } else {
1414 /* Call an update to set the sensitivities */
1415 multiattrib_update (multiattrib);
1420 /*! \brief Disconnect signal handler and weak_ref on the SELECTION object
1422 * \par Function Description
1424 * If the dialog is watching a SELECTION object, disconnect the
1425 * "changed" signal and remove our weak reference on the object.
1427 * \param [in] multiattrib The Multiattrib dialog.
1429 static void disconnect_selection( Multiattrib *multiattrib )
1431 if (multiattrib->selection != NULL) {
1432 g_signal_handler_disconnect (multiattrib->selection,
1433 multiattrib->selection_changed_id);
1434 g_object_weak_unref(G_OBJECT( multiattrib->selection ),
1435 selection_weak_ref_cb,
1436 multiattrib );
1441 /*! \brief GObject finalise handler
1443 * \par Function Description
1445 * Just before the Multiattrib GObject is finalized, disconnect from
1446 * the SELECTION object being watched and then chain up to the parent
1447 * class's finalize handler.
1449 * \param [in] object The GObject being finalized.
1451 static void multiattrib_finalize (GObject *object)
1453 Multiattrib *multiattrib = MULTIATTRIB(object);
1455 disconnect_selection( multiattrib );
1456 G_OBJECT_CLASS (multiattrib_parent_class)->finalize (object);
1460 /*! \brief GType class initialiser for Multiattrib
1462 * \par Function Description
1464 * GType class initialiser for Multiattrib. We override our parent
1465 * virtual class methods as needed and register our GObject properties.
1467 * \param [in] klass The MultiattribClass we are initialising
1469 static void multiattrib_class_init(MultiattribClass *klass)
1471 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1473 multiattrib_parent_class = g_type_class_peek_parent (klass);
1475 gobject_class->set_property = multiattrib_set_property;
1476 gobject_class->get_property = multiattrib_get_property;
1477 gobject_class->finalize = multiattrib_finalize;
1479 g_object_class_install_property (
1480 gobject_class, PROP_SELECTION,
1481 g_param_spec_pointer ("selection",
1484 G_PARAM_READWRITE));
1488 /*! \brief GType instance initialiser for Multiattrib
1490 * \par Function Description
1492 * GType instance initialiser for Multiattrib. Create
1493 * and setup the widgets which make up the dialog.
1495 * \param [in] dialog The Multiattrib we are initialising
1497 static void multiattrib_init(Multiattrib *multiattrib)
1499 GtkWidget *frame, *label, *scrolled_win, *treeview;
1500 GtkWidget *table, *textview, *combo, *optionm, *button;
1501 GtkTreeModel *store;
1502 GtkCellRenderer *renderer;
1503 GtkTreeViewColumn *column;
1504 GtkTreeSelection *selection;
1505 GtkStyle *style;
1507 /* dialog initialization */
1508 g_object_set (G_OBJECT (multiattrib),
1509 /* GtkContainer */
1510 "border-width", 0,
1511 /* GtkWindow */
1512 "type", GTK_WINDOW_TOPLEVEL,
1513 "title", _("Edit Attributes"),
1514 "default-width", 320,
1515 "default-height", 350,
1516 "window-position", GTK_WIN_POS_MOUSE,
1517 "allow-grow", TRUE,
1518 "allow-shrink", FALSE,
1519 /* GtkDialog */
1520 "has-separator", TRUE,
1521 NULL);
1523 multiattrib->object = NULL;
1525 /* create the attribute list frame */
1526 frame = GTK_WIDGET (g_object_new (GTK_TYPE_FRAME,
1527 /* GtkFrame */
1528 "label", _("Attributes"),
1529 NULL));
1530 multiattrib->frame_add = frame;
1531 /* - create the model for the treeview */
1532 store = (GtkTreeModel*)gtk_list_store_new (NUM_COLUMNS,
1533 G_TYPE_POINTER); /* attribute */
1534 /* - create a scrolled window for the treeview */
1535 scrolled_win = GTK_WIDGET (
1536 g_object_new (GTK_TYPE_SCROLLED_WINDOW,
1537 /* GtkContainer */
1538 "border-width", 3,
1539 /* GtkScrolledWindow */
1540 "hscrollbar-policy",
1541 GTK_POLICY_AUTOMATIC,
1542 "vscrollbar-policy",
1543 GTK_POLICY_AUTOMATIC,
1544 "shadow-type",
1545 GTK_SHADOW_ETCHED_IN,
1546 NULL));
1547 /* - create the treeview */
1548 treeview = GTK_WIDGET (g_object_new (GTK_TYPE_TREE_VIEW,
1549 /* GtkTreeView */
1550 "model", store,
1551 "rules-hint", TRUE,
1552 NULL));
1553 g_signal_connect (treeview,
1554 "key-press-event",
1555 G_CALLBACK (multiattrib_callback_key_pressed),
1556 multiattrib);
1557 g_signal_connect (treeview,
1558 "button-press-event",
1559 G_CALLBACK (multiattrib_callback_button_pressed),
1560 multiattrib);
1561 g_signal_connect (treeview,
1562 "popup-menu",
1563 G_CALLBACK (multiattrib_callback_popup_menu),
1564 multiattrib);
1565 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
1566 gtk_tree_selection_set_mode (selection,
1567 GTK_SELECTION_SINGLE);
1569 /* - and now the columns of the treeview */
1570 /* - column 1: attribute name */
1571 renderer = GTK_CELL_RENDERER (
1572 g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
1573 /* GtkCellRendererText */
1574 "editable", TRUE,
1575 /* unknown in GTK 2.4 */
1576 /* "ellipsize",
1577 * PANGO_ELLIPSIZE_END, */
1578 NULL));
1579 g_signal_connect (renderer,
1580 "edited",
1581 G_CALLBACK (multiattrib_callback_edited_name),
1582 multiattrib);
1583 column = GTK_TREE_VIEW_COLUMN (
1584 g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
1585 /* GtkTreeViewColumn */
1586 "title", _("Name"),
1587 "min-width", 100,
1588 "resizable", TRUE,
1589 NULL));
1590 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1591 gtk_tree_view_column_set_cell_data_func (column, renderer,
1592 multiattrib_column_set_data_name,
1593 NULL, NULL);
1594 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1595 /* - column 2: attribute value */
1596 renderer = GTK_CELL_RENDERER (
1597 g_object_new (TYPE_CELL_RENDERER_MULTI_LINE_TEXT,
1598 /* GtkCellRendererText */
1599 "editable", TRUE,
1600 /* unknown in GTK 2.4 */
1601 /* "ellipsize",
1602 PANGO_ELLIPSIZE_END, */
1603 NULL));
1604 g_signal_connect (renderer,
1605 "edited",
1606 G_CALLBACK (multiattrib_callback_edited_value),
1607 multiattrib);
1608 column = GTK_TREE_VIEW_COLUMN (
1609 g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
1610 /* GtkTreeViewColumn */
1611 "title", _("Value"),
1612 "min-width", 140,
1613 "resizable", TRUE,
1614 NULL));
1615 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1616 gtk_tree_view_column_set_cell_data_func (column, renderer,
1617 multiattrib_column_set_data_value,
1618 NULL, NULL);
1619 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1620 /* - column 3: visibility */
1621 renderer = GTK_CELL_RENDERER (
1622 g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE,
1623 /* GtkCellRendererToggle */
1624 "activatable", TRUE,
1625 NULL));
1626 g_signal_connect (renderer,
1627 "toggled",
1628 G_CALLBACK (multiattrib_callback_toggled_visible),
1629 multiattrib);
1630 column = GTK_TREE_VIEW_COLUMN (
1631 g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
1632 /* GtkTreeViewColumn */
1633 "title", _("Vis?"),
1634 NULL));
1635 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1636 gtk_tree_view_column_set_cell_data_func (column, renderer,
1637 multiattrib_column_set_data_visible,
1638 NULL, NULL);
1639 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1640 /* - column 4: show name */
1641 renderer = GTK_CELL_RENDERER (
1642 g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE,
1643 /* GtkCellRendererToggle */
1644 "activatable", TRUE,
1645 NULL));
1646 g_signal_connect (renderer,
1647 "toggled",
1648 G_CALLBACK (multiattrib_callback_toggled_show_name),
1649 multiattrib);
1650 column = GTK_TREE_VIEW_COLUMN (
1651 g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
1652 /* GtkTreeViewColumn */
1653 "title", _("N"),
1654 NULL));
1655 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1656 gtk_tree_view_column_set_cell_data_func (column, renderer,
1657 multiattrib_column_set_data_show_name,
1658 NULL, NULL);
1659 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1660 /* - column 5: show value */
1661 renderer = GTK_CELL_RENDERER (
1662 g_object_new (GTK_TYPE_CELL_RENDERER_TOGGLE,
1663 /* GtkCellRendererToggle */
1664 "activatable", TRUE,
1665 NULL));
1666 g_signal_connect (renderer,
1667 "toggled",
1668 G_CALLBACK (multiattrib_callback_toggled_show_value),
1669 multiattrib);
1670 column = GTK_TREE_VIEW_COLUMN (
1671 g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
1672 /* GtkTreeViewColumn */
1673 "title", _("V"),
1674 NULL));
1675 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1676 gtk_tree_view_column_set_cell_data_func (column, renderer,
1677 multiattrib_column_set_data_show_value,
1678 NULL, NULL);
1679 gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1681 /* add the treeview to the scrolled window */
1682 gtk_container_add (GTK_CONTAINER (scrolled_win), treeview);
1683 /* set treeview of multiattrib */
1684 multiattrib->treeview = GTK_TREE_VIEW (treeview);
1685 /* add the scrolled window to frame */
1686 gtk_container_add (GTK_CONTAINER (frame), scrolled_win);
1687 /* pack the frame */
1688 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (multiattrib)->vbox), frame,
1689 TRUE, TRUE, 1);
1690 gtk_widget_show_all (frame);
1692 /* create the add/edit frame */
1693 frame = GTK_WIDGET (g_object_new (GTK_TYPE_FRAME,
1694 "label", _("Add Attribute"),
1695 NULL));
1696 multiattrib->frame_attributes = frame;
1697 table = GTK_WIDGET (g_object_new (GTK_TYPE_TABLE,
1698 /* GtkTable */
1699 "n-rows", 4,
1700 "n-columns", 2,
1701 "homogeneous", FALSE,
1702 NULL));
1704 /* - the name entry: a GtkComboBoxEntry */
1705 label = GTK_WIDGET (g_object_new (GTK_TYPE_LABEL,
1706 /* GtkMisc */
1707 "xalign", 0.0,
1708 "yalign", 0.5,
1709 /* GtkLabel */
1710 "label", _("Name:"),
1711 NULL));
1712 combo = GTK_WIDGET (g_object_new (GTK_TYPE_COMBO,
1713 /* GtkCombo */
1714 "value-in-list", FALSE,
1715 NULL));
1716 multiattrib_init_attrib_names (GTK_COMBO (combo));
1717 multiattrib->combo_name = GTK_COMBO (combo);
1718 gtk_table_attach (GTK_TABLE (table), label,
1719 0, 1, 0, 1, 0, 0, 0, 0);
1720 gtk_table_attach (GTK_TABLE (table), combo,
1721 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 6, 3);
1723 /* - the value entry: a GtkEntry */
1724 label = GTK_WIDGET (g_object_new (GTK_TYPE_LABEL,
1725 /* GtkMisc */
1726 "xalign", 0.0,
1727 "yalign", 0.5,
1728 /* GtkLabel */
1729 "label", _("Value:"),
1730 NULL));
1731 scrolled_win = GTK_WIDGET (
1732 g_object_new (GTK_TYPE_SCROLLED_WINDOW,
1733 /* GtkScrolledWindow */
1734 "hscrollbar-policy",
1735 GTK_POLICY_NEVER,
1736 "vscrollbar-policy",
1737 GTK_POLICY_AUTOMATIC,
1738 "shadow-type",
1739 GTK_SHADOW_IN,
1740 NULL));
1741 textview = GTK_WIDGET (g_object_new (GTK_TYPE_TEXT_VIEW,
1742 NULL));
1743 g_signal_connect (textview,
1744 "key_press_event",
1745 G_CALLBACK (multiattrib_callback_value_key_pressed),
1746 multiattrib);
1747 g_signal_connect (textview,
1748 "grab-focus",
1749 G_CALLBACK (multiattrib_callback_value_grab_focus),
1750 multiattrib);
1751 /* Save the GTK_STATE_NORMAL color so we can work around GtkTextView's
1752 * stubborn refusal to draw with GTK_STATE_INSENSITIVE later on */
1753 style = gtk_widget_get_style (textview);
1754 multiattrib->value_normal_text_color = style->text[ GTK_STATE_NORMAL ];
1756 gtk_container_add (GTK_CONTAINER (scrolled_win), textview);
1757 multiattrib->textview_value = GTK_TEXT_VIEW (textview);
1758 gtk_table_attach (GTK_TABLE (table), label,
1759 0, 1, 1, 2, 0, 0, 0, 0);
1760 gtk_table_attach (GTK_TABLE (table), scrolled_win,
1761 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 6, 3);
1763 /* - the visible status */
1764 button = GTK_WIDGET (g_object_new (GTK_TYPE_CHECK_BUTTON,
1765 /* GtkButton */
1766 "label", _("Visible"),
1767 "active", TRUE,
1768 NULL));
1769 multiattrib->button_visible = GTK_CHECK_BUTTON (button);
1770 gtk_table_attach (GTK_TABLE (table), button,
1771 0, 1, 2, 3, GTK_FILL, 0, 3, 0);
1773 /* - the visibility type */
1774 optionm = GTK_WIDGET (g_object_new (GTK_TYPE_OPTION_MENU,
1775 NULL));
1776 multiattrib_init_visible_types (GTK_OPTION_MENU (optionm));
1777 multiattrib->optionmenu_shownv = GTK_OPTION_MENU (optionm);
1778 gtk_table_attach (GTK_TABLE (table), optionm,
1779 1, 2, 2, 3, GTK_EXPAND | GTK_FILL, 0, 6, 3);
1780 gtk_widget_show_all (table);
1782 /* create the add button */
1783 button = gtk_button_new_from_stock (GTK_STOCK_ADD);
1784 g_signal_connect (button,
1785 "clicked",
1786 G_CALLBACK (multiattrib_callback_button_add),
1787 multiattrib);
1788 gtk_table_attach (GTK_TABLE (table), button,
1789 2, 3, 0, 3, 0, 0, 6, 3);
1791 /* add the table to the frame */
1792 gtk_container_add (GTK_CONTAINER (frame), table);
1793 /* pack the frame in the dialog */
1794 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (multiattrib)->vbox), frame,
1795 FALSE, TRUE, 4);
1796 gtk_widget_show_all (frame);
1799 /* now add the close button to the action area */
1800 gtk_dialog_add_button (GTK_DIALOG (multiattrib),
1801 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
1806 /*! \brief GObject property setter function
1808 * \par Function Description
1809 * Setter function for Multiattrib's GObject property, "selection".
1811 * \param [in] object The GObject whose properties we are setting
1812 * \param [in] property_id The numeric id. under which the property was
1813 * registered with g_object_class_install_property()
1814 * \param [in] value The GValue the property is being set from
1815 * \param [in] pspec A GParamSpec describing the property being set
1818 static void multiattrib_set_property (GObject *object,
1819 guint property_id,
1820 const GValue *value,
1821 GParamSpec *pspec)
1823 Multiattrib *multiattrib = MULTIATTRIB (object);
1825 switch(property_id) {
1826 case PROP_SELECTION:
1827 disconnect_selection (multiattrib);
1828 connect_selection (multiattrib, g_value_get_pointer (value));
1829 break;
1830 default:
1831 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1836 /*! \brief GObject property getter function
1838 * \par Function Description
1839 * Getter function for Multiattrib's GObject property, "selection".
1841 * \param [in] object The GObject whose properties we are getting
1842 * \param [in] property_id The numeric id. under which the property was
1843 * registered with g_object_class_install_property()
1844 * \param [out] value The GValue in which to return the value of the property
1845 * \param [in] pspec A GParamSpec describing the property being got
1847 static void multiattrib_get_property (GObject *object,
1848 guint property_id,
1849 GValue *value,
1850 GParamSpec *pspec)
1852 Multiattrib *multiattrib = MULTIATTRIB (object);
1854 switch(property_id) {
1855 case PROP_SELECTION:
1856 g_value_set_pointer (value, (gpointer)multiattrib->selection);
1857 break;
1858 default:
1859 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1865 /*! \brief Update the multiattrib editor dialog's interface
1867 * \par Function Description
1869 * Update the dialog to reflect the attributes of the currently selected
1870 * object. If no (or multiple) objects are selected, the dialog's controls
1871 * are set insensitive.
1873 * \todo The dialog doesn't currently support editing multiple objects at once
1875 * \param [in] multiattrib The multi-attribute editor dialog.
1877 void multiattrib_update (Multiattrib *multiattrib)
1879 GtkListStore *liststore;
1880 GtkTreeIter iter;
1881 OBJECT **object_attribs, *o_current;
1882 gint i;
1883 gboolean sensitive;
1884 GtkStyle *style;
1886 g_assert (GSCHEM_DIALOG (multiattrib)->toplevel != NULL);
1888 /* clear the list of attributes */
1889 liststore = (GtkListStore*)gtk_tree_view_get_model (multiattrib->treeview);
1890 gtk_list_store_clear (liststore);
1892 /* Update sensitivities */
1893 sensitive = (multiattrib->selection != NULL && multiattrib->object != NULL);
1894 gtk_widget_set_sensitive (GTK_WIDGET (multiattrib->frame_attributes), sensitive);
1895 gtk_widget_set_sensitive (GTK_WIDGET (multiattrib->frame_add), sensitive);
1897 /* Work around GtkTextView's stubborn indifference
1898 * to GTK_STATE_INSENSITIVE when rendering its text. */
1899 style = gtk_widget_get_style (GTK_WIDGET (multiattrib->textview_value));
1900 gtk_widget_modify_text (GTK_WIDGET (multiattrib->textview_value),
1901 GTK_STATE_NORMAL,
1902 sensitive ? &multiattrib->value_normal_text_color
1903 : &style->text[GTK_STATE_INSENSITIVE]);
1905 /* If we aren't sensitive, there is nothing more to do */
1906 if (!sensitive)
1907 return;
1909 /* get list of attributes */
1910 object_attribs = o_attrib_return_attribs (
1911 GSCHEM_DIALOG (multiattrib)->toplevel->page_current->object_head,
1912 multiattrib->object);
1913 /* populate the store with attributes */
1914 if (object_attribs) {
1915 for (i = 0, o_current = object_attribs[i];
1916 o_current != NULL;
1917 i++, o_current = object_attribs[i]) {
1918 gtk_list_store_append (liststore, &iter);
1919 gtk_list_store_set (liststore, &iter,
1920 COLUMN_ATTRIBUTE, o_current,
1921 -1);
1924 /* delete the list of attribute objects */
1925 o_attrib_free_returned (object_attribs);