stddlg.pas 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745
  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. inc(i2);
  697. inc(i1);
  698. if (i1>length(hstr1)) or (i2>length(hstr2)) then
  699. break;
  700. case hstr1[i1] of
  701. '?' :
  702. found:=true;
  703. '*' :
  704. begin
  705. found:=true;
  706. if (i1=length(hstr1)) then
  707. i2:=length(hstr2)
  708. else
  709. if (i1<length(hstr1)) and (hstr1[i1+1]<>hstr2[i2]) then
  710. begin
  711. if i2<length(hstr2) then
  712. dec(i1)
  713. end
  714. else
  715. if i2>1 then
  716. dec(i2);
  717. end;
  718. else
  719. found:=(hstr1[i1]=hstr2[i2]) or (hstr2[i2]='?');
  720. end;
  721. until (not found);
  722. if found then
  723. found:=(i1>=length(hstr1)) and (i2>=length(hstr2));
  724. CmpStr:=found;
  725. end;
  726. var
  727. D1,D2 : DirStr;
  728. N1,N2 : NameStr;
  729. E1,E2 : Extstr;
  730. begin
  731. {$ifdef Unix}
  732. FSplit(What,D1,N1,E1);
  733. FSplit(Mask,D2,N2,E2);
  734. {$else}
  735. FSplit(Upper(What),D1,N1,E1);
  736. FSplit(Upper(Mask),D2,N2,E2);
  737. {$endif}
  738. MatchesMask:=CmpStr(N2,N1) and CmpStr(E2,E1);
  739. end;
  740. function MatchesMaskList(What, MaskList: string): boolean;
  741. var P: integer;
  742. Match: boolean;
  743. begin
  744. Match:=false;
  745. if What<>'' then
  746. repeat
  747. P:=Pos(ListSeparator, MaskList);
  748. if P=0 then P:=length(MaskList)+1;
  749. Match:=MatchesMask(What,copy(MaskList,1,P-1));
  750. Delete(MaskList,1,P);
  751. until Match or (MaskList='');
  752. MatchesMaskList:=Match;
  753. end;
  754. constructor TFileList.Init(var Bounds: TRect; AScrollBar: PScrollBar);
  755. begin
  756. TSortedListBox.Init(Bounds, 2, AScrollBar);
  757. end;
  758. destructor TFileList.Done;
  759. begin
  760. if List <> nil then Dispose(List, Done);
  761. TListBox.Done;
  762. end;
  763. function TFileList.DataSize: Sw_Word;
  764. begin
  765. DataSize := 0;
  766. end;
  767. procedure TFileList.FocusItem(Item: Sw_Integer);
  768. begin
  769. TSortedListBox.FocusItem(Item);
  770. if (List^.Count > 0) then
  771. Message(Owner, evBroadcast, cmFileFocused, List^.At(Item));
  772. end;
  773. procedure TFileList.GetData(var Rec);
  774. begin
  775. end;
  776. function TFileList.GetKey(var S: String): Pointer;
  777. const
  778. SR: TSearchRec = ();
  779. procedure UpStr(var S: String);
  780. var
  781. I: Sw_Integer;
  782. begin
  783. for I := 1 to Length(S) do S[I] := UpCase(S[I]);
  784. end;
  785. begin
  786. if (HandleDir{ShiftState and $03 <> 0}) or ((S <> '') and (S[1]='.')) then
  787. SR.Attr := Directory
  788. else SR.Attr := 0;
  789. SR.Name := S;
  790. {$ifndef Unix}
  791. UpStr(SR.Name);
  792. {$endif Unix}
  793. GetKey := @SR;
  794. end;
  795. function TFileList.GetText(Item,MaxLen: Sw_Integer): String;
  796. var
  797. S: String;
  798. SR: PSearchRec;
  799. begin
  800. SR := PSearchRec(List^.At(Item));
  801. S := SR^.Name;
  802. if SR^.Attr and Directory <> 0 then
  803. begin
  804. S[Length(S)+1] := DirSeparator;
  805. Inc(S[0]);
  806. end;
  807. GetText := S;
  808. end;
  809. procedure TFileList.HandleEvent(var Event: TEvent);
  810. var
  811. S : String;
  812. K : pointer;
  813. Value : Sw_integer;
  814. begin
  815. if (Event.What = evMouseDown) and (Event.Double) then
  816. begin
  817. Event.What := evCommand;
  818. Event.Command := cmOK;
  819. PutEvent(Event);
  820. ClearEvent(Event);
  821. end
  822. else if (Event.What = evKeyDown) and (Event.CharCode='<') then
  823. begin
  824. { select '..' }
  825. S := '..';
  826. K := GetKey(S);
  827. If PSortedCollection(List)^.Search(K, Value) then
  828. FocusItem(Value);
  829. end
  830. else TSortedListBox.HandleEvent(Event);
  831. end;
  832. procedure TFileList.ReadDirectory(AWildCard: PathStr);
  833. const
  834. FindAttr = ReadOnly + Archive;
  835. {$ifdef Unix}
  836. AllFiles = '*';
  837. {$else}
  838. AllFiles = '*.*';
  839. {$endif}
  840. PrevDir = '..';
  841. var
  842. S: SearchRec;
  843. P: PSearchRec;
  844. FileList: PFileCollection;
  845. NumFiles: Word;
  846. FindStr,
  847. WildName : string;
  848. Dir: DirStr;
  849. Ext: ExtStr;
  850. Name: NameStr;
  851. Event : TEvent;
  852. Tmp: PathStr;
  853. begin
  854. NumFiles := 0;
  855. FileList := New(PFileCollection, Init(5, 5));
  856. AWildCard := FExpand(AWildCard);
  857. FSplit(AWildCard, Dir, Name, Ext);
  858. if pos(ListSeparator,AWildCard)>0 then
  859. begin
  860. WildName:=Copy(AWildCard,length(Dir)+1,255);
  861. FindStr:=Dir+AllFiles;
  862. end
  863. else
  864. begin
  865. WildName:=Name+Ext;
  866. FindStr:=AWildCard;
  867. end;
  868. FindFirst(FindStr, FindAttr, S);
  869. P := PSearchRec(@P);
  870. while assigned(P) and (DosError = 0) do
  871. begin
  872. if (S.Attr and Directory = 0) and
  873. MatchesMaskList(S.Name,WildName) then
  874. begin
  875. P := MemAlloc(SizeOf(P^));
  876. if assigned(P) then
  877. begin
  878. P^.Attr:=S.Attr;
  879. P^.Time:=S.Time;
  880. P^.Size:=S.Size;
  881. P^.Name:=S.Name;
  882. FileList^.Insert(P);
  883. end;
  884. end;
  885. FindNext(S);
  886. end;
  887. {$ifdef fpc}
  888. FindClose(S);
  889. {$endif}
  890. Tmp := Dir + AllFiles;
  891. FindFirst(Tmp, Directory, S);
  892. while (P <> nil) and (DosError = 0) do
  893. begin
  894. if (S.Attr and Directory <> 0) and (S.Name <> '.') and (S.Name <> '..') then
  895. begin
  896. P := MemAlloc(SizeOf(P^));
  897. if P <> nil then
  898. begin
  899. P^.Attr:=S.Attr;
  900. P^.Time:=S.Time;
  901. P^.Size:=S.Size;
  902. P^.Name:=S.Name;
  903. FileList^.Insert(P);
  904. end;
  905. end;
  906. FindNext(S);
  907. end;
  908. {$ifdef fpc}
  909. FindClose(S);
  910. {$endif}
  911. {$ifndef Unix}
  912. if Length(Dir) > 4 then
  913. {$endif not Unix}
  914. begin
  915. P := MemAlloc(SizeOf(P^));
  916. if P <> nil then
  917. begin
  918. FindFirst(Tmp, Directory, S);
  919. FindNext(S);
  920. if (DosError = 0) and (S.Name = PrevDir) then
  921. begin
  922. P^.Attr:=S.Attr;
  923. P^.Time:=S.Time;
  924. P^.Size:=S.Size;
  925. P^.Name:=S.Name;
  926. end
  927. else
  928. begin
  929. P^.Name := PrevDir;
  930. P^.Size := 0;
  931. P^.Time := $210000;
  932. P^.Attr := Directory;
  933. end;
  934. FileList^.Insert(PSearchRec(P));
  935. {$ifdef fpc}
  936. FindClose(S);
  937. {$endif}
  938. end;
  939. end;
  940. if P = nil then
  941. MessageBox(strings^.get(sTooManyFiles), nil, mfOkButton + mfWarning);
  942. NewList(FileList);
  943. if List^.Count > 0 then
  944. begin
  945. Event.What := evBroadcast;
  946. Event.Command := cmFileFocused;
  947. Event.InfoPtr := List^.At(0);
  948. Owner^.HandleEvent(Event);
  949. end;
  950. end;
  951. procedure TFileList.SetData(var Rec);
  952. begin
  953. with PFileDialog(Owner)^ do
  954. Self.ReadDirectory(Directory^ + WildCard);
  955. end;
  956. {****************************************************************************}
  957. { TFileInfoPane Object }
  958. {****************************************************************************}
  959. {****************************************************************************}
  960. { TFileInfoPane.Init }
  961. {****************************************************************************}
  962. constructor TFileInfoPane.Init(var Bounds: TRect);
  963. begin
  964. TView.Init(Bounds);
  965. FillChar(S,SizeOf(S),#0);
  966. EventMask := EventMask or evBroadcast;
  967. end;
  968. {****************************************************************************}
  969. { TFileInfoPane.Draw }
  970. {****************************************************************************}
  971. procedure TFileInfoPane.Draw;
  972. var
  973. B: TDrawBuffer;
  974. D: String[9];
  975. M: String[3];
  976. PM: Boolean;
  977. Color: Word;
  978. Time: DateTime;
  979. Path: PathStr;
  980. FmtId: String;
  981. Params: array[0..7] of LongInt;
  982. Str: String[80];
  983. const
  984. sDirectoryLine = ' %-12s %-9s %3s %2d, %4d %2d:%02d%cm';
  985. sFileLine = ' %-12s %-9d %3s %2d, %4d %2d:%02d%cm';
  986. InValidFiles : array[0..2] of string[12] = ('','.','..');
  987. var
  988. Month: array[1..12] of String[3];
  989. begin
  990. Month[1] := Strings^.Get(smJan);
  991. Month[2] := Strings^.Get(smFeb);
  992. Month[3] := Strings^.Get(smMar);
  993. Month[4] := Strings^.Get(smApr);
  994. Month[5] := Strings^.Get(smMay);
  995. Month[6] := Strings^.Get(smJun);
  996. Month[7] := Strings^.Get(smJul);
  997. Month[8] := Strings^.Get(smAug);
  998. Month[9] := Strings^.Get(smSep);
  999. Month[10] := Strings^.Get(smOct);
  1000. Month[11] := Strings^.Get(smNov);
  1001. Month[12] := Strings^.Get(smDec);
  1002. { Display path }
  1003. if (PFileDialog(Owner)^.Directory <> nil) then
  1004. Path := PFileDialog(Owner)^.Directory^
  1005. else Path := '';
  1006. Path := FExpand(Path+PFileDialog(Owner)^.WildCard);
  1007. { avoid B Buffer overflow PM }
  1008. Path := ShrinkPath(Path, Size.X - 1);
  1009. Color := GetColor($01);
  1010. MoveChar(B, ' ', Color, Size.X); { fill with empty spaces }
  1011. WriteLine(0, 0, Size.X, Size.Y, B);
  1012. MoveStr(B[1], Path, Color);
  1013. WriteLine(0, 0, Size.X, 1, B);
  1014. if (S.Name = InValidFiles[0]) or (S.Name = InValidFiles[1]) or
  1015. (S.Name = InValidFiles[2]) then
  1016. Exit;
  1017. { Display file }
  1018. Params[0] := LongInt(@S.Name);
  1019. if S.Attr and Directory <> 0 then
  1020. begin
  1021. FmtId := sDirectoryLine;
  1022. D := Strings^.Get(sDirectory);
  1023. Params[1] := LongInt(@D);
  1024. end else
  1025. begin
  1026. FmtId := sFileLine;
  1027. Params[1] := S.Size;
  1028. end;
  1029. UnpackTime(S.Time, Time);
  1030. M := Month[Time.Month];
  1031. Params[2] := LongInt(@M);
  1032. Params[3] := Time.Day;
  1033. Params[4] := Time.Year;
  1034. PM := Time.Hour >= 12;
  1035. Time.Hour := Time.Hour mod 12;
  1036. if Time.Hour = 0 then Time.Hour := 12;
  1037. Params[5] := Time.Hour;
  1038. Params[6] := Time.Min;
  1039. if PM then
  1040. Params[7] := Byte('p')
  1041. else Params[7] := Byte('a');
  1042. FormatStr(Str, FmtId, Params);
  1043. MoveStr(B, Str, Color);
  1044. WriteLine(0, 1, Size.X, 1, B);
  1045. { Fill in rest of rectangle }
  1046. MoveChar(B, ' ', Color, Size.X);
  1047. WriteLine(0, 2, Size.X, Size.Y-2, B);
  1048. end;
  1049. function TFileInfoPane.GetPalette: PPalette;
  1050. const
  1051. P: String[Length(CInfoPane)] = CInfoPane;
  1052. begin
  1053. GetPalette := PPalette(@P);
  1054. end;
  1055. procedure TFileInfoPane.HandleEvent(var Event: TEvent);
  1056. begin
  1057. TView.HandleEvent(Event);
  1058. if (Event.What = evBroadcast) and (Event.Command = cmFileFocused) then
  1059. begin
  1060. S := PSearchRec(Event.InfoPtr)^;
  1061. DrawView;
  1062. end;
  1063. end;
  1064. {****************************************************************************
  1065. TFileHistory
  1066. ****************************************************************************}
  1067. function LTrim(const S: String): String;
  1068. var
  1069. I: Sw_Integer;
  1070. begin
  1071. I := 1;
  1072. while (I < Length(S)) and (S[I] = ' ') do Inc(I);
  1073. LTrim := Copy(S, I, 255);
  1074. end;
  1075. function RTrim(const S: String): String;
  1076. var
  1077. I: Sw_Integer;
  1078. begin
  1079. I := Length(S);
  1080. while S[I] = ' ' do Dec(I);
  1081. RTrim := Copy(S, 1, I);
  1082. end;
  1083. function RelativePath(var S: PathStr): Boolean;
  1084. begin
  1085. S := LTrim(RTrim(S));
  1086. RelativePath := not ((S <> '') and ((S[1] = DirSeparator) or (S[2] = ':')));
  1087. end;
  1088. { try to reduce the length of S+dir as a file path+pattern }
  1089. function Simplify (var S,Dir : string) : string;
  1090. var i : sw_integer;
  1091. begin
  1092. if RelativePath(Dir) then
  1093. begin
  1094. if (S<>'') and (Copy(Dir,1,3)='..'+DirSeparator) then
  1095. begin
  1096. i:=Length(S);
  1097. for i:=Length(S)-1 downto 1 do
  1098. if S[i]=DirSeparator then
  1099. break;
  1100. if S[i]=DirSeparator then
  1101. Simplify:=Copy(S,1,i)+Copy(Dir,4,255)
  1102. else
  1103. Simplify:=S+Dir;
  1104. end
  1105. else
  1106. Simplify:=S+Dir;
  1107. end
  1108. else
  1109. Simplify:=Dir;
  1110. end;
  1111. {****************************************************************************}
  1112. { TFileHistory.HandleEvent }
  1113. {****************************************************************************}
  1114. procedure TFileHistory.HandleEvent(var Event: TEvent);
  1115. var
  1116. HistoryWindow: PHistoryWindow;
  1117. R,P: TRect;
  1118. C: Word;
  1119. Rslt: String;
  1120. begin
  1121. TView.HandleEvent(Event);
  1122. if (Event.What = evMouseDown) or
  1123. ((Event.What = evKeyDown) and (CtrlToArrow(Event.KeyCode) = kbDown) and
  1124. (Link^.State and sfFocused <> 0)) then
  1125. begin
  1126. if not Link^.Focus then
  1127. begin
  1128. ClearEvent(Event);
  1129. Exit;
  1130. end;
  1131. if assigned(CurDir) then
  1132. Rslt:=CurDir^
  1133. else
  1134. Rslt:='';
  1135. Rslt:=Simplify(Rslt,Link^.Data^);
  1136. RemoveDoubleDirSep(Rslt);
  1137. If IsWild(Rslt) then
  1138. RecordHistory(Rslt);
  1139. Link^.GetBounds(R);
  1140. Dec(R.A.X); Inc(R.B.X); Inc(R.B.Y,7); Dec(R.A.Y,1);
  1141. Owner^.GetExtent(P);
  1142. R.Intersect(P);
  1143. Dec(R.B.Y,1);
  1144. HistoryWindow := InitHistoryWindow(R);
  1145. if HistoryWindow <> nil then
  1146. begin
  1147. C := Owner^.ExecView(HistoryWindow);
  1148. if C = cmOk then
  1149. begin
  1150. Rslt := HistoryWindow^.GetSelection;
  1151. if Length(Rslt) > Link^.MaxLen then Rslt[0] := Char(Link^.MaxLen);
  1152. Link^.Data^ := Rslt;
  1153. Link^.SelectAll(True);
  1154. Link^.DrawView;
  1155. end;
  1156. Dispose(HistoryWindow, Done);
  1157. end;
  1158. ClearEvent(Event);
  1159. end
  1160. else if (Event.What = evBroadcast) then
  1161. if ((Event.Command = cmReleasedFocus) and (Event.InfoPtr = Link))
  1162. or (Event.Command = cmRecordHistory) then
  1163. begin
  1164. if assigned(CurDir) then
  1165. Rslt:=CurDir^
  1166. else
  1167. Rslt:='';
  1168. Rslt:=Simplify(Rslt,Link^.Data^);
  1169. RemoveDoubleDirSep(Rslt);
  1170. If IsWild(Rslt) then
  1171. RecordHistory(Rslt);
  1172. end;
  1173. end;
  1174. procedure TFileHistory.AdaptHistoryToDir(Dir : string);
  1175. var S,S2 : String;
  1176. i,Count : Sw_word;
  1177. begin
  1178. if assigned(CurDir) then
  1179. begin
  1180. S:=CurDir^;
  1181. if S=Dir then
  1182. exit;
  1183. DisposeStr(CurDir);
  1184. end
  1185. else
  1186. S:='';
  1187. CurDir:=NewStr(Simplify(S,Dir));
  1188. Count:=HistoryCount(HistoryId);
  1189. for i:=1 to count do
  1190. begin
  1191. S2:=HistoryStr(HistoryId,1);
  1192. HistoryRemove(HistoryId,1);
  1193. if RelativePath(S2) then
  1194. if S<>'' then
  1195. S2:=S+S2
  1196. else
  1197. S2:=FExpand(S2);
  1198. { simply full path
  1199. we should simplify relative to Dir ! }
  1200. HistoryAdd(HistoryId,S2);
  1201. end;
  1202. end;
  1203. destructor TFileHistory.Done;
  1204. begin
  1205. If assigned(CurDir) then
  1206. DisposeStr(CurDir);
  1207. Inherited Done;
  1208. end;
  1209. {****************************************************************************
  1210. TFileDialog
  1211. ****************************************************************************}
  1212. constructor TFileDialog.Init(AWildCard: TWildStr; const ATitle,
  1213. InputName: String; AOptions: Word; HistoryId: Byte);
  1214. var
  1215. Control: PView;
  1216. R: TRect;
  1217. Opt: Word;
  1218. begin
  1219. R.Assign(15,1,64,20);
  1220. TDialog.Init(R, ATitle);
  1221. Options := Options or ofCentered;
  1222. WildCard := AWildCard;
  1223. R.Assign(3,3,31,4);
  1224. FileName := New(PFileInputLine, Init(R, 79));
  1225. FileName^.Data^ := WildCard;
  1226. Insert(FileName);
  1227. R.Assign(2,2,3+CStrLen(InputName),3);
  1228. Control := New(PLabel, Init(R, InputName, FileName));
  1229. Insert(Control);
  1230. R.Assign(31,3,34,4);
  1231. FileHistory := New(PFileHistory, Init(R, FileName, HistoryId));
  1232. Insert(FileHistory);
  1233. R.Assign(3,14,34,15);
  1234. Control := New(PScrollBar, Init(R));
  1235. Insert(Control);
  1236. R.Assign(3,6,34,14);
  1237. FileList := New(PFileList, Init(R, PScrollBar(Control)));
  1238. Insert(FileList);
  1239. R.Assign(2,5,8,6);
  1240. Control := New(PLabel, Init(R, labels^.get(slFiles), FileList));
  1241. Insert(Control);
  1242. R.Assign(35,3,46,5);
  1243. Opt := bfDefault;
  1244. if AOptions and fdOpenButton <> 0 then
  1245. begin
  1246. Insert(New(PButton, Init(R,labels^.get(slOpen), cmFileOpen, Opt)));
  1247. Opt := bfNormal;
  1248. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1249. end;
  1250. if AOptions and fdOkButton <> 0 then
  1251. begin
  1252. Insert(New(PButton, Init(R,labels^.get(slOk), cmFileOpen, Opt)));
  1253. Opt := bfNormal;
  1254. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1255. end;
  1256. if AOptions and fdReplaceButton <> 0 then
  1257. begin
  1258. Insert(New(PButton, Init(R, labels^.get(slReplace),cmFileReplace, Opt)));
  1259. Opt := bfNormal;
  1260. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1261. end;
  1262. if AOptions and fdClearButton <> 0 then
  1263. begin
  1264. Insert(New(PButton, Init(R, labels^.get(slClear),cmFileClear, Opt)));
  1265. Opt := bfNormal;
  1266. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1267. end;
  1268. Insert(New(PButton, Init(R, labels^.get(slCancel), cmCancel, bfNormal)));
  1269. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1270. if AOptions and fdHelpButton <> 0 then
  1271. begin
  1272. Insert(New(PButton, Init(R,labels^.get(slHelp),cmHelp, bfNormal)));
  1273. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1274. end;
  1275. R.Assign(1,16,48,18);
  1276. Control := New(PFileInfoPane, Init(R));
  1277. Insert(Control);
  1278. SelectNext(False);
  1279. if AOptions and fdNoLoadDir = 0 then ReadDirectory;
  1280. end;
  1281. constructor TFileDialog.Load(var S: TStream);
  1282. begin
  1283. if not TDialog.Load(S) then
  1284. Fail;
  1285. S.Read(WildCard, SizeOf(WildCard));
  1286. if (S.Status <> stOk) then
  1287. begin
  1288. TDialog.Done;
  1289. Fail;
  1290. end;
  1291. GetSubViewPtr(S, FileName);
  1292. GetSubViewPtr(S, FileList);
  1293. GetSubViewPtr(S, FileHistory);
  1294. ReadDirectory;
  1295. if (DosError <> 0) then
  1296. begin
  1297. TDialog.Done;
  1298. Fail;
  1299. end;
  1300. end;
  1301. destructor TFileDialog.Done;
  1302. begin
  1303. DisposeStr(Directory);
  1304. TDialog.Done;
  1305. end;
  1306. procedure TFileDialog.GetData(var Rec);
  1307. begin
  1308. GetFilename(PathStr(Rec));
  1309. end;
  1310. procedure TFileDialog.GetFileName(var S: PathStr);
  1311. var
  1312. Path: PathStr;
  1313. Name: NameStr;
  1314. Ext: ExtStr;
  1315. TWild : string;
  1316. TPath: PathStr;
  1317. TName: NameStr;
  1318. TExt: NameStr;
  1319. i : Sw_integer;
  1320. begin
  1321. S := FileName^.Data^;
  1322. if RelativePath(S) then
  1323. begin
  1324. if (Directory <> nil) then
  1325. S := FExpand(Directory^ + S);
  1326. end
  1327. else
  1328. S := FExpand(S);
  1329. if Pos(ListSeparator,S)=0 then
  1330. begin
  1331. If FileExists(S) then
  1332. exit;
  1333. FSplit(S, Path, Name, Ext);
  1334. if ((Name = '') or (Ext = '')) and not IsDir(S) then
  1335. begin
  1336. TWild:=WildCard;
  1337. repeat
  1338. i:=Pos(ListSeparator,TWild);
  1339. if i=0 then
  1340. i:=length(TWild)+1;
  1341. FSplit(Copy(TWild,1,i-1), TPath, TName, TExt);
  1342. if ((Name = '') and (Ext = '')) then
  1343. S := Path + TName + TExt
  1344. else
  1345. if Name = '' then
  1346. S := Path + TName + Ext
  1347. else
  1348. if Ext = '' then
  1349. begin
  1350. if IsWild(Name) then
  1351. S := Path + Name + TExt
  1352. else
  1353. S := Path + Name + NoWildChars(TExt);
  1354. end;
  1355. if FileExists(S) then
  1356. break;
  1357. System.Delete(TWild,1,i);
  1358. until TWild='';
  1359. if TWild='' then
  1360. S := Path + Name + Ext;
  1361. end;
  1362. end;
  1363. end;
  1364. procedure TFileDialog.HandleEvent(var Event: TEvent);
  1365. begin
  1366. if (Event.What and evBroadcast <> 0) and
  1367. (Event.Command = cmListItemSelected) then
  1368. begin
  1369. EndModal(cmFileOpen);
  1370. ClearEvent(Event);
  1371. end;
  1372. TDialog.HandleEvent(Event);
  1373. if Event.What = evCommand then
  1374. case Event.Command of
  1375. cmFileOpen, cmFileReplace, cmFileClear:
  1376. begin
  1377. EndModal(Event.Command);
  1378. ClearEvent(Event);
  1379. end;
  1380. end;
  1381. end;
  1382. procedure TFileDialog.SetData(var Rec);
  1383. begin
  1384. TDialog.SetData(Rec);
  1385. if (PathStr(Rec) <> '') and (IsWild(TWildStr(Rec))) then
  1386. begin
  1387. Valid(cmFileInit);
  1388. FileName^.Select;
  1389. end;
  1390. end;
  1391. procedure TFileDialog.ReadDirectory;
  1392. begin
  1393. FileList^.ReadDirectory(WildCard);
  1394. FileHistory^.AdaptHistoryToDir(GetCurDir);
  1395. Directory := NewStr(GetCurDir);
  1396. end;
  1397. procedure TFileDialog.Store(var S: TStream);
  1398. begin
  1399. TDialog.Store(S);
  1400. S.Write(WildCard, SizeOf(WildCard));
  1401. PutSubViewPtr(S, FileName);
  1402. PutSubViewPtr(S, FileList);
  1403. PutSubViewPtr(S, FileHistory);
  1404. end;
  1405. function TFileDialog.Valid(Command: Word): Boolean;
  1406. var
  1407. FName: PathStr;
  1408. Dir: DirStr;
  1409. Name: NameStr;
  1410. Ext: ExtStr;
  1411. function CheckDirectory(var S: PathStr): Boolean;
  1412. begin
  1413. if not PathValid(S) then
  1414. begin
  1415. MessageBox(Strings^.Get(sInvalidDriveOrDir), nil, mfError + mfOkButton);
  1416. FileName^.Select;
  1417. CheckDirectory := False;
  1418. end else CheckDirectory := True;
  1419. end;
  1420. function CompleteDir(const Path: string): string;
  1421. begin
  1422. { keep c: untouched PM }
  1423. if (Path<>'') and (Path[Length(Path)]<>DirSeparator) and
  1424. (Path[Length(Path)]<>':') then
  1425. CompleteDir:=Path+DirSeparator
  1426. else
  1427. CompleteDir:=Path;
  1428. end;
  1429. function NormalizeDir(const Path: string): string;
  1430. var Root: boolean;
  1431. begin
  1432. Root:=false;
  1433. {$ifdef Unix}
  1434. if Path=DirSeparator then Root:=true;
  1435. {$else}
  1436. if (length(Path)=3) and (Upcase(Path[1]) in['A'..'Z']) and
  1437. (Path[2]=':') and (Path[3]=DirSeparator) then
  1438. Root:=true;
  1439. {$endif}
  1440. if (Root=false) and (copy(Path,length(Path),1)=DirSeparator) then
  1441. NormalizeDir:=copy(Path,1,length(Path)-1)
  1442. else
  1443. NormalizeDir:=Path;
  1444. end;
  1445. function NormalizeDirF(var S: openstring): boolean;
  1446. begin
  1447. S:=NormalizeDir(S);
  1448. NormalizeDirF:=true;
  1449. end;
  1450. begin
  1451. if Command = 0 then
  1452. begin
  1453. Valid := True;
  1454. Exit;
  1455. end
  1456. else Valid := False;
  1457. if TDialog.Valid(Command) then
  1458. begin
  1459. GetFileName(FName);
  1460. if (Command <> cmCancel) and (Command <> cmFileClear) then
  1461. begin
  1462. if IsWild(FName) or IsList(FName) then
  1463. begin
  1464. FSplit(FName, Dir, Name, Ext);
  1465. if CheckDirectory(Dir) then
  1466. begin
  1467. FileHistory^.AdaptHistoryToDir(Dir);
  1468. DisposeStr(Directory);
  1469. Directory := NewStr(Dir);
  1470. if Pos(ListSeparator,FName)>0 then
  1471. WildCard:=Copy(FName,length(Dir)+1,255)
  1472. else
  1473. WildCard := Name+Ext;
  1474. if Command <> cmFileInit then
  1475. FileList^.Select;
  1476. FileList^.ReadDirectory(Directory^+WildCard);
  1477. end;
  1478. end
  1479. else
  1480. if NormalizeDirF(FName) then
  1481. { ^^ this is just a dummy if construct (the func always returns true,
  1482. it's just there, 'coz I don't want to rearrange the following "if"s... }
  1483. if IsDir(FName) then
  1484. begin
  1485. if CheckDirectory(FName) then
  1486. begin
  1487. FileHistory^.AdaptHistoryToDir(CompleteDir(FName));
  1488. DisposeStr(Directory);
  1489. Directory := NewSTr(CompleteDir(FName));
  1490. if Command <> cmFileInit then FileList^.Select;
  1491. FileList^.ReadDirectory(Directory^+WildCard);
  1492. end
  1493. end
  1494. else
  1495. if ValidFileName(FName) then
  1496. Valid := True
  1497. else
  1498. begin
  1499. MessageBox(^C + Strings^.Get(sInvalidFileName), nil, mfError + mfOkButton);
  1500. Valid := False;
  1501. end;
  1502. end
  1503. else Valid := True;
  1504. end;
  1505. end;
  1506. { TDirCollection }
  1507. function TDirCollection.GetItem(var S: TStream): Pointer;
  1508. var
  1509. DirItem: PDirEntry;
  1510. begin
  1511. New(DirItem);
  1512. DirItem^.DisplayText := S.ReadStr;
  1513. DirItem^.Directory := S.ReadStr;
  1514. GetItem := DirItem;
  1515. end;
  1516. procedure TDirCollection.FreeItem(Item: Pointer);
  1517. var
  1518. DirItem: PDirEntry absolute Item;
  1519. begin
  1520. DisposeStr(DirItem^.DisplayText);
  1521. DisposeStr(DirItem^.Directory);
  1522. Dispose(DirItem);
  1523. end;
  1524. procedure TDirCollection.PutItem(var S: TStream; Item: Pointer);
  1525. var
  1526. DirItem: PDirEntry absolute Item;
  1527. begin
  1528. S.WriteStr(DirItem^.DisplayText);
  1529. S.WriteStr(DirItem^.Directory);
  1530. end;
  1531. { TDirListBox }
  1532. const
  1533. DrivesS: String = '';
  1534. Drives: PString = @DrivesS;
  1535. constructor TDirListBox.Init(var Bounds: TRect; AScrollBar:
  1536. PScrollBar);
  1537. begin
  1538. DrivesS := strings^.get(sDrives);
  1539. TListBox.Init(Bounds, 1, AScrollBar);
  1540. Dir := '';
  1541. end;
  1542. destructor TDirListBox.Done;
  1543. begin
  1544. if (List <> nil) then
  1545. Dispose(List,Done);
  1546. TListBox.Done;
  1547. end;
  1548. function TDirListBox.GetText(Item,MaxLen: Sw_Integer): String;
  1549. begin
  1550. GetText := PDirEntry(List^.At(Item))^.DisplayText^;
  1551. end;
  1552. procedure TDirListBox.HandleEvent(var Event: TEvent);
  1553. begin
  1554. case Event.What of
  1555. evMouseDown:
  1556. if Event.Double then
  1557. begin
  1558. Event.What := evCommand;
  1559. Event.Command := cmChangeDir;
  1560. PutEvent(Event);
  1561. ClearEvent(Event);
  1562. end;
  1563. evKeyboard:
  1564. if (Event.CharCode = ' ') and
  1565. (PSearchRec(List^.At(Focused))^.Name = '..') then
  1566. NewDirectory(PSearchRec(List^.At(Focused))^.Name);
  1567. end;
  1568. TListBox.HandleEvent(Event);
  1569. end;
  1570. function TDirListBox.IsSelected(Item: Sw_Integer): Boolean;
  1571. begin
  1572. { IsSelected := Item = Cur; }
  1573. IsSelected := Inherited IsSelected(Item);
  1574. end;
  1575. procedure TDirListBox.NewDirectory(var ADir: DirStr);
  1576. const
  1577. PathDir = 'ÀÄÂ';
  1578. FirstDir = 'ÀÂÄ';
  1579. MiddleDir = ' ÃÄ';
  1580. LastDir = ' ÀÄ';
  1581. IndentSize = ' ';
  1582. var
  1583. AList: PCollection;
  1584. NewDir, Dirct: DirStr;
  1585. C, OldC: Char;
  1586. S, Indent: String[80];
  1587. P: PString;
  1588. NewCur: Word;
  1589. isFirst: Boolean;
  1590. SR: SearchRec;
  1591. I: Sw_Integer;
  1592. function NewDirEntry(const DisplayText, Directory: String): PDirEntry;{$ifdef PPC_BP}near;{$endif}
  1593. var
  1594. DirEntry: PDirEntry;
  1595. begin
  1596. New(DirEntry);
  1597. DirEntry^.DisplayText := NewStr(DisplayText);
  1598. If Directory='' then
  1599. DirEntry^.Directory := NewStr(DirSeparator)
  1600. else
  1601. DirEntry^.Directory := NewStr(Directory);
  1602. NewDirEntry := DirEntry;
  1603. end;
  1604. begin
  1605. Dir := ADir;
  1606. AList := New(PDirCollection, Init(5,5));
  1607. {$ifdef HAS_DOS_DRIVES}
  1608. AList^.Insert(NewDirEntry(Drives^,Drives^));
  1609. if Dir = Drives^ then
  1610. begin
  1611. isFirst := True;
  1612. OldC := ' ';
  1613. for C := 'A' to 'Z' do
  1614. begin
  1615. if (C < 'C') or DriveValid(C) then
  1616. begin
  1617. if OldC <> ' ' then
  1618. begin
  1619. if isFirst then
  1620. begin
  1621. S := FirstDir + OldC;
  1622. isFirst := False;
  1623. end
  1624. else S := MiddleDir + OldC;
  1625. AList^.Insert(NewDirEntry(S, OldC + ':' + DirSeparator));
  1626. end;
  1627. if C = GetCurDrive then NewCur := AList^.Count;
  1628. OldC := C;
  1629. end;
  1630. end;
  1631. if OldC <> ' ' then
  1632. AList^.Insert(NewDirEntry(LastDir + OldC, OldC + ':' + DirSeparator));
  1633. end
  1634. else
  1635. {$endif HAS_DOS_DRIVES}
  1636. begin
  1637. Indent := IndentSize;
  1638. NewDir := Dir;
  1639. {$ifdef HAS_DOS_DRIVES}
  1640. Dirct := Copy(NewDir,1,3);
  1641. AList^.Insert(NewDirEntry(PathDir + Dirct, Dirct));
  1642. NewDir := Copy(NewDir,4,255);
  1643. {$else HAS_DOS_DRIVES}
  1644. Dirct := '';
  1645. {$endif HAS_DOS_DRIVES}
  1646. while NewDir <> '' do
  1647. begin
  1648. I := Pos(DirSeparator,NewDir);
  1649. if I <> 0 then
  1650. begin
  1651. S := Copy(NewDir,1,I-1);
  1652. Dirct := Dirct + S;
  1653. AList^.Insert(NewDirEntry(Indent + PathDir + S, Dirct));
  1654. NewDir := Copy(NewDir,I+1,255);
  1655. end
  1656. else
  1657. begin
  1658. Dirct := Dirct + NewDir;
  1659. AList^.Insert(NewDirEntry(Indent + PathDir + NewDir, Dirct));
  1660. NewDir := '';
  1661. end;
  1662. Indent := Indent + IndentSize;
  1663. Dirct := Dirct + DirSeparator;
  1664. end;
  1665. NewCur := AList^.Count-1;
  1666. isFirst := True;
  1667. NewDir := Dirct + '*.*';
  1668. FindFirst(NewDir, Directory, SR);
  1669. while DosError = 0 do
  1670. begin
  1671. if (SR.Attr and Directory <> 0) and
  1672. {$ifdef FPC}
  1673. (SR.Name <> '.') and (SR.Name <> '..') then
  1674. {$else : not FPC}
  1675. (SR.Name[1] <> '.') then
  1676. {$endif not FPC}
  1677. begin
  1678. if isFirst then
  1679. begin
  1680. S := FirstDir;
  1681. isFirst := False;
  1682. end else S := MiddleDir;
  1683. AList^.Insert(NewDirEntry(Indent + S + SR.Name, Dirct + SR.Name));
  1684. end;
  1685. FindNext(SR);
  1686. end;
  1687. {$ifdef fpc}
  1688. FindClose(SR);
  1689. {$endif}
  1690. P := PDirEntry(AList^.At(AList^.Count-1))^.DisplayText;
  1691. I := Pos('À',P^);
  1692. if I = 0 then
  1693. begin
  1694. I := Pos('Ã',P^);
  1695. if I <> 0 then P^[I] := 'À';
  1696. end else
  1697. begin
  1698. P^[I+1] := 'Ä';
  1699. P^[I+2] := 'Ä';
  1700. end;
  1701. end;
  1702. NewList(AList);
  1703. FocusItem(NewCur);
  1704. Cur:=NewCur;
  1705. end;
  1706. procedure TDirListBox.SetState(AState: Word; Enable: Boolean);
  1707. begin
  1708. TListBox.SetState(AState, Enable);
  1709. if AState and sfFocused <> 0 then
  1710. PChDirDialog(Owner)^.ChDirButton^.MakeDefault(Enable);
  1711. end;
  1712. {****************************************************************************}
  1713. { TChDirDialog Object }
  1714. {****************************************************************************}
  1715. {****************************************************************************}
  1716. { TChDirDialog.Init }
  1717. {****************************************************************************}
  1718. constructor TChDirDialog.Init(AOptions: Word; HistoryId: Sw_Word);
  1719. var
  1720. R: TRect;
  1721. Control: PView;
  1722. begin
  1723. R.Assign(16, 2, 64, 20);
  1724. TDialog.Init(R,strings^.get(sChangeDirectory));
  1725. Options := Options or ofCentered;
  1726. R.Assign(3, 3, 30, 4);
  1727. DirInput := New(PInputLine, Init(R, 68));
  1728. Insert(DirInput);
  1729. R.Assign(2, 2, 17, 3);
  1730. Control := New(PLabel, Init(R,labels^.get(slDirectoryName), DirInput));
  1731. Insert(Control);
  1732. R.Assign(30, 3, 33, 4);
  1733. Control := New(PHistory, Init(R, DirInput, HistoryId));
  1734. Insert(Control);
  1735. R.Assign(32, 6, 33, 16);
  1736. Control := New(PScrollBar, Init(R));
  1737. Insert(Control);
  1738. R.Assign(3, 6, 32, 16);
  1739. DirList := New(PDirListBox, Init(R, PScrollBar(Control)));
  1740. Insert(DirList);
  1741. R.Assign(2, 5, 17, 6);
  1742. Control := New(PLabel, Init(R, labels^.get(slDirectoryTree), DirList));
  1743. Insert(Control);
  1744. R.Assign(35, 6, 45, 8);
  1745. OkButton := New(PButton, Init(R, labels^.get(slOk), cmOK, bfDefault));
  1746. Insert(OkButton);
  1747. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1748. ChDirButton := New(PButton,Init(R,labels^.get(slChDir),cmChangeDir,
  1749. bfNormal));
  1750. Insert(ChDirButton);
  1751. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1752. Insert(New(PButton, Init(R,labels^.get(slRevert), cmRevert, bfNormal)));
  1753. if AOptions and cdHelpButton <> 0 then
  1754. begin
  1755. Inc(R.A.Y,3); Inc(R.B.Y,3);
  1756. Insert(New(PButton, Init(R,labels^.get(slHelp), cmHelp, bfNormal)));
  1757. end;
  1758. if AOptions and cdNoLoadDir = 0 then SetUpDialog;
  1759. SelectNext(False);
  1760. end;
  1761. {****************************************************************************}
  1762. { TChDirDialog.Load }
  1763. {****************************************************************************}
  1764. constructor TChDirDialog.Load(var S: TStream);
  1765. begin
  1766. TDialog.Load(S);
  1767. GetSubViewPtr(S, DirList);
  1768. GetSubViewPtr(S, DirInput);
  1769. GetSubViewPtr(S, OkButton);
  1770. GetSubViewPtr(S, ChDirbutton);
  1771. SetUpDialog;
  1772. end;
  1773. {****************************************************************************}
  1774. { TChDirDialog.DataSize }
  1775. {****************************************************************************}
  1776. function TChDirDialog.DataSize: Sw_Word;
  1777. begin
  1778. DataSize := 0;
  1779. end;
  1780. {****************************************************************************}
  1781. { TChDirDialog.GetData }
  1782. {****************************************************************************}
  1783. procedure TChDirDialog.GetData(var Rec);
  1784. begin
  1785. end;
  1786. {****************************************************************************}
  1787. { TChDirDialog.HandleEvent }
  1788. {****************************************************************************}
  1789. procedure TChDirDialog.HandleEvent(var Event: TEvent);
  1790. var
  1791. CurDir: DirStr;
  1792. P: PDirEntry;
  1793. begin
  1794. TDialog.HandleEvent(Event);
  1795. case Event.What of
  1796. evCommand:
  1797. begin
  1798. case Event.Command of
  1799. cmRevert: GetDir(0,CurDir);
  1800. cmChangeDir:
  1801. begin
  1802. P := DirList^.List^.At(DirList^.Focused);
  1803. if (P^.Directory^ = Drives^)
  1804. or DriveValid(P^.Directory^[1]) then
  1805. CurDir := P^.Directory^
  1806. else Exit;
  1807. end;
  1808. else
  1809. Exit;
  1810. end;
  1811. if (Length(CurDir) > 3) and
  1812. (CurDir[Length(CurDir)] = DirSeparator) then
  1813. CurDir := Copy(CurDir,1,Length(CurDir)-1);
  1814. DirList^.NewDirectory(CurDir);
  1815. DirInput^.Data^ := CurDir;
  1816. DirInput^.DrawView;
  1817. DirList^.Select;
  1818. ClearEvent(Event);
  1819. end;
  1820. end;
  1821. end;
  1822. {****************************************************************************}
  1823. { TChDirDialog.SetData }
  1824. {****************************************************************************}
  1825. procedure TChDirDialog.SetData(var Rec);
  1826. begin
  1827. end;
  1828. {****************************************************************************}
  1829. { TChDirDialog.SetUpDialog }
  1830. {****************************************************************************}
  1831. procedure TChDirDialog.SetUpDialog;
  1832. var
  1833. CurDir: DirStr;
  1834. begin
  1835. if DirList <> nil then
  1836. begin
  1837. CurDir := GetCurDir;
  1838. DirList^.NewDirectory(CurDir);
  1839. if (Length(CurDir) > 3) and (CurDir[Length(CurDir)] = DirSeparator) then
  1840. CurDir := Copy(CurDir,1,Length(CurDir)-1);
  1841. if DirInput <> nil then
  1842. begin
  1843. DirInput^.Data^ := CurDir;
  1844. DirInput^.DrawView;
  1845. end;
  1846. end;
  1847. end;
  1848. {****************************************************************************}
  1849. { TChDirDialog.Store }
  1850. {****************************************************************************}
  1851. procedure TChDirDialog.Store(var S: TStream);
  1852. begin
  1853. TDialog.Store(S);
  1854. PutSubViewPtr(S, DirList);
  1855. PutSubViewPtr(S, DirInput);
  1856. PutSubViewPtr(S, OkButton);
  1857. PutSubViewPtr(S, ChDirButton);
  1858. end;
  1859. {****************************************************************************}
  1860. { TChDirDialog.Valid }
  1861. {****************************************************************************}
  1862. function TChDirDialog.Valid(Command: Word): Boolean;
  1863. var
  1864. P: PathStr;
  1865. begin
  1866. Valid := True;
  1867. if Command = cmOk then
  1868. begin
  1869. P := FExpand(DirInput^.Data^);
  1870. if (Length(P) > 3) and (P[Length(P)] = DirSeparator) then
  1871. Dec(P[0]);
  1872. {$I-}
  1873. ChDir(P);
  1874. if (IOResult <> 0) then
  1875. begin
  1876. MessageBox(Strings^.Get(sInvalidDirectory), nil, mfError + mfOkButton);
  1877. Valid := False;
  1878. end;
  1879. {$I+}
  1880. end;
  1881. end;
  1882. {****************************************************************************}
  1883. { TEditChDirDialog Object }
  1884. {****************************************************************************}
  1885. {****************************************************************************}
  1886. { TEditChDirDialog.DataSize }
  1887. {****************************************************************************}
  1888. function TEditChDirDialog.DataSize : Sw_Word;
  1889. begin
  1890. DataSize := SizeOf(DirStr);
  1891. end;
  1892. {****************************************************************************}
  1893. { TEditChDirDialog.GetData }
  1894. {****************************************************************************}
  1895. procedure TEditChDirDialog.GetData (var Rec);
  1896. var
  1897. CurDir : DirStr absolute Rec;
  1898. begin
  1899. if (DirInput = nil) then
  1900. CurDir := ''
  1901. else begin
  1902. CurDir := DirInput^.Data^;
  1903. if (CurDir[Length(CurDir)] <> DirSeparator) then
  1904. CurDir := CurDir + DirSeparator;
  1905. end;
  1906. end;
  1907. {****************************************************************************}
  1908. { TEditChDirDialog.SetData }
  1909. {****************************************************************************}
  1910. procedure TEditChDirDialog.SetData (var Rec);
  1911. var
  1912. CurDir : DirStr absolute Rec;
  1913. begin
  1914. if DirList <> nil then
  1915. begin
  1916. DirList^.NewDirectory(CurDir);
  1917. if DirInput <> nil then
  1918. begin
  1919. if (Length(CurDir) > 3) and (CurDir[Length(CurDir)] = DirSeparator) then
  1920. DirInput^.Data^ := Copy(CurDir,1,Length(CurDir)-1)
  1921. else DirInput^.Data^ := CurDir;
  1922. DirInput^.DrawView;
  1923. end;
  1924. end;
  1925. end;
  1926. {****************************************************************************}
  1927. { TSortedListBox Object }
  1928. {****************************************************************************}
  1929. {****************************************************************************}
  1930. { TSortedListBox.Init }
  1931. {****************************************************************************}
  1932. constructor TSortedListBox.Init(var Bounds: TRect; ANumCols: Sw_Word;
  1933. AScrollBar: PScrollBar);
  1934. begin
  1935. TListBox.Init(Bounds, ANumCols, AScrollBar);
  1936. SearchPos := 0;
  1937. ShowCursor;
  1938. SetCursor(1,0);
  1939. end;
  1940. {****************************************************************************}
  1941. { TSortedListBox.HandleEvent }
  1942. {****************************************************************************}
  1943. procedure TSortedListBox.HandleEvent(var Event: TEvent);
  1944. const
  1945. SpecialChars: set of Char = [#0,#9,#27];
  1946. var
  1947. CurString, NewString: String;
  1948. K: Pointer;
  1949. Value : Sw_integer;
  1950. OldPos, OldValue: Sw_Integer;
  1951. T: Boolean;
  1952. begin
  1953. OldValue := Focused;
  1954. TListBox.HandleEvent(Event);
  1955. if (OldValue <> Focused) or
  1956. ((Event.What = evBroadcast) and (Event.InfoPtr = @Self) and
  1957. (Event.Command = cmReleasedFocus)) then
  1958. SearchPos := 0;
  1959. if Event.What = evKeyDown then
  1960. begin
  1961. { patched to prevent error when no or empty list or Escape pressed }
  1962. if (not (Event.CharCode in SpecialChars)) and
  1963. (List <> nil) and (List^.Count > 0) then
  1964. begin
  1965. Value := Focused;
  1966. if Value < Range then
  1967. CurString := GetText(Value, 255)
  1968. else
  1969. CurString := '';
  1970. OldPos := SearchPos;
  1971. if Event.KeyCode = kbBack then
  1972. begin
  1973. if SearchPos = 0 then Exit;
  1974. Dec(SearchPos);
  1975. if SearchPos = 0 then
  1976. HandleDir:= ((GetShiftState and $3) <> 0) or (Event.CharCode in ['A'..'Z']);
  1977. CurString[0] := Char(SearchPos);
  1978. end
  1979. else if (Event.CharCode = '.') then
  1980. SearchPos := Pos('.',CurString)
  1981. else
  1982. begin
  1983. Inc(SearchPos);
  1984. if SearchPos = 1 then
  1985. HandleDir := ((GetShiftState and 3) <> 0) or (Event.CharCode in ['A'..'Z']);
  1986. CurString[0] := Char(SearchPos);
  1987. CurString[SearchPos] := Event.CharCode;
  1988. end;
  1989. K := GetKey(CurString);
  1990. T := PSortedCollection(List)^.Search(K, Value);
  1991. if Value < Range then
  1992. begin
  1993. if Value < Range then
  1994. NewString := GetText(Value, 255)
  1995. else
  1996. NewString := '';
  1997. if Equal(NewString, CurString, SearchPos) then
  1998. begin
  1999. if Value <> OldValue then
  2000. begin
  2001. FocusItem(Value);
  2002. { Assumes ListControl will set the cursor to the first character }
  2003. { of the sfFocused item }
  2004. SetCursor(Cursor.X+SearchPos, Cursor.Y);
  2005. end
  2006. else
  2007. SetCursor(Cursor.X+(SearchPos-OldPos), Cursor.Y);
  2008. end
  2009. else
  2010. SearchPos := OldPos;
  2011. end
  2012. else SearchPos := OldPos;
  2013. if (SearchPos <> OldPos) or (Event.CharCode in ['A'..'Z','a'..'z']) then
  2014. ClearEvent(Event);
  2015. end;
  2016. end;
  2017. end;
  2018. function TSortedListBox.GetKey(var S: String): Pointer;
  2019. begin
  2020. GetKey := @S;
  2021. end;
  2022. procedure TSortedListBox.NewList(AList: PCollection);
  2023. begin
  2024. TListBox.NewList(AList);
  2025. SearchPos := 0;
  2026. end;
  2027. {****************************************************************************}
  2028. { Global Procedures and Functions }
  2029. {****************************************************************************}
  2030. {****************************************************************************}
  2031. { Contains }
  2032. {****************************************************************************}
  2033. function Contains(S1, S2: String): Boolean;
  2034. { Contains returns true if S1 contains any characters in S2. }
  2035. var
  2036. i : Byte;
  2037. begin
  2038. Contains := True;
  2039. i := 1;
  2040. while ((i < Length(S2)) and (i < Length(S1))) do
  2041. if (Upcase(S1[i]) = Upcase(S2[i])) then
  2042. Exit
  2043. else Inc(i);
  2044. Contains := False;
  2045. end;
  2046. {****************************************************************************}
  2047. { StdDeleteFile }
  2048. {****************************************************************************}
  2049. function StdDeleteFile (AFile : FNameStr) : Boolean;
  2050. var
  2051. Rec : PStringRec;
  2052. begin
  2053. if CheckOnDelete then
  2054. begin
  2055. AFile := ShrinkPath(AFile,33);
  2056. Rec.AString := PString(@AFile);
  2057. StdDeleteFile := (MessageBox(^C + Strings^.Get(sDeleteFile),
  2058. @Rec,mfConfirmation or mfOkCancel) = cmOk);
  2059. end
  2060. else StdDeleteFile := False;
  2061. end;
  2062. {****************************************************************************}
  2063. { DriveValid }
  2064. {****************************************************************************}
  2065. function DriveValid(Drive: Char): Boolean;
  2066. {$ifdef HAS_DOS_DRIVES}
  2067. var
  2068. D: Char;
  2069. begin
  2070. D := GetCurDrive;
  2071. {$I-}
  2072. ChDir(Drive+':');
  2073. if (IOResult = 0) then
  2074. begin
  2075. DriveValid := True;
  2076. ChDir(D+':')
  2077. end
  2078. else DriveValid := False;
  2079. {$I+}
  2080. end;
  2081. {$else HAS_DOS_DRIVES}
  2082. begin
  2083. DriveValid:=true;
  2084. end;
  2085. {$endif HAS_DOS_DRIVES}
  2086. {****************************************************************************}
  2087. { Equal }
  2088. {****************************************************************************}
  2089. function Equal(const S1, S2: String; Count: Sw_word): Boolean;
  2090. var
  2091. i: Sw_Word;
  2092. begin
  2093. Equal := False;
  2094. if (Length(S1) < Count) or (Length(S2) < Count) then
  2095. Exit;
  2096. for i := 1 to Count do
  2097. if UpCase(S1[I]) <> UpCase(S2[I]) then
  2098. Exit;
  2099. Equal := True;
  2100. end;
  2101. {****************************************************************************}
  2102. { ExtractDir }
  2103. {****************************************************************************}
  2104. function ExtractDir(AFile: FNameStr): DirStr;
  2105. { ExtractDir returns the path of AFile terminated with a trailing '\'. If
  2106. AFile contains no directory information, an empty string is returned. }
  2107. var
  2108. D: DirStr;
  2109. N: NameStr;
  2110. E: ExtStr;
  2111. begin
  2112. FSplit(AFile,D,N,E);
  2113. if D = '' then
  2114. begin
  2115. ExtractDir := '';
  2116. Exit;
  2117. end;
  2118. if D[Byte(D[0])] <> DirSeparator then
  2119. D := D + DirSeparator;
  2120. ExtractDir := D;
  2121. end;
  2122. {****************************************************************************}
  2123. { ExtractFileName }
  2124. {****************************************************************************}
  2125. function ExtractFileName(AFile: FNameStr): NameStr;
  2126. var
  2127. D: DirStr;
  2128. N: NameStr;
  2129. E: ExtStr;
  2130. begin
  2131. FSplit(AFile,D,N,E);
  2132. ExtractFileName := N;
  2133. end;
  2134. {****************************************************************************}
  2135. { FileExists }
  2136. {****************************************************************************}
  2137. function FileExists (AFile : FNameStr) : Boolean;
  2138. begin
  2139. FileExists := (FSearch(AFile,'') <> '');
  2140. end;
  2141. {****************************************************************************}
  2142. { GetCurDir }
  2143. {****************************************************************************}
  2144. function GetCurDir: DirStr;
  2145. var
  2146. CurDir: DirStr;
  2147. begin
  2148. GetDir(0, CurDir);
  2149. if (Length(CurDir) > 3) then
  2150. begin
  2151. Inc(CurDir[0]);
  2152. CurDir[Length(CurDir)] := DirSeparator;
  2153. end;
  2154. GetCurDir := CurDir;
  2155. end;
  2156. {****************************************************************************}
  2157. { GetCurDrive }
  2158. {****************************************************************************}
  2159. function GetCurDrive: Char;
  2160. {$ifdef go32v2}
  2161. var
  2162. Regs : Registers;
  2163. begin
  2164. Regs.AH := $19;
  2165. Intr($21,Regs);
  2166. GetCurDrive := Char(Regs.AL + Byte('A'));
  2167. end;
  2168. {$else not go32v2}
  2169. var
  2170. D : DirStr;
  2171. begin
  2172. D:=GetCurDir;
  2173. if (Length(D)>1) and (D[2]=':') then
  2174. begin
  2175. if (D[1]>='a') and (D[1]<='z') then
  2176. GetCurDrive:=Char(Byte(D[1])+Byte('A')-Byte('a'))
  2177. else
  2178. GetCurDrive:=D[1];
  2179. end
  2180. else
  2181. GetCurDrive:='C';
  2182. end;
  2183. {$endif not go32v2}
  2184. {****************************************************************************}
  2185. { IsDir }
  2186. {****************************************************************************}
  2187. function IsDir(const S: String): Boolean;
  2188. var
  2189. SR: SearchRec;
  2190. Is: boolean;
  2191. begin
  2192. Is:=false;
  2193. {$ifdef Unix}
  2194. Is:=(S=DirSeparator); { handle root }
  2195. {$else}
  2196. Is:=(length(S)=3) and (Upcase(S[1]) in['A'..'Z']) and (S[2]=':') and (S[3]=DirSeparator);
  2197. { handle root dirs }
  2198. {$endif}
  2199. if Is=false then
  2200. begin
  2201. FindFirst(S, Directory, SR);
  2202. if DosError = 0 then
  2203. Is := (SR.Attr and Directory) <> 0
  2204. else
  2205. Is := False;
  2206. {$ifdef fpc}
  2207. FindClose(SR);
  2208. {$endif}
  2209. end;
  2210. IsDir:=Is;
  2211. end;
  2212. {****************************************************************************}
  2213. { IsWild }
  2214. {****************************************************************************}
  2215. function IsWild(const S: String): Boolean;
  2216. begin
  2217. IsWild := (Pos('?',S) > 0) or (Pos('*',S) > 0);
  2218. end;
  2219. {****************************************************************************}
  2220. { IsList }
  2221. {****************************************************************************}
  2222. function IsList(const S: String): Boolean;
  2223. begin
  2224. IsList := (Pos(ListSeparator,S) > 0);
  2225. end;
  2226. {****************************************************************************}
  2227. { MakeResources }
  2228. {****************************************************************************}
  2229. procedure MakeResources;
  2230. var
  2231. Dlg : PDialog;
  2232. Key : String;
  2233. i : Word;
  2234. begin
  2235. for i := 0 to 1 do
  2236. begin
  2237. case i of
  2238. 0 : begin
  2239. Key := reOpenDlg;
  2240. Dlg := New(PFileDialog,Init('*.*',strings^.get(sOpen),
  2241. labels^.get(slName),
  2242. fdOkButton or fdHelpButton or fdNoLoadDir,0));
  2243. end;
  2244. 1 : begin
  2245. Key := reSaveAsDlg;
  2246. Dlg := New(PFileDialog,Init('*.*',strings^.get(sSaveAs),
  2247. labels^.get(slName),
  2248. fdOkButton or fdHelpButton or fdNoLoadDir,0));
  2249. end;
  2250. 2 : begin
  2251. Key := reEditChDirDialog;
  2252. Dlg := New(PEditChDirDialog,Init(cdHelpButton,
  2253. hiCurrentDirectories));
  2254. end;
  2255. end;
  2256. if Dlg = nil then
  2257. begin
  2258. PrintStr('Error initializing dialog ' + Key);
  2259. Halt;
  2260. end
  2261. else begin
  2262. RezFile^.Put(Dlg,Key);
  2263. if (RezFile^.Stream^.Status <> stOk) then
  2264. begin
  2265. PrintStr('Error writing dialog ' + Key + ' to the resource file.');
  2266. Halt;
  2267. end;
  2268. end;
  2269. end;
  2270. end;
  2271. {****************************************************************************}
  2272. { NoWildChars }
  2273. {****************************************************************************}
  2274. function NoWildChars(S: String): String;
  2275. const
  2276. WildChars : array[0..1] of Char = ('?','*');
  2277. var
  2278. i : Sw_Word;
  2279. begin
  2280. repeat
  2281. i := Pos('?',S);
  2282. if (i > 0) then
  2283. System.Delete(S,i,1);
  2284. until (i = 0);
  2285. repeat
  2286. i := Pos('*',S);
  2287. if (i > 0) then
  2288. System.Delete(S,i,1);
  2289. until (i = 0);
  2290. NoWildChars:=S;
  2291. end;
  2292. {****************************************************************************}
  2293. { OpenFile }
  2294. {****************************************************************************}
  2295. function OpenFile (var AFile : FNameStr; HistoryID : Byte) : Boolean;
  2296. var
  2297. Dlg : PFileDialog;
  2298. begin
  2299. {$ifdef cdResource}
  2300. Dlg := PFileDialog(RezFile^.Get(reOpenDlg));
  2301. {$else}
  2302. Dlg := New(PFileDialog,Init('*.*',strings^.get(sOpen),labels^.get(slName),
  2303. fdOkButton or fdHelpButton,0));
  2304. {$endif cdResource}
  2305. { this might not work }
  2306. PHistory(Dlg^.FileName^.Next^.Next)^.HistoryID := HistoryID;
  2307. OpenFile := (Application^.ExecuteDialog(Dlg,@AFile) = cmFileOpen);
  2308. end;
  2309. {****************************************************************************}
  2310. { OpenNewFile }
  2311. {****************************************************************************}
  2312. function OpenNewFile (var AFile: FNameStr; HistoryID: Byte): Boolean;
  2313. { OpenNewFile allows the user to select a directory from disk and enter a
  2314. new file name. If the file name entered is an existing file the user is
  2315. optionally prompted for confirmation of replacing the file based on the
  2316. value in #CheckOnReplace#. If a file name is successfully entered,
  2317. OpenNewFile returns True. }
  2318. {#X OpenFile }
  2319. begin
  2320. OpenNewFile := False;
  2321. if OpenFile(AFile,HistoryID) then
  2322. begin
  2323. if not ValidFileName(AFile) then
  2324. Exit;
  2325. if FileExists(AFile) then
  2326. if (not CheckOnReplace) or (not ReplaceFile(AFile)) then
  2327. Exit;
  2328. OpenNewFile := True;
  2329. end;
  2330. end;
  2331. {****************************************************************************}
  2332. { RegisterStdDlg }
  2333. {****************************************************************************}
  2334. procedure RegisterStdDlg;
  2335. begin
  2336. RegisterType(RFileInputLine);
  2337. RegisterType(RFileCollection);
  2338. RegisterType(RFileList);
  2339. RegisterType(RFileInfoPane);
  2340. RegisterType(RFileDialog);
  2341. RegisterType(RDirCollection);
  2342. RegisterType(RDirListBox);
  2343. RegisterType(RSortedListBox);
  2344. RegisterType(RChDirDialog);
  2345. end;
  2346. {****************************************************************************}
  2347. { StdReplaceFile }
  2348. {****************************************************************************}
  2349. function StdReplaceFile (AFile : FNameStr) : Boolean;
  2350. var
  2351. Rec : PStringRec;
  2352. begin
  2353. if CheckOnReplace then
  2354. begin
  2355. AFile := ShrinkPath(AFile,33);
  2356. Rec.AString := PString(@AFile);
  2357. StdReplaceFile :=
  2358. (MessageBox(^C + Strings^.Get(sReplaceFile),
  2359. @Rec,mfConfirmation or mfOkCancel) = cmOk);
  2360. end
  2361. else StdReplaceFile := True;
  2362. end;
  2363. {****************************************************************************}
  2364. { SaveAs }
  2365. {****************************************************************************}
  2366. function SaveAs (var AFile : FNameStr; HistoryID : Word) : Boolean;
  2367. var
  2368. Dlg : PFileDialog;
  2369. begin
  2370. SaveAs := False;
  2371. {$ifdef cdResource}
  2372. Dlg := PFileDialog(RezFile^.Get(reSaveAsDlg));
  2373. {$else}
  2374. Dlg := New(PFileDialog,Init('*.*',strings^.get(sSaveAs),
  2375. labels^.get(slSaveAs),
  2376. fdOkButton or fdHelpButton,0));
  2377. {$endif cdResource}
  2378. { this might not work }
  2379. PHistory(Dlg^.FileName^.Next^.Next)^.HistoryID := HistoryID;
  2380. Dlg^.HelpCtx := hcSaveAs;
  2381. if (Application^.ExecuteDialog(Dlg,@AFile) = cmFileOpen) and
  2382. ((not FileExists(AFile)) or ReplaceFile(AFile)) then
  2383. SaveAs := True;
  2384. end;
  2385. {****************************************************************************}
  2386. { SelectDir }
  2387. {****************************************************************************}
  2388. function SelectDir (var ADir : DirStr; HistoryID : Byte) : Boolean;
  2389. var
  2390. Dir: DirStr;
  2391. Dlg : PEditChDirDialog;
  2392. Rec : DirStr;
  2393. begin
  2394. {$I-}
  2395. GetDir(0,Dir);
  2396. {$I+}
  2397. Rec := FExpand(ADir);
  2398. {$ifdef cdResource}
  2399. Dlg := PEditChDirDialog(RezFile^.Get(reEditChDirDialog));
  2400. {$else}
  2401. Dlg := New(PEditChDirDialog,Init(cdHelpButton,HistoryID));
  2402. {$endif cdResource}
  2403. if (Application^.ExecuteDialog(Dlg,@Rec) = cmOk) then
  2404. begin
  2405. SelectDir := True;
  2406. ADir := Rec;
  2407. end
  2408. else SelectDir := False;
  2409. {$I-}
  2410. ChDir(Dir);
  2411. {$I+}
  2412. end;
  2413. {****************************************************************************}
  2414. { ShrinkPath }
  2415. {****************************************************************************}
  2416. function ShrinkPath (AFile : FNameStr; MaxLen : Byte) : FNameStr;
  2417. var
  2418. Filler: string;
  2419. D1 : DirStr;
  2420. N1 : NameStr;
  2421. E1 : ExtStr;
  2422. i : Sw_Word;
  2423. begin
  2424. if Length(AFile) > MaxLen then
  2425. begin
  2426. FSplit(FExpand(AFile),D1,N1,E1);
  2427. AFile := Copy(D1,1,3) + '..' + DirSeparator;
  2428. i := Pred(Length(D1));
  2429. while (i > 0) and (D1[i] <> DirSeparator) do
  2430. Dec(i);
  2431. if (i = 0) then
  2432. AFile := AFile + D1
  2433. else AFile := AFile + Copy(D1,Succ(i),Length(D1)-i);
  2434. if AFile[Length(AFile)] <> DirSeparator then
  2435. AFile := AFile + DirSeparator;
  2436. if Length(AFile)+Length(N1)+Length(E1) <= MaxLen then
  2437. AFile := AFile + N1 + E1
  2438. else
  2439. begin
  2440. Filler := '...' + DirSeparator;
  2441. AFile:=Copy(Afile,1,MaxLen-Length(Filler)-Length(N1)-Length(E1))
  2442. +Filler+N1+E1;
  2443. end;
  2444. end;
  2445. ShrinkPath := AFile;
  2446. end;
  2447. {****************************************************************************}
  2448. { ValidFileName }
  2449. {****************************************************************************}
  2450. function ValidFileName(var FileName: PathStr): Boolean;
  2451. var
  2452. IllegalChars: string[12];
  2453. Dir: DirStr;
  2454. Name: NameStr;
  2455. Ext: ExtStr;
  2456. begin
  2457. {$ifdef PPC_FPC}
  2458. {$ifdef go32v2}
  2459. { spaces are allowed if LFN is supported }
  2460. if LFNSupport then
  2461. IllegalChars := ';,=+<>|"[]'+DirSeparator
  2462. else
  2463. IllegalChars := ';,=+<>|"[] '+DirSeparator;
  2464. {$else not go32v2}
  2465. {$ifdef win32}
  2466. IllegalChars := ';,=+<>|"[]'+DirSeparator;
  2467. {$else not go32v2 and not win32 }
  2468. IllegalChars := ';,=+<>|"[] '+DirSeparator;
  2469. {$endif not win32}
  2470. {$endif not go32v2}
  2471. {$else not PPC_FPC}
  2472. IllegalChars := ';,=+<>|"[] '+DirSeparator;
  2473. {$endif PPC_FPC}
  2474. ValidFileName := True;
  2475. FSplit(FileName, Dir, Name, Ext);
  2476. if not ((Dir = '') or PathValid(Dir)) or
  2477. Contains(Name, IllegalChars) or
  2478. Contains(Dir, IllegalChars) then
  2479. ValidFileName := False;
  2480. end;
  2481. {****************************************************************************}
  2482. { Unit Initialization Section }
  2483. {****************************************************************************}
  2484. begin
  2485. {$ifdef PPC_BP}
  2486. ReplaceFile := StdReplaceFile;
  2487. DeleteFile := StdDeleteFile;
  2488. {$else}
  2489. ReplaceFile := @StdReplaceFile;
  2490. DeleteFile := @StdDeleteFile;
  2491. {$endif PPC_BP}
  2492. end.