LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-06-2012, 10:13 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 278

Rep: Reputation: 5
Ncurses bug or ncurses program error.


The key down feature in case MENU_DESC doesn't key down the way I want it to. If you compile and run this program tell me if its an error on my behalf or if there's a function of ncurses I need to know about.

Code:
#include <time.h>
#include <curses.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

WINDOW *scrn;
WINDOW *map;
WINDOW *menu;
WINDOW *key;

WINDOW *name_box;
WINDOW *desc_box;

/* Player state */
#define MENU_MAIN        0
#define MENU_NAME        1
#define MENU_DESC        2
#define MENU_SIZE        3
#define MENU_CSIZE       4
#define MENU_CEXPR       5
#define MENU_FLIGHT      6
#define MENU_FBRIGHT     7
#define MENU_BACK        8

#define CREATING         9

const char *menu_main[] =
{
     "Create Dungeon Map",
     "       Exit       "
};

const char *menu_name[] =
{
     "Enter a map name",
     "",
     "",
     "",
     "Exit"
};

const char *menu_desc[] =
{
     "Enter a description",
     "",
     "",
     "",
     "",
     "",
     "",
     "",
     "Go back",
     "Done   ",
     "Exit   "
};

const char *desc_key[] =
{
     "Numpad explores description",
     "  ^8 up",
     "<4  >6 left and right",
     "  v2 down"
};

const char *menu_size[] =
{
     " Select Map Size ",
     "      51x101     ",
     "     101x201     ",
     "     151x301     ",
     "     251x501     ",
     "     451x901     ",
     "   Custom size   ",
     "     Go back     "
};

const char *menu_csize[] =
{
     "Enter height#",
     "Enter width #"
};

const char *menu_cexpr[] =
{
     "Would you like to modify",
     "Color expressions ex: @r",
     "           Yes          ",
     "           NO           ",
};

const char *menu_flight[] =
{
     "Foreground Light",
     "Black  #",
     "Red    #",
     "Magenta#",
     "Cyan   #",
     "Blue   #",
     "Green  #",
     "White  #",
     "Yellow #"
};

const char *menu_fbright[] =
{
     "Foreground Bright",
     "Black  #",
     "Red    #",
     "Magenta#",
     "Cyan   #",
     "Blue   #",
     "Green  #",
     "White  #",
     "Yellow #"
};

const char *menu_back[] =
{
     "Background Light",
     "Black  #",
     "Red    #",
     "Magenta#",
     "Cyan   #",
     "Blue   #",
     "Green  #",
     "White  #",
     "Yellow #"
};

 /* local cunftions */
void create_area(int xin, int yin, const char *path, const char *name, const char *description);
void print_menu(WINDOW *menu_win, const char **menu, int highlight, short int n_choices);
void color_ini();

int main(int argc, char **argv)
{
     bool quit = false;

     char name[100];
     char name_box_disp[20];
     char desc[10000];
     char area_map[502][1002];
     area_map[501][1001] = 'Q';
     char color_map[502][1002];
     color_map[501][1001] = 'Q';
     char bg_map[502][1002];
     bg_map[501][1001] = 'Q';
     
     short int y = 0x00, x = 0x00;
     short int scrmaxh, scrmaxw;
     short int mapmaxh, mapmaxw;
     short int menumaxh, menumaxw;
     short int state = MENU_MAIN;
     short int highlight = 0x00;
     short int selection = 0x00;
     short int cursory, cursorx;
     short int name_pos = 0x00;
     short int place = 0x00;
     
     int C = 0x00;

     /* initialize your non-curses data structures here                      */
     scrn = initscr();        /* initialize the curses library               */
     clear();                 /* Clears windows on refresh with new values   */
     noecho();                /* Do not echo input                           */
     cbreak();                /* Line buffering disabled. pass on everything */
     keypad(scrn, TRUE);      /* setup wgetch and getch functions            */
     curs_set(0);             /* Hide the cursor untill we start editing     */
     raw();
     //nodelay(scrn, TRUE);   /* wget and getch don't delay loop             */

     color_ini();
     refresh();
     
     // getyx(win, int, int cursor position in current window.
     getmaxyx(scrn, scrmaxh, scrmaxw);
     map = newwin(25, 50, 1, 1);
     getmaxyx(map, mapmaxh, mapmaxw);
     menu = newwin(2, 18, 1, mapmaxw + 2);
     getmaxyx(menu, menumaxh, menumaxw);
     key = newwin(4, 27, 1, mapmaxw + menumaxw + 3);
     
     name_box = newwin(3, 22, 2, mapmaxw + 2);
     box(name_box, 0, 0);

     desc_box = newwin( 7, 22, 2, mapmaxw + 2);
     box(desc_box, 0, 0);
     
     for( y = 0; y < mapmaxh; y++)
          for( x = 0; x < mapmaxw; x++)
               if( y == 0)
                    if(x == 0 || x == (mapmaxw - 1)){
                         wattron(map, COLOR_PAIR(3));
                         mvwprintw(map, y, x, "+");
                         wattroff(map, COLOR_PAIR(3));
                    } else {
                         wattron(map, COLOR_PAIR(3));
                         mvwprintw(map, y, x, "-");
                         wattroff(map, COLOR_PAIR(3));
                    }
               else if ( y == (mapmaxh - 1))
                    if(x == 0 || x == (mapmaxw - 1)){
                         wattron(map, COLOR_PAIR(3));
                         mvwprintw(map, y, x, "+");
                         wattroff(map, COLOR_PAIR(3));
                    } else {
                         wattron(map, COLOR_PAIR(3));
                         mvwprintw(map, y, x, "-");
                         wattroff(map, COLOR_PAIR(3));
                    }
               else if ( x == 0 || x == (mapmaxw - 1)){
                    wattron(map, COLOR_PAIR(3));
                    mvwprintw(map, y, x, "|");
                    wattroff(map, COLOR_PAIR(3));
               } else
                    mvwprintw(map, y, x, "#");
     
     wrefresh(map);
     print_menu(menu, menu_main, 0, 2);

     while ( quit == false)
     {
          C = wgetch(scrn);
          getyx(scrn, cursory, cursorx);
          if ( state == MENU_NAME)
          {
               if ( cursorx > 72)
               {
                    cursorx--;
                    wmove(scrn, cursory, cursorx);
               }

               if ( C == 263 /* Backspace */)
               {
                    if ( name_pos > 0)
                         name_pos--;
                    
                    if ( cursorx == 71 && name_pos > 19)
                    {
                         cursorx++;
                         wmove(scrn, cursory, cursorx);
                    }
                    place = name_pos;
               }
                    
               if( C == KEY_LEFT && name_pos > 0)
               {
                    name_pos--;
                    
                    if ( name_pos < 19)
                    {
                         cursorx--;
                         wmove(scrn, cursory, cursorx);
                    }
               }
               
               if( C == KEY_RIGHT && name_pos  < 100 && name_pos < place)
               {
                    name_pos++;

                    if ( name_pos < 20)
                    {
                         cursorx++;
                         wmove(scrn, cursory, cursorx);
                    }
               }
               
               if ( C > 20 && C < 126 && name_pos < 100)
               {
                    name[name_pos++] = C;
                    if ( place < name_pos)
                         place = name_pos;
               }
               
               if ( name_pos >= place && name_pos < 100 && place != 100)
                    name[name_pos] = 0x00;

               int x, y;
               y = name_pos;
               for ( x = 0; x < 20; x++)
               {
                    if ( name_pos >= 20)
                    {
                         name_box_disp[x] = name[y - 19];
                         y++;
                    }
                    else
                         name_box_disp[x] = name[x];
                    if( name_box_disp[x] == 0x00)
                       break;
               }
                    
               mvwprintw(name_box, 1, 1, "%-20s", name_box_disp);
               //box(name_box, 0, 0);
               wrefresh(name_box);
               move(cursory, cursorx);
          }

          switch ( C )
          {
               case KEY_UP:
                    switch(state)
                    {
                         case MENU_MAIN:
                              if (highlight == 0)
                                   break;
                              else
                              {
                                   highlight--;
                                   selection = highlight;
                                   print_menu(menu, menu_main, highlight, 2);
                                   
                              }
                              break;
                         case MENU_NAME:
                              if ( highlight == 4 )
                              {
                                   curs_set(1);
                                   highlight = -1;
                                   selection = -1;
                                   wmove(scrn, cursory, cursorx);
                                   print_menu(menu, menu_name, highlight, 5);
                                   wrefresh(menu);
                                   wrefresh(name_box);
                              }
                              break;
                         case MENU_DESC:
                              if ( highlight == 8 )
                              {
                                   curs_set(1);
                                   highlight = -1;
                                   selection = -1;
                                   wmove(scrn, cursory, cursorx);
                                   print_menu(menu, menu_desc, highlight, 11);
                                   wrefresh(menu);
                                   wrefresh(desc_box);
                              }
                              if ( highlight == 9 )
                              {
                                   highlight = 8;
                                   selection = 8;
                                   print_menu(menu, menu_desc, highlight, 11);
                                   wrefresh(menu);
                                   wrefresh(desc_box);
                              }
                              if ( highlight == 10 )
                              {
                                   highlight = 9;
                                   selection = 9;
                                   print_menu(menu, menu_desc, highlight, 11);
                                   wrefresh(menu);
                                   wrefresh(desc_box);
                              }
                              break;
                         case MENU_SIZE:
                              break;
                         case MENU_CSIZE:
                              break;
                         case MENU_CEXPR:
                              break;
                         case MENU_FLIGHT:
                              break;
                         case MENU_FBRIGHT:
                              break;
                         case MENU_BACK:
                              break;
                         case CREATING:
                              break;
                         default:
                              break;
                    }
                    break;
               case KEY_DOWN:
                    switch(state)
                    {
                         case MENU_MAIN:
                                   if (highlight == 1)
                                        break;
                                   else
                                   {
                                        highlight++;
                                        selection = highlight;
                                        print_menu(menu, menu_main, highlight, 2);
                                   }
                              break;
                         case MENU_NAME:
                                   if ( highlight == -1)
                                   {
                                        curs_set(0);
                                        highlight = 4;
                                        selection = 4;
                                        print_menu(menu, menu_name, highlight, 5);
                                        wrefresh(menu);
                                        wrefresh(name_box);
                                   }
                              break;
                         case MENU_DESC:
                                   if ( highlight == -1)
                                   {
                                        curs_set(0);
                                        highlight = 8;
                                        selection = 8;
                                        print_menu(menu, menu_desc, highlight, 11);
                                        wrefresh(menu);
                                        wrefresh(desc_box);
                                   }
                                   if ( highlight == 8)
                                   {
                                        highlight = 9;
                                        selection = 9;
                                        print_menu(menu, menu_desc, highlight, 11);
                                        wrefresh(menu);
                                        wrefresh(desc_box);
                                   }
                                   if ( highlight == 9)
                                   {
                                        highlight = 10;
                                        selection = 10;
                                        print_menu(menu, menu_desc, highlight, 11);
                                        wrefresh(menu);
                                        wrefresh(desc_box);
                                   }
                              break;
                         case MENU_SIZE:
                              break;
                         case MENU_CSIZE:
                              break;
                         case MENU_CEXPR:
                              break;
                         case MENU_FLIGHT:
                              break;
                         case MENU_FBRIGHT:
                              break;
                         case MENU_BACK:
                              break;
                         case CREATING:
                              break;
                         default:
                              break;
                    }
                    break;
               case KEY_LEFT:
                    switch(state)
                    {
                         case MENU_NAME:
                              break;
                         case MENU_DESC:
                              break;
                         case MENU_FLIGHT:
                              break;
                         case MENU_FBRIGHT:
                              break;
                         case MENU_BACK:
                              break;
                         case CREATING:
                              break;
                         default:
                              break;
                    }
                    break;
               case KEY_RIGHT:
                    switch(state)
                    {
                         case MENU_NAME:
                              break;
                         case MENU_DESC:
                              break;
                         case MENU_FLIGHT:
                              break;
                         case MENU_FBRIGHT:
                              break;
                         case MENU_BACK:
                              break;
                         case CREATING:
                              break;
                         default:
                              break;
                    }
                    break;
               case 0x0A: //ENTER WAS PRESSEd
                    switch(state)
                    {
                         case MENU_MAIN:
                              if ( selection == 0)
                              {
                                   wclear(menu);
                                   wrefresh(menu);
                                   
                                   state = MENU_NAME;

                                   wresize(menu, 5, 16);
                                   print_menu(menu, menu_name, highlight = -1, 5);
                                   wrefresh(menu);

                                   curs_set(1);
                                   echo();
                                   move(3, mapmaxw + 3);
                                   
                                   wrefresh(name_box);

                                   highlight = -1;
                                   selection = -1;
                              }
                              else
                                   quit = true;
                              break;
                         case MENU_NAME:
                                   if ( selection == 4)
                                        quit = true;

                                   highlight = -1;
                                   selection = -1;
                                   
                                   wclear(menu);
                                   wrefresh(menu);
                                   
                                   state = MENU_DESC;

                                   wresize(menu, 11, 19);
                                   print_menu(menu, menu_desc, highlight, 11);
                                   wrefresh(menu);

                                   move(3, mapmaxw + 3);
                                   wrefresh(desc_box);
                              break;
                         case MENU_DESC:
                                   if ( selection == 8)
                                        state = MENU_NAME;
                                   if ( selection == 9)
                                        state = MENU_SIZE;
                                   if ( selection == 10)
                                        quit = true;
                              break;
                         case MENU_SIZE:
                              break;
                         case MENU_CSIZE:
                              break;
                         case MENU_CEXPR:
                              break;
                         case MENU_FLIGHT:
                              break;
                         case MENU_FBRIGHT:
                              break;
                         case MENU_BACK:
                              break;
                         default:
                              break;
                    }
                    break;
               case 3:
                    quit = true;
                    break;
               default:
                    break;
          }
     }

     /* do your non-curses wrapup here */
     clear();
     refresh();
     endwin();
     if ( name )
          printf("Name: %s\r\nNumber[%d]\r\n", name, name_pos);
     exit(0);
}

void print_menu(WINDOW *menu_win, const char **menu, int highlight, short int n_choices)
{
     short int y, x, i;

     y = 0;
     x = 0;
     for(i = 0; i < n_choices; ++i)
     {    if(highlight == i) /* High light the present choice */
          {    wattron(menu_win, A_REVERSE);
               mvwprintw(menu_win, y, x, "%s", menu[i]);
               wattroff(menu_win, A_REVERSE);
          }
          else
               mvwprintw(menu_win, y, x, "%s", menu[i]);
          ++y;
     }
     wrefresh(menu_win);
}

void create_area(int yin, int xin, const char *path, const char *name, const char *description)
{
     FILE *fp;

     int x, y;

     if ((fp = fopen(path, "w")) == NULL)
          printf("Error opening %s\r\n", path);

     fwrite(name, 1, strlen(name), fp); fwrite("\n", 1, 1, fp);
     fwrite(description, 1, strlen(description), fp);
     fwrite("~\n", 1, 2, fp);

     for ( y = 0; y < yin && y <= SHRT_MAX - 1; y++)
     {
          for ( x = 0; x < xin && x <= SHRT_MAX - 1; x++)
          {
               if ( y == ( yin - 1) && x == (xin - 1))
                    fwrite("Q", 1, 1, fp);
               else
                    fwrite(".", 1, 1, fp);

          }
          if ( y < (yin - 1))
               fwrite("\n", 1, 1, fp);
     }
     fclose(fp);
}

void color_ini()
{
     if (has_colors())
     {
          start_color();

          /*
           * Simple color assignment, often all we need.  Color pair 0 cannot
           * be redefined.  This example uses the same value for the color
           * pair as for the foreground color, though of course that is not
           * necessary:
           */

          init_pair(0, COLOR_WHITE,   COLOR_BLACK);
          init_pair(1, COLOR_RED,     COLOR_BLACK);
          init_pair(2, COLOR_GREEN,   COLOR_BLACK);
          init_pair(3, COLOR_YELLOW,  COLOR_BLACK);
          init_pair(4, COLOR_BLUE,    COLOR_BLACK);
          init_pair(5, COLOR_CYAN,    COLOR_BLACK);
          init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
          init_pair(7, COLOR_BLACK,   COLOR_BLACK);

          init_pair(8,  COLOR_WHITE,   COLOR_WHITE);
          init_pair(9,  COLOR_RED,     COLOR_WHITE);
          init_pair(10, COLOR_GREEN,   COLOR_WHITE);
          init_pair(11, COLOR_YELLOW,  COLOR_WHITE);
          init_pair(12, COLOR_BLUE,    COLOR_WHITE);
          init_pair(13, COLOR_CYAN,    COLOR_WHITE);
          init_pair(14, COLOR_MAGENTA, COLOR_WHITE);
          init_pair(15, COLOR_BLACK,   COLOR_WHITE);

          init_pair(16, COLOR_WHITE,   COLOR_RED);
          init_pair(17, COLOR_RED,     COLOR_RED);
          init_pair(18, COLOR_GREEN,   COLOR_RED);
          init_pair(19, COLOR_YELLOW,  COLOR_RED);
          init_pair(20, COLOR_BLUE,    COLOR_RED);
          init_pair(21, COLOR_CYAN,    COLOR_RED);
          init_pair(22, COLOR_MAGENTA, COLOR_RED);
          init_pair(23, COLOR_BLACK,   COLOR_RED);

          init_pair(24, COLOR_WHITE,   COLOR_GREEN);
          init_pair(25, COLOR_RED,     COLOR_GREEN);
          init_pair(26, COLOR_GREEN,   COLOR_GREEN);
          init_pair(27, COLOR_YELLOW,  COLOR_GREEN);
          init_pair(28, COLOR_BLUE,    COLOR_GREEN);
          init_pair(29, COLOR_CYAN,    COLOR_GREEN);
          init_pair(30, COLOR_MAGENTA, COLOR_GREEN);
          init_pair(31, COLOR_BLACK,   COLOR_GREEN);

          init_pair(32, COLOR_WHITE,   COLOR_YELLOW);
          init_pair(33, COLOR_RED,     COLOR_YELLOW);
          init_pair(34, COLOR_GREEN,   COLOR_YELLOW);
          init_pair(35, COLOR_YELLOW,  COLOR_YELLOW);
          init_pair(36, COLOR_BLUE,    COLOR_YELLOW);
          init_pair(37, COLOR_CYAN,    COLOR_YELLOW);
          init_pair(38, COLOR_MAGENTA, COLOR_YELLOW);
          init_pair(39, COLOR_BLACK,   COLOR_YELLOW);

          init_pair(40, COLOR_WHITE,   COLOR_BLUE);
          init_pair(41, COLOR_RED,     COLOR_BLUE);
          init_pair(42, COLOR_GREEN,   COLOR_BLUE);
          init_pair(43, COLOR_YELLOW,  COLOR_BLUE);
          init_pair(44, COLOR_BLUE,    COLOR_BLUE);
          init_pair(45, COLOR_CYAN,    COLOR_BLUE);
          init_pair(46, COLOR_MAGENTA, COLOR_BLUE);
          init_pair(47, COLOR_BLACK,   COLOR_BLUE);

          init_pair(48, COLOR_WHITE,   COLOR_CYAN);
          init_pair(49, COLOR_RED,     COLOR_CYAN);
          init_pair(50, COLOR_GREEN,   COLOR_CYAN);
          init_pair(51, COLOR_YELLOW,  COLOR_CYAN);
          init_pair(52, COLOR_BLUE,    COLOR_CYAN);
          init_pair(53, COLOR_CYAN,    COLOR_CYAN);
          init_pair(54, COLOR_MAGENTA, COLOR_CYAN);
          init_pair(55, COLOR_BLACK,   COLOR_CYAN);

          init_pair(56, COLOR_WHITE,   COLOR_MAGENTA);
          init_pair(57, COLOR_RED,     COLOR_MAGENTA);
          init_pair(58, COLOR_GREEN,   COLOR_MAGENTA);
          init_pair(59, COLOR_YELLOW,  COLOR_MAGENTA);
          init_pair(60, COLOR_BLUE,    COLOR_MAGENTA);
          init_pair(61, COLOR_CYAN,    COLOR_MAGENTA);
          init_pair(62, COLOR_MAGENTA, COLOR_MAGENTA);
          init_pair(63, COLOR_BLACK,   COLOR_MAGENTA);
     }
}

Last edited by errigour; 10-07-2012 at 10:41 AM.
 
Old 10-06-2012, 10:18 PM   #2
errigour
Member
 
Registered: May 2009
Posts: 278

Original Poster
Rep: Reputation: 5
Just wanna point out that im not really concerned about the enter key during the description menu editing

Also if i wasn't thorough enough the, when entering a description and pressing keydown it should go to the ninth string in the description array but it just skips to the exit string and the keyup works fine. ctrl+c exits. it's in raw mode so that should be right.

Last edited by errigour; 10-06-2012 at 11:45 PM.
 
Old 01-24-2013, 09:36 AM   #3
errigour
Member
 
Registered: May 2009
Posts: 278

Original Poster
Rep: Reputation: 5
Ok I have found the answer to my problem. It's Not an ncurses bug. haha solved.
Code:
               if ( state == 0 )
               {
                    if ( selection == 1 )
                         selection = 2;
                    if ( selection == 2 )
                         selection = 3;
               }
Should have been:
Code:
               if ( state == 0 )
               {
                    if ( selection == 1 )
                         selection = 2;
                    else if ( selection == 2 )
                         selection = 3;
               }
Selection was always three.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with a program - ncurses hamedhsn Programming 3 01-24-2011 08:31 PM
how to print text in color by ncurses without opening a ncurses window Greatrebel Programming 0 12-20-2006 10:15 AM
ncurses program gayatri Linux - Software 0 05-03-2004 10:07 AM
compiling program that uses ncurses calble Programming 4 08-10-2003 05:30 PM
ncurses-5.2-28 conflicts with file from package ncurses-5.2-12 tubby Linux - Software 4 06-16-2002 01:00 AM


All times are GMT -5. The time now is 03:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration