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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/***************************************************************************
value.c - description
-------------------
begin : Sun Sep 23 2001
copyright : (C) 2001 by Michael Speck
email : kulkanie@gmx.net
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include <ctype.h>
#include "manager.h"
/*
====================================================================
Locals
====================================================================
*/
/*
====================================================================
Update val_str if not ITEM_VALUE
====================================================================
*/
void value_update_str( Value *value )
{
switch ( value->type ) {
case VALUE_RANGE_INT: sprintf( value->val_str, "%i", *value->val_int ); break;
case VALUE_RANGE_STR: sprintf( value->val_str, "%s", value->names[*value->val_int] ); break;
case VALUE_KEY:
if ( value->grab ) { strcpy( value->val_str, "???" ); break; }
switch ( *value->val_int ) {
case SDLK_SPACE: strcpy( value->val_str, _("Space") ); break;
case SDLK_LEFT: strcpy( value->val_str, _("Left") ); break;
case SDLK_RIGHT: strcpy( value->val_str, _("Right") ); break;
case SDLK_UP: strcpy( value->val_str, _("Up") ); break;
case SDLK_DOWN: strcpy( value->val_str, _("Down") ); break;
case SDLK_RSHIFT: strcpy( value->val_str, _("Right Shift") ); break;
case SDLK_LSHIFT: strcpy( value->val_str, _("Left Shift") ); break;
case SDLK_RCTRL: strcpy( value->val_str, _("Right Ctrl") ); break;
case SDLK_LCTRL: strcpy( value->val_str, _("Left Ctrl") ); break;
case SDLK_RALT: strcpy( value->val_str, _("Right Alt") ); break;
case SDLK_LALT: strcpy( value->val_str, _("Left Alt") ); break;
case SDLK_INSERT: strcpy( value->val_str, _("Insert") ); break;
case SDLK_END: strcpy( value->val_str, _("End") ); break;
case SDLK_PAGEUP: strcpy( value->val_str, _("PageUp") ); break;
case SDLK_PAGEDOWN: strcpy( value->val_str, _("PageDown") ); break;
case SDLK_HOME: strcpy( value->val_str, _("Home") ); break;
default:
if ( *value->val_int >= SDLK_a && *value->val_int <= SDLK_z )
sprintf( value->val_str, "'%c'", (char)toupper( *value->val_int ) );
else
if ( *value->val_int >= SDLK_0 && *value->val_int <= SDLK_9 )
sprintf( value->val_str, "'%i'", *value->val_int - 48 );
else
if ( *value->val_int >= SDLK_KP0 && *value->val_int <= SDLK_KP9 )
sprintf( value->val_str, "'Pad %i'", *value->val_int - 256 );
else
sprintf( value->val_str, "%i", *value->val_int );
break;
}
break;
}
}
/*
====================================================================
Create basic value
====================================================================
*/
Value *value_create_basic( int type, int *val_int )
{
Value *value = calloc( 1, sizeof( Value ) );
value->type = type;
value->val_int = val_int;
/* if not VALUE_EDIT create val_str which carries translated val_int */
if ( type != VALUE_EDIT ) value->val_str = calloc( 256, sizeof( char ) );
return value;
}
/*
====================================================================
Publics
====================================================================
*/
/*
====================================================================
Auxiliary functions to setup a key filter.
====================================================================
*/
void filter_clear( int *filter )
{
memset( filter, 0, sizeof( int ) * SDLK_LAST );
}
void filter_set( int *filter, int first, int last, int allowed )
{
int i;
for ( i = first; i <= last; i++ )
filter[i] = allowed;
}
/*
====================================================================
Create a value. names and filter are duplicated and freed by
value_delete().
Return Value: value
====================================================================
*/
Value *value_create_empty()
{
return value_create_basic( VALUE_NONE, 0 );
}
Value *value_create_range_int( int *val_int, int min, int max, int step )
{
Value *value = value_create_basic( VALUE_RANGE_INT, val_int );
value->min = min; value->max = max; value->step = step;
value_update_str( value );
return value;
}
Value *value_create_range_str( int *val_int, char **names, int count )
{
int i;
Value *value = value_create_basic( VALUE_RANGE_STR, val_int );
value->min = 0; value->max = count - 1; value->step = 1;
/* duplicate names */
value->name_count = count;
value->names = calloc( count, sizeof( char* ) );
for ( i = 0; i < count; i++ )
value->names[i] = strdup( names[i] );
value_update_str( value );
return value;
}
Value *value_create_key( int *val_int, int *filter )
{
int i;
Value *value = value_create_basic( VALUE_KEY, val_int );
/* duplicate filter */
value->filter = calloc( SDLK_LAST, sizeof( int ) );
for ( i = 0; i < SDLK_LAST; i++ ) value->filter[i] = filter[i];
value_update_str( value );
/* list of other key values */
value->other_keys = list_create( LIST_NO_AUTO_DELETE, LIST_NO_CALLBACK );
return value;
}
Value *value_create_edit( char *val_str, int limit )
{
Value *value = value_create_basic( VALUE_EDIT, 0 );
value->val_str = val_str;
value->max = limit;
return value;
}
/*
====================================================================
Delete a value
====================================================================
*/
void value_delete( Value *value )
{
int i;
if ( !value ) return;
if ( value->filter ) free( value->filter );
if ( value->names ) {
for ( i = 0; i < value->name_count; i++ )
if ( value->names[i] ) free( value->names[i] );
free( value->names );
}
if ( value->type != VALUE_EDIT && value->val_str ) free( value->val_str );
if ( value->other_keys ) list_delete( value->other_keys );
free( value );
}
/*
====================================================================
Increase, decrease if range.
====================================================================
*/
void value_dec( Value *value )
{
*value->val_int -= value->step;
if ( *value->val_int < value->min ) *value->val_int = value->max;
value_update_str( value );
}
void value_inc( Value *value )
{
*value->val_int += value->step;
if ( *value->val_int > value->max ) *value->val_int = value->min;
value_update_str( value );
}
/*
====================================================================
Grab input of VALUE_KEY
====================================================================
*/
void value_grab( Value *value )
{
if ( value->type == VALUE_KEY ) {
value->grab = 1;
value_update_str( value );
}
}
/*
====================================================================
Set key value if VALUE_KEY and clear grab flag
====================================================================
*/
void value_set_key( Value *value, int val_int )
{
Value *other_key;
if ( !value->filter[val_int] ) return;
list_reset( value->other_keys );
while ( ( other_key = list_next( value->other_keys ) ) )
if ( *other_key->val_int == val_int ) return;
/* ok, set */
*value->val_int = val_int;
value->grab = 0;
value_update_str( value );
}
/*
====================================================================
Edit string if VALUE_EDIT
====================================================================
*/
void value_edit( Value *value, int code, int unicode )
{
int length = strlen( value->val_str );
if ( code == SDLK_BACKSPACE && length > 0 )
value->val_str[length - 1] = 0;
else
if ( code >= 32 && code < 128 && length < value->max )
value->val_str[length] = unicode;
}
/*
====================================================================
Add another dynamically restricted key (as VALUE_KEY) to other_key
list.
====================================================================
*/
void value_add_other_key( Value *value, Value *other_key )
{
if ( value->type != VALUE_KEY ) return;
list_add( value->other_keys, other_key );
}
/*
====================================================================
Set a new name list (and update position) for VALUE_RANGE_STR.
====================================================================
*/
void value_set_new_names( Value *value, char **names, int count )
{
int i;
if ( value->type != VALUE_RANGE_STR ) return;
if ( value->names ) {
for ( i = 0; i < value->name_count; i++ )
if ( value->names[i] ) free( value->names[i] );
free( value->names );
}
value->name_count = count;
value->max = count - 1;
if ( *value->val_int > value->max ) *value->val_int = 0;
value->names = calloc( count, sizeof( char* ) );
for ( i = 0; i < count; i++ )
value->names[i] = strdup( names[i] );
value_update_str( value );
}
|