mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
first checked in
This commit is contained in:
parent
b5f54608da
commit
f2766ec8e9
4 changed files with 352 additions and 0 deletions
77
xwords4/linux/cursesdlgutil.c
Normal file
77
xwords4/linux/cursesdlgutil.c
Normal file
|
@ -0,0 +1,77 @@
|
|||
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||
/*
|
||||
* Copyright 2000-2003 by Eric House (fixin@peak.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.
|
||||
*/
|
||||
|
||||
#ifdef PLATFORM_NCURSES
|
||||
|
||||
#include "cursesdlgutil.h"
|
||||
|
||||
void
|
||||
measureAskText( XP_UCHAR* question, FormatInfo* fip )
|
||||
{
|
||||
XP_U16 i;
|
||||
XP_U16 maxWidth = 0;
|
||||
XP_Bool done = XP_FALSE;
|
||||
|
||||
for ( i = 0; i < MAX_LINES && !done; ++i ) {
|
||||
XP_UCHAR* next = strstr( question, XP_CR );
|
||||
XP_U16 thisWidth;
|
||||
|
||||
fip->line[i].substr = question;
|
||||
|
||||
if ( !!next ) {
|
||||
thisWidth = next - question;
|
||||
} else {
|
||||
thisWidth = strlen(question);
|
||||
done = XP_TRUE;
|
||||
}
|
||||
fip->line[i].len = thisWidth;
|
||||
|
||||
if ( thisWidth > maxWidth ) {
|
||||
maxWidth = thisWidth;
|
||||
}
|
||||
|
||||
question = next + strlen(XP_CR);
|
||||
}
|
||||
|
||||
fip->nLines = i;
|
||||
fip->maxLen = maxWidth;
|
||||
} /* measureAskText */
|
||||
|
||||
void
|
||||
drawButtons( WINDOW* confWin, XP_U16 line, short spacePerButton,
|
||||
short numButtons, short curSelButton, char** button1 )
|
||||
{
|
||||
short i;
|
||||
for ( i = 0; i < numButtons; ++i ) {
|
||||
short len = strlen( *button1 );
|
||||
|
||||
if ( i == curSelButton ) {
|
||||
wstandout( confWin );
|
||||
}
|
||||
mvwprintw( confWin, line, ((i+1) * spacePerButton) - (len/2),
|
||||
"[%s]", *button1 );
|
||||
if ( i == curSelButton ) {
|
||||
wstandend( confWin );
|
||||
}
|
||||
++button1;
|
||||
}
|
||||
wrefresh( confWin );
|
||||
} /* drawButtons */
|
||||
|
||||
#endif
|
49
xwords4/linux/cursesdlgutil.h
Normal file
49
xwords4/linux/cursesdlgutil.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||
/*
|
||||
* Copyright 2000 by Eric House (fixin@peak.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.
|
||||
*/
|
||||
|
||||
#ifndef _CURSESDLGUTIL_H_
|
||||
#define _CURSESDLGUTIL_H_
|
||||
|
||||
#include "comtypes.h"
|
||||
#include "linuxmain.h"
|
||||
#include "cursesmain.h"
|
||||
|
||||
#define ASK_HEIGHT 5
|
||||
#define PAD 2
|
||||
#define MAX_LINES 15
|
||||
#define MIN_WIDTH 25
|
||||
|
||||
|
||||
|
||||
typedef struct FormatInfo {
|
||||
XP_U16 nLines;
|
||||
XP_U16 maxLen;
|
||||
struct {
|
||||
XP_UCHAR* substr;
|
||||
XP_U16 len;
|
||||
} line[MAX_LINES];
|
||||
} FormatInfo;
|
||||
|
||||
|
||||
void measureAskText( XP_UCHAR* question, FormatInfo* fip );
|
||||
|
||||
void drawButtons( WINDOW* confWin, XP_U16 line, short spacePerButton,
|
||||
short numButtons, short curSelButton, char** button1 );
|
||||
|
||||
#endif
|
196
xwords4/linux/cursesletterask.c
Normal file
196
xwords4/linux/cursesletterask.c
Normal file
|
@ -0,0 +1,196 @@
|
|||
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||
/*
|
||||
* Copyright 2003 by Eric House (fixin@peak.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.
|
||||
*/
|
||||
|
||||
#ifdef PLATFORM_NCURSES
|
||||
|
||||
#include "cursesletterask.h"
|
||||
#include "cursesdlgutil.h"
|
||||
|
||||
#define MAX_TILE_BUTTON_ROWS 10
|
||||
#define MAX_TILE_BUTTON_WIDTH (sizeof(XP_UCHAR4) + 2)
|
||||
|
||||
static void
|
||||
sizeTextsAsButtons( XP_UCHAR4* texts, XP_U16 maxLen, XP_U16 nTiles,
|
||||
XP_U16* textsCols, XP_U16* textsRows,
|
||||
XP_U16* textsOffsets )
|
||||
{
|
||||
XP_U16 nCols = maxLen / MAX_TILE_BUTTON_WIDTH;
|
||||
XP_U16 nRows = (nTiles + nCols - 1) / nCols;
|
||||
XP_U16 i;
|
||||
|
||||
*textsCols = nCols;
|
||||
*textsRows = nRows;
|
||||
|
||||
for ( i = 0; i < nRows; ++i ) {
|
||||
textsOffsets[i] = i * nCols;
|
||||
}
|
||||
|
||||
XP_DEBUGF( "broke %d letters into %d rows of %d cols",
|
||||
nTiles, nRows, nCols );
|
||||
} /* sizeTextsAsButtons */
|
||||
|
||||
XP_S16
|
||||
curses_askLetter( CursesAppGlobals* globals, XP_UCHAR* query,
|
||||
XP_UCHAR4* texts, XP_U16 nTiles )
|
||||
{
|
||||
XP_S16 result;
|
||||
WINDOW* confWin;
|
||||
int x, y, rows, row, nLines, i;
|
||||
short newSelButton = 0;
|
||||
short curSelButton = 1; /* force draw by being different */
|
||||
short maxWidth;
|
||||
short numCtlButtons;
|
||||
char* ctlButtons[] = { "Ok", "Cancel" };
|
||||
XP_Bool dismissed = XP_FALSE;
|
||||
FormatInfo fi;
|
||||
int len;
|
||||
XP_U16 textsCols, textsRows;
|
||||
XP_U16 textsOffsets[MAX_TILE_BUTTON_ROWS];
|
||||
XP_U16 spacePerCtlButton;
|
||||
char* textPtrs[MAX_UNIQUE_TILES];
|
||||
|
||||
for ( i = 0; i < nTiles; ++i ) {
|
||||
textPtrs[i] = (char*)&texts[i];
|
||||
}
|
||||
|
||||
getmaxyx(globals->boardWin, y, x);
|
||||
XP_DEBUGF( "getmaxyx=>x=%d,y=%d", x, y );
|
||||
|
||||
numCtlButtons = sizeof(ctlButtons) / sizeof(ctlButtons[0]);
|
||||
|
||||
measureAskText( query, &fi );
|
||||
|
||||
sizeTextsAsButtons( texts, x, nTiles,
|
||||
&textsCols, &textsRows, textsOffsets );
|
||||
|
||||
|
||||
len = XP_MAX( fi.maxLen, textsCols * MAX_TILE_BUTTON_WIDTH );
|
||||
if ( len < MIN_WIDTH ) {
|
||||
len = MIN_WIDTH;
|
||||
}
|
||||
|
||||
rows = fi.nLines + textsRows + 1;
|
||||
maxWidth = x - (PAD*2) - 2; /* 2 for two borders */
|
||||
XP_DEBUGF( "set maxWidth=%d", maxWidth );
|
||||
|
||||
|
||||
if ( len > x-2 ) {
|
||||
rows = (len / maxWidth) + 1;
|
||||
len = maxWidth;
|
||||
}
|
||||
|
||||
nLines = ASK_HEIGHT + rows - 1;
|
||||
XP_DEBUGF( "newwin( %d, %d, (%d/2) - (%d/2), (%d-%d-2)/2",
|
||||
nLines, len,//+(PAD*2),
|
||||
y, nLines, x, len );
|
||||
|
||||
XP_ASSERT( y >= nLines );
|
||||
confWin = newwin( nLines, len,//+(PAD*2),
|
||||
(y/2) - (nLines/2), (x-len-2)/2 );
|
||||
XP_ASSERT( !!confWin );
|
||||
|
||||
wclear( confWin );
|
||||
box( confWin, '|', '-');
|
||||
|
||||
for ( row = 0; row < fi.nLines; ++row ) {
|
||||
mvwaddnstr( confWin, row+1, PAD,
|
||||
fi.line[row].substr, fi.line[row].len );
|
||||
}
|
||||
|
||||
spacePerCtlButton = (len+(PAD*2)) / (numCtlButtons + 1);
|
||||
|
||||
result = newSelButton;
|
||||
|
||||
while ( !dismissed ) {
|
||||
int ch;
|
||||
|
||||
if ( newSelButton != curSelButton ) {
|
||||
|
||||
XP_U16 i;
|
||||
for ( i = 0; i < textsRows; ++i ) {
|
||||
XP_U16 nInRow = textsCols;
|
||||
|
||||
if ( i + 1 == textsRows ) {
|
||||
nInRow = nTiles % textsCols;
|
||||
if ( nInRow == 0 ) {
|
||||
nInRow = textsCols;
|
||||
}
|
||||
}
|
||||
|
||||
XP_DEBUGF( "printing %d cols, row %d, first char %s",
|
||||
nInRow, i, textPtrs[textsOffsets[i]] );
|
||||
drawButtons( confWin, rows + 2 - textsRows + i,
|
||||
MAX_TILE_BUTTON_WIDTH-1,
|
||||
nInRow,
|
||||
newSelButton - textsOffsets[i],
|
||||
(char**)&textPtrs[textsOffsets[i]] );
|
||||
}
|
||||
|
||||
|
||||
drawButtons( confWin, rows+2, spacePerCtlButton,
|
||||
numCtlButtons,
|
||||
newSelButton - nTiles, ctlButtons );
|
||||
|
||||
curSelButton = newSelButton;
|
||||
}
|
||||
|
||||
ch = fgetc( stdin );
|
||||
switch ( ch ) {
|
||||
case '\t':
|
||||
newSelButton = (curSelButton+1) % (numCtlButtons + nTiles);
|
||||
if ( newSelButton < nTiles ) {
|
||||
result = newSelButton;
|
||||
}
|
||||
break;
|
||||
case EOF:
|
||||
case 4: /* C-d */
|
||||
case 27: /* ESC */
|
||||
curSelButton = 0; /* should be the cancel case */
|
||||
case '\r':
|
||||
case '\n':
|
||||
dismissed = XP_TRUE;
|
||||
break;
|
||||
default:
|
||||
if ( ch >= 'a' && ch <= 'z' ) {
|
||||
ch += 'A' - 'a';
|
||||
}
|
||||
for ( i = 0; i < nTiles; ++i ) {
|
||||
if ( ch == texts[i][0] ) {
|
||||
XP_DEBUGF( "picking %c", (char)ch);
|
||||
newSelButton = i;
|
||||
result = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
XP_DEBUGF( "newSelButton=%d", newSelButton );
|
||||
|
||||
}
|
||||
delwin( confWin );
|
||||
|
||||
/* this leaves a ghost line, but I can't figure out a better way. */
|
||||
wtouchln( globals->boardWin, (y/2)-(nLines/2), ASK_HEIGHT + rows - 1, 1 );
|
||||
wrefresh( globals->boardWin );
|
||||
|
||||
return result;
|
||||
} /* curses_askLetter */
|
||||
|
||||
#endif /* PLATFORM_NCURSES */
|
30
xwords4/linux/cursesletterask.h
Normal file
30
xwords4/linux/cursesletterask.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||
/*
|
||||
* Copyright 2003 by Eric House (fixin@peak.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.
|
||||
*/
|
||||
|
||||
#ifndef _CURSESLETTERASK_H_
|
||||
#define _CURSESLETTERASK_H_
|
||||
|
||||
#include "linuxmain.h"
|
||||
#include "cursesmain.h"
|
||||
|
||||
XP_S16 curses_askLetter( CursesAppGlobals* globals, XP_UCHAR* query,
|
||||
XP_UCHAR4* texts, XP_U16 nTiles );
|
||||
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue