File: mouse.tex

package info (click to toggle)
fpc 1.9.4-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 63,532 kB
  • ctags: 93,677
  • sloc: pascal: 675,850; makefile: 219,089; xml: 9,242; perl: 7,703; yacc: 3,074; ansic: 2,275; lex: 711; sh: 406; asm: 71; csh: 34; sed: 33; cpp: 26; tcl: 7
file content (353 lines) | stat: -rw-r--r-- 11,368 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
%
%   $Id: mouse.tex,v 1.6 2003/02/24 23:37:53 michael Exp $
%   This file is part of the FPC documentation.
%   Copyright (C) 1997, by Michael Van Canneyt
%
%   The FPC documentation is free text; you can redistribute it and/or
%   modify it under the terms of the GNU Library General Public License as
%   published by the Free Software Foundation; either version 2 of the
%   License, or (at your option) any later version.
%
%   The FPC Documentation is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   Library General Public License for more details.
%
%   You should have received a copy of the GNU Library General Public
%   License along with the FPC documentation; see the file COPYING.LIB.  If not,
%   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
%   Boston, MA 02111-1307, USA.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The Mouse unit
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{The MOUSE unit}
\FPCexampledir{mouseex}
The \var{Mouse} unit implements a platform independent mouse handling 
interface. It is implemented identically on all platforms supported by 
\fpc{} and can be enhanced with custom drivers, should this be needed.
It is intended to be used only in text-based screens, for instance in 
conjunction with the keyboard and video unit. No support for graphical
screens is implemented, and there are (currently) no plans to implement
this.

\section{Constants, Types and Variables}
\subsection{Constants}	
The following constants can be used when mouse drivers need to report 
errors:
\begin{verbatim}
const
  { We have an errorcode base of 1030 }
  errMouseBase                    = 1030;
  errMouseInitError               = errMouseBase + 0;
  errMouseNotImplemented          = errMouseBase + 1;
\end{verbatim}
The following constants describe which action a mouse event describes
\begin{verbatim}
const
  MouseActionDown = $0001;  { Mouse down event }
  MouseActionUp   = $0002;  { Mouse up event }
  MouseActionMove = $0004;  { Mouse move event }
\end{verbatim}
The following constants describe the used buttons in a mouse event:
\begin{verbatim}
  MouseLeftButton   = $01;  { Left mouse button }
  MouseRightButton  = $02;  { Right mouse button }
  MouseMiddleButton = $04;  { Middle mouse button }
\end{verbatim}
The mouse unit has a mechanism to buffer mouse events. The following
constant defines the size of the event buffer:
\begin{verbatim}
MouseEventBufSize = 16;
\end{verbatim}
\subsection{Types}
The \var{TMouseEvent} is the central type of the mouse unit, it is used
to describe the mouse events:
\begin{verbatim}
PMouseEvent=^TMouseEvent;
TMouseEvent=packed record { 8 bytes }
  buttons : word;
  x,y     : word;
  Action  : word;
end;
\end{verbatim}
The \var{Buttons} field describes which buttons were down when the event
occurred. The \var{x,y} fields describe where the event occurred on the
screen. The \var{Action} describes what action was going on when the event
occurred. The \var{Buttons} and \var{Action} field can be examined using the
above constants.

The following record is used to implement a mouse driver in the
\seep{SetMouseDriver} function:
\begin{verbatim}
TMouseDriver = Record 
  UseDefaultQueue : Boolean;
  InitDriver : Procedure;
  DoneDriver : Procedure;
  DetectMouse : Function : Byte;
  ShowMouse : Procedure;
  HideMouse : Procedure;
  GetMouseX : Function : Word;
  GetMouseY : Function : Word;
  GetMouseButtons : Function : Word;
  SetMouseXY : procedure (x,y:word);
  GetMouseEvent : procedure (var MouseEvent:TMouseEvent);
  PollMouseEvent : function (var MouseEvent: TMouseEvent):boolean;
  PutMouseEvent : procedure (Const MouseEvent:TMouseEvent); 
end;
\end{verbatim}
Its fields will be explained in the section on writing a custom driver.

\subsection{Variables}
The following variables are used to keep the current position and state of
the mouse.
\begin{verbatim}
MouseIntFlag : Byte;  { Mouse in int flag }
MouseButtons : Byte;  { Mouse button state }
MouseWhereX,
MouseWhereY  : Word;  { Mouse position }
\end{verbatim}

\section{Functions and procedures}

\begin{function}{DetectMouse}
\Declaration
Function DetectMouse:byte;
\Description
\var{DetectMouse} detects whether a mouse is attached to the system or not.
If there is no mouse, then zero is returned. If a mouse is attached, then
the number of mouse buttons is returned.

This function should be called after the mouse driver was initialized.
\Errors
None.
\SeeAlso
\seep{InitMouse},\seep{DoneMouse},
\end{function}

\FPCexample{ex1}

\begin{procedure}{DoneMouse}
\Declaration
Procedure DoneMouse;
\Description
\var{DoneMouse} De-initializes the mouse driver. It cleans up any memory
allocated when the mouse was initialized, or removes possible mouse hooks
from memory. The mouse functions will not work after \var{DoneMouse} was
called. If \var{DoneMouse} is called a second time, it will exit at once.
\var{InitMouse} should be called before \var{DoneMouse} can be called again.
\Errors
None.
\SeeAlso
\seef{DetectMouse}, \seep{InitMouse}
\end{procedure}

For an example, see most other mouse functions.

\begin{function}{GetMouseButtons}
\Declaration
Function GetMouseButtons:word;
\Description
\var{GetMouseButtons} returns the current button state of the mouse, i.e.
it returns a or-ed combination of the following constants:
\begin{description}
\item[MouseLeftButton] When the left mouse button is held down.
\item[MouseRightButton] When the right mouse button is held down.
\item[MouseMiddleButton] When the middle mouse button is held down.
\end{description}
\Errors
None.
\SeeAlso
\seep{GetMouseEvent}, \seef{GetMouseX}, \seef{GetMouseY}
\end{function}

\FPCexample{ex2}

\begin{procedure}{GetMouseDriver}
\Declaration
Procedure GetMouseDriver(Var Driver : TMouseDriver);
\Description
\var{GetMouseDriver} returns the currently set mouse driver. It can be used
to retrieve the current mouse driver, and override certain callbacks.

A more detailed explanation about getting and setting mouse drivers can be found in
\sees{mousedrv}.

\Errors
None.
\SeeAlso
\seep{SetMouseDriver}
\end{procedure}

For an example, see the section on writing a custom mouse driver,
\sees{mousedrv}

\begin{procedure}{GetMouseEvent}
\Declaration
Procedure GetMouseEvent(var MouseEvent:TMouseEvent);
\Description
\var{GetMouseEvent} returns the next mouse event (a movement, button press or
button release), and waits for one if none is available in the queue.

Some mouse drivers can implement a mouse event queue which can hold multiple
events till they are fetched.; Others don't, and in that case, a one-event
queue is implemented for use with \seef{PollMouseEvent}.
\Errors
None.
\SeeAlso
\seef{GetMouseButtons}, \seef{GetMouseX}, \seef{GetMouseY}
\end{procedure}

\begin{function}{GetMouseX}
\Declaration
Function GetMouseX:word;
\Description
\var{GetMouseX} returns the current \var{X} position of the mouse. \var{X} is
measured in characters, starting at 0 for the left side of the screen.
\Errors
None.
\SeeAlso
\seef{GetMouseButtons},\seep{GetMouseEvent}, \seef{GetMouseY}
\end{function}

\FPCexample{ex4}

\begin{function}{GetMouseY}
\Declaration
Function GetMouseY:word; 
\Description
\var{GetMouseY} returns the current \var{Y} position of the mouse. \var{Y} is
measured in characters, starting at 0 for the top of the screen.
\Errors
None.
\SeeAlso
\seef{GetMouseButtons},\seep{GetMouseEvent}, \seef{GetMouseX}
\end{function}

For an example, see \seef{GetMouseX}

\begin{procedure}{HideMouse}
\Declaration
Procedure HideMouse;
\Description
\var{HideMouse} hides the mouse cursor. This may or may not be implemented
on all systems, and depends on the driver.
\Errors
None.
\SeeAlso
\seep{ShowMouse}
\end{procedure}

\FPCexample{ex5}

\begin{procedure}{InitMouse}
\Declaration
Procedure InitMouse;
\Description
\var{InitMouse} Initializes the mouse driver. This will allocate any data
structures needed for the mouse to function. All mouse functions can be
used after a call to \var{InitMouse}.

A call to \var{InitMouse} must always be followed by a call to \seep{DoneMouse}
at program exit. Failing to do so may leave the mouse in an unusable state,
or may result in memory leaks.
\Errors
None.
\SeeAlso
\seep{DoneMouse}, \seef{DetectMouse}
\end{procedure}

For an example, see most other functions.

\begin{function}{PollMouseEvent}
\Declaration
Function PollMouseEvent(var MouseEvent: TMouseEvent):boolean; 
\Description
\var{PollMouseEvent} checks whether a mouse event is available, and 
returns it in \var{MouseEvent} if one is found. The function result is
\var{True} in that case. If no mouse event is pending, the function result
is \var{False}, and the contents of \var{MouseEvent} is undefined.

Note that after a call to \var{PollMouseEvent}, the event should still 
be removed from the mouse event queue with a call to \var{GetMouseEvent}.
\Errors
None.
\SeeAlso
\seep{GetMouseEvent}, \seep{PutMouseEvent}
\end{function}

\begin{procedure}{PutMouseEvent}
\Declaration
Procedure PutMouseEvent(const MouseEvent: TMouseEvent);
\Description
\var{PutMouseEvent} adds \var{MouseEvent} to the input queue. The next
call to \seep{GetMouseEvent} or \var{PollMouseEvent} will then return
\var{MouseEvent}. 

Please note that depending on the implementation the mouse event queue
can hold only one value.
\Errors
None.
\SeeAlso
\seep{GetMouseEvent}, \seef{PollMouseEvent}
\end{procedure}

\begin{procedure}{SetMouseDriver}
\Declaration
Procedure SetMouseDriver(Const Driver : TMouseDriver);
\Description
\var{SetMouseDriver} sets the mouse driver to \var{Driver}. This function
should be called before \seep{InitMouse} is called, or after \var{DoneMouse}
is called. If it is called after the mouse has been initialized, it does
nothing.

For more information on setting the mouse driver, \sees{mousedrv}.
\Errors

\SeeAlso
\seep{InitMouse}, \seep{DoneMouse}, \seep{GetMouseDriver}
\end{procedure}

For an example, see \sees{mousedrv}

\begin{procedure}{SetMouseXY}
\Declaration
Procedure SetMouseXY(x,y:word); 
\Description
\var{SetMouseXY} places the mouse cursor on \var{X,Y}. X and Y are zero
based character coordinates: \var{0,0} is the top-left corner of the screen,
and the position is in character cells (i.e. not in pixels).
\Errors
None.
\SeeAlso
\seef{GetMouseX}, \seef{GetMouseY}
\end{procedure}

\FPCexample{ex7}

\begin{procedure}{ShowMouse}
\Declaration
Procedure ShowMouse; 
\Description
\var{ShowMouse} shows the mouse cursor if it was previously hidden. The
capability to hide or show the mouse cursor depends on the driver.
\Errors
None.
\SeeAlso
\seep{HideMouse}
\end{procedure}

For an example, see \seep{HideMouse}

\section{Writing a custom mouse driver}
\label{se:mousedrv}
The \file{mouse} has support for adding a custom mouse driver. This can be
used to add support for mouses not supported by the standard \fpc{} driver,
but also to enhance an existing driver for instance to log mouse events or
to implement a record and playback function. 

The following unit shows how a mouse driver can be enhanced by adding some
logging capabilities to the driver.
\FPCexample{logmouse}