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