File: dwimg.cpp

package info (click to toggle)
gs 5.10-3.99.slink.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 14,992 kB
  • ctags: 25,337
  • sloc: ansic: 163,794; makefile: 3,156; cpp: 2,237; sh: 1,030; asm: 684; tcl: 434; perl: 56
file content (415 lines) | stat: -rw-r--r-- 10,094 bytes parent folder | download | duplicates (3)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* Copyright (C) 1996, Russell Lang.  All rights reserved.
  
  This file is part of Aladdin Ghostscript.
  
  Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  License (the "License") for full details.
  
  Every copy of Aladdin Ghostscript must include a copy of the License,
  normally in a plain ASCII text file named PUBLIC.  The License grants you
  the right to copy, modify and redistribute Aladdin Ghostscript, but only
  under certain conditions described in the License.  Among other things, the
  License requires that the copyright notice and this notice be preserved on
  all copies.
*/

// dwimg.cpp


#define STRICT
#include <windows.h>
#include <shellapi.h>
extern "C" {
#include "gsdll.h"
}
#include "dwmain.h"
#include "dwdll.h"
#include "dwimg.h"

extern gsdll_class gsdll;

char szImgName[] = "Ghostscript Image";
#define M_COPY_CLIP 1
#if defined(_MSC_VER) && defined(__WIN32__)
#define _export
#else
#define max(a,b) ( (a>b) ? a : b )
#define min(a,b) ( (a<b) ? a : b )
#endif

// Forward references
LRESULT CALLBACK _export WndImgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

HINSTANCE ImageWindow::hInstance;
HWND ImageWindow::hwndtext;
ImageWindow *ImageWindow::first;


// friend
ImageWindow *
FindImageWindow(char FAR *dev)
{
    ImageWindow *iw;
    for (iw = ImageWindow::first; iw!=0; iw=iw->next) {
	if (iw->device == dev)
	    return iw;
    }
    return NULL;
}


// open window for device and add to list
void 
ImageWindow::open(char FAR *dev)
{
    // register class if first window
    if (first == (ImageWindow *)NULL)
	register_class();
   
    // add to list
    if (first)
	next = first;
    first = this;

    // remember device handle
    device = dev;

    // open window
    create_window();
}

// close window and remove from list
void
ImageWindow::close(void)
{
    DestroyWindow(hwnd);

    // remove from list
    if (this == first) {
	first = this->next;
    }
    else {
	ImageWindow *tmp;
	for (tmp = first; tmp!=0; tmp=tmp->next) {
	    if (this == tmp->next)
		tmp->next = this->next;
	}
    }
    
}


void
ImageWindow::register_class(void)
{
	WNDCLASS wndclass;

	/* register the window class for graphics */
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndImgProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = sizeof(LONG);
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(hInstance,(LPSTR)MAKEINTRESOURCE(GSIMAGE_ICON));
	wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szImgName;
	RegisterClass(&wndclass);
}

void
ImageWindow::create_window(void)
{
	HMENU sysmenu;

	cxClient = cyClient = 0;
	nVscrollPos = nVscrollMax = 0;
	nHscrollPos = nHscrollMax = 0;

	// create window
	hwnd = CreateWindow(szImgName, (LPSTR)szImgName,
		  WS_OVERLAPPEDWINDOW,
		  CW_USEDEFAULT, CW_USEDEFAULT, 
		  CW_USEDEFAULT, CW_USEDEFAULT, 
		  NULL, NULL, hInstance, (void FAR *)this);
	ShowWindow(hwnd, SW_SHOWMINNOACTIVE);

	// modify the menu to have the new items we want
	sysmenu = GetSystemMenu(hwnd, 0);	// get the sysmenu
	AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
	AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
}

	
void
ImageWindow::sync(void)
{
    if ( !IsWindow(hwnd) )	// some clod closed the window
	create_window();

    if ( !IsIconic(hwnd) ) {  /* redraw window */
	InvalidateRect(hwnd, NULL, 1);
	UpdateWindow(hwnd);
    }
}


void
ImageWindow::page(void)
{
    if (IsIconic(hwnd))    // useless as an Icon so fix it
	ShowWindow(hwnd, SW_SHOWNORMAL);
    BringWindowToTop(hwnd);

    sync();
}


void
ImageWindow::size(int x, int y)
{
    width = x;
    height = y;
}


/* image window */
LRESULT CALLBACK _export
WndImgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ImageWindow *iw;
    if (message == WM_CREATE) {
	// Object is stored in window extra data.
	// Nothing must try to use the object before WM_CREATE
	// initializes it here.
	iw = (ImageWindow *)(((CREATESTRUCT FAR *)lParam)->lpCreateParams);
	SetWindowLong(hwnd, 0, (LONG)iw);
    }

    // call the object window procedure
    iw = (ImageWindow *)GetWindowLong(hwnd, 0);
    return iw->WndProc(hwnd, message, wParam, lParam);
}

/* graphics window */
LRESULT 
ImageWindow::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    int nVscrollInc, nHscrollInc;

    switch(message) {
	case WM_SYSCOMMAND:
	    // copy to clipboard
	    if (LOWORD(wParam) == M_COPY_CLIP) {
		HGLOBAL hglobal;
		HPALETTE hpalette;
		gsdll.lock_device(device, 1);
		hglobal = gsdll.copy_dib(device);
		if (hglobal == (HGLOBAL)NULL) {
		    MessageBox(hwnd, "Not enough memory to Copy to Clipboard", 
			szImgName, MB_OK | MB_ICONEXCLAMATION);
		    gsdll.lock_device(device, 0);
		    return 0;
		}
		OpenClipboard(hwnd);
		EmptyClipboard();
		SetClipboardData(CF_DIB, hglobal);
		hpalette = gsdll.copy_palette(device);
		if (hpalette)
		    SetClipboardData(CF_PALETTE, hpalette);
		CloseClipboard();
		gsdll.lock_device(device, 0);
		return 0;
	    }
	    break;
	case WM_CREATE:
	    // enable drag-drop
	    DragAcceptFiles(hwnd, TRUE);
	    break;
	case WM_SIZE:
	    if (wParam == SIZE_MINIMIZED)
		    return(0);
	    cyClient = HIWORD(lParam);
	    cxClient = LOWORD(lParam);

	    cyAdjust = min(height, cyClient) - cyClient;
	    cyClient += cyAdjust;

	    nVscrollMax = max(0, height - cyClient);
	    nVscrollPos = min(nVscrollPos, nVscrollMax);

	    SetScrollRange(hwnd, SB_VERT, 0, nVscrollMax, FALSE);
	    SetScrollPos(hwnd, SB_VERT, nVscrollPos, TRUE);

	    cxAdjust = min(width,  cxClient) - cxClient;
	    cxClient += cxAdjust;

	    nHscrollMax = max(0, width - cxClient);
	    nHscrollPos = min(nHscrollPos, nHscrollMax);

	    SetScrollRange(hwnd, SB_HORZ, 0, nHscrollMax, FALSE);
	    SetScrollPos(hwnd, SB_HORZ, nHscrollPos, TRUE);

	    if ((wParam==SIZENORMAL) && (cxAdjust!=0 || cyAdjust!=0)) {
		GetWindowRect(GetParent(hwnd),&rect);
		MoveWindow(GetParent(hwnd),rect.left,rect.top,
		    rect.right-rect.left+cxAdjust,
		    rect.bottom-rect.top+cyAdjust, TRUE);
		cxAdjust = cyAdjust = 0;
	    }
	    return(0);
	case WM_VSCROLL:
	    switch(LOWORD(wParam)) {
		case SB_TOP:
		    nVscrollInc = -nVscrollPos;
		    break;
		case SB_BOTTOM:
		    nVscrollInc = nVscrollMax - nVscrollPos;
		    break;
		case SB_LINEUP:
		    nVscrollInc = -cyClient/16;
		    break;
		case SB_LINEDOWN:
		    nVscrollInc = cyClient/16;
		    break;
		case SB_PAGEUP:
		    nVscrollInc = min(-1,-cyClient);
		    break;
		case SB_PAGEDOWN:
		    nVscrollInc = max(1,cyClient);
		    break;
		case SB_THUMBTRACK:
		case SB_THUMBPOSITION:
#ifdef __WIN32__
		    nVscrollInc = HIWORD(wParam) - nVscrollPos;
#else
		    nVscrollInc = LOWORD(lParam) - nVscrollPos;
#endif
		    break;
		default:
		    nVscrollInc = 0;
	    }
	    if ((nVscrollInc = max(-nVscrollPos, 
		min(nVscrollInc, nVscrollMax - nVscrollPos)))!=0) {
		nVscrollPos += nVscrollInc;
		ScrollWindow(hwnd,0,-nVscrollInc,NULL,NULL);
		SetScrollPos(hwnd,SB_VERT,nVscrollPos,TRUE);
		UpdateWindow(hwnd);
	    }
	    return(0);
	case WM_HSCROLL:
	    switch(LOWORD(wParam)) {
		case SB_LINEUP:
		    nHscrollInc = -cxClient/16;
		    break;
		case SB_LINEDOWN:
		    nHscrollInc = cyClient/16;
		    break;
		case SB_PAGEUP:
		    nHscrollInc = min(-1,-cxClient);
		    break;
		case SB_PAGEDOWN:
		    nHscrollInc = max(1,cxClient);
		    break;
		case SB_THUMBTRACK:
		case SB_THUMBPOSITION:
#ifdef __WIN32__
		    nHscrollInc = HIWORD(wParam) - nHscrollPos;
#else
		    nHscrollInc = LOWORD(lParam) - nHscrollPos;
#endif
		    break;
		default:
		    nHscrollInc = 0;
	    }
	    if ((nHscrollInc = max(-nHscrollPos, 
		min(nHscrollInc, nHscrollMax - nHscrollPos)))!=0) {
		nHscrollPos += nHscrollInc;
		ScrollWindow(hwnd,-nHscrollInc,0,NULL,NULL);
		SetScrollPos(hwnd,SB_HORZ,nHscrollPos,TRUE);
		UpdateWindow(hwnd);
	    }
	    return(0);
	case WM_KEYDOWN:
	    switch(LOWORD(wParam)) {
		case VK_HOME:
		    SendMessage(hwnd,WM_VSCROLL,SB_TOP,0L);
		    break;
		case VK_END:
		    SendMessage(hwnd,WM_VSCROLL,SB_BOTTOM,0L);
		    break;
		case VK_PRIOR:
		    SendMessage(hwnd,WM_VSCROLL,SB_PAGEUP,0L);
		    break;
		case VK_NEXT:
		    SendMessage(hwnd,WM_VSCROLL,SB_PAGEDOWN,0L);
		    break;
		case VK_UP:
		    SendMessage(hwnd,WM_VSCROLL,SB_LINEUP,0L);
		    break;
		case VK_DOWN:
		    SendMessage(hwnd,WM_VSCROLL,SB_LINEDOWN,0L);
		    break;
		case VK_LEFT:
		    SendMessage(hwnd,WM_HSCROLL,SB_PAGEUP,0L);
		    break;
		case VK_RIGHT:
		    SendMessage(hwnd,WM_HSCROLL,SB_PAGEDOWN,0L);
		    break;
		case VK_RETURN:
		    BringWindowToTop(hwndtext);
		    break;
	    }
	    return(0);
	case WM_CHAR:
	    // send on all characters to text window
	    if (hwndtext)
		SendMessage(hwndtext, message, wParam, lParam);
	    return 0;
	case WM_PAINT:
	    {
	    int sx,sy,wx,wy,dx,dy;
	    hdc = BeginPaint(hwnd, &ps);
	    SetMapMode(hdc, MM_TEXT);
	    SetBkMode(hdc,OPAQUE);
	    gsdll.lock_device(device, 1);
	    rect = ps.rcPaint;
	    dx = rect.left;	/* destination */
	    dy = rect.top;
	    wx = rect.right-rect.left; /* width */
	    wy = rect.bottom-rect.top;
	    sx = rect.left;	/* source */
	    sy = rect.top;
	    sx += nHscrollPos; /* scrollbars */
	    sy += nVscrollPos;	
	    if (sx+wx > width)
		    wx = width - sx;
	    if (sy+wy > height)
		    wy = height - sy;

	    gsdll.draw(device, hdc, dx, dy, wx, wy, sx, sy);
	    gsdll.lock_device(device, 0);

	    EndPaint(hwnd, &ps);
	    return 0;
	    }
	case WM_DROPFILES:
	    SendMessage(hwndtext, message, wParam, lParam);
	    break;
	case WM_DESTROY:
	    DragAcceptFiles(hwnd, FALSE);
	    break;

    }

not_ours:
	return DefWindowProc(hwnd, message, wParam, lParam);
}