1
/*------------------------------------------------------------------------
3
* Copyright (C) 2001 Enpc/Jean-Philippe Chancelier
5
--------------------------------------------------------------------------*/
9
#include "men_scilab.h"
11
extern int AllocAndCopy(char **strh1, char *str2);
12
static int choices_cmap(void);
14
static GtkWidget *window = NULL;
16
/* Data structure to deal with a set of choices */
20
char *name; /* name of combo box */
21
int num_toggles; /* number of choice in the combo */
22
int default_toggle; /* initial value for combo choice */
26
char **name; /* table with the combo box list description */
29
static SciComboData ** choices_data;
31
/*---------------------------------------------------------------
32
* data and callbacks for print and export menu
33
*---------------------------------------------------------------*/
35
typedef enum { pOK, pCANCEL , RESET } state;
37
static void sci_choices_ok (GtkButton *button, state * rep)
41
/* Loop on the combo boxes */
42
while ( choices_data[i] != NULL)
44
SciComboData *info = choices_data[i];
45
entry_text = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(info->choice.combo)->entry));
46
for (j = 0; j < info->choice.num_toggles; ++j)
48
if ( strcmp(entry_text,info->name[j]) == 0)
50
info->choice.default_toggle = j;
56
gtk_widget_destroy(window);
61
static void sci_choices_cancel (GtkButton *button, state * rep)
63
gtk_widget_destroy(window);
68
/*---------------------------------------------------------------
69
* The x_choice interaction window
70
*---------------------------------------------------------------*/
72
int SciChoiceI(char *label, int *defval, int nitems)
74
int Nchoices=0, use_scrolled=0, i;
75
static state rep = RESET ;
80
GtkWidget *button_cancel;
83
GtkWidget *scrolled_win;
85
start_sci_gtk(); /* in case gtk was not initialized */
88
/* do not accept a reenter mode */
89
if ( window != NULL) return FALSE ;
91
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
92
gtk_window_set_title (GTK_WINDOW (window), "Scilab choices");
94
gtk_window_set_title (GTK_WINDOW (window),"Scilab dialog");
95
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);
96
gtk_window_set_wmclass (GTK_WINDOW (window), "choices", "Scilab");
98
gtk_signal_connect (GTK_OBJECT (window), "destroy",
99
GTK_SIGNAL_FUNC(sci_choices_cancel),
102
gtk_container_set_border_width (GTK_CONTAINER (window), 0);
104
vbox = gtk_vbox_new (FALSE, 0);
105
gtk_container_add (GTK_CONTAINER (window), vbox);
106
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
107
gtk_widget_show (vbox);
109
/* label widget description of the choices */
110
labelw = gtk_label_new (label);
111
gtk_box_pack_start (GTK_BOX (vbox), labelw, FALSE, FALSE, 0);
112
gtk_widget_show (labelw);
114
/* table widget of the choices */
116
while ( choices_data[Nchoices] != (SciComboData *) NULL ) Nchoices++;
118
if ( Nchoices > 15 ) use_scrolled = 1;
120
if ( use_scrolled ) {
121
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
122
gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 1);
123
gtk_widget_set_usize (scrolled_win,300,300);
124
gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
125
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
126
GTK_POLICY_AUTOMATIC,
127
GTK_POLICY_AUTOMATIC);
130
table = gtk_table_new ( Nchoices , 2, TRUE);
131
gtk_widget_show (table);
133
if ( use_scrolled == 1)
135
gtk_scrolled_window_add_with_viewport
136
(GTK_SCROLLED_WINDOW (scrolled_win), table);
137
gtk_widget_show(scrolled_win);
140
gtk_box_pack_start (GTK_BOX (vbox), table , TRUE, TRUE , 0);
142
gtk_container_set_border_width (GTK_CONTAINER (table), 5);
144
for ( i = 0 ; i < Nchoices ; i++)
147
SciComboData *info = choices_data[i];
148
GList *cbitems = NULL;
150
if ( strncmp( info->choice.name,"colors",6)==0 && strlen(info->choice.name) > 7 )
151
info->choice.label = gtk_label_new (&info->choice.name[7]);
153
info->choice.label = gtk_label_new (info->choice.name);
154
/* set up the toggle widgets */
155
/* qui doit detruire cette liste ? XXXXXXX */
156
for (j = 0; j < info->choice.num_toggles; ++j)
157
cbitems = g_list_append(cbitems, info->name[j]);
158
info->choice.combo = combo = gtk_combo_new ();
159
gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
160
gtk_entry_set_text (GTK_ENTRY (GTK_COMBO(combo)->entry),
161
info->name[info->choice.default_toggle]);
162
gtk_entry_set_editable(GTK_ENTRY (GTK_COMBO(combo)->entry),FALSE);
163
gtk_widget_show (combo);
164
gtk_widget_show (info->choice.label);
165
gtk_table_attach (GTK_TABLE (table),info->choice.label,0,1,i,i+1,
166
GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
168
gtk_table_attach (GTK_TABLE (table), combo,1,2,i,i+1,
169
GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
175
hbbox = gtk_hbutton_box_new ();
176
gtk_box_pack_start (GTK_BOX (vbox), hbbox, FALSE, FALSE , 2);
177
gtk_widget_show (hbbox);
179
button_ok = gtk_button_new_with_label ("OK");
180
gtk_container_add (GTK_CONTAINER (hbbox), button_ok);
182
gtk_signal_connect (GTK_OBJECT (button_ok), "clicked",
183
GTK_SIGNAL_FUNC (sci_choices_ok),
186
GTK_WIDGET_SET_FLAGS (button_ok, GTK_CAN_DEFAULT);
187
gtk_widget_grab_default (button_ok);
188
gtk_widget_show (button_ok);
192
button_cancel = gtk_button_new_with_label ("Cancel");
193
gtk_container_add (GTK_CONTAINER (hbbox), button_cancel);
194
gtk_signal_connect (GTK_OBJECT (button_cancel), "clicked",
195
GTK_SIGNAL_FUNC (sci_choices_cancel),
197
GTK_WIDGET_SET_FLAGS (button_cancel, GTK_CAN_DEFAULT);
198
gtk_widget_show (button_cancel);
200
gtk_widget_show (window);
204
/* here we only want to quit gtk_main after a selection in
207
if ( rep != RESET ) break;
212
for ( i=0 ; i < nitems ; i++)
214
defval[i]= choices_data[i]->choice.default_toggle +1;
217
return (rep == pOK) ? TRUE : FALSE ;
220
/****************************************************
221
* SciChoiceCreate(items,defval,nitems)
222
* This fuction is used to create the required SciComboData
223
* Object in order to call create_choices
224
* from a simpler data structure in order to be able
225
* to communicate with Scilab
226
* char* items[] = { "Label1", "choice1", "choice2",...,NULL,
227
* "Label2", "choice1", "choice2",...,NULL}
228
* les valeurs par defaut sont numerotes a partir de 1
229
* int defval[]={1,2,3,....}
230
* nitems : number of labels
231
* En sortie defval contient ce qu'on a choisit
232
****************************************************/
234
int SciChoiceCreate( char **items,int defval[],int nitems)
237
if ( choices_data != (SciComboData **) NULL)
239
/** someone is using toggles at the same time */
242
choices_data= (SciComboData **) MALLOC( (nitems+1)*sizeof(SciComboData *));
243
if ( choices_data == (SciComboData **) NULL) return(0);
244
choices_data[nitems]= (SciComboData *) NULL;
245
for ( i=0 ; i < nitems ; i++)
249
while ( *loc != (char *) NULL) { loc++;numch++; };
253
sciprint("x_choices : There's no choice to the %d item\r\n",i);
256
choices_data[i]= (SciComboData *) MALLOC( sizeof(SciComboData));
257
if ( choices_data[i] == (SciComboData *) NULL)
261
if ( AllocAndCopy(&(choices_data[i]->choice.name),items[0]) == 0)
265
choices_data[i]->choice.num_toggles= numch;
266
choices_data[i]->choice.default_toggle = Min(Max(0,defval[i]-1),numch-1);
267
choices_data[i]->choice.label = NULL;
268
choices_data[i]->choice.combo = NULL;
269
choices_data[i]->name = (char **) MALLOC( numch*sizeof(char *));
270
if ( choices_data[i]->name == NULL) return(0);
271
for ( j = 0 ; j < numch ; j++)
273
if ( AllocAndCopy(& choices_data[i]->name[j] ,items[j+1]) == 0)
278
items = items + numch+2;
283
int AllocAndCopy( char **strh1,char *str2)
285
*strh1= (char *) MALLOC((strlen(str2)+1)*sizeof(char));
286
if ( *strh1 == (char *) NULL) return(0);
292
int SciChoiceFree(int nitems)
295
for ( i=0 ; i < nitems ; i++)
297
for (j = 0 ; j < choices_data[i]->choice.num_toggles ; j++)
298
FREE(choices_data[i]->name[j]);
299
FREE(choices_data[i]->name) ;
300
FREE(choices_data[i]->choice.name);
307
/****************************************************
308
* checks for color status
309
* checks if we have in our toggle list
310
* a list with colored toggles buttons
311
****************************************************/
313
static int choices_cmap(void)
315
int Nchoices=0,i,flag=0 ; /* counter */
316
while ( choices_data[Nchoices] != (SciComboData *) NULL ) Nchoices++;
317
for (i=0 ; i < Nchoices ; ++i)
319
if ( strncmp(choices_data[i]->choice.name,"colors",6)==0)