1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
/* AADL plugin for DIA
*
* Copyright (C) 2005 Laboratoire d'Informatique de Paris 6
* Author: Pierre Duquesne
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <gtk/gtk.h>
#include <string.h>
#include "aadl.h"
#include "edit_port_declaration.h"
int aadlbox_point_near_port(Aadlbox *aadlbox, Point *p);
/***********************************************
** U N D O / R E D O **
***********************************************/
struct EditPortDeclarationChange
{
ObjectChange obj_change;
int applied;
int port_num;
gchar *oldvalue;
gchar *newvalue;
};
static void edit_port_declaration_apply
(struct EditPortDeclarationChange *change, DiaObject *obj)
{
Aadlbox *aadlbox = (Aadlbox *) obj;
int port_num = change->port_num;
change->applied = 1;
aadlbox->ports[port_num]->declaration = change->newvalue;
}
static void edit_port_declaration_revert
(struct EditPortDeclarationChange *change, DiaObject *obj)
{
Aadlbox *aadlbox = (Aadlbox *) obj;
int port_num = change->port_num;
change->applied = 0;
aadlbox->ports[port_num]->declaration = change->oldvalue;
}
static void edit_port_declaration_free (struct EditPortDeclarationChange *change)
{
if (change->applied)
g_free(change->oldvalue);
else
g_free(change->newvalue);
}
/***********************************************
** G T K W I N D O W **
***********************************************/
static GtkWidget *entry;
static gchar *text;
static void save_text()
{
text = (gchar *) g_malloc (strlen(gtk_entry_get_text (GTK_ENTRY (entry)))+1);
strcpy(text, gtk_entry_get_text (GTK_ENTRY (entry)));
}
static gboolean delete_event ( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
save_text();
return FALSE;
}
static void enter_callback( GtkWidget *widget,
GtkWidget *window )
{
save_text();
gtk_widget_destroy(window);
}
static gboolean focus_out_event( GtkWidget *widget,
GdkEventButton *event,
GtkWidget *window )
{
save_text();
gtk_widget_destroy(window);
return FALSE;
}
/* I have to write this little GTK code, because there's no way to know
which point was clicked if I use the properties functions (get_props, ...)*/
ObjectChange *edit_port_declaration_callback (DiaObject *obj,
Point *clicked, gpointer data)
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *button;
struct EditPortDeclarationChange *change;
Aadlport *port;
Aadlbox *aadlbox = (Aadlbox *) obj;
int port_num;
gtk_init (0, NULL);
port_num = aadlbox_point_near_port(aadlbox, clicked);
port = aadlbox->ports[port_num];
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_widget_set_size_request(window,400,50);
gtk_window_set_title(GTK_WINDOW(window) , "Port Declaration");
gtk_container_set_border_width(GTK_CONTAINER(window),5);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
entry = gtk_entry_new ();
gtk_entry_set_max_length (GTK_ENTRY (entry), 1024);
gtk_entry_set_text (GTK_ENTRY (entry), port->declaration);
gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);
gtk_widget_show(entry);
button = gtk_button_new_from_stock (GTK_STOCK_OK);
gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
#if GTK_CHECK_VERSION(2,18,0)
gtk_widget_set_can_default (GTK_WIDGET (button), TRUE);
#else
GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
#endif
gtk_widget_grab_default (button);
gtk_widget_show (button);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect_swapped (G_OBJECT (window), "delete_event",
G_CALLBACK(delete_event),
G_OBJECT (window) );
g_signal_connect (G_OBJECT (entry), "activate",
G_CALLBACK (enter_callback),
(gpointer) window);
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (enter_callback),
(gpointer) window);
/* avoid bug when quitting DIA with this window opened */
g_signal_connect (G_OBJECT (window), "focus_out_event",
G_CALLBACK (focus_out_event),
(gpointer) window);
gtk_widget_show (window);
gtk_main();
/* Text has been edited - widgets destroyed */
change = (struct EditPortDeclarationChange *)
g_malloc (sizeof(struct EditPortDeclarationChange));
change->obj_change.apply =
(ObjectChangeApplyFunc) edit_port_declaration_apply;
change->obj_change.revert =
(ObjectChangeRevertFunc) edit_port_declaration_revert;
change->obj_change.free =
(ObjectChangeFreeFunc) edit_port_declaration_free;
change->port_num = port_num;
change->newvalue = text;
change->oldvalue = aadlbox->ports[port_num]->declaration;
change->obj_change.apply((ObjectChange *)change, obj);
return (ObjectChange *) change;
}
|