~ubuntu-branches/ubuntu/hardy/xcircuit/hardy

« back to all changes in this revision

Viewing changes to w32x11.c

  • Committer: Bazaar Package Importer
  • Author(s): Aanjhan Ranganathan
  • Date: 2006-04-18 23:51:39 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060418235139-s49pkhwdzxvsxm5k
Tags: 3.4.21-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <tk.h>
 
2
#include <X11/Xlib.h>
 
3
#include <X11/keysym.h>
 
4
#include <xcircuit.h>
 
5
 
 
6
#include <tk.h>
 
7
#include <tkPlatDecls.h>
 
8
 
 
9
#include <X11/cursorfont.h>
 
10
 
 
11
extern Tcl_Interp *xcinterp;
 
12
extern Clientdata areastruct;
 
13
extern char* TkKeysymToString(KeySym ks);
 
14
 
 
15
static void unimplemented(const char *str)
 
16
{
 
17
        printf(">>>>>>>>> Unimplemented: %s\n", str);
 
18
}
 
19
 
 
20
typedef struct font_item {
 
21
        Tk_Font tkfont;
 
22
        Font fid;
 
23
        struct font_item *next;
 
24
};
 
25
static struct font_item *font_map = NULL;
 
26
 
 
27
static void add_tkfont(Tk_Font tkfont, Font fid)
 
28
{
 
29
        struct font_item *item = (struct font_item*)malloc(sizeof(struct font_item));
 
30
 
 
31
        item->next = font_map;
 
32
        font_map = item;
 
33
        item->tkfont = tkfont;
 
34
        item->fid = fid;
 
35
}
 
36
 
 
37
static Tk_Font get_tkfont(Font fid)
 
38
{
 
39
        struct font_item *item = font_map;
 
40
 
 
41
        while (item != NULL) {
 
42
                if (item->fid == fid)
 
43
                        return item->tkfont;
 
44
                item = item->next;
 
45
        }
 
46
        fprintf(stderr, "Font not found: ID=%d\n", fid);
 
47
        return (Tk_Font)NULL;
 
48
}
 
49
 
 
50
#ifndef STATIC_BUILD
 
51
int XDrawPoint(Display *dpy, Drawable d, GC gc, int x, int y)
 
52
{
 
53
        HDC hdc;
 
54
        HWND hwnd;
 
55
 
 
56
        hwnd = Tk_GetHWND(d);
 
57
        if (IsWindow(hwnd)) {
 
58
                hdc = GetDC(hwnd);
 
59
                SetPixelV(hdc, x, y, gc->foreground);
 
60
                ReleaseDC(hwnd, hdc);
 
61
                hdc = NULL;
 
62
        } else {
 
63
                /* This is pretty slow when called a lot
 
64
                hdc = CreateCompatibleDC(NULL);
 
65
                SelectObject(hdc, hwnd);
 
66
                SetPixelV(hdc, x, y, gc->foreground);
 
67
                DeleteDC(hdc);
 
68
                */
 
69
        }
 
70
        return 1;
 
71
}
 
72
#endif
 
73
 
 
74
int XClearArea(Display *dpy, Window w, int x, int y, unsigned int width, unsigned int height, Bool exposures)
 
75
{
 
76
        HWND hnd;
 
77
        RECT r;
 
78
        HDC hdc;
 
79
        int oldROP;
 
80
 
 
81
        hnd = Tk_GetHWND(w);
 
82
        hdc = GetDC(hnd);
 
83
        oldROP = SetROP2(hdc, R2_COPYPEN);
 
84
        GetClientRect(hnd, &r);
 
85
        if (width != 0 || height != 0) {
 
86
                r.left = x;
 
87
                r.top = y;
 
88
                r.right = x+width;
 
89
                r.bottom = y+height;
 
90
                FillRect(hdc, &r, (HBRUSH)(COLOR_WINDOW+1));
 
91
        }
 
92
        SetROP2(hdc, oldROP);
 
93
        ReleaseDC(hnd, hdc);
 
94
        return 1;
 
95
}
 
96
 
 
97
Pixmap XCreatePixmap(Display *dpy, Drawable d, unsigned int width, unsigned int height, unsigned int depth)
 
98
{
 
99
        return Tk_GetPixmap(dpy, d, width, height, depth);
 
100
}
 
101
 
 
102
int XLookupString(XKeyEvent *event, char *buf, int buflen, KeySym *keysym, XComposeStatus *status)
 
103
{
 
104
#if 0
 
105
        printf("code : %04x\n", event->keycode);
 
106
        printf("state: %04x\n", event->state);
 
107
#endif
 
108
        switch (event->keycode) {
 
109
          case VK_NUMPAD0:
 
110
          case VK_NUMPAD1:
 
111
          case VK_NUMPAD2:
 
112
          case VK_NUMPAD3:
 
113
          case VK_NUMPAD4:
 
114
          case VK_NUMPAD5:
 
115
          case VK_NUMPAD6:
 
116
          case VK_NUMPAD7:
 
117
          case VK_NUMPAD8:
 
118
          case VK_NUMPAD9:
 
119
                  *keysym = XK_KP_0 + (event->keycode - VK_NUMPAD0);
 
120
                  event->state |= ShiftMask;
 
121
                  break;
 
122
          default:
 
123
                  *keysym = XKeycodeToKeysym(NULL, event->keycode, event->state);
 
124
                  break;
 
125
        }
 
126
        return 1;
 
127
}
 
128
 
 
129
Bool XCheckWindowEvent(Display *dpy, Window w, long event_mask, XEvent *event)
 
130
{
 
131
        unimplemented("XCheckWindowEvent");
 
132
        return False;
 
133
}
 
134
 
 
135
int XTextWidth(XFontStruct *font, char *string, int len)
 
136
{
 
137
        Tk_Font tkfont = get_tkfont(font->fid);
 
138
        return Tk_TextWidth(tkfont, string, len);
 
139
}
 
140
 
 
141
int XDrawString(Display *dpy, Drawable d, GC gc, int x, int y, char *string, int len)
 
142
{
 
143
        Tk_Font tkfont = get_tkfont(gc->font);
 
144
        Tk_DrawChars(dpy, d, gc, tkfont, string, len, x, y); 
 
145
        return 1;
 
146
}
 
147
 
 
148
int XFreePixmap(Display *dpy, Pixmap pix)
 
149
{
 
150
        Tk_FreePixmap(dpy, pix);
 
151
        return 1;
 
152
}
 
153
 
 
154
#ifndef STATIC_BUILD
 
155
int XPutImage(Display *dpy, Drawable d, GC gc, XImage *img, int src_x, int src_y, int dest_x, int dest_y, unsigned int width, unsigned int height)
 
156
{
 
157
        unimplemented("XPutImage");
 
158
        return 1;
 
159
}
 
160
#endif
 
161
 
 
162
int XSync(Display *dpy, Bool discard)
 
163
{
 
164
        unimplemented("XSync");
 
165
        return 1;
 
166
}
 
167
 
 
168
int XFlush(Display *dpy)
 
169
{
 
170
        unimplemented("XFlush");
 
171
        return 1;
 
172
}
 
173
 
 
174
#ifndef STATIC_BUILD
 
175
Cursor XCreateFontCursor(Display *dpy, unsigned int shape)
 
176
{
 
177
        Tk_Window win = Tk_MainWindow(xcinterp);
 
178
 
 
179
        switch (shape) {
 
180
                case XC_xterm: return (Cursor)Tk_GetCursor(xcinterp, win, "xterm");
 
181
                case XC_watch: return (Cursor)Tk_GetCursor(xcinterp, win, "watch");
 
182
                default: return (Cursor)NULL;
 
183
        }
 
184
}
 
185
#endif
 
186
 
 
187
void XDefineCursor_TkW32(Display *dpy, Window w, Cursor c)
 
188
{
 
189
        Tk_DefineCursor(Tk_IdToWindow(dpy, w), (Tk_Cursor)c);
 
190
}
 
191
 
 
192
static u_char reverse_byte(u_char c)
 
193
{
 
194
        u_char rc = 0;
 
195
        int i;
 
196
 
 
197
        for (i=(sizeof(char)*8-1); i>=0; i--, c>>=1)
 
198
                rc |= (c&0x01) << i;
 
199
        return rc;
 
200
}
 
201
 
 
202
static void compute_cursor_src_mask(u_char *src, u_char *mask)
 
203
{
 
204
        u_char pixsrc = *src, pixmask = *mask;
 
205
        *src = ~(reverse_byte(pixmask));
 
206
        *mask = reverse_byte(~pixsrc & pixmask);
 
207
}
 
208
 
 
209
typedef struct tkw32cursor {
 
210
        struct tkcursor {
 
211
                Tk_Cursor cursor;
 
212
                Display *display;
 
213
                int resourceCount;
 
214
                int objRefCount;
 
215
                Tcl_HashTable *otherTable;
 
216
                Tcl_HashEntry *hashPtr;
 
217
                Tcl_HashEntry *idHashPtr;
 
218
                struct tkcursor *nextPtr;
 
219
        } info;
 
220
        HCURSOR winCursor;
 
221
        int system;
 
222
} w32cursor;
 
223
 
 
224
static
 
225
Cursor Tk_GetCursorFromData_TkW32(Tcl_Interp *interp, Tk_Window w, u_char *src, u_char *mask, int width, int height, int xhot, int yhot, Tk_Uid fg, Tk_Uid bg)
 
226
{
 
227
        w32cursor *wcursor;
 
228
 
 
229
        wcursor = (w32cursor*)ckalloc(sizeof(w32cursor));
 
230
        wcursor->info.cursor = (Tk_Cursor)wcursor;
 
231
        wcursor->winCursor = NULL;
 
232
        wcursor->system = 0;
 
233
 
 
234
        wcursor->winCursor = CreateCursor(Tk_GetHINSTANCE(), xhot, yhot, width, height, src, mask);
 
235
        if (wcursor->winCursor == NULL) {
 
236
                ckfree((char*)wcursor);
 
237
                return (Cursor)NULL;
 
238
        }
 
239
 
 
240
        return (Cursor)wcursor;
 
241
}
 
242
 
 
243
Cursor CreateW32Cursor(Tcl_Interp *interp, Tk_Window w, u_char *src, u_char *mask, int width, int height, int xhot, int yhot, Tk_Uid fg, Tk_Uid bg)
 
244
{
 
245
        u_char *new_src, *new_mask;
 
246
        int nb = (width-1)/(8*sizeof(char))+1;
 
247
        int nb2 = (GetSystemMetrics(SM_CXCURSOR)-1)/(8*sizeof(char))+1, height2 = GetSystemMetrics(SM_CYCURSOR);
 
248
        int i, j, idx1 = 0, idx2 = 0;
 
249
        Cursor cursor;
 
250
 
 
251
        new_src = (u_char*)malloc(sizeof(char)*nb2*height2);
 
252
        new_mask = (u_char*)malloc(sizeof(char)*nb2*height2);
 
253
 
 
254
        for (j=0; j<height; j++) {
 
255
                for (i=0; i<nb; i++, idx1++, idx2++) {
 
256
                        new_src[idx2] = src[idx1];
 
257
                        new_mask[idx2] = mask[idx1];
 
258
                        compute_cursor_src_mask(&new_src[idx2], &new_mask[idx2]);
 
259
                        /*printf("%02x ", new_src[idx2]);*/
 
260
                }
 
261
                for (i=0; i<(nb2-nb); i++, idx2++) {
 
262
                        new_src[idx2] = 0xff;
 
263
                        new_mask[idx2] = 0x00;
 
264
                        /*printf("%02x ", new_src[idx2]);*/
 
265
                }
 
266
                /*printf("\n");*/
 
267
        }
 
268
        for (j=0; j<(height2-height); j++) {
 
269
                for (i=0; i<nb2; i++, idx2++) {
 
270
                        new_src[idx2] = 0xff;
 
271
                        new_mask[idx2] = 0x00;
 
272
                        /*printf("%02x ", new_src[idx2]);*/
 
273
                }
 
274
                /*printf("\n");*/
 
275
        }
 
276
        /*printf("\n");*/
 
277
 
 
278
        cursor = Tk_GetCursorFromData_TkW32(interp, w, new_src, new_mask, nb2*8, height2, xhot, yhot, fg, bg);
 
279
 
 
280
        free(new_src);
 
281
        free(new_mask);
 
282
 
 
283
        return cursor;
 
284
}
 
285
 
 
286
int XRecolorCursor(Display *dpy, Cursor cursor, XColor *foreground, XColor *background)
 
287
{
 
288
        unimplemented("XRecolorCursor");
 
289
        return 1;
 
290
}
 
291
 
 
292
Status XAllocNamedColor(Display *dpy, Colormap cm, char* cname, XColor *screen_return, XColor *exact_return)
 
293
{
 
294
        XColor *c = Tk_GetColor(xcinterp, areastruct.area, cname);
 
295
 
 
296
        if (c != NULL) {
 
297
                screen_return->pixel = c->pixel;
 
298
                exact_return->pixel = c->pixel;
 
299
                return True;
 
300
        }
 
301
        return False;
 
302
}
 
303
 
 
304
Status XLookupColor_TkW32(Display *dpy, Colormap cmap, const char *name, XColor *cvcolor, XColor *cvexact)
 
305
{
 
306
        if (XParseColor(dpy, cmap, name, cvcolor)) {
 
307
                return True;
 
308
        }
 
309
        else {
 
310
                return False;
 
311
        }
 
312
}
 
313
 
 
314
int XQueryColors_TkW32(Display *dpy, Colormap cmap, XColor *colors, int ncolors)
 
315
{
 
316
        int i;
 
317
 
 
318
        for (i=0; i<ncolors; i++) {
 
319
                int pixel = colors[i].pixel;
 
320
                colors[i].red = ((pixel&0x000000ff)<<8)|(pixel&0x000000ff);
 
321
                colors[i].green = (pixel&0x0000ff00)|((pixel&0x0000ff00)>>8);
 
322
                colors[i].blue = ((pixel&0x00ff0000)>>8)|((pixel&0x00ff0000)>>16);
 
323
        }
 
324
 
 
325
        return 1;
 
326
}
 
327
 
 
328
Bool XQueryPointer_TkW32(Display *dpy, Window w, Window *root_return, Window *child_return,
 
329
                int *root_x, int *root_y, int *win_x, int *win_y, unsigned int *mask)
 
330
{
 
331
        POINT p;
 
332
 
 
333
        GetCursorPos(&p);
 
334
        *root_x = p.x;
 
335
        *root_y = p.y;
 
336
        ScreenToClient(Tk_GetHWND(w), &p);
 
337
        *win_x = p.x;
 
338
        *win_y = p.y;
 
339
        *mask = 0;
 
340
 
 
341
        return True;
 
342
}
 
343
 
 
344
Colormap XCopyColormapAndFree(Display *dpy, Colormap cmap)
 
345
{
 
346
        unimplemented("XCopyColormapAndFree");
 
347
        return cmap;
 
348
}
 
349
 
 
350
char* XDisplayString(Display *dpy)
 
351
{
 
352
        /*printf("XDisplayString\n");*/
 
353
        return "localhost:0.0\n";
 
354
}
 
355
 
 
356
 
 
357
char* XKeysymToString_TkW32(KeySym ks)
 
358
{
 
359
        return TkKeysymToString(ks);
 
360
}
 
361
 
 
362
XFontStruct* XLoadQueryFont(Display *dpy, char *fontname)
 
363
{
 
364
        Tk_Font tkfont;
 
365
        Tk_FontMetrics tkfm;
 
366
        XFontStruct *fs;
 
367
 
 
368
        tkfont = Tk_GetFont(xcinterp, Tk_MainWindow(xcinterp), fontname);
 
369
        if (tkfont != NULL)
 
370
        {
 
371
                fs = (XFontStruct*)malloc(sizeof(XFontStruct));
 
372
                fs->fid = Tk_FontId(tkfont);
 
373
                Tk_GetFontMetrics(tkfont, &tkfm);
 
374
                fs->ascent = tkfm.ascent;
 
375
                fs->descent = tkfm.descent;
 
376
                add_tkfont(tkfont, fs->fid);
 
377
                return fs;
 
378
        }
 
379
        else
 
380
                return NULL;
 
381
}
 
382
 
 
383
DIR* opendir(const char *name)
 
384
{
 
385
        DIR *d = (DIR*)malloc(sizeof(DIR));
 
386
        static char buffer[MAX_PATH];
 
387
 
 
388
        strncpy(buffer, name, MAX_PATH);
 
389
        strncat(buffer, "\\*", MAX_PATH);
 
390
        d->hnd = FindFirstFile(buffer, &(d->fd));
 
391
        if (d->hnd == INVALID_HANDLE_VALUE)
 
392
                return NULL;
 
393
        d->dirty = 1;
 
394
        return d;
 
395
}
 
396
 
 
397
void closedir(DIR *d)
 
398
{
 
399
        free(d);
 
400
}
 
401
 
 
402
struct direct* readdir(DIR *d)
 
403
{
 
404
        if (!d->dirty)
 
405
        {
 
406
                if (!FindNextFile(d->hnd, &(d->fd)))
 
407
                        return NULL;
 
408
        }
 
409
        d->d.d_name = d->fd.cFileName;
 
410
        d->dirty = 0;
 
411
        return &(d->d);
 
412
}