In this section:
Consider the task of writing a simple paint program. Such programs often allow the user to hold down the left mouse key in order to open up a box that is used to select an area. Alternatively, the effect of dragging the mouse could be to create a line rather than a box.
%gr drawing surfaces have an associated 'selection mode'. This mode tells ClearWin+ what to do if the mouse is dragged (by the left button) over the area. The mode can be set using the BOX_SELECTION, LINE_SELECTION or FREE_SELECTION options on the %gr (Graphics Region) format code. The mode can also be changed dynamically using the SET_GRAPHICS_SELECTION@ function. This operates on the current drawing surface. The argument to this function takes the following values:
0 |
default - no selection |
1 |
box selection |
2 |
line selection |
4 |
invisible box selection |
When a selection is enabled, dragging the mouse with the left button depressed opens an XOR drawn box or line. A program obtains the co-ordinates using GET_GRAPHICS_SELECTED_AREA@. Typically you would call this function on the release of the left mouse (or possibly in response to a button press) so that the program would respond to the final position of the line or box.
The following example uses BOX_SELECTION. When the left mouse button is released, the program draws a diagonal line using the box position and dimensions. If the shift key is held down, a copy of the box is produced instead of a line. Alternatively, if the control key is held down, an ellipse is drawn to fill the box.
WINAPP
INTEGER i,winio@
CHARACTER*80 fmt
EXTERNAL gr_func
i=winio@('%ww[no_border]%ca[Box Selection]%sy[3d_depressed]&')
fmt='%^gr[black,box_selection,full_mouse_input,rgb_colours]'
i=winio@(fmt,200,200,gr_func)
END
INTEGER FUNCTION gr_func()
INCLUDE <windows.ins>
INTEGER x1,y1,x2,y2,a,b,flags,grey
LOGICAL IsUp,WasDown
DATA WasDown/.FALSE./
flags=clearwin_info@('graphics_mouse_flags')
IsUp=AND(flags,MK_LBUTTON).EQ.0
IF(IsUp.AND.WasDown)THEN
grey=RGB@(170,170,170)
CALL get_graphics_selected_area@(x1,y1,x2,y2)
CALL set_graphics_selection@(0)
IF(AND(flags,MK_SHIFT).EQ.MK_SHIFT)THEN
CALL draw_filled_rectangle@(x1,y1,x2,y2,grey)
ELSEIF(AND(flags,MK_CONTROL).EQ.MK_CONTROL)THEN
a=0.5*(x2-x1)
b=0.5*(y2-y1)
CALL draw_filled_ellipse@(x1+a,y1+b,a,b,grey)
ELSE
CALL draw_line_between@(x1,y1,x2,y2,grey)
ENDIF
CALL set_graphics_selection@(1)
ENDIF
WasDown=.NOT.IsUp
gr_func=1
END