Thread: Writing a text editor

  1. #61
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by behzad_shabani View Post
    thanks (however I'm losing std::getline instead)
    another question, (I know I am pert ) I'm using pdcurses to handle the cursor, but how could I move cursor to previous line?
    when i press key up it bring the last line that i wrote.
    OK I used curses when I did mine, I asume it was the same/similar.
    Anyway I dug out the old code and typed it in (hope there are no typing errors!)
    CWIN is a pointer to a window I believe.
    I noticed I used a function 'fix' maybe it was to fix the problem you are encountering?
    I am guessing really. But none of the other curses routines needed it.

    Actually as I used mutliple windows CWIN is actually one of an array
    or window pointers, it's probably just a window pointer in yours?
    I also had to maintain an array of the data in each window (bd[wn]) wn=window number.

    It's also posible you need to set the value in the following functions.

    leaveok(stdscr,TRUE);
    scrollok(stdscr,FALSE);

    However I do not know what your approach to it was so it might be nothing like mine.

    Might be easier if you just look at how I used the cursors functions, rather than the code
    to maintain my arrays of data.

    Dunno if this will be of any help or not.


    Code:
    struct wid **bd // array of structures of window data, position of window, of cursor row, column etc.... and data in each window
    #defne OCOL bd[wn]->offcol // an offset for the column, used when some coulmns have 'scrolled' off the screen.
    #defne WROW bd[wn]->rowpos //screen row
    #defne WCOL bd[wn]->colpos //screen column
    #define CWIN winarray[wn] //current window shorthand
    WINDOW **winarray //array of window pointers
    cur_up(){
    int x,y;
    
    getxy(CWIN,x,y);
    if (y<1){ y=1;
                  OROW--;
                  scroll_win(NO_SCROLL);
                  x=1;
    else { WROW--1;}
    wmove(CWIN,x,y);
    fix();
    }
    
    fix(){
    int x,y;
    getxy((CWIN,x,y);
    wmove(CWIN,x,y);
    wrefresh(CWIN);
    WROW=x;
    WCOL=y;
    }
    Note the scroll_win was a function I wrote to take care of scrolling you can ignore it for now, it only
    is needed when you reach the top of the window, which is not the case yet I believe.
    Last edited by esbo; 01-24-2009 at 12:17 AM.

  2. #62
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Actually looking back at your posts I think you are going the wrong way about it, well if it is going to work anything like mine. Basically I turned all the terminal functions off and did them my self. ('raw more').

    Code:
    initscr();
    cbreak();
    noecho();
    nonl();
    So nothing is echoed back to the screen, you just read each key pressed and process it as appropiate so you can't use getline or whatever.

    Well maybe you can but I think you will run into problems, I think you will end up with you wanting to do one thing and the terminal trying to do another.
    Maybe they is the problem with the cursor up??

    Basically I read a keystroke and put the character on the screen (and into my array of data) as appropiate, if it is the up arrow I move
    the cursor up and move up a line in my array of data.
    CArridge return for example moves the cursor down a line and to the start of the next line, and I updata my data indexes as appropiate.
    It is more work (but not that much) but you end up with a much better editor. I mean getline() is just repeated calls to get character/keystroke.
    I recommmed you try it my way or you will end up with a crap editor (like this one!!).
    You see I ended up with multiple resizeable and moveable windows.
    Plus I could save my array of structures on exit and reload it on start and every thing is back as you left it, right down to window size,
    position and cursor positions. The only thing it could not do was write the code for you
    Anyway good luck.
    Last edited by esbo; 01-24-2009 at 12:24 AM.

  3. #63
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I'm using pdcurses to handle the cursor, but how could I move cursor to previous line?
    What classes do you have in your program so far?

    Look at post #40 - that's an editor class.
    Input and display are completely separate problems.

    For example, this could be a main() for your tests
    Code:
    int main ( ) {
      myEditor e;
      e.new();
      e.appendLine("Line 1");
      e.appendLine("Line 3");
      e.insertLine(2,"Line 2");
      // and so on with delete, find, search, replace
      e.save("test.txt");
    }
    When you have an API for your editor which works, then you can choose what kind of user interface to wrap around it. But you'll have the foundation of an editor which can load and save things, and do things to the file when it's been loaded.

    Embedding lots of pdcurses stuff inside your editor class is NOT the way to go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #64
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    As curses works on Unix, Linux, Msdos, and Apple Mac it does not seem to be that bad a way to go to me, anyway it seems to me he has chosen a 'API' for his editor, and it is curses!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  2. dtextp: a codeforming text editor
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-18-2007, 07:11 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Text editor with font color
    By KingoftheWorld in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-15-2003, 01:45 PM