1 /* splitWin.c is a simple example to show how to deal with split screens. 2 Due to the limited time, this program is not finished yet. 3 To compile: gcc splitWin.c -lncurses 4 Sam Hsu (11/17/10) 5 */ 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 WINDOW *create_newwin(int, int, int, int); 13 void destroy_win(WINDOW *); 14 void input_win(WINDOW *, char *); 15 void display_win(WINDOW *, char *); 16 void destroy_win(WINDOW *win); 17 void blankWin(WINDOW *win); 18 main() 19 { 20 WINDOW *chat_win, *msg_win; 21 int chat_startx, chat_starty, chat_width, chat_height, 22 msg_startx, msg_starty, msg_width, msg_height; 23 char buf[BUFSIZ]; 24 initscr(); /* Start curses mode */ 25 cbreak(); 26 noecho(); 27 refresh(); 28 chat_height = 5; 29 chat_width = COLS - 2; 30 chat_startx = 1; 31 chat_starty = LINES - chat_height; 32 msg_height = LINES - chat_height - 1; 33 msg_width = COLS; 34 msg_startx = 0; 35 msg_starty = 0; 36 37 msg_win = create_newwin(msg_height, msg_width, msg_starty, msg_startx); 38 scrollok(msg_win, TRUE); 39 chat_win = create_newwin(chat_height, chat_width, chat_starty, chat_startx); 40 scrollok(chat_win, TRUE); 41 input_win(chat_win, buf); 42 display_win(msg_win, buf); 43 input_win(chat_win, buf); 44 display_win(msg_win, buf); 45 sleep(5); /* to get a delay */ 46 destroy_win(chat_win); 47 destroy_win(msg_win); 48 endwin(); 49 } 50 WINDOW *create_newwin(int height, int width, int starty, int startx) 51 { 52 WINDOW *local_win; 53 local_win = newwin(height, width, starty, startx); 54 box(local_win, 0, 0); /* draw a box */ 55 wmove(local_win, 1, 1); /* position cursor at top */ 56 wrefresh(local_win); 57 return local_win; 58 } 59 /* This function is for taking input chars from the user */ 60 void input_win(WINDOW *win, char *word) 61 { 62 int i, ch; 63 int maxrow, maxcol, row = 1, col = 0; 64 blankWin(win); /* make it a clean window */ 65 getmaxyx(win, maxrow, maxcol); /* get window size */ 66 bzero(word, BUFSIZ); 67 wmove(win, 1, 1); /* position cusor at top */ 68 for (i = 0; (ch = wgetch(win)) != '\n'; i++) { 69 word[i] = ch; /* '\n' not copied */ 70 if (col++ < maxcol-2) /* if within window */ 71 wprintw(win, "%c", word[i]); /* display the char recv'd */ 72 else { /* last char pos reached */ 73 col = 1; 74 if (row == maxrow-2) { /* last line in the window */ 75 scroll(win); /* go up one line */ 76 row = maxrow-2; /* stay at the last line */ 77 wmove(win, row, col); /* move cursor to the beginning */ 78 wclrtoeol(win); /* clear from cursor to eol */ 79 box(win, 0, 0); /* draw the box again */ 80 } else 81 row++; 82 wmove(win, row, col); /* move cursor to the beginning */ 83 wrefresh(win); 84 wprintw(win, "%c", word[i]); /* display the char recv'd */ 85 } 86 } 87 } /* input_win */ 88 void display_win(WINDOW *win, char *word) 89 { 90 blankWin(win); /* make it a clean window */ 91 wmove(win, 1, 1); /* position cusor at top */ 92 wprintw(win, word); 93 wrefresh(win); 94 } /* display_win */ 95 void destroy_win(WINDOW *win) 96 { 97 delwin(win); 98 } /* destory_win */ 99 void blankWin(WINDOW *win) 100 { 101 int i; 102 int maxrow, maxcol; 103 getmaxyx(win, maxrow, maxcol); 104 for (i = 1; i < maxcol-2; i++) { 105 wmove(win, i, 1); 106 refresh(); 107 wclrtoeol(win); 108 wrefresh(win); 109 } 110 box(win, 0, 0); /* draw the box again */ 111 wrefresh(win); 112 } /* blankWin */