note 43305 added to ref.ncurses

From: Date: Thu, 17 Jun 2004 04:27:00 +0000
Subject: note 43305 added to ref.ncurses
Groups: php.notes 
Request: Send a blank email to php-notes+get-71185@lists.php.net to get a copy of this message
An implementation of a scrolling selection box: <?php function ncurses_menu_select( $options, $values, $max_height = 7, $max_width = 20, $y = 2, $x = 2 ) { // Size inside of borders $height = $max_height - 2; $width = $max_width - 2; // Number of options $num_options = count( $options ); // Trim all values to fit foreach( $options as $key => $value ) { $options[ $key ] = substr( $value, 0, $width ); } // Create Window $menu_window = ncurses_newwin( $max_height, $max_width, $y, $x ); ncurses_wborder( $menu_window, 0, 0, 0, 0, 0, 0, 0, 0 ); // Initialize Window $current = 0; // Currently selected $position = 1; // Position in list $topitem = 0; // Top menu item for ( $a = 0; $a < min( $height, $num_options ); $a++ ) { if ( $a == $current ) { ncurses_wattron( $menu_window, NCURSES_A_REVERSE ); ncurses_mvwaddstr( $menu_window, 1 + $a, 1, $options[ $a ] ); ncurses_wattroff( $menu_window, NCURSES_A_REVERSE ); } else { ncurses_mvwaddstr( $menu_window, 1 + $a, 1, $options[ $a ] ); } } ncurses_mvwaddstr( $menu_window, 1, 0, '*' ); ncurses_wrefresh( $menu_window ); // Loop until a selection is made while( ! in_array( $key = ncurses_getch( $menu_window ), array( 13, 10 ) ) ) { if ( $key == NCURSES_KEY_UP && $current > 0 ) { $move = -1; } else if ( $key == NCURSES_KEY_DOWN && $current < $num_options - 1 ) { $move = 1; } else { continue; } $current += $move; $position += $move; // If we scroll off the window, redraw items. if ( $position < 1 || $position > $height ) { if ( $position < 1 ) { $position = 1; } else { $position = $height; } $topitem += $move; for ( $a = 1; $a <= $height; $a++ ) { ncurses_mvwaddstr( $menu_window, $a, 1, str_repeat( ' ', $width ) ); if ( $a == $position ) { ncurses_wattron( $menu_window, NCURSES_A_REVERSE ); ncurses_mvwaddstr( $menu_window, $a, 1, $options[ $topitem + $a - 1 ] ); ncurses_wattroff( $menu_window, NCURSES_A_REVERSE ); } else { ncurses_mvwaddstr( $menu_window, $a, 1, $options[ $topitem + $a - 1 ] ); } } } else { // Just update changed items ncurses_wattron( $menu_window, NCURSES_A_REVERSE ); ncurses_mvwaddstr( $menu_window, $position, 1, $options[ $current ] ); ncurses_wattroff( $menu_window, NCURSES_A_REVERSE ); ncurses_mvwaddstr( $menu_window, $position - $move, 1, $options[ $current - $move ] ); } // Update 'scroll bar dot' ncurses_wborder( $menu_window, 0, 0, 0, 0, 0, 0, 0, 0 ); $dot_position = round ( ( $current / $num_options ) * ( $height - 1 ) ); ncurses_mvwaddstr( $menu_window, 1 + $dot_position, 0, '*' ); ncurses_wrefresh( $menu_window ); } return $values[ $current ]; } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.ncurses.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43305 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43305&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43305&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71185) next »