2006-12-05 David Lodge <dave@cirt.net>
[dia.git] / objects / AADL / edit_port_declaration.c
blob12f8e710852c05c3d2941a55b3053baff29d109c
1 /* AADL plugin for DIA
3 * Copyright (C) 2005 Laboratoire d'Informatique de Paris 6
4 * Author: Pierre Duquesne
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-1307, USA.
22 #include <gtk/gtk.h>
23 #include <string.h>
24 #include "aadl.h"
26 int aadlbox_point_near_port(Aadlbox *aadlbox, Point *p);
30 /***********************************************
31 ** U N D O / R E D O **
32 ***********************************************/
34 struct EditPortDeclarationChange
36 ObjectChange obj_change;
38 int applied;
40 int port_num;
42 gchar *oldvalue;
43 gchar *newvalue;
46 static void edit_port_declaration_apply
47 (struct EditPortDeclarationChange *change, DiaObject *obj)
49 Aadlbox *aadlbox = (Aadlbox *) obj;
50 int port_num = change->port_num;
52 change->applied = 1;
53 aadlbox->ports[port_num]->declaration = change->newvalue;
57 static void edit_port_declaration_revert
58 (struct EditPortDeclarationChange *change, DiaObject *obj)
60 Aadlbox *aadlbox = (Aadlbox *) obj;
61 int port_num = change->port_num;
63 change->applied = 0;
64 aadlbox->ports[port_num]->declaration = change->oldvalue;
68 static void edit_port_declaration_free (struct EditPortDeclarationChange *change)
70 if (change->applied)
71 g_free(change->oldvalue);
72 else
73 g_free(change->newvalue);
78 /***********************************************
79 ** G T K W I N D O W **
80 ***********************************************/
82 static GtkWidget *entry;
83 static gchar *text;
85 static void save_text()
87 text = (gchar *) g_malloc (strlen(gtk_entry_get_text (GTK_ENTRY (entry)))+1);
88 strcpy(text, gtk_entry_get_text (GTK_ENTRY (entry)));
91 static gboolean delete_event ( GtkWidget *widget,
92 GdkEvent *event,
93 gpointer data )
95 save_text();
96 return FALSE;
100 static void enter_callback( GtkWidget *widget,
101 GtkWidget *window )
103 save_text();
104 gtk_widget_destroy(window);
107 static gboolean focus_out_event( GtkWidget *widget,
108 GdkEventButton *event,
109 GtkWidget *window )
111 save_text();
112 gtk_widget_destroy(window);
113 return FALSE;
117 /* I have to write this little GTK code, because there's no way to know
118 which point was clicked if I use the properties functions (get_props, ...)*/
120 ObjectChange *edit_port_declaration_callback (DiaObject *obj,
121 Point *clicked, gpointer data)
123 GtkWidget *window;
124 GtkWidget *vbox;
125 GtkWidget *button;
126 struct EditPortDeclarationChange *change;
127 Aadlport *port;
128 Aadlbox *aadlbox = (Aadlbox *) obj;
129 int port_num;
131 gtk_init (0, NULL);
133 port_num = aadlbox_point_near_port(aadlbox, clicked);
134 port = aadlbox->ports[port_num];
137 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
138 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
139 gtk_widget_set_usize(window,400,50);
140 gtk_window_set_title(GTK_WINDOW(window) , "Port Declaration");
141 gtk_container_set_border_width(GTK_CONTAINER(window),5);
143 vbox = gtk_vbox_new (FALSE, 0);
144 gtk_container_add (GTK_CONTAINER (window), vbox);
145 gtk_widget_show (vbox);
147 entry = gtk_entry_new ();
148 gtk_entry_set_max_length (GTK_ENTRY (entry), 1024);
149 gtk_entry_set_text (GTK_ENTRY (entry), port->declaration);
150 gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);
151 gtk_widget_show(entry);
153 button = gtk_button_new_from_stock (GTK_STOCK_OK);
154 gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
155 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
156 gtk_widget_grab_default (button);
157 gtk_widget_show (button);
161 g_signal_connect (G_OBJECT (window), "destroy",
162 G_CALLBACK (gtk_main_quit), NULL);
164 g_signal_connect_swapped (G_OBJECT (window), "delete_event",
165 G_CALLBACK(delete_event),
166 G_OBJECT (window) );
168 g_signal_connect (G_OBJECT (entry), "activate",
169 G_CALLBACK (enter_callback),
170 (gpointer) window);
172 g_signal_connect (G_OBJECT (button), "clicked",
173 G_CALLBACK (enter_callback),
174 (gpointer) window);
177 /* avoid bug when quitting DIA with this window opened */
178 g_signal_connect (G_OBJECT (window), "focus_out_event",
179 G_CALLBACK (focus_out_event),
180 (gpointer) window);
183 gtk_widget_show (window);
185 gtk_main();
187 /* Text has been edited - widgets destroyed */
189 change = (struct EditPortDeclarationChange *)
190 g_malloc (sizeof(struct EditPortDeclarationChange));
192 change->obj_change.apply =
193 (ObjectChangeApplyFunc) edit_port_declaration_apply;
195 change->obj_change.revert =
196 (ObjectChangeRevertFunc) edit_port_declaration_revert;
198 change->obj_change.free =
199 (ObjectChangeFreeFunc) edit_port_declaration_free;
201 change->port_num = port_num;
203 change->newvalue = text;
204 change->oldvalue = aadlbox->ports[port_num]->declaration;
206 change->obj_change.apply(change, obj);
208 return (ObjectChange *) change;