Tk Source Code

Attachment Details
Login
Overview

Artifact ID: 221bc4e30c21be22fb227b3c6468fe080b3a96a7
Ticket: 1187074fffffffffffffffffffffffffffffffff
Date: 2005-04-21 11:01:47
User: hobbs
Artifact Attached: 79730caa5d5d3ec90863d4c4e8e190a4caccc7e6
Filename:tkWinCE.c
Description:CE arc function code
Content:
     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
   354
   355
   356
   357
   358
   359
   360
   361
   362
   363
   364
   365
   366
   367
   368
   369
   370
   371
   372
   373
// tkWinCE.c
//
// Time-stamp: <20/03/02 08:40:26 [email protected]>

#include "tkWinInt.h"
#include <stdio.h>
#include <celib.h>
#include <time.h>

//////////////////////////////////////////////////////////////////////
// Emulate CreateDIBitmap

HBITMAP 
CreateDIBitmap(HDC  hdc,
	BITMAPINFOHEADER *lpbmih,
	DWORD  fdwInit,
	VOID *lpBits,
	BITMAPINFO *lpBitsInfo,
	UINT  iUsage
    )
{
    HBITMAP hbitmap;
    LPBYTE lpDestBits;
    DWORD dwLen;
    LPBITMAPINFOHEADER lpH;
    int nPadWidth = 4;
    int pad = 0;
    DWORD dwError;
    DWORD i;
    BYTE *d;
    WORD *s;
    WORD *s0;
    int orig_bitcount;
    XImage *image = (XImage *)lpBits;
    lpBits = image->data;

    lpH = lpbmih;

    if (lpH->biWidth % nPadWidth) {
	pad = nPadWidth - lpH->biWidth % nPadWidth;
    }

    // there are problems with padding. i dont know the rules
    // anymore...
    pad = 0;

    // This is wrong when the infoheader is followed by color data...
    // We hope that biSizeImage is set then...
    dwLen = ((lpH->biWidth+pad) * abs(lpH->biHeight) * lpH->biBitCount) / 8;

    // includes colordata, if any...
    if (lpH->biSizeImage != 0) {
	dwLen = lpH->biSizeImage;
    }

    orig_bitcount = lpH->biBitCount;

    if (lpH->biBitCount == 16) {
	lpH->biBitCount = 24;
    }

    hbitmap = CreateDIBSection(hdc, lpBitsInfo, iUsage, &lpDestBits, NULL, 0);
    if (hbitmap != NULL) {
	if (orig_bitcount == 16) {
	    if (lpH->biCompression == BI_RGB) {
		s = (WORD *) lpBits;
		d = (BYTE *) lpDestBits;
		s0 = (WORD *) lpDestBits;
		// There is a bug in this code when padding was used!

		// how do you get the full color range from 5 bits???
		// shifting left seems to be ok...
#if 1
		dwLen = dwLen >> 1;
		for (i = 0; i < dwLen; i++) {
		    *d++ = (((*s) >> 0) & 0x1F) << 3;
		    *d++ = (((*s) >> 5) & 0x1F) << 3;
		    *d++ = (((*s) >> 10) & 0x1F) << 3;
		    s++;
		}
#endif
	    } else {
		XCEShowMessageA("Cannot create bitmap: wrong number of colors");
		//DebugBreak();
		return (HBITMAP) GDI_ERROR;
	    }
	} else {
	    memcpy(lpDestBits, lpBits, dwLen);
	}

	return hbitmap;
    }

    dwError = GetLastError();

    XCEShowMessageA("Cannot create bitmap: %d", dwError);
    //DebugBreak();

    return (HBITMAP) GDI_ERROR;
}

//////////////////////////////////////////////////////////////////////
#if CE_OLD
static int cursor_x, cursor_y;

// Dummy... How emulate correctly...

// The original function on ARM/HPC/WCE300 also returns error 120:
// unsupported function!

BOOL
WinCEGetCursorPos(POINT *p)
{
    p->x = cursor_x;
    p->y = cursor_y;

    return TRUE;
}

BOOL
WinCESetCursorPos(int x, int y)
{
    cursor_x = x;
    cursor_y = y;

    return TRUE;
}
#endif
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////

/*
 *  Arc, Chord and Pie compatability functions for CE
 *  http://www.experts-exchange.com/Programming/Programming_Platforms/Win_Prog/Q_11141686.html
 */

BOOL CeArc(
    HDC hdc,         // handle to device context
    int nLeftRect,   // x-coord of upper-left corner of rectangle
    int nTopRect,    // y-coord of upper-left corner of rectangle
    int nRightRect,  // x-coord of lower-right corner of rectangle
    int nBottomRect, // y-coord of lower-right corner of rectangle
    int nXStartArc,  // x-coord of first radial's endpoint
    int nYStartArc,  // y-coord of first radial's endpoint
    int nXEndArc,    // x-coord of second radial's endpoint
    int nYEndArc     // y-coord of second radial's endpoint
    );

BOOL CeChord(
    HDC hdc,         // handle to device context
    int nLeftRect,   // x-coord of upper-left corner of rectangle
    int nTopRect,    // y-coord of upper-left corner of rectangle
    int nRightRect,  // x-coord of lower-right corner of rectangle
    int nBottomRect, // y-coord of lower-right corner of rectangle
    int nXRadial1,   // x-coord of first radial's endpoint
    int nYRadial1,   // y-coord of first radial's endpoint
    int nXRadial2,   // x-coord of second radial's endpoint
    int nYRadial2    // y-coord of second radial's endpoint
    );

BOOL CePie(
    HDC hdc,         // handle to device context
    int nLeftRect,   // x-coord of upper-left corner of rectangle
    int nTopRect,    // y-coord of upper-left corner of rectangle
    int nRightRect,  // x-coord of lower-right corner of rectangle
    int nBottomRect, // y-coord of lower-right corner of rectangle
    int nXRadial1,   // x-coord of first radial's endpoint
    int nYRadial1,   // y-coord of first radial's endpoint
    int nXRadial2,   // x-coord of second radial's endpoint
    int nYRadial2    // y-coord of second radial's endpoint
    );

static LPPOINT
GetArcPoints(
    int nLeftRect,   // [IN] x-coord of upper-left corner of rectangle
    int nTopRect,    // [IN] y-coord of upper-left corner of rectangle
    int nRightRect,  // [IN] x-coord of lower-right corner of rectangle
    int nBottomRect, // [IN] y-coord of lower-right corner of rectangle
    int nXStartArc,  // [IN] x-coord of first radial's endpoint
    int nYStartArc,  // [IN] y-coord of first radial's endpoint
    int nXEndArc,    // [IN] x-coord of second radial's endpoint
    int nYEndArc,    // [IN] y-coord of second radial's endpoint
    int *Points     // [IN] zero: Arc, Chord; nonzero: Pie. [OUT] # of points
    )
{
    // The drawing direction is always counterclockwise.
    int nPoints = *Points;

    // drawing a pie?
    const BOOL bPie = (nPoints != 0);

    // PI
    const double PI = 3.14159265358979;

    // delta angle
    const double dDeltaAngle = 0.01;

    // rectangle center
    const int nCX = (nLeftRect + nRightRect) / 2,
	nCY = (nTopRect + nBottomRect) / 2;

    // ellipse radius 
    const int nRX = abs(nLeftRect - nRightRect) / 2,
	nRY = abs(nTopRect - nBottomRect) / 2;

    // array of points
    LPPOINT lppt;

    // begin and end angles
    double dBeginAngle, dEndAngle, dCurAngle;

    int ni;

    if (nRX == 0 || nRY == 0)
	return NULL;

    if (nYStartArc == nCY) {
	dBeginAngle = nXStartArc < nCX ? PI : 0.0;
    } else {
	// -PI < dBeginAngle && dBeginAngle < PI
	dBeginAngle = atan2((nCY - nYStartArc) * nRX / nRY, nXStartArc - nCX);
    }

    if (nYEndArc == nCY) {
	dEndAngle = nXEndArc < nCX ? PI : 0.0;
    } else {
	// -PI < dEndAngle && dEndAngle < PI
	dEndAngle = atan2((nCY - nYEndArc) * nRX / nRY, nXEndArc - nCX);
    }

    // normalization
    if (dBeginAngle >= dEndAngle) {
	dEndAngle += PI * 2.0;
    }

    ASSERT(dBeginAngle < dEndAngle);

    // number of points
    nPoints = (int) ((dEndAngle - dBeginAngle) / dDeltaAngle) + 1;
    *Points = nPoints;

    if (nPoints < 2)
	return NULL;

    // array of points
    lppt = (LPPOINT) malloc((nPoints + (bPie ? 1 : 0)) * sizeof(POINT));

    if (lppt == NULL)
	return NULL;

    dCurAngle = dBeginAngle;
    for (ni = 0; ni < nPoints; ni++) {
	lppt[ni].x = (int) (nRX * cos(dCurAngle)) + nCX;
	lppt[ni].y = (int) (-nRY * sin(dCurAngle)) + nCY;

	dCurAngle += dDeltaAngle;
    }

    if (bPie) {
	// a pie's last point is the center.
	lppt[nPoints].x = nCX;
	lppt[nPoints].y = nCY;
	*Points = nPoints + 1;
    }

    return lppt;
}

static void ReleaseArcPoints(LPPOINT lppt)
{
    if (lppt != NULL) {
	free(lppt);
    }
}

BOOL CeArc(
    HDC hdc,         // handle to device context
    int nLeftRect,   // x-coord of upper-left corner of rectangle
    int nTopRect,    // y-coord of upper-left corner of rectangle
    int nRightRect,  // x-coord of lower-right corner of rectangle
    int nBottomRect, // y-coord of lower-right corner of rectangle
    int nXStartArc,  // x-coord of first radial's endpoint
    int nYStartArc,  // y-coord of first radial's endpoint
    int nXEndArc,    // x-coord of second radial's endpoint
    int nYEndArc     // y-coord of second radial's endpoint
    )
{
    int nPoints = 0;  // Arc
    const LPPOINT lppt = GetArcPoints(nLeftRect, nTopRect,
	    nRightRect, nBottomRect,
	    nXStartArc, nYStartArc,
	    nXEndArc, nYEndArc,
	    &nPoints);

    if (lppt != NULL) {
	//ASSERT(nPoints >= 2);

	// drawing 
	const BOOL bRet = Polyline(hdc, lppt, nPoints);

	ReleaseArcPoints(lppt);

	return bRet;
    } else {
	return FALSE;
    }
}

BOOL CeChord(
    HDC hdc,         // handle to device context
    int nLeftRect,   // x-coord of upper-left corner of rectangle
    int nTopRect,    // y-coord of upper-left corner of rectangle
    int nRightRect,  // x-coord of lower-right corner of rectangle
    int nBottomRect, // y-coord of lower-right corner of rectangle
    int nXRadial1,   // x-coord of first radial's endpoint
    int nYRadial1,   // y-coord of first radial's endpoint
    int nXRadial2,   // x-coord of second radial's endpoint
    int nYRadial2    // y-coord of second radial's endpoint
    )
{
    int nPoints = 0;  // Chord
    const LPPOINT lppt = GetArcPoints(nLeftRect, nTopRect,
	    nRightRect, nBottomRect,
	    nXRadial1, nYRadial1,
	    nXRadial2, nYRadial2,
	    &nPoints);

    if (lppt != NULL) {
	//ASSERT(nPoints >= 2);

	// drawing 
	const BOOL bRet = Polygon(hdc, lppt, nPoints);

	ReleaseArcPoints(lppt);

	return bRet;
    } else {
	return FALSE;
    }
}

BOOL CePie(
    HDC hdc,         // handle to device context
    int nLeftRect,   // x-coord of upper-left corner of rectangle
    int nTopRect,    // y-coord of upper-left corner of rectangle
    int nRightRect,  // x-coord of lower-right corner of rectangle
    int nBottomRect, // y-coord of lower-right corner of rectangle
    int nXRadial1,   // x-coord of first radial's endpoint
    int nYRadial1,   // y-coord of first radial's endpoint
    int nXRadial2,   // x-coord of second radial's endpoint
    int nYRadial2    // y-coord of second radial's endpoint
    )
{
    int nPoints = 1;  // Pie
    const LPPOINT lppt = GetArcPoints(nLeftRect, nTopRect,
	    nRightRect, nBottomRect,
	    nXRadial1, nYRadial1,
	    nXRadial2, nYRadial2,
	    &nPoints);

    if (lppt != NULL) {
	//ASSERT(nPoints >= 2);

	// drawing 
	const BOOL bRet = Polygon(hdc, lppt, nPoints);

	ReleaseArcPoints(lppt);

	return bRet;
    } else {
	return FALSE;
    }
}