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