aHardyX

ncurses trial 3

Aug 6th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define FALSE   0
  7. #define TRUE    1
  8.  
  9. /*
  10.  * GLOBAL VARIABLES
  11.  */
  12.  
  13. WINDOW *layer1, *layer2, *layerBottom, *AddButton;
  14.  
  15. char *TEXT_LIBRARY[3] =
  16.         {
  17.             "Patta Address Book",
  18.             "All Contacts",
  19.             "^A: Add \t^E: Edit \t^D: Delete \t^S: Search \t^G: Groups \t^X: Export \t^Q: Quit\t"
  20.         };
  21.  
  22. int scrX;
  23. int scrY;
  24.  
  25. /*
  26.  * FUNCTION DECLARATIONS
  27.  */
  28. int     checkScrSize();
  29.  
  30.  
  31. /* Prints the text at center of Window/Layer. Parameters: WINDOW*, the row number and the string */
  32. void    center_print(WINDOW*, int, const char*);
  33. void    center_color_print(WINDOW*, int, int, int, const char*);  
  34.  
  35. /*
  36.  * FUNCTION DEFINATIONS
  37.  */
  38.  
  39. int checkScrSize()
  40. {
  41.     getmaxyx(stdscr, scrY, scrX);
  42.     if(scrX < 80 || scrY < 24)
  43.         return FALSE;
  44.     return TRUE;
  45. }
  46.  
  47. void center_print(WINDOW *win, int textY, const char *text)
  48. {
  49.     int length = strlen(text);
  50.     int winX, winY, textX;
  51.  
  52.     getmaxyx(win, winY, winX);
  53.  
  54.     textX = (winX/2)-(length/2);
  55.     mvwprintw(win, textY,textX, "%s", text);
  56. }
  57.  
  58. void center_color_print(WINDOW *win, int FOREGROUND, int BACKGROUND, int textY, const char *text)
  59. {
  60.     init_pair(1, FOREGROUND, BACKGROUND);
  61.     int length = strlen(text);
  62.     int winX, winY, textX;
  63.  
  64.     getmaxyx(win, winY, winX);
  65.  
  66.     textX = (winX/2)-(length/2);
  67.     attron(COLOR_PAIR(1));
  68.     mvwprintw(win, textY,textX, "%s", text);
  69.     attroff(COLOR_PAIR(1));
  70. }
  71.  
  72. int main()
  73. {
  74.    
  75.    
  76.  
  77.     initscr();
  78.     init_pair(1, COLOR_BLACK, COLOR_RED);
  79.     start_color();
  80.     if (checkScrSize() == FALSE)
  81.     {
  82.         endwin();
  83.         printf("Please change you terminal size to atleast 80x24 and restart the program\n");
  84.         exit(0);
  85.     }
  86.  
  87.     center_print(stdscr,0,TEXT_LIBRARY[0]);
  88.  
  89.     refresh();
  90.  
  91.     /* Window/Layer Properties */
  92.     layer1 = newwin(scrY-4, scrX/2, 1,0);
  93.         box(layer1,0,0);
  94.     layer2 = newwin(scrY-4, (scrX/2), 1,(scrX/2));
  95.         box(layer2,0,0);
  96.     layerBottom = newwin(4,scrX,scrY-3,0);
  97.  
  98.     center_print(layer1, 2, TEXT_LIBRARY[1]);
  99.     center_color_print(layerBottom, COLOR_GREEN, COLOR_BLACK, 0, "This is a status bar.");
  100.     attron(COLOR_PAIR(1));
  101.     mvwprintw(layerBottom,1,0,TEXT_LIBRARY[2]);
  102.     attroff(COLOR_PAIR(1));
  103.    
  104.    
  105.  
  106.     /* Window/Layer Initialization */
  107.     wrefresh(layer1);
  108.     wrefresh(layer2);
  109.     wrefresh(layerBottom);
  110.  
  111.     /* Window/Layer Deletion */
  112.     delwin(layer1);
  113.     delwin(layer2);
  114.     delwin(layerBottom);
  115.  
  116.     getch();
  117.  
  118.     endwin();
  119. }
Advertisement
Add Comment
Please, Sign In to add comment