File: gxmen_choice.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (359 lines) | stat: -rw-r--r-- 10,597 bytes parent folder | download | duplicates (2)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*------------------------------------------------------------------------
 *    Scilab Gtk menus 
 *    Copyright (C) 2001 Enpc/Jean-Philippe Chancelier
 *    jpc@cermics.enpc.fr 
 --------------------------------------------------------------------------*/

#include <stdio.h>
#include <gtk/gtk.h>
#include "men_scilab.h"

extern int AllocAndCopy(char **strh1, char *str2);
extern char *sci_convert_to_utf8(char *str, int *alloc);

static int AllocAndCopyUtf8(char **strh1, char *str2);

static int choices_cmap(void);

static GtkWidget *window = NULL; 

/* Data structure to deal with a set of choices */

typedef struct {
  struct {
    char *name;          /* name of combo box */
    int  num_toggles;    /* number of choice in the combo */
    int  default_toggle; /* initial value for combo choice */ 
    GtkWidget *label;
    GtkWidget *combo;
  } choice;
  char **name;         /* table with the combo box list description */
} SciComboData;

static SciComboData ** choices_data;

/*---------------------------------------------------------------
 * data and callbacks for print and export menu  
 *---------------------------------------------------------------*/

typedef enum { pOK, pCANCEL , RESET } state; 

static void sci_choices_ok (GtkButton       *button, state * rep) 
{  
  int i = 0, j;
  gchar *entry_text;
  /* Loop on the combo boxes */
  while ( choices_data[i] != NULL) 
    {
      SciComboData *info = choices_data[i];
      entry_text = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(info->choice.combo)->entry));
      for (j = 0; j < info->choice.num_toggles; ++j) 
	{ 
	  if ( strcmp(entry_text,info->name[j]) == 0) 
	    {
	      info->choice.default_toggle = j;
	      break;
	    }
	}
      i++;
    }
  gtk_widget_destroy(window); 
  *rep = pOK;  
  gtk_main_quit();
} 

static void sci_choices_cancel (GtkButton       *button, state * rep) 
{
  gtk_widget_destroy(window); 
  *rep = pCANCEL;  
  gtk_main_quit();
}

/*---------------------------------------------------------------
 * The x_choice interaction window 
 *---------------------------------------------------------------*/

int SciChoiceI(char *label, int *defval, int nitems)
{
  char *label_utf8;
  int Nchoices=0, use_scrolled=0, i,alloc_label=0;
  static state rep = RESET ;
  
  GtkWidget *table;
  GtkWidget *labelw;
  GtkWidget *button_ok;
  GtkWidget *button_cancel;
  GtkWidget *vbox;
  GtkWidget *hbbox;
  GtkWidget *scrolled_win;
  
  start_sci_gtk(); /* in case gtk was not initialized */

  rep =RESET;
  /* do not accept a reenter mode */ 
  if ( window != NULL) return FALSE ; 

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Scilab choices");

  gtk_window_set_title   (GTK_WINDOW (window),"Scilab dialog");
  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);
  gtk_window_set_wmclass  (GTK_WINDOW (window), "choices", "Scilab");

  gtk_signal_connect (GTK_OBJECT (window), "destroy",
		      GTK_SIGNAL_FUNC(sci_choices_cancel),
		      &rep);

  gtk_container_set_border_width (GTK_CONTAINER (window), 0);

  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
  gtk_widget_show (vbox);

  /* label widget description of the choices */

  label_utf8  = sci_convert_to_utf8(label,&alloc_label);
  labelw = gtk_label_new (label_utf8);
  if (alloc_label == 1 ) free(label_utf8);

  gtk_box_pack_start (GTK_BOX (vbox), labelw, FALSE, FALSE, 0);
  gtk_widget_show (labelw);

  /* table widget  of the choices */

  while ( choices_data[Nchoices] != (SciComboData *) NULL ) Nchoices++;

  if ( Nchoices  > 15 ) use_scrolled = 1;

  if ( use_scrolled ) {
    scrolled_win = gtk_scrolled_window_new (NULL, NULL);
    gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 1);
    gtk_widget_set_usize (scrolled_win,300,300);
    gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
				    GTK_POLICY_AUTOMATIC,
				    GTK_POLICY_AUTOMATIC);
  }

  table = gtk_table_new ( Nchoices , 2, TRUE);
  gtk_widget_show (table);

  if ( use_scrolled == 1) 
    {
      gtk_scrolled_window_add_with_viewport
	(GTK_SCROLLED_WINDOW (scrolled_win), table);
      gtk_widget_show(scrolled_win);  
    }
  else 
    gtk_box_pack_start (GTK_BOX (vbox), table , TRUE, TRUE , 0);

  gtk_container_set_border_width (GTK_CONTAINER (table), 5);
  
  for ( i = 0 ; i <  Nchoices ; i++) 
    {
      int j;
      SciComboData *info = choices_data[i];
      GList *cbitems = NULL;
      GtkWidget *combo;
      if ( strncmp( info->choice.name,"colors",6)==0 &&  strlen(info->choice.name) > 7 )
	info->choice.label = gtk_label_new (&info->choice.name[7]);
      else 
	info->choice.label = gtk_label_new (info->choice.name);
      /* set up the toggle widgets */
      /* qui doit detruire cette liste ? XXXXXXX */ 
      for (j = 0; j < info->choice.num_toggles; ++j) 
	cbitems = g_list_append(cbitems, info->name[j]);
      info->choice.combo = combo =  gtk_combo_new ();
      gtk_combo_set_popdown_strings (GTK_COMBO (combo), cbitems);
      gtk_entry_set_text (GTK_ENTRY (GTK_COMBO(combo)->entry),
		     info->name[info->choice.default_toggle]);
      gtk_entry_set_editable(GTK_ENTRY (GTK_COMBO(combo)->entry),FALSE);
      gtk_widget_show (combo);
      gtk_widget_show (info->choice.label);
      gtk_table_attach (GTK_TABLE (table),info->choice.label,0,1,i,i+1,
			GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
			0,0);
      gtk_table_attach (GTK_TABLE (table), combo,1,2,i,i+1,
			GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
			0,0);
    }

  /* ok */ 

  hbbox = gtk_hbutton_box_new ();
  gtk_box_pack_start (GTK_BOX (vbox), hbbox, FALSE, FALSE , 2);
  gtk_widget_show (hbbox);

  button_ok = gtk_button_new_with_label ("OK");
  gtk_container_add (GTK_CONTAINER (hbbox), button_ok);

  gtk_signal_connect (GTK_OBJECT (button_ok), "clicked",
		      GTK_SIGNAL_FUNC (sci_choices_ok),
		      &rep);

  GTK_WIDGET_SET_FLAGS (button_ok, GTK_CAN_DEFAULT);
  gtk_widget_grab_default (button_ok);
  gtk_widget_show (button_ok);

  /* cancel */

  button_cancel = gtk_button_new_with_label ("Cancel");
  gtk_container_add (GTK_CONTAINER (hbbox), button_cancel);
  gtk_signal_connect (GTK_OBJECT (button_cancel), "clicked",
		      GTK_SIGNAL_FUNC (sci_choices_cancel),
		      &rep);
  GTK_WIDGET_SET_FLAGS (button_cancel, GTK_CAN_DEFAULT);
  gtk_widget_show (button_cancel);

  gtk_widget_show (window);

  while (1) 
    {
      /* here we only want to quit gtk_main after a selection in 
       */
      gtk_main();
      if ( rep != RESET ) break;
    }
  window = NULL;
  if ( rep == pOK ) 
    {
      for ( i=0 ; i < nitems ; i++) 
	{
	  defval[i]= choices_data[i]->choice.default_toggle +1;
	}
    }
  return (rep == pOK) ? TRUE : FALSE  ;
}

/****************************************************
 *   SciChoiceCreate(items,defval,nitems) 
 *   This fuction is used to create the required SciComboData  
 *   Object in order to call create_choices  
 *   from a simpler data structure in order to be able  
 *   to communicate with Scilab  
 *   char*  items[] = {     "Label1",      "choice1",      "choice2",...,NULL, 
 *                          "Label2",      "choice1",      "choice2",...,NULL} 
 *   les valeurs par defaut sont numerotes a partir de 1  
 *   int  defval[]={1,2,3,....} 
 *   nitems : number of labels 	 
 *   En sortie defval contient ce qu'on a choisit  
 ****************************************************/

int  SciChoiceCreate( char **items,int defval[],int nitems)
{
  int i,j;
  if ( choices_data != (SciComboData **) NULL) 
    {
      /** someone is using toggles at the same time */
      return(-1);
    }
  choices_data= (SciComboData **) MALLOC( (nitems+1)*sizeof(SciComboData *));
  if ( choices_data == (SciComboData **) NULL) return(0);
  choices_data[nitems]= (SciComboData *) NULL;
  for ( i=0 ; i < nitems ; i++) 
    {
      char **loc= items ;
      int numch=0;
      while ( *loc != (char *) NULL) { loc++;numch++; };
      numch--;
      if ( numch == 0) 
	{
	  sciprint("x_choices : There's no choice to the %d item\r\n",i);
	  return(0);
	};
      choices_data[i]= (SciComboData *) MALLOC( sizeof(SciComboData));
      if ( choices_data[i] == (SciComboData *) NULL) 
	{
	  return(0);
	}
      if ( AllocAndCopyUtf8(&(choices_data[i]->choice.name),items[0]) == 0) 
	{
	  return(0);
	}
      choices_data[i]->choice.num_toggles= numch;
      choices_data[i]->choice.default_toggle = Min(Max(0,defval[i]-1),numch-1);
      choices_data[i]->choice.label = NULL;
      choices_data[i]->choice.combo = NULL;
      choices_data[i]->name = (char **) MALLOC( numch*sizeof(char *));
      if ( choices_data[i]->name == NULL)  return(0);
      for ( j = 0 ; j < numch ; j++) 
	{
	  if ( AllocAndCopyUtf8(& choices_data[i]->name[j] ,items[j+1]) == 0) 
	    {
	      return(0);
	    }
	}
      items = items + numch+2;
    }
  return(1);
}

int AllocAndCopy( char **strh1,char *str2)
{
  *strh1= (char *) MALLOC((strlen(str2)+1)*sizeof(char));
  if ( *strh1 == (char *) NULL) return(0);
  strcpy(*strh1,str2);
  return(1);
}




int AllocAndCopyUtf8( char **strh1,char *str2)
{
  int alloc=0;
  char *loc = sci_convert_to_utf8(str2, &alloc);
  if ( alloc == 1 ) 
    {
      /* sci_convert_to_utf8 has allocated the utf8 string */
      *strh1 = loc;
    }
  else 
    {
      /* sci_convert_to_utf8 returned str2 without allocation 
       * which means error or str2 was already utf8 
       * we just copy
       */
      *strh1= (char *) MALLOC((strlen(str2)+1)*sizeof(char));
      if ( *strh1 == (char *) NULL) return(0);
      strcpy(*strh1,str2);
    }
  return(1);
}


int SciChoiceFree(int nitems) 
{
  int i,j;
  for ( i=0 ; i < nitems ; i++) 
    {
      for (j = 0 ; j < choices_data[i]->choice.num_toggles ; j++) 
	FREE(choices_data[i]->name[j]);
      FREE(choices_data[i]->name) ;
      FREE(choices_data[i]->choice.name);
    }
  FREE(choices_data);
  choices_data = NULL;
  return(0);
}

/****************************************************
 * checks for color status 
 * checks if we have in our toggle list 
 *   a list with colored toggles buttons 
 ****************************************************/

static int choices_cmap(void)
{
  int Nchoices=0,i,flag=0 ;			/* counter */
  while ( choices_data[Nchoices] != (SciComboData *) NULL ) Nchoices++;
  for (i=0 ; i < Nchoices ; ++i) 
    { 
      if ( strncmp(choices_data[i]->choice.name,"colors",6)==0)
	flag=1;
  }
  return(flag);
}