627 lines
11 KiB
C++
627 lines
11 KiB
C++
#include "compileconfig.h"
|
|
#include "programresources.h"
|
|
|
|
|
|
|
|
namespace ProgramResourcesGlobal
|
|
{
|
|
ProgramResources program_resource;
|
|
}
|
|
|
|
|
|
ProgramResources * GetPrgRes()
|
|
{
|
|
return &ProgramResourcesGlobal::program_resource;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ProgramResources::SetXPos(int x)
|
|
{
|
|
x_pos = x;
|
|
}
|
|
|
|
|
|
void ProgramResources::SetYPos(int y)
|
|
{
|
|
y_pos = y;
|
|
}
|
|
|
|
|
|
int ProgramResources::GetXPos()
|
|
{
|
|
return x_pos;
|
|
}
|
|
|
|
|
|
int ProgramResources::GetYPos()
|
|
{
|
|
return y_pos;
|
|
}
|
|
|
|
|
|
void ProgramResources::SetXSize(int x)
|
|
{
|
|
x_size = x;
|
|
}
|
|
|
|
void ProgramResources::SetYSize(int y)
|
|
{
|
|
y_size = y;
|
|
}
|
|
|
|
int ProgramResources::GetXSize()
|
|
{
|
|
return x_size;
|
|
}
|
|
|
|
int ProgramResources::GetYSize()
|
|
{
|
|
return y_size;
|
|
}
|
|
|
|
void ProgramResources::SetYSizeNormal(int y)
|
|
{
|
|
y_size_normal = y;
|
|
}
|
|
|
|
int ProgramResources::GetYSizeNormal()
|
|
{
|
|
return y_size_normal;
|
|
}
|
|
|
|
void ProgramResources::SetYSizeCompact(int y)
|
|
{
|
|
y_size_compact = y;
|
|
}
|
|
|
|
int ProgramResources::GetYSizeCompact()
|
|
{
|
|
return y_size_compact;
|
|
}
|
|
|
|
void ProgramResources::SetXSizeMin(int x)
|
|
{
|
|
x_size_min = x;
|
|
}
|
|
|
|
int ProgramResources::GetXSizeMin()
|
|
{
|
|
return x_size_min;
|
|
}
|
|
|
|
|
|
void ProgramResources::SetView(ProgramResources::View v)
|
|
{
|
|
view = v;
|
|
|
|
if( v == view_normal)
|
|
MoveWindow(main_window, x_pos, y_pos, x_size, y_size_normal, true);
|
|
else
|
|
MoveWindow(main_window, x_pos, y_pos, y_size, y_size_compact, true);
|
|
}
|
|
|
|
|
|
ProgramResources::View ProgramResources::GetView()
|
|
{
|
|
return view;
|
|
}
|
|
|
|
|
|
void ProgramResources::SetMaximized(bool max)
|
|
{
|
|
maximized = max;
|
|
|
|
if( maximized )
|
|
ShowWindow(main_window, SW_SHOWMAXIMIZED);
|
|
else
|
|
ShowWindow(main_window, SW_SHOWNORMAL);
|
|
}
|
|
|
|
|
|
bool ProgramResources::GetMaximized()
|
|
{
|
|
return maximized;
|
|
}
|
|
|
|
void ProgramResources::SetAlwaysOnTop(bool always)
|
|
{
|
|
always_on_top = always;
|
|
|
|
if( always )
|
|
SetWindowPos(main_window, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
|
|
else
|
|
SetWindowPos(main_window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
|
|
}
|
|
|
|
bool ProgramResources::GetAlwaysOnTop()
|
|
{
|
|
return always_on_top;
|
|
}
|
|
|
|
|
|
void ProgramResources::SetDisplayRounding(int r)
|
|
{
|
|
if(r<-2)
|
|
r = -2;
|
|
|
|
if(r>99)
|
|
r = 99;
|
|
|
|
display_rounding = r;
|
|
}
|
|
|
|
int ProgramResources::GetDisplayRounding()
|
|
{
|
|
return display_rounding;
|
|
}
|
|
|
|
void ProgramResources::SetDisplayAlwaysScientific(bool a)
|
|
{
|
|
display_always_scientific = a;
|
|
}
|
|
|
|
bool ProgramResources::GetDisplayAlwaysScientific()
|
|
{
|
|
return display_always_scientific;
|
|
}
|
|
|
|
void ProgramResources::SetDisplayWhenScientific(int w)
|
|
{
|
|
if(w<1)
|
|
w = 1;
|
|
|
|
if( w > 99 )
|
|
w = 99;
|
|
|
|
display_when_scientific = w;
|
|
}
|
|
|
|
int ProgramResources::GetDisplayWhenScientific()
|
|
{
|
|
return display_when_scientific;
|
|
}
|
|
|
|
|
|
|
|
void ProgramResources::SetBaseInput(int i)
|
|
{
|
|
if(i<2)
|
|
i = 2;
|
|
|
|
if(i>16)
|
|
i = 16;
|
|
|
|
base_input = i;
|
|
}
|
|
|
|
|
|
int ProgramResources::GetBaseInput()
|
|
{
|
|
return base_input;
|
|
}
|
|
|
|
|
|
void ProgramResources::SetBaseOutput(int i)
|
|
{
|
|
if(i<2)
|
|
i = 2;
|
|
|
|
if(i>16)
|
|
i = 16;
|
|
|
|
base_output = i;
|
|
}
|
|
|
|
|
|
int ProgramResources::GetBaseOutput()
|
|
{
|
|
return base_output;
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
ttmath::Objects * ProgramResources::GetVariables()
|
|
{
|
|
return &variables;
|
|
}
|
|
|
|
ttmath::Objects * ProgramResources::GetFunctions()
|
|
{
|
|
return &functions;
|
|
}
|
|
|
|
Languages * ProgramResources::GetLanguages()
|
|
{
|
|
return &languages;
|
|
}
|
|
|
|
void ProgramResources::SetInstance(HINSTANCE h)
|
|
{
|
|
hInstance = h;
|
|
}
|
|
|
|
HINSTANCE ProgramResources::GetInstance()
|
|
{
|
|
return hInstance;
|
|
}
|
|
|
|
void ProgramResources::SetMainWindow(HWND h)
|
|
{
|
|
main_window = h;
|
|
}
|
|
|
|
HWND ProgramResources::GetMainWindow()
|
|
{
|
|
return main_window;
|
|
}
|
|
|
|
void ProgramResources::SetTabWindow(unsigned int id, HWND h)
|
|
{
|
|
if( id >= sizeof(tab_window) / sizeof(HWND) )
|
|
return;
|
|
|
|
tab_window[id] = h;
|
|
}
|
|
|
|
HWND ProgramResources::GetTabWindow(unsigned int id)
|
|
{
|
|
if( id >= sizeof(tab_window) / sizeof(HWND) )
|
|
return 0;
|
|
|
|
return tab_window[id];
|
|
}
|
|
|
|
unsigned int ProgramResources::HowManyTabWindows()
|
|
{
|
|
return sizeof(tab_window) / sizeof(HWND);
|
|
}
|
|
|
|
char * ProgramResources::GetBuffer()
|
|
{
|
|
return buffer;
|
|
}
|
|
|
|
unsigned int ProgramResources::GetBufferSize()
|
|
{
|
|
return buffer_size;
|
|
}
|
|
|
|
volatile ThreadController * ProgramResources::GetThreadController()
|
|
{
|
|
return &thread_controller;
|
|
}
|
|
|
|
void ProgramResources::SetPrecision(int p)
|
|
{
|
|
if( p < 0 )
|
|
p = 0;
|
|
|
|
if( p > 2 )
|
|
p = 2;
|
|
|
|
precision = p;
|
|
}
|
|
|
|
int ProgramResources::GetPrecision()
|
|
{
|
|
return precision;
|
|
}
|
|
|
|
|
|
|
|
ProgramResources::ProgramResources()
|
|
{
|
|
buffer_size = 300;
|
|
buffer = new char[buffer_size];
|
|
buffer[0] = 0;
|
|
|
|
|
|
always_on_top = false;
|
|
view = view_normal;
|
|
|
|
maximized = false;
|
|
|
|
precision = 0;
|
|
|
|
hInstance = 0;
|
|
main_window = 0;
|
|
|
|
base_input = 10;
|
|
base_output = 10;
|
|
|
|
display_always_scientific = false;
|
|
display_when_scientific = 15;
|
|
display_rounding = -2;
|
|
|
|
for(int i=HowManyTabWindows()-1 ; i!=-1 ; --i)
|
|
tab_window[i] = 0;
|
|
|
|
x_pos = 0;
|
|
y_pos = 0;
|
|
x_size = 100;
|
|
y_size = 100;
|
|
|
|
y_size_normal = 0;
|
|
y_size_compact = 0;
|
|
x_size_min = 0;
|
|
}
|
|
|
|
|
|
ProgramResources::~ProgramResources()
|
|
{
|
|
delete [] buffer;
|
|
}
|
|
|
|
|
|
|
|
bool ProgramResources::IsWhiteCharacter(int c)
|
|
{
|
|
if( c==' ' || c=='\t' || c==13 || c=='\n' )
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
const char * ProgramResources::SkipWhiteCharacters(const char * string)
|
|
{
|
|
while( IsWhiteCharacter(*string) )
|
|
++string;
|
|
|
|
return string;
|
|
}
|
|
|
|
bool ProgramResources::IsDecDigit(int c)
|
|
{
|
|
if( c>='0' && c<='9' )
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ProgramResources::SplitFunction(const std::string & input, const char * * name, int * param)
|
|
{
|
|
const char * pchar = input.c_str();
|
|
const int buffer_len = 20;
|
|
char buffer[buffer_len];
|
|
int i;
|
|
|
|
pchar = SkipWhiteCharacters(pchar);
|
|
|
|
for(i=0 ; i<buffer_len-1 && IsDecDigit(*pchar) ; ++i, ++pchar)
|
|
buffer[i] = *pchar;
|
|
|
|
buffer[i] = 0;
|
|
|
|
*param = atoi(buffer);
|
|
if(*param < 0)
|
|
*param = 0;
|
|
else
|
|
if(*param > 9)
|
|
*param = 9;
|
|
|
|
|
|
//
|
|
while( IsDecDigit(*pchar) );
|
|
|
|
pchar = SkipWhiteCharacters(pchar);
|
|
if( *pchar != '|' )
|
|
return false;
|
|
|
|
pchar = SkipWhiteCharacters(pchar+1);
|
|
|
|
*name = pchar;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool ProgramResources::IsWindowsNt()
|
|
{
|
|
OSVERSIONINFO os_info;
|
|
|
|
os_info.dwOSVersionInfoSize = sizeof( os_info );
|
|
|
|
if( !GetVersionEx( &os_info ) )
|
|
return false;
|
|
|
|
if( os_info.dwPlatformId != VER_PLATFORM_WIN32_NT )
|
|
// we've got Windows 3.1, Windows 95 or Windows 98
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool ProgramResources::ReadTextValueFromRegistry(HKEY main_key, const char * sub_key, const char * value, std::string & result)
|
|
{
|
|
HKEY reg_key;
|
|
|
|
if( RegOpenKeyEx( main_key, sub_key, 0, KEY_QUERY_VALUE, ®_key ) != ERROR_SUCCESS )
|
|
return false;
|
|
|
|
bool status = false;
|
|
DWORD result_type;
|
|
char result_buffer[300];
|
|
DWORD buffer_size = sizeof(result_buffer) / sizeof(char);
|
|
|
|
if( RegQueryValueEx( reg_key, value, 0, &result_type, reinterpret_cast<BYTE*>(result_buffer), &buffer_size ) == ERROR_SUCCESS )
|
|
{
|
|
if( result_type == REG_SZ )
|
|
{
|
|
result = result_buffer;
|
|
status = true;
|
|
}
|
|
}
|
|
|
|
RegCloseKey(reg_key);
|
|
|
|
return status;
|
|
}
|
|
|
|
|
|
|
|
void ProgramResources::SetNameOfConfigurationFile()
|
|
{
|
|
static const char simple_file_name[] = "ttcalc.ini";
|
|
static const char simple_directory_name[] = "ttcalc";
|
|
std::string application_data;
|
|
|
|
// if there'll be an error we assume that the current directory will be used
|
|
configuration_file = std::string(".\\") + simple_file_name;
|
|
|
|
// for the first we must check which operating system we're using
|
|
if( !IsWindowsNt() )
|
|
return;
|
|
|
|
// we're trying to read the value "AppData" from registry
|
|
// which can be, for instance, as:
|
|
// "C:\Documents and Settings\user\data application"
|
|
if( ReadTextValueFromRegistry(
|
|
HKEY_CURRENT_USER,
|
|
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
|
|
"AppData",
|
|
application_data) )
|
|
{
|
|
std::string application_directory = application_data + '\\' + simple_directory_name;
|
|
bool success = false;
|
|
|
|
// we're testing whether we've got our directory or not
|
|
if( SetCurrentDirectory( application_directory.c_str() ) )
|
|
success = true;
|
|
else
|
|
// now we must make our directory inside it
|
|
if( CreateDirectory(application_directory.c_str(), 0) )
|
|
success = true;
|
|
|
|
if( success )
|
|
configuration_file = application_directory + '\\' + simple_file_name;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
IniParser::Error ProgramResources::ReadFromFile()
|
|
{
|
|
IniParser iparser;
|
|
IniParser::Section temp_variables, temp_functions;
|
|
IniParser::Section::iterator ic;
|
|
std::string ini_value[12];
|
|
|
|
|
|
iparser.Associate( "global|always.on.top", &ini_value[0] );
|
|
iparser.Associate( "global|view", &ini_value[1] );
|
|
iparser.Associate( "global|maximized", &ini_value[2] );
|
|
iparser.Associate( "global|x", &ini_value[3] );
|
|
iparser.Associate( "global|y", &ini_value[4] );
|
|
iparser.Associate( "global|size.x", &ini_value[5] );
|
|
iparser.Associate( "global|precision", &ini_value[6] );
|
|
iparser.Associate( "global|disp.input", &ini_value[7] );
|
|
iparser.Associate( "global|disp.output", &ini_value[8] );
|
|
iparser.Associate( "global|disp.alw.scientific", &ini_value[9] );
|
|
iparser.Associate( "global|disp.when.scientific", &ini_value[10] );
|
|
iparser.Associate( "global|disp.rounding", &ini_value[11] );
|
|
|
|
iparser.Associate( "variables", &temp_variables );
|
|
iparser.Associate( "functions", &temp_functions );
|
|
|
|
bad_line = -1;
|
|
IniParser::Error err = iparser.ReadFromFile( configuration_file.c_str() );
|
|
|
|
if( err == IniParser::err_cant_open_file )
|
|
return err;
|
|
|
|
// we're adding variables
|
|
variables.Clear();
|
|
for( ic = temp_variables.begin() ; ic!=temp_variables.end() ; ++ic )
|
|
variables.Add(ic->first, ic->second);
|
|
|
|
// we're adding functions
|
|
functions.Clear();
|
|
for( ic = temp_functions.begin() ; ic!=temp_functions.end() ; ++ic )
|
|
{
|
|
const char * name;
|
|
int param;
|
|
|
|
if( SplitFunction(ic->second, &name, ¶m) )
|
|
functions.Add(ic->first, name, param);
|
|
}
|
|
|
|
// we're setting various values
|
|
always_on_top = bool( atoi(ini_value[0].c_str()) );
|
|
view = atoi(ini_value[1].c_str()) == 0 ? view_normal : view_compact;
|
|
maximized = bool( atoi(ini_value[2].c_str()) );
|
|
x_pos = atoi( ini_value[3].c_str() );
|
|
y_pos = atoi( ini_value[4].c_str() );
|
|
x_size = atoi( ini_value[5].c_str() );
|
|
|
|
SetPrecision( atoi(ini_value[6].c_str()) );
|
|
SetBaseInput( atoi(ini_value[7].c_str()) );
|
|
SetBaseOutput( atoi(ini_value[8].c_str()) );
|
|
|
|
SetDisplayAlwaysScientific( bool(atoi(ini_value[9].c_str())) );
|
|
SetDisplayWhenScientific( atoi(ini_value[10].c_str()) );
|
|
SetDisplayRounding( atoi(ini_value[11].c_str()) );
|
|
|
|
if( err != IniParser::err_ok )
|
|
bad_line = iparser.GetBadLine();
|
|
|
|
return err;
|
|
}
|
|
|
|
|
|
|
|
void ProgramResources::SaveToFile()
|
|
{
|
|
std::ofstream file( configuration_file.c_str() );
|
|
|
|
if( !file )
|
|
return;
|
|
|
|
file << "# the configuration file of the program ttcalc\n\n";
|
|
file << "[GLOBAL]\n";
|
|
|
|
file << "always.on.top = " << (int)always_on_top << std::endl;
|
|
file << "view = " << (int)view << std::endl;
|
|
file << "maximized = " << (int)maximized << std::endl;
|
|
file << "x = " << x_pos << std::endl;
|
|
file << "y = " << y_pos << std::endl;
|
|
file << "size.x = " << x_size << std::endl;
|
|
file << "precision = " << precision << std::endl;
|
|
file << "disp.input = " << base_input << std::endl;
|
|
file << "disp.output = " << base_output << std::endl;
|
|
|
|
file << "disp.alw.scientific = " << (int)display_always_scientific << std::endl;
|
|
file << "disp.when.scientific = " << display_when_scientific << std::endl;
|
|
file << "disp.rounding = " << display_rounding << std::endl;
|
|
|
|
file << "\n\n[variables]\n";
|
|
|
|
ttmath::Objects::CIterator iv = variables.Begin();
|
|
|
|
for( ; iv != variables.End() ; ++iv )
|
|
file << iv->first.c_str() << " = " << iv->second.value.c_str() << std::endl;
|
|
|
|
file << "\n\n[functions]\n";
|
|
|
|
iv = functions.Begin();
|
|
for( ; iv != functions.End() ; ++iv )
|
|
file << iv->first.c_str() << " = " << iv->second.param <<
|
|
" | " << iv->second.value.c_str() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
int ProgramResources::GetBadLine()
|
|
{
|
|
return bad_line;
|
|
}
|
|
|
|
|
|
|