~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to routines/menusX/gxmen_choice.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2005-01-09 22:58:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050109225821-473xr8vhgugxxx5j
Tags: 3.0-12
changed configure.in to build scilab's own malloc.o, closes: #255869

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*------------------------------------------------------------------------
 
2
 *    Scilab Gtk menus 
 
3
 *    Copyright (C) 2001 Enpc/Jean-Philippe Chancelier
 
4
 *    jpc@cermics.enpc.fr 
 
5
 --------------------------------------------------------------------------*/
 
6
 
 
7
#include <stdio.h>
 
8
#include <gtk/gtk.h>
 
9
#include "men_scilab.h"
 
10
 
 
11
extern int AllocAndCopy(char **strh1, char *str2);
 
12
static int choices_cmap(void);
 
13
 
 
14
static GtkWidget *window = NULL; 
 
15
 
 
16
/* Data structure to deal with a set of choices */
 
17
 
 
18
typedef struct {
 
19
  struct {
 
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 */ 
 
23
    GtkWidget *label;
 
24
    GtkWidget *combo;
 
25
  } choice;
 
26
  char **name;         /* table with the combo box list description */
 
27
} SciComboData;
 
28
 
 
29
static SciComboData ** choices_data;
 
30
 
 
31
/*---------------------------------------------------------------
 
32
 * data and callbacks for print and export menu  
 
33
 *---------------------------------------------------------------*/
 
34
 
 
35
typedef enum { pOK, pCANCEL , RESET } state; 
 
36
 
 
37
static void sci_choices_ok (GtkButton       *button, state * rep) 
 
38
{  
 
39
  int i = 0, j;
 
40
  gchar *entry_text;
 
41
  /* Loop on the combo boxes */
 
42
  while ( choices_data[i] != NULL) 
 
43
    {
 
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) 
 
47
        { 
 
48
          if ( strcmp(entry_text,info->name[j]) == 0) 
 
49
            {
 
50
              info->choice.default_toggle = j;
 
51
              break;
 
52
            }
 
53
        }
 
54
      i++;
 
55
    }
 
56
  gtk_widget_destroy(window); 
 
57
  *rep = pOK;  
 
58
  gtk_main_quit();
 
59
 
60
 
 
61
static void sci_choices_cancel (GtkButton       *button, state * rep) 
 
62
{
 
63
  gtk_widget_destroy(window); 
 
64
  *rep = pCANCEL;  
 
65
  gtk_main_quit();
 
66
}
 
67
 
 
68
/*---------------------------------------------------------------
 
69
 * The x_choice interaction window 
 
70
 *---------------------------------------------------------------*/
 
71
 
 
72
int SciChoiceI(char *label, int *defval, int nitems)
 
73
{
 
74
  int Nchoices=0, use_scrolled=0, i;
 
75
  static state rep = RESET ;
 
76
  
 
77
  GtkWidget *table;
 
78
  GtkWidget *labelw;
 
79
  GtkWidget *button_ok;
 
80
  GtkWidget *button_cancel;
 
81
  GtkWidget *vbox;
 
82
  GtkWidget *hbbox;
 
83
  GtkWidget *scrolled_win;
 
84
  
 
85
  start_sci_gtk(); /* in case gtk was not initialized */
 
86
 
 
87
  rep =RESET;
 
88
  /* do not accept a reenter mode */ 
 
89
  if ( window != NULL) return FALSE ; 
 
90
 
 
91
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 
92
  gtk_window_set_title (GTK_WINDOW (window), "Scilab choices");
 
93
 
 
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");
 
97
 
 
98
  gtk_signal_connect (GTK_OBJECT (window), "destroy",
 
99
                      GTK_SIGNAL_FUNC(sci_choices_cancel),
 
100
                      &rep);
 
101
 
 
102
  gtk_container_set_border_width (GTK_CONTAINER (window), 0);
 
103
 
 
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);
 
108
 
 
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);
 
113
 
 
114
  /* table widget  of the choices */
 
115
 
 
116
  while ( choices_data[Nchoices] != (SciComboData *) NULL ) Nchoices++;
 
117
 
 
118
  if ( Nchoices  > 15 ) use_scrolled = 1;
 
119
 
 
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);
 
128
  }
 
129
 
 
130
  table = gtk_table_new ( Nchoices , 2, TRUE);
 
131
  gtk_widget_show (table);
 
132
 
 
133
  if ( use_scrolled == 1) 
 
134
    {
 
135
      gtk_scrolled_window_add_with_viewport
 
136
        (GTK_SCROLLED_WINDOW (scrolled_win), table);
 
137
      gtk_widget_show(scrolled_win);  
 
138
    }
 
139
  else 
 
140
    gtk_box_pack_start (GTK_BOX (vbox), table , TRUE, TRUE , 0);
 
141
 
 
142
  gtk_container_set_border_width (GTK_CONTAINER (table), 5);
 
143
  
 
144
  for ( i = 0 ; i <  Nchoices ; i++) 
 
145
    {
 
146
      int j;
 
147
      SciComboData *info = choices_data[i];
 
148
      GList *cbitems = NULL;
 
149
      GtkWidget *combo;
 
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]);
 
152
      else 
 
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,
 
167
                        0,0);
 
168
      gtk_table_attach (GTK_TABLE (table), combo,1,2,i,i+1,
 
169
                        GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
 
170
                        0,0);
 
171
    }
 
172
 
 
173
  /* ok */ 
 
174
 
 
175
  hbbox = gtk_hbutton_box_new ();
 
176
  gtk_box_pack_start (GTK_BOX (vbox), hbbox, FALSE, FALSE , 2);
 
177
  gtk_widget_show (hbbox);
 
178
 
 
179
  button_ok = gtk_button_new_with_label ("OK");
 
180
  gtk_container_add (GTK_CONTAINER (hbbox), button_ok);
 
181
 
 
182
  gtk_signal_connect (GTK_OBJECT (button_ok), "clicked",
 
183
                      GTK_SIGNAL_FUNC (sci_choices_ok),
 
184
                      &rep);
 
185
 
 
186
  GTK_WIDGET_SET_FLAGS (button_ok, GTK_CAN_DEFAULT);
 
187
  gtk_widget_grab_default (button_ok);
 
188
  gtk_widget_show (button_ok);
 
189
 
 
190
  /* cancel */
 
191
 
 
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),
 
196
                      &rep);
 
197
  GTK_WIDGET_SET_FLAGS (button_cancel, GTK_CAN_DEFAULT);
 
198
  gtk_widget_show (button_cancel);
 
199
 
 
200
  gtk_widget_show (window);
 
201
 
 
202
  while (1) 
 
203
    {
 
204
      /* here we only want to quit gtk_main after a selection in 
 
205
       */
 
206
      gtk_main();
 
207
      if ( rep != RESET ) break;
 
208
    }
 
209
  window = NULL;
 
210
  if ( rep == pOK ) 
 
211
    {
 
212
      for ( i=0 ; i < nitems ; i++) 
 
213
        {
 
214
          defval[i]= choices_data[i]->choice.default_toggle +1;
 
215
        }
 
216
    }
 
217
  return (rep == pOK) ? TRUE : FALSE  ;
 
218
}
 
219
 
 
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
 ****************************************************/
 
233
 
 
234
int  SciChoiceCreate( char **items,int defval[],int nitems)
 
235
{
 
236
  int i,j;
 
237
  if ( choices_data != (SciComboData **) NULL) 
 
238
    {
 
239
      /** someone is using toggles at the same time */
 
240
      return(-1);
 
241
    }
 
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++) 
 
246
    {
 
247
      char **loc= items ;
 
248
      int numch=0;
 
249
      while ( *loc != (char *) NULL) { loc++;numch++; };
 
250
      numch--;
 
251
      if ( numch == 0) 
 
252
        {
 
253
          sciprint("x_choices : There's no choice to the %d item\r\n",i);
 
254
          return(0);
 
255
        };
 
256
      choices_data[i]= (SciComboData *) MALLOC( sizeof(SciComboData));
 
257
      if ( choices_data[i] == (SciComboData *) NULL) 
 
258
        {
 
259
          return(0);
 
260
        }
 
261
      if ( AllocAndCopy(&(choices_data[i]->choice.name),items[0]) == 0) 
 
262
        {
 
263
          return(0);
 
264
        }
 
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++) 
 
272
        {
 
273
          if ( AllocAndCopy(& choices_data[i]->name[j] ,items[j+1]) == 0) 
 
274
            {
 
275
              return(0);
 
276
            }
 
277
        }
 
278
      items = items + numch+2;
 
279
    }
 
280
  return(1);
 
281
}
 
282
 
 
283
int AllocAndCopy( char **strh1,char *str2)
 
284
{
 
285
  *strh1= (char *) MALLOC((strlen(str2)+1)*sizeof(char));
 
286
  if ( *strh1 == (char *) NULL) return(0);
 
287
  strcpy(*strh1,str2);
 
288
  return(1);
 
289
}
 
290
 
 
291
 
 
292
int SciChoiceFree(int nitems) 
 
293
{
 
294
  int i,j;
 
295
  for ( i=0 ; i < nitems ; i++) 
 
296
    {
 
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);
 
301
    }
 
302
  FREE(choices_data);
 
303
  choices_data = NULL;
 
304
  return(0);
 
305
}
 
306
 
 
307
/****************************************************
 
308
 * checks for color status 
 
309
 * checks if we have in our toggle list 
 
310
 *   a list with colored toggles buttons 
 
311
 ****************************************************/
 
312
 
 
313
static int choices_cmap(void)
 
314
{
 
315
  int Nchoices=0,i,flag=0 ;                     /* counter */
 
316
  while ( choices_data[Nchoices] != (SciComboData *) NULL ) Nchoices++;
 
317
  for (i=0 ; i < Nchoices ; ++i) 
 
318
    { 
 
319
      if ( strncmp(choices_data[i]->choice.name,"colors",6)==0)
 
320
        flag=1;
 
321
  }
 
322
  return(flag);
 
323
}
 
324
 
 
325