# include <config.h>
#endif
-#include "prop_picker.hxx"
+#include <simgear/compiler.h>
+#include <simgear/props/props.hxx>
+
+#include STL_STRING
+
#include <Main/globals.hxx>
-#include <simgear/compiler.h>
-#include <simgear/misc/props.hxx>
+#include "prop_picker.hxx"
+
+SG_USING_STD(string);
+
+// A local alternative name, for use when a variable called "string" is in scope - e.g. in classes derived from puInput.
+typedef string stdString;
#define DOTDOTSLASH "../"
#define SLASH "/"
-string name, line, value;
-
static puObject *PP_widget = 0;
// widget location and size...
me -> find_props();
}
-void prop_editInit(char * name, char * value, char * proppath)
+void prop_editInit( const char * name, const char * value, char * proppath )
{
if( PE_widget == 0 ) {
fgPropEdit *PP = new fgPropEdit ( name, value, proppath );
}
}
-void prop_editOpen( char * name, char * value, char * proppath )
+void prop_editOpen( const char * name, const char * value, char * proppath )
{
if( PE_widget == 0 ) {
prop_editInit( name, value, proppath );
}
// return a human readable form of the value "type"
-static string getValueTypeString( const SGPropertyNode *node ) {
+static string getValueTypeString( const SGPropertyNode_ptr node ) {
string result;
if ( node == NULL ) {
void fgPropPicker::fgPropPickerHandleSlider ( puObject * slider )
{
+ puListBox* list_box = (puListBox*) slider -> getUserData () ;
+
float val ;
slider -> getValue ( &val ) ;
val = 1.0f - val ;
-
- puListBox* list_box = (puListBox*) slider -> getUserData () ;
- int index = int ( list_box -> getNumItems () * val ) ;
- list_box -> setTopItem ( index ) ;
+ int scroll_range = list_box->getNumItems () - list_box->getNumVisible() ;
+ if ( scroll_range > 0 )
+ {
+ int index = int ( scroll_range * val + 0.5 ) ;
+ list_box -> setTopItem ( index ) ;
+ }
}
void fgPropPicker::fgPropPickerHandleArrow ( puObject *arrow )
float val ;
slider -> getValue ( &val ) ;
val = 1.0f - val ;
- int num_items = list_box->getNumItems () - 1 ;
- if ( num_items > 0 )
+ int scroll_range = list_box->getNumItems () - list_box->getNumVisible() ;
+ if ( scroll_range > 0 )
{
- int index = int ( num_items * val + 0.5 ) + inc ;
- if ( index > num_items ) index = num_items ;
+ int index = int ( scroll_range * val + 0.5 ) ;
+ index += inc ;
+ // if ( index > scroll_range ) index = scroll_range ;
+ // Allow buttons to scroll further than the slider does
+ if ( index > ( list_box->getNumItems () - 1 ) )
+ index = ( list_box->getNumItems () - 1 ) ;
if ( index < 0 ) index = 0 ;
- slider -> setValue ( 1.0f - (float)index / num_items ) ;
+ slider -> setValue ( 1.0f - (float)index / scroll_range ) ;
list_box -> setTopItem ( index ) ;
}
}
void fgPropPicker::handle_select ( puObject* list_box )
{
fgPropPicker* prop_picker = (fgPropPicker*) list_box -> getUserData () ;
-
int selected ;
list_box -> getValue ( &selected ) ;
char *dst = prop_picker -> startDir ;
char *src = prop_picker -> files [ selected ] ;
- if ( strcmp ( src, "." ) == 0 )
- {
- /* Do nothing - but better refresh anyway. */
-
- prop_picker -> find_props () ;
- return ;
- }
-
- if ( strcmp ( src, ".." ) == 0 )
- {
- /* Do back up one level - so refresh. */
-
- go_up_one_directory ( dst ) ;
- prop_picker -> find_props () ;
- return ;
- }
+ if (prop_picker->dotFiles && (selected < 2)) {
+ if ( strcmp ( src, "." ) == 0 ) {
+ /* Do nothing - but better refresh anyway. */
+
+ prop_picker -> find_props () ;
+ return ;
+ } else if ( strcmp ( src, ".." ) == 0 ) {
+ /* Do back up one level - so refresh. */
+
+ go_up_one_directory ( dst ) ;
+ prop_picker -> find_props () ;
+ return ;
+ }
+ }
- if ( prop_picker -> dflag [ selected ] )
- {
+ // we know we're dealing with a regular entry, so convert
+ // it to an index into children[]
+ if (prop_picker->dotFiles) selected -= 2;
+ SGPropertyNode_ptr child = prop_picker->children[selected];
+ assert(child != NULL);
+
+ // check if it's a directory (had children)
+ if ( child->nChildren() ) {
/* If this is a directory - then descend into it and refresh */
if ( strlen ( dst ) + strlen ( src ) + 2 >= PUSTRING_MAX )
"PUI: fgPropPicker - path is too long, max is %d.", PUSTRING_MAX ) ;
return ;
}
- prop_editOpen( prop_picker -> names [ selected ], prop_picker -> values [ selected ], dst);
+
+ prop_editOpen(child->getName(), child->getStringValue(), dst);
}
else
{
refresh just in case some other process created the
file.
*/
-
- prop_picker -> find_props () ;
+ // should be obsolete once we observe child add/remove on our top node
+ prop_picker -> find_props () ;
}
}
FG_POP_PUI_DIALOG( prop_picker );
}
-/*
-fgPropPicker::~fgPropPicker ()
+void fgPropPicker::delete_arrays ()
{
if ( files )
{
for ( int i=0; i<num_files; i++ ) {
- delete files[i];
- delete names[i];
- delete values[i];
+ delete[] files[i];
}
+ for (int C=0; C<num_children; ++C) {
+ if (children[C]->nChildren() == 0)
+ children[C]->removeChangeListener(this);
+ }
+
delete[] files;
- delete[] names;
- delete[] values;
- delete[] dflag;
+ delete[] children;
}
+}
+
+/*
+
+fgPropPicker::~fgPropPicker ()
+{
+ delete_arrays();
if ( this == puActiveWidget () )
puDeactivateWidget () ;
puGetDefaultFonts ( &LegendFont, &LabelFont );
files = NULL ;
- dflag = NULL ;
- names = NULL ;
- values = NULL ;
num_files = 0 ;
strcpy ( startDir, dir ) ;
proppath -> setLabel (startDir);
slider = new puSlider (w-30,40+20*arrows,h-100-40*arrows,TRUE,20);
- slider->setDelta(0.1f);
slider->setValue(1.0f);
- slider->setSliderFraction (0.2f) ;
- slider->setCBMode( PUSLIDER_DELTA );
list_box = new puListBox ( 10, 40, w-40, h-60 ) ;
list_box -> setLabel ( title );
up_arrow->setCallback ( fgPropPickerHandleArrow ) ;
}
- if ( arrows == 2 )
- {
- down_arrow = new puArrowButton ( w-30, 40, w-10, 60, PUARROW_FASTDOWN ) ;
- down_arrow->setUserData ( slider ) ;
- down_arrow->setCallback ( fgPropPickerHandleArrow ) ;
-
- up_arrow = new puArrowButton ( w-30, h-80, w-10, h-60, PUARROW_FASTUP ) ;
- up_arrow->setUserData ( slider ) ;
- up_arrow->setCallback ( fgPropPickerHandleArrow ) ;
- }
-
// after picker is built, load the list box with data...
find_props () ;
slider -> setCallback ( fgPropPickerHandleSlider ) ;
FG_FINALIZE_PUI_DIALOG( this );
- printf("fgPropPicker - End of Init\n");
}
-void fgPropPicker::find_props ()
+// Like strcmp, but for sorting property nodes into a suitable display order.
+static int nodeNameCompare(const void *ppNode1, const void *ppNode2)
{
+ const SGPropertyNode_ptr pNode1 = *(const SGPropertyNode_ptr *)ppNode1;
+ const SGPropertyNode_ptr pNode2 = *(const SGPropertyNode_ptr *)ppNode2;
- int pi;
+ // Compare name first, and then index.
+ int diff = strcmp(pNode1->getName(), pNode2->getName());
+ if (diff) return diff;
+ return pNode1->getIndex() - pNode2->getIndex();
+}
- if ( files != NULL )
- {
- for ( int i = 0 ; i < num_files ; i++ ) {
- delete files[i] ;
- delete names[i] ;
- delete values[i] ;
- }
- delete [] files ;
- delete [] names ;
- delete [] values ;
- delete [] dflag ;
- }
+// Replace the current list of properties with the children of node "startDir".
+void fgPropPicker::find_props ()
+{
+ int pi;
+ int i;
+ delete_arrays();
num_files = 0 ;
- char dir [ PUSTRING_MAX * 2 ] ;
-
- int iindex = 0;
- char sindex [ 20 ];
-
- strcpy ( dir, startDir ) ;
-
- int i = 0 ;
-// printf("dir begin of find_props=%s\n",dir);
-// printf("len of dir=%i",strlen(dir));
- SGPropertyNode * node = globals->get_props()->getNode(dir);
-
- num_files = (int)node->nChildren();
+// printf("dir begin of find_props=%s\n",startDir);
+// printf("len of dir=%i",strlen(startDir));
+ SGPropertyNode * node = globals->get_props()->getNode(startDir);
+ num_files = (node) ? (int)node->nChildren() : 0;
+
// instantiate string objects and add [.] and [..] for subdirs
- if (strcmp(dir,"/") == 0) {
+ if (strcmp(startDir,"/") == 0) {
files = new char* [ num_files+1 ] ;
- names = new char* [ num_files+1 ] ;
- values = new char* [ num_files+1 ] ;
- dflag = new char [ num_files+1 ] ;
pi = 0;
+ dotFiles = false;
} else {
// add two for the .. and .
num_files = num_files + 2;
// make room for .. and .
files = new char* [ num_files+1 ] ;
- names = new char* [ num_files+1 ] ;
- values = new char* [ num_files+1 ] ;
- dflag = new char [ num_files+1 ] ;
- line = ".";
- files [ 0 ] = new char[ strlen(line.c_str())+2 ];
+
+ stdString line = ".";
+ files [ 0 ] = new char[line.size() + 1];
strcpy ( files [ 0 ], line.c_str() );
- names [ 0 ] = new char[ 2 ];
- values[ 0 ] = new char[ 2 ] ;
+
line = "..";
- dflag[ 0 ] = 1;
- files [ 1 ] = new char[ strlen(line.c_str())+2 ];
+ files [ 1 ] = new char[line.size() + 1];
strcpy ( files [ 1 ], line.c_str() );
- names [ 1 ] = new char[ strlen(line.c_str())+2 ];
- values[ 1 ] = new char[ 2 ] ;
- strcpy ( names [ 1 ], line.c_str() );
- dflag[ 1 ] = 1;
pi = 2;
+ dotFiles = true;
};
- for (i = 0; i < (int)node->nChildren(); i++) {
- SGPropertyNode * child = node->getChild(i);
- name = child->getName();
- if ( node->getChild(name.c_str(), 1) != 0 ) {
- iindex = child->getIndex();
- sprintf(sindex, "[%d]", iindex);
- name += sindex;
- }
- line = name;
- names[ pi ] = new char[ strlen(line.c_str())+2 ] ;
- strcpy ( names [ pi ], line.c_str() ) ;
- if ( child->nChildren() > 0 ) {
- dflag[ pi ] = 1 ;
- files[ pi ] = new char[ strlen(line.c_str())+strlen(sindex)+4 ] ;
- strcpy ( files [ pi ], line.c_str() ) ;
- strcat ( files [ pi ], "/" ) ;
- values[ pi ] = new char[ 2 ] ;
- } else {
- dflag[ pi ] = 0 ;
- value = node->getStringValue ( name.c_str(), "" );
- values[ pi ] = new char[ strlen(value.c_str())+2 ] ;
- strcpy ( values [pi], value.c_str() );
- line += " = '" + value + "' " + "(";
- line += getValueTypeString( node->getNode( name.c_str() ) );
- line += ")";
- files[ pi ] = new char[ strlen(line.c_str())+2 ] ;
- strcpy ( files [ pi ], line.c_str() ) ;
- }
- // printf("files->%i of %i %s\n",pi, node->nChildren(), files [pi]);
- ++pi;
- }
-
- files [ num_files ] = NULL ;
-
- // leave the . and .. alone...
- int ii = ( strcmp(files [0], "." ) == 0 ) ? 2 : 0;
-
- // Sort the entries. This is a simple N^2 extraction sort. More
- // elaborate algorithms aren't necessary for the few dozen
- // properties we're going to sort.
- for(i=ii; i<num_files; i++) {
- int j, min = i;
- char df, *tmp;
- for(j=i+1; j<num_files; j++)
- if(strcmp(names[j], names[min]) < 0)
- min = j;
- if(i != min) {
- tmp = names[min]; names[min] = names[i]; names[i] = tmp;
- tmp = files[min]; files[min] = files[i]; files[i] = tmp;
- tmp = values[min]; values[min] = values[i]; values[i] = tmp;
- df = dflag[min]; dflag[min] = dflag[i]; dflag[i] = df;
+ if (node) {
+ // Get the list of children
+ num_children = node->nChildren();
+ children = new SGPropertyNode_ptr[num_children];
+ for (i = 0; i < num_children; i++) {
+ children[i] = node->getChild(i);
+ }
+
+ // Sort the children into display order
+ qsort(children, num_children, sizeof(children[0]), nodeNameCompare);
+
+ // Make lists of the children's names, values, etc.
+ for (i = 0; i < num_children; i++) {
+ SGPropertyNode * child = children[i];
+ stdString name = child->getDisplayName(true);
+
+ if ( child->nChildren() > 0 ) {
+ files[ pi ] = new char[ strlen(name.c_str())+2 ] ;
+ strcpy ( files [ pi ], name.c_str() ) ;
+ strcat ( files [ pi ], "/" ) ;
+ } else {
+ files[pi] = NULL; // ensure it's NULL before setting intial value
+ updateTextForEntry(i);
+ // observe it
+ child->addChangeListener(this);
+ }
+
+ ++pi;
}
}
- // printf("files pointer=%i/%i\n", files, num_files);
+ files [ num_files ] = NULL ;
proppath -> setLabel (startDir);
list_box -> newList ( files ) ;
- // if non-empty list, adjust the size of the slider...
- if (num_files > 1) {
- if ((11.0f/(num_files-1)) < 1) {
- slider->setSliderFraction (11.0f/(num_files-1)) ;
- slider->reveal();
- up_arrow->reveal();
- down_arrow->reveal();
- }
- else {
- slider->setSliderFraction (0.9999f) ;
- slider->hide();
- up_arrow->hide();
- down_arrow->hide();
- }
+ // adjust the size of the slider...
+ if (num_files > list_box->getNumVisible()) {
+ slider->setSliderFraction((float)list_box->getNumVisible() / num_files);
+ slider->setValue(1.0f);
+ slider->reveal();
+ up_arrow->reveal();
+ down_arrow->reveal();
+ } else {
+ slider->hide();
+ up_arrow->hide();
+ down_arrow->hide();
}
-
+}
+
+void fgPropPicker::updateTextForEntry(int index)
+{
+ assert((index >= 0) && (index < num_children));
+ SGPropertyNode_ptr node = children[index];
+
+ // take a copy of the value
+ stdString value = node->getStringValue();
+
+ stdString line = node->getDisplayName() + stdString(" = '")
+ + value + "' " + "(";
+ line += getValueTypeString( node );
+ line += ")";
+
+ // truncate entries to plib pui limit
+ if (line.length() >= PUSTRING_MAX)
+ line[PUSTRING_MAX-1] = '\0';
+
+ if (dotFiles) index +=2;
+
+ // don't leak everywhere if we're updating
+ if (files[index]) delete[] files[index];
+
+ files[index] = new char[ strlen(line.c_str())+2 ] ;
+ strcpy ( files [ index ], line.c_str() ) ;
+}
+
+void fgPropPicker::valueChanged(SGPropertyNode *nd)
+{
+ for (int C=0; C<num_children; ++C)
+ if (children[C] == nd) {
+ updateTextForEntry(C);
+ return;
+ }
}
void fgPropEdit::fgPropEditHandleCancel ( puObject* b )
FG_POP_PUI_DIALOG( prop_edit );
}
-fgPropEdit::fgPropEdit ( char *name, char *value, char *proppath ) : puDialogBox ( 0, 0 )
+fgPropEdit::fgPropEdit ( const char *name, const char *value, char *proppath ) : puDialogBox ( 0, 0 )
{
puFont LegendFont, LabelFont;
FG_FINALIZE_PUI_DIALOG( this );
}
-
-