summaryrefslogtreecommitdiff
path: root/gs/psi/dwtrace.c
blob: acb5b1391b955b71ab48a629480b4ecb4bca9c25 (plain)
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
/* Copyright (C) 2001-2012 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
   CA  94903, U.S.A., +1(415)492-9861, for further information.
*/


/* Graphical trace server for Windows */

/*  This module use Win32-specific API.
    For 16-bit Windows many of functions compile to stubs.
*/

/*  fixme : Restoring image on WM_PAINT is NOT implemented yet.
*/

#define STRICT
#include <windows.h>
#include <math.h>
#include "dwimg.h"

static COLORREF WindowsColor(unsigned long c)
{   /*  This body uses a Windows specific macro RGB, which is being
        redefined in GS include files. Please include them after
        this definition.
    */
    return RGB(c >> 16, (c >> 8) & 255, c & 255);
}

#include "gscdefs.h"
#include "stdpre.h"
#include "gsdll.h"
#include "vdtrace.h"
#include "dwtrace.h"

struct vd_trace_host_s {
    bool inited;
    IMAGE * tw;
    HDC hdc;
    int count_GetDC;
    int window_height;
    int line_width;
    COLORREF color;
    HPEN pen, pen0;
    HBRUSH brush, brush0;
    double bx, by;
};

static struct vd_trace_host_s host = {false, NULL, NULL};
vd_trace_interface visual_tracer = { &host, 1, 1, 0, 0, 0, 0 };
static const char *vdtrace_ini = "gs_vdtrace.ini";

static void get_window()
{   if (!host.inited) {
        host.tw = image_new(NULL, NULL);	/* create and add to list */
        if (host.tw) {
            image_open(host.tw);
            UpdateWindow(host.tw->hwnd);
        }
        host.hdc = NULL;
        host.count_GetDC = 0;
        host.window_height = 100;
        host.line_width = 1;
        host.color = 0;
        host.inited = true;
    }
}

static inline int ScaleX(struct vd_trace_host_s *h, double x)
{   return (int)(x + 0.5);
}

static inline int ScaleY(struct vd_trace_host_s *h, double y)
{   return h->window_height - (int)(y + 0.5);
}

#define SX(x) ScaleX(I->host,x)
#define SY(y) ScaleY(I->host,y)

static inline void delete_pen_brush(vd_trace_interface *I)
{   SelectObject(I->host->hdc, I->host->pen0);
    SelectObject(I->host->hdc, I->host->brush0);
    if(I->host->pen != NULL)
        DeleteObject(I->host->pen);
    I->host->pen = NULL;
    if(I->host->brush != NULL)
        DeleteObject(I->host->brush);
    I->host->brush = NULL;
}

static inline void new_pen_brush(vd_trace_interface *I)
{   delete_pen_brush(I);
    I->host->pen = CreatePen(PS_SOLID, I->host->line_width, WindowsColor(I->host->color));
    I->host->brush = CreateSolidBrush(WindowsColor(I->host->color));
    SelectObject(I->host->hdc, I->host->pen);
    SelectObject(I->host->hdc, I->host->brush);
}

static double dw_gt_get_size_x(vd_trace_interface *I)
{   RECT r;
    get_window();
    if (host.tw == NULL)
        return(100);
    GetClientRect(I->host->tw->hwnd,&r);
    return r.right - r.left;
}

static double dw_gt_get_size_y(vd_trace_interface *I)
{   RECT r;
    get_window();
    if (host.tw == NULL)
        return(100);
    GetClientRect(I->host->tw->hwnd,&r);
    return r.bottom - r.top;
}

static void dw_gt_get_dc(vd_trace_interface *I, vd_trace_interface **I1)
{   get_window();
    if (host.tw == NULL)
        return;
    if (I->host->hdc == NULL) {
        RECT r;
        I->host->hdc = GetDC(I->host->tw->hwnd);
        SetMapMode(I->host->hdc,MM_TEXT);
        GetClientRect(I->host->tw->hwnd, &r);
        I->host->window_height = r.bottom;
        SetBkMode(I->host->hdc,TRANSPARENT);
        I->host->pen0 = (HPEN)SelectObject(I->host->hdc, GetStockObject(BLACK_PEN));
        I->host->brush0 = (HBRUSH)SelectObject(I->host->hdc, GetStockObject(BLACK_BRUSH));
        I->host->color = 0;
        I->host->count_GetDC = 1;
        *I1 = I;
    } else
        ++I->host->count_GetDC;
}

static void dw_gt_release_dc(vd_trace_interface *I, vd_trace_interface **I1)
{   get_window();
    if (host.tw == NULL)
        return;
    --I->host->count_GetDC;
    if(I->host->count_GetDC == 0) {
        ReleaseDC(I->host->tw->hwnd, I->host->hdc);
        I->host->hdc = NULL;
        delete_pen_brush(I);
        *I1 = NULL;
    } else if(I->host->count_GetDC < 0) {
        /* safety : */
        I->host->count_GetDC = 0;
        *I1 = NULL;
    }
}

static void dw_gt_erase(vd_trace_interface *I, unsigned long rgbcolor)
{   HWND hwnd;
    RECT r;
    HBRUSH hbr;
    get_window();
    if (host.tw == NULL)
        return;
    hwnd = I->host->tw->hwnd;
    GetClientRect(hwnd, &r);
    hbr = CreateSolidBrush(rgbcolor);
    FillRect(I->host->hdc, &r, hbr);
    DeleteObject(hbr);
}

static void dw_gt_beg_path(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    BeginPath(I->host->hdc);
}

static void dw_gt_end_path(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    EndPath(I->host->hdc);
}

static void dw_gt_moveto(vd_trace_interface *I, double x, double y)
{   POINT p;
    get_window();
    if (host.tw == NULL)
        return;
#ifdef __WIN32__
    MoveToEx(I->host->hdc, SX(x), SY(y), &p);
#else
    MoveTo(I->host->hdc, SX(x), SY(y));
#endif
    I->host->bx = x; I->host->by = y;
}

static void dw_gt_lineto(vd_trace_interface *I, double x, double y)
{   get_window();
    if (host.tw == NULL)
        return;
    LineTo(I->host->hdc, SX(x), SY(y));
}

static void dw_gt_curveto(vd_trace_interface *I, double x0, double y0, double x1, double y1, double x2, double y2)
{   POINT p[3];
    get_window();
    if (host.tw == NULL)
        return;
    p[0].x = SX(x0), p[0].y = SY(y0);
    p[1].x = SX(x1), p[1].y = SY(y1);
    p[2].x = SX(x2), p[2].y = SY(y2);
    PolyBezierTo(I->host->hdc, p, 3);
}

static void dw_gt_closepath(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    LineTo(I->host->hdc, SX(I->host->bx), SY(I->host->by));
    CloseFigure(I->host->hdc);
}

static void dw_gt_circle(vd_trace_interface *I, double x, double y, int r)
{   HBRUSH h;
    get_window();
    if (host.tw == NULL)
        return;
    h = (HBRUSH)SelectObject(I->host->hdc, GetStockObject(NULL_BRUSH));
    Ellipse(I->host->hdc, SX(x)-r, SY(y)-r, SX(x)+r, SY(y)+r);
    SelectObject(I->host->hdc, h);
}

static void dw_gt_round(vd_trace_interface *I, double x, double y, int r)
{   HPEN h;
    get_window();
    if (host.tw == NULL)
        return;
    h = (HPEN)SelectObject(I->host->hdc, GetStockObject(NULL_PEN));
    Ellipse(I->host->hdc, SX(x)-r, SY(y)-r, SX(x)+r, SY(y)+r);
    SelectObject(I->host->hdc, h);
}

static void dw_gt_pixel(vd_trace_interface *I, double x, double y, unsigned long rgbcolor)
{   get_window();
    if (host.tw == NULL)
        return;
    SetPixel(I->host->hdc, SX(x), SY(y), rgbcolor);
}

static void dw_gt_fill(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    FillPath(I->host->hdc);
}

static void dw_gt_stroke(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    StrokePath(I->host->hdc);
}

static void dw_gt_setcolor(vd_trace_interface *I, unsigned long rgbcolor)
{   get_window();
    if (host.tw == NULL)
        return;
    if (I->host->color != rgbcolor) {
        I->host->color = rgbcolor;
        new_pen_brush(I);
        SetTextColor(I->host->hdc, rgbcolor);
    }
}

static void dw_gt_setlinewidth(vd_trace_interface *I, unsigned int width)
{   get_window();
    if (host.tw == NULL)
        return;
    if (I->host->line_width != width) {
        I->host->line_width = width;
        new_pen_brush(I);
    }
}

static void dw_gt_text(vd_trace_interface *I, double x, double y, char *ASCIIZ)
{   get_window();
    if (host.tw == NULL)
        return;
    TextOut(I->host->hdc, SX(x), SY(y), ASCIIZ, strlen(ASCIIZ));
}

static void dw_gt_wait(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    /* fixme : not implemented yet. */
}

static void dw_gt_set_scale(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    I->scale_x *= GetPrivateProfileInt("VDTRACE", "ScaleX", 1000, vdtrace_ini) / 1000.0;
    I->scale_y *= GetPrivateProfileInt("VDTRACE", "ScaleY", 1000, vdtrace_ini) / 1000.0;
}

static void dw_gt_set_shift(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    I->shift_x += (int)GetPrivateProfileInt("VDTRACE", "ShiftX", 0, vdtrace_ini);
    I->shift_y += (int)GetPrivateProfileInt("VDTRACE", "ShiftY", 0, vdtrace_ini);
}

static void dw_gt_set_origin(vd_trace_interface *I)
{   get_window();
    if (host.tw == NULL)
        return;
    I->orig_x += (int)GetPrivateProfileInt("VDTRACE", "OrigX", 0, vdtrace_ini);
    I->orig_y += (int)GetPrivateProfileInt("VDTRACE", "OrigY", 0, vdtrace_ini);
}

#ifdef __WIN32__
#    define SET_CALLBACK(I,a) I.a = dw_gt_##a
#else
#    define SET_CALLBACK(I,a) I.a = 0
#endif

vd_trace_interface *visual_tracer_init(void)
{   SET_CALLBACK(visual_tracer, get_dc);
    SET_CALLBACK(visual_tracer, release_dc);
    SET_CALLBACK(visual_tracer, erase);
    SET_CALLBACK(visual_tracer, get_size_x);
    SET_CALLBACK(visual_tracer, get_size_y);
    SET_CALLBACK(visual_tracer, erase);
    SET_CALLBACK(visual_tracer, beg_path);
    SET_CALLBACK(visual_tracer, end_path);
    SET_CALLBACK(visual_tracer, moveto);
    SET_CALLBACK(visual_tracer, lineto);
    SET_CALLBACK(visual_tracer, curveto); /* optional */
    SET_CALLBACK(visual_tracer, closepath);
    SET_CALLBACK(visual_tracer, circle);
    SET_CALLBACK(visual_tracer, round);
    SET_CALLBACK(visual_tracer, pixel);
    SET_CALLBACK(visual_tracer, fill);
    SET_CALLBACK(visual_tracer, stroke);
    SET_CALLBACK(visual_tracer, setcolor);
    SET_CALLBACK(visual_tracer, setlinewidth);
    SET_CALLBACK(visual_tracer, text);
    SET_CALLBACK(visual_tracer, wait);
    SET_CALLBACK(visual_tracer, set_scale);
    SET_CALLBACK(visual_tracer, set_shift);
    SET_CALLBACK(visual_tracer, set_origin);
    return &visual_tracer;
}

void visual_tracer_close(void)
{   if (host.tw != NULL) {
        image_delete(host.tw);
        image_close(host.tw);
    }
}