bool TextBuffer::indentToColumn(int col) { // reindent current line so that there are col white spaces before // the first non-white char in line int bol=pointBOL(); int top=bol+currentIndent(); // current pos of first non-white or eol int loc=bol+col; // goal pos for indentation setCaretVisible(false); setScrollToShowCursor(false); if (loc < top) { //std::cout << "deleting " << top-loc << " spaces.\n"; setPoint(bol); setHighlightedRegion(bol, (top-loc)); insertTextAtCursor(String::empty); } else if (loc > top) { setPoint(bol); String pad=String::empty; for (int i=top; i<loc; i++) pad << T(" "); //std::cout << "inserting " << pad.length() << " spaces.\n"; insertTextAtCursor(pad); } setPoint(loc); setCaretVisible(true); setScrollToShowCursor(true); return (loc!=top); }
void TextBuffer::changeCase(int flag) { // change case of word at point. 0=lower,1=upper,2=capitalize int beg=point(); forwardWord(); int end=point(); String text=getTextSubstring(beg, end); if (text==String::empty) return; deleteRegion(beg,end); if (flag==0) insertTextAtCursor(text.toLowerCase()); else if (flag==1) insertTextAtCursor(text.toUpperCase()); else if (flag==2) { int len=text.length(); // get first alphachar int loc=skip_syntax(syntax->syntab, text, T("^w"), 0, len); insertTextAtCursor(text.substring(0,loc)); if (loc<len) { insertTextAtCursor(text.substring(loc,loc+1).toUpperCase()); insertTextAtCursor(text.substring(loc+1,len)); } } beg=point(); //colorizeAfterChange(CommandIDs::EditorIndent); // recolorize whole line for now.. setPoint(beg); setFlag(EditFlags::NeedsSave); }
void TextBuffer::openLine() { // C-o new line does not change point int here=point(); insertTextAtCursor(T("\n")); setPoint(here); setFlag(EditFlags::NeedsSave); }
bool TextBuffer::replace(String rep) { if (!(getHighlightedRegionLength()>0)) return false; insertTextAtCursor(rep); setFlag(EditFlags::NeedsSave); return true; }
void TextBuffer::deleteRegion(int from, int to) { // delete chars between from and to (exclusive), do not update // clipboard if (to>from) { setHighlightedRegion(from, to-from); insertTextAtCursor(String::empty); } }
void TextEdit::setText(const char *new_text) { if (!new_text) { clear(); return; } // XXX should the text be validated (FLAG_*)? size_t size = strlen(new_text); initBuffer(size + GAP_SIZE_EXPAND); insertTextAtCursor(new_text, size); }
void TextBuffer::paste() { String clip=SystemClipboard::getTextFromClipboard(); if (clip.containsChar('\r')) { #ifdef MACOSX clip=clip.replaceCharacter('\r', '\n'); #else String temp=String::empty; for (int i=0; i<clip.length(); i++) if (clip[i] != '\r' ) temp << clip[i]; clip=temp; #endif } insertTextAtCursor(clip); setFlag(EditFlags::NeedsSave); }
//Key press event: Does most of the things in this editor: void CodeEditor::keyPressEvent(QKeyEvent *e) { e->accept(); /* Code Editor features: • Code completion • Auto Indentation • Autoclose for () [] {} " ' and comments */ //First: The keyboard controlling Qt::KeyboardModifiers mods = e->modifiers(); bool Shift = mods.testFlag(Qt::ShiftModifier); bool Ctrl = mods.testFlag(Qt::ControlModifier); // COMMAND key on Mac OS bool Alt = mods.testFlag(Qt::AltModifier); switch(e->key()) { //Movement & selection case Qt::Key_Right: moveRight(Ctrl,Shift); break; case Qt::Key_Left: moveLeft(Ctrl,Shift); break; case Qt::Key_Up: moveUp(Shift); break; case Qt::Key_Down: moveDown(Shift); break; case Qt::Key_Home: if(Ctrl)moveToBeginOfDocument(Shift); else moveToLineBegin(Shift); break; case Qt::Key_End: if(Ctrl)moveToEndOfDocument(Shift); else moveToLineEnd(Shift); break; //Deleting and clearing case Qt::Key_Backspace: deleteLeft(Ctrl); break; case Qt::Key_Delete: deleteRight(Ctrl); break; //Misc case Qt::Key_Escape: //Close the completer if it's shown break; case Qt::Key_Enter: case Qt::Key_Return: newLineAtCursor(); break; //Function keys case Qt::Key_Tab: insertTextAtCursor("\t"); break; case Qt::Key_Insert: if(Shift)paste(); else if(Ctrl)copySelection(); else insertMode=!insertMode; break; case Qt::Key_F3: //Search & replace functionality TODO: not important break; case Qt::Key_F1: case Qt::Key_F2: case Qt::Key_F4: case Qt::Key_F5: case Qt::Key_F6: case Qt::Key_F7: case Qt::Key_F8: case Qt::Key_F9: case Qt::Key_F10: case Qt::Key_F11: case Qt::Key_F12: case Qt::Key_F13: case Qt::Key_F14: case Qt::Key_F15: case Qt::Key_F16: case Qt::Key_F17: case Qt::Key_F18: case Qt::Key_F19: case Qt::Key_F20: case Qt::Key_F21: case Qt::Key_F22: case Qt::Key_F23: case Qt::Key_F24: //Just NO-OP for now! break; //NO-OPs: case Qt::Key_CapsLock: case Qt::Key_NumLock: case Qt::Key_ScrollLock: case Qt::Key_Pause: case Qt::Key_Print: case Qt::Key_SysReq: case Qt::Key_Control: case Qt::Key_Alt: case Qt::Key_AltGr: case Qt::Key_Super_L: case Qt::Key_Super_R: case Qt::Key_Meta: break; default: if(e->key()==Qt::Key_V && Ctrl)paste(); else if(e->key()==Qt::Key_C && Ctrl)copySelection(); else if(e->key()==Qt::Key_X && Ctrl){copySelection();clearSelection();} else if(e->key()==Qt::Key_Z && Ctrl)undo(); else if(e->key()==Qt::Key_Y && Ctrl)redo(); else if(e->key()==Qt::Key_A && Ctrl)selectAll(); //Type the character: else if(!Ctrl)insertTextAtCursor(e->text()); } cursorVisible=true; viewport()->repaint(); }
void CodeEditor::paste() { insertTextAtCursor(qApp->clipboard()->text()); updateCursorXPos(); updateHorizontalScrollBarPos(); }
void CodeEditor::newLineAtCursor() { insertTextAtCursor("\n"); }
void TextBuffer::colorize (int from, int to, bool force) { // JUCE LACAUNEA: (1) recoloring should only happen in the visible // region of buffer but i dont see anyway to get this information // from the viewpoint. (2) In order to recolor I have to delete and // add the text back into the buffer // this is needed because coloring changes the text buffer and we // dont want the editor's change callback to call the colorizer for // these changes. // setFlag(EditFlags::Coloring); String text = getTextSubstring(from, to); int len = text.length(); int here = point(), pos = 0, start, end; String expr; scanresult typ; HiliteID hilite; Colour color, normal=syntax->hilites[HiliteIDs::None]; static KeyPress dkey = KeyPress(KeyPress::deleteKey); // offset is the starting position of text string in buffer. setCaretVisible(false); setScrollToShowCursor(false); //printf("hiliting %d to %d...\n", from, to); while (pos < len) { typ=parse_sexpr(syntax->syntab, text, -1, len, 1, SCAN_COLOR, &pos, &start, &end); hilite=HiliteIDs::None; if (typ>0) { if (typ==SCAN_TOKEN) { hilite=syntax->getHilite(text, start, end); } else if (typ==SCAN_COMMENT) { hilite=HiliteIDs::Comment; } else if (typ==SCAN_STRING) { hilite=HiliteIDs::String; } if ((hilite>HiliteIDs::None) || force) { // this is REALLY GROSS! expr=text.substring(start,end); color=syntax->hilites[hilite]; setPoint(from+start); setHighlightedRegion(from+start, end-start); TextEditor::keyPressed(dkey); setColour(TextEditor::textColourId, color); insertTextAtCursor(expr); setColour(TextEditor::textColourId, normal); /** color=syntax->hilites[hilite]; setColour(TextEditor::textColourId, color); TextEditor::repaintText(from+start, end-start); setColour(TextEditor::textColourId, normal); **/ } } } setPoint(here); setCaretVisible(true); setScrollToShowCursor(true); // clearFlag(EditFlags::Coloring); }
void TextEdit::insertTextAtCursor(const char *new_text) { g_assert(new_text); insertTextAtCursor(new_text, strlen(new_text)); }