00001
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <ctype.h>
00016 #include <fcntl.h>
00017 #include <signal.h>
00018 #include <errno.h>
00019 #include <string.h>
00020 #ifndef WIN32
00021 #include <unistd.h>
00022 #include <sys/file.h>
00023 #include <sys/wait.h>
00024 #endif
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027
00028 #include "getcar.h"
00029 #include "sic_comm.h"
00030 #include "sic_launch.h"
00031 #ifdef WIN32
00032 #include "win-dialogs.h"
00033 #endif
00034
00035 #include "sic_xgag.h"
00036
00037 #ifndef SIGCLD
00038 #define SIGCLD SIGCHLD
00039 #endif
00040
00041 static char window_title[TITLELENGTH], helpfilename[HLPFILELNGTH];
00042
00043 static global_struct widgets[NSTRUCT], *last_widget = widgets;
00044 #ifdef WIN32
00045 extern char *dialoghelpname[MAX_NB_DIALOG];
00046 #endif
00047
00048 static void my_f2c_strncpy( char *dest, CFC_FString txt, size_t fsize, size_t max)
00049 {
00050
00051
00052
00053
00054
00055
00056
00057
00058 size_t n = (fsize < max) ? fsize : max - 1;
00059
00060 CFC_f2c_strcpy( dest, txt, (int)n);
00061 }
00062
00063
00064
00065
00066 static void my_fp2c_strncpy( char *dest, char *txt, size_t fsize, size_t max)
00067 {
00068 CFC_FString tmp;
00069
00070 tmp.value = txt;
00071 my_f2c_strncpy( dest, tmp, fsize, max);
00072 }
00073
00074 void CFC_API xgag_separator( int *win_id)
00075 {
00076 generic_struct *generic = &(last_widget++)->generic;
00077
00078 generic->window_id = *win_id;
00079 generic->type = SEPARATOR;
00080 }
00081
00082 global_struct *set_generic( int type, int win_id, CFC_FString variable, CFC_FString label)
00083 {
00084 global_struct *widget;
00085 generic_struct *gene;
00086 char variable_name[SICVLN];
00087
00088 my_f2c_strncpy( variable_name, variable, SICVLN, SICVLN);
00089 for (widget = widgets; widget < last_widget; widget++) {
00090 gene = &widget->generic;
00091 if (gene->type == type && !strcmp( gene->variable, variable_name) && gene->window_id == win_id)
00092 return widget;
00093 }
00094 gene = &last_widget->generic;
00095 gene->window_id = win_id;
00096 gene->type = type;
00097 strcpy( gene->variable, variable_name);
00098 my_f2c_strncpy( gene->label, label, LABELLENGTH, LABELLENGTH);
00099 return last_widget++;
00100 }
00101
00102 void CFC_API xgag_logic( CFC_FString variable, CFC_FString txt, int *flag, int *win_id)
00103 {
00104 logic_struct *logic = &set_generic( LOGIC, *win_id, variable, txt)->logic;
00105
00106 logic->logic = flag;
00107 logic->userlogic = (*flag == 1);
00108 }
00109
00110 void CFC_API xgag_file( CFC_FString variable, CFC_FString txt, char *c, int *length, CFC_FString filter, int *win_id)
00111 {
00112 file_struct *file = &set_generic( BROWSER, *win_id, variable, txt)->file;
00113
00114 my_f2c_strncpy( file->filter, filter, FILTERLENGTH, FILTERLENGTH);
00115 file->chain = c;
00116 my_fp2c_strncpy( file->userchain, c, *length, CHAINLENGTH);
00117 file->length = strlen( file->userchain);
00118 }
00119
00120
00121 void CFC_API xgag_choice( CFC_FString variable, CFC_FString txt, char *ch, int *length, xgag_choicelst * liste, int *nch, int *ind, int *win_id)
00122 {
00123 int i;
00124 choice_struct *choice = &set_generic( CHOICE, *win_id, variable, txt)->choice;
00125
00126 choice->choice = ch;
00127 my_fp2c_strncpy( choice->userchoice, ch, *length, CHAINLENGTH);
00128 choice->length = strlen( choice->userchoice);
00129 choice->nchoices = *nch;
00130 for (i = 0; i < choice->nchoices; i++) {
00131 my_fp2c_strncpy( choice->choices[i], liste[i].chs, X_CHOLEN, X_CHOLEN);
00132 }
00133 choice->mode = *ind;
00134 }
00135
00136 void CFC_API xgag_slider( CFC_FString variable, CFC_FString txt, float *f, float *min, float *max, int *win_id)
00137 {
00138 slider_struct *slider = &set_generic( SLIDER, *win_id, variable, txt)->slider;
00139
00140 slider->value = f;
00141 slider->uservalue = *f;
00142 slider->width = *max - *min;
00143 slider->min = *min;
00144 }
00145
00146 void CFC_API xgag_ch( CFC_FString variable, CFC_FString txt, char *c, int *length, int *win_id, int *editable)
00147 {
00148 chain_struct *chain = &set_generic( CHAIN, *win_id, variable, txt)->chain;
00149
00150 chain->chain = c;
00151 my_fp2c_strncpy( chain->userchain, c, *length, CHAINLENGTH);
00152 chain->length = strlen( chain->userchain);
00153 chain->editable = *editable;
00154 }
00155
00156 void CFC_API xgag_show( char *c, int *length, int *win_id)
00157 {
00158 show_struct *show = &(last_widget++)->show;
00159
00160 show->window_id = *win_id;
00161 show->type = SHOW;
00162 my_fp2c_strncpy( show->text, c, *length, TEXTLENGTH);
00163 }
00164
00165 void CFC_API xgag_button( CFC_FString title, CFC_FString command, CFC_FString label, int *length, CFC_FString helptxt, CFC_FString moretxt)
00166 {
00167 button_struct *button = &(last_widget++)->button;
00168
00169 button->window_id = 0;
00170 button->type = BUTTON;
00171 my_f2c_strncpy( button->title, title, TITLELENGTH, TITLELENGTH);
00172 my_f2c_strncpy( button->command, command, COMMANDLENGTH, COMMANDLENGTH);
00173 my_f2c_strncpy( button->label, label, LABELLENGTH, LABELLENGTH);
00174
00175
00176
00177 button->showlength = *length;
00178 my_f2c_strncpy( button->helptxt, helptxt, HLPFILELNGTH, HLPFILELNGTH);
00179 my_f2c_strncpy( button->moretxt, moretxt, TITLELENGTH, TITLELENGTH);
00180 }
00181
00182 static task_t xinput_task = SIC_TASK_NULL;
00183
00184 static void kill_xwindow( )
00185 {
00186 if (xinput_task != SIC_TASK_NULL) {
00187 sic_terminate( xinput_task);
00188 xinput_task = SIC_TASK_NULL;
00189 }
00190 }
00191
00192 void CFC_API end_dialog( void)
00193 {
00194 int i;
00195
00196 kill_xwindow( );
00197
00198 last_widget = widgets;
00199 for (i = 0; i < NSTRUCT; i++) {
00200 widgets[i].generic.type = 0;
00201 strcpy( widgets[i].generic.variable, "\0");
00202 }
00203 #ifdef WIN32
00204 on_end_dialog( );
00205 #endif
00206 }
00207
00208 void CFC_API xgag_open( CFC_FString txt, CFC_FString name, int *code)
00209 {
00210 end_dialog( );
00211
00212 my_f2c_strncpy( window_title, txt, TITLELENGTH, TITLELENGTH);
00213 my_f2c_strncpy( helpfilename, name, HLPFILELNGTH, HLPFILELNGTH);
00214 #ifdef WIN32
00215 sprintf(dialoghelpname[0], "%s", helpfilename);
00216 #endif
00217 }
00218
00219 void CFC_API xgag_update( CFC_FString variable, CFC_FzString update)
00220 {
00221 int i;
00222 char variable_name[SICVLN];
00223
00224 if (xinput_task == SIC_TASK_NULL)
00225 return;
00226
00227 my_f2c_strncpy( variable_name, variable, SICVLN, SICVLN);
00228
00229
00230
00231
00232
00233
00234 for (i = 0; i < NSTRUCT; i++) {
00235
00236 if (widgets[i].generic.type == 0)
00237 break;
00238
00239 if (strcmp( variable_name, widgets[i].generic.variable) == 0) {
00240 switch (widgets[i].generic.type) {
00241 case SLIDER:
00242 widgets[i].slider.uservalue = (float)atof( CFC_fz2c_string( update));
00243 break;
00244 case LOGIC:
00245 widgets[i].logic.userlogic = strcmp( CFC_fz2c_string( update), "YES") ? 0 : 1;
00246 break;
00247 case CHAIN:
00248 strncpy( widgets[i].chain.userchain, CFC_fz2c_string( update), CHAINLENGTH);
00249 break;
00250 case CHOICE:
00251 strncpy( widgets[i].choice.userchoice, CFC_fz2c_string( update), CHAINLENGTH);
00252 break;
00253 case BROWSER:
00254 strncpy( widgets[i].file.userchain, CFC_fz2c_string( update), CHAINLENGTH);
00255 break;
00256 }
00257
00258 sic_fire_modified_variable_event( &widgets[i]);
00259
00260 return;
00261 }
00262 }
00263 }
00264
00265
00266
00267 void CFC_API get_user_input( CFC_FzString return_command, int *err_code)
00268 {
00269 int i;
00270 int nb_widgets;
00271
00272 nb_widgets = sic_open_widget_board( );
00273
00274 for (i = 0; i < nb_widgets; i++) {
00275 sic_get_widget_def( i, &widgets[i]);
00276 }
00277
00278 sic_get_widget_returned_command( CFC_fz2c_string( return_command));
00279 *err_code = sic_get_widget_returned_code( );
00280
00281 sic_close_widget_board( );
00282
00283 if (*err_code != -3) {
00284 for (i = 0; i < NSTRUCT; i++) {
00285 if (widgets[i].generic.type == 0)
00286 break;
00287
00288 switch (widgets[i].generic.type) {
00289 case CHAIN:
00290 strcpy( widgets[i].chain.chain, widgets[i].chain.userchain);
00291 break;
00292 case BROWSER:
00293 strcpy( widgets[i].file.chain, widgets[i].file.userchain);
00294 break;
00295 case LOGIC:
00296 *widgets[i].logic.logic = widgets[i].logic.userlogic;
00297 break;
00298 case SLIDER:
00299 *widgets[i].slider.value = widgets[i].slider.uservalue;
00300 break;
00301 case CHOICE:
00302 strcpy( widgets[i].choice.choice, widgets[i].choice.userchoice);
00303 break;
00304 }
00305 }
00306 } else {
00307 kill_xwindow( );
00308
00309
00310 for (i = 0; i < NSTRUCT; i++) {
00311 if (widgets[i].generic.type == 0)
00312 break;
00313 switch (widgets[i].generic.type) {
00314 case LOGIC:
00315 widgets[i].logic.userlogic = *widgets[i].logic.logic;
00316 break;
00317
00318 case BROWSER:
00319 my_fp2c_strncpy( widgets[i].file.userchain, widgets[i].file.chain, widgets[i].file.length, CHAINLENGTH);
00320 break;
00321 case CHOICE:
00322 my_fp2c_strncpy( widgets[i].choice.userchoice, widgets[i].choice.choice, widgets[i].choice.length, CHAINLENGTH);
00323 break;
00324 }
00325 }
00326 }
00327 }
00328
00329 void CFC_API xgag_dialog( CFC_FzString return_command, int *err_code)
00330 {
00331 int i;
00332
00333 sic_create_widget_board( );
00334
00335 sic_set_widget_global_infos( window_title, helpfilename, CFC_fz2c_string( return_command));
00336
00337
00338 for (i = 0; widgets[i].generic.type != 0; i++) {
00339 sic_add_widget_def( &widgets[i]);
00340 }
00341
00342 sic_close_widget_board( );
00343
00344 xinput_task = launch_xinput( );
00345
00346 sic_wait_widget_created( );
00347 }
00348