📄 tree_utils.c
字号:
if(lpInc)
*lpInc = nIndex;
// we cannot return NULL treeNode yet
//return NULL;
TreeNode trJunk;
return trJunk;
}
int tree_copy_values(const TreeNode& trSrc, TreeNode& trDest, WORD wOptions)// = TREE_COPY_SKIP_HIDDEN | TREE_COPY_ATTRIB_ENABLE
{
bool bSkipHidden = (wOptions & TREE_COPY_SKIP_HIDDEN) ? true : false;
bool bCopyEnable = (wOptions & TREE_COPY_ATTRIB_ENABLE) ? true : false;
int nCount = 0;
foreach( TreeNode trSN in trSrc.Children )
{
TreeNode trDN = trDest.GetNode(trSN.tagName);
if( trDN )
{
if( trSN.GetNodeCount() > 0 ) // branch node
nCount += tree_copy_values(trSN, trDN, wOptions);
else
{
if( !bSkipHidden || trSN.Show != 0 )
{
trDN.strVal = trSN.strVal;
if( bCopyEnable )
trDN.Enable = trSN.Enable;
nCount++;
}
}
}
}
return nCount;
}
//---- CPY 5/30/03 QA70-4577 EVENT_HANDLER_NEED_DIALOG_AND_VALUE_UPDATE
/**
walk all tree nodes to delete specified attribute
Parameters:
tr = TreeNode to walk
lpcszAttribute = attribute name to delete
Returns:
total number of nodes the specified attribute is deleted
*/
int tree_remove_attribute(TreeNode& tr, LPCSTR lpcszAttribute)
{
int nCount = 0;
foreach(TreeNode cNode in tr.Children)
{
if(cNode.RemoveAttribute(lpcszAttribute))
nCount++;
if(cNode.GetNodeCount() > 0) // branch node
nCount += tree_remove_attribute(cNode, lpcszAttribute);
}
return nCount;
}
// if bSet, then NULL will clear changed
bool tree_node_changed(TreeNode& trNode, bool bSet, LPCSTR lpcszOldValue) // = false, = NULL
{
if(bSet)
{
if(lpcszOldValue)
trNode.SetAttribute(STR_CHANGED_ATTRIB, lpcszOldValue);
else
trNode.RemoveAttribute(STR_CHANGED_ATTRIB);
return true;
}
else // get
{
string strOldVal;
if(trNode.GetAttribute(STR_CHANGED_ATTRIB, strOldVal))
return true;
return false;
}
return false;
}
static void tree_node_get_value(TreeNode& trNode, string& strVal)
{
if(trNode.GetNodeCount() > 0) // branch node
strVal.Empty();
else
strVal = trNode.strVal;
}
// vs is assumed to be empty on top level call
void tree_get_values(TreeNode& tr, vector<string>& vs)
{
string strVal;
foreach(TreeNode cNode in tr.Children)
{
tree_node_get_value(cNode, strVal);
vs.Add(strVal);
if(cNode.GetNodeCount() > 0) // branch node
tree_get_values(cNode, vs);
}
}
/**
set numeric value as well as putting a descriptive string to the node
Parameters:
tr = TreeNode to set
dVal = a numerical value to set
lpcszLabel = a text label to associate
Example:
tree_node_set_value(trCalculation.t, tvalue, "t-Value");
Returns:
false if tr is invalid
bool tree_node_set_double(TreeNode& tr, double dVal, LPCSTR lpcszLabel)
*/
/**
get both the numeric value of a tree node as well as its associated label
Parameters:
tr = TreeNode to get
strLabel = the text label if present, otherwise the tagName
dDefault = default value in case node is not a valid node
*/
double tree_node_get_value(TreeNode& tr, string& strLabel, double dDefault)// = _ONAN);
{
strLabel.Empty();
if(!tr.IsValid())
return dDefault;
if(!tr.GetAttribute(STR_LABEL_ATTRIB, strLabel))
strLabel = tr.tagName;
return tr.dVal;
}
int tree_node_get_int(TreeNode& tr, int nDefault)// = -1)
{
if(tr)
return tr.nVal;
return nDefault;
}
// compare values in tree with those in vector and if diff, add attribute to remember
// return index into vs, which this function will increment according to the number of nodes
static int tree_count_and_update_changes(TreeNode& tr, const vector<string>& vs, int& nChanged, int nIndex = 0 )
{
string strVal;
foreach(TreeNode cNode in tr.Children)
{
tree_node_get_value(cNode, strVal);
if(strVal.Compare(vs[nIndex]) != 0)
{
tree_node_changed(cNode, true, vs[nIndex]);
nChanged++;
}
else
tree_node_changed(cNode, true);
nIndex++;
if(cNode.GetNodeCount() > 0) // branch node
nIndex = tree_count_and_update_changes(cNode, vs, nChanged, nIndex);
}
return nIndex;
}
/**
walk tree and store attribute "OldValue" into all the nodes that has different value then the specified string
Parameters:
tr = TreeNode to walk
vs = a linearized array of string values that should map to every tree node
Returns:
total number of nodes that has OldValue attributes added (changed)
Remark:
the vs array can be obtained by tree_get_values.
*/
int tree_update_changes(TreeNode& tr, const vector<string>& vs)
{
int nChanged = 0;
tree_count_and_update_changes(tr, vs, nChanged);
return nChanged;
}
//---- end CPY 5/30/03
///////////////////////////////////////////////////////////
// page.info and tree
///////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
////////// Info Variables and TreeNode utilities ////////////
/////////////////////////////////////////////////////////////
#define STR_USER "User"
#define STR_VARS "Variables"
static string make_link_var_str(string& strStorage, string& strSection, string& strItem, int nPlotInLayer)
{
string str;
if(nPlotInLayer < NPLOT_FOR_WKS)
{
if(strStorage.CompareNoCase(STR_USER) == 0 && strSection.CompareNoCase(STR_VARS) == 0)
str.Format("%%(%d, @W,%s)", nPlotInLayer, strItem);
else
str.Format("%%(%d, @W,%s.%s.%s)", nPlotInLayer, strStorage, strSection, strItem);
}
else
{
if(strStorage.CompareNoCase(STR_USER) == 0 && strSection.CompareNoCase(STR_VARS) == 0)
str.Format("%%(%%H, @W,%s)", strItem);
else
str.Format("%%(%%H, @W,%s.%s.%s)", strStorage, strSection, strItem);
}
return str;
}
//nPlotInLayer >=0 will add Data attribute into node for labtalk substition notation,
static bool tree_info_add_section(TreeNode& trNode, TreeNode& trSrc, string& strStorage, string& strSection, int nPlotInLayer)
{
string strFolder = strStorage + "." + strSection;
TreeNode trList = trNode.AddNode(strFolder);
foreach(TreeNode trSrcItem in trSrc.Children)
{
string strItem = trSrcItem.tagName;
string strStorageItem = strItem;
int nID;
if(trSrcItem.GetAttribute(STR_ID_ATTRIB, nID) && TRGP_STR == nID)
strStorageItem += "$";
TreeNode tr1;
tr1 = trList.AddTextNode(trSrcItem.strVal, strItem);
if(nPlotInLayer >= 0)
tr1.SetAttribute(STR_DATA_ATTRIB, make_link_var_str(strStorage, strSection, strStorageItem, nPlotInLayer));
}
return true;
}
//bool tree_add_info(TreeNode& trNode, const OriginObject& orgObj, const string& strName, const string& strLabel, int nPlotInLayer) // = -1
bool tree_add_info(TreeNode& trNode, const OriginObject& orgObj, LPCSTR lpcszObjName, LPCSTR lpcszObjLabel, int nObjIndex ) // = NULL, = NULL, = -1
{
if(!orgObj)
return false;
TreeNode trPage;
string strName = lpcszObjName;
if(!strName.IsEmpty())
{
string strLabel = lpcszObjLabel;
trPage = trNode.AddNode(strName);
trPage.SetAttribute(STR_LABEL_ATTRIB, strLabel);
}
else
trPage = trNode;
vector<string> vsStoreNames;
if(orgObj.GetStorageNames(vsStoreNames))
{
for(int ii = 0; ii < vsStoreNames.GetSize(); ii++)
{
storage st;
st = orgObj.GetStorage(vsStoreNames[ii]);
vector<string> vsSectionNames;
if(st.GetSectionNames(vsSectionNames))
{
for(int jj = 0; jj < vsSectionNames.GetSize(); jj++)
{
Tree trTemp;
if(st.GetSection(vsSectionNames[jj], trTemp))
tree_info_add_section(trPage, trTemp, vsStoreNames[ii], vsSectionNames[jj], nObjIndex);
}
}
}
return trPage.GetNodeCount()> 0? true : false;
}
return false;
}
////////////////////////////////////////////////////////////////////////
/////////// Saving and Loading of settings /////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
#define STR_DEFAULT_SETTINGS_FILE_EXTENSION "xml"
static string get_default_settings_folder_name(int nCategory)
{
// must match enum {SETTINGS_MAIN, SETTINGS_GUI};
vector<string> vsCategorys = {"", "GUI"};
string strSubFolder;
if(nCategory >= 0 && nCategory < vsCategorys.GetSize())
strSubFolder = vsCategorys[nCategory];
string strFolder = "DefaultSettings";
if(!strSubFolder.IsEmpty())
strFolder += "\\" + strSubFolder;
return strFolder;
}
void save_default_settings(TreeNode& tr, LPCSTR lpcszClassName, int nCategory)
{
string strPath = GetAppPath() + get_default_settings_folder_name(nCategory);
if( CheckMakePath(strPath) )
{
ASSERT(strPath.IsPath());
string strFileName;
strFileName.Format("%s\\%s.%s", strPath, lpcszClassName, STR_DEFAULT_SETTINGS_FILE_EXTENSION);
Tree trTemp;
trTemp = tr;
tree_remove_attribute(trTemp, STR_COMBO_ATTRIB);//CPY 5/31/03, this will make the xml file smaller and easier to read
trTemp.Save(strFileName);
}
}
BOOL load_default_settings(Tree& tr, LPCSTR lpcszClassName, int nCategory)
{
string strPath = GetAppPath() + get_default_settings_folder_name(nCategory);
string strFileName;
strFileName.Format("%s\\%s.%s", strPath, lpcszClassName, STR_DEFAULT_SETTINGS_FILE_EXTENSION);
if( strFileName.IsFile() )
{
tr.Load(strFileName);
return TRUE;
}
return FALSE;
}
/////////// dialog check boxes settings are stored in registry
static DWORD byte_array_to_bits(const vector<byte>& vn)
{
DWORD dwTemp = 0;
for(int ii = 0; ii < vn.GetSize(); ii++)
{
if(vn[ii] > 0)
dwTemp += 1 << ii;
}
return dwTemp;
}
static void bits_to_byte_array(DWORD dw, vector<byte>& vn)
{
vn.SetSize(0);
DWORD dwTest = 1;
for(int ii = 0; ii < 32; ii++)
{
if(dw & dwTest)
vn.Add(1);
else
vn.Add(0);
dwTest<<=1;
}
}
#define STR_REG_DIALOGS "\\Dialogs\\"
#define STR_REG_CHECKBOX_KEY "CheckBoxes"
bool dlg_save_to_registry(LPCSTR lpcszSecName, LPCSTR lpcszValName, DWORD dwVal)
{
Registry reg(HKEY_CURRENT_USER);
string strKey = GetRegKey() + STR_REG_DIALOGS;
strKey+= lpcszSecName;
reg.SetValue(strKey, lpcszValName, dwVal);
return true;
}
bool dlg_load_registry(LPCSTR lpcszSecName, LPCSTR lpcszValName, DWORD& dwVal)
{
Registry reg(HKEY_CURRENT_USER);
string strKey = GetRegKey() + STR_REG_DIALOGS;
strKey+= lpcszSecName;
dwVal = 0;
if(reg.GetValue(strKey, lpcszValName, dwVal))
return true;
return false;
}
//save the given array of boolean values into a single DWORD in the registry
bool save_default_checkboxes(LPCSTR lpcszDlgName, const vector<byte>& vbValues, LPCSTR lpcszValName) // = NULL
{
string strValName = lpcszValName;
if(strValName.IsEmpty())
strValName = STR_REG_CHECKBOX_KEY;
return dlg_save_to_registry(lpcszDlgName, strValName, byte_array_to_bits(vbValues));
}
//retrive a array of boolean values saved by save_default_checkboxes in the registry
bool load_default_checkboxes(LPCSTR lpcszDlgName, vector<byte>& vbValues, LPCSTR lpcszValName) // = NULL
{
string strValName = lpcszValName;
if(strValName.IsEmpty())
strValName = STR_REG_CHECKBOX_KEY;
DWORD dwVal = 0;
if(dlg_load_registry(lpcszDlgName, strValName, dwVal))
{
bits_to_byte_array(dwVal, vbValues);
return true;
}
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -