1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
#include "testApp.h"
//--------------------------------------------------------------
editorWindow::~editorWindow(){
cout << "editor window destroyed" << endl;
}
void editorWindow::setup(){
ofxFenster* win=ofxFensterManager::get()->createFenster(0, 0, 400, 400, OF_WINDOW);
win->setWindowTitle("editor");
win->addListener(this);
selected=false;
changed=false;
text.push_back(string(""));
output="";
windowResized(400,400);
insX=2;insY=2;
scrollY=false;scrollX=false;
scrollPosY=0;scrollPosX=0;
scrollSelY=scrollSelX=false;
}
void editorWindow::screenResize(){
w=ofGetWidth();h=ofGetHeight();
screen.allocate(w-(insX*2)-(scrollY?15:0),h-(insY*2)-(scrollX?15:0));
printf("editor window resized to %ix%i\n",w,h);
changed=true;
}
void editorWindow::draw(){
bool sY=(((text.size()*12)+(insY*2))>h);
if (sY!=scrollY||w!=ofGetWidth()||h!=ofGetHeight()) {
scrollY=sY;
screenResize();
}
if (changed) drawScreen();
ofSetColor(255,255,255);
screen.draw(insX,insY);
if (!selected) {
//draw insertion point
if ((ofGetElapsedTimeMillis()/300)%2) {
ofRect((insertionPoint.column*8)+insX,(insertionPoint.row*12)+insY,2,10);
}
}
if (scrollY) {
if (scrollX) {
ofLine(0,h-15,w-15,h-15);
ofLine(w-15,0,w-15,h-15);
}
else ofLine(w-15,0,w-15,h);
}
else if (scrollX) ofLine(0,h-15,w,h-15);
if (scrollY) {
float yBarSize=(((float)h-(insX*2)-15)/(text.size()*12))*((float)h-(insX*2)-15);
float yBarPos=2; //+ actual scroll amount
if (scrollSelY) ofFill();
else ofNoFill();
ofRect(w-12,yBarPos,11,yBarSize);
ofFill();
}
}
void editorWindow::drawScreen(){
screen.begin();
ofBackground(0);
ofDisableAlphaBlending();
ofSetColor(255,255,255);
for (int i=0;i<text.size();i++) {
//handle tabs - this could be done in ofGLRenderer.cpp - void drawString
ofDrawBitmapString(text[i],0,10+(i*12));
}
if (selected) {
ofEnableAlphaBlending();
ofSetColor(0,128,255,160);
//draw selection
if (selectionStart.row==selectionEnd.row) {
ofRect((selectionStart.column*8)+0,(selectionStart.row*12)+0,(selectionEnd.column-selectionStart.column)*8,12);
}
else {
ofRect((selectionStart.column*8)+0,(selectionStart.row*12)+0,(text[selectionStart.row].size()-selectionStart.column)*8,12);
for (int i=selectionStart.row+1;i<selectionEnd.row;i++) {
ofRect(0,(i*12)+0,(text[i].size())*8,12);
}
ofRect(0,(selectionEnd.row*12)+0,(selectionEnd.column)*8,12);
}
ofDisableAlphaBlending();
}
screen.end();
changed=false;
}
void editorWindow::keyPressed(int key){
//printf("%i\n",key);
changed=true;
vector<string>::iterator i;
string t;
int l;
unsigned char *c;
switch (key) {
case OF_KEY_DEL:
if (selected) deleteSelection();
else {
if (insertionPoint.column<text[insertionPoint.row].size()-1) {
text[insertionPoint.row].erase(insertionPoint.column,1);
}
else if (insertionPoint.row<text.size()-1) {
text[insertionPoint.row]+=text[insertionPoint.row+1];
i=text.begin()+insertionPoint.row;
text.erase(i);
}
}
break;
case OF_KEY_BACKSPACE:
if (selected) deleteSelection();
else {
if (insertionPoint.column) {
text[insertionPoint.row].erase(insertionPoint.column-1,1);
insertionPoint.column--;
}
else if (insertionPoint.row) {
insertionPoint.column=text[insertionPoint.row-1].size();
text[insertionPoint.row-1]+=text[insertionPoint.row];
i=text.begin()+insertionPoint.row;
text.erase(i);
insertionPoint.row--;
}
}
break;
case OF_KEY_RETURN:
deleteSelection();
i=text.begin()+insertionPoint.row+1;
t=text[insertionPoint.row].substr(insertionPoint.column);
text[insertionPoint.row].erase(insertionPoint.column);
text.insert(i,t);
insertionPoint.row++;
insertionPoint.column=0;
break;
case OF_KEY_LEFT:
if (insertionPoint.column>0) insertionPoint.column--;
else if (insertionPoint.row>0) {
insertionPoint.row--;
insertionPoint.column=text[insertionPoint.row].size();
}
break;
case OF_KEY_UP:
if (insertionPoint.row>0) {
insertionPoint.row--;
insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
}
break;
case OF_KEY_RIGHT:
if (insertionPoint.column<text[insertionPoint.row].size()) {
insertionPoint.column++;
}
else {
if (text.size()>insertionPoint.column) {
insertionPoint.row++;
insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
}
}
break;
case OF_KEY_DOWN:
if (insertionPoint.row<text.size()-1) {
insertionPoint.row++;
insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
}
break;
case 24: //ctrl-x
setClipboard(getSelection());
deleteSelection();
break;
case 3: //ctrl-c
setClipboard(getSelection());
break;
case 22: //ctrl-v
deleteSelection();
c=ofxFensterManager::get()->getClipboard();
if (c>0) insertText(string(reinterpret_cast<const char *>(c)));
break;
default:
if (key==9) key=32; //convert tab to space for simplicity
if (key>31 && key<127) {
deleteSelection();
text[insertionPoint.row].insert(insertionPoint.column,1,(char)key);
insertionPoint.column++;
}
}
}
void editorWindow::keyReleased(int key){
//key repeat?
}
void editorWindow::mousePressed(int x, int y, int button){
clickX=x;
clickY=y;
if (scrollY&&x>(h-16)) {
scrollSelY=true;
}
else {
selected=false;
changed=true;
}
}
void editorWindow::mouseReleased(int x, int y, int button){
if (scrollSelY) {
scrollSelY=false;
}
else {
if (clickX=x&&clickY==y) {
insertionPoint=clickPos(x,y);
}
changed=true;
}
}
void editorWindow::mouseDragged(int x, int y, int button){
if (scrollSelY) {
}
else {
texPt c1=clickPos(clickX,clickY);
texPt c2=clickPos(x,y);
if ((c1.row==c2.row&&c2.column>c1.column)||c2.row>c1.row) {
selectionStart=c1;
selectionEnd=c2;
}
else {
selectionStart=c2;
selectionEnd=c1;
}
if (selectionStart.row!=selectionEnd.row||selectionStart.column!=selectionEnd.column) selected=true;
changed=true;
}
}
texPt editorWindow::clickPos(int x,int y) {
texPt t;
t.row=max(0,min((int)text.size()-1,(y-insY)/12));
t.column=max(0,min((int)text[t.row].size(),(x-insX)/8));
return t;
}
void editorWindow::insertText(string t){
// printf("%s\n",t.c_str());
for (int i=0;i<t.size();i++) if (t[i]==9) t[i]=32; //convert tab to space
istringstream in(t);
string line;
vector<string> lines;
int l=0;
while(std::getline(in, line)) {
lines.push_back(line);
}
//for (int i=0;i<lines.size();i++) printf("%i %s\n",i,lines[i].c_str());
if (lines.size()==1) {
text[insertionPoint.row].insert(insertionPoint.column,lines[0]);
insertionPoint.column+=lines[0].size();
}
else {
string t1=text[insertionPoint.row].substr(insertionPoint.column);
vector<string>::iterator i=text.begin()+insertionPoint.row+1;
text[insertionPoint.row].erase(insertionPoint.column);
text[insertionPoint.row].insert(insertionPoint.column,lines[0]);
for (int j=1;j<lines.size();j++) {
//printf("about to insert %s after '%s'\n",lines[j].c_str(),text[insertionPoint.row+j-1].c_str());
if (text.size()>insertionPoint.row+j) text.insert(i,lines[j]); //crashes on last iter?
else text.push_back(lines[j]);
i++;
}
insertionPoint.row+=lines.size()-1;
if (text.size()>insertionPoint.column) {
insertionPoint.column=text[insertionPoint.row].size();
text[insertionPoint.row].append(t1);
}
}
}
void editorWindow::setClipboard(string text){
char * cstr = new char [text.size()+1];
strcpy (cstr, text.c_str());
ofxFensterManager::get()->setClipboard(cstr);
delete[] cstr;
}
string editorWindow::getSelection(){
string t=text[selectionStart.row].substr(selectionStart.column);
if (selectionStart.row==selectionEnd.row) {
t.erase(selectionEnd.column-selectionStart.column);
}
else {
for (int i=selectionStart.row+1;i<selectionEnd.row;i++) {
t+="\n";
t+=text[i];
}
//if (selectionEnd.column<text[selectionEnd.row].size()-1) {
t+="\n";
t+=text[selectionEnd.row].substr(0,selectionEnd.column);
//}
}
//printf("selection:\n%s\n----------\n",t.c_str());
return t;
}
void editorWindow::deleteSelection(){
if (selected){
string t=text[selectionEnd.row].substr(selectionEnd.column);
text[selectionStart.row].erase(selectionStart.column);
text[selectionStart.row]+=t;
if (selectionEnd.row>selectionStart.row) {
vector<string>::iterator i=text.begin()+selectionStart.row+1;
vector<string>::iterator j=text.begin()+selectionEnd.row+1;
text.erase(i,j);
}
insertionPoint=selectionStart;
selected=false;
}
}
string editorWindow::getText(){
if (changed) {
output =="";
for (int i=0;i<text.size();i++) {
output +=text[i];
output +="\n";
}
changed=false;
}
return output;
}
//--------------------------------------------------------------
void testApp::setup(){
ofBackground(0);
ofSetColor(255);
editorWin=new editorWindow();
editorWin->setup();
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}
|