|
Example of C++ Code
The following example shows the typical structure of the software that
I write, and the level of commenting present:
// ******************************************************
// *** CURSOR ***
// ******************************************************
void TGCL_System::MoveCursor( const Rcoord& rNewCursorPos, uint flags )
{
// given a new real coordinate, updates GDI and TGS cursors
Rcoord rOldCursorPos;
char num_string[CUR_FIELDWIDTH+1];
char string[128];
HDC hDC; // temp DC
POINT pt; // temp point
Rcoord rxy;
Acoord axy;
LUcoord luxy;
LUcoord luWndTL; // window boundary - lu
LUcoord luWndBR;
XYcoord dluxy; // lu as double for range
XYcoord dluCursorPos; // lu as double for range
double rc_prop;
LUcoord luViewPos; // current view pos
bool on_dragpoint;
MSG msg;
rOldCursorPos = rCursorPos; // to test for a change
rCursorPos = rNewCursorPos; // save new position
// perform snap operation
if( CursorSnap_rx != 0. )
rCursorPos.x = round( rCursorPos.x / CursorSnap_rx, 0 ) *
CursorSnap_rx;
if( CursorSnap_ry != 0. )
rCursorPos.y = round( rCursorPos.y / CursorSnap_ry, 0 ) *
CursorSnap_ry;
if( flags & CUR_AUTOSCROLL )
{
// get window dimensions as logical units
// we must do this each time as the window may have scrolled
// get device context for paint window
hDC = GetDC( hWndPaint );
if( !hDC ) // unable to get device context
_Sys_Error( SE_WINRESOURCE, 0xF329 );
// set current window origin
SetWindowOrg( hDC, luWindowOrg.x, luWindowOrg.y );
GetClientRect( hWndPaint, &rect );
if( !DPtoLP( hDC, (LPPOINT) &rect, 2 ) ) // -> logical units
_Int_Error( 0xF330 ); // conversion failed
if( !ReleaseDC( hWndPaint, hDC ) )
_Sys_Error( SE_WINRESOURCE, 0xF331 );
// unable to release device context
luWndTL.x = rect.left;
luWndTL.y = rect.top;
luWndBR.x = rect.right;
luWndBR.y = rect.bottom;
if( flags & CUR_MOUSESCROLL )
{
// has the mouse moved the GDI cursor outside or to the edge of
// the window ?
luxy = RealToLu( rNewCursorPos );
outside_left = ( luxy.x <= luWndTL.x );
outside_right = ( luxy.x >= luWndBR.x );
outside_top = ( luxy.y <= luWndTL.y );
outside_bottom = ( luxy.y >= luWndBR.y );
// determine if real grid has zero net rotation
angle_r = RotationAngle_r - ViewRotation_r;
while( angle_r > +PI ) // limit range to +/-PI
angle_r -= 2.*PI; // for zero testing
while( angle_r < -PI )
angle_r += 2.*PI;
zero_rotation = ( fabs( angle_r ) < ALMOST_ZERO );
|
|