From 25fc9816fa59f0ddf14b298988ff35bd1749fe61 Mon Sep 17 00:00:00 2001 From: Comment Date: Thu, 3 Jan 2013 14:13:15 +0000 Subject: cut+paste fixed --- src/testApp.cpp | 158 ++++++++++++++++++++++++++++++++++++-------------------- src/testApp.h | 12 +++-- 2 files changed, 109 insertions(+), 61 deletions(-) (limited to 'src') 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::iterator i; string t; int l; + unsigned char *c; switch (key) { case OF_KEY_DEL: if (selected) deleteSelection(); else { - if (insertionPoint.column0) { 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(ofxFensterManager::get()->getClipboard()))); + c=ofxFensterManager::get()->getClipboard(); + if (c>0) insertText(string(reinterpret_cast(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 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::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;jinsertionPoint.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; + int insX,insY; texPt insertionPoint; texPt selectionStart; texPt selectionEnd; bool selected,changed; string output; - int clickX,clickY; }; -- cgit v1.2.3