1 /* AADL plugin for DIA
2 *
3 * Copyright (C) 2005 Laboratoire d'Informatique de Paris 6
4 * Author: Pierre Duquesne
5 *
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.
10 *
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.
15 *
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.
19 */
20 
21 
22 #include "aadl.h"
23 #include "pixmaps/aadldata.xpm"
24 
25 /***********************************************
26  **                 AADL DATA                 **
27  ***********************************************/
28 
29 
30 static void
aadldata_draw_borders(Aadlbox * aadlbox,DiaRenderer * renderer)31 aadldata_draw_borders(Aadlbox *aadlbox, DiaRenderer *renderer)
32 {
33   DiaRendererClass *renderer_ops = DIA_RENDERER_GET_CLASS (renderer);
34   Element *elem;
35   real x, y, w, h;
36   Point points[2];
37 
38   assert(aadlbox != NULL);
39   assert(renderer != NULL);
40 
41   elem = &aadlbox->element;
42 
43   x = elem->corner.x;
44   y = elem->corner.y;
45   w = elem->width;
46   h = elem->height;
47 
48   renderer_ops->set_fillstyle(renderer, FILLSTYLE_SOLID);
49   renderer_ops->set_linewidth(renderer, AADLBOX_BORDERWIDTH);
50   renderer_ops->set_linestyle(renderer, LINESTYLE_SOLID);
51 
52   points[0].x = x;     points[0].y = y;
53   points[1].x = x + w; points[1].y = y + h;
54 
55   renderer_ops->fill_rect(renderer, points, points + 1, &aadlbox->fill_color);
56   renderer_ops->draw_rect(renderer, points, points + 1, &aadlbox->line_color);
57 }
58 
59 static void
aadldata_draw(Aadlbox * aadlbox,DiaRenderer * renderer)60 aadldata_draw(Aadlbox *aadlbox, DiaRenderer *renderer)
61 {
62   aadldata_draw_borders(aadlbox, renderer);
63   aadlbox_draw(aadlbox, renderer);
64 }
65 
66 void
aadlbox_project_point_on_rectangle(Rectangle * rectangle,Point * p,real * angle)67 aadlbox_project_point_on_rectangle(Rectangle *rectangle,Point *p,real *angle)
68 {
69 
70   /* top left corner */
71   coord x1 = rectangle->left;
72   coord y1 = rectangle->top;
73 
74   /* bottom right corner */
75   coord x2 = rectangle->right;
76   coord y2 = rectangle->bottom;
77 
78   /* _ outside box: */
79 
80   /* left side*/
81   if (p->x <= x1 && p->y<=y1) { p->x = x1; p->y = y1; *angle = 1.25 * M_PI;}
82   else if (p->x <= x1 && p->y >= y2) {p->x = x1; p->y = y2; *angle=0.75*M_PI;}
83   else if (p->x <= x1 && p->y >= y1 && p->y <= y2) {p->x = x1;*angle = M_PI;}
84 
85   /* right side */
86   else if (p->x >= x2 && p->y<=y1) {p->x = x2; p->y = y1; *angle = 1.75*M_PI;}
87   else if (p->x >= x2 && p->y >= y2) {p->x = x2; p->y = y2; *angle=0.25*M_PI;}
88   else if (p->x >= x2 && p->y >= y1 && p->y <= y2) { p->x = x2; *angle = 0;}
89 
90   /* upper */
91   else if (p->y <= y1) {p->y = y1;  *angle = 1.5 * M_PI;}
92 
93   /* lower */
94   else if (p->y >= y2) {p->y = y2; *angle = 0.5 * M_PI;}
95 
96   /* _ inside box: */
97   else {
98     /* distances to border */
99     real d1 = p->x - x1;
100     real d2 = x2 - p->x;
101     real d3 = p->y - y1;
102     real d4 = y2 - p->y;
103 
104     real mini = min(min(d1,d2), min(d3,d4));
105 
106     if (d1 == mini)      { p->x = x1; *angle = M_PI     ;}
107     else if (d2 == mini) { p->x = x2; *angle = 0        ;}
108     else if (d3 == mini) { p->y = y1; *angle = 1.5*M_PI ;}
109     else if (d4 == mini) { p->y = y2; *angle = 0.5*M_PI ;}
110   }
111 }
112 
113 void
aadldata_project_point_on_nearest_border(Aadlbox * aadlbox,Point * p,real * angle)114 aadldata_project_point_on_nearest_border(Aadlbox *aadlbox,Point *p,real *angle)
115 {
116   Rectangle rectangle;
117 
118   rectangle.left = aadlbox->element.corner.x;
119   rectangle.top  = aadlbox->element.corner.y;
120   rectangle.right  = aadlbox->element.corner.x + aadlbox->element.width;
121   rectangle.bottom = aadlbox->element.corner.y + aadlbox->element.height;
122 
123   aadlbox_project_point_on_rectangle(&rectangle, p, angle);
124 }
125 
126 
127 
128 static Aadlbox_specific aadldata_specific =
129 {
130   (AadlProjectionFunc) aadldata_project_point_on_nearest_border,
131   (AadlTextPosFunc)    aadldata_text_position,
132   (AadlSizeFunc) aadldata_minsize
133 };
134 
135 
136 
137 ObjectTypeOps aadldata_type_ops;
138 
139 DiaObjectType aadldata_type =
140 {
141   "AADL - Data",           /* name */
142   0,                      /* version */
143   (char **) aadldata_xpm,  /* pixmap */
144 
145   &aadldata_type_ops,       /* ops */
146   NULL,
147   &aadldata_specific      /* user data */
148 };
149 
150 
151 static ObjectOps aadldata_ops =
152 {
153   (DestroyFunc)         aadlbox_destroy,
154   (DrawFunc)            aadldata_draw,              /* redefined */
155   (DistanceFunc)        aadlbox_distance_from,
156   (SelectFunc)          aadlbox_select,
157   (CopyFunc)            aadlbox_copy,
158   (MoveFunc)            aadlbox_move,
159   (MoveHandleFunc)      aadlbox_move_handle,
160   (GetPropertiesFunc)   object_create_props_dialog,
161   (ApplyPropertiesDialogFunc) object_apply_props_from_dialog,
162   (ObjectMenuFunc)      aadlbox_get_object_menu,
163   (DescribePropsFunc)   aadlbox_describe_props,
164   (GetPropsFunc)        aadlbox_get_props,
165   (SetPropsFunc)        aadlbox_set_props,
166   (TextEditFunc) 0,
167   (ApplyPropertiesListFunc) object_apply_props,
168 };
169 
170 
171 
aadldata_create(Point * startpoint,void * user_data,Handle ** handle1,Handle ** handle2)172 static DiaObject *aadldata_create(Point *startpoint, void *user_data, Handle **handle1, Handle **handle2)
173 {
174   DiaObject *obj = aadlbox_create(startpoint, user_data, handle1, handle2);
175 
176   obj->type = &aadldata_type;
177   obj->ops  = &aadldata_ops;
178 
179   return obj;
180 }
181 
aadldata_load(ObjectNode obj_node,int version,const char * filename)182 static DiaObject *aadldata_load(ObjectNode obj_node, int version, const char *filename)
183 {
184   DiaObject *obj;
185   Point startpoint = {0.0,0.0};
186   Handle *handle1,*handle2;
187 
188   obj = aadldata_create(&startpoint,&aadldata_specific, &handle1,&handle2);
189   aadlbox_load(obj_node, version, filename, (Aadlbox *) obj);
190 
191   return obj;
192 }
193 
194 
195 ObjectTypeOps aadldata_type_ops =
196 {
197   (CreateFunc) aadldata_create,
198   (LoadFunc)   aadldata_load,      /* load */
199   (SaveFunc)   aadlbox_save,       /* save */
200   (GetDefaultsFunc)   NULL,
201   (ApplyDefaultsFunc) NULL
202 };
203