2011년8월10일_네트워크프로젝트(7일차)-출력창내용보존법 찾기(결과는 실패)




채팅흐름도

 

카톡(?)과 비슷하게 클라이언트가 글을 적어 서버로 보내면 서버는 접속한 모든 클라이언트에게 방송하고,
수신받은 클라이언트들을 수신된 메시지를 해석하여 데이터와 커멘드로 분류하고 데이터일 경우 상기의 흐름도와 같이,
좌우 위치를 정하고 수신받은 데이터의 크기를 측정하여 말풍선의 크기를 정해 그리고 글자를 삽입한다.
좌측에 위치한 사람은 좌측에서 우측을 보고 말을 하고 우측에 위치한 사람은 좌측을 보고 말을 한다.
이를 구현하기 위해 중요한 것은 이전 기록을 유지하는 것이다.
그래서 스크롤이 되어도 이전 데이터가 보존되는 함수가 있나 찾아 보았다.

우선 스크롤활성은 scrollok()이고,
스크롤관련함수 세 가지를 man page에서 찾아 보고 실행해 봤으나 원하는 결과를 얻지 못 했었다.

 

//2011년8월9일 보고서 참조.


그 외에 상기와 같은 다른 함수들도 테스트해 보았으나 원하는 결과를 얻지 못 하였다.
그래서 window가 아닌 panel과 form도 해봤다.


● panel.c

   1: #include <panel.h>                                                                                                                      
   2:                                                                                                                                         
   3: typedef struct _PANEL_DATA {                                                                                                            
   4:         int x, y, w, h;                                                                                                                 
   5:         char label[80];                                                                                                                 
   6:         int label_color;                                                                                                                
   7:         PANEL *next;                                                                                                                    
   8: }PANEL_DATA;                                                                                                                            
   9:                                                                                                                                         
  10: #define NLINES 10                                                                                                                       
  11: #define NCOLS 40                                                                                                                        
  12:                                                                                                                                         
  13: void init_wins(WINDOW **wins, int n);                                                                                                   
  14: void win_show(WINDOW *win, char *label, int label_color);                                                                               
  15: void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);                                       
  16: void set_user_ptrs(PANEL **panels, int n);                                                                                              
  17:                                                                                                                                         
  18: int main()                                                                                                                              
  19: {       WINDOW *my_wins[3];                                                                                                             
  20:         PANEL  *my_panels[3];                                                                                                           
  21:         PANEL_DATA  *top;                                                                                                               
  22:         PANEL *stack_top;                                                                                                               
  23:         WINDOW *temp_win, *old_win;                                                                                                     
  24:         int ch;                                                                                                                         
  25:         int newx, newy, neww, newh;                                                                                                     
  26:         int size = FALSE, move = FALSE;                                                                                                 
  27:                                                                                                                                         
  28:         /* Initialize curses */                                                                                                         
  29:         initscr();                                                                                                                      
  30:         start_color();                                                                                                                  
  31:         cbreak();                                                                                                                       
  32:         noecho();                                                                                                                       
  33:         keypad(stdscr, TRUE);                                                                                                           
  34:                                                                                                                                         
  35:         /* Initialize all the colors */                                                                                                 
  36:         init_pair(1, COLOR_RED, COLOR_BLACK);                                                                                           
  37:         init_pair(2, COLOR_GREEN, COLOR_BLACK);                                                                                         
  38:         init_pair(3, COLOR_BLUE, COLOR_BLACK);                                                                                          
  39:         init_pair(4, COLOR_CYAN, COLOR_BLACK);                                                                                          
  40:                                                                                                                                         
  41:         init_wins(my_wins, 3);                                                                                                          
  42:                                                                                                                                         
  43:         /* Attach a panel to each window */     /* Order is bottom up */                                                                
  44:         my_panels[0] = new_panel(my_wins[0]);   /* Push 0, order: stdscr-0 */                                                           
  45:         my_panels[1] = new_panel(my_wins[1]);   /* Push 1, order: stdscr-0-1 */                                                         
  46:         my_panels[2] = new_panel(my_wins[2]);   /* Push 2, order: stdscr-0-1-2 */                                                       
  47:                                                                                                                                         
  48:         set_user_ptrs(my_panels, 3);                                                                                                    
  49:         /* Update the stacking order. 2nd panel will be on top */                                                                       
  50:         update_panels();                                                                                                                
  51:                                                                                                                                         
  52:         /* Show it on the screen */                                                                                                     
  53:         attron(COLOR_PAIR(4));                                                                                                          
  54:         mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");                                                                 
  55:         mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");                                                   
  56:         attroff(COLOR_PAIR(4));                                                                                                         
  57:         doupdate();                                                                                                                     
  58:                                                                                                                                         
  59:         stack_top = my_panels[2];                                                                                                       
  60:         top = (PANEL_DATA *)panel_userptr(stack_top);                                                                                   
  61:         newx = top->x;                                                                                                                  
  62:         newy = top->y;                                                                                                                  
  63:         neww = top->w;                                                                                                                  
  64:         newh = top->h;                                                                                                                  
  65:         while((ch = getch()) != KEY_F(1))                                                                                               
  66:         {       switch(ch)                                                                                                              
  67:                 {       case 9:         /* Tab */                                                                                       
  68:                                 top = (PANEL_DATA *)panel_userptr(stack_top);                                                           
  69:                                 top_panel(top->next);                                                                                   
  70:                                 stack_top = top->next;                                                                                  
  71:                                 top = (PANEL_DATA *)panel_userptr(stack_top);                                                           
  72:                                 newx = top->x;                                                                                          
  73:                                 newy = top->y;                                                                                          
  74:                                 neww = top->w;                                                                                          
  75:                                 newh = top->h;                                                                                          
  76:                                 break;                                                                                                  
  77:                         case 'r':       /* Re-Size*/                                                                                    
  78:                                 size = TRUE;                                                                                            
  79:                                 attron(COLOR_PAIR(4));                                                                                  
  80:                                 mvprintw(LINES - 4, 0, "Entered Resizing :Use Arrow Keys to resize and press <ENTER> to end resizing"); 
  81:                                 refresh();                                                                                              
  82:                                 attroff(COLOR_PAIR(4));                                                                                 
  83:                                 break;                                                                                                  
  84:                         case 'm':       /* Move */                                                                                      
  85:                                 attron(COLOR_PAIR(4));                                                                                  
  86:                                 mvprintw(LINES - 4, 0, "Entered Moving: Use Arrow Keys to Move and press <ENTER> to end moving");       
  87:                                 refresh();                                                                                              
  88:                                 attroff(COLOR_PAIR(4));                                                                                 
  89:                                 move = TRUE;                                                                                            
  90:                                 break;                                                                                                  
  91:                         case KEY_LEFT:                                                                                                  
  92:                                 if(size == TRUE)                                                                                        
  93:                                 {       --newx;                                                                                         
  94:                                         ++neww;                                                                                         
  95:                                 }                                                                                                       
  96:                                 if(move == TRUE)                                                                                        
  97:                                         --newx;                                                                                         
  98:                                 break;                                                                                                  
  99:                         case KEY_RIGHT:                                                                                                 
 100:                                 if(size == TRUE)                                                                                        
 101:                                 {       ++newx;                                                                                         
 102:                                         --neww;                                                                                         
 103:                                 }                                                                                                       
 104:                                 if(move == TRUE)                                                                                        
 105:                                         ++newx;                                                                                         
 106:                                 break;                                                                                                  
 107:                         case KEY_UP:                                                                                                    
 108:                                 if(size == TRUE)                                                                                        
 109:                                 {       --newy;                                                                                         
 110:                                         ++newh;                                                                                         
 111:                                 }                                                                                                       
 112:                                 if(move == TRUE)                                                                                        
 113:                                         --newy;                                                                                         
 114:                                 break;                                                                                                  
 115:                         case KEY_DOWN:                                                                                                  
 116:                                 if(size == TRUE)                                                                                        
 117:                                 {       ++newy;                                                                                         
 118:                                         --newh;                                                                                         
 119:                                 }                                                                                                       
 120:                                 if(move == TRUE)                                                                                        
 121:                                         ++newy;                                                                                         
 122:                                 break;                                                                                                  
 123:                         case 10:        /* Enter */                                                                                     
 124:                                 move(LINES - 4, 0);                                                                                     
 125:                                 clrtoeol();                                                                                             
 126:                                 refresh();                                                                                              
 127:                                 if(size == TRUE)                                                                                        
 128:                                 {       old_win = panel_window(stack_top);                                                              
 129:                                         temp_win = newwin(newh, neww, newy, newx);                                                      
 130:                                         replace_panel(stack_top, temp_win);                                                             
 131:                                         win_show(temp_win, top->label, top->label_color);                                               
 132:                                         delwin(old_win);                                                                                
 133:                                         size = FALSE;                                                                                   
 134:                                 }                                                                                                       
 135:                                 if(move == TRUE)                                                                                        
 136:                                 {       move_panel(stack_top, newy, newx);                                                              
 137:                                         move = FALSE;                                                                                   
 138:                                 }                                                                                                       
 139:                                 break;                                                                                                  
 140:                                                                                                                                         
 141:                 }                                                                                                                       
 142:                 attron(COLOR_PAIR(4));                                                                                                  
 143:                 mvprintw(LINES - 3, 0, "Use 'm' for moving, 'r' for resizing");                                                         
 144:                 mvprintw(LINES - 2, 0, "Use tab to browse through the windows (F1 to Exit)");                                           
 145:                 attroff(COLOR_PAIR(4));                                                                                                 
 146:                 refresh();                                                                                                              
 147:                 update_panels();                                                                                                        
 148:                 doupdate();                                                                                                             
 149:         }                                                                                                                               
 150:         endwin();                                                                                                                       
 151:         return 0;                                                                                                                       
 152: }                                                                                                                                       
 153:                                                                                                                                         
 154: /* Put all the windows */                                                                                                               
 155: void init_wins(WINDOW **wins, int n)                                                                                                    
 156: {       int x, y, i;                                                                                                                    
 157:         char label[80];                                                                                                                 
 158:                                                                                                                                         
 159:         y = 2;                                                                                                                          
 160:         x = 10;                                                                                                                         
 161:         for(i = 0; i < n; ++i)                                                                                                          
 162:         {       wins[i] = newwin(NLINES, NCOLS, y, x);                                                                                  
 163:                 sprintf(label, "Window Number %d", i + 1);                                                                              
 164:                 win_show(wins[i], label, i + 1);                                                                                        
 165:                 y += 3;                                                                                                                 
 166:                 x += 7;                                                                                                                 
 167:         }                                                                                                                               
 168: }                                                                                                                                       
 169:                                                                                                                                         
 170: /* Set the PANEL_DATA structures for individual panels */                                                                               
 171: void set_user_ptrs(PANEL **panels, int n)                                                                                               
 172: {       PANEL_DATA *ptrs;                                                                                                               
 173:         WINDOW *win;                                                                                                                    
 174:         int x, y, w, h, i;                                                                                                              
 175:         char temp[80];                                                                                                                  
 176:                                                                                                                                         
 177:         ptrs = (PANEL_DATA *)calloc(n, sizeof(PANEL_DATA));                                                                             
 178:                                                                                                                                         
 179:         for(i = 0;i < n; ++i)                                                                                                           
 180:         {       win = panel_window(panels[i]);                                                                                          
 181:                 getbegyx(win, y, x);                                                                                                    
 182:                 getmaxyx(win, h, w);                                                                                                    
 183:                 ptrs[i].x = x;                                                                                                          
 184:                 ptrs[i].y = y;                                                                                                          
 185:                 ptrs[i].w = w;                                                                                                          
 186:                 ptrs[i].h = h;                                                                                                          
 187:                 sprintf(temp, "Window Number %d", i + 1);                                                                               
 188:                 strcpy(ptrs[i].label, temp);                                                                                            
 189:                 ptrs[i].label_color = i + 1;                                                                                            
 190:                 if(i + 1 == n)                                                                                                          
 191:                         ptrs[i].next = panels[0];                                                                                       
 192:                 else                                                                                                                    
 193:                         ptrs[i].next = panels[i + 1];                                                                                   
 194:                 set_panel_userptr(panels[i], &ptrs[i]);                                                                                 
 195:         }                                                                                                                               
 196: }                                                                                                                                       
 197:                                                                                                                                         
 198: /* Show the window with a border and a label */                                                                                         
 199: void win_show(WINDOW *win, char *label, int label_color)                                                                                
 200: {       int startx, starty, height, width;                                                                                              
 201:                                                                                                                                         
 202:         getbegyx(win, starty, startx);                                                                                                  
 203:         getmaxyx(win, height, width);                                                                                                   
 204:                                                                                                                                         
 205:         box(win, 0, 0);                                                                                                                 
 206:         mvwaddch(win, 2, 0, ACS_LTEE);                                                                                                  
 207:         mvwhline(win, 2, 1, ACS_HLINE, width - 2);                                                                                      
 208:         mvwaddch(win, 2, width - 1, ACS_RTEE);                                                                                          
 209:                                                                                                                                         
 210:         print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color));                                                              
 211: }                                                                                                                                       
 212:                                                                                                                                         
 213: void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)                                        
 214: {       int length, x, y;                                                                                                               
 215:         float temp;                                                                                                                     
 216:                                                                                                                                         
 217:         if(win == NULL)                                                                                                                 
 218:                 win = stdscr;                                                                                                           
 219:         getyx(win, y, x);                                                                                                               
 220:         if(startx != 0)                                                                                                                 
 221:                 x = startx;                                                                                                             
 222:         if(starty != 0)                                                                                                                 
 223:                 y = starty;                                                                                                             
 224:         if(width == 0)                                                                                                                  
 225:                 width = 80;                                                                                                             
 226:                                                                                                                                         
 227:         length = strlen(string);                                                                                                        
 228:         temp = (width - length)/ 2;                                                                                                     
 229:         x = startx + (int)temp;                                                                                                         
 230:         wattron(win, color);                                                                                                            
 231:         mvwprintw(win, y, x, "%s", string);                                                                                             
 232:         wattroff(win, color);                                                                                                           
 233:         refresh();                                                                                                                      
 234: } 
 235:  
 236:  


<실행결과>



● form.c

   1: #include <form.h>                                                                                 
   2:                                                                                                   
   3: void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color); 
   4:                                                                                                   
   5: int main()                                                                                        
   6: {                                                                                                 
   7:         FIELD *field[3];                                                                          
   8:         FORM  *my_form;                                                                           
   9:         WINDOW *my_form_win;                                                                      
  10:         int ch, rows, cols;                                                                       
  11:                                                                                                   
  12:         /* Initialize curses */                                                                   
  13:         initscr();                                                                                
  14:         start_color();                                                                            
  15:         cbreak();                                                                                 
  16:         noecho();                                                                                 
  17:         keypad(stdscr, TRUE);                                                                     
  18:                                                                                                   
  19:         /* Initialize few color pairs */                                                          
  20:         init_pair(1, COLOR_RED, COLOR_BLACK);                                                     
  21:                                                                                                   
  22:         /* Initialize the fields */                                                               
  23:         field[0] = new_field(1, 10, 6, 1, 0, 0);                                                  
  24:         field[1] = new_field(1, 10, 8, 1, 0, 0);                                                  
  25:         field[2] = NULL;                                                                          
  26:                                                                                                   
  27:         /* Set field options */                                                                   
  28:         set_field_back(field[0], A_UNDERLINE);                                                    
  29:         field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */              
  30:                                               /* Field is filled up             */                
  31:         set_field_back(field[1], A_UNDERLINE);                                                    
  32:         field_opts_off(field[1], O_AUTOSKIP);                                                     
  33:                                                                                                   
  34:         /* Create the form and post it */                                                         
  35:         my_form = new_form(field);                                                                
  36:                                                                                                   
  37:         /* Calculate the area required for the form */                                            
  38:         scale_form(my_form, &rows, &cols);                                                        
  39:                                                                                                   
  40:         /* Create the window to be associated with the form */                                    
  41:         my_form_win = newwin(rows + 4, cols + 4, 4, 4);                                           
  42:         keypad(my_form_win, TRUE);                                                                
  43:                                                                                                   
  44:         /* Set main window and sub window */                                                      
  45:         set_form_win(my_form, my_form_win);                                                       
  46:         set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2));                             
  47:                                                                                                   
  48:         /* Print a border around the main window and print a title */                             
  49:         box(my_form_win, 0, 0);                                                                   
  50:         print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1));                   
  51:                                                                                                   
  52:         post_form(my_form);                                                                       
  53:         wrefresh(my_form_win);                                                                    
  54:                                                                                                   
  55:         mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields");               
  56:         refresh();                                                                                
  57:                                                                                                   
  58:         /* Loop through to get user requests */                                                   
  59:         while((ch = wgetch(my_form_win)) != KEY_F(1))                                             
  60:         {       switch(ch)                                                                        
  61:                 {       case KEY_DOWN:                                                            
  62:                                 /* Go to next field */                                            
  63:                                 form_driver(my_form, REQ_NEXT_FIELD);                             
  64:                                 /* Go to the end of the present buffer */                         
  65:                                 /* Leaves nicely at the last character */                         
  66:                                 form_driver(my_form, REQ_END_LINE);                               
  67:                                 break;                                                            
  68:                         case KEY_UP:                                                              
  69:                                 /* Go to previous field */                                        
  70:                                 form_driver(my_form, REQ_PREV_FIELD);                             
  71:                                 form_driver(my_form, REQ_END_LINE);                               
  72:                                 break;                                                            
  73:                         default:                                                                  
  74:                                 /* If this is a normal character, it gets */                      
  75:                                 /* Printed                                */                      
  76:                                 form_driver(my_form, ch);                                         
  77:                                 break;                                                            
  78:                 }                                                                                 
  79:         }                                                                                         
  80:                                                                                                   
  81:         /* Un post form and free the memory */                                                    
  82:         unpost_form(my_form);                                                                     
  83:         free_form(my_form);                                                                       
  84:         free_field(field[0]);                                                                     
  85:         free_field(field[1]);                                                                     
  86:                                                                                                   
  87:         endwin();                                                                                 
  88:         return 0;                                                                                 
  89: }                                                                                                 
  90:                                                                                                   
  91: void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)  
  92: {       int length, x, y;                                                                         
  93:         float temp;                                                                               
  94:                                                                                                   
  95:         if(win == NULL)                                                                           
  96:                 win = stdscr;                                                                     
  97:         getyx(win, y, x);                                                                         
  98:         if(startx != 0)                                                                           
  99:                 x = startx;                                                                       
 100:         if(starty != 0)                                                                           
 101:                 y = starty;                                                                       
 102:         if(width == 0)                                                                            
 103:                 width = 80;                                                                       
 104:                                                                                                   
 105:         length = strlen(string);                                                                  
 106:         temp = (width - length)/ 2;                                                               
 107:         x = startx + (int)temp;                                                                   
 108:         wattron(win, color);                                                                      
 109:         mvwprintw(win, y, x, "%s", string);                                                       
 110:         wattroff(win, color);                                                                     
 111:         refresh();                                                                                
 112: }


<실행결과>



이것들은 내가 원하는게 아니야~
오늘 하루종일 뭐한거지? ㅡ_ㅡ;
왠지 스크롤 포기하고 캐릭터고정챗으로 해야겠다는...
기분이 든다(?)