stddlg.pas 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  1. {*******************************************************}
  2. { Free Vision Runtime Library }
  3. { StdDlg Unit }
  4. { Version: 0.1.0 }
  5. { Release Date: July 23, 1998 }
  6. { }
  7. {*******************************************************}
  8. { }
  9. { This unit is a port of Borland International's }
  10. { StdDlg.pas unit. It is for distribution with the }
  11. { Free Pascal (FPK) Compiler as part of the 32-bit }
  12. { Free Vision library. The unit is still fully }
  13. { functional under BP7 by using the tp compiler }
  14. { directive when rebuilding the library. }
  15. { }
  16. {*******************************************************}
  17. { Revision History
  18. 1.1a (97/12/29)
  19. - fixed bug in TFileDialog.HandleEvent that prevented the user from being
  20. able to have an action taken automatically when the FileList was
  21. selected and kbEnter pressed
  22. 1.1
  23. - modified OpenNewFile to take a history list ID
  24. - implemented OpenNewFile
  25. 1.0 (1992)
  26. - original implementation }
  27. unit StdDlg;
  28. {
  29. This unit has been modified to make some functions global, apply patches
  30. from version 3.1 of the TVBUGS list, added TEditChDirDialog, and added
  31. several new global functions and procedures.
  32. }
  33. {$i platform.inc}
  34. {$ifdef PPC_FPC}
  35. {$H-}
  36. {$else}
  37. {$F+,O+,E+,N+}
  38. {$endif}
  39. {$X+,R-,I-,Q-,V-}
  40. {$ifndef OS_UNIX}
  41. {$S-}
  42. {$endif}
  43. {$ifdef OS_DOS}
  44. {$define HAS_DOS_DRIVES}
  45. {$endif}
  46. {$ifdef OS_WINDOWS}
  47. {$define HAS_DOS_DRIVES}
  48. {$endif}
  49. {$ifdef OS_OS2}
  50. {$define HAS_DOS_DRIVES}
  51. {$endif}
  52. interface
  53. uses
  54. FVConsts, Objects, Drivers, Views, Dialogs, Validate, Dos;
  55. const
  56. MaxDir = 255; { Maximum length of a DirStr. }
  57. MaxFName = 255; { Maximum length of a FNameStr. }
  58. DirSeparator : Char = system.DirectorySeparator;
  59. {$ifdef Unix}
  60. AllFiles = '*';
  61. {$else}
  62. AllFiles = '*.*';
  63. {$endif}
  64. type
  65. { TSearchRec }
  66. { Record used to store directory information by TFileDialog
  67. This is a part of Dos.Searchrec for Bp !! }
  68. TSearchRec =
  69. {$ifndef FPC_REQUIRES_PROPER_ALIGNMENT}
  70. packed
  71. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  72. record
  73. Attr: Longint;
  74. Time: Longint;
  75. Size: Longint;
  76. Name: string[MaxFName];
  77. end;
  78. PSearchRec = ^TSearchRec;
  79. type
  80. { TFileInputLine is a special input line that is used by }
  81. { TFileDialog that will update its contents in response to a }
  82. { cmFileFocused command from a TFileList. }
  83. PFileInputLine = ^TFileInputLine;
  84. TFileInputLine = object(TInputLine)
  85. constructor Init(var Bounds: TRect; AMaxLen: Sw_Integer);
  86. procedure HandleEvent(var Event: TEvent); virtual;
  87. end;
  88. { TFileCollection is a collection of TSearchRec's. }
  89. PFileCollection = ^TFileCollection;
  90. TFileCollection = object(TSortedCollection)
  91. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  92. procedure FreeItem(Item: Pointer); virtual;
  93. function GetItem(var S: TStream): Pointer; virtual;
  94. procedure PutItem(var S: TStream; Item: Pointer); virtual;
  95. end;
  96. {#Z+}
  97. PFileValidator = ^TFileValidator;
  98. {#Z-}
  99. TFileValidator = Object(TValidator)
  100. end; { of TFileValidator }
  101. { TSortedListBox is a TListBox that assumes it has a }
  102. { TStoredCollection instead of just a TCollection. It will }
  103. { perform an incremental search on the contents. }
  104. PSortedListBox = ^TSortedListBox;
  105. TSortedListBox = object(TListBox)
  106. SearchPos: Byte;
  107. {ShiftState: Byte;}
  108. HandleDir : boolean;
  109. constructor Init(var Bounds: TRect; ANumCols: Sw_Word;
  110. AScrollBar: PScrollBar);
  111. procedure HandleEvent(var Event: TEvent); virtual;
  112. function GetKey(var S: String): Pointer; virtual;
  113. procedure NewList(AList: PCollection); virtual;
  114. end;
  115. { TFileList is a TSortedList box that assumes it contains }
  116. { a TFileCollection as its collection. It also communicates }
  117. { through broadcast messages to TFileInput and TInfoPane }
  118. { what file is currently selected. }
  119. PFileList = ^TFileList;
  120. TFileList = object(TSortedListBox)
  121. constructor Init(var Bounds: TRect; AScrollBar: PScrollBar);
  122. destructor Done; virtual;
  123. function DataSize: Sw_Word; virtual;
  124. procedure FocusItem(Item: Sw_Integer); virtual;
  125. procedure GetData(var Rec); virtual;
  126. function GetText(Item,MaxLen: Sw_Integer): String; virtual;
  127. function GetKey(var S: String): Pointer; virtual;
  128. procedure HandleEvent(var Event: TEvent); virtual;
  129. procedure ReadDirectory(AWildCard: PathStr);
  130. procedure SetData(var Rec); virtual;
  131. end;
  132. { TFileInfoPane is a TView that displays the information }
  133. { about the currently selected file in the TFileList }
  134. { of a TFileDialog. }
  135. PFileInfoPane = ^TFileInfoPane;
  136. TFileInfoPane = object(TView)
  137. S: TSearchRec;
  138. constructor Init(var Bounds: TRect);
  139. procedure Draw; virtual;
  140. function GetPalette: PPalette; virtual;
  141. procedure HandleEvent(var Event: TEvent); virtual;
  142. end;
  143. { TFileDialog is a standard file name input dialog }
  144. TWildStr = PathStr;
  145. const
  146. fdOkButton = $0001; { Put an OK button in the dialog }
  147. fdOpenButton = $0002; { Put an Open button in the dialog }
  148. fdReplaceButton = $0004; { Put a Replace button in the dialog }
  149. fdClearButton = $0008; { Put a Clear button in the dialog }
  150. fdHelpButton = $0010; { Put a Help button in the dialog }
  151. fdNoLoadDir = $0100; { Do not load the current directory }
  152. { contents into the dialog at Init. }
  153. { This means you intend to change the }
  154. { WildCard by using SetData or store }
  155. { the dialog on a stream. }
  156. type
  157. PFileHistory = ^TFileHistory;
  158. TFileHistory = object(THistory)
  159. CurDir : PString;
  160. procedure HandleEvent(var Event: TEvent);virtual;
  161. destructor Done; virtual;
  162. procedure AdaptHistoryToDir(Dir : string);
  163. end;
  164. PFileDialog = ^TFileDialog;
  165. TFileDialog = object(TDialog)
  166. FileName: PFileInputLine;
  167. FileList: PFileList;
  168. FileHistory: PFileHistory;
  169. WildCard: TWildStr;
  170. Directory: PString;
  171. constructor Init(AWildCard: TWildStr; const ATitle,
  172. InputName: String; AOptions: Word; HistoryId: Byte);
  173. constructor Load(var S: TStream);
  174. destructor Done; virtual;
  175. procedure GetData(var Rec); virtual;
  176. procedure GetFileName(var S: PathStr);
  177. procedure HandleEvent(var Event: TEvent); virtual;
  178. procedure SetData(var Rec); virtual;
  179. procedure Store(var S: TStream);
  180. function Valid(Command: Word): Boolean; virtual;
  181. private
  182. procedure ReadDirectory;
  183. end;
  184. { TDirEntry }
  185. PDirEntry = ^TDirEntry;
  186. TDirEntry = record
  187. DisplayText: PString;
  188. Directory: PString;
  189. end; { of TDirEntry }
  190. { TDirCollection is a collection of TDirEntry's used by }
  191. { TDirListBox. }
  192. PDirCollection = ^TDirCollection;
  193. TDirCollection = object(TCollection)
  194. function GetItem(var S: TStream): Pointer; virtual;
  195. procedure FreeItem(Item: Pointer); virtual;
  196. procedure PutItem(var S: TStream; Item: Pointer); virtual;
  197. end;
  198. { TDirListBox displays a tree of directories for use in the }
  199. { TChDirDialog. }
  200. PDirListBox = ^TDirListBox;
  201. TDirListBox = object(TListBox)
  202. Dir: DirStr;
  203. Cur: Word;
  204. constructor Init(var Bounds: TRect; AScrollBar: PScrollBar);
  205. destructor Done; virtual;
  206. function GetText(Item,MaxLen: Sw_Integer): String; virtual;
  207. procedure HandleEvent(var Event: TEvent); virtual;
  208. function IsSelected(Item: Sw_Integer): Boolean; virtual;
  209. procedure NewDirectory(var ADir: DirStr);
  210. procedure SetState(AState: Word; Enable: Boolean); virtual;
  211. end;
  212. { TChDirDialog is a standard change directory dialog. }
  213. const
  214. cdNormal = $0000; { Option to use dialog immediately }
  215. cdNoLoadDir = $0001; { Option to init the dialog to store on a stream }
  216. cdHelpButton = $0002; { Put a help button in the dialog }
  217. type
  218. PChDirDialog = ^TChDirDialog;
  219. TChDirDialog = object(TDialog)
  220. DirInput: PInputLine;
  221. DirList: PDirListBox;
  222. OkButton: PButton;
  223. ChDirButton: PButton;
  224. constructor Init(AOptions: Word; HistoryId: Sw_Word);
  225. constructor Load(var S: TStream);
  226. function DataSize: Sw_Word; virtual;
  227. procedure GetData(var Rec); virtual;
  228. procedure HandleEvent(var Event: TEvent); virtual;
  229. procedure SetData(var Rec); virtual;
  230. procedure Store(var S: TStream);
  231. function Valid(Command: Word): Boolean; virtual;
  232. private
  233. procedure SetUpDialog;
  234. end;
  235. PEditChDirDialog = ^TEditChDirDialog;
  236. TEditChDirDialog = Object(TChDirDialog)
  237. { TEditChDirDialog allows setting/getting the starting directory. The
  238. transfer record is a DirStr. }
  239. function DataSize : Sw_Word; virtual;
  240. procedure GetData (var Rec); virtual;
  241. procedure SetData (var Rec); virtual;
  242. end; { of TEditChDirDialog }
  243. {#Z+}
  244. PDirValidator = ^TDirValidator;
  245. {#Z-}
  246. TDirValidator = Object(TFilterValidator)
  247. constructor Init;
  248. function IsValid(const S: string): Boolean; virtual;
  249. function IsValidInput(var S: string; SuppressFill: Boolean): Boolean;
  250. virtual;
  251. end; { of TDirValidator }
  252. FileConfirmFunc = function (AFile : FNameStr) : Boolean;
  253. { Functions of type FileConfirmFunc's are used to prompt the end user for
  254. confirmation of an operation.
  255. FileConfirmFunc's should ask the user whether to perform the desired
  256. action on the file named AFile. If the user elects to perform the
  257. function FileConfirmFunc's return True, otherwise they return False.
  258. Using FileConfirmFunc's allows routines to be coded independant of the
  259. user interface implemented. OWL and TurboVision are supported through
  260. conditional defines. If you do not use either user interface you must
  261. compile this unit with the conditional define cdNoMessages and set all
  262. FileConfirmFunc variables to a valid function prior to calling any
  263. routines in this unit. }
  264. {#X ReplaceFile DeleteFile }
  265. var
  266. ReplaceFile : FileConfirmFunc;
  267. { ReplaceFile returns True if the end user elects to replace the existing
  268. file with the new file, otherwise it returns False.
  269. ReplaceFile is only called when #CheckOnReplace# is True. }
  270. {#X DeleteFile }
  271. DeleteFile : FileConfirmFunc;
  272. { DeleteFile returns True if the end user elects to delete the file,
  273. otherwise it returns False.
  274. DeleteFile is only called when #CheckOnDelete# is True. }
  275. {#X ReplaceFile }
  276. const
  277. CInfoPane = #30;
  278. { TStream registration records }
  279. function Contains(S1, S2: String): Boolean;
  280. { Contains returns true if S1 contains any characters in S2. }
  281. function DriveValid(Drive: Char): Boolean;
  282. { DriveValid returns True if Drive is a valid DOS drive. Drive valid works
  283. by attempting to change the current directory to Drive, then restoring
  284. the original directory. }
  285. function ExtractDir(AFile: FNameStr): DirStr;
  286. { ExtractDir returns the path of AFile terminated with a trailing '\'. If
  287. AFile contains no directory information, an empty string is returned. }
  288. function ExtractFileName(AFile: FNameStr): NameStr;
  289. { ExtractFileName returns the file name without any directory or file
  290. extension information. }
  291. function Equal(const S1, S2: String; Count: Sw_word): Boolean;
  292. { Equal returns True if S1 equals S2 for up to Count characters. Equal is
  293. case-insensitive. }
  294. function FileExists (AFile : FNameStr) : Boolean;
  295. { FileExists looks for the file specified in AFile. If AFile is present
  296. FileExists returns true, otherwise FileExists returns False.
  297. The search is performed relative to the current system directory, but
  298. other directories may be searched by prefacing a file name with a valid
  299. directory path.
  300. There is no check for a vaild file name or drive. Errrors are handled
  301. internally and not reported in DosError. Critical errors are left to
  302. the system's critical error handler. }
  303. {#X OpenFile }
  304. function GetCurDir: DirStr;
  305. { GetCurDir returns the current directory. The directory returned always
  306. ends with a trailing backslash '\'. }
  307. function GetCurDrive: Char;
  308. { GetCurDrive returns the letter of the current drive as reported by the
  309. operating system. }
  310. function IsWild(const S: String): Boolean;
  311. { IsWild returns True if S contains a question mark (?) or asterix (*). }
  312. function IsList(const S: String): Boolean;
  313. { IsList returns True if S contains list separator (;) char }
  314. function IsDir(const S: String): Boolean;
  315. { IsDir returns True if S is a valid DOS directory. }
  316. procedure MakeResources;
  317. { MakeResources places a language specific version of all resources
  318. needed for the StdDlg unit to function on the RezFile using the string
  319. constants and variables in the Resource unit. The Resource unit and the
  320. appropriate string lists must be initialized prior to calling this
  321. procedure. }
  322. function NoWildChars(S: String): String;
  323. { NoWildChars deletes the wild card characters ? and * from the string S
  324. and returns the result. }
  325. function OpenFile (var AFile : FNameStr; HistoryID : Byte) : Boolean;
  326. { OpenFile prompts the user to select a file using the file specifications
  327. in AFile as the starting file and path. Wildcards are accepted. If the
  328. user accepts a file OpenFile returns True, otherwise OpenFile returns
  329. False.
  330. Note: The file returned may or may not exist. }
  331. function OpenNewFile (var AFile: FNameStr; HistoryID: Byte): Boolean;
  332. { OpenNewFile allows the user to select a directory from disk and enter a
  333. new file name. If the file name entered is an existing file the user is
  334. optionally prompted for confirmation of replacing the file based on the
  335. value in #CheckOnReplace#. If a file name is successfully entered,
  336. OpenNewFile returns True. }
  337. {#X OpenFile }
  338. function PathValid(var Path: PathStr): Boolean;
  339. { PathValid returns True if Path is a valid DOS path name. Path may be a
  340. file or directory name. Trailing '\'s are removed. }
  341. procedure RegisterStdDlg;
  342. { RegisterStdDlg registers all objects in the StdDlg unit for stream
  343. usage. }
  344. function SaveAs (var AFile : FNameStr; HistoryID : Word) : Boolean;
  345. { SaveAs prompts the user for a file name using AFile as a template. If
  346. AFile already exists and CheckOnReplace is True, the user is prompted
  347. to replace the file.
  348. If a valid file name is entered SaveAs returns True, other SaveAs returns
  349. False. }
  350. function SelectDir (var ADir : DirStr; HistoryID : Byte) : Boolean;
  351. { SelectDir prompts the user to select a directory using ADir as the
  352. starting directory. If a directory is selected, SelectDir returns True.
  353. The directory returned is gauranteed to exist. }
  354. function ShrinkPath (AFile : FNameStr; MaxLen : Byte) : FNameStr;
  355. { ShrinkPath returns a file name with a maximu length of MaxLen.
  356. Internal directories are removed and replaced with elipses as needed to
  357. make the file name fit in MaxLen.
  358. AFile must be a valid path name. }
  359. function StdDeleteFile (AFile : FNameStr) : Boolean;
  360. { StdDeleteFile returns True if the end user elects to delete the file,
  361. otherwise it returns False.
  362. DeleteFile is only called when CheckOnDelete is True. }
  363. function StdReplaceFile (AFile : FNameStr) : Boolean;
  364. { StdReplaceFile returns True if the end user elects to replace the existing
  365. AFile with the new AFile, otherwise it returns False.
  366. ReplaceFile is only called when CheckOnReplace is True. }
  367. function ValidFileName(var FileName: PathStr): Boolean;
  368. { ValidFileName returns True if FileName is a valid DOS file name. }
  369. const
  370. CheckOnReplace : Boolean = True;
  371. { CheckOnReplace is used by file functions. If a file exists, it is
  372. optionally replaced based on the value of CheckOnReplace.
  373. If CheckOnReplace is False the file is replaced without asking the
  374. user. If CheckOnReplace is True, the end user is asked to replace the
  375. file using a call to ReplaceFile.
  376. CheckOnReplace is set to True by default. }
  377. CheckOnDelete : Boolean = True;
  378. { CheckOnDelete is used by file and directory functions. If a file
  379. exists, it is optionally deleted based on the value of CheckOnDelete.
  380. If CheckOnDelete is False the file or directory is deleted without
  381. asking the user. If CheckOnDelete is True, the end user is asked to
  382. delete the file/directory using a call to DeleteFile.
  383. CheckOnDelete is set to True by default. }
  384. const
  385. RFileInputLine: TStreamRec = (
  386. ObjType: idFileInputLine;
  387. VmtLink: Ofs(TypeOf(TFileInputLine)^);
  388. Load: @TFileInputLine.Load;
  389. Store: @TFileInputLine.Store
  390. );
  391. RFileCollection: TStreamRec = (
  392. ObjType: idFileCollection;
  393. VmtLink: Ofs(TypeOf(TFileCollection)^);
  394. Load: @TFileCollection.Load;
  395. Store: @TFileCollection.Store
  396. );
  397. RFileList: TStreamRec = (
  398. ObjType: idFileList;
  399. VmtLink: Ofs(TypeOf(TFileList)^);
  400. Load: @TFileList.Load;
  401. Store: @TFileList.Store
  402. );
  403. RFileInfoPane: TStreamRec = (
  404. ObjType: idFileInfoPane;
  405. VmtLink: Ofs(TypeOf(TFileInfoPane)^);
  406. Load: @TFileInfoPane.Load;
  407. Store: @TFileInfoPane.Store
  408. );
  409. RFileDialog: TStreamRec = (
  410. ObjType: idFileDialog;
  411. VmtLink: Ofs(TypeOf(TFileDialog)^);
  412. Load: @TFileDialog.Load;
  413. Store: @TFileDialog.Store
  414. );
  415. RDirCollection: TStreamRec = (
  416. ObjType: idDirCollection;
  417. VmtLink: Ofs(TypeOf(TDirCollection)^);
  418. Load: @TDirCollection.Load;
  419. Store: @TDirCollection.Store
  420. );
  421. RDirListBox: TStreamRec = (
  422. ObjType: idDirListBox;
  423. VmtLink: Ofs(TypeOf(TDirListBox)^);
  424. Load: @TDirListBox.Load;
  425. Store: @TDirListBox.Store
  426. );
  427. RChDirDialog: TStreamRec = (
  428. ObjType: idChDirDialog;
  429. VmtLink: Ofs(TypeOf(TChDirDialog)^);
  430. Load: @TChDirDialog.Load;
  431. Store: @TChDirDialog.Store
  432. );
  433. RSortedListBox: TStreamRec = (
  434. ObjType: idSortedListBox;
  435. VmtLink: Ofs(TypeOf(TSortedListBox)^);
  436. Load: @TSortedListBox.Load;
  437. Store: @TSortedListBox.Store
  438. );
  439. REditChDirDialog : TStreamRec = (
  440. ObjType : idEditChDirDialog;
  441. VmtLink : Ofs(TypeOf(TEditChDirDialog)^);
  442. Load : @TEditChDirDialog.Load;
  443. Store : @TEditChDirDialog.Store);
  444. implementation
  445. {****************************************************************************}
  446. { Local Declarations }
  447. {****************************************************************************}
  448. uses
  449. App, Memory, HistList, MsgBox, Resource;
  450. type
  451. PStringRec = record
  452. { PStringRec is needed for properly displaying PStrings using
  453. MessageBox. }
  454. AString : PString;
  455. end;
  456. {****************************************************************************}
  457. { PathValid }
  458. {****************************************************************************}
  459. {$ifdef go32v2}
  460. {$define NetDrive}
  461. {$endif go32v2}
  462. {$ifdef win32}
  463. {$define NetDrive}
  464. {$endif win32}
  465. procedure RemoveDoubleDirSep(var ExpPath : PathStr);
  466. var
  467. p: longint;
  468. {$ifdef NetDrive}
  469. OneDirSepRemoved: boolean;
  470. {$endif NetDrive}
  471. begin
  472. p:=pos(DirSeparator+DirSeparator,ExpPath);
  473. {$ifdef NetDrive}
  474. if p=1 then
  475. begin
  476. ExpPath:=Copy(ExpPath,1,high(ExpPath));
  477. OneDirSepRemoved:=true;
  478. p:=pos(DirSeparator+DirSeparator,ExpPath);
  479. end
  480. else
  481. OneDirSepRemoved:=false;
  482. {$endif NetDrive}
  483. while p>0 do
  484. begin
  485. ExpPath:=Copy(ExpPath,1,p)+Copy(ExpPath,p+2,high(ExpPath));
  486. p:=pos(DirSeparator+DirSeparator,ExpPath);
  487. end;
  488. {$ifdef NetDrive}
  489. if OneDirSepRemoved then
  490. ExpPath:=DirSeparator+ExpPath;
  491. {$endif NetDrive}
  492. end;
  493. function PathValid (var Path: PathStr): Boolean;
  494. var
  495. ExpPath: PathStr;
  496. SR: SearchRec;
  497. begin
  498. RemoveDoubleDirSep(Path);
  499. ExpPath := FExpand(Path);
  500. {$ifdef HAS_DOS_DRIVES}
  501. if (Length(ExpPath) <= 3) then
  502. PathValid := DriveValid(ExpPath[1])
  503. else
  504. {$endif}
  505. begin
  506. { do not change '/' into '' }
  507. if (Length(ExpPath)>1) and (ExpPath[Length(ExpPath)] = DirSeparator) then
  508. Dec(ExpPath[0]);
  509. FindFirst(ExpPath, Directory, SR);
  510. PathValid := (DosError = 0) and (SR.Attr and Directory <> 0);
  511. {$ifdef NetDrive}
  512. if (DosError<>0) and (length(ExpPath)>2) and
  513. (ExpPath[1]='\') and (ExpPath[2]='\')then
  514. begin
  515. { Checking '\\machine\sharedfolder' directly always fails..
  516. rather try '\\machine\sharedfolder\*' PM }
  517. {$ifdef fpc}
  518. FindClose(SR);
  519. {$endif}
  520. FindFirst(ExpPath+'\*',AnyFile,SR);
  521. PathValid:=(DosError = 0);
  522. end;
  523. {$endif NetDrive}
  524. {$ifdef fpc}
  525. FindClose(SR);
  526. {$endif}
  527. end;
  528. end;
  529. {****************************************************************************}
  530. { TDirValidator Object }
  531. {****************************************************************************}
  532. {****************************************************************************}
  533. { TDirValidator.Init }
  534. {****************************************************************************}
  535. constructor TDirValidator.Init;
  536. const { What should this list be? The commented one doesn't allow home,
  537. end, right arrow, left arrow, Ctrl+XXXX, etc. }
  538. Chars: TCharSet = ['A'..'Z','a'..'z','.','~',':','_','-'];
  539. { Chars: TCharSet = [#0..#255]; }
  540. begin
  541. Chars := Chars + [DirSeparator];
  542. if not inherited Init(Chars) then
  543. Fail;
  544. end;
  545. {****************************************************************************}
  546. { TDirValidator.IsValid }
  547. {****************************************************************************}
  548. function TDirValidator.IsValid(const S: string): Boolean;
  549. begin
  550. { IsValid := False; }
  551. IsValid := True;
  552. end;
  553. {****************************************************************************}
  554. { TDirValidator.IsValidInput }
  555. {****************************************************************************}
  556. function TDirValidator.IsValidInput(var S: string; SuppressFill: Boolean): Boolean;
  557. begin
  558. { IsValid := False; }
  559. IsValidInput := True;
  560. end;
  561. {****************************************************************************}
  562. { TFileInputLine Object }
  563. {****************************************************************************}
  564. {****************************************************************************}
  565. { TFileInputLine.Init }
  566. {****************************************************************************}
  567. constructor TFileInputLine.Init(var Bounds: TRect; AMaxLen: Sw_Integer);
  568. begin
  569. TInputLine.Init(Bounds, AMaxLen);
  570. EventMask := EventMask or evBroadcast;
  571. end;
  572. {****************************************************************************}
  573. { TFileInputLine.HandleEvent }
  574. {****************************************************************************}
  575. procedure TFileInputLine.HandleEvent(var Event: TEvent);
  576. begin
  577. TInputLine.HandleEvent(Event);
  578. if (Event.What = evBroadcast) and (Event.Command = cmFileFocused) and
  579. (State and sfSelected = 0) then
  580. begin
  581. if PSearchRec(Event.InfoPtr)^.Attr and Directory <> 0 then
  582. begin
  583. Data^ := PSearchRec(Event.InfoPtr)^.Name + DirSeparator +
  584. PFileDialog(Owner)^.WildCard;
  585. { PFileDialog(Owner)^.FileHistory^.AdaptHistoryToDir(
  586. PSearchRec(Event.InfoPtr)^.Name+DirSeparator);}
  587. end
  588. else Data^ := PSearchRec(Event.InfoPtr)^.Name;
  589. DrawView;
  590. end;
  591. end;
  592. {****************************************************************************}
  593. { TFileCollection Object }
  594. {****************************************************************************}
  595. {****************************************************************************}
  596. { TFileCollection.Compare }
  597. {****************************************************************************}
  598. function uppername(const s : string) : string;
  599. var
  600. i : Sw_integer;
  601. in_name : boolean;
  602. begin
  603. in_name:=true;
  604. for i:=length(s) downto 1 do
  605. if in_name and (s[i] in ['a'..'z']) then
  606. uppername[i]:=char(byte(s[i])-32)
  607. else
  608. begin
  609. uppername[i]:=s[i];
  610. if s[i] = DirSeparator then
  611. in_name:=false;
  612. end;
  613. uppername[0]:=s[0];
  614. end;
  615. function TFileCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  616. begin
  617. if PSearchRec(Key1)^.Name = PSearchRec(Key2)^.Name then Compare := 0
  618. else if PSearchRec(Key1)^.Name = '..' then Compare := 1
  619. else if PSearchRec(Key2)^.Name = '..' then Compare := -1
  620. else if (PSearchRec(Key1)^.Attr and Directory <> 0) and
  621. (PSearchRec(Key2)^.Attr and Directory = 0) then Compare := 1
  622. else if (PSearchRec(Key2)^.Attr and Directory <> 0) and
  623. (PSearchRec(Key1)^.Attr and Directory = 0) then Compare := -1
  624. else if UpperName(PSearchRec(Key1)^.Name) > UpperName(PSearchRec(Key2)^.Name) then
  625. Compare := 1
  626. {$ifdef unix}
  627. else if UpperName(PSearchRec(Key1)^.Name) < UpperName(PSearchRec(Key2)^.Name) then
  628. Compare := -1
  629. else if PSearchRec(Key1)^.Name > PSearchRec(Key2)^.Name then
  630. Compare := 1
  631. {$endif def unix}
  632. else
  633. Compare := -1;
  634. end;
  635. {****************************************************************************}
  636. { TFileCollection.FreeItem }
  637. {****************************************************************************}
  638. procedure TFileCollection.FreeItem(Item: Pointer);
  639. begin
  640. Dispose(PSearchRec(Item));
  641. end;
  642. {****************************************************************************}
  643. { TFileCollection.GetItem }
  644. {****************************************************************************}
  645. function TFileCollection.GetItem(var S: TStream): Pointer;
  646. var
  647. Item: PSearchRec;
  648. begin
  649. New(Item);
  650. S.Read(Item^, SizeOf(TSearchRec));
  651. GetItem := Item;
  652. end;
  653. {****************************************************************************}
  654. { TFileCollection.PutItem }
  655. {****************************************************************************}
  656. procedure TFileCollection.PutItem(var S: TStream; Item: Pointer);
  657. begin
  658. S.Write(Item^, SizeOf(TSearchRec));
  659. end;
  660. {*****************************************************************************
  661. TFileList
  662. *****************************************************************************}
  663. const
  664. ListSeparator=';';
  665. function MatchesMask(What, Mask: string): boolean;
  666. function upper(const s : string) : string;
  667. var
  668. i : Sw_integer;
  669. begin
  670. for i:=1 to length(s) do
  671. if s[i] in ['a'..'z'] then
  672. upper[i]:=char(byte(s[i])-32)
  673. else
  674. upper[i]:=s[i];
  675. upper[0]:=s[0];
  676. end;
  677. Function CmpStr(const hstr1,hstr2:string):boolean;
  678. var
  679. found : boolean;
  680. i1,i2 : Sw_integer;
  681. begin
  682. i1:=0;
  683. i2:=0;
  684. if hstr1='' then
  685. begin
  686. CmpStr:=(hstr2='');
  687. exit;
  688. end;
  689. found:=true;
  690. repeat
  691. inc(i1);
  692. if (i1>length(hstr1)) then
  693. break;
  694. inc(i2);
  695. if (i2>length(hstr2)) then
  696. break;
  697. case hstr1[i1] of
  698. '?' :
  699. found:=true;
  700. '*' :
  701. begin
  702. found:=true;
  703. if (i1=length(hstr1)) then
  704. i2:=length(hstr2)
  705. else
  706. if (i1<length(hstr1)) and (hstr1[i1+1]<>hstr2[i2]) then
  707. begin
  708. if i2<length(hstr2) then
  709. dec(i1)
  710. end
  711. else
  712. if i2>1 then
  713. dec(i2);
  714. end;
  715. else
  716. found:=(hstr1[i1]=hstr2[i2]) or (hstr2[i2]='?');
  717. end;
  718. until not found;
  719. if found then
  720. begin
  721. found:=(i2>=length(hstr2)) and
  722. (
  723. (i1>length(hstr1)) or
  724. ((i1=length(hstr1)) and
  725. (hstr1[i1]='*'))
  726. );
  727. end;
  728. CmpStr:=found;
  729. end;
  730. var
  731. D1,D2 : DirStr;
  732. N1,N2 : NameStr;
  733. E1,E2 : Extstr;
  734. begin
  735. {$ifdef Unix}
  736. FSplit(What,D1,N1,E1);
  737. FSplit(Mask,D2,N2,E2);
  738. {$else}
  739. FSplit(Upper(What),D1,N1,E1);
  740. FSplit(Upper(Mask),D2,N2,E2);
  741. {$endif}
  742. MatchesMask:=CmpStr(N2,N1) and CmpStr(E2,E1);
  743. end;
  744. function MatchesMaskList(What, MaskList: string): boolean;
  745. var P: integer;
  746. Match: boolean;
  747. begin
  748. Match:=false;
  749. if What<>'' then
  750. repeat
  751. P:=Pos(ListSeparator, MaskList);
  752. if P=0 then P:=length(MaskList)+1;
  753. Match:=MatchesMask(What,copy(MaskList,1,P-1));
  754. Delete(MaskList,1,P);
  755. until Match or (MaskList='');
  756. MatchesMaskList:=Match;
  757. end;
  758. constructor TFileList.Init(var Bounds: TRect; AScrollBar: PScrollBar);
  759. begin
  760. TSortedListBox.Init(Bounds, 2, AScrollBar);
  761. end;
  762. destructor TFileList.Done;
  763. begin
  764. if List <> nil then Dispose(List, Done);
  765. TListBox.Done;
  766. end;
  767. function TFileList.DataSize: Sw_Word;
  768. begin
  769. DataSize := 0;
  770. end;
  771. procedure TFileList.FocusItem(Item: Sw_Integer);
  772. begin
  773. TSortedListBox.FocusItem(Item);
  774. if (List^.Count > 0) then
  775. Message(Owner, evBroadcast, cmFileFocused, List^.At(Item));
  776. end;
  777. procedure TFileList.GetData(var Rec);
  778. begin
  779. end;
  780. function TFileList.GetKey(var S: String): Pointer;
  781. const
  782. SR: TSearchRec = ();
  783. procedure UpStr(var S: String);
  784. var
  785. I: Sw_Integer;
  786. begin
  787. for I := 1 to Length(S) do S[I] := UpCase(S[I]);
  788. end;
  789. begin
  790. if (HandleDir{ShiftState and $03 <> 0}) or ((S <> '') and (S[1]='.')) then
  791. SR.Attr := Directory
  792. else SR.Attr := 0;
  793. SR.Name := S;
  794. {$ifndef Unix}
  795. UpStr(SR.Name);
  796. {$endif Unix}
  797. GetKey := @SR;
  798. end;
  799. function TFileList.GetText(Item,MaxLen: Sw_Integer): String;
  800. var
  801. S: String;
  802. SR: PSearchRec;
  803. begin
  804. SR := PSearchRec(List^.At(Item));
  805. S := SR^.Name;
  806. if SR^.Attr and Directory <> 0 then
  807. begin
  808. S[Length(S)+1] := DirSeparator;
  809. Inc(S[0]);
  810. end;
  811. GetText := S;
  812. end;
  813. procedure TFileList.HandleEvent(var Event: TEvent);
  814. var
  815. S : String;
  816. K : pointer;
  817. Value : Sw_integer;
  818. begin
  819. if (Event.What = evMouseDown) and (Event.Double) then
  820. begin
  821. Event.What := evCommand;
  822. Event.Command := cmOK;
  823. PutEvent(Event);
  824. ClearEvent(Event);
  825. end
  826. else if (Event.What = evKeyDown) and (Event.CharCode='<') then
  827. begin
  828. { select '..' }
  829. S := '..';
  830. K := GetKey(S);
  831. If PSortedCollection(List)^.Search(K, Value) then
  832. FocusItem(Value);
  833. end
  834. else TSortedListBox.HandleEvent(Event);
  835. end;
  836. procedure TFileList.ReadDirectory(AWildCard: PathStr);
  837. const
  838. FindAttr = ReadOnly + Archive;
  839. PrevDir = '..';
  840. var
  841. S: SearchRec;
  842. P: PSearchRec;
  843. FileList: PFileCollection;
  844. NumFiles: Word;
  845. FindStr,
  846. WildName : string;
  847. Dir: DirStr;
  848. Ext: ExtStr;
  849. Name: NameStr;
  850. Event : TEvent;
  851. Tmp: PathStr;
  852. begin
  853. NumFiles := 0;
  854. FileList := New(PFileCollection, Init(5, 5));
  855. AWildCard := FExpand(AWildCard);
  856. FSplit(AWildCard, Dir, Name, Ext);
  857. if pos(ListSeparator,AWildCard)>0 then
  858. begin
  859. WildName:=Copy(AWildCard,length(Dir)+1,255);
  860. FindStr:=Dir+AllFiles;
  861. end
  862. else
  863. begin
  864. WildName:=Name+Ext;
  865. FindStr:=AWildCard;
  866. end;
  867. FindFirst(FindStr, FindAttr, S);
  868. P := PSearchRec(@P);
  869. while assigned(P) and (DosError = 0) do
  870. begin
  871. if (S.Attr and Directory = 0) and
  872. MatchesMaskList(S.Name,WildName) then
  873. begin
  874. P := MemAlloc(SizeOf(P^));
  875. if assigned(P) then
  876. begin
  877. P^.Attr:=S.Attr;
  878. P^.Time:=S.Time;
  879. P^.Size:=S.Size;
  880. P^.Name:=S.Name;
  881. FileList^.Insert(P);
  882. end;
  883. end;
  884. FindNext(S);
  885. end;
  886. {$ifdef fpc}
  887. FindClose(S);
  888. {$endif}
  889. Tmp := Dir + AllFiles;
  890. FindFirst(Tmp, Directory, S);
  891. while (P <> nil) and (DosError = 0) do
  892. begin
  893. if (S.Attr and Directory <> 0) and (S.Name <> '.') and (S.Name <> '..') then
  894. begin
  895. P := MemAlloc(SizeOf(P^));
  896. if P <> nil then
  897. begin
  898. P^.Attr:=S.Attr;
  899. P^.Time:=S.Time;
  900. P^.Size:=S.Size;
  901. P^.Name:=S.Name;
  902. FileList^.Insert(P);
  903. end;
  904. end;
  905. FindNext(S);
  906. end;
  907. {$ifdef fpc}
  908. FindClose(S);
  909. {$endif}
  910. {$ifndef Unix}
  911. if Length(Dir) > 4 then
  912. {$endif not Unix}
  913. begin
  914. P := MemAlloc(SizeOf(P^));
  915. if P <> nil then
  916. begin
  917. FindFirst(Tmp, Directory, S);
  918. FindNext(S);
  919. if (DosError = 0) and (S.Name = PrevDir) then
  920. begin
  921. P^.Attr:=S.Attr;
  922. P^.Time:=S.Time;
  923. P^.Size:=S.Size;
  924. P^.Name:=S.Name;
  925. end
  926. else
  927. begin
  928. P^.Name := PrevDir;
  929. P^.Size := 0;
  930. P^.Time := $210000;
  931. P^.Attr := Directory;
  932. end;
  933. FileList^.Insert(PSearchRec(P));
  934. {$ifdef fpc}
  935. FindClose(S);
  936. {$endif}
  937. end;
  938. end;
  939. if P = nil then
  940. MessageBox(strings^.get(sTooManyFiles), nil, mfOkButton + mfWarning);
  941. NewList(FileList);
  942. if List^.Count > 0 then
  943. begin
  944. Event.What := evBroadcast;
  945. Event.Command := cmFileFocused;
  946. Event.InfoPtr := List^.At(0);
  947. Owner^.HandleEvent(Event);
  948. end;
  949. end;
  950. procedure TFileList.SetData(var Rec);
  951. begin
  952. with PFileDialog(Owner)^ do
  953. Self.ReadDirectory(Directory^ + WildCard);
  954. end;
  955. {****************************************************************************}
  956. { TFileInfoPane Object }
  957. {****************************************************************************}
  958. {****************************************************************************}
  959. { TFileInfoPane.Init }
  960. {****************************************************************************}
  961. constructor TFileInfoPane.Init(var Bounds: TRect);
  962. begin
  963. TView.Init(Bounds);
  964. FillChar(S,SizeOf(S),#0);
  965. EventMask := EventMask or evBroadcast;
  966. end;
  967. {****************************************************************************}
  968. { TFileInfoPane.Draw }
  969. {****************************************************************************}
  970. procedure TFileInfoPane.Draw;
  971. var
  972. B: TDrawBuffer;
  973. D: String[9];
  974. M: String[3];
  975. PM: Boolean;
  976. Color: Word;
  977. Time: DateTime;
  978. Path: PathStr;
  979. FmtId: String;
  980. Params: array[0..7] of PtrInt;
  981. Str: String[80];
  982. const
  983. sDirectoryLine = ' %-12s %-9s %3s %2d, %4d %2d:%02d%cm';
  984. sFileLine = ' %-12s %-9d %3s %2d, %4d %2d:%02d%cm';
  985. InValidFiles : array[0..2] of string[12] = ('','.','..');
  986. var
  987. Month: array[1..12] of String[3];
  988. begin
  989. Month[1] := Strings^.Get(smJan);
  990. Month[2] := Strings^.Get(smFeb);
  991. Month[3] := Strings^.Get(smMar);
  992. Month[4] := Strings^.Get(smApr);
  993. Month[5] := Strings^.Get(smMay);
  994. Month[6] := Strings^.Get(smJun);
  995. Month[7] := Strings^.Get(smJul);
  996. Month[8] := Strings^.Get(smAug);
  997. Month[9] := Strings^.Get(smSep);
  998. Month[10] := Strings^.Get(smOct);
  999. Month[11] := Strings^.Get(smNov);
  1000. Month[12] := Strings^.Get(smDec);
  1001. { Display path }
  1002. if (PFileDialog(Owner)^.Directory <> nil) then
  1003. Path := PFileDialog(Owner)^.Directory^
  1004. else Path := '';
  1005. Path := FExpand(Path+PFileDialog(Owner)^.WildCard);
  1006. { avoid B Buffer overflow PM }
  1007. Path := ShrinkPath(Path, Size.X - 1);
  1008. Color := GetColor($01);
  1009. MoveChar(B, ' ', Color, Size.X); { fill with empty spaces }
  1010. WriteLine(0, 0, Size.X, Size.Y, B);
  1011. MoveStr(B[1], Path, Color);
  1012. WriteLine(0, 0, Size.X, 1, B);
  1013. if (S.Name = InValidFiles[0]) or (S.Name = InValidFiles[1]) or
  1014. (S.Name = InValidFiles[2]) then
  1015. Exit;
  1016. { Display file }
  1017. Params[0] := PtrInt(@S.Name);
  1018. if S.Attr and Directory <> 0 then
  1019. begin
  1020. FmtId := sDirectoryLine;
  1021. D := Strings^.Get(sDirectory);
  1022. Params[1] := PtrInt(@D);
  1023. end else
  1024. begin
  1025. FmtId := sFileLine;
  1026. Params[1] := S.Size;
  1027. end;
  1028. UnpackTime(S.Time, Time);
  1029. M := Month[Time.Month];
  1030. Params[2] := PtrInt(@M);
  1031. Params[3] := Time.Day;
  1032. Params[4] := Time.Year;
  1033. PM := Time.Hour >= 12;
  1034. Time.Hour := Time.Hour mod 12;
  1035. if Time.Hour = 0 then Time.Hour := 12;
  1036. Params[5] := Time.Hour;
  1037. Params[6] := Time.Min;
  1038. if PM then
  1039. Params[7] := Byte('p')
  1040. else Params[7] := Byte('a');
  1041. FormatStr(Str, FmtId, Params);
  1042. MoveStr(B, Str, Color);
  1043. WriteLine(0, 1, Size.X, 1, B);
  1044. { Fill in rest of rectangle }
  1045. MoveChar(B, ' ', Color, Size.X);
  1046. WriteLine(0, 2, Size.X, Size.Y-2, B);
  1047. end;
  1048. function TFileInfoPane.GetPalette: PPalette;
  1049. const
  1050. P: String[Length(CInfoPane)] = CInfoPane;
  1051. begin
  1052. GetPalette := PPalette(@P);
  1053. end;
  1054. procedure TFileInfoPane.HandleEvent(var Event: TEvent);
  1055. begin
  1056. TView.HandleEvent(Event);
  1057. if (Event.What = evBroadcast) and (Event.Command = cmFileFocused) then
  1058. begin
  1059. S := PSearchRec(Event.InfoPtr)^;
  1060. DrawView;
  1061. end;
  1062. end;
  1063. {****************************************************************************
  1064. TFileHistory
  1065. ****************************************************************************}
  1066. function LTrim(const S: String): String;
  1067. var
  1068. I: Sw_Integer;
  1069. begin
  1070. I := 1;
  1071. while (I < Length(S)) and (S[I] = ' ') do Inc(I);
  1072. LTrim := Copy(S, I, 255);
  1073. end;
  1074. function RTrim(const S: String): String;
  1075. var
  1076. I: Sw_Integer;
  1077. begin
  1078. I := Length(S);
  1079. while S[I] = ' ' do Dec(I);
  1080. RTrim := Copy(S, 1, I);
  1081. end;
  1082. function RelativePath(var S: PathStr): Boolean;
  1083. begin
  1084. S := LTrim(RTrim(S));
  1085. RelativePath := not ((S <> '') and ((S[1] = DirSeparator) or (S[2] = ':')));
  1086. end;
  1087. { try to reduce the length of S+dir as a file path+pattern }
  1088. function Simplify (var S,Dir : string) : string;
  1089. var i : sw_integer;
  1090. begin
  1091. if RelativePath(Dir) then
  1092. begin
  1093. if (S<>'') and (Copy(Dir,1,3)='..'+DirSeparator) then
  1094. begin
  1095. i:=Length(S);
  1096. for i:=Length(S)-1 downto 1 do
  1097. if S[i]=DirSeparator then
  1098. break;
  1099. if S[i]=DirSeparator then
  1100. Simplify:=Copy(S,1,i)+Copy(Dir,4,255)
  1101. else
  1102. Simplify:=S+Dir;
  1103. end
  1104. else
  1105. Simplify:=S+Dir;
  1106. end
  1107. else
  1108. Simplify:=Dir;
  1109. end;
  1110. {****************************************************************************}
  1111. { TFileHistory.HandleEvent }
  1112. {****************************************************************************}
  1113. procedure TFileHistory.HandleEvent(var Event: TEvent);
  1114. var
  1115. HistoryWindow: PHistoryWindow;
  1116. R,P: TRect;
  1117. C: Word;
  1118. Rslt: String;
  1119. begin
  1120. TView.HandleEvent(Event);
  1121. if (Event.What = evMouseDown) or
  1122. ((Event.What = evKeyDown) and (CtrlToArrow(Event.KeyCode) = kbDown) and
  1123. (Link^.State and sfFocused <> 0)) then
  1124. begin
  1125. if not Link^.Focus then
  1126. begin
  1127. ClearEvent(Event);
  1128. Exit;
  1129. end;
  1130. if assigned(CurDir) then
  1131. Rslt:=CurDir^
  1132. else
  1133. Rslt:='';
  1134. Rslt:=Simplify(Rslt,Link^.Data^);
  1135. RemoveDoubleDirSep(Rslt);
  1136. If IsWild(Rslt) then
  1137. RecordHistory(Rslt);
  1138. Link^.GetBounds(R);
  1139. Dec(R.A.X); Inc(R.B.X); Inc(R.B.Y,7); Dec(R.A.Y,1);
  1140. Owner^.GetExtent(P);
  1141. R.Intersect(P);
  1142. Dec(R.B.Y,1);
  1143. HistoryWindow := InitHistoryWindow(R);
  1144. if HistoryWindow <> nil then
  1145. begin
  1146. C := Owner^.ExecView(HistoryWindow);
  1147. if C = cmOk then
  1148. begin
  1149. Rslt := HistoryWindow^.GetSelection;
  1150. if Length(Rslt) > Link^.MaxLen then Rslt[0] := Char(Link^.MaxLen);
  1151. Link^.Data^ := Rslt;
  1152. Link^.SelectAll(True);
  1153. Link^.DrawView;
  1154. end;
  1155. Dispose(HistoryWindow, Done);
  1156. end;
  1157. ClearEvent(Event);
  1158. end
  1159. else if (Event.What = evBroadcast) then
  1160. if ((Event.Command = cmReleasedFocus) and (Event.InfoPtr = Link))
  1161. or (Event.Command = cmRecordHistory) then
  1162. begin
  1163. if assigned(CurDir) then
  1164. Rslt:=CurDir^
  1165. else
  1166. Rslt:='';
  1167. Rslt:=Simplify(Rslt,Link^.Data^);
  1168. RemoveDoubleDirSep(Rslt);
  1169. If IsWild(Rslt) then
  1170. RecordHistory(Rslt);
  1171. end;
  1172. end;
  1173. procedure TFileHistory.AdaptHistoryToDir(Dir : string);
  1174. var S,S2 : String;
  1175. i,Count : Sw_word;
  1176. begin
  1177. if assigned(CurDir) then
  1178. begin
  1179. S:=CurDir^;
  1180. if S=Dir then
  1181. exit;
  1182. DisposeStr(CurDir);
  1183. end
  1184. else
  1185. S:='';
  1186. CurDir:=NewStr(Simplify(S,Dir));
  1187. Count:=HistoryCount(HistoryId);
  1188. for i:=1 to count do
  1189. begin
  1190. S2:=HistoryStr(HistoryId,1);
  1191. HistoryRemove(HistoryId,1);
  1192. if RelativePath(S2) then
  1193. if S<>'' then
  1194. S2:=S+S2
  1195. else
  1196. S2:=FExpand(S2);
  1197. { simply full path
  1198. we should simplify relative to Dir ! }
  1199. HistoryAdd(HistoryId,S2);
  1200. end;
  1201. end;
  1202. destructor TFileHistory.Done;
  1203. begin
  1204. If assigned(CurDir) then
  1205. DisposeStr(CurDir);
  1206. Inherited Done;
  1207. end;
  1208. {****************************************************************************
  1209. TFileDialog
  1210. ****************************************************************************}
  1211. constructor TFileDialog.Init(AWildCard: TWildStr; const ATitle,
  1212. InputName: String; AOptions: Word; HistoryId: Byte);
  1213. var
  1214. Control: PView;
  1215. R: TRect;
  1216. Opt: Word;
  1217. begin
  1218. R.Assign(15,1,64,20);
  1219. TDialog.Init(R, ATitle);
  1220. Options := Options or ofCentered;
  1221. WildCard := AWildCard;
  1222. R.Assign(3,3,31,4);
  1223. FileName := New(PFileInputLine, Init(R, 79));
  1224. FileName^.Data^ := WildCard;
  1225. Insert(FileName);
  1226. R.Assign(2,2,3+CStrLen(InputName),3);
  1227. Control := New(PLabel, Init(R, InputName, FileName));
  1228. Insert(Control);
  1229. R.Assign(31,3,34,4);
  1230. FileHistory := New(PFileHistory, Init(R, FileName, HistoryId));
  1231. Insert(FileHistory);
  1232. R.Assign(3,14,34,15);
  1233. Control := New(PScrollBar, Init(R));
  1234. Insert(Control);
  1235. R.Assign(3,6,34,14);
  1236. FileList := New(PFileList, Init(R, PScrollBar(Control)));
  1237. Insert(FileList);
  1238. R.Assign(2,5,8,6);
  1239. Control := New(PLabel, Init(R, labels^.get(slFiles), FileList));
  1240. Insert(Control);
  1241. R.Assign(35,3,46,5);
  1242. Opt := bfDefault;
  1243. if AOptions and fdOpenButton <> 0 then
  1244. begin
  1245. Insert(New(PButton, Init(R,labels^.get(slOpen), cmFileOpen, Opt)));
  1246. Opt := bfNormal;
  1247. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1248. end;
  1249. if AOptions and fdOkButton <> 0 then
  1250. begin
  1251. Insert(New(PButton, Init(R,labels^.get(slOk), cmFileOpen, Opt)));
  1252. Opt := bfNormal;
  1253. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1254. end;
  1255. if AOptions and fdReplaceButton <> 0 then
  1256. begin
  1257. Insert(New(PButton, Init(R, labels^.get(slReplace),cmFileReplace, Opt)));
  1258. Opt := bfNormal;
  1259. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1260. end;
  1261. if AOptions and fdClearButton <> 0 then
  1262. begin
  1263. Insert(New(PButton, Init(R, labels^.get(slClear),cmFileClear, Opt)));
  1264. Opt := bfNormal;
  1265. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1266. end;
  1267. Insert(New(PButton, Init(R, labels^.get(slCancel), cmCancel, bfNormal)));
  1268. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1269. if AOptions and fdHelpButton <> 0 then
  1270. begin
  1271. Insert(New(PButton, Init(R,labels^.get(slHelp),cmHelp, bfNormal)));
  1272. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1273. end;
  1274. R.Assign(1,16,48,18);
  1275. Control := New(PFileInfoPane, Init(R));
  1276. Insert(Control);
  1277. SelectNext(False);
  1278. if AOptions and fdNoLoadDir = 0 then ReadDirectory;
  1279. end;
  1280. constructor TFileDialog.Load(var S: TStream);
  1281. begin
  1282. if not TDialog.Load(S) then
  1283. Fail;
  1284. S.Read(WildCard, SizeOf(WildCard));
  1285. if (S.Status <> stOk) then
  1286. begin
  1287. TDialog.Done;
  1288. Fail;
  1289. end;
  1290. GetSubViewPtr(S, FileName);
  1291. GetSubViewPtr(S, FileList);
  1292. GetSubViewPtr(S, FileHistory);
  1293. ReadDirectory;
  1294. if (DosError <> 0) then
  1295. begin
  1296. TDialog.Done;
  1297. Fail;
  1298. end;
  1299. end;
  1300. destructor TFileDialog.Done;
  1301. begin
  1302. DisposeStr(Directory);
  1303. TDialog.Done;
  1304. end;
  1305. procedure TFileDialog.GetData(var Rec);
  1306. begin
  1307. GetFilename(PathStr(Rec));
  1308. end;
  1309. procedure TFileDialog.GetFileName(var S: PathStr);
  1310. var
  1311. Path: PathStr;
  1312. Name: NameStr;
  1313. Ext: ExtStr;
  1314. TWild : string;
  1315. TPath: PathStr;
  1316. TName: NameStr;
  1317. TExt: NameStr;
  1318. i : Sw_integer;
  1319. begin
  1320. S := FileName^.Data^;
  1321. if RelativePath(S) then
  1322. begin
  1323. if (Directory <> nil) then
  1324. S := FExpand(Directory^ + S);
  1325. end
  1326. else
  1327. S := FExpand(S);
  1328. if Pos(ListSeparator,S)=0 then
  1329. begin
  1330. If FileExists(S) then
  1331. exit;
  1332. FSplit(S, Path, Name, Ext);
  1333. if ((Name = '') or (Ext = '')) and not IsDir(S) then
  1334. begin
  1335. TWild:=WildCard;
  1336. repeat
  1337. i:=Pos(ListSeparator,TWild);
  1338. if i=0 then
  1339. i:=length(TWild)+1;
  1340. FSplit(Copy(TWild,1,i-1), TPath, TName, TExt);
  1341. if ((Name = '') and (Ext = '')) then
  1342. S := Path + TName + TExt
  1343. else
  1344. if Name = '' then
  1345. S := Path + TName + Ext
  1346. else
  1347. if Ext = '' then
  1348. begin
  1349. if IsWild(Name) then
  1350. S := Path + Name + TExt
  1351. else
  1352. S := Path + Name + NoWildChars(TExt);
  1353. end;
  1354. if FileExists(S) then
  1355. break;
  1356. System.Delete(TWild,1,i);
  1357. until TWild='';
  1358. if TWild='' then
  1359. S := Path + Name + Ext;
  1360. end;
  1361. end;
  1362. end;
  1363. procedure TFileDialog.HandleEvent(var Event: TEvent);
  1364. begin
  1365. if (Event.What and evBroadcast <> 0) and
  1366. (Event.Command = cmListItemSelected) then
  1367. begin
  1368. EndModal(cmFileOpen);
  1369. ClearEvent(Event);
  1370. end;
  1371. TDialog.HandleEvent(Event);
  1372. if Event.What = evCommand then
  1373. case Event.Command of
  1374. cmFileOpen, cmFileReplace, cmFileClear:
  1375. begin
  1376. EndModal(Event.Command);
  1377. ClearEvent(Event);
  1378. end;
  1379. end;
  1380. end;
  1381. procedure TFileDialog.SetData(var Rec);
  1382. begin
  1383. TDialog.SetData(Rec);
  1384. if (PathStr(Rec) <> '') and (IsWild(TWildStr(Rec))) then
  1385. begin
  1386. Valid(cmFileInit);
  1387. FileName^.Select;
  1388. end;
  1389. end;
  1390. procedure TFileDialog.ReadDirectory;
  1391. begin
  1392. FileList^.ReadDirectory(WildCard);
  1393. FileHistory^.AdaptHistoryToDir(GetCurDir);
  1394. Directory := NewStr(GetCurDir);
  1395. end;
  1396. procedure TFileDialog.Store(var S: TStream);
  1397. begin
  1398. TDialog.Store(S);
  1399. S.Write(WildCard, SizeOf(WildCard));
  1400. PutSubViewPtr(S, FileName);
  1401. PutSubViewPtr(S, FileList);
  1402. PutSubViewPtr(S, FileHistory);
  1403. end;
  1404. function TFileDialog.Valid(Command: Word): Boolean;
  1405. var
  1406. FName: PathStr;
  1407. Dir: DirStr;
  1408. Name: NameStr;
  1409. Ext: ExtStr;
  1410. function CheckDirectory(var S: PathStr): Boolean;
  1411. begin
  1412. if not PathValid(S) then
  1413. begin
  1414. MessageBox(Strings^.Get(sInvalidDriveOrDir), nil, mfError + mfOkButton);
  1415. FileName^.Select;
  1416. CheckDirectory := False;
  1417. end else CheckDirectory := True;
  1418. end;
  1419. function CompleteDir(const Path: string): string;
  1420. begin
  1421. { keep c: untouched PM }
  1422. if (Path<>'') and (Path[Length(Path)]<>DirSeparator) and
  1423. (Path[Length(Path)]<>':') then
  1424. CompleteDir:=Path+DirSeparator
  1425. else
  1426. CompleteDir:=Path;
  1427. end;
  1428. function NormalizeDir(const Path: string): string;
  1429. var Root: boolean;
  1430. begin
  1431. Root:=false;
  1432. {$ifdef Unix}
  1433. if Path=DirSeparator then Root:=true;
  1434. {$else}
  1435. if (length(Path)=3) and (Upcase(Path[1]) in['A'..'Z']) and
  1436. (Path[2]=':') and (Path[3]=DirSeparator) then
  1437. Root:=true;
  1438. {$endif}
  1439. if (Root=false) and (copy(Path,length(Path),1)=DirSeparator) then
  1440. NormalizeDir:=copy(Path,1,length(Path)-1)
  1441. else
  1442. NormalizeDir:=Path;
  1443. end;
  1444. function NormalizeDirF(var S: openstring): boolean;
  1445. begin
  1446. S:=NormalizeDir(S);
  1447. NormalizeDirF:=true;
  1448. end;
  1449. begin
  1450. if Command = 0 then
  1451. begin
  1452. Valid := True;
  1453. Exit;
  1454. end
  1455. else Valid := False;
  1456. if TDialog.Valid(Command) then
  1457. begin
  1458. GetFileName(FName);
  1459. if (Command <> cmCancel) and (Command <> cmFileClear) then
  1460. begin
  1461. if IsWild(FName) or IsList(FName) then
  1462. begin
  1463. FSplit(FName, Dir, Name, Ext);
  1464. if CheckDirectory(Dir) then
  1465. begin
  1466. FileHistory^.AdaptHistoryToDir(Dir);
  1467. DisposeStr(Directory);
  1468. Directory := NewStr(Dir);
  1469. if Pos(ListSeparator,FName)>0 then
  1470. WildCard:=Copy(FName,length(Dir)+1,255)
  1471. else
  1472. WildCard := Name+Ext;
  1473. if Command <> cmFileInit then
  1474. FileList^.Select;
  1475. FileList^.ReadDirectory(Directory^+WildCard);
  1476. end;
  1477. end
  1478. else
  1479. if NormalizeDirF(FName) then
  1480. { ^^ this is just a dummy if construct (the func always returns true,
  1481. it's just there, 'coz I don't want to rearrange the following "if"s... }
  1482. if IsDir(FName) then
  1483. begin
  1484. if CheckDirectory(FName) then
  1485. begin
  1486. FileHistory^.AdaptHistoryToDir(CompleteDir(FName));
  1487. DisposeStr(Directory);
  1488. Directory := NewSTr(CompleteDir(FName));
  1489. if Command <> cmFileInit then FileList^.Select;
  1490. FileList^.ReadDirectory(Directory^+WildCard);
  1491. end
  1492. end
  1493. else
  1494. if ValidFileName(FName) then
  1495. Valid := True
  1496. else
  1497. begin
  1498. MessageBox(^C + Strings^.Get(sInvalidFileName), nil, mfError + mfOkButton);
  1499. Valid := False;
  1500. end;
  1501. end
  1502. else Valid := True;
  1503. end;
  1504. end;
  1505. { TDirCollection }
  1506. function TDirCollection.GetItem(var S: TStream): Pointer;
  1507. var
  1508. DirItem: PDirEntry;
  1509. begin
  1510. New(DirItem);
  1511. DirItem^.DisplayText := S.ReadStr;
  1512. DirItem^.Directory := S.ReadStr;
  1513. GetItem := DirItem;
  1514. end;
  1515. procedure TDirCollection.FreeItem(Item: Pointer);
  1516. var
  1517. DirItem: PDirEntry absolute Item;
  1518. begin
  1519. DisposeStr(DirItem^.DisplayText);
  1520. DisposeStr(DirItem^.Directory);
  1521. Dispose(DirItem);
  1522. end;
  1523. procedure TDirCollection.PutItem(var S: TStream; Item: Pointer);
  1524. var
  1525. DirItem: PDirEntry absolute Item;
  1526. begin
  1527. S.WriteStr(DirItem^.DisplayText);
  1528. S.WriteStr(DirItem^.Directory);
  1529. end;
  1530. { TDirListBox }
  1531. const
  1532. DrivesS: String = '';
  1533. Drives: PString = @DrivesS;
  1534. constructor TDirListBox.Init(var Bounds: TRect; AScrollBar:
  1535. PScrollBar);
  1536. begin
  1537. DrivesS := strings^.get(sDrives);
  1538. TListBox.Init(Bounds, 1, AScrollBar);
  1539. Dir := '';
  1540. end;
  1541. destructor TDirListBox.Done;
  1542. begin
  1543. if (List <> nil) then
  1544. Dispose(List,Done);
  1545. TListBox.Done;
  1546. end;
  1547. function TDirListBox.GetText(Item,MaxLen: Sw_Integer): String;
  1548. begin
  1549. GetText := PDirEntry(List^.At(Item))^.DisplayText^;
  1550. end;
  1551. procedure TDirListBox.HandleEvent(var Event: TEvent);
  1552. begin
  1553. case Event.What of
  1554. evMouseDown:
  1555. if Event.Double then
  1556. begin
  1557. Event.What := evCommand;
  1558. Event.Command := cmChangeDir;
  1559. PutEvent(Event);
  1560. ClearEvent(Event);
  1561. end;
  1562. evKeyboard:
  1563. if (Event.CharCode = ' ') and
  1564. (PSearchRec(List^.At(Focused))^.Name = '..') then
  1565. NewDirectory(PSearchRec(List^.At(Focused))^.Name);
  1566. end;
  1567. TListBox.HandleEvent(Event);
  1568. end;
  1569. function TDirListBox.IsSelected(Item: Sw_Integer): Boolean;
  1570. begin
  1571. { IsSelected := Item = Cur; }
  1572. IsSelected := Inherited IsSelected(Item);
  1573. end;
  1574. procedure TDirListBox.NewDirectory(var ADir: DirStr);
  1575. const
  1576. PathDir = 'ÀÄÂ';
  1577. FirstDir = 'ÀÂÄ';
  1578. MiddleDir = ' ÃÄ';
  1579. LastDir = ' ÀÄ';
  1580. IndentSize = ' ';
  1581. var
  1582. AList: PCollection;
  1583. NewDir, Dirct: DirStr;
  1584. C, OldC: Char;
  1585. S, Indent: String[80];
  1586. P: PString;
  1587. NewCur: Word;
  1588. isFirst: Boolean;
  1589. SR: SearchRec;
  1590. I: Sw_Integer;
  1591. function NewDirEntry(const DisplayText, Directory: String): PDirEntry;{$ifdef PPC_BP}near;{$endif}
  1592. var
  1593. DirEntry: PDirEntry;
  1594. begin
  1595. New(DirEntry);
  1596. DirEntry^.DisplayText := NewStr(DisplayText);
  1597. If Directory='' then
  1598. DirEntry^.Directory := NewStr(DirSeparator)
  1599. else
  1600. DirEntry^.Directory := NewStr(Directory);
  1601. NewDirEntry := DirEntry;
  1602. end;
  1603. begin
  1604. Dir := ADir;
  1605. AList := New(PDirCollection, Init(5,5));
  1606. {$ifdef HAS_DOS_DRIVES}
  1607. AList^.Insert(NewDirEntry(Drives^,Drives^));
  1608. if Dir = Drives^ then
  1609. begin
  1610. isFirst := True;
  1611. OldC := ' ';
  1612. for C := 'A' to 'Z' do
  1613. begin
  1614. if (C < 'C') or DriveValid(C) then
  1615. begin
  1616. if OldC <> ' ' then
  1617. begin
  1618. if isFirst then
  1619. begin
  1620. S := FirstDir + OldC;
  1621. isFirst := False;
  1622. end
  1623. else S := MiddleDir + OldC;
  1624. AList^.Insert(NewDirEntry(S, OldC + ':' + DirSeparator));
  1625. end;
  1626. if C = GetCurDrive then NewCur := AList^.Count;
  1627. OldC := C;
  1628. end;
  1629. end;
  1630. if OldC <> ' ' then
  1631. AList^.Insert(NewDirEntry(LastDir + OldC, OldC + ':' + DirSeparator));
  1632. end
  1633. else
  1634. {$endif HAS_DOS_DRIVES}
  1635. begin
  1636. Indent := IndentSize;
  1637. NewDir := Dir;
  1638. {$ifdef HAS_DOS_DRIVES}
  1639. Dirct := Copy(NewDir,1,3);
  1640. AList^.Insert(NewDirEntry(PathDir + Dirct, Dirct));
  1641. NewDir := Copy(NewDir,4,255);
  1642. {$else HAS_DOS_DRIVES}
  1643. Dirct := '';
  1644. {$endif HAS_DOS_DRIVES}
  1645. while NewDir <> '' do
  1646. begin
  1647. I := Pos(DirSeparator,NewDir);
  1648. if I <> 0 then
  1649. begin
  1650. S := Copy(NewDir,1,I-1);
  1651. Dirct := Dirct + S;
  1652. AList^.Insert(NewDirEntry(Indent + PathDir + S, Dirct));
  1653. NewDir := Copy(NewDir,I+1,255);
  1654. end
  1655. else
  1656. begin
  1657. Dirct := Dirct + NewDir;
  1658. AList^.Insert(NewDirEntry(Indent + PathDir + NewDir, Dirct));
  1659. NewDir := '';
  1660. end;
  1661. Indent := Indent + IndentSize;
  1662. Dirct := Dirct + DirSeparator;
  1663. end;
  1664. NewCur := AList^.Count-1;
  1665. isFirst := True;
  1666. NewDir := Dirct + AllFiles;
  1667. FindFirst(NewDir, Directory, SR);
  1668. while DosError = 0 do
  1669. begin
  1670. if (SR.Attr and Directory <> 0) and
  1671. (SR.Name <> '.') and (SR.Name <> '..') then
  1672. begin
  1673. if isFirst then
  1674. begin
  1675. S := FirstDir;
  1676. isFirst := False;
  1677. end else S := MiddleDir;
  1678. AList^.Insert(NewDirEntry(Indent + S + SR.Name, Dirct + SR.Name));
  1679. end;
  1680. FindNext(SR);
  1681. end;
  1682. FindClose(SR);
  1683. P := PDirEntry(AList^.At(AList^.Count-1))^.DisplayText;
  1684. I := Pos('À',P^);
  1685. if I = 0 then
  1686. begin
  1687. I := Pos('Ã',P^);
  1688. if I <> 0 then P^[I] := 'À';
  1689. end else
  1690. begin
  1691. P^[I+1] := 'Ä';
  1692. P^[I+2] := 'Ä';
  1693. end;
  1694. end;
  1695. NewList(AList);
  1696. FocusItem(NewCur);
  1697. Cur:=NewCur;
  1698. end;
  1699. procedure TDirListBox.SetState(AState: Word; Enable: Boolean);
  1700. begin
  1701. TListBox.SetState(AState, Enable);
  1702. if AState and sfFocused <> 0 then
  1703. PChDirDialog(Owner)^.ChDirButton^.MakeDefault(Enable);
  1704. end;
  1705. {****************************************************************************}
  1706. { TChDirDialog Object }
  1707. {****************************************************************************}
  1708. {****************************************************************************}
  1709. { TChDirDialog.Init }
  1710. {****************************************************************************}
  1711. constructor TChDirDialog.Init(AOptions: Word; HistoryId: Sw_Word);
  1712. var
  1713. R: TRect;
  1714. Control: PView;
  1715. begin
  1716. R.Assign(16, 2, 64, 20);
  1717. TDialog.Init(R,strings^.get(sChangeDirectory));
  1718. Options := Options or ofCentered;
  1719. R.Assign(3, 3, 30, 4);
  1720. DirInput := New(PInputLine, Init(R, FileNameLen+4));
  1721. Insert(DirInput);
  1722. R.Assign(2, 2, 17, 3);
  1723. Control := New(PLabel, Init(R,labels^.get(slDirectoryName), DirInput));
  1724. Insert(Control);
  1725. R.Assign(30, 3, 33, 4);
  1726. Control := New(PHistory, Init(R, DirInput, HistoryId));
  1727. Insert(Control);
  1728. R.Assign(32, 6, 33, 16);
  1729. Control := New(PScrollBar, Init(R));
  1730. Insert(Control);
  1731. R.Assign(3, 6, 32, 16);
  1732. DirList := New(PDirListBox, Init(R, PScrollBar(Control)));
  1733. Insert(DirList);
  1734. R.Assign(2, 5, 17, 6);
  1735. Control := New(PLabel, Init(R, labels^.get(slDirectoryTree), DirList));
  1736. Insert(Control);
  1737. R.Assign(35, 6, 45, 8);
  1738. OkButton := New(PButton, Init(R, labels^.get(slOk), cmOK, bfDefault));
  1739. Insert(OkButton);
  1740. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1741. ChDirButton := New(PButton,Init(R,labels^.get(slChDir),cmChangeDir,
  1742. bfNormal));
  1743. Insert(ChDirButton);
  1744. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1745. Insert(New(PButton, Init(R,labels^.get(slRevert), cmRevert, bfNormal)));
  1746. if AOptions and cdHelpButton <> 0 then
  1747. begin
  1748. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1749. Insert(New(PButton, Init(R,labels^.get(slHelp), cmHelp, bfNormal)));
  1750. end;
  1751. if AOptions and cdNoLoadDir = 0 then SetUpDialog;
  1752. SelectNext(False);
  1753. end;
  1754. {****************************************************************************}
  1755. { TChDirDialog.Load }
  1756. {****************************************************************************}
  1757. constructor TChDirDialog.Load(var S: TStream);
  1758. begin
  1759. TDialog.Load(S);
  1760. GetSubViewPtr(S, DirList);
  1761. GetSubViewPtr(S, DirInput);
  1762. GetSubViewPtr(S, OkButton);
  1763. GetSubViewPtr(S, ChDirbutton);
  1764. SetUpDialog;
  1765. end;
  1766. {****************************************************************************}
  1767. { TChDirDialog.DataSize }
  1768. {****************************************************************************}
  1769. function TChDirDialog.DataSize: Sw_Word;
  1770. begin
  1771. DataSize := 0;
  1772. end;
  1773. {****************************************************************************}
  1774. { TChDirDialog.GetData }
  1775. {****************************************************************************}
  1776. procedure TChDirDialog.GetData(var Rec);
  1777. begin
  1778. end;
  1779. {****************************************************************************}
  1780. { TChDirDialog.HandleEvent }
  1781. {****************************************************************************}
  1782. procedure TChDirDialog.HandleEvent(var Event: TEvent);
  1783. var
  1784. CurDir: DirStr;
  1785. P: PDirEntry;
  1786. begin
  1787. TDialog.HandleEvent(Event);
  1788. case Event.What of
  1789. evCommand:
  1790. begin
  1791. case Event.Command of
  1792. cmRevert: GetDir(0,CurDir);
  1793. cmChangeDir:
  1794. begin
  1795. P := DirList^.List^.At(DirList^.Focused);
  1796. if (P^.Directory^ = Drives^)
  1797. or DriveValid(P^.Directory^[1]) then
  1798. CurDir := P^.Directory^
  1799. else Exit;
  1800. end;
  1801. else
  1802. Exit;
  1803. end;
  1804. if (Length(CurDir) > 3) and
  1805. (CurDir[Length(CurDir)] = DirSeparator) then
  1806. CurDir := Copy(CurDir,1,Length(CurDir)-1);
  1807. DirList^.NewDirectory(CurDir);
  1808. DirInput^.Data^ := CurDir;
  1809. DirInput^.DrawView;
  1810. DirList^.Select;
  1811. ClearEvent(Event);
  1812. end;
  1813. end;
  1814. end;
  1815. {****************************************************************************}
  1816. { TChDirDialog.SetData }
  1817. {****************************************************************************}
  1818. procedure TChDirDialog.SetData(var Rec);
  1819. begin
  1820. end;
  1821. {****************************************************************************}
  1822. { TChDirDialog.SetUpDialog }
  1823. {****************************************************************************}
  1824. procedure TChDirDialog.SetUpDialog;
  1825. var
  1826. CurDir: DirStr;
  1827. begin
  1828. if DirList <> nil then
  1829. begin
  1830. CurDir := GetCurDir;
  1831. DirList^.NewDirectory(CurDir);
  1832. if (Length(CurDir) > 3) and (CurDir[Length(CurDir)] = DirSeparator) then
  1833. CurDir := Copy(CurDir,1,Length(CurDir)-1);
  1834. if DirInput <> nil then
  1835. begin
  1836. DirInput^.Data^ := CurDir;
  1837. DirInput^.DrawView;
  1838. end;
  1839. end;
  1840. end;
  1841. {****************************************************************************}
  1842. { TChDirDialog.Store }
  1843. {****************************************************************************}
  1844. procedure TChDirDialog.Store(var S: TStream);
  1845. begin
  1846. TDialog.Store(S);
  1847. PutSubViewPtr(S, DirList);
  1848. PutSubViewPtr(S, DirInput);
  1849. PutSubViewPtr(S, OkButton);
  1850. PutSubViewPtr(S, ChDirButton);
  1851. end;
  1852. {****************************************************************************}
  1853. { TChDirDialog.Valid }
  1854. {****************************************************************************}
  1855. function TChDirDialog.Valid(Command: Word): Boolean;
  1856. var
  1857. P: PathStr;
  1858. begin
  1859. Valid := True;
  1860. if Command = cmOk then
  1861. begin
  1862. P := FExpand(DirInput^.Data^);
  1863. if (Length(P) > 3) and (P[Length(P)] = DirSeparator) then
  1864. Dec(P[0]);
  1865. {$I-}
  1866. ChDir(P);
  1867. if (IOResult <> 0) then
  1868. begin
  1869. MessageBox(Strings^.Get(sInvalidDirectory), nil, mfError + mfOkButton);
  1870. Valid := False;
  1871. end;
  1872. {$I+}
  1873. end;
  1874. end;
  1875. {****************************************************************************}
  1876. { TEditChDirDialog Object }
  1877. {****************************************************************************}
  1878. {****************************************************************************}
  1879. { TEditChDirDialog.DataSize }
  1880. {****************************************************************************}
  1881. function TEditChDirDialog.DataSize : Sw_Word;
  1882. begin
  1883. DataSize := SizeOf(DirStr);
  1884. end;
  1885. {****************************************************************************}
  1886. { TEditChDirDialog.GetData }
  1887. {****************************************************************************}
  1888. procedure TEditChDirDialog.GetData (var Rec);
  1889. var
  1890. CurDir : DirStr absolute Rec;
  1891. begin
  1892. if (DirInput = nil) then
  1893. CurDir := ''
  1894. else begin
  1895. CurDir := DirInput^.Data^;
  1896. if (CurDir[Length(CurDir)] <> DirSeparator) then
  1897. CurDir := CurDir + DirSeparator;
  1898. end;
  1899. end;
  1900. {****************************************************************************}
  1901. { TEditChDirDialog.SetData }
  1902. {****************************************************************************}
  1903. procedure TEditChDirDialog.SetData (var Rec);
  1904. var
  1905. CurDir : DirStr absolute Rec;
  1906. begin
  1907. if DirList <> nil then
  1908. begin
  1909. DirList^.NewDirectory(CurDir);
  1910. if DirInput <> nil then
  1911. begin
  1912. if (Length(CurDir) > 3) and (CurDir[Length(CurDir)] = DirSeparator) then
  1913. DirInput^.Data^ := Copy(CurDir,1,Length(CurDir)-1)
  1914. else DirInput^.Data^ := CurDir;
  1915. DirInput^.DrawView;
  1916. end;
  1917. end;
  1918. end;
  1919. {****************************************************************************}
  1920. { TSortedListBox Object }
  1921. {****************************************************************************}
  1922. {****************************************************************************}
  1923. { TSortedListBox.Init }
  1924. {****************************************************************************}
  1925. constructor TSortedListBox.Init(var Bounds: TRect; ANumCols: Sw_Word;
  1926. AScrollBar: PScrollBar);
  1927. begin
  1928. TListBox.Init(Bounds, ANumCols, AScrollBar);
  1929. SearchPos := 0;
  1930. ShowCursor;
  1931. SetCursor(1,0);
  1932. end;
  1933. {****************************************************************************}
  1934. { TSortedListBox.HandleEvent }
  1935. {****************************************************************************}
  1936. procedure TSortedListBox.HandleEvent(var Event: TEvent);
  1937. const
  1938. SpecialChars: set of Char = [#0,#9,#27];
  1939. var
  1940. CurString, NewString: String;
  1941. K: Pointer;
  1942. Value : Sw_integer;
  1943. OldPos, OldValue: Sw_Integer;
  1944. T: Boolean;
  1945. begin
  1946. OldValue := Focused;
  1947. TListBox.HandleEvent(Event);
  1948. if (OldValue <> Focused) or
  1949. ((Event.What = evBroadcast) and (Event.InfoPtr = @Self) and
  1950. (Event.Command = cmReleasedFocus)) then
  1951. SearchPos := 0;
  1952. if Event.What = evKeyDown then
  1953. begin
  1954. { patched to prevent error when no or empty list or Escape pressed }
  1955. if (not (Event.CharCode in SpecialChars)) and
  1956. (List <> nil) and (List^.Count > 0) then
  1957. begin
  1958. Value := Focused;
  1959. if Value < Range then
  1960. CurString := GetText(Value, 255)
  1961. else
  1962. CurString := '';
  1963. OldPos := SearchPos;
  1964. if Event.KeyCode = kbBack then
  1965. begin
  1966. if SearchPos = 0 then Exit;
  1967. Dec(SearchPos);
  1968. if SearchPos = 0 then
  1969. HandleDir:= ((GetShiftState and $3) <> 0) or (Event.CharCode in ['A'..'Z']);
  1970. CurString[0] := Char(SearchPos);
  1971. end
  1972. else if (Event.CharCode = '.') then
  1973. SearchPos := Pos('.',CurString)
  1974. else
  1975. begin
  1976. Inc(SearchPos);
  1977. if SearchPos = 1 then
  1978. HandleDir := ((GetShiftState and 3) <> 0) or (Event.CharCode in ['A'..'Z']);
  1979. CurString[0] := Char(SearchPos);
  1980. CurString[SearchPos] := Event.CharCode;
  1981. end;
  1982. K := GetKey(CurString);
  1983. T := PSortedCollection(List)^.Search(K, Value);
  1984. if Value < Range then
  1985. begin
  1986. if Value < Range then
  1987. NewString := GetText(Value, 255)
  1988. else
  1989. NewString := '';
  1990. if Equal(NewString, CurString, SearchPos) then
  1991. begin
  1992. if Value <> OldValue then
  1993. begin
  1994. FocusItem(Value);
  1995. { Assumes ListControl will set the cursor to the first character }
  1996. { of the sfFocused item }
  1997. SetCursor(Cursor.X+SearchPos, Cursor.Y);
  1998. end
  1999. else
  2000. SetCursor(Cursor.X+(SearchPos-OldPos), Cursor.Y);
  2001. end
  2002. else
  2003. SearchPos := OldPos;
  2004. end
  2005. else SearchPos := OldPos;
  2006. if (SearchPos <> OldPos) or (Event.CharCode in ['A'..'Z','a'..'z']) then
  2007. ClearEvent(Event);
  2008. end;
  2009. end;
  2010. end;
  2011. function TSortedListBox.GetKey(var S: String): Pointer;
  2012. begin
  2013. GetKey := @S;
  2014. end;
  2015. procedure TSortedListBox.NewList(AList: PCollection);
  2016. begin
  2017. TListBox.NewList(AList);
  2018. SearchPos := 0;
  2019. end;
  2020. {****************************************************************************}
  2021. { Global Procedures and Functions }
  2022. {****************************************************************************}
  2023. {****************************************************************************}
  2024. { Contains }
  2025. {****************************************************************************}
  2026. function Contains(S1, S2: String): Boolean;
  2027. { Contains returns true if S1 contains any characters in S2. }
  2028. var
  2029. i : Byte;
  2030. begin
  2031. Contains := True;
  2032. i := 1;
  2033. while ((i < Length(S2)) and (i < Length(S1))) do
  2034. if (Upcase(S1[i]) = Upcase(S2[i])) then
  2035. Exit
  2036. else Inc(i);
  2037. Contains := False;
  2038. end;
  2039. {****************************************************************************}
  2040. { StdDeleteFile }
  2041. {****************************************************************************}
  2042. function StdDeleteFile (AFile : FNameStr) : Boolean;
  2043. var
  2044. Rec : PStringRec;
  2045. begin
  2046. if CheckOnDelete then
  2047. begin
  2048. AFile := ShrinkPath(AFile,33);
  2049. Rec.AString := PString(@AFile);
  2050. StdDeleteFile := (MessageBox(^C + Strings^.Get(sDeleteFile),
  2051. @Rec,mfConfirmation or mfOkCancel) = cmOk);
  2052. end
  2053. else StdDeleteFile := False;
  2054. end;
  2055. {****************************************************************************}
  2056. { DriveValid }
  2057. {****************************************************************************}
  2058. function DriveValid(Drive: Char): Boolean;
  2059. {$ifdef HAS_DOS_DRIVES}
  2060. var
  2061. D: Char;
  2062. begin
  2063. D := GetCurDrive;
  2064. {$I-}
  2065. ChDir(Drive+':');
  2066. if (IOResult = 0) then
  2067. begin
  2068. DriveValid := True;
  2069. ChDir(D+':')
  2070. end
  2071. else DriveValid := False;
  2072. {$I+}
  2073. end;
  2074. {$else HAS_DOS_DRIVES}
  2075. begin
  2076. DriveValid:=true;
  2077. end;
  2078. {$endif HAS_DOS_DRIVES}
  2079. {****************************************************************************}
  2080. { Equal }
  2081. {****************************************************************************}
  2082. function Equal(const S1, S2: String; Count: Sw_word): Boolean;
  2083. var
  2084. i: Sw_Word;
  2085. begin
  2086. Equal := False;
  2087. if (Length(S1) < Count) or (Length(S2) < Count) then
  2088. Exit;
  2089. for i := 1 to Count do
  2090. if UpCase(S1[I]) <> UpCase(S2[I]) then
  2091. Exit;
  2092. Equal := True;
  2093. end;
  2094. {****************************************************************************}
  2095. { ExtractDir }
  2096. {****************************************************************************}
  2097. function ExtractDir(AFile: FNameStr): DirStr;
  2098. { ExtractDir returns the path of AFile terminated with a trailing '\'. If
  2099. AFile contains no directory information, an empty string is returned. }
  2100. var
  2101. D: DirStr;
  2102. N: NameStr;
  2103. E: ExtStr;
  2104. begin
  2105. FSplit(AFile,D,N,E);
  2106. if D = '' then
  2107. begin
  2108. ExtractDir := '';
  2109. Exit;
  2110. end;
  2111. if D[Byte(D[0])] <> DirSeparator then
  2112. D := D + DirSeparator;
  2113. ExtractDir := D;
  2114. end;
  2115. {****************************************************************************}
  2116. { ExtractFileName }
  2117. {****************************************************************************}
  2118. function ExtractFileName(AFile: FNameStr): NameStr;
  2119. var
  2120. D: DirStr;
  2121. N: NameStr;
  2122. E: ExtStr;
  2123. begin
  2124. FSplit(AFile,D,N,E);
  2125. ExtractFileName := N;
  2126. end;
  2127. {****************************************************************************}
  2128. { FileExists }
  2129. {****************************************************************************}
  2130. function FileExists (AFile : FNameStr) : Boolean;
  2131. begin
  2132. FileExists := (FSearch(AFile,'') <> '');
  2133. end;
  2134. {****************************************************************************}
  2135. { GetCurDir }
  2136. {****************************************************************************}
  2137. function GetCurDir: DirStr;
  2138. var
  2139. CurDir: DirStr;
  2140. begin
  2141. GetDir(0, CurDir);
  2142. if (Length(CurDir) > 3) then
  2143. begin
  2144. Inc(CurDir[0]);
  2145. CurDir[Length(CurDir)] := DirSeparator;
  2146. end;
  2147. GetCurDir := CurDir;
  2148. end;
  2149. {****************************************************************************}
  2150. { GetCurDrive }
  2151. {****************************************************************************}
  2152. function GetCurDrive: Char;
  2153. {$ifdef go32v2}
  2154. var
  2155. Regs : Registers;
  2156. begin
  2157. Regs.AH := $19;
  2158. Intr($21,Regs);
  2159. GetCurDrive := Char(Regs.AL + Byte('A'));
  2160. end;
  2161. {$else not go32v2}
  2162. var
  2163. D : DirStr;
  2164. begin
  2165. D:=GetCurDir;
  2166. if (Length(D)>1) and (D[2]=':') then
  2167. begin
  2168. if (D[1]>='a') and (D[1]<='z') then
  2169. GetCurDrive:=Char(Byte(D[1])+Byte('A')-Byte('a'))
  2170. else
  2171. GetCurDrive:=D[1];
  2172. end
  2173. else
  2174. GetCurDrive:='C';
  2175. end;
  2176. {$endif not go32v2}
  2177. {****************************************************************************}
  2178. { IsDir }
  2179. {****************************************************************************}
  2180. function IsDir(const S: String): Boolean;
  2181. var
  2182. SR: SearchRec;
  2183. Is: boolean;
  2184. begin
  2185. Is:=false;
  2186. {$ifdef Unix}
  2187. Is:=(S=DirSeparator); { handle root }
  2188. {$else}
  2189. Is:=(length(S)=3) and (Upcase(S[1]) in['A'..'Z']) and (S[2]=':') and (S[3]=DirSeparator);
  2190. { handle root dirs }
  2191. {$endif}
  2192. if Is=false then
  2193. begin
  2194. FindFirst(S, Directory, SR);
  2195. if DosError = 0 then
  2196. Is := (SR.Attr and Directory) <> 0
  2197. else
  2198. Is := False;
  2199. {$ifdef fpc}
  2200. FindClose(SR);
  2201. {$endif}
  2202. end;
  2203. IsDir:=Is;
  2204. end;
  2205. {****************************************************************************}
  2206. { IsWild }
  2207. {****************************************************************************}
  2208. function IsWild(const S: String): Boolean;
  2209. begin
  2210. IsWild := (Pos('?',S) > 0) or (Pos('*',S) > 0);
  2211. end;
  2212. {****************************************************************************}
  2213. { IsList }
  2214. {****************************************************************************}
  2215. function IsList(const S: String): Boolean;
  2216. begin
  2217. IsList := (Pos(ListSeparator,S) > 0);
  2218. end;
  2219. {****************************************************************************}
  2220. { MakeResources }
  2221. {****************************************************************************}
  2222. procedure MakeResources;
  2223. var
  2224. Dlg : PDialog;
  2225. Key : String;
  2226. i : Word;
  2227. begin
  2228. for i := 0 to 1 do
  2229. begin
  2230. case i of
  2231. 0 : begin
  2232. Key := reOpenDlg;
  2233. Dlg := New(PFileDialog,Init('*.*',strings^.get(sOpen),
  2234. labels^.get(slName),
  2235. fdOkButton or fdHelpButton or fdNoLoadDir,0));
  2236. end;
  2237. 1 : begin
  2238. Key := reSaveAsDlg;
  2239. Dlg := New(PFileDialog,Init('*.*',strings^.get(sSaveAs),
  2240. labels^.get(slName),
  2241. fdOkButton or fdHelpButton or fdNoLoadDir,0));
  2242. end;
  2243. 2 : begin
  2244. Key := reEditChDirDialog;
  2245. Dlg := New(PEditChDirDialog,Init(cdHelpButton,
  2246. hiCurrentDirectories));
  2247. end;
  2248. end;
  2249. if Dlg = nil then
  2250. begin
  2251. PrintStr('Error initializing dialog ' + Key);
  2252. Halt;
  2253. end
  2254. else begin
  2255. RezFile^.Put(Dlg,Key);
  2256. if (RezFile^.Stream^.Status <> stOk) then
  2257. begin
  2258. PrintStr('Error writing dialog ' + Key + ' to the resource file.');
  2259. Halt;
  2260. end;
  2261. end;
  2262. end;
  2263. end;
  2264. {****************************************************************************}
  2265. { NoWildChars }
  2266. {****************************************************************************}
  2267. function NoWildChars(S: String): String;
  2268. const
  2269. WildChars : array[0..1] of Char = ('?','*');
  2270. var
  2271. i : Sw_Word;
  2272. begin
  2273. repeat
  2274. i := Pos('?',S);
  2275. if (i > 0) then
  2276. System.Delete(S,i,1);
  2277. until (i = 0);
  2278. repeat
  2279. i := Pos('*',S);
  2280. if (i > 0) then
  2281. System.Delete(S,i,1);
  2282. until (i = 0);
  2283. NoWildChars:=S;
  2284. end;
  2285. {****************************************************************************}
  2286. { OpenFile }
  2287. {****************************************************************************}
  2288. function OpenFile (var AFile : FNameStr; HistoryID : Byte) : Boolean;
  2289. var
  2290. Dlg : PFileDialog;
  2291. begin
  2292. {$ifdef cdResource}
  2293. Dlg := PFileDialog(RezFile^.Get(reOpenDlg));
  2294. {$else}
  2295. Dlg := New(PFileDialog,Init('*.*',strings^.get(sOpen),labels^.get(slName),
  2296. fdOkButton or fdHelpButton,0));
  2297. {$endif cdResource}
  2298. { this might not work }
  2299. PHistory(Dlg^.FileName^.Next^.Next)^.HistoryID := HistoryID;
  2300. OpenFile := (Application^.ExecuteDialog(Dlg,@AFile) = cmFileOpen);
  2301. end;
  2302. {****************************************************************************}
  2303. { OpenNewFile }
  2304. {****************************************************************************}
  2305. function OpenNewFile (var AFile: FNameStr; HistoryID: Byte): Boolean;
  2306. { OpenNewFile allows the user to select a directory from disk and enter a
  2307. new file name. If the file name entered is an existing file the user is
  2308. optionally prompted for confirmation of replacing the file based on the
  2309. value in #CheckOnReplace#. If a file name is successfully entered,
  2310. OpenNewFile returns True. }
  2311. {#X OpenFile }
  2312. begin
  2313. OpenNewFile := False;
  2314. if OpenFile(AFile,HistoryID) then
  2315. begin
  2316. if not ValidFileName(AFile) then
  2317. Exit;
  2318. if FileExists(AFile) then
  2319. if (not CheckOnReplace) or (not ReplaceFile(AFile)) then
  2320. Exit;
  2321. OpenNewFile := True;
  2322. end;
  2323. end;
  2324. {****************************************************************************}
  2325. { RegisterStdDlg }
  2326. {****************************************************************************}
  2327. procedure RegisterStdDlg;
  2328. begin
  2329. RegisterType(RFileInputLine);
  2330. RegisterType(RFileCollection);
  2331. RegisterType(RFileList);
  2332. RegisterType(RFileInfoPane);
  2333. RegisterType(RFileDialog);
  2334. RegisterType(RDirCollection);
  2335. RegisterType(RDirListBox);
  2336. RegisterType(RSortedListBox);
  2337. RegisterType(RChDirDialog);
  2338. end;
  2339. {****************************************************************************}
  2340. { StdReplaceFile }
  2341. {****************************************************************************}
  2342. function StdReplaceFile (AFile : FNameStr) : Boolean;
  2343. var
  2344. Rec : PStringRec;
  2345. begin
  2346. if CheckOnReplace then
  2347. begin
  2348. AFile := ShrinkPath(AFile,33);
  2349. Rec.AString := PString(@AFile);
  2350. StdReplaceFile :=
  2351. (MessageBox(^C + Strings^.Get(sReplaceFile),
  2352. @Rec,mfConfirmation or mfOkCancel) = cmOk);
  2353. end
  2354. else StdReplaceFile := True;
  2355. end;
  2356. {****************************************************************************}
  2357. { SaveAs }
  2358. {****************************************************************************}
  2359. function SaveAs (var AFile : FNameStr; HistoryID : Word) : Boolean;
  2360. var
  2361. Dlg : PFileDialog;
  2362. begin
  2363. SaveAs := False;
  2364. Dlg := New(PFileDialog,Init('*.*',strings^.get(sSaveAs),
  2365. labels^.get(slSaveAs),
  2366. fdOkButton or fdHelpButton,0));
  2367. { this might not work }
  2368. PHistory(Dlg^.FileName^.Next^.Next)^.HistoryID := HistoryID;
  2369. Dlg^.HelpCtx := hcSaveAs;
  2370. if (Application^.ExecuteDialog(Dlg,@AFile) = cmFileOpen) and
  2371. ((not FileExists(AFile)) or ReplaceFile(AFile)) then
  2372. SaveAs := True;
  2373. end;
  2374. {****************************************************************************}
  2375. { SelectDir }
  2376. {****************************************************************************}
  2377. function SelectDir (var ADir : DirStr; HistoryID : Byte) : Boolean;
  2378. var
  2379. Dir: DirStr;
  2380. Dlg : PEditChDirDialog;
  2381. Rec : DirStr;
  2382. begin
  2383. {$I-}
  2384. GetDir(0,Dir);
  2385. {$I+}
  2386. Rec := FExpand(ADir);
  2387. Dlg := New(PEditChDirDialog,Init(cdHelpButton,HistoryID));
  2388. if (Application^.ExecuteDialog(Dlg,@Rec) = cmOk) then
  2389. begin
  2390. SelectDir := True;
  2391. ADir := Rec;
  2392. end
  2393. else SelectDir := False;
  2394. {$I-}
  2395. ChDir(Dir);
  2396. {$I+}
  2397. end;
  2398. {****************************************************************************}
  2399. { ShrinkPath }
  2400. {****************************************************************************}
  2401. function ShrinkPath (AFile : FNameStr; MaxLen : Byte) : FNameStr;
  2402. var
  2403. Filler: string;
  2404. D1 : DirStr;
  2405. N1 : NameStr;
  2406. E1 : ExtStr;
  2407. i : Sw_Word;
  2408. begin
  2409. if Length(AFile) > MaxLen then
  2410. begin
  2411. FSplit(FExpand(AFile),D1,N1,E1);
  2412. AFile := Copy(D1,1,3) + '..' + DirSeparator;
  2413. i := Pred(Length(D1));
  2414. while (i > 0) and (D1[i] <> DirSeparator) do
  2415. Dec(i);
  2416. if (i = 0) then
  2417. AFile := AFile + D1
  2418. else AFile := AFile + Copy(D1,Succ(i),Length(D1)-i);
  2419. if AFile[Length(AFile)] <> DirSeparator then
  2420. AFile := AFile + DirSeparator;
  2421. if Length(AFile)+Length(N1)+Length(E1) <= MaxLen then
  2422. AFile := AFile + N1 + E1
  2423. else
  2424. begin
  2425. Filler := '...' + DirSeparator;
  2426. AFile:=Copy(Afile,1,MaxLen-Length(Filler)-Length(N1)-Length(E1))
  2427. +Filler+N1+E1;
  2428. end;
  2429. end;
  2430. ShrinkPath := AFile;
  2431. end;
  2432. {****************************************************************************}
  2433. { ValidFileName }
  2434. {****************************************************************************}
  2435. function ValidFileName(var FileName: PathStr): Boolean;
  2436. var
  2437. IllegalChars: string[12];
  2438. Dir: DirStr;
  2439. Name: NameStr;
  2440. Ext: ExtStr;
  2441. begin
  2442. {$ifdef PPC_FPC}
  2443. {$ifdef go32v2}
  2444. { spaces are allowed if LFN is supported }
  2445. if LFNSupport then
  2446. IllegalChars := ';,=+<>|"[]'+DirSeparator
  2447. else
  2448. IllegalChars := ';,=+<>|"[] '+DirSeparator;
  2449. {$else not go32v2}
  2450. {$ifdef win32}
  2451. IllegalChars := ';,=+<>|"[]'+DirSeparator;
  2452. {$else not go32v2 and not win32 }
  2453. IllegalChars := ';,=+<>|"[] '+DirSeparator;
  2454. {$endif not win32}
  2455. {$endif not go32v2}
  2456. {$else not PPC_FPC}
  2457. IllegalChars := ';,=+<>|"[] '+DirSeparator;
  2458. {$endif PPC_FPC}
  2459. ValidFileName := True;
  2460. FSplit(FileName, Dir, Name, Ext);
  2461. if not ((Dir = '') or PathValid(Dir)) or
  2462. Contains(Name, IllegalChars) or
  2463. Contains(Dir, IllegalChars) then
  2464. ValidFileName := False;
  2465. end;
  2466. {****************************************************************************}
  2467. { Unit Initialization Section }
  2468. {****************************************************************************}
  2469. begin
  2470. {$ifdef PPC_BP}
  2471. ReplaceFile := StdReplaceFile;
  2472. DeleteFile := StdDeleteFile;
  2473. {$else}
  2474. ReplaceFile := @StdReplaceFile;
  2475. DeleteFile := @StdDeleteFile;
  2476. {$endif PPC_BP}
  2477. end.