summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorComment <tim@gray.(none)>2012-12-30 10:45:58 +0000
committerComment <tim@gray.(none)>2012-12-30 10:45:58 +0000
commit8bc88cb5db84219a5ad4d1c49239607113ec85fa (patch)
tree1e65d541b717dab4a6f2ff7ecb34b298fefbdf1e /src
parentf855d3ba224c878dae5ef02688b7c0b8630b1348 (diff)
nearly working
Diffstat (limited to 'src')
-rwxr-xr-xsrc/testApp.cpp260
-rwxr-xr-xsrc/testApp.h18
2 files changed, 171 insertions, 107 deletions
diff --git a/src/testApp.cpp b/src/testApp.cpp
index 0cffe26..df59d6e 100755
--- a/src/testApp.cpp
+++ b/src/testApp.cpp
@@ -18,6 +18,8 @@ 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 -
@@ -39,7 +41,9 @@ void editorWindow::setup(){
win->setWindowTitle("editor");
win->addListener(this);
selected=false;
+ changed=false;
text.push_back(string(""));
+ output="";
}
void editorWindow::draw(){
ofDisableAlphaBlending();
@@ -47,17 +51,6 @@ void editorWindow::draw(){
for (int i=0;i<text.size();i++) {
ofDrawBitmapString(text[i],10,15+(i*12));
}
- //draw blinking cursor
- /*
- not trivial if we deal with 1 string with /ns in
- we have to scan the file for /ns every time we display the blinking cursor
- is easier with vectors?
- the difficulty here is that its messy to remember row/column
- can we even get the height of a string formatted block
- shorter strings make inserting quicker
- could have a single string and maintain a vector representing line breaks- however inserting a char meansn having to manipulate the entire vector
- with a vector of strings and then row/column insertion point its prob better
- */
if (selected) {
ofEnableAlphaBlending();
ofSetColor(0,128,255,160);
@@ -65,15 +58,13 @@ void editorWindow::draw(){
if (selectionStart.row==selectionEnd.row) {
ofRect((selectionStart.column*8)+10,(selectionStart.row*12)+5,(selectionEnd.column-selectionStart.column)*8,12);
}
- else if (selectionStart.row<selectionEnd.row) {
+ 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);
}
- else if (selectionStart.row>selectionEnd.row) {
- }
ofDisableAlphaBlending();
}
else {
@@ -84,98 +75,105 @@ void editorWindow::draw(){
}
}
-/*
- #define OF_KEY_LEFT (100 | OF_KEY_MODIFIER)
- #define OF_KEY_UP (101 | OF_KEY_MODIFIER)
- #define OF_KEY_RIGHT (102 | OF_KEY_MODIFIER)
- #define OF_KEY_DOWN (103 | OF_KEY_MODIFIER)
- #define OF_KEY_PAGE_UP (104 | OF_KEY_MODIFIER)
- #define OF_KEY_PAGE_DOWN (105 | OF_KEY_MODIFIER)
- #define OF_KEY_HOME (106 | OF_KEY_MODIFIER)
- #define OF_KEY_END (107 | OF_KEY_MODIFIER)
- #define OF_KEY_INSERT
-*/
void editorWindow::keyPressed(int key){
- //printf("%i\n",key);
+ printf("%i\n",key);
+ changed=true;
vector<string>::iterator i;
string t;
int l;
- switch (key) {
- case OF_KEY_DEL:
- if (insertionPoint.column<text[insertionPoint.row].size()) {
- text[insertionPoint.row].erase(insertionPoint.column,1);
- }
- else if (insertionPoint.row<text.size()-1) {
- text[insertionPoint.row]+=text[insertionPoint.row+1];
- for (i=text.begin(),l=0;l<=insertionPoint.row;i++,l++) {}
- text.erase(i);
- }
- break;
- case OF_KEY_BACKSPACE:
- 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];
- for (i=text.begin(),l=0;l<=insertionPoint.row;i++,l++) {}
- text.erase(i);
- insertionPoint.row--;
- }
- break;
- case OF_KEY_RETURN:
- for (i=text.begin(),l=0;l<=insertionPoint.row;i++,l++) {}
- t=text[insertionPoint.row].substr(insertionPoint.column);
- text[insertionPoint.row].erase(insertionPoint.column);
- text.insert(i,t);
- insertionPoint.row++;
- insertionPoint.column=0;
- break;
- case 267: //OF_KEY_LEFT in fenster
- insertionPoint.column--;
- if (insertionPoint.column<0) {
- if (insertionPoint.row>0) {
- insertionPoint.row--;
- insertionPoint.column=text[insertionPoint.row].size();
- }
- else insertionPoint.column=0;
- }
- break;
- case 269: //OF_KEY_UP in fenster
- if (insertionPoint.row>0) {
- insertionPoint.row--;
- insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
- }
- break;
- case 268: //OF_KEY_RIGHT in fenster
- insertionPoint.column++;
- if (insertionPoint.column>text[insertionPoint.row].size()) {
- if (text.size()>insertionPoint.column) {
- insertionPoint.row++;
- insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
- }
- else insertionPoint.column--;
- }
- break;
- case 270: //OF_KEY_DOWN in fenster
- if (insertionPoint.row<text.size()) {
- insertionPoint.row++;
- insertionPoint.column=min(insertionPoint.column,(int)text[insertionPoint.row].size());
- }
- break;
- default:
- if (key<256) {
- text[insertionPoint.row].insert(insertionPoint.column,1,(char)key);
- insertionPoint.column++;
- }
+ 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();
+ insertText(string(reinterpret_cast<const char *>(ofxFensterManager::get()->getClipboard())));
+ break;
+ default:
+ if (key>-1 && key<256) {
+ 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;
+ selected=false;
clickX=x;
clickY=y;
}
@@ -185,16 +183,76 @@ void editorWindow::mouseReleased(int x, int y, int button){
}
}
void editorWindow::mouseDragged(int x, int y, int button){
- selected=true;
- selectionStart=clickPos(clickX,clickY);
- selectionEnd=clickPos(x,y);
+ 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;
}
texPt editorWindow::clickPos(int x,int y) {
texPt t;
- t.row=min((int)text.size()-1,(y-5)/12);
- t.column=min((int)text[insertionPoint.row].size(),(x-10)/8);
+ 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));
return t;
}
+void editorWindow::insertText(string text){
+ string line;
+ int l=0;
+ //while(std::getline(text, line)) {
+ //if (line==0)
+ //}
+}
+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];
+ }
+ t+=text[selectionEnd.row].substr(0,selectionEnd.column);
+ }
+ 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(){
diff --git a/src/testApp.h b/src/testApp.h
index dde4502..52cd191 100755
--- a/src/testApp.h
+++ b/src/testApp.h
@@ -39,15 +39,21 @@ class editorWindow: public ofxFensterListener{
void keyPressed(int key);
void keyReleased(int key);
void mousePressed(int x, int y, int button);
- void mouseReleased(int x, int y, int button);
- void mouseDragged(int x, int y, int button);
- texPt clickPos(int x,int y);
+ void mouseReleased(int x, int y, int button);
+ void mouseDragged(int x, int y, int button);
+ string getText();
private:
- vector<string> text;
+ texPt clickPos(int x,int y);
+ void setClipboard(string text);
+ string getSelection();
+ void insertText(string text);
+ void deleteSelection();
+ vector<string> text;
texPt insertionPoint;
texPt selectionStart;
- texPt selectionEnd;
- bool selected;
+ texPt selectionEnd;
+ bool selected,changed;
+ string output;
int clickX,clickY;
};