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