|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2015 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include "graphics.h" |
|
33 |
|
34 #include "container.h" |
|
35 |
|
36 static void ui_drawingarea_expose(Widget widget, XtPointer u, XtPointer c) { |
|
37 UiDrawEvent *drawevent = u; |
|
38 XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct *)c; |
|
39 XEvent *event = cbs->event; |
|
40 Display *dpy = XtDisplay(widget); |
|
41 |
|
42 UiEvent ev; |
|
43 ev.obj = drawevent->obj; |
|
44 ev.window = drawevent->obj->window; |
|
45 ev.document = drawevent->obj->ctx->document; |
|
46 |
|
47 XtVaGetValues( |
|
48 widget, |
|
49 XmNwidth, |
|
50 &drawevent->gr.g.width, |
|
51 XmNheight, |
|
52 &drawevent->gr.g.height, |
|
53 NULL); |
|
54 |
|
55 XGCValues gcvals; |
|
56 gcvals.foreground = BlackPixelOfScreen(XtScreen(widget)); |
|
57 drawevent->gr.gc = XCreateGC(dpy, XtWindow(widget), (GCForeground), &gcvals); |
|
58 |
|
59 drawevent->callback(&ev, &drawevent->gr.g, drawevent->userdata); |
|
60 } |
|
61 |
|
62 UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) { |
|
63 UiContainer *ct = uic_get_current_container(obj); |
|
64 |
|
65 int n = 0; |
|
66 Arg args[16]; |
|
67 |
|
68 Widget parent = ct->prepare(ct, args, &n, TRUE); |
|
69 Widget drawingarea = XmCreateDrawingArea(parent, "drawingarea", args, n); |
|
70 |
|
71 if(f) { |
|
72 UiDrawEvent *event = malloc(sizeof(UiDrawEvent)); |
|
73 event->obj = obj; |
|
74 event->callback = f; |
|
75 event->userdata = userdata; |
|
76 |
|
77 event->gr.display = XtDisplay(drawingarea); |
|
78 event->gr.widget = drawingarea; |
|
79 |
|
80 Colormap colormap; |
|
81 XtVaGetValues(drawingarea, XmNcolormap, &colormap, NULL); |
|
82 event->gr.colormap = colormap; |
|
83 |
|
84 XtAddCallback( |
|
85 drawingarea, |
|
86 XmNexposeCallback, |
|
87 ui_drawingarea_expose, |
|
88 event); |
|
89 } |
|
90 |
|
91 XtManageChild(drawingarea); |
|
92 return drawingarea; |
|
93 } |
|
94 |
|
95 |
|
96 /* -------------------- text layout functions -------------------- */ |
|
97 UiTextLayout* ui_text(UiGraphics *g) { |
|
98 UiTextLayout *text = malloc(sizeof(UiTextLayout)); |
|
99 text->text = NULL; |
|
100 text->length = 0; |
|
101 text->widget = ((UiXlibGraphics*)g)->widget; |
|
102 text->fontset = NULL; |
|
103 return text; |
|
104 } |
|
105 |
|
106 static void create_default_fontset(UiTextLayout *layout) { |
|
107 char **missing = NULL; |
|
108 int num_missing = 0; |
|
109 char *def = NULL; |
|
110 Display *dpy = XtDisplay(layout->widget); |
|
111 XFontSet fs = XCreateFontSet( |
|
112 dpy, |
|
113 "-dt-interface system-medium-r-normal-s*utf*:," |
|
114 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-1," |
|
115 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-10," |
|
116 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-15," |
|
117 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-2," |
|
118 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-3," |
|
119 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-4," |
|
120 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-5," |
|
121 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-9," |
|
122 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-e," |
|
123 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-r," |
|
124 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-ru," |
|
125 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-u," |
|
126 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-uni," |
|
127 "-misc-fixed-medium-r-normal--14-130-75-75-c-140-jisx0208", |
|
128 &missing, &num_missing, &def); |
|
129 layout->fontset = fs; |
|
130 } |
|
131 |
|
132 void ui_text_free(UiTextLayout *text) { |
|
133 // TODO |
|
134 } |
|
135 |
|
136 void ui_text_setstring(UiTextLayout *layout, char *str) { |
|
137 layout->text = str; |
|
138 layout->length = strlen(str); |
|
139 } |
|
140 |
|
141 void ui_text_setstringl(UiTextLayout *layout, char *str, int len) { |
|
142 layout->text = str; |
|
143 layout->length = len; |
|
144 } |
|
145 |
|
146 void ui_text_setfont(UiTextLayout *layout, char *font, int size) { |
|
147 create_default_fontset(layout);//TODO |
|
148 } |
|
149 |
|
150 void ui_text_getsize(UiTextLayout *layout, int *width, int *height) { |
|
151 // TODO |
|
152 } |
|
153 |
|
154 void ui_text_setwidth(UiTextLayout *layout, int width) { |
|
155 // TODO |
|
156 } |
|
157 |
|
158 |
|
159 /* -------------------- drawing functions -------------------- */ |
|
160 |
|
161 void ui_graphics_color(UiGraphics *g, int red, int green, int blue) { |
|
162 UiXlibGraphics *gr = (UiXlibGraphics*)g; |
|
163 XColor color; |
|
164 color.flags= DoRed | DoGreen | DoBlue; |
|
165 color.red = red * 257; |
|
166 color.green = green * 257; |
|
167 color.blue = blue * 257; |
|
168 XAllocColor(gr->display, gr->colormap, &color); |
|
169 XSetForeground(gr->display, gr->gc, color.pixel); |
|
170 } |
|
171 |
|
172 void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) { |
|
173 UiXlibGraphics *gr = (UiXlibGraphics*)g; |
|
174 if(fill) { |
|
175 XFillRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h); |
|
176 } else { |
|
177 XDrawRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h); |
|
178 } |
|
179 } |
|
180 |
|
181 void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) { |
|
182 UiXlibGraphics *gr = (UiXlibGraphics*)g; |
|
183 XmbDrawString( |
|
184 gr->display, |
|
185 XtWindow(gr->widget), |
|
186 text->fontset, |
|
187 gr->gc, |
|
188 x, |
|
189 y, |
|
190 text->text, |
|
191 text->length); |
|
192 } |