]> git.saurik.com Git - wxWidgets.git/blob - src/x11/nanox.c
Attempt at creating appropriate fonts in Nano-X
[wxWidgets.git] / src / x11 / nanox.c
1 /*
2 * nanox.c
3 *
4 * Replacements for some comomon Xlib functions
5 */
6
7 #include "wx/setup.h"
8
9 #if wxUSE_NANOX
10
11 #ifdef __GNUG__
12 #pragma implementation "Xlib.h"
13 #endif
14
15 #include <ctype.h>
16 #include <malloc.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include "wx/defs.h"
20 #include "wx/x11/nanox/X11/Xlib.h"
21
22 /* Expands to some compatibility functions (see XtoNX.h) */
23
24 STATIC_FUNCTIONS
25
26 static GR_PALETTE* s_globalColormap = 0;
27
28 Colormap DefaultColormapOfScreen(Screen* screen)
29 {
30 if (!s_globalColormap)
31 {
32 s_globalColormap = (GR_PALETTE*) malloc(sizeof(GR_PALETTE));
33
34 GrGetSystemPalette(s_globalColormap);
35 }
36
37 return s_globalColormap;
38 }
39
40 int XSetGraphicsExposures( Display* display, GC gc, Bool graphics_exposures)
41 {
42 return Success ;
43 }
44
45 int XWarpPointer( Display* display, Window srcW, Window destW,
46 int srcX, int srcY,
47 unsigned int srcWidth,
48 unsigned int srcHeight,
49 int destX, int destY)
50 {
51 GrMoveCursor(destX, destY);
52 return Success;
53 }
54
55 int XSetInputFocus(Display* display, Window focus, int revert_to, Time time)
56 {
57 GrSetFocus(focus);
58 return Success;
59 }
60
61 int XGetInputFocus(Display* display, Window* focus_return, int* revert_to_return)
62 {
63 * focus_return = GrGetFocus();
64 * revert_to_return = 0;
65 return Success;
66 }
67
68 int XGrabPointer(Display* display, Window grab_window,
69 Bool owner_events, unsigned int event_mask,
70 int pointer_mode, int keyboard_mode,
71 Window confine_to, Cursor cursor, Time time)
72 {
73 /* According to comments in srvevent.c in Nano-X, the pointer
74 * is implicitly grabbed when a mouse button is down.
75 * We may be able to simulate this further in the event loop.
76 */
77 return Success;
78 }
79
80 int XUngrabPointer(Display* display, Time time)
81 {
82 return Success;
83 }
84
85 int XCopyArea(Display* display, Drawable src, Drawable dest, GC gc,
86 int src_x, int src_y, unsigned int width, unsigned int height,
87 int dest_x, int dest_y)
88 {
89 GrCopyArea(dest, gc, dest_x, dest_y,
90 width, height, src,
91 src_x, src_y, 0);
92 return Success;
93 }
94
95 int XCopyPlane(Display* display, Drawable src, Drawable dest, GC gc,
96 int src_x, int src_y, unsigned int width, unsigned int height,
97 int dest_x, int dest_y, unsigned long plane)
98 {
99 GrCopyArea(dest, gc, dest_x, dest_y,
100 width, height, src,
101 src_x, src_y, 0);
102 return Success;
103 }
104
105 #if 0
106 typedef struct {
107 GR_WINDOW_ID wid; /* window id (or 0 if no such window) */
108 GR_WINDOW_ID parent; /* parent window id */
109 GR_WINDOW_ID child; /* first child window id (or 0) */
110 GR_WINDOW_ID sibling; /* next sibling window id (or 0) */
111 GR_BOOL inputonly; /* TRUE if window is input only */
112 GR_BOOL mapped; /* TRUE if window is mapped */
113 GR_COUNT unmapcount; /* reasons why window is unmapped */
114 GR_COORD x; /* absolute x position of window */
115 GR_COORD y; /* absolute y position of window */
116 GR_SIZE width; /* width of window */
117 GR_SIZE height; /* height of window */
118 GR_SIZE bordersize; /* size of border */
119 GR_COLOR bordercolor; /* color of border */
120 GR_COLOR background; /* background color */
121 GR_EVENT_MASK eventmask; /* current event mask for this client */
122 GR_WM_PROPS props; /* window properties */
123 GR_CURSOR_ID cursor; /* cursor id*/
124 unsigned long processid; /* process id of owner*/
125 } GR_WINDOW_INFO;
126
127 typedef struct {
128 int x, y; /* location of window */
129 int width, height; /* width and height of window */
130 int border_width; /* border width of window */
131 int depth; /* depth of window */
132 Visual *visual; /* the associated visual structure */
133 Window root; /* root of screen containing window */
134 int class; /* InputOutput, InputOnly*/
135 int bit_gravity; /* one of the bit gravity values */
136 int win_gravity; /* one of the window gravity values */
137 int backing_store; /* NotUseful, WhenMapped, Always */
138 unsigned long backing_planes;/* planes to be preserved if possible */
139 unsigned long backing_pixel;/* value to be used when restoring planes */
140 Bool save_under; /* boolean, should bits under be saved? */
141 Colormap colormap; /* color map to be associated with window */
142 Bool map_installed; /* boolean, is color map currently installed*/
143 int map_state; /* IsUnmapped, IsUnviewable, IsViewable */
144 long all_event_masks; /* set of events all people have interest in*/
145 long your_event_mask; /* my event mask */
146 long do_not_propagate_mask;/* set of events that should not propagate */
147 Bool override_redirect; /* boolean value for override-redirect */
148 Screen *screen; /* back pointer to correct screen */
149 } XWindowAttributes;
150
151 #endif
152
153
154 Status XGetWindowAttributes(Display* display, Window w,
155 XWindowAttributes* window_attributes)
156 {
157 GR_WINDOW_INFO info;
158 GrGetWindowInfo(w, & info);
159
160 window_attributes->x = info.x;
161 window_attributes->y = info.y;
162 window_attributes->width = info.width;
163 window_attributes->height = info.height;
164 window_attributes->border_width = info.bordersize;
165 window_attributes->depth = 0;
166 window_attributes->visual = NULL;
167 window_attributes->root = 0;
168 window_attributes->_class = info.inputonly ? InputOnly : InputOutput ;
169 window_attributes->bit_gravity = 0;
170 window_attributes->win_gravity = 0;
171 window_attributes->backing_store = 0;
172 window_attributes->backing_planes = 0;
173 window_attributes->backing_pixel = 0;
174 window_attributes->save_under = FALSE;
175 window_attributes->colormap = DefaultColormapOfScreen(0);
176 window_attributes->map_installed = FALSE;
177 window_attributes->map_state = info.mapped ? IsViewable : IsUnmapped ;
178 window_attributes->all_event_masks = 0;
179 window_attributes->do_not_propagate_mask = 0;
180 window_attributes->override_redirect = FALSE;
181 window_attributes->screen = NULL;
182
183 return 1;
184 }
185
186 static XErrorHandler* g_ErrorHandler = NULL;
187
188 static void DefaultNanoXErrorHandler(GR_EVENT_ERROR* ep)
189 {
190 if (g_ErrorHandler)
191 {
192 XErrorEvent errEvent;
193 errEvent.type = ep->type;
194 errEvent.display = NULL;
195 errEvent.resourceid = ep->id;
196 errEvent.serial = 0;
197 errEvent.error_code = ep->code;
198 errEvent.request_code = 0;
199 errEvent.minor_code = 0;
200 (*g_ErrorHandler)(NULL, & errEvent);
201 }
202 }
203
204 XErrorHandler XSetErrorHandler (XErrorHandler handler)
205 {
206 XErrorHandler oldHandler = g_ErrorHandler;
207 g_ErrorHandler = handler;
208 GrSetErrorHandler(DefaultNanoXErrorHandler);
209 return oldHandler;
210 }
211
212 static Screen s_screen;
213 Screen *XScreenOfDisplay(Display* display,
214 int screen_number)
215 {
216 /* TODO: fill in the members. See Xlib.h */
217 return & s_screen;
218 }
219
220 int DisplayWidth(Display* display, int screen)
221 {
222 return _display.display_width;
223 }
224
225 int DisplayHeight(Display* display, int screen)
226 {
227 return _display.display_height;
228 }
229
230 int DefaultDepth(Display* display, int screen)
231 {
232 return _display.display_bpp;
233 }
234
235 int XAllocColor(Display* display, Colormap cmap,
236 XColor* color)
237 {
238 GR_PIXELVAL pixel;
239 GrFindColor(GR_RGB(color->red, color->green, color->blue), & pixel);
240 color->pixel = pixel;
241 return 1;
242 }
243
244 typedef struct {
245 const char* name;
246 unsigned int red;
247 unsigned int green;
248 unsigned int blue;
249 } _wxColourEntry;
250
251 static _wxColourEntry _wxColourDatabase[] =
252 {
253 { "WHITE", 255, 255, 255 },
254 { "BLACK", 0, 0, 0 },
255 { "RED", 255, 0, 0 },
256 { "GREEN", 0, 255, 0 },
257 { "BLUE", 0, 255, 255 },
258 { "GREY", 128, 128, 128 },
259 { "GRAY", 128, 128, 128 },
260 { "LIGHT GREY", 192, 192, 192 },
261 { "LIGHT GRAY", 192, 192, 192 },
262 { "DARK GREY", 32, 32, 32 },
263 { "DARK GRAY", 32, 32, 32 },
264 { "CYAN", 0, 255, 255 },
265 { "MAGENTA", 255, 255, 0 },
266
267 /* TODO: the rest */
268 { NULL, 0, 0, 0 }
269 };
270
271 int XParseColor(Display* display, Colormap cmap,
272 const char* cname, XColor* color)
273 {
274 int i = 0;
275 for (;;)
276 {
277 if (!_wxColourDatabase[i].name)
278 break;
279 else
280 {
281 if (strcmp(cname, _wxColourDatabase[i].name) == 0)
282 {
283 color->red = _wxColourDatabase[i].red;
284 color->green = _wxColourDatabase[i].green;
285 color->blue = _wxColourDatabase[i].blue;
286
287 return 1;
288 }
289 i ++;
290 }
291 }
292
293 /* Not found: use black */
294 color->red = 0;
295 color->green = 0;
296 color->blue = 0;
297
298 return 0;
299 }
300
301 int XDrawLine(Display* display, Window win, GC gc,
302 int x1, int y1, int x2, int y2)
303 {
304 GR_POINT points[2];
305 points[0].x = x1;
306 points[0].y = y1;
307 points[1].x = x2;
308 points[1].y = y2;
309
310 GrDrawLines(win, gc, points, 2);
311 return 1;
312 }
313
314 int XTextExtents( XFontStruct* font, char* s, int len, int* direction,
315 int* ascent, int* descent2, XCharStruct* overall)
316 {
317 GR_SIZE retwidth, retheight, retbase;
318 GR_GC_ID gc = GrNewGC();
319
320 *ascent = font->info.baseline;
321 *direction = 1; /* ? */
322 *descent2 = 0; /* ? */
323
324 GrSetGCFont(gc, font->fid);
325
326 /* TODO need to set the relevant flags for the character set.
327 * A good trick might be to pass a wxString instead of char*
328 * to this function.
329 */
330
331 GrGetGCTextSize(gc, s, len, GR_TFASCII, & retwidth,
332 & retheight, & retbase);
333 if (overall)
334 {
335 overall->width = retwidth;
336 overall->lbearing = 0;
337 overall->rbearing = 0;
338 overall->ascent = *ascent;
339 overall->descent = 0;
340 overall->attributes = 0;
341 }
342
343 GrDestroyGC(gc);
344
345 return 1;
346 }
347
348 XFontStruct* XLoadQueryFont(Display* display, const char* fontSpec)
349 {
350 /* TODO: map fontSpec to something sensible for Nano-X */
351 char *fontName = NULL;
352 XFontStruct* fontInfo = malloc(sizeof(XFontStruct));
353 fontInfo->fid = GrCreateFont(fontName, 0, 0);
354 GrGetFontInfo(fontInfo->fid, & fontInfo->info);
355 printf("Warning: should not call XLoadQueryFont since font spec is not used in Nano-X.\n");
356 return fontInfo;
357 }
358
359 int XFreeFont(Display* display, XFontStruct* fontStruct)
360 {
361 GrDestroyFont(fontStruct->fid);
362 free(fontStruct);
363 return 1;
364 }
365
366 int XQueryColor(Display* display, Colormap cmap, XColor* color)
367 {
368 /* cmap is a GR_PALETTE */
369 if (color->pixel < cmap->count)
370 {
371 color->red = cmap->palette[color->pixel].r;
372 color->green = cmap->palette[color->pixel].g;
373 color->blue = cmap->palette[color->pixel].b;
374 return 1;
375 }
376 else
377 return 0;
378 }
379
380 int XConfigureWindow(Display* display, Window w, int mask, XWindowChanges* changes)
381 {
382 if ((mask & CWX) && (mask & CWY))
383 GrMoveWindow(w, changes->x, changes->y);
384 if ((mask & CWWidth) && (mask & CWHeight))
385 GrResizeWindow(w, changes->width, changes->height);
386 if (mask & CWBorderWidth)
387 {
388 /* TODO */
389 }
390 if (mask & CWSibling)
391 {
392 /* TODO */
393 }
394 if (mask & CWStackMode)
395 {
396 /* TODO */
397 }
398 return 1;
399 }
400
401 int XTranslateCoordinates(Display* display, Window srcWindow, Window destWindow, int srcX, int srcY, int* destX, int* destY, Window* childReturn)
402 {
403 int offx = 0;
404 int offy = 0;
405
406 Window w = srcWindow;
407 while (w != GR_ROOT_WINDOW_ID)
408 {
409 GR_WINDOW_INFO info;
410 GrGetWindowInfo(w, & info);
411 w = info.parent;
412
413 offx += info.x ;
414 offy += info.y ;
415 }
416
417 w = destWindow;
418
419 while (w != GR_ROOT_WINDOW_ID)
420 {
421 GR_WINDOW_INFO info;
422 GrGetWindowInfo(w, & info);
423 w = info.parent;
424
425 offx -= info.x ;
426 offy -= info.y ;
427 }
428
429 *destX = srcX + offx;
430 *destY = srcY + offy;
431
432 if (childReturn)
433 *childReturn = 0;
434
435 return 1;
436 }
437
438 /* Should not really be necessary but in no-optimize mode
439 * gcc complains that wxNoop is not found if wxNoop is inline.
440 */
441
442 void wxNoop()
443 {
444 }
445
446 #endif
447 /* wxUSE_NANOX */