]> git.mxchange.org Git - flightgear.git/blob - src/GUI/prop_picker.cxx
Updated mouse gui tweaks and Jim's nifty new external view controls.
[flightgear.git] / src / GUI / prop_picker.cxx
1 /*
2
3      Adapted by Jim Wilson, beginning Sept 2001 (FG v 0.79)
4
5 ****     Insert FlightGear GPL here.
6
7      Based on puFilePicker from:
8
9      ********
10      PLIB - A Suite of Portable Game Libraries
11      Copyright (C) 2001  Steve Baker
12  
13      This library is free software; you can redistribute it and/or
14      modify it under the terms of the GNU Library General Public
15      License as published by the Free Software Foundation; either
16      version 2 of the License, or (at your option) any later version.
17  
18      This library is distributed in the hope that it will be useful,
19      but WITHOUT ANY WARRANTY; without even the implied warranty of
20      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21      Library General Public License for more details.
22  
23      You should have received a copy of the GNU Library General Public
24      License along with this library; if not, write to the Free
25      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  
27      For further information visit http://plib.sourceforge.net
28
29      $Id$
30      ********
31 */
32
33
34 #ifdef HAVE_CONFIG_H
35 #  include <config.h>
36 #endif
37
38 #include "prop_picker.hxx"
39 #include <Main/globals.hxx>
40
41 #include <simgear/compiler.h>
42
43 #define DOTDOTSLASH "../"
44 #define SLASH       "/"
45
46 string name, line, value;
47
48 static puObject *PP_widget = 0;
49
50 // widget location and size...
51 #define PROPPICK_X 100
52 #define PROPPICK_Y 200
53 #define PROPPICK_W 500
54 #define PROPPICK_H 300
55
56 static puObject *PE_widget = 0;
57
58 void prop_pickerInit()
59 {
60         if( PP_widget == 0 ) {
61             fgPropPicker *PP = new fgPropPicker ( PROPPICK_X, PROPPICK_Y, PROPPICK_W, PROPPICK_H, 1, "/", "FG Properties");
62             PP_widget = PP;
63         }
64 }
65
66 void prop_pickerView( puObject * )
67 {
68         if( PP_widget == 0 ) {
69                 prop_pickerInit();
70         }
71         fgPropPicker *me = (fgPropPicker *)PP_widget -> getUserData();
72         // refresh
73         me -> find_props();
74         FG_PUSH_PUI_DIALOG( me );
75 }
76
77 void prop_pickerRefresh()
78 {
79         if( PP_widget == 0 ) {
80                 prop_pickerInit();
81         }
82         fgPropPicker *me = (fgPropPicker *)PP_widget -> getUserData();
83         me -> find_props();
84 }
85
86 void prop_editInit(char * name, char * value, char * proppath)
87 {
88         if( PE_widget == 0 ) {
89             fgPropEdit *PP = new fgPropEdit ( name, value, proppath );
90             PE_widget = PP;
91         }
92 }
93
94 void prop_editOpen( char * name, char * value, char * proppath )
95 {
96         if( PE_widget == 0 ) {
97                 prop_editInit( name, value, proppath );
98         }
99         fgPropEdit *me = (fgPropEdit *)PE_widget -> getUserData();
100         me -> propname ->    setLabel          (name);
101         me -> propinput ->   setValue          (value);
102         strcpy(me -> propPath, proppath);
103         me -> propinput -> acceptInput ();
104         FG_PUSH_PUI_DIALOG( me );
105 }
106
107 // return a human readable form of the value "type"
108 static string getValueTypeString( const SGPropertyNode *node ) {
109     string result;
110
111     if ( node == NULL ) {
112         return "unknown";
113     }
114
115     SGPropertyNode::Type type = node->getType();
116     if ( type == SGPropertyNode::UNSPECIFIED ) {
117         result = "unspecified";
118     } else if ( type == SGPropertyNode::NONE ) {
119         result = "none";
120     } else if ( type == SGPropertyNode::BOOL ) {
121         result = "bool";
122     } else if ( type == SGPropertyNode::INT ) {
123         result = "int";
124     } else if ( type == SGPropertyNode::LONG ) {
125         result = "long";
126     } else if ( type == SGPropertyNode::FLOAT ) {
127         result = "float";
128     } else if ( type == SGPropertyNode::DOUBLE ) {
129         result = "double";
130     } else if ( type == SGPropertyNode::STRING ) {
131         result = "string";
132     }
133
134     return result;
135 }
136
137 void fgPropPicker::fgPropPickerHandleSlider ( puObject * slider )
138 {
139   float val ;
140   slider -> getValue ( &val ) ;
141   val = 1.0f - val ;
142
143   puListBox* list_box = (puListBox*) slider -> getUserData () ;
144   int index = int ( list_box -> getNumItems () * val ) ;
145   list_box -> setTopItem ( index ) ;
146 }
147
148 void fgPropPicker::fgPropPickerHandleArrow ( puObject *arrow )
149 {
150   puSlider *slider = (puSlider *) arrow->getUserData () ;
151   puListBox* list_box = (puListBox*) slider -> getUserData () ;
152
153   int type = ((puArrowButton *)arrow)->getArrowType() ;
154   int inc = ( type == PUARROW_DOWN     ) ?   1 :
155             ( type == PUARROW_UP       ) ?  -1 :
156             ( type == PUARROW_FASTDOWN ) ?  10 :
157             ( type == PUARROW_FASTUP   ) ? -10 : 0 ;
158
159   float val ;
160   slider -> getValue ( &val ) ;
161   val = 1.0f - val ;
162   int num_items = list_box->getNumItems () - 1 ;
163   if ( num_items > 0 )
164   {
165     int index = int ( num_items * val + 0.5 ) + inc ;
166     if ( index > num_items ) index = num_items ;
167     if ( index < 0 ) index = 0 ;
168
169     slider -> setValue ( 1.0f - (float)index / num_items ) ;
170     list_box -> setTopItem ( index ) ;
171   }
172 }
173
174
175 void fgPropPicker::chop_file ( char *fname )
176 {
177   /* removes everything back to the last '/' */
178
179   for ( int i = strlen(fname)-1 ; fname[i] != SLASH[0] && i >= 0 ; i-- )
180     fname[i] = '\0' ;
181 }
182
183
184 void fgPropPicker::go_up_one_directory ( char *fname )
185 {
186   /* removes everything back to the last but one '/' */
187
188   chop_file ( fname ) ;
189
190   if ( strlen ( fname ) == 0 )
191   {
192     /* Empty string!  The only way to go up is to append a "../" */
193
194     strcpy ( fname, DOTDOTSLASH ) ;
195     return ;
196   }
197
198   /* If the last path element is a "../" then we'll have to add another "../" */
199
200   if ( strcmp ( & fname [ strlen(fname)-3 ], DOTDOTSLASH ) == 0 )
201   {
202    if ( strlen ( fname ) + 4 >= PUSTRING_MAX )
203     {
204       ulSetError ( UL_WARNING, "PUI: fgPropPicker - path is too long, max is %d.",
205                           PUSTRING_MAX ) ;
206       return ;
207     }
208
209     strcat ( fname, DOTDOTSLASH ) ;
210     return ;
211   }
212
213   /* Otherwise, just delete the last element of the path. */
214
215   /* Remove the trailing slash - then remove the rest as if it was a file name */
216
217   fname [ strlen(fname)-1 ] = '\0' ;
218   chop_file ( fname ) ;
219 }
220
221
222 void fgPropPicker::handle_select ( puObject* list_box )
223 {
224   fgPropPicker* prop_picker = (fgPropPicker*) list_box -> getUserData () ;
225
226   int selected ;
227   list_box -> getValue ( &selected ) ;
228
229   if ( selected >= 0 && selected < prop_picker -> num_files )
230   {
231     char *dst = prop_picker -> startDir ;
232     char *src = prop_picker -> files [ selected ] ;
233
234     if ( strcmp ( src, "." ) == 0 )
235     {
236       /* Do nothing - but better refresh anyway. */
237
238       prop_picker -> find_props () ;
239       return ;
240     } 
241
242     printf("select got here 2\n");
243     if ( strcmp ( src, ".." ) == 0 )
244     {
245       /* Do back up one level - so refresh. */
246
247       go_up_one_directory ( dst ) ;
248       prop_picker -> find_props () ;
249       return ;
250     } 
251
252     if ( prop_picker -> dflag [ selected ] )
253     {
254       /* If this is a directory - then descend into it and refresh */
255
256       if ( strlen ( dst ) + strlen ( src ) + 2 >= PUSTRING_MAX )
257       {
258         ulSetError ( UL_WARNING,
259           "PUI: fgPropPicker - path is too long, max is %d.", PUSTRING_MAX ) ;
260         return ;
261       }
262
263       strcat ( dst, src ) ; /* add path to descend to */
264       prop_picker -> find_props () ;
265       return ;
266     }
267
268     /* If this is a regular file - then just append it to the string */
269
270     if ( strlen ( dst ) + strlen ( src ) + 2 >= PUSTRING_MAX )
271     {
272       ulSetError ( UL_WARNING, 
273         "PUI: fgPropPicker - path is too long, max is %d.", PUSTRING_MAX ) ;
274       return ;
275     }
276     prop_editOpen( prop_picker -> names [ selected ], prop_picker -> values [ selected ], dst);
277   }
278   else
279   {
280     /*
281       The user clicked on blank screen - maybe we should
282       refresh just in case some other process created the
283       file.
284     */
285
286     prop_picker -> find_props () ;
287   }
288 }
289
290
291 void fgPropPicker::fgPropPickerHandleOk ( puObject* b )
292 {
293   fgPropPicker* prop_picker = (fgPropPicker*) b -> getUserData () ;
294
295   /* nothing to do, just hide */
296   FG_POP_PUI_DIALOG( prop_picker );
297 }
298
299 /*
300
301 fgPropPicker::~fgPropPicker ()
302 {
303   if ( files )
304   {
305     for ( int i=0; i<num_files; i++ ) {
306       delete files[i];
307       delete names[i];
308       delete values[i];
309     }
310
311     delete[] files;
312     delete[] names;
313     delete[] values;
314     delete[] dflag;
315   }
316
317   if ( this == puActiveWidget () )
318     puDeactivateWidget () ;
319 }
320
321 */
322
323 fgPropPicker::fgPropPicker ( int x, int y, int w, int h, int arrows,
324                                       const char *dir, const char *title ) : puDialogBox ( x,y )
325 {
326   puFont LegendFont, LabelFont;
327   puGetDefaultFonts ( &LegendFont, &LabelFont );
328
329   files = NULL ;
330   dflag = NULL ;
331   names = NULL ;
332   values = NULL ;
333   num_files = 0 ;
334
335   strcpy ( startDir, dir ) ;
336   printf ( "StartDirLEN=%i", strlen(startDir));
337   if ( arrows > 2 ) arrows = 2 ;
338   if ( arrows < 0 ) arrows = 0 ;
339   arrow_count = arrows ;
340
341   frame = new puFrame ( 0, 0, w, h );
342
343   setUserData( this );
344
345   proppath = new puText            (10, h-30);
346   proppath ->    setLabel          (startDir);
347
348   slider = new puSlider (w-30,40+20*arrows,h-100-40*arrows,TRUE,20);
349   slider->setDelta(0.1f);
350   slider->setValue(1.0f);
351   slider->setSliderFraction (0.2f) ;
352   slider->setCBMode( PUSLIDER_DELTA );
353   
354   list_box = new puListBox ( 10, 40, w-40, h-60 ) ;
355   list_box -> setLabel ( title );
356   list_box -> setLabelPlace ( PUPLACE_ABOVE ) ;
357   list_box -> setStyle ( -PUSTYLE_SMALL_SHADED ) ;
358   list_box -> setUserData ( this ) ;
359   list_box -> setCallback ( handle_select ) ;
360   list_box -> setValue ( 0 ) ;
361
362   find_props () ;
363
364 //  printf("after Props files[1]=%s\n",files[1]);
365 //  printf("num items %i", list_box -> getNumItems ());
366
367   slider -> setUserData ( list_box ) ;
368   slider -> setCallback ( fgPropPickerHandleSlider ) ;
369
370   ok_button = new puOneShot ( 10, 10, (w<170)?(w/2-5):80, 30 ) ;
371   ok_button -> setLegend ( "Ok" ) ;
372   ok_button -> setUserData ( this ) ;
373   ok_button -> setCallback ( fgPropPickerHandleOk ) ;
374
375   if ( arrows > 0 )
376   {
377     puArrowButton *down_arrow = new puArrowButton ( w-30, 20+20*arrows, w-10, 40+20*arrows, PUARROW_DOWN ) ;
378     down_arrow->setUserData ( slider ) ;
379     down_arrow->setCallback ( fgPropPickerHandleArrow ) ;
380
381     puArrowButton *up_arrow = new puArrowButton ( w-30, h-30-20*arrows, w-10, h-10-20*arrows, PUARROW_UP ) ;
382     up_arrow->setUserData ( slider ) ;
383     up_arrow->setCallback ( fgPropPickerHandleArrow ) ;
384   }
385
386   if ( arrows == 2 )
387   {
388     puArrowButton *down_arrow = new puArrowButton ( w-30, 40, w-10, 60, PUARROW_FASTDOWN ) ;
389     down_arrow->setUserData ( slider ) ;
390     down_arrow->setCallback ( fgPropPickerHandleArrow ) ;
391
392     puArrowButton *up_arrow = new puArrowButton ( w-30, h-50, w-10, h-30, PUARROW_FASTUP ) ;
393     up_arrow->setUserData ( slider ) ;
394     up_arrow->setCallback ( fgPropPickerHandleArrow ) ;
395   }
396
397   FG_FINALIZE_PUI_DIALOG( this );
398   printf("fgPropPicker - End of Init\n");
399 }
400
401
402 void fgPropPicker::find_props ()
403 {
404
405   int pi;
406
407   if ( files != NULL )
408   {
409     for ( int i = 0 ; i < num_files ; i++ ) {
410       delete files[i] ;
411       delete names[i] ;
412       delete values[i] ;
413     }
414
415     delete [] files ;
416     delete [] names ;
417     delete [] values ;
418     delete [] dflag ;
419   }
420
421   num_files = 0 ;
422
423   char dir [ PUSTRING_MAX * 2 ] ;
424
425   int  iindex = 0;
426   char sindex [ 20 ];
427
428   strcpy ( dir, startDir ) ;
429
430   int i = 0 ;
431 //  printf("dir begin of find_props=%s\n",dir);
432 //  printf("len of dir=%i",strlen(dir));
433   SGPropertyNode * node = globals->get_props()->getNode(dir);
434
435   printf("find_props: allocation of node\n");
436   num_files = (int)node->nChildren();
437
438   // take off one for the trailing "null" in each SGProperty list
439   num_files = num_files -1;
440
441   // instantiate string objects and add [.] and [..] for subdirs
442   if (strcmp(dir,"/") == 0) {
443     files = new char* [ num_files+1 ] ;
444     names = new char* [ num_files+1 ] ;
445     values = new char* [ num_files+1 ] ;
446     dflag = new char  [ num_files+1 ] ;
447     pi = 0;
448   } else {
449     // add two for the .. and .
450     num_files = num_files +2;
451     // make room for .. and .
452     files = new char* [ num_files+3 ] ;
453     names = new char* [ num_files+3 ] ;
454     values = new char* [ num_files+3 ] ;
455     dflag = new char  [ num_files+3 ] ;
456     line = ".";
457     files [ 0 ] = new char[ strlen(line.c_str())+2 ];
458     strcpy ( files [ 0 ], line.c_str() );
459     names [ 0 ] = new char[ 2 ];
460     values[ 0 ] = new char[ 2 ] ;
461     line = "..";
462     dflag[ 0 ] = 1;
463     files [ 1 ] = new char[ strlen(line.c_str())+2 ];
464     strcpy ( files [ 1 ], line.c_str() );
465     names [ 1 ] = new char[ 2 ];
466     values[ 1 ] = new char[ 2 ] ;
467     strcpy ( names [ 1 ], line.c_str() );
468
469     dflag[ 1 ] = 1;
470     pi = 2;
471   };
472
473
474   for (int i = 0; i < (int)node->nChildren()-1; i++) {
475             SGPropertyNode * child = node->getChild(i);
476             name = child->getName();
477             line = name;
478             names[ pi ] = new char[ strlen(line.c_str())+2 ] ;
479             strcpy ( names [ pi ], line.c_str() ) ;
480             if ( child->nChildren() > 0 ) {
481                 iindex = child->getIndex();
482                 sprintf(sindex, "%d", iindex);
483                 dflag[ pi ] = 1 ;
484                 files[ pi ] = new char[ strlen(line.c_str())+strlen(sindex)+4 ] ;
485                 strcpy ( files [ pi ], line.c_str() ) ;
486                 strcat ( files [ pi ], "[" ) ;
487                 strcat ( files [ pi ], sindex ) ;
488                 strcat ( files [ pi ], "]/" ) ;
489                 values[ pi ] = new char[ 2 ] ;
490             } else {
491                 dflag[ pi ] = 0 ;
492                 value = node->getStringValue ( name, "" );
493                 values[ pi ] = new char[ strlen(value.c_str())+2 ] ;
494                 strcpy ( values [pi], value.c_str() );
495                 line += " = '" + value + "' " + "(";
496                 line += getValueTypeString( node->getNode( name ) );
497                 line += ")";
498                 files[ pi ] = new char[ strlen(line.c_str())+2 ] ;
499                 strcpy ( files [ pi ], line.c_str() ) ;
500             }
501             printf("files->%i %s\n",pi, files [pi]);
502             ++pi;
503   }
504
505   files [ num_files ] = NULL ;
506
507   printf("files pointer=%i\n", files);
508   proppath ->    setLabel          (startDir);
509   list_box -> newList ( files ) ;
510   
511 }
512
513 void fgPropEdit::fgPropEditHandleCancel ( puObject* b )
514 {
515   fgPropEdit* prop_edit = (fgPropEdit*) b -> getUserData () ;
516   FG_POP_PUI_DIALOG( prop_edit );
517 }
518
519 void fgPropEdit::fgPropEditHandleOK ( puObject* b )
520 {
521   fgPropEdit* prop_edit = (fgPropEdit*) b -> getUserData () ;
522   const char* tname;
523   char* tvalue;
524
525   // use label text for property node to be updated
526   tname = prop_edit -> propname -> getLabel();
527   prop_edit -> propinput -> getValue( &tvalue );
528
529   SGPropertyNode * node = globals->get_props()->getNode(prop_edit -> propPath);
530   node->getNode( prop_edit -> propname -> getLabel(), true)->setStringValue(tvalue);
531
532   // update the picker display so it shows new value
533   prop_pickerRefresh();
534
535   FG_POP_PUI_DIALOG( prop_edit );
536 }
537
538 fgPropEdit::fgPropEdit ( char *name, char *value, char *proppath ) : puDialogBox ( 0, 0 )
539
540 {
541     puFont LegendFont, LabelFont;
542     puGetDefaultFonts ( &LegendFont, &LabelFont );
543
544     // locate in relation to picker widget...
545     int fx = PROPPICK_X;
546     int fy = PROPPICK_Y + PROPPICK_H;
547     frame   = new puFrame           (fx,fy, fx+500, fy+120);
548
549     strcpy (propPath, proppath);
550
551     setUserData( this );
552
553     propname = new puText            (fx+10, fy+90);
554     propname ->    setLabel          (name);
555         
556     propinput   = new puInput           (fx+10, fy+50, fx+480, fy+80);
557     propinput   ->    setValue          (value);
558     propinput   ->    acceptInput();
559         
560
561     ok_button     =  new puOneShot   (fx+10, fy+10, fx+80, fy+30);
562     ok_button     ->     setUserData (this);
563     ok_button     ->     setLegend   (gui_msg_OK);
564     ok_button     ->     setCallback (fgPropEditHandleOK);
565     ok_button     ->     makeReturnDefault(TRUE);
566         
567     cancel_button =  new puOneShot   (fx+100, fy+10, fx+180, fy+30);
568     cancel_button     ->     setUserData (this);
569     cancel_button ->     setLegend   (gui_msg_CANCEL);
570     cancel_button ->     setCallback (fgPropEditHandleCancel);
571         
572     FG_FINALIZE_PUI_DIALOG( this );
573 }
574
575