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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*
* This file is part of the XForms library package.
*
* XForms is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1, or
* (at your option) any later version.
*
* XForms 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with XForms. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file goodie_choice.c
*
* This file is part of the XForms library package.
* Copyright (c) 1996-2002 T.C. Zhao and Mark Overmars
* All rights reserved.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "include/forms.h"
#include "flinternal.h"
#include "private/flsnprintf.h"
/****************** Make a choice ***********************{**/
typedef struct
{
FL_FORM * form;
FL_OBJECT * str;
FL_OBJECT * but[ 3 ];
const char * sc[ 3 ];
} FD_choice;
static FD_choice *fd_choice;
static int default_choice;
/***************************************
***************************************/
static FD_choice *
create_choice( void )
{
FD_choice *fdui = fl_malloc( sizeof *fdui );
int oldy = fli_inverted_y;
int oldu = fl_get_coordunit();
fli_inverted_y = 0;
fl_set_coordunit( FL_COORD_PIXEL );
fdui->form = fl_bgn_form( FL_UP_BOX, 460, 130 );
fl_set_form_title( fdui->form, "Choice" );
fdui->str = fl_add_box( FL_FLAT_BOX, 20, 15, 420, 65, "" );
fdui->but[ 0 ] = fl_add_button( FL_NORMAL_BUTTON, 40, 93, 90, 27, "" );
fdui->but[ 1 ] = fl_add_button( FL_NORMAL_BUTTON, 185, 93, 90, 27, "" );
fdui->but[ 2 ] = fl_add_button( FL_NORMAL_BUTTON, 330, 93, 90, 27, "" );
fdui->sc[ 0 ] = fl_strdup( "1" );
fdui->sc[ 1 ] = fl_strdup( "2" );
fdui->sc[ 2 ] = fl_strdup( "3" );
fl_end_form( );
fli_inverted_y = oldy;
fl_set_coordunit( oldu );
return fdui;
}
/***************************************
***************************************/
int
fl_show_choices( const char * msg,
int numb,
const char * c0,
const char * c1,
const char * c2,
int def )
{
FL_OBJECT *retobj;
const char *c[ ] = { c0, c1, c2 };
int i;
if ( ! fd_choice )
fd_choice = create_choice( );
fli_handle_goodie_font( fd_choice->but[ 0 ], fd_choice->but[ 1 ] );
fli_handle_goodie_font( fd_choice->but[ 2 ], fd_choice->str );
fl_set_object_label( fd_choice->str, msg );
fl_hide_object( fd_choice->but[ 0 ] );
fl_hide_object( fd_choice->but[ 1 ] );
fl_hide_object( fd_choice->but[ 2 ] );
default_choice = def;
switch ( numb )
{
case 3:
for ( i = 0; i < 3; i++ )
{
fl_set_object_label( fd_choice->but[ i ], c[ i ] );
fl_set_object_shortcut( fd_choice->but[ i ],
fd_choice->sc[ i ], 1 );
fl_show_object( fd_choice->but[ i ] );
fl_fit_object_label( fd_choice->but[ i ], 1, 1 );
}
break;
case 2:
/* pick button 0 and 2 */
fl_set_object_label( fd_choice->but[ 0 ], c[ 0 ] );
fl_set_object_shortcut( fd_choice->but[ 0 ],
fd_choice->sc[ 0 ], 1 );
fl_show_object( fd_choice->but[ 0 ] );
fl_fit_object_label( fd_choice->but[ 0 ], 1, 1 );
fl_set_object_label( fd_choice->but[ 2 ], c[ 1 ] );
fl_set_object_shortcut( fd_choice->but[ 2 ],
fd_choice->sc[ 2 ], 1 );
fl_show_object( fd_choice->but[ 2 ] );
fl_fit_object_label( fd_choice->but[ 2 ], 1, 1 );
break;
case 1:
fl_set_object_label( fd_choice->but[ 0 ], c[ 0 ] );
fl_set_object_shortcut( fd_choice->but[ 0 ],
fd_choice->sc[ 0 ], 1 );
fl_show_object( fd_choice->but[ 0 ] );
fl_fit_object_label( fd_choice->but[ 0 ], 1, 1 );
break;
default:
return 0;
}
fli_get_goodie_title( fd_choice->form, FLChoiceTitle );
if ( ! fd_choice->form->visible )
fl_deactivate_all_forms( );
if ( def > 0 && def <= 3 )
fl_set_form_hotobject( fd_choice->form, fd_choice->but[ def - 1 ] );
else
fl_set_form_hotspot( fd_choice->form, -1, -1 );
fl_show_form( fd_choice->form, FL_PLACE_HOTSPOT, FL_TRANSIENT,
fd_choice->form->label );
fl_update_display( 0 );
retobj = fl_do_only_forms( );
fl_hide_form( fd_choice->form );
fl_activate_all_forms( );
return retobj == fd_choice->but[ 0 ] ?
1 : ( ( retobj == fd_choice->but[ 1 ] || numb == 2 ) ? 2 : 3);
}
/***************************************
***************************************/
int
fl_show_choice( const char * m1,
const char * m2,
const char * m3,
int numb,
const char * c1,
const char * c2,
const char * c3,
int def )
{
char *buf;
size_t len;
int ret;
len = ( m1 ? strlen( m1 ) : 0 ) + 1
+ ( m2 ? strlen( m2 ) : 0 ) + 1
+ ( m3 ? strlen( m3 ) : 0 ) + 1;
if ( len == 3 )
{
M_warn( "fl_show_choice", "Only NULL or empty strings" );
return 0;
}
buf = fl_malloc( len );
sprintf( buf, "%s\n%s\n%s",
m1 ? m1 : "", m2 ? m2 : "", m3 ? m3 : "" );
ret = fl_show_choices( buf, numb, c1, c2, c3, def );
fl_free( buf );
return ret;
}
/***************************************
***************************************/
void
fl_set_choices_shortcut( const char * a,
const char * b,
const char * c )
{
if ( ! fd_choice )
fd_choice = create_choice( );
if ( fd_choice->sc[ 0 ] )
fl_free( ( char * ) fd_choice->sc[ 0 ] );
fd_choice->sc[ 0 ] = ( a && *a ) ? fl_strdup( a ) : NULL;
if ( fd_choice->sc[ 1 ] )
fl_free( ( char * ) fd_choice->sc[ 1 ] );
fd_choice->sc[ 1 ] = ( b && *b ) ? fl_strdup( b ) : NULL;
if ( fd_choice->sc[ 2 ] )
fl_free( ( char * ) fd_choice->sc[ 2 ] );
fd_choice->sc[ 2 ] = ( c && *c ) ? fl_strdup( c ) : NULL;
}
/***************************************
***************************************/
void
fl_hide_choice( void )
{
if ( fd_choice && fd_choice->form->visible )
{
if ( default_choice <= 0 || default_choice > 3 )
default_choice = 1;
fl_trigger_object( fd_choice->but[ default_choice ] );
}
}
/***************************************
***************************************/
void
fli_choice_cleanup( void )
{
if ( ! fd_choice )
return;
if ( fd_choice->sc[ 0 ] )
fl_free( ( char * ) fd_choice->sc[ 0 ] );
if ( fd_choice->sc[ 1 ] )
fl_free( ( char * ) fd_choice->sc[ 1 ] );
if ( fd_choice->sc[ 2 ] )
fl_free( ( char * ) fd_choice->sc[ 2 ] );
fl_safe_free( fd_choice );
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|