crt.tex 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. %
  2. % $Id$
  3. % This file is part of the FPC documentation.
  4. % Copyright (C) 1997, by Michael Van Canneyt
  5. %
  6. % The FPC documentation is free text; you can redistribute it and/or
  7. % modify it under the terms of the GNU Library General Public License as
  8. % published by the Free Software Foundation; either version 2 of the
  9. % License, or (at your option) any later version.
  10. %
  11. % The FPC Documentation is distributed in the hope that it will be useful,
  12. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. % Library General Public License for more details.
  15. %
  16. % You should have received a copy of the GNU Library General Public
  17. % License along with the FPC documentation; see the file COPYING.LIB. If not,
  18. % write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. % Boston, MA 02111-1307, USA.
  20. %
  21. \chapter{The CRT unit.}
  22. \label{ch:crtunit}
  23. \FPCexampledir{crtex}
  24. This chapter describes the \var{CRT} unit for Free Pascal, both under \dos
  25. \linux and \windows. The unit was first written for \dos by Florian kl\"ampfl.
  26. The unit was ported to \linux by Mark May\footnote{Current
  27. e-mail address \textsf{[email protected]}}, and enhanced by Micha\"el Van Canneyt
  28. and Peter Vreman. It works on the \linux console, and in xterm and rxvt windows
  29. under X-Windows. The functionality for both is the same, except that under
  30. \linux the use of an early implementation (versions 0.9.1 and earlier of the
  31. compiler) the crt unit automatically cleared the screen at program startup.
  32. There are some caveats when using the CRT unit:
  33. \begin{itemize}
  34. \item Programs using the CRT unit will {\em not} be usable when input/output
  35. is being redirected on the command-line.
  36. \item For similar reasons they are not usable as CGI-scripts for use with a
  37. webserver.
  38. \item The use of the CRT unit and the graph unit may not always be supported.
  39. \item On \linux or other unix OSes , executing other programs that expect
  40. special terminal behaviour (using one of the special functions in the linux
  41. unit) will not work. The terminal is set in RAW mode, which will destroy
  42. most terminal emulation settings.
  43. \end{itemize}
  44. This chapter is divided in two sections.
  45. \begin{itemize}
  46. \item The first section lists the pre-defined constants, types and variables.
  47. \item The second section describes the functions which appear in the
  48. interface part of the CRT unit.
  49. \end{itemize}
  50. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  51. % Types, Variables, Constants
  52. \section{Types, Variables, Constants}
  53. Color definitions :
  54. \begin{verbatim}
  55. Black = 0;
  56. Blue = 1;
  57. Green = 2;
  58. Cyan = 3;
  59. Red = 4;
  60. Magenta = 5;
  61. Brown = 6;
  62. LightGray = 7;
  63. DarkGray = 8;
  64. LightBlue = 9;
  65. LightGreen = 10;
  66. LightCyan = 11;
  67. LightRed = 12;
  68. LightMagenta = 13;
  69. Yellow = 14;
  70. White = 15;
  71. Blink = 128;
  72. \end{verbatim}
  73. Miscellaneous constants
  74. \begin{verbatim}
  75. TextAttr: Byte = $07;
  76. TextChar: Char = ' ';
  77. CheckBreak: Boolean = True;
  78. CheckEOF: Boolean = False;
  79. CheckSnow: Boolean = False;
  80. DirectVideo: Boolean = False;
  81. LastMode: Word = 3;
  82. WindMin: Word = $0;
  83. WindMax: Word = $184f;
  84. ScreenWidth = 80;
  85. ScreenHeight = 25;
  86. \end{verbatim}
  87. Some variables for compatibility with Turbo Pascal. However, they're not
  88. used by \fpc.
  89. \begin{verbatim}
  90. var
  91. checkbreak : boolean;
  92. checkeof : boolean;
  93. checksnow : boolean;
  94. \end{verbatim}
  95. The following constants define screen modes on a \dos system:
  96. \begin{verbatim}
  97. Const
  98. bw40 = 0;
  99. co40 = 1;
  100. bw80 = 2;
  101. co80 = 3;
  102. mono = 7;
  103. \end{verbatim}
  104. The \var{TextAttr} variable controls the attributes with which characters
  105. are written to screen.
  106. \begin{verbatim}
  107. var TextAttr : byte;
  108. \end{verbatim}
  109. The \var{DirectVideo} variable controls the writing to the screen. If it is
  110. \var{True}, the the cursor is set via direct port access. If \var{False},
  111. then the BIOS is used. This is defined under \dos only.
  112. \begin{verbatim}
  113. var DirectVideo : Boolean;
  114. \end{verbatim}
  115. The \var{Lastmode} variable tells you which mode was last selected for the
  116. screen. It is defined on \dos only.
  117. \begin{verbatim}
  118. var lastmode : Word;
  119. \end{verbatim}
  120. % Procedures and functions.
  121. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  122. \section{Procedures and Functions}
  123. \begin{procedure}{AssignCrt}
  124. \Declaration
  125. Procedure AssignCrt (Var F: Text);
  126. \Description
  127. \var{AssignCrt} Assigns a file F to the console. Everything written to
  128. the file F goes to the console instead. If the console contains a window,
  129. everything is written to the window instead.
  130. \Errors
  131. None.
  132. \SeeAlso
  133. \seep{Window}
  134. \end{procedure}
  135. \FPCexample{ex1}
  136. \begin{procedure}{CursorBig}
  137. \Declaration
  138. Procedure CursorBig ;
  139. \Description
  140. Makes the cursor a big rectangle.
  141. Not implemented on \linux.
  142. \Errors
  143. None.
  144. \SeeAlso
  145. \seep{CursorOn}, \seep{CursorOff}
  146. \end{procedure}
  147. \begin{procedure}{ClrEol}
  148. \Declaration
  149. Procedure ClrEol ;
  150. \Description
  151. ClrEol clears the current line, starting from the cursor position, to the
  152. end of the window. The cursor doesn't move
  153. \Errors
  154. None.
  155. \SeeAlso
  156. \seep{DelLine}, \seep{InsLine}, \seep{ClrScr}
  157. \end{procedure}
  158. \FPCexample{ex9}
  159. \begin{procedure}{ClrScr}
  160. \Declaration
  161. Procedure ClrScr ;
  162. \Description
  163. ClrScr clears the current window (using the current colors),
  164. and sets the cursor in the top left
  165. corner of the current window.
  166. \Errors
  167. None.
  168. \SeeAlso
  169. \seep{Window}
  170. \end{procedure}
  171. \FPCexample{ex8}
  172. \begin{procedure}{CursorOff}
  173. \Declaration
  174. Procedure CursorOff ;
  175. \Description
  176. Switches the cursor off (i.e. the cursor is no
  177. longer visible).
  178. Not implemented on \linux.
  179. \Errors
  180. None.
  181. \SeeAlso
  182. \seep{CursorOn}, \seep{CursorBig}
  183. \end{procedure}
  184. \begin{procedure}{CursorOn}
  185. \Declaration
  186. Procedure CursorOn ;
  187. \Description
  188. Switches the cursor on.
  189. Not implemented on \linux.
  190. \Errors
  191. None.
  192. \SeeAlso
  193. \seep{CursorBig}, \seep{CursorOff}
  194. \end{procedure}
  195. \begin{procedure}{Delay}
  196. \Declaration
  197. Procedure Delay (DTime: Word);
  198. \Description
  199. Delay waits a specified number of milliseconds. The number of specified
  200. seconds is an approximation, and may be off a lot, if system load is high.
  201. \Errors
  202. None
  203. \SeeAlso
  204. \seep{Sound}, \seep{NoSound}
  205. \end{procedure}
  206. \FPCexample{ex15}
  207. \begin{procedure}{DelLine}
  208. \Declaration
  209. Procedure DelLine ;
  210. \Description
  211. DelLine removes the current line. Lines following the current line are
  212. scrolled 1 line up, and an empty line is inserted at the bottom of the
  213. current window. The cursor doesn't move.
  214. \Errors
  215. None.
  216. \SeeAlso
  217. \seep{ClrEol}, \seep{InsLine}, \seep{ClrScr}
  218. \end{procedure}
  219. \FPCexample{ex11}
  220. \begin{procedure}{GotoXY}
  221. \Declaration
  222. Procedure GotoXY (X: Byte; Y: Byte);
  223. \Description
  224. Positions the cursor at \var{(X,Y)}, \var{X} in horizontal, \var{Y} in
  225. vertical direction relative to the origin of the current window. The origin
  226. is located at \var{(1,1)}, the upper-left corner of the window.
  227. \Errors
  228. None.
  229. \SeeAlso
  230. \seef{WhereX}, \seef{WhereY}, \seep{Window}
  231. \end{procedure}
  232. \FPCexample{ex6}
  233. \begin{procedure}{HighVideo}
  234. \Declaration
  235. Procedure HighVideo ;
  236. \Description
  237. HighVideo switches the output to highlighted text. (It sets the high
  238. intensity bit of the video attribute)
  239. \Errors
  240. None.
  241. \SeeAlso
  242. \seep{TextColor}, \seep{TextBackground}, \seep{LowVideo},
  243. \seep{NormVideo}
  244. \end{procedure}
  245. \FPCexample{ex14}
  246. \begin{procedure}{InsLine}
  247. \Declaration
  248. Procedure InsLine ;
  249. \Description
  250. InsLine inserts an empty line at the current cursor position.
  251. Lines following the current line are scrolled 1 line down,
  252. causing the last line to disappear from the window.
  253. The cursor doesn't move.
  254. \Errors
  255. None.
  256. \SeeAlso
  257. \seep{ClrEol}, \seep{DelLine}, \seep{ClrScr}
  258. \end{procedure}
  259. \FPCexample{ex10}
  260. \begin{function}{KeyPressed}
  261. \Declaration
  262. Function KeyPressed : Boolean;
  263. \Description
  264. The Keypressed function scans the keyboard buffer and sees if a key has
  265. been pressed. If this is the case, \var{True} is returned. If not,
  266. \var{False} is returned. The \var{Shift, Alt, Ctrl} keys are not reported.
  267. The key is not removed from the buffer, and can hence still be read after
  268. the KeyPressed function has been called.
  269. \Errors
  270. None.
  271. \SeeAlso
  272. \seef{ReadKey}
  273. \end{function}
  274. \FPCexample{ex2}
  275. \begin{procedure}{LowVideo}
  276. \Declaration
  277. Procedure LowVideo ;
  278. \Description
  279. LowVideo switches the output to non-highlighted text. (It clears the high
  280. intensity bit of the video attribute)
  281. \Errors
  282. None.
  283. \SeeAlso
  284. \seep{TextColor}, \seep{TextBackground}, \seep{HighVideo},
  285. \seep{NormVideo}
  286. \end{procedure}
  287. For an example, see \seep{HighVideo}
  288. \begin{procedure}{NormVideo}
  289. \Declaration
  290. Procedure NormVideo ;
  291. \Description
  292. NormVideo switches the output to the defaults, read at startup. (The
  293. defaults are read from the cursor position at startup)
  294. \Errors
  295. None.
  296. \SeeAlso
  297. \seep{TextColor}, \seep{TextBackground}, \seep{LowVideo},
  298. \seep{HighVideo}
  299. \end{procedure}
  300. For an example, see \seep{HighVideo}
  301. \begin{procedure}{NoSound}
  302. \Declaration
  303. Procedure NoSound ;
  304. \Description
  305. Stops the speaker sound.
  306. This call is not supported on all operating systems.
  307. \Errors
  308. None.
  309. \SeeAlso
  310. \seep{Sound}
  311. \end{procedure}
  312. \FPCexample{ex16}
  313. \begin{function}{ReadKey}
  314. \Declaration
  315. Function ReadKey : Char;
  316. \Description
  317. The ReadKey function reads 1 key from the keyboard buffer, and returns this.
  318. If an extended or function key has been pressed, then the zero ASCII code is
  319. returned. You can then read the scan code of the key with a second ReadKey
  320. call.
  321. \textbf{Remark.} Key mappings under Linux can cause the wrong key to be
  322. reported by ReadKey, so caution is needed when using ReadKey.
  323. \Errors
  324. None.
  325. \SeeAlso
  326. \seef{KeyPressed}
  327. \end{function}
  328. \FPCexample{ex3}
  329. \begin{procedure}{Sound}
  330. \Declaration
  331. Procedure Sound (hz : word);
  332. \Description
  333. Sounds the speaker at a frequency of \var{hz}. Under \windows,
  334. a system sound is played and the frequency parameter is ignored.
  335. On other operating systems, this routine may not be implemented.
  336. \Errors
  337. None.
  338. \SeeAlso
  339. \seep{NoSound}
  340. \end{procedure}
  341. \begin{procedure}{TextBackground}
  342. \Declaration
  343. Procedure TextBackground (CL: Byte);
  344. \Description
  345. TextBackground sets the background color to \var{CL}. \var{CL} can be one of the
  346. predefined color constants.
  347. \Errors
  348. None.
  349. \SeeAlso
  350. \seep{TextColor}, \seep{HighVideo}, \seep{LowVideo},
  351. \seep{NormVideo}
  352. \end{procedure}
  353. \FPCexample{ex13}
  354. \begin{procedure}{TextColor}
  355. \Declaration
  356. Procedure TextColor (CL: Byte);
  357. \Description
  358. TextColor sets the foreground color to \var{CL}. \var{CL} can be one of the
  359. predefined color constants.
  360. \Errors
  361. None.
  362. \SeeAlso
  363. \seep{TextBackground}, \seep{HighVideo}, \seep{LowVideo},
  364. \seep{NormVideo}
  365. \end{procedure}
  366. \FPCexample{ex12}
  367. \begin{procedure}{TextMode}
  368. \Declaration
  369. procedure TextMode(Mode: Integer);
  370. \Description
  371. \var{TextMode} sets the textmode of the screen (i.e. the number of lines
  372. and columns of the screen). The lower byte is use to set the VGA text mode.
  373. This procedure is only implemented on \dos.
  374. \Errors
  375. None.
  376. \SeeAlso
  377. \seep{Window}
  378. \end{procedure}
  379. \begin{function}{WhereX}
  380. \Declaration
  381. Function WhereX : Byte;
  382. \Description
  383. WhereX returns the current X-coordinate of the cursor, relative to the
  384. current window. The origin is \var{(1,1)}, in the upper-left corner of the
  385. window.
  386. \Errors
  387. None.
  388. \SeeAlso
  389. \seep{GotoXY}, \seef{WhereY}, \seep{Window}
  390. \end{function}
  391. \FPCexample{ex7}
  392. \begin{function}{WhereY}
  393. \Declaration
  394. Function WhereY : Byte;
  395. \Description
  396. WhereY returns the current Y-coordinate of the cursor, relative to the
  397. current window. The origin is \var{(1,1)}, in the upper-left corner of the
  398. window.
  399. \Errors
  400. None.
  401. \SeeAlso
  402. \seep{GotoXY}, \seef{WhereX}, \seep{Window}
  403. \end{function}
  404. \FPCexample{ex7}
  405. \begin{procedure}{Window}
  406. \Declaration
  407. Procedure Window (X1, Y1, X2, Y2: Byte);
  408. \Description
  409. Window creates a window on the screen, to which output will be sent.
  410. \var{(X1,Y1)} are the coordinates of the upper left corner of the window,
  411. \var{(X2,Y2)} are the coordinates of the bottom right corner of the window.
  412. These coordinates are relative to the entire screen, with the top left
  413. corner equal to \var{(1,1)}
  414. Further coordinate operations, except for the next Window call,
  415. are relative to the window's top left corner.
  416. \Errors
  417. None.
  418. \SeeAlso
  419. \seep{GotoXY}, \seef{WhereX}, \seef{WhereY}, \seep{ClrScr}
  420. \end{procedure}
  421. \FPCexample{ex5}