home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / games / volume15 / reversi / part02 / menu.c < prev    next >
C/C++ Source or Header  |  1993-01-27  |  4KB  |  178 lines

  1. /*
  2.     reversi - play a game of reversi against the computer or a human
  3.     Copyright (C) 1992  Elias Martenson
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     Contact me by email at elias@proxxi.se
  20. */
  21.  
  22. /*
  23.  *  These menu routines is written by Stefan Rapp (rappen@proxxi.se)
  24.  *
  25.  */
  26.  
  27. #include <curses.h>
  28. #include "menu.h"
  29.  
  30. /*                      */
  31. /*  Start: 0 = First    */
  32. /*                      */
  33.  
  34. menu (x, y, width, height, start, flags, names)
  35. int x, y, width, height, start, flags;
  36. MenuEntry *names;
  37. {
  38.   MenuEntry *namesstart = names;
  39.   WINDOW *menu_window;
  40.   int max_width=0, number_names= 0, counter, names_select = FALSE;
  41.   char ch;
  42.   char *str;
  43.  
  44.   do{
  45.     if( strlen( names->name ) > max_width  ){ 
  46.       max_width = strlen(names->name);
  47.     }
  48.     number_names++;
  49.   } while((++names) -> name != NULL );
  50.  
  51.   if( height == 0){
  52.     height = number_names;
  53.   }
  54.  
  55.   if( width == 0){
  56.     width = max_width;
  57.   }
  58.   
  59.   names=namesstart;
  60.   names+=start;
  61.  
  62.   counter = 0;
  63.   
  64.   menu_window = newwin( height, width + 1, y, x );
  65.   if( ( flags & DRAW_BOX ) == DRAW_BOX ){
  66.     box( menu_window, DRAW_BOX_SYMBOL_VERT, DRAW_BOX_SYMBOL_HORIZ );
  67.  }
  68.   wclear( menu_window );
  69.   refresh_names( menu_window, 1, width, height, names+1);
  70.   wstandout( menu_window );
  71.   mvwaddstr(menu_window, 0, 0, (names)->name);
  72.   wstandend( menu_window );
  73.  
  74.   do{
  75.     wrefresh(menu_window);
  76.     ch = wgetch( menu_window );
  77.  
  78.     wstandend( menu_window );
  79.     mvwaddstr( menu_window, counter, 0, names-> name);
  80.         
  81.     switch( ch ){
  82.     case MV_UP:
  83.       if(names-namesstart > 0){
  84.     names--;
  85.     counter--;
  86.         if( counter < 0 ){    
  87.       counter = 0;
  88.       refresh_names( menu_window, 0, width, height, names);
  89.         }
  90.       }
  91.       else if( (flags & WRAP) == WRAP ){
  92.     names = namesstart;
  93.     names += number_names -1;
  94.         counter = height -1;
  95.     refresh_names( menu_window, 0, width, height, names - (height -1));
  96.       }
  97.       break;
  98.     case MV_DOWN:
  99.       if( (names+1)->name != NULL){
  100.     names++;
  101.     counter++;
  102.         if( counter >= height ){
  103.       counter = height -1;
  104.       refresh_names( menu_window, 0, width, height, names - (height -1));
  105.     }
  106.       }
  107.       else if(( flags & WRAP) == WRAP ){
  108.     names = namesstart;
  109.     counter = 0;
  110.     refresh_names( menu_window, 0, width, height, names);
  111.       }
  112.       break;
  113.     case PG_UP:
  114.       break;
  115.     case PG_DOWN:
  116.       break;
  117.     case SELECT:
  118.       names_select = TRUE;
  119.       break;
  120.     case QUIT:
  121.       if( (flags & ENABLE_QUIT) == ENABLE_QUIT){
  122.     names_select = -1;
  123.       }
  124.       break;
  125.     case HELP:
  126.       if( (flags & ENABLE_HELP) == ENABLE_HELP){
  127.     /* help command */
  128.       }
  129.       break;
  130.     } /* switch */
  131.     
  132.     wstandout( menu_window);
  133.     mvwaddstr( menu_window, counter, 0, (names -> name ));
  134.   } while( names_select == FALSE);
  135.   
  136.   if( (flags & CLEAR_ON_EXIT) == CLEAR_ON_EXIT ){
  137.     wclear( menu_window );
  138.     wrefresh( menu_window );
  139.   }
  140.   delwin( menu_window );
  141.   
  142.   if( names_select == -1){
  143.     return(-1);
  144.   }
  145.   else{
  146.     return(names - namesstart);
  147.   }
  148. }   
  149.        
  150.  
  151. refresh_names( menu_window, y, width, height,  names)
  152. WINDOW *menu_window;
  153. MenuEntry *names;
  154. int width, height, y;
  155. {
  156.   int counter;
  157.   
  158.   for( counter = y; ( counter < height  ) && ( names -> name != NULL ); counter++ ){
  159.     mvwaddstr( menu_window, counter , 0 , (names) -> name );
  160.     if( strlen( names -> name) < width){
  161.       write_space( menu_window, width - strlen(names->name));
  162.       write_space( menu_window, 1);
  163.     }
  164.     names++;
  165.   }
  166. }
  167.  
  168. write_space( menu_window, spaces)
  169. WINDOW *menu_window;
  170. int spaces;
  171. {
  172.   int counter;
  173.   
  174.   for( counter = 0; counter < spaces; counter++ ){
  175.     waddch( menu_window, ' ' );
  176.   }
  177. }
  178.