1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/* text.c: simple text entry widget
Copyright (c) 2002-2005 Philip Kendall
$Id: text.c 4103 2009-11-21 10:16:36Z fredm $
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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <string.h>
#include <ctype.h>
#include "widget_internals.h"
char *widget_text_text = NULL; /* What we return the text in */
static const char *title; /* The window title */
static widget_text_input_allow allow;
#define WIDGET_TEXT_LENGTH 64
static char text[WIDGET_TEXT_LENGTH]; /* The current entry text */
static int widget_text_draw_text( void );
static void delete_character( void );
static void append_character( char c );
int
widget_text_draw( void *data )
{
widget_text_t* text_data = data;
if( data ) {
title = text_data->title;
allow = text_data->allow;
snprintf( text, sizeof( text ), "%s", text_data->text );
}
widget_dialog_with_border( 1, 2, 30, 3 );
widget_printstring( 10, 16, WIDGET_COLOUR_TITLE, title );
widget_printstring_right( 12, 28, 5, "[" );
widget_printstring( 244, 28, 5, "]" );
widget_display_lines( 2, 3 );
return widget_text_draw_text();
}
static int
widget_text_draw_text( void )
{
int width;
const char *tptr;
widget_rectangle( 12, 28, 232, 8, WIDGET_COLOUR_BACKGROUND );
tptr = text - 1;
do {
width = widget_stringwidth (++tptr);
} while (width > 28 * 8 - 4);
if( tptr != text )
widget_rectangle( 14, 29, 1, 6, 5 );
widget_printstring( 16, 28, WIDGET_COLOUR_FOREGROUND, tptr );
widget_rectangle( 17 + width, 35, 4, 1, 5 );
widget_display_rasters( 28, 8 );
return 0;
}
void
widget_text_keyhandler( input_key key )
{
switch( key ) {
case INPUT_KEY_BackSpace: /* Backspace generates DEL which is Caps + 0 */
delete_character(); widget_text_draw_text();
return;
case INPUT_KEY_Escape:
widget_end_widget( WIDGET_FINISHED_CANCEL );
return;
case INPUT_KEY_Return:
case INPUT_KEY_KP_Enter:
widget_end_widget( WIDGET_FINISHED_OK );
return;
default: /* Keep gcc happy */
break;
}
/* Input validation.
* We rely on various INPUT_KEY_* being mapped directly onto ASCII.
*/
/* FIXME: we *don't* want keypresses filtered through the input layer */
/* First, return if the character isn't printable ASCII. */
if( key < ' ' || key > '~' ) return;
/* Return if the key isn't valid. */
switch( allow ) {
case WIDGET_INPUT_ASCII:
break;
case WIDGET_INPUT_DIGIT:
if( !isdigit( key ) ) return;
break;
case WIDGET_INPUT_ALPHA:
if( !isalpha( key ) ) return;
break;
case WIDGET_INPUT_ALNUM:
if( !isdigit( key ) && !isalpha( key ) ) return;
break;
}
/* If we've got this far, we have a valid key */
append_character( key );
widget_text_draw_text();
}
static void
delete_character( void )
{
size_t length = strlen( text );
if( length ) text[ length - 1 ] = '\0';
}
static void
append_character( char c )
{
size_t length = strlen( text );
if( length < 23 ) {
text[ length ] = c; text[ length + 1 ] = '\0';
}
}
int
widget_text_finish( widget_finish_state finished )
{
if( finished == WIDGET_FINISHED_OK ) {
widget_text_text = realloc( widget_text_text, strlen( text ) + 1 );
if( !widget_text_text ) {
ui_error( UI_ERROR_ERROR, "Out of memory at %s:%d", __FILE__, __LINE__ );
return 1;
}
strcpy( widget_text_text, text );
} else {
free( widget_text_text );
widget_text_text = NULL;
}
return 0;
}
|