stddlg.pas 77 KB

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