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