1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
{
appsettings.pas
Class which stores the program settings
Copyright (C) 1998 - 2007 Harri Pyy, Chris O'Donnell, Felipe Monteiro de Carvalho
This file is part of Virtual Magnifying Glass.
Virtual Magnifying Glass is free software;
you can redistribute it and/or modify it under the
terms of the GNU General Public License version 2
as published by the Free Software Foundation.
Virtual Magnifying Glass is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
Please note that the General Public License version 2 does not permit
incorporating Virtual Magnifying Glass into proprietary
programs.
AUTHORS: Chris O'Donnell, Felipe Monteiro de Carvalho and Harri Pyy
}
unit appsettings;
{$IFDEF FPC}
{$MODE DELPHI}{$H+}
{$ENDIF}
{$IFDEF Win32}
{$DEFINE Windows}
{$ENDIF}
interface
uses
{$IFDEF Windows}
Windows, shlobj,
{$ENDIF}
Classes, SysUtils, Forms, IniFiles, translationsvmg, constants;
type
{ TConfigurations }
TConfigurations = class(TObject)
private
ConfigFilePath: string;
public
iMagnification: Double;
iMagnificationMax: Integer;
xSquare, ySquare: Integer;
xSquareMax, ySquareMax: Integer;
showTools, graphicalBorder, AntiAliasing, ExtraAntiAliasing: Boolean;
showWhenExecuted, autoRun: Boolean;
invertColors: Boolean;
MyDirectory: string;
Language: Integer;
firstRun: Boolean;
// Hot Key
HotKeyMode: Integer;
HotKeyKey: Char;
// Plugins
UsePlugins: Boolean;
PluginName: string;
PluginData: Integer;
Contrast: Integer;
constructor Create;
destructor Destroy; override;
procedure ReadFromFile(Sender: TObject);
procedure Save(Sender: TObject);
function GetConfigFilePath: string;
function GetMyDirectory: string;
function GetSystemLanguage: Integer;
function GetOSVersion: TOSVersion;
end;
var
vConfigurations: TConfigurations;
implementation
{$ifdef Darwin}
uses
MacOSAll;
{$endif}
{ TConfigurations }
{*******************************************************************
* TConfigurations.Create ()
*
* DESCRIPTION: Creates an object,
* populates the class with the default configurations and
* then tryes to load the configurations from the XML file
*
* Under Mac OS X there is also the need to find the location
* of the Application Bundle
*
* PARAMETERS: None
*
* RETURNS: A pointer to the newly created object
*
*******************************************************************}
constructor TConfigurations.Create;
begin
{ First we use some good defaults in case the configuration file doesn't yet exist }
iMagnification := 2.0;
xSquare := 16;
ySquare := 10;
showTools := False;
graphicalBorder := True;
AntiAliasing := False;
showWhenExecuted := True;
autoRun := False;
invertColors := False;
Language := GetSystemLanguage();
HotKeyMode := 1;
HotKeyKey := 'm';
UsePlugins := False;
PluginName := 'pas_overlays.dll';
PluginData := -1;
{ Now identifies where the configuration file should be }
ConfigFilePath := GetConfigFilePath();
// Under Mac OS X we need to get the location of the bundle
MyDirectory := GetMyDirectory;
ReadFromFile(nil);
vTranslations.TranslateToLanguage(Language);
end;
{*******************************************************************
* TConfigurations.Destroy ()
*
* DESCRIPTION: Dont call this method directly. Use Free instead.
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
destructor TConfigurations.Destroy;
begin
Save(nil);
inherited Destroy;
end;
{*******************************************************************
* TConfigurations.ReadFromFile ()
*
* DESCRIPTION:
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
procedure TConfigurations.ReadFromFile(Sender: TObject);
var
MyFile: TIniFile;
begin
if not FileExists(ConfigFilePath) then Exit;
MyFile := TIniFile.Create(ConfigFilePath);
try
xSquare := MyFile.ReadInteger(SectionGeneral, IdentxSquare, 16);
ySquare := MyFile.ReadInteger(SectionGeneral, IdentySquare, 10);
iMagnification := MyFile.ReadFloat(SectionGeneral, IdentiMagnification, 2.0);
showTools := MyFile.ReadBool(SectionGeneral, IdentTools, False);
graphicalBorder := MyFile.ReadBool(SectionGeneral, IdentgraphicalBorder, True);
invertColors := MyFile.ReadBool(SectionGeneral, IdentInvertColors, False);
AntiAliasing := MyFile.ReadBool(SectionGeneral, IdentAntiAliasing, False);
ExtraAntiAliasing := MyFile.ReadBool(SectionGeneral, IdentExtraAntiAliasing, False);
showWhenExecuted := MyFile.ReadBool(SectionGeneral, IdentShowWhenExecuted, True);
autoRun := MyFile.ReadBool(SectionGeneral, IdentAutoRun, False);
Language := MyFile.ReadInteger(SectionGeneral, IdentLanguage, ID_MENU_ENGLISH);
firstRun := MyFile.ReadBool(SectionGeneral, IdentFirstRun, True);
Contrast := MyFile.ReadInteger(SectionGeneral, IdentContrast, 0);
{$ifdef UNIX}
{$ifndef DARWIN}
MyDirectory := MyFile.ReadString(SectionUnix, IdentMyDirectory, DefaultDirectory);
{$endif}
{$endif}
HotKeyMode := MyFile.ReadInteger(SectionWindows, IdentHotKeyMode, ID_HOTKEY_CTRLALTKEY);
HotKeyKey := MyFile.ReadString(SectionWindows, IdentHotKeyKey, ID_DEFAULT_HOTKEY)[1];
UsePlugins := MyFile.ReadBool(SectionPlugins, IdentUsePlugins, False);
PluginName := MyFile.ReadString(SectionPlugins, IdentPluginName, 'pas_overlays.dll');
PluginData := MyFile.ReadInteger(SectionPlugins, IdentPluginData, 0);
finally
MyFile.Free;
{*******************************************************************
* Fix for the default value apparently not working on Windows for Float
*******************************************************************}
if iMagnification = 0.0 then iMagnification := 2.0;
end;
end;
{*******************************************************************
* TConfigurations.Save ()
*
* DESCRIPTION:
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*******************************************************************}
procedure TConfigurations.Save(Sender: TObject);
var
MyFile: TIniFile;
begin
MyFile := TIniFile.Create(ConfigFilePath);
try
MyFile.WriteInteger(SectionGeneral, IdentxSquare, xSquare);
MyFile.WriteInteger(SectionGeneral, IdentySquare, ySquare);
MyFile.WriteFloat(SectionGeneral, IdentiMagnification, iMagnification);
MyFile.WriteBool(SectionGeneral, IdentTools, showTools);
MyFile.WriteBool(SectionGeneral, IdentgraphicalBorder, graphicalBorder);
MyFile.WriteBool(SectionGeneral, IdentInvertColors, invertColors);
MyFile.WriteBool(SectionGeneral, IdentAntiAliasing, AntiAliasing);
MyFile.WriteBool(SectionGeneral, IdentExtraAntiAliasing, ExtraAntiAliasing);
MyFile.WriteBool(SectionGeneral, IdentShowWhenExecuted, showWhenExecuted);
MyFile.WriteBool(SectionGeneral, IdentAutoRun, autoRun);
MyFile.WriteInteger(SectionGeneral, IdentLanguage, Language);
MyFile.WriteBool(SectionGeneral, IdentFirstRun, firstRun);
MyFile.WriteInteger(SectionGeneral, IdentContrast, Contrast);
MyFile.WriteString(SectionUnix, IdentMyDirectory, MyDirectory);
MyFile.WriteInteger(SectionWindows, IdentHotKeyMode, HotKeyMode);
MyFile.WriteString(SectionWindows, IdentHotKeyKey, HotKeyKey);
MyFile.WriteBool(SectionPlugins, IdentUsePlugins, UsePlugins);
MyFile.WriteString(SectionPlugins, IdentPluginName, PluginName);
MyFile.WriteInteger(SectionPlugins, IdentPluginData, PluginData);
finally
MyFile.Free;
end;
end;
function TConfigurations.GetConfigFilePath: string;
var
APath : Array[0..MAX_PATH] of char;
LocalConfigFile: string;
attrs: Integer;
begin
{$ifdef Windows}
// First tryes to use a configuration file in the application directory
// Necessary to use the magnifier as a "portable" application
LocalConfigFile := ExtractFilePath(Application.EXEName) + 'magnifier.ini';
// Only uses the file if it exist, isn't readonly and isn't a directory
attrs := FileGetAttr(LocalConfigFile);
if FileExists(LocalConfigFile) and (attrs and faReadOnly = 0)
and (attrs and faDirectory = 0) then
begin
Result := LocalConfigFile;
end
else
begin
// GetAppConfigFile from FPC is too unstable to be used
// Microsoft recomends CSIDL_APPDATA\Company\Product\Version\
Result := '';
SHGetSpecialFolderPath(0, APath, CSIDL_APPDATA, True);
// Another option: uses shfolder SHGetFolderPath(0, CSIDL_APPDATA or CSIDL_FLAG_CREATE,0,0,@APATH[0]);
Result := IncludeTrailingPathDelimiter(StrPas(@APath[0])) + 'Magnifier\';
ForceDirectories(Result);
Result := Result + 'magnifier.ini';
end;
{$endif}
{$ifdef Unix}
Result := GetEnvironmentVariable('HOME') + '/.magnifier.ini';
{$endif}
end;
function TConfigurations.GetMyDirectory: string;
{$ifdef Darwin}
var
pathRef: CFURLRef;
pathCFStr: CFStringRef;
pathStr: shortstring;
{$endif}
begin
{$ifdef UNIX}
{$ifdef Darwin}
pathRef := CFBundleCopyBundleURL(CFBundleGetMainBundle());
pathCFStr := CFURLCopyFileSystemPath(pathRef, kCFURLPOSIXPathStyle);
CFStringGetPascalString(pathCFStr, @pathStr, 255, CFStringGetSystemEncoding());
CFRelease(pathRef);
CFRelease(pathCFStr);
Result := pathStr + BundleResourcesDirectory;
{$else}
Result := DefaultDirectory;
{$endif}
{$endif}
{$ifdef Windows}
Result := ExtractFilePath(Application.EXEName);
{$endif}
end;
function TConfigurations.GetSystemLanguage: Integer;
{$ifdef Windows}
var
LangId: Word;
MainLangId: Byte;
begin
LangId := GetSystemDefaultLangID;
MainLangId := LangId;
case MainLangId of
LANG_PORTUGUESE: Result := ID_MENU_PORTUGUESE;
LANG_SPANISH: Result := ID_MENU_SPANISH;
LANG_FRENCH: Result := ID_MENU_FRENCH;
LANG_GERMAN: Result := ID_MENU_GERMAN;
LANG_ITALIAN: Result := ID_MENU_ITALIAN;
LANG_RUSSIAN: Result := ID_MENU_RUSSIAN;
LANG_POLISH: Result := ID_MENU_POLISH;
LANG_JAPANESE: Result := ID_MENU_JAPANESE;
else
Result := ID_MENU_ENGLISH;
end;
end;
{$endif}
{$ifdef UNIX}
begin
Result := ID_MENU_ENGLISH;
end;
{$endif}
{@@
Returns the Windows Version
}
function TConfigurations.GetOSVersion: TOSVersion;
{$IFDEF Windows}
var
Versao: TOSVersionInfo;
WinMajorVersion, WinMinorVersion: Cardinal;
begin
Result := vwDesconhecida;
{----------------------------------------------
Inicializations
----------------------------------------------}
Versao.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
if not GetVersionEx(Versao) then Exit;
WinMajorVersion := Versao.dwMajorVersion;
WinMinorVersion := Versao.dwMinorVersion;
{----------------------------------------------
Identifyes the Operating System
----------------------------------------------
Series 9x
----------------------------------------------}
if Versao.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
begin
if (WinMajorVersion = 4) and
(WinMinorVersion = 90) then
Result := vwWinMe
else if (WinMajorVersion = 4) and
(WinMinorVersion = 10) then
Result := vwWin98
else Result := vwWin95;
end
{----------------------------------------------
Series NT
----------------------------------------------}
else if Versao.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
if (WinMajorVersion = 3) and
(WinMinorVersion = 10) then
Result := vwWinNT351
else if (WinMajorVersion = 4) then
Result := vwWinNT
else if (WinMajorVersion = 5) and (WinMinorVersion = 0) then
Result := vwWin2000
else if (WinMajorVersion = 5) and (WinMinorVersion = 1) then
Result := vwWinXP
else if (WinMajorVersion = 5) and (WinMinorVersion = 2) then
Result := vwWin2003
else if (WinMajorVersion >= 6) then
Result := vwWinVista;
end
{----------------------------------------------
Windows CE
----------------------------------------------}
else if Versao.dwPlatformId = VER_PLATFORM_WIN32_CE then
begin
case WinMajorVersion of
1: Result := vwWinCE1;
2: Result := vwWinCE2;
3: Result := vwWinCE3;
4: Result := vwWinCE4;
else
Result := vwWinCE5;
end;
end;
end;
{$ELSE}
begin
Result := vwDesconhecida;
end;
{$ENDIF}
{*******************************************************************
* Initialization section
*
* DESCRIPTION: Upon startup an instance of the TConfigurations object
* is created to make the configuration info available for
* TGlass.
*
*******************************************************************}
initialization
vConfigurations := TConfigurations.Create;
{*******************************************************************
* Finalization section
*
* DESCRIPTION: Free memory allocated on the initialization section
*
*******************************************************************}
finalization
FreeAndNil(vConfigurations);
end.
|