Thread: help with ncurses

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    22

    help with ncurses

    Guys,

    I need some help with ncurses.

    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    #include <time.h>
    #include <curses.h>
    
    int current_getch;
    int doloop = 1;
    static WINDOW *mainwnd;
    static WINDOW *screen;
    WINDOW *my_win;
    
    int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
    time_t now;
    struct tm *now_tm;
    
    void screen_init(void)
    {
       mainwnd = initscr();
       noecho();
       cbreak();
       nodelay(mainwnd, TRUE);
       refresh(); // 1)
       wrefresh(mainwnd);
       screen = newwin(20, 45, 5, 5);
       box(screen, ACS_VLINE, ACS_HLINE);
    }
    
    static void update_display(void) {
       curs_set(0);
       mvwprintw(screen,1,15,"_________");
       mvwprintw(screen,2,2,"USERNAME");
       mvwprintw(screen,2,14,"|_________|");
       mvwprintw(screen,4,15,"__________");
       mvwprintw(screen,5,2,"PASSWORD");
       mvwprintw(screen,5,14,"|__________|");
       mvwprintw(screen,7,6,"TIME: %d:%d:%d", now_hour, now_min, now_sec);
       mvwprintw(screen,9,6,"DATE: %d-%d-%d", now_day, now_month, now_year);
       mvwprintw(screen,11,6,"PRESS e TO END");
       wrefresh(screen);
       refresh();
    }
    
    void screen_end(void)
    {
       endwin();
    }
    
    void maketime(void) 
        {
            // Get the current date/time
            now = time (NULL);
            now_tm = localtime (&now);
            now_sec = now_tm->tm_sec;
            now_min = now_tm->tm_min;
            now_hour = now_tm->tm_hour;
            now_day = now_tm->tm_mday;
            now_wday = now_tm->tm_wday;
            now_month = now_tm->tm_mon + 1;
            now_year = now_tm->tm_year + 1900;
            }
    
    int main(void)
      {
       screen_init();
       while (doloop)
         {
          current_getch = getch();
          if (current_getch == 101)
            {
             doloop = 0;
            }
          maketime();
          update_display();
          sleep(1);
          }
       screen_end();
       printf("program ended successfully\n");
       return 0;
      }
    This code gives the below output





    ┌───────────────────────────────────────────┐
    │ _________ │
    │ USERNAME |_________| │
    │ │
    │ __________ │
    │ PASSWORD |__________| │
    │ |
    │ TIME: 0:50:33 │
    │ │
    │ DATE: 29-8-2009 │
    │ │
    │ PRESS e TO END │
    │ │
    │ │
    │ │
    │ │
    │ │
    │ │
    │ │
    └───────────────────────────────────────────┘



    Pardon me for the poor bordering, but this program only draws a few borders but doesnt produce an actual textbox.I would like to know how to input text in a actual textbox and save it to a file.

    Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You don't need a text box. You position the cursor, write the prompt ("Password: ") if necessary, turn echo back on so you can see what you are typing, getstr(), turn echo off again...
    Code:
            char response[64]
    	mvprintw(5,2,"Password: ");
    	echo();
    	getstr(response);
    	noecho();
    If you want the inputted text to stand out from everything else, use color sets -- sandwich getstr() with attron() attroff calls. Eg, these two are the inverse of each other:
    Code:
            start_color();
    	init_pair(1, COLOR_MAGENTA,COLOR_BLACK);
    	init_pair(2, COLOR_BLACK,COLOR_MAGENTA);
    	attron(COLOR_PAIR(2));
    	printw("hello world");
    	attroff(COLOR_PAIR(2));
    Last edited by MK27; 08-28-2009 at 08:00 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    22

    Help with ncurses

    Thanks,

    I managed to come up with this program after a few changes

    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <unistd.h>
    #include <time.h>
    #include <curses.h>
    
    char user[64],password[64];
    int current_getch;
    int doloop = 1;
    static WINDOW *mainwnd;
    static WINDOW *screen;
    WINDOW *my_win;
    
    int now_sec, now_min, now_hour, now_day, now_wday, now_month, now_year;
    time_t now;
    struct tm *now_tm;
     
      void screen_init(void)
      {
       mainwnd = initscr();
       noecho();
       cbreak();
       nodelay(mainwnd, TRUE);
       refresh(); // 1)
       wrefresh(mainwnd);
       screen = newwin(20, 45, 5, 5);
       box(screen, ACS_VLINE, ACS_HLINE);
      }
    
      static void update_display(void)
      {
       curs_set(0);
       mvwprintw(screen,2,6,"TIME: %d:%d:%d",now_hour, now_min, now_sec);
       mvwprintw(screen,4,6,"DATE: %d-%d-%d", now_day, now_month, now_year);
       mvwprintw(screen,5,2,"USERNAME");
       echo();
       mvwgetnstr(screen,5,12,user,12);
       mvwprintw(screen,7,2,"PASSWORD");
       echo();
       mvwgetnstr(screen,7,12,password,8);
       mvwprintw(screen,11,6,"PRESS E TO END");
       wrefresh(screen);
       refresh();
        }
    
      void screen_end(void)
      {
       endwin();
      }
    
      void maketime(void)
      {
            // Get the current date/time
            now = time (NULL);
            now_tm = localtime (&now);
            now_sec = now_tm->tm_sec;
            now_min = now_tm->tm_min;
            now_hour = now_tm->tm_hour;
            now_day = now_tm->tm_mday;
            now_wday = now_tm->tm_wday;
            now_month = now_tm->tm_mon + 1;
            now_year = now_tm->tm_year + 1900;
    
      }
    
      int main(void)
      {
       screen_init();
       while (doloop)
         {
          current_getch = getch();
          if (current_getch == 69)
            {
             doloop = 0;
            }
          maketime();
          update_display();
          sleep(1);
          }
        screen_end();
        printf("program ended successfully\n");
        return 0;
         }
    But I want the password to display only ******* even if I input ssssss.
    It will be nice if you could help me out with that.

    Last edited by rakesh_01; 08-31-2009 at 05:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ncurses or not?
    By Zarniwoop in forum C Programming
    Replies: 4
    Last Post: 05-04-2008, 11:19 AM
  2. scroll up with ncurses ? plz help
    By dimaash in forum Linux Programming
    Replies: 1
    Last Post: 01-30-2006, 05:17 AM
  3. Ncurses and Standard Functions
    By gsoft in forum C Programming
    Replies: 2
    Last Post: 02-07-2005, 08:18 PM
  4. ncurses, windows, and colors
    By subflood in forum Linux Programming
    Replies: 2
    Last Post: 08-26-2004, 10:14 AM
  5. nCurses
    By MethodMan in forum Linux Programming
    Replies: 6
    Last Post: 02-18-2003, 07:29 AM