NEURON
mlinedit.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 extern int hoc_return_type_code;
4 
5 #include <stdio.h>
6 
7 #if HAVE_IV
8 #include <InterViews/iv3text.h>
9 #include <InterViews/layout.h>
10 #include <InterViews/background.h>
11 #include <InterViews/event.h>
12 #include <IV-look/kit.h>
13 #include "ocglyph.h"
14 #endif
15 
16 #include "classreg.h"
17 #if HAVE_IV
18 #include "oc2iv.h"
19 #include "apwindow.h"
20 #include "ivoc.h"
21 #endif
22 
23 #include "gui-redirect.h"
24 
25 extern Object** (*nrnpy_gui_helper_)(const char* name, Object* obj);
26 extern double (*nrnpy_object_to_double_)(Object*);
27 extern char** (*nrnpy_gui_helper3_str_)(const char* name, Object* obj, int handle_strptr);
28 
29 #if HAVE_IV
30 class OcText: public Text {
31  public:
32  OcText(unsigned rows = 24, unsigned cols = 80, TextBuffer* buf = NULL);
33  virtual ~OcText();
34  virtual void keystroke(const Event& event);
35 };
36 
37 class OcMLineEditor: public OcGlyph {
38  public:
39  OcMLineEditor(unsigned row, unsigned col, const char* buf = NULL);
40  virtual ~OcMLineEditor();
41 
42  public:
43  OcText* txt_;
44 };
45 #endif
46 
47 static double map(void* v) {
48  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("TextEditor.map", v);
49 #if HAVE_IV
50  IFGUI
51  OcMLineEditor* e = (OcMLineEditor*) v;
52  PrintableWindow* w;
53  if (ifarg(3)) {
54  w = e->make_window(float(*getarg(2)),
55  float(*getarg(3)),
56  float(*getarg(4)),
57  float(*getarg(5)));
58  } else {
59  w = e->make_window();
60  }
61  if (ifarg(1)) {
62  char* name = gargstr(1);
63  w->name(name);
64  }
65  w->map();
66  ENDGUI
67  return 0.;
68 #else
69  return 0.;
70 #endif
71 }
72 
73 static double readonly(void* v) {
74  TRY_GUI_REDIRECT_ACTUAL_DOUBLE("TextEditor.readonly", v);
75 #if HAVE_IV
76  IFGUI
77  OcMLineEditor* e = (OcMLineEditor*) v;
78  hoc_return_type_code = 2; // boolean
79  if (ifarg(1)) {
80  e->txt_->readOnly(int(chkarg(1, 0, 1)));
81  }
82  return double(e->txt_->readOnly());
83  ENDGUI
84 #endif
85  return 0.;
86 }
87 
88 static const char** v_text(void* v) {
89  TRY_GUI_REDIRECT_ACTUAL_STR("TextEditor.text", v);
90 #if HAVE_IV
91  IFGUI
92  OcMLineEditor* e = (OcMLineEditor*) v;
93  TextBuffer* tb = e->txt_->editBuffer();
94  if (ifarg(1)) {
95  e->txt_->reset();
96  const char* s = gargstr(1);
97  tb->Insert(0, s, strlen(s));
98  }
99  char** p = hoc_temp_charptr();
100  *p = (char*) tb->Text();
101  return (const char**) p;
102  ENDGUI
103 #endif
104  return 0;
105 }
106 
107 
108 static Member_func members[] = {"readonly", readonly, "map", map, 0, 0};
109 
110 static Member_ret_str_func retstr_members[] = {"text", v_text, 0, 0};
111 
112 static void* cons(Object*) {
113  TRY_GUI_REDIRECT_OBJ("TextEditor", NULL);
114 #if HAVE_IV
115  IFGUI
116  const char* buf = "";
117  unsigned row = 5;
118  unsigned col = 30;
119  if (ifarg(1)) {
120  buf = gargstr(1);
121  }
122  if (ifarg(2)) {
123  row = unsigned(chkarg(2, 1, 1000));
124  col = unsigned(chkarg(3, 1, 1000));
125  }
126  OcMLineEditor* e = new OcMLineEditor(row, col, buf);
127  e->ref();
128  return (void*) e;
129  ENDGUI
130 #endif /* HAVE_IV */
131  return nullptr;
132 }
133 
134 static void destruct(void* v) {
135  TRY_GUI_REDIRECT_NO_RETURN("~TextEditor", v);
136 #if HAVE_IV
137  IFGUI
138  OcMLineEditor* e = (OcMLineEditor*) v;
139  if (e->has_window()) {
140  e->window()->dismiss();
141  }
142  e->unref();
143  ENDGUI
144 #endif /* HAVE_IV */
145 }
146 
148  class2oc("TextEditor", cons, destruct, members, NULL, NULL, retstr_members);
149 }
150 
151 #if HAVE_IV
152 OcMLineEditor::OcMLineEditor(unsigned row, unsigned col, const char* buf) {
153  txt_ = new OcText(row, col, new TextBuffer(buf, strlen(buf), 1000));
154  txt_->ref();
155  body(new Background(txt_, WidgetKit::instance()->background()));
156 }
157 OcMLineEditor::~OcMLineEditor() {
158  txt_->unref();
159 }
160 
161 OcText::OcText(unsigned rows, unsigned cols, TextBuffer* buf)
162  : Text(rows, cols, buf) {}
163 
164 OcText::~OcText() {}
165 
166 void OcText::keystroke(const Event& e) {
167  if (readOnly_) {
168  return;
169  }
170  char buffer[8]; // needs to be dynamically adjusted
171  int count = e.mapkey(buffer, 8);
172  if (count <= 0) {
173  return;
174  }
175  Text::keystroke(e);
176 }
177 
178 #endif
#define TextBuffer
Definition: _defines.h:299
#define Background
Definition: _defines.h:43
#define Event
Definition: _defines.h:107
#define Text
Definition: _defines.h:297
virtual const char * name() const
virtual void map()
double chkarg(int, double low, double high)
Definition: code2.cpp:638
char buf[512]
Definition: init.cpp:13
char ** hoc_temp_charptr(void)
Definition: code.cpp:642
#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 TRY_GUI_REDIRECT_ACTUAL_STR(name, obj)
Definition: gui-redirect.h:82
#define IFGUI
Definition: hocdec.h:372
#define getarg
Definition: hocdec.h:15
#define gargstr
Definition: hocdec.h:14
#define ENDGUI
Definition: hocdec.h:373
int ifarg(int)
Definition: code.cpp:1581
#define v
Definition: md1redef.h:4
static double readonly(void *v)
Definition: mlinedit.cpp:73
static Member_ret_str_func retstr_members[]
Definition: mlinedit.cpp:110
static Member_func members[]
Definition: mlinedit.cpp:108
static void * cons(Object *)
Definition: mlinedit.cpp:112
double(* nrnpy_object_to_double_)(Object *)
Definition: xmenu.cpp:14
static void destruct(void *v)
Definition: mlinedit.cpp:134
static double map(void *v)
Definition: mlinedit.cpp:47
void TextEditor_reg()
Definition: mlinedit.cpp:147
static const char ** v_text(void *v)
Definition: mlinedit.cpp:88
int hoc_return_type_code
Definition: code.cpp:42
char * name
Definition: init.cpp:16
static unsigned row
Definition: nonlin.cpp:85
size_t p
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
#define e
Definition: passive0.cpp:22
#define NULL
Definition: sptree.h:16
Definition: hocdec.h:227