summaryrefslogtreecommitdiff
path: root/src/testApp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/testApp.cpp')
-rwxr-xr-xsrc/testApp.cpp158
1 files changed, 101 insertions, 57 deletions
diff --git a/src/testApp.cpp b/src/testApp.cpp
index df59d6e..babc8e7 100755
--- a/src/testApp.cpp
+++ b/src/testApp.cpp
@@ -2,14 +2,6 @@
//--------------------------------------------------------------
/*
-text editor functionality:
-
-keep text as 1 string? or as a vector of strings?
-prob faster to use a vector of strings for display
-makes it more awkward for selections? keep track of which line/char to start and end
-
-alternative - go through text and seperate into lines either by width or by /n every time it changes
-keep a display copy with /n's inserted?
alternative - just have a horizontal scroll bar (contextual)
maybe draw into an FBO in order to put a window in a window
@@ -26,11 +18,9 @@ insertion point/ selection - set with mouse -
get and set clipboard (ofxFensterManager.h)
-
vector insert a problem performance wise?
you need to use an iterator to insert into a vector anyway- may as well use a list
-
*/
editorWindow::~editorWindow(){
cout << "editor window destroyed" << endl;
@@ -44,55 +34,74 @@ void editorWindow::setup(){
changed=false;
text.push_back(string(""));
output="";
+ windowResized(400,400);
+ insX=2;insY=2;
+}
+void editorWindow::windowResized(int _w, int _h){
+ w=_w;h=_h;
+ screen.allocate(w-insX-4,h-insY-4);
+ printf("editor window resized to %ix%i\n",w,h);
+ changed=true;
}
void editorWindow::draw(){
- ofDisableAlphaBlending();
- ofSetColor(255,255,255);
- for (int i=0;i<text.size();i++) {
- ofDrawBitmapString(text[i],10,15+(i*12));
- }
- if (selected) {
- ofEnableAlphaBlending();
- ofSetColor(0,128,255,160);
- //draw selection
- if (selectionStart.row==selectionEnd.row) {
- ofRect((selectionStart.column*8)+10,(selectionStart.row*12)+5,(selectionEnd.column-selectionStart.column)*8,12);
- }
- else {
- ofRect((selectionStart.column*8)+10,(selectionStart.row*12)+5,(text[selectionStart.row].size()-selectionStart.column)*8,12);
- for (int i=selectionStart.row+1;i<selectionEnd.row;i++) {
- ofRect(10,(i*12)+5,(text[i].size())*8,12);
- }
- ofRect(10,(selectionEnd.row*12)+5,(selectionEnd.column)*8,12);
- }
- ofDisableAlphaBlending();
- }
- else {
- //draw insertion point
- if ((ofGetElapsedTimeMillis()/300)%2) {
- ofRect((insertionPoint.column*8)+10,(insertionPoint.row*12)+5,2,10);
- }
- }
-
+ if (w!=ofGetWidth()||h!=ofGetHeight()) windowResized(ofGetWidth(),ofGetHeight());
+ 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);
+ }
+ }
+}
+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);
+ //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);
- }
+ 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:
@@ -127,7 +136,7 @@ void editorWindow::keyPressed(int key){
insertionPoint.column=text[insertionPoint.row].size();
}
break;
- case OF_KEY_UP:
+ case OF_KEY_UP:
if (insertionPoint.row>0) {
insertionPoint.row--;
insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
@@ -159,10 +168,12 @@ void editorWindow::keyPressed(int key){
break;
case 22: //ctrl-v
deleteSelection();
- insertText(string(reinterpret_cast<const char *>(ofxFensterManager::get()->getClipboard())));
+ c=ofxFensterManager::get()->getClipboard();
+ if (c>0) insertText(string(reinterpret_cast<const char *>(c)));
break;
default:
- if (key>-1 && key<256) {
+ 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++;
@@ -176,11 +187,13 @@ void editorWindow::mousePressed(int x, int y, int button){
selected=false;
clickX=x;
clickY=y;
+ changed=true;
}
void editorWindow::mouseReleased(int x, int y, int button){
if (clickX=x&&clickY==y) {
insertionPoint=clickPos(x,y);
}
+ changed=true;
}
void editorWindow::mouseDragged(int x, int y, int button){
texPt c1=clickPos(clickX,clickY);
@@ -194,19 +207,46 @@ void editorWindow::mouseDragged(int x, int y, int button){
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-5)/12));
- t.column=max(0,min((int)text[t.row].size(),(x-10)/8));
+ 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 text){
+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(text, line)) {
- //if (line==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];
@@ -224,8 +264,12 @@ string editorWindow::getSelection(){
t+="\n";
t+=text[i];
}
- t+=text[selectionEnd.row].substr(0,selectionEnd.column);
+ //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(){