added LFSTK_argsToPrefs to prefsclass

This commit is contained in:
K D Hedger
2025-01-27 12:01:35 +00:00
parent 9b711b29e1
commit 23f8d92cbe
6 changed files with 232 additions and 2 deletions

View File

@ -1,4 +1,5 @@
0.6.1
Added LFSTK_argsToPrefs to prefs class.
Added LFSTK_moveResizeWindow to lib.
Fix window tile cario surface getting occasional wrong size.
Added extra positioning for window context window.

View File

@ -44,6 +44,7 @@
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <getopt.h>
#include <experimental/filesystem>

View File

@ -169,8 +169,12 @@ void LFSTK_prefsClass::LFSTK_loadVarsFromFile(std::string filepath)
case TYPEBOOL:
x.second.boolData=this->LFSTK_stringToBool(data);
break;
case TYPEINT:
x.second.intData=std::stoi(data);
case TYPEINT:
try
{
x.second.intData=std::stoll(data,nullptr,0);
}
catch(std::exception &err) {}
break;
default:
break;
@ -260,4 +264,67 @@ int LFSTK_prefsClass::LFSTK_getInt(const char *key)
void LFSTK_prefsClass::LFSTK_setInt(const char *key,int val)
{
this->prefsMap.at(LFSTK_UtilityClass::LFSTK_hashFromKey(key)).intData=val;
}
/**
* Set prefs from command line.
* \param int argc, char **argv as passed to application.
* \param longoptions[] normal option long_options[] from getopt_long.
* \return boolean false=No prefs set, true=OK.
*/
bool LFSTK_prefsClass::LFSTK_argsToPrefs(int argc, char **argv,option longoptions[])
{
int ocnt=0;
int c;
std::string optstr="";
int option_index;
if(this->prefsMap.size()==0)
{
fprintf(stderr,"No prefs set ...\n");
return(false);
}
while(longoptions[ocnt].name!=0)
{
optstr+=longoptions[ocnt].val;
if(longoptions[ocnt].has_arg==required_argument)
optstr+=":";
if(longoptions[ocnt].has_arg==optional_argument)
optstr+="::";
ocnt++;
}
while (1)
{
option_index=0;
c=getopt_long (argc,argv,optstr.c_str(),longoptions,&option_index);
if(c==-1)
break;
ocnt=0;
while(longoptions[ocnt].name!=0)
{
if(longoptions[ocnt].val==c)
{
dataType typeit=this->prefsMap.at(LFSTK_UtilityClass::LFSTK_hashFromKey(longoptions[ocnt].name)).type;
switch(typeit)
{
case TYPESTRING:
this->LFSTK_setString(longoptions[ocnt].name,optarg);
break;
case TYPEINT:
this->LFSTK_setInt(longoptions[ocnt].name,strtol(optarg,NULL,0));
break;
case TYPEBOOL:
if(optarg==NULL)
this->LFSTK_setBool(longoptions[ocnt].name,true);
else
this->LFSTK_setBool(longoptions[ocnt].name,this->LFSTK_stringToBool(optarg));
break;
}
}
ocnt++;
}
}
return(true);
}

View File

@ -24,6 +24,7 @@
#include <map>
#include <string>
#include <string.h>
#include <getopt.h>
enum dataType {TYPEINVALID,TYPESTRING,TYPEBOOL,TYPEINT};
@ -70,6 +71,9 @@ class LFSTK_prefsClass
int LFSTK_getInt(const char *key);
void LFSTK_setInt(const char *key,int val);
//get cli args
bool LFSTK_argsToPrefs(int argc, char **argv,option longoptions[]);
private:
const char *bools[2]={"false","true"};
};

152
LFSToolKit/examples/PrefsTest.cpp Executable file
View File

@ -0,0 +1,152 @@
#if 0
pushd ../
make
popd
if [ "X$USEVALGRIND" != "X" ];then
VALGRIND="valgrind --leak-check=full"
fi
APPNAME=$(basename $0 .cpp)
# Run as for instance ./PrefsTest.cpp --colour="green" --gravity=3 -v0 --theme="mytheme"
g++ "$0" -O0 -ggdb -I../LFSToolKit -L../LFSToolKit/app/.libs $(pkg-config --cflags --libs x11 xft cairo glib-2.0 imlib2) -llfstoolkit -o $APPNAME||exit 1
LD_LIBRARY_PATH=../LFSToolKit/app/.libs $VALGRIND ./$APPNAME "$@"
retval=$?
echo "Exit code $retval"
rm $APPNAME
exit $retval
#endif
#include "lfstk/LFSTKGlobals.h"
#define BOXLABEL "Basic LFSTK App"
LFSTK_applicationClass *apc=NULL;
LFSTK_windowClass *wc=NULL;
LFSTK_labelClass *label=NULL;
LFSTK_labelClass *personal=NULL;
LFSTK_labelClass *copyrite=NULL;
LFSTK_buttonClass *seperator=NULL;
LFSTK_buttonClass *quit=NULL;
LFSTK_buttonClass *test=NULL;
LFSTK_prefsClass prefs;
bool doQuit(void *p,void* ud)
{
apc->exitValue=0;
apc->mainLoop=false;
return(false);
}
bool buttonCB(void *p,void* ud)
{
LFSTK_buttonClass *button=NULL;
fprintf(stderr,"button=%p ud=%p\n",p,ud);
return(true);
}
int cnt=1;
bool timerCB(LFSTK_applicationClass *p,void* ud)
{
printf("From window %s\n",ud);
printf("Timer callback number %i of 10\n",cnt);
cnt++;
if(cnt<11)
return(true);
else
return(false);
}
void setPrefs(int argc, char **argv)
{
prefs.prefsMap=
{
{LFSTK_UtilityClass::LFSTK_hashFromKey("theme"),{TYPESTRING,"theme","",false,0}},
{LFSTK_UtilityClass::LFSTK_hashFromKey("colour"),{TYPESTRING,"colour","",false,0}},
{LFSTK_UtilityClass::LFSTK_hashFromKey("gravity"),{TYPEINT,"gravity","",false,0}},
{LFSTK_UtilityClass::LFSTK_hashFromKey("vertical"),{TYPEBOOL,"vertical","",false,0}},
};
prefs.LFSTK_loadVarsFromFile("./testprefs.rc");
}
void printPrefs(void)
{
prefs.LFSTK_saveVarsToFile("-");
}
int main(int argc, char **argv)
{
int sy=BORDER;
LFSTK_buttonClass *button=NULL;
apc=new LFSTK_applicationClass();
apc->LFSTK_addWindow(NULL,BOXLABEL,"LFSTKExample");
wc=apc->mainWindow;
wc->userData=USERDATA("Main Window");
label=new LFSTK_labelClass(apc->windows->back().window,BOXLABEL,0,sy,DIALOGWIDTH-BORDER-BORDER,GADGETHITE);
label->LFSTK_setCairoFontDataParts("sB",20);
label->LFSTK_setTile(NULL,0);
label=new LFSTK_labelClass(wc,BOXLABEL,BORDER,sy,DIALOGWIDTH-BORDER-BORDER,GADGETHITE);
label->LFSTK_setCairoFontDataParts("sB",20);
sy+=YSPACING;
copyrite=new LFSTK_labelClass(wc,COPYRITE,BORDER,sy,DIALOGWIDTH-BORDER-BORDER,GADGETHITE);
sy+=HALFYSPACING;
personal=new LFSTK_labelClass(wc,PERSONAL,BORDER,sy,DIALOGWIDTH-BORDER-BORDER,GADGETHITE);
personal->LFSTK_setCairoFontDataParts("B");
sy+=YSPACING;
//line
seperator=new LFSTK_buttonClass(wc,"--",0,sy,DIALOGWIDTH,GADGETHITE,BUTTONGRAV);
seperator->LFSTK_setStyle(BEVELNONE);
seperator->gadgetDetails.buttonTile=false;
seperator->gadgetDetails.colour=&wc->windowColourNames[NORMALCOLOUR];
sy+=YSPACING;
//test
test=new LFSTK_buttonClass(wc,"Somthing",DIALOGMIDDLE-GADGETWIDTH,sy,GADGETWIDTH*2,GADGETHITE,BUTTONGRAV);
test->LFSTK_setMouseCallBack(NULL,buttonCB,(void*)(apc->windows->size()-2));
sy+=YSPACING;
//quit
quit=new LFSTK_buttonClass(wc,"Quit",DIALOGMIDDLE-HALFGADGETWIDTH,sy,GADGETWIDTH,GADGETHITE,BUTTONGRAV);
quit->LFSTK_setMouseCallBack(NULL,doQuit,NULL);
sy+=YSPACING;
wc->LFSTK_resizeWindow(DIALOGWIDTH,sy,true);
wc->LFSTK_showWindow();
printf("Number of gadgets in window=%i\n",wc->LFSTK_gadgetCount());
//apc->LFSTK_setTimer(1);
//apc->LFSTK_setTimerCallBack(timerCB,NULL);
fprintf(stderr,"Loading prefs from file ...\n");
setPrefs(argc,argv);
printPrefs();
option long_options[]=
{
{"theme",required_argument,NULL,'t'},
{"colour",required_argument,NULL,'c'},
{"gravity",required_argument,NULL,'g'},
{"vertical",optional_argument,NULL,'v'},
{"help",no_argument,0,'?'},
{0,0,0,0}
};
fprintf(stderr,"\nOverriding prefs from commandline ...\n");
prefs.LFSTK_argsToPrefs(argc,argv,long_options);
printPrefs();
int retval=apc->LFSTK_runApp();
delete apc;
cairo_debug_reset_static_data();
return retval;
}

View File

@ -0,0 +1,5 @@
theme /home/keithhedger/.themes/WinterWoodQT
colour #ff00ff
gravity 0xfe
vertical true