NEURON
ocdeck.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 #if HAVE_IV
4 #include <OS/string.h>
5 #include <InterViews/deck.h>
6 #include <InterViews/patch.h>
7 #include <InterViews/layout.h>
8 #include <InterViews/background.h>
9 #include <IV-look/kit.h>
10 #include <ivstream.h>
11 #include <stdio.h>
12 #include "ocdeck.h"
13 #include "apwindow.h"
14 #include "oc2iv.h"
15 #endif /* HAVE_IV */
16 #include "classreg.h"
17 #include "gui-redirect.h"
18 
19 extern Object** (*nrnpy_gui_helper_)(const char* name, Object* obj);
20 extern double (*nrnpy_object_to_double_)(Object*);
21 
22 #if HAVE_IV
23 class SpecialPatch: public Patch {
24  public:
25  SpecialPatch(Glyph*);
26  virtual ~SpecialPatch();
27  virtual void request(Requisition&) const;
28  virtual void allocate(Canvas*, const Allocation&, Extension&);
29  virtual void draw(Canvas*, const Allocation&) const;
30 };
31 
32 SpecialPatch::SpecialPatch(Glyph* g)
33  : Patch(g) {}
34 SpecialPatch::~SpecialPatch() {}
35 void SpecialPatch::request(Requisition& req) const {
36  Patch::request(req);
37 }
38 void SpecialPatch::allocate(Canvas* c, const Allocation& a, Extension& e) {
39 #if 0
40  Allocation aa = a;
41  if (aa.bottom() < 0.) {
42  Allotment& y = aa.y_allotment();
43  y.span((y.origin() - 0.)/y.alignment());
44 //printf("allotment %g %g %g %g %g\n", y.origin(), y.span(), y.alignment(), y.begin(), y.end());
45 //printf("SpecialPatch::allocate a.bottom=%g aa.bottom=%g\n", a.bottom(), aa.bottom());
46 }
47  Patch::allocate(c, aa, e);
48 #else
49  Patch::allocate(c, a, e);
50 #endif
51 }
52 
53 void SpecialPatch::draw(Canvas* c, const Allocation& a) const {
54 #if 1
55  Allocation aa = a;
56  if (aa.bottom() < 0.) {
57  Allotment& y = aa.y_allotment();
58  y.span((y.origin() - 0.) / y.alignment());
59  }
60  Patch::draw(c, aa);
61 #else
62  Patch::draw(c, a);
63 #endif
64 }
65 
66 /*static*/ class OcDeckImpl {
67  public:
68  PolyGlyph* ocglyph_list_;
69  Deck* deck_;
70  Object* oc_ref_; // reference to oc "this"
71  CopyString* save_action_;
72 };
73 #endif /* HAVE_IV */
74 
75 static void* cons(Object*) {
76  TRY_GUI_REDIRECT_OBJ("Deck", NULL);
77 #if HAVE_IV
78  OcDeck* b = NULL;
79  IFGUI
80  b = new OcDeck();
81  b->ref();
82  ENDGUI
83  return (void*) b;
84 #else
85  return nullptr;
86 #endif /* HAVE_IV */
87 }
88 
89 static void destruct(void* v) {
90  TRY_GUI_REDIRECT_NO_RETURN("~Deck", v);
91 #if HAVE_IV
92  IFGUI
93  OcDeck* b = (OcDeck*) v;
94  if (b->has_window()) {
95  b->window()->dismiss();
96  }
97  b->unref();
98  ENDGUI
99 #endif /* HAVE_IV */
100 }
101 
102 static double intercept(void* v) {
103  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.intercept", v);
104 #if HAVE_IV
105  bool b = int(chkarg(1, 0., 1.));
106  IFGUI((OcDeck*) v)->intercept(b);
107  ENDGUI
108  return double(b);
109 #else
110  return 0.;
111 #endif /* HAVE_IV */
112 }
113 
114 static double map(void* v) {
115  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.map", v);
116 #if HAVE_IV
117  IFGUI
118  OcDeck* b = (OcDeck*) v;
119  PrintableWindow* w;
120  if (ifarg(3)) {
121  w = b->make_window(float(*getarg(2)),
122  float(*getarg(3)),
123  float(*getarg(4)),
124  float(*getarg(5)));
125  } else {
126  w = b->make_window();
127  }
128  if (ifarg(1)) {
129  char* name = gargstr(1);
130  w->name(name);
131  }
132  w->map();
133  ENDGUI
134  return 1.;
135 #else
136  return 0.;
137 #endif /* HAVE_IV */
138 }
139 
140 static double unmap(void* v) {
141  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.unmap", v);
142 #if HAVE_IV
143  IFGUI
144  OcDeck* b = (OcDeck*) v;
145  if (b->has_window()) {
146  b->window()->dismiss();
147  }
148  ENDGUI
149  return 0.;
150 #else
151  return 0.;
152 #endif /* HAVE_IV */
153 }
154 
155 static double save(void* v) {
156  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.save", v);
157 #if HAVE_IV
158  IFGUI
159  OcDeck* b = (OcDeck*) v;
160 #if 0
161  int i;
162  Object* o[4];
163  for (i=0; i < 4; ++i) {
164  if (ifarg(i+1)) {
165  o[i] = *hoc_objgetarg(i+1);
166  }else{
167  o[i] = NULL;
168  }
169  }
170  b->save_action(gargstr(1), o[0]);
171 #else
172  b->save_action(gargstr(1), 0);
173 #endif
174  ENDGUI
175  return 1.;
176 #else
177  return 0.;
178 #endif /* HAVE_IV */
179 }
180 
181 static double flip_to(void* v) {
182  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.flip_to", v);
183 #if HAVE_IV
184  int i = -1;
185  IFGUI
186  OcDeck* b = (OcDeck*) v;
187  i = int(chkarg(1, -1, b->count() - 1));
188  b->flip_to(i);
189  ENDGUI
190  return double(i);
191 #else
192  return 0.;
193 #endif /* HAVE_IV */
194 }
195 
196 static double remove_last(void* v) {
197  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.remove_last", v);
198 #if HAVE_IV
199  IFGUI((OcDeck*) v)->remove_last();
200  ENDGUI
201  return 0.;
202 #else
203  return 0.;
204 #endif /* HAVE_IV */
205 }
206 
207 static double remove(void* v) {
208  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.remove", v);
209 #if HAVE_IV
210  IFGUI
211  OcDeck* b = (OcDeck*) v;
212  b->remove((int) chkarg(1, 0, b->count() - 1));
213  ENDGUI
214  return 0.;
215 #else
216  return 0.;
217 #endif /* HAVE_IV */
218 }
219 
220 static double move_last(void* v) {
221  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("Deck.move_last", v);
222 #if HAVE_IV
223  IFGUI
224  OcDeck* b = (OcDeck*) v;
225  b->move_last((int) chkarg(1, 0, b->count() - 1));
226  ENDGUI
227  return 0.;
228 #else
229  return 0.;
230 #endif /* HAVE_IV */
231 }
232 
233 static Member_func members[] = {"flip_to",
234  flip_to,
235  "intercept",
236  intercept,
237  "save",
238  save,
239  "map",
240  map,
241  "unmap",
242  unmap,
243  "remove_last",
244  remove_last,
245  "remove",
246  remove,
247  "move_last",
248  move_last,
249  0,
250  0};
251 
252 void OcDeck_reg() {
253  class2oc("Deck", cons, destruct, members, NULL, NULL, NULL);
254 }
255 #if HAVE_IV
257  : OcGlyphContainer() {
258  WidgetKit& wk = *WidgetKit::instance();
259  LayoutKit& lk = *LayoutKit::instance();
260  bi_ = new OcDeckImpl;
261  bi_->ocglyph_list_ = new PolyGlyph();
262  bi_->deck_ = new Deck(2);
263  Resource::ref(bi_->ocglyph_list_);
264  Resource::ref(bi_->deck_);
265  body(new SpecialPatch(new Background(
266  // wk.inset_frame(
267  lk.flexible(bi_->deck_
268  // )
269  ),
270  wk.background())));
271  bi_->oc_ref_ = NULL;
272  bi_->save_action_ = NULL;
273 }
274 
275 OcDeck::~OcDeck() {
276  Resource::unref(bi_->ocglyph_list_);
277  Resource::unref(bi_->deck_);
278  if (bi_->oc_ref_) {
279  hoc_dec_refcount(&bi_->oc_ref_);
280  }
281  if (bi_->save_action_) {
282  delete (bi_->save_action_);
283  }
284  delete bi_;
285 }
286 
287 void OcDeck::flip_to(int i) {
288  bi_->deck_->flip_to(GlyphIndex(i));
289  ((SpecialPatch*) body())->reallocate();
290  ((SpecialPatch*) body())->redraw();
291 }
292 
294  WidgetKit& wk = *WidgetKit::instance();
295  LayoutKit& lk = *LayoutKit::instance();
296  bi_->ocglyph_list_->append(g);
297  bi_->deck_->append(g);
298 }
299 
300 void OcDeck::remove_last() {
301  GlyphIndex last = bi_->ocglyph_list_->count() - 1;
302  if (last < 0) {
303  return;
304  }
305  if (bi_->deck_->card() == last) {
306  flip_to(-1);
307  }
308  bi_->ocglyph_list_->remove(last);
309  bi_->deck_->remove(last);
310 }
311 
312 void OcDeck::remove(int i) {
313  if (bi_->deck_->card() == i) {
314  flip_to(-1);
315  }
316  bi_->ocglyph_list_->remove(i);
317  bi_->deck_->remove(i);
318 }
319 
320 void OcDeck::move_last(int i) {
321  int last = bi_->ocglyph_list_->count() - 1;
322  if (i == last) {
323  return;
324  }
325  OcGlyph* g = (OcGlyph*) bi_->ocglyph_list_->component(last);
326  bi_->ocglyph_list_->insert(i, g);
327  bi_->deck_->insert(i, g);
328  last = bi_->ocglyph_list_->count() - 1;
329  bi_->ocglyph_list_->remove(last);
330  bi_->deck_->remove(last);
331 }
332 
333 void OcDeck::save_action(const char* creat, Object* o) {
334  bi_->save_action_ = new CopyString(creat);
335  if (o) {
336  bi_->oc_ref_ = o;
337  ++bi_->oc_ref_->refcount;
338  }
339 }
340 
341 void OcDeck::save(ostream& o) {
342  char buf[256];
343  if (bi_->save_action_) {
344  sprintf(buf, "{ocbox_ = %s", bi_->save_action_->string());
345  o << buf << endl;
346  } else {
347  o << "{ocbox_ = new Deck()" << endl;
348  o << "ocbox_list_.prepend(ocbox_)" << endl;
349  o << "ocbox_.intercept(1)}" << endl;
350  long i, cnt = bi_->ocglyph_list_->count();
351  for (i = 0; i < cnt; ++i) {
352  ((OcGlyph*) bi_->ocglyph_list_->component(i))->save(o);
353  }
354  o << "{ocbox_ = ocbox_list_.object(0)" << endl;
355  o << "ocbox_list_.remove(0)" << endl;
356  o << "ocbox_.intercept(0)" << endl;
357  }
358  if (has_window()) {
359  sprintf(buf,
360  "ocbox_.map(\"%s\", %g, %g, %g, %g)}",
361  window()->name(),
362  window()->save_left(),
363  window()->save_bottom(),
364  window()->width(),
365  window()->height());
366  o << buf << endl;
367  } else {
368  o << "ocbox_.map()}" << endl;
369  }
370  if (bi_->oc_ref_) {
371  sprintf(buf, "%s = ocbox_", hoc_object_pathname(bi_->oc_ref_));
372  o << buf << endl;
373  }
374 }
375 #endif /* HAVE_IV */
#define Patch
Definition: _defines.h:201
#define Background
Definition: _defines.h:43
#define Deck
Definition: _defines.h:93
#define Canvas
Definition: _defines.h:65
#define WidgetKit
Definition: _defines.h:331
#define GlyphIndex
Definition: _defines.h:23
#define PolyGlyph
Definition: _defines.h:207
#define LayoutKit
Definition: _defines.h:161
#define Glyph
Definition: _defines.h:132
#define CopyString
Definition: _defines.h:2
Coord bottom() const
Definition: geometry.h:294
Allotment & y_allotment()
Definition: geometry.h:286
void alignment(float)
Definition: geometry.h:271
void origin(Coord)
Definition: geometry.h:266
void span(Coord)
Definition: geometry.h:269
virtual const char * name() const
Definition: ocdeck.h:10
virtual void flip_to(int)
virtual void save_action(const char *, Object *)
virtual void box_append(OcGlyph *)
OcDeckImpl * bi_
Definition: ocdeck.h:23
virtual void move_last(int)
virtual void remove(int)
virtual ~OcDeck()
virtual void save(ostream &)
virtual void remove_last()
virtual PrintableWindow * window()
virtual bool has_window()
virtual void map()
virtual void ref() const
Definition: resource.cpp:47
virtual void unref() const
Definition: resource.cpp:52
sprintf(buf, " if (secondorder) {\n" " int _i;\n" " for (_i = 0; _i < %d; ++_i) {\n" " _p[_slist%d[_i]] += dt*_p[_dlist%d[_i]];\n" " }}\n", numeqn, listnum, listnum)
double chkarg(int, double low, double high)
Definition: code2.cpp:638
#define c
char buf[512]
Definition: init.cpp:13
char * hoc_object_pathname(Object *ob)
Definition: hoc_oop.cpp:1794
void hoc_dec_refcount(Object **pobj)
Definition: hoc_oop.cpp:1816
#define TRY_GUI_REDIRECT_ACTUAL_DOUBLE(name, obj)
Definition: gui-redirect.h:71
#define TRY_GUI_REDIRECT_NO_RETURN(name, obj)
Definition: gui-redirect.h:47
#define TRY_GUI_REDIRECT_OBJ(name, obj)
Definition: gui-redirect.h:12
#define IFGUI
Definition: hocdec.h:372
#define getarg
Definition: hocdec.h:15
#define gargstr
Definition: hocdec.h:14
#define ENDGUI
Definition: hocdec.h:373
Object ** hoc_objgetarg(int)
Definition: code.cpp:1587
int ifarg(int)
Definition: code.cpp:1581
#define v
Definition: md1redef.h:4
#define i
Definition: md1redef.h:12
char * name
Definition: init.cpp:16
void class2oc(const char *, void *(*cons)(Object *), void(*destruct)(void *), Member_func *, int(*checkpoint)(void **), Member_ret_obj_func *, Member_ret_str_func *)
Definition: hoc_oop.cpp:1560
void OcDeck_reg()
Definition: ocdeck.cpp:252
static Member_func members[]
Definition: ocdeck.cpp:233
static double move_last(void *v)
Definition: ocdeck.cpp:220
static void * cons(Object *)
Definition: ocdeck.cpp:75
double(* nrnpy_object_to_double_)(Object *)
Definition: xmenu.cpp:14
static void destruct(void *v)
Definition: ocdeck.cpp:89
static double unmap(void *v)
Definition: ocdeck.cpp:140
static double map(void *v)
Definition: ocdeck.cpp:114
static double save(void *v)
Definition: ocdeck.cpp:155
static double remove(void *v)
Definition: ocdeck.cpp:207
static double intercept(void *v)
Definition: ocdeck.cpp:102
static double remove_last(void *v)
Definition: ocdeck.cpp:196
static double flip_to(void *v)
Definition: ocdeck.cpp:181
#define g
Definition: passive0.cpp:21
#define e
Definition: passive0.cpp:22
o
Definition: seclist.cpp:175
#define cnt
Definition: spt2queue.cpp:19
#define NULL
Definition: sptree.h:16
Definition: hocdec.h:227