xwords/xwords4/linux/cursesask.c
Eric House 5d53df9f0b tweak linux client to work with old test script
Included adding a new enum
2024-02-02 16:41:44 -08:00

152 lines
4.2 KiB
C

/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2000 by Eric House (xwords@eehouse.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* Put up a "dialog" with a question and tab-selectable buttons below.
* On <CR>, return the number of the button selected at the time.
*/
#ifdef PLATFORM_NCURSES
#include <ncurses.h>
#include "cursesask.h"
#include "cursesdlgutil.h"
/* Figure out how many lines there are and how wide the widest is.
*/
int
cursesask( WINDOW* parentWin, const char* question, short numButtons,
const char** buttons )
{
XP_LOGFF( "(question=%s, parentWin=%p)", question, parentWin );
XP_ASSERT( !!parentWin );
WINDOW* confWin;
int x, y, rows, row, nLines;
int left, top;
short newSelButton = 0;
short curSelButton = 1; /* force draw by being different */
short spacePerButton, num;
short maxWidth;
XP_Bool dismissed = XP_FALSE;
FormatInfo fi;
int len;
getmaxyx( parentWin, y, x);
getbegyx( parentWin, top, left );
measureAskText( question, x-2, &fi );
len = fi.maxLen;
if ( len < MIN_WIDTH ) {
len = MIN_WIDTH;
}
rows = fi.nLines;
maxWidth = x - (PAD*2) - 2; /* 2 for two borders */
if ( len > x-2 ) {
rows = (len / maxWidth) + 1;
len = maxWidth;
}
nLines = ASK_HEIGHT + rows - 1;
confWin = newwin( nLines, len+(PAD*2), top + ((y/2) - (nLines/2)),
left + ((x-len-2)/2) );
keypad( confWin, TRUE );
wclear( confWin );
box( confWin, '|', '-');
for ( row = 0; row < rows; ++row ) {
mvwaddnstr( confWin, row+1, PAD,
fi.line[row].substr, fi.line[row].len );
}
spacePerButton = (len+(PAD*2)) / (numButtons + 1);
while ( !dismissed ) {
int ch;
if ( newSelButton != curSelButton ) {
drawButtons( confWin, rows+1, spacePerButton, numButtons,
curSelButton=newSelButton, buttons );
}
ch = wgetch( confWin );
switch ( ch ) {
case 'L':
case KEY_RIGHT:
case 525:
newSelButton = (curSelButton+1) % numButtons;
break;
case 'H':
case '\t':
case KEY_LEFT:
case 524:
newSelButton = (numButtons+curSelButton-1) % numButtons;
break;
case EOF:
case 4: /* C-d */
case 27: /* ESC */
curSelButton = 0; /* should be the cancel case */
case KEY_B2: /* "center of keypad" */
case '\r':
case '\n':
dismissed = XP_TRUE;
break;
case '1':
case '2':
case '3':
case '4':
num = ch - '1';
if ( num < numButtons ) {
newSelButton = num;
}
break;
default:
beep();
}
}
delwin( confWin );
/* this leaves a ghost line, but I can't figure out a better way. */
wtouchln( parentWin, (y/2)-(nLines/2), ASK_HEIGHT + rows - 1, 1 );
wrefresh( parentWin );
return curSelButton;
} /* cursesask */
void
ca_inform( WINDOW* window, const char* message )
{
if ( !!window ) {
const char* buttons[] = { "Ok" };
(void)cursesask( window, message, VSIZE(buttons), buttons );
}
}
void
ca_informf( WINDOW* window, const char* fmt, ... )
{
va_list args;
va_start( args, fmt );
gchar* msg = g_strdup_vprintf( fmt, args );
va_end( args );
ca_inform( window, msg );
g_free( msg );
}
#endif /* PLATFORM_NCURSES */