Ncurses Pad锁定光标

时间:2018-03-16 04:52:00

标签: c ncurses

我正在尝试使用Ncurses在C中创建文本编辑器。我目前正在打印文本到我可以滚动的打击垫。但是,当我执行wdelch(pad)时,预期的字符将被删除,但之后我无法移动光标。

在调用wdelch()之前,我调用wmove(pad),以使pad逻辑光标与stdscr光标位于同一位置,并删除预期的字符。奇怪的是,如果我再次点击删除,光标将更新并移动到下一个位置。从尝试调试代码开始,似乎wmove(pad)是取消光标控制的行。

您能帮我理解如何正确删除打击垫上的字符并保留移动光标的能力吗?

我认为问题必然在于我不完全理解刷新和预刷新。代码如下,我真的很感激任何反馈。

第二组switch语句 case 127 是问题发生的地方。刷新和预刷新发生在while循环的开头。谢谢!

int main (int argc, char** argv){

int c, row, col, crow, ccol,prow,pcol;
// Program requires exactly one command line argument
if (argc != 2){
    printf("Please provide a file name argument only");
}

char* fileName = argv[1];   //get the file name from the command line
char str[150];
FILE* file = fopen(fileName,"r");
if ( file == NULL) {
    perror("");
    return(1);
}

char** text = textArray();  //create the array of pointers that will be used to store text info
int counter = 0;
//read text file contents into text array data structure
while (fgets(str,150,file))
{
    char* mem_allocation = (char*)malloc (150 * sizeof(char) );
    strcpy( mem_allocation,str);
    text[counter] = mem_allocation;
    counter++;
}
//set up ncurses screen ###########
initscr();
getmaxyx(stdscr,row,col);
raw();
noecho();
// create pad
WINDOW* pad;
pad = newpad (10000, col);
keypad(pad, TRUE);
int padMinRow =0,padMinCol = 0,padMaxRow = row - 2,padMaxCol = col - 2;
refresh();
wprintw(pad,"Team #4   \"%s\"",fileName);
prefresh(pad,0,0,0,0,padMaxRow,padMaxCol);
//#################################

//print text contents to screen
wmove(pad,0,0);
counter = 0;
while (text[counter] != NULL)
{
    waddstr(pad,text[counter]);
    counter++;
}
move(1,0);
prefresh(pad,padMinRow,padMinCol,1,0,padMaxRow,padMaxCol);
//0 is command mode
//1 is edit mode
//2 is exit with write
//3 is exit without write
int editorMode = 0;
while (1) {
    refresh();
    prefresh(pad,padMinRow,padMinCol,1,0,padMaxRow,padMaxCol);
    c = wgetch(pad);
    getyx(stdscr,crow,ccol);
    //mvprintw(row-1,0,"%d",crow);   //get ascii value of char
    if (editorMode == 0) {
        switch (c) {

            case 105:
                editorMode = 1;
                getyx(stdscr,crow,ccol);
                mvprintw(row-1,0,"--Insert--");
                move(crow,ccol);
                break;
            case KEY_UP:
                getyx(stdscr,crow,ccol);
                if (crow > 1) {
                    move(crow-1,ccol);
                }
                else if (crow == 1) {
                    padMinRow--;
                }
                break;
            case KEY_DOWN:

                getyx(stdscr,crow,ccol);
                if (crow < row - 2) {
                    move(crow+1,ccol);
                }
                else if (crow >= (row - 2)) {
                    padMinRow++;
                    prow = (crow - 1) + 1;
                }

                break;
            case KEY_LEFT:
                getyx(stdscr,crow,ccol);
                if (ccol > 0) {
                    move(crow,ccol-1);
                }
                break;
            case KEY_RIGHT:
                getyx(stdscr,crow,ccol);
                if (ccol < col-1) {
                    move(crow,ccol+1);
                }
                break;
            case 10:
                endwin();
                return 0;
                break;
        }

    }
    else if (editorMode == 1) {
        switch (c) {

            case 27: //ESC ascii code is 27
                editorMode = 0;
                move(row-1,0);
                clrtoeol();
                break;
            case KEY_UP:
                getyx(stdscr,crow,ccol);
                if (crow > 1) {
                    move(crow-1,ccol);
                }
                else if (crow == 1) {
                    padMinRow--;
                }
                break;
            case KEY_DOWN:
                getyx(stdscr,crow,ccol);
                if (crow < row - 2) {
                    move(crow+1,ccol);
                    prow = crow - 1;
                }
                else if (crow >= (row - 2)) {
                    padMinRow++;
                    prow = (crow - 1) + 1;
                }

                break;
            case KEY_LEFT:
                getyx(stdscr,crow,ccol);
                if (ccol > 0) {
                    move(crow,ccol-1);
                }
                break;
            case KEY_RIGHT:
                getyx(stdscr,crow,ccol);
                if (ccol < col-1) {
                    move(crow,ccol+1);
                }
                break;
            // ########## HERE #########3
            case 127:
                getyx(stdscr,crow,ccol);
                getyx(pad,prow,pcol);
                wmove(pad,crow-1,ccol);
                wdelch(pad);
                move(crow,ccol);
                break;
            case KEY_BACKSPACE:

                wdelch(pad);
                break;
            case 10:
                endwin();
                return 0;
                break;
        }
    }
}


endwin();
/*deallocate memory
 for (int i=0; i<10000; i++) {
 free(text[i]);
 }
 free(text);
 */
return 0;
}

0 个答案:

没有答案