#include "testApp.h" //-------------------------------------------------------------- /* alternative - just have a horizontal scroll bar (contextual) maybe draw into an FBO in order to put a window in a window method to retreive text as a single string with \n's set box size - draw scrollbar & set scrollpoint - drag to scroll load & save architecture :- text editor in a box or a window? 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; } 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; } 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(){ 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::iterator i; string t; int l; unsigned char *c; switch (key) { case OF_KEY_DEL: if (selected) deleteSelection(); else { if (insertionPoint.column0) 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.columninsertionPoint.column) { insertionPoint.row++; insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size()); } } break; case OF_KEY_DOWN: if (insertionPoint.rowgetClipboard(); if (c>0) insertText(string(reinterpret_cast(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){ 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); 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 lines; int l=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]; 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;iselectionStart.row) { vector::iterator i=text.begin()+selectionStart.row+1; vector::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;isetup(); } //-------------------------------------------------------------- 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){ }