Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.90 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <string.h>
  3. typedef struct _win_border_struct {
  4.     chtype  ls, rs, ts, bs,
  5.         tl, tr, bl, br;
  6. }WIN_BORDER;
  7.  
  8. typedef struct _WIN_struct {
  9.     char name[20];
  10.     int startx, starty;
  11.     int height, width;
  12.     WIN_BORDER border;
  13. }WIN;
  14.  
  15. void init_con_params(WIN *p_win);
  16. void print_win_params(WIN *p_win);
  17. void create_box(WIN *win, bool flag);
  18. void resize_win(WIN *p_win);
  19. int main(int argc, char *argv[])
  20. {
  21.    
  22.     WIN contacts;
  23.     strcpy(contacts.name,  "contacts");
  24.     WIN write;
  25.     strcpy(write.name, "write");
  26.     WIN conversation;
  27.     strcpy(conversation.name, "conversation");
  28.     int ch;
  29.  
  30.     initscr();
  31.     start_color();
  32.     cbreak;
  33.  
  34.     keypad(stdscr, TRUE);
  35.     noecho();
  36.     init_pair(1, COLOR_CYAN, COLOR_BLACK);
  37.  
  38.     //init window stuff
  39.     init_con_params(&conversation);
  40.     print_win_params(&conversation);
  41.  
  42.     attron(COLOR_PAIR(1));
  43.     printw("Press F1 to close");
  44.     refresh();
  45.     attroff(COLOR_PAIR(1));
  46.  
  47.     create_box(&conversation, TRUE);
  48.     while((ch = getch()) != KEY_F(1))
  49.     {
  50.         resize_win(&conversation);
  51.         refresh();
  52.         switch(ch)
  53.         {case KEY_LEFT:
  54.             create_box(&conversation,FALSE);
  55.             --conversation.startx;
  56.             create_box(&conversation,TRUE);
  57.             break;
  58.          case KEY_RIGHT:
  59.             create_box(&conversation,FALSE);
  60.             ++conversation.startx;
  61.             create_box(&conversation,TRUE);
  62.             break;
  63.         case KEY_UP:
  64.             create_box(&conversation,FALSE);
  65.             --conversation.starty;
  66.             create_box(&conversation,TRUE);
  67.             break;
  68.         case KEY_DOWN:
  69.             create_box(&conversation,FALSE);
  70.             ++conversation.starty;
  71.             create_box(&conversation,TRUE);
  72.             break;
  73.         }}
  74. endwin();
  75. return 0;
  76. }
  77. void resize_win(WIN *p_win){
  78.     if (strcmp("conversation",p_win->name) ==0){
  79.         p_win->height = LINES -4;
  80.         p_win->width = COLS -12;
  81.     }
  82.     create_box(p_win,FALSE);
  83.     create_box(p_win,TRUE);
  84.  
  85.  
  86. }
  87. void init_con_params(WIN *p_win){
  88.     p_win->height = LINES -4;
  89.     p_win->width = COLS -12;
  90.     p_win->starty = 0;
  91.     p_win->startx = 0;
  92.    
  93.     p_win->border.ls = '|';
  94.     p_win->border.rs = '|';
  95.     p_win->border.ts = '|';
  96.     p_win->border.bs = '|';
  97.     p_win->border.tl = '+';
  98.     p_win->border.tr = '+';
  99.     p_win->border.bl = '+';
  100.     p_win->border.br = '+';
  101. }
  102.  
  103. void print_win_params(WIN *p_win)
  104. {
  105. #ifdef _DEBUG
  106.     mvprintw(25, 0, "%d %d %d %d", p_win->startx, p_win->starty,
  107.             p_win->width,p_win->height);
  108.     refresh();
  109. #endif
  110. }
  111.  
  112. void create_box(WIN *p_win, bool flag)
  113. {   int i,j;
  114.     int x, y, w, h;
  115.  
  116.     x = p_win->startx;
  117.     y = p_win->starty;
  118.     w = p_win->width;
  119.     h = p_win->height;
  120.  
  121.     if(flag == TRUE)
  122.     {
  123.         mvaddch(y,x, p_win->border.tl);
  124.         mvaddch(y, x+w, p_win->border.tr);
  125.         mvaddch(y+h, x, p_win->border.bl);
  126.         mvaddch(y+h, x+w, p_win->border.br);
  127.         mvhline(y,x+1, p_win->border.ts, w-1);
  128.         mvhline(y+h, x+1, p_win->border.bs, w-1);
  129.         mvvline(y+1, x, p_win->border.ls, h-1);
  130.         mvvline(y+1, x+w, p_win->border.ls, h-1);
  131.     }
  132.     else
  133.         for(j=y;j <= y + h; ++j)
  134.             for(i = x; i<= x +w;++i)
  135.                 mvaddch(j,i,' ');
  136.     refresh();
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement