GR32.Examples.pas 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. unit GR32.Examples;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Anders Melander <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2000-2009
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$include GR32.inc}
  35. //------------------------------------------------------------------------------
  36. //
  37. // Utilities for use by the Graphics32 examples
  38. //
  39. //------------------------------------------------------------------------------
  40. type
  41. Graphics32Examples = record
  42. // The location of the examples Media files (bitmaps, etc)
  43. class function MediaFolder: string; static;
  44. // Look for a file in the examples Media folder
  45. class function MediaFileExists(const Filename: string): boolean; static;
  46. end;
  47. implementation
  48. uses
  49. {$ifndef FPC}
  50. IOUtils,
  51. {$endif FPC}
  52. SysUtils;
  53. // FreePascal support
  54. {$ifdef FPC}
  55. type
  56. TPath = record
  57. class function GetDirectoryName(const APath: string): string; static;
  58. class function Combine(const APath, BPath: string): string; static;
  59. end;
  60. TDirectory = record
  61. class function Exists(const AFoldername: string): boolean; static;
  62. class function GetParent(const AFoldername: string): string; static;
  63. end;
  64. TFile = record
  65. class function Exists(const AFilename: string): boolean; static;
  66. end;
  67. class function TPath.GetDirectoryName(const APath: string): string;
  68. begin
  69. Result := ExtractFileDir(APath);
  70. end;
  71. class function TPath.Combine(const APath, BPath: string): string;
  72. begin
  73. Result := ConcatPaths([APath, BPath]);
  74. end;
  75. class function TDirectory.Exists(const AFoldername: string): boolean;
  76. begin
  77. Result := DirectoryExists(AFoldername);
  78. end;
  79. class function TDirectory.GetParent(const AFoldername: string): string;
  80. begin
  81. Result := ExtractFileDir(ExcludeTrailingPathDelimiter(AFoldername));
  82. end;
  83. class function TFile.Exists(const AFilename: string): boolean;
  84. begin
  85. Result := FileExists(AFilename);
  86. end;
  87. {$endif FPC}
  88. var
  89. FGraphics32MediaFolder: string;
  90. FGraphics32MediaFolderFailed: boolean = False;
  91. function GetGraphics32MediaFolder(RaiseOnFail: boolean): boolean;
  92. const
  93. sFolderName = 'Media';
  94. var
  95. ParentFolder: string;
  96. NewParentFolder: string;
  97. begin
  98. if (not FGraphics32MediaFolderFailed) and (FGraphics32MediaFolder = '') then
  99. begin
  100. ParentFolder := TPath.GetDirectoryName(ParamStr(0));
  101. FGraphics32MediaFolder := TPath.Combine(ParentFolder, sFolderName);
  102. while (not TDirectory.Exists(FGraphics32MediaFolder)) do
  103. begin
  104. NewParentFolder := TDirectory.GetParent(ParentFolder);
  105. if (NewParentFolder = ParentFolder) then
  106. begin
  107. FGraphics32MediaFolderFailed := True;
  108. break;
  109. end;
  110. ParentFolder := NewParentFolder;
  111. FGraphics32MediaFolder := TPath.Combine(ParentFolder, sFolderName);
  112. end;
  113. end;
  114. if (RaiseOnFail and FGraphics32MediaFolderFailed) then
  115. raise Exception.CreateFmt('Graphics32 examples %s folder not found', [sFolderName]);
  116. Result := (not FGraphics32MediaFolderFailed);
  117. end;
  118. class function Graphics32Examples.MediaFolder: string;
  119. begin
  120. GetGraphics32MediaFolder(True);
  121. Result := FGraphics32MediaFolder;
  122. end;
  123. class function Graphics32Examples.MediaFileExists(const Filename: string): boolean;
  124. begin
  125. Result := GetGraphics32MediaFolder(False) and TFile.Exists(FGraphics32MediaFolder + '\' + Filename);
  126. end;
  127. end.