NEURON
rect.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #if HAVE_IV // to end of file
3 
4 #include <InterViews/glyph.h>
5 #include <InterViews/session.h>
6 #include <InterViews/style.h>
7 #include <InterViews/canvas.h>
8 #include <InterViews/color.h>
9 #include <InterViews/hit.h>
10 #include <InterViews/brush.h>
11 #include <InterViews/transformer.h>
12 #include <IV-look/kit.h>
13 #include <OS/string.h>
14 #include "scenevie.h"
15 #include "rect.h"
16 #include "mymath.h"
17 #include "idraw.h"
18 #include <stdio.h>
19 
20 Appear::Appear(const Color* c, const Brush* b) {
21  color_ = NULL;
22  brush_ = NULL;
23  color(c);
24  brush(b);
25 }
29 }
30 
31 void Appear::color(const Color* c) {
32  const Color* c1;
33  if (c) {
34  c1 = c;
35  }else{
36  c1 = default_color();
37  }
38  Resource::ref(c1);
40  color_ = c1;
41 }
42 void Appear::brush(const Brush* b) {
43  const Brush* b1;
44  if (b) {
45  b1 = b;
46  }else{
47  b1 = default_brush();
48  }
49  Resource::ref(b1);
51  brush_ = b1;
52 }
53 
54 const Color* Appear::dc_;
55 const Brush* Appear::db_;
56 
59 }
60 
62  if (!db_) {
63  Style* s = Session::instance()->style();
64  Coord b = 0;
65  s->find_attribute("default_brush", b);
66  db_ = new Brush(b);
68  }
69  return db_;
70 }
71 
72 //Rect
73 Rect::Rect(Coord l, Coord b, Coord w, Coord h, const Color* c, const Brush* br)
74  : Appear(c, br) {
75  left(l); bottom(b); width(w); height(h);
76 }
77 
78 void Rect::request(Requisition& req) const {
79  Requirement rx(width(), 0, 0, left()/width());
80  Requirement ry(height(), 0, 0, bottom()/height());
81  req.require_x(rx);
82  req.require_y(ry);
83 }
84 
85 void Rect::allocate(Canvas* c, const Allocation& a, Extension& ext) {
86  ext.set(c, a);
87  MyMath::extend(ext, 1);
88 }
89 
90 void Rect::draw(Canvas* c, const Allocation& a) const {
91  Coord x = a.x();
92  Coord y = a.y();
93  c->rect(x + left(), y+ bottom(), x + right(), y + top(), color(), brush());
94 }
95 
96 void Rect::pick(Canvas*, const Allocation&, int depth, Hit& h) {
97  Coord x = h.left();
98  Coord y = h.bottom();
99  if (MyMath::inside(x, y, left(), right(), bottom(), top())) {
100  h.target(depth, this, 0);
101  }
102 }
103 
104 //Line
105 Line::Line(Coord dx, Coord dy, const Color* c, const Brush* b) : Appear(c, b) {
106  dx_ = dx;
107  dy_ = dy;
108  x_ = 0;
109  y_ = 0;
110 }
111 Line::Line(Coord dx, Coord dy, float xa, float ya,
112  const Color* c, const Brush* b) : Appear(c, b) {
113  dx_ = dx;
114  dy_ = dy;
115  x_ = -dx_*xa;
116  y_ = -dy_*ya;
117 }
118 Line::~Line(){}
119 
120 void Line::request(Requisition& req) const {
121  Coord dx, dy;
122  dx = (dx_)?dx_:1e-5;
123  dy = (dy_)?dy_:1e-5;
124  Requirement rx(dx, 0, 0, x_/dx);
125  Requirement ry(dy, 0, 0, y_/dy);
126  req.require_x(rx);
127  req.require_y(ry);
128 }
129 
130 void Line::allocate(Canvas* c, const Allocation& a, Extension& ext) {
131  ext.set(c, a);
132  MyMath::extend(ext, brush()->width()/2 + 1);
133 }
134 
135 #if 0
136 void Line::draw(Canvas* c, const Allocation& a) const {
137  Coord x = a.x() + x_;
138  Coord y = a.y() + y_;
139 //printf("line %g %g %g %g\n", x, y, dx_, dy_);
140  c->line(x, y, x + dx_, y + dy_, color(), brush());
141  IfIdraw(line(c, x, y, x + dx_, y + dy_, color(), brush()));
142 }
143 #else
144 void Line::draw(Canvas* c, const Allocation& a) const {
145  Coord x = a.x() + x_;
146  Coord y = a.y() + y_;
147 //printf("line %g %g %g %g\n", x, y, dx_, dy_);
148 
149  c->new_path();
150  c->move_to(x, y);
151  c->line_to(x+dx_,y+dy_);
152 
153 // transform the line to get thickness right on printer
154  XYView::current_draw_view()->stroke(c, color(), brush());
155  IfIdraw(line(c, x, y, x + dx_, y + dy_, color(), brush()));
156 }
157 #endif
158 
159 void Line::pick(Canvas* c, const Allocation& a, int depth, Hit& h) {
160  Coord l=a.x() + x_, b=a.y() + y_, r=a.x()+ x_ + dx_, t=a.y()+ y_ + dy_;
161  Coord x = h.left();
162  Coord y = h.bottom();
163  if (MyMath::inside(x,y,l,b,r,t)) {
164  float epsilon = 5;
165  const Transformer& tr = c->transformer();
166  tr.transform(x,y);
167  tr.transform(l,b);
168  tr.transform(r,t);
169  if (MyMath::near_line(x, y, l, b, r, t, epsilon)) {
170  h.target(depth, this, 0);
171  }
172  }
173 }
174 
175 //Circle
176 Circle::Circle(float radius, bool filled, const Color* c, const Brush* b)
177  :Appear(c, b)
178 {
179  radius_ = radius;
180  filled_ = filled;
181 }
182 
183 Circle::~Circle() {}
184 
185 void Circle::request(Requisition& req) const {
186  Coord w = brush()->width();
187  Coord diameter = radius_ + radius_ + w + w;
188  Requirement rx(diameter, 0, 0, 0.5);
189  Requirement ry(diameter, 0, 0, 0.5);
190  req.require(Dimension_X, rx);
191  req.require(Dimension_Y, ry);
192 }
193 
194 void Circle::allocate(Canvas* c, const Allocation& a, Extension& ext) {
195  ext.merge(c, a);
196  MyMath::extend(ext, brush()->width()/2 + 1);
197 }
198 
199 void Circle::draw(Canvas* c, const Allocation& a) const {
200  const Coord r = radius_, x = a.x(), y = a.y();
201  const Coord p0 = 1.00000000 * r;
202  const Coord p1 = 0.89657547 * r; // cos 30 * sqrt(1 + tan 15 * tan 15)
203  const Coord p2 = 0.70710678 * r; // cos 45
204  const Coord p3 = 0.51763809 * r; // cos 60 * sqrt(1 + tan 15 * tan 15)
205  const Coord p4 = 0.26794919 * r; // tan 15
206  c->new_path();
207  c->move_to(x+p0, y);
208  c->curve_to(x+p2, y+p2, x+p0, y+p4, x+p1, y+p3);
209  c->curve_to(x, y+p0, x+p3, y+p1, x+p4, y+p0);
210  c->curve_to(x-p2, y+p2, x-p4, y+p0, x-p3, y+p1);
211  c->curve_to(x-p0, y, x-p1, y + p3, x-p0, y+p4);
212  c->curve_to(x-p2, y-p2, x-p0, y-p4, x-p1, y-p3);
213  c->curve_to(x, y-p0, x-p3, y-p1, x-p4, y-p0);
214  c->curve_to(x+p2, y-p2, x+p4, y-p0, x+p3, y-p1);
215  c->curve_to(x+p0, y, x+p1, y-p3, x+p0, y-p4);
216  c->close_path();
217  if (filled_) {
218  c->fill(color());
219  }else{
220  c->stroke(color(), brush());
221  }
222  IfIdraw(ellipse(c, x, y, r, r, color(), brush(), filled_));
223 }
224 
225 
226 //Rectangle
227 Rectangle::Rectangle(float height, float width, bool filled, const Color* c, const Brush* b)
228  :Appear(c, b)
229 {
230  height_ = height;
231  width_ = width;
232  filled_ = filled;
233 }
234 
236 
237 void Rectangle::request(Requisition& req) const {
238  Coord w = brush()->width();
239  Requirement rx(width_ + w + w, 0, 0, 0.5);
240  Requirement ry(height_ + w + w, 0, 0, 0.5);
241  req.require(Dimension_X, rx);
242  req.require(Dimension_Y, ry);
243 }
244 
245 void Rectangle::allocate(Canvas* c, const Allocation& a, Extension& ext) {
246  ext.merge(c, a);
247  MyMath::extend(ext, brush()->width()/2 + 1);
248 }
249 
250 void Rectangle::draw(Canvas* c, const Allocation& a) const {
251  const Coord dx = width_/2, dy = height_/2, x = a.x(), y = a.y();
252 
253  if (filled_) {
254  c->fill_rect(x-dx,y-dy,x+dx,y+dy,color());
255  } else {
256  c->rect(x-dx,y-dy,x+dx,y+dy,color(),brush());
257  }
258  IfIdraw(rect(c, x-dx, y-dy, x+dx, y+dy, color(), brush(), filled_));
259 }
260 
261 
262 //Triangle
263 Triangle::Triangle(float side, bool filled, const Color* c, const Brush* b)
264  :Appear(c, b)
265 {
266  side_ = side/2;
267  filled_ = filled;
268 }
269 
271 
272 void Triangle::request(Requisition& req) const {
273  Coord w = brush()->width();
274  Requirement rx(side_ + side_+ w + w, 0, 0, 0.5);
275  Requirement ry((side_ + side_)*1.1547 + w + w, 0, 0, 0.5);
276  req.require(Dimension_X, rx);
277  req.require(Dimension_Y, ry);
278 }
279 
280 void Triangle::allocate(Canvas* c, const Allocation& a, Extension& ext) {
281  ext.merge(c, a);
282  MyMath::extend(ext, brush()->width()/2 + 1);
283 }
284 
285 void Triangle::draw(Canvas* c, const Allocation& a) const {
286  const Coord x = a.x(), y = a.y();
287  const Coord radius = 1.1547*side_;
288 
289  c->new_path();
290  c->move_to(x, y+radius);
291  c->line_to(x+side_, y-radius);
292  c->line_to(x-side_, y-radius);
293  c->close_path();
294  if (filled_) {
295  c->fill(color());
296  } else {
297  c->stroke(color(),brush());
298  }
299 
300  Coord *xList = new Coord[4];
301  Coord *yList = new Coord[4];
302 
303  xList[0] = x;
304  xList[1] = x+side_;
305  xList[2] = x-side_;
306  xList[3] = x;
307 
308  yList[0] = y+radius;
309  yList[1] = y-radius;
310  yList[2] = y-radius;
311  yList[3] = y+radius;
312 
313  IfIdraw(polygon(c, 3, xList, yList, color(), brush(), filled_));
314 
315  delete [] xList;
316  delete [] yList;
317 }
318 
319 
320 
321 #endif
void merge(const Extension &)
void set(Canvas *, const Allocation &)
virtual void draw(Canvas *, const Allocation &) const
Coord x() const
Definition: geometry.h:290
virtual ~Triangle()
virtual void allocate(Canvas *, const Allocation &, Extension &)
const Color * color_
Definition: rect.h:27
#define Coord
Definition: _defines.h:19
#define Brush
Definition: _defines.h:59
static const Color * dc_
Definition: rect.h:29
#define Color
Definition: _defines.h:74
static const Color * default_color()
const Brush * brush() const
Definition: rect.h:22
virtual void pick(Canvas *, const Allocation &, int depth, Hit &)
virtual void stroke(Canvas *, const Color *, const Brush *)
#define IfIdraw(arg)
Definition: idraw.h:61
virtual ~Line()
Coord y() const
Definition: geometry.h:291
virtual void ref() const
Definition: resource.cpp:47
static bool inside(Coord x, Coord min, Coord max)
Definition: mymath.h:97
virtual void pick(Canvas *, const Allocation &, int depth, Hit &)
#define e
Definition: passive0.cpp:24
virtual void draw(Canvas *, const Allocation &) const
const Color * color() const
Definition: rect.h:20
static const Color * default_foreground()
static const Brush * default_brush()
const Brush * brush_
Definition: rect.h:28
virtual void draw(Canvas *, const Allocation &) const
virtual void request(Requisition &) const
#define color
Definition: rbtqueue.cpp:50
virtual void allocate(Canvas *, const Allocation &, Extension &)
virtual void request(Requisition &) const
_CONST char * s
Definition: system.cpp:74
Line(Coord dx, Coord dy, const Color *color=NULL, const Brush *brush=NULL)
static N_Vector x_
virtual void request(Requisition &) const
static XYView * current_draw_view()
#define Canvas
Definition: _defines.h:65
void require_y(const Requirement &)
Definition: geometry.h:248
Definition: rect.h:15
static void extend(Extension &, Coord)
Definition: mymath.h:89
virtual void draw(Canvas *, const Allocation &) const
virtual void unref() const
Definition: resource.cpp:52
virtual void request(Requisition &) const
static bool near_line(Coord x, Coord y, Coord x1, Coord y1, Coord x2, Coord y2, float epsilon)
Definition: mymath.cpp:115
static N_Vector y_
#define left
Definition: rbtqueue.cpp:45
virtual void request(Requisition &) const
#define right
Definition: rbtqueue.cpp:46
Rectangle(float height, float width, bool filled=false, const Color *color=NULL, const Brush *brush=NULL)
void require_x(const Requirement &)
Definition: geometry.h:247
virtual ~Rectangle()
#define Transformer
Definition: _defines.h:316
Rect(Coord left, Coord bottom, Coord width, Coord height, const Color *c=NULL, const Brush *b=NULL)
virtual ~Circle()
#define c
static char line[MAXLINE]
Definition: ivecop.c:35
virtual ~Appear()
#define Style
Definition: _defines.h:281
virtual void allocate(Canvas *, const Allocation &, Extension &)
virtual void allocate(Canvas *, const Allocation &, Extension &)
Triangle(float side, bool filled=false, const Color *color=NULL, const Brush *brush=NULL)
double t
Definition: init.cpp:123
virtual void draw(Canvas *, const Allocation &) const
virtual void allocate(Canvas *, const Allocation &, Extension &)
void require(DimensionName, const Requirement &)
return NULL
Definition: cabcode.cpp:461
Appear(const Color *color=NULL, const Brush *brush=NULL)
#define Hit
Definition: _defines.h:147
static const Brush * db_
Definition: rect.h:30
Circle(float radius, bool filled=false, const Color *color=NULL, const Brush *brush=NULL)