2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
31 #include <X11/Intrinsic.h>
32 #include <X11/IntrinsicP.h>
36 #include "container.h"
38 static void ui_drawingarea_expose(Widget widget, XtPointer u, XtPointer c) {
39 UiDrawEvent *drawevent = u;
40 //XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct *)c;
41 //XEvent *event = cbs->event;
42 Display *dpy = XtDisplay(widget);
45 ev.obj = drawevent->obj;
46 ev.window = drawevent->obj->window;
47 ev.document = drawevent->obj->ctx->document;
54 &drawevent->gr.g.width,
56 &drawevent->gr.g.height,
60 gcvals.foreground = BlackPixelOfScreen(XtScreen(widget));
61 drawevent->gr.gc = XCreateGC(dpy, XtWindow(widget), (GCForeground), &gcvals);
63 drawevent->callback(&ev, &drawevent->gr.g, drawevent->userdata);
66 UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) {
67 UiContainer *ct = uic_get_current_container(obj);
72 Widget parent = ct->prepare(ct, args, &n, TRUE);
73 Widget drawingarea = XmCreateDrawingArea(parent, "drawingarea", args, n);
76 UiDrawEvent *event = malloc(sizeof(UiDrawEvent));
79 event->userdata = userdata;
81 event->gr.display = XtDisplay(drawingarea);
82 event->gr.widget = drawingarea;
85 XtVaGetValues(drawingarea, XmNcolormap, &colormap, NULL);
86 event->gr.colormap = colormap;
91 ui_drawingarea_expose,
94 XtVaSetValues(drawingarea, XmNuserData, event, NULL);
97 XtManageChild(drawingarea);
101 static void ui_drawingarea_input(Widget widget, XtPointer u, XtPointer c) {
102 XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct*)c;
103 XEvent *xevent = cbs->event;
104 UiMouseEventData *event = u;
106 if (cbs->reason == XmCR_INPUT) {
107 if (xevent->xany.type == ButtonPress) {
109 me.x = xevent->xbutton.x;
110 me.y = xevent->xbutton.y;
111 // TODO: configurable double click time
112 me.type = xevent->xbutton.time - event->last_event > 300 ? UI_PRESS : UI_PRESS2;
116 e.window = event->obj->window;
117 e.document = event->obj->ctx->document;
120 event->callback(&e, event->userdata);
123 event->last_event = me.type == UI_PRESS2 ? 0 : xevent->xbutton.time;
129 void ui_drawingarea_mousehandler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
131 UiMouseEventData *event = malloc(sizeof(UiMouseEventData));
135 event->last_event = 0;
137 XtAddCallback(widget, XmNinputCallback, ui_drawingarea_input, event);
141 void ui_drawingarea_getsize(UIWIDGET drawingarea, int *width, int *height) {
151 void ui_drawingarea_redraw(UIWIDGET drawingarea) {
152 //XClearArea(XtDisplay(drawingarea), drawingarea->core.window, 0, 0, drawingarea->core.width, drawingarea->core.height, True);
154 XtVaGetValues(drawingarea, XmNuserData, &event, NULL);
155 ui_drawingarea_expose(drawingarea, event, NULL);
159 /* -------------------- text layout functions -------------------- */
160 UiTextLayout* ui_text(UiGraphics *g) {
161 UiTextLayout *text = malloc(sizeof(UiTextLayout));
162 memset(text, 0, sizeof(UiTextLayout));
165 text->widget = ((UiXlibGraphics*)g)->widget;
166 text->fontset = NULL;
170 static void create_default_fontset(UiTextLayout *layout) {
171 char **missing = NULL;
174 Display *dpy = XtDisplay(layout->widget);
175 XFontSet fs = XCreateFontSet(
177 "-dt-interface system-medium-r-normal-s*utf*:,"
178 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-1,"
179 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-10,"
180 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-15,"
181 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-2,"
182 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-3,"
183 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-4,"
184 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-5,"
185 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-9,"
186 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-e,"
187 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-r,"
188 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-ru,"
189 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-u,"
190 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-uni,"
191 "-misc-fixed-medium-r-normal--14-130-75-75-c-140-jisx0208",
192 &missing, &num_missing, &def);
193 layout->fontset = fs;
196 void ui_text_free(UiTextLayout *text) {
200 void ui_text_setstring(UiTextLayout *layout, char *str) {
201 ui_text_setstringl(layout, str, strlen(str));
204 void ui_text_setstringl(UiTextLayout *layout, char *str, int len) {
206 layout->length = len;
210 void ui_text_setfont(UiTextLayout *layout, char *font, int size) {
211 create_default_fontset(layout);//TODO
215 void ui_text_getsize(UiTextLayout *layout, int *width, int *height) {
216 if(layout->changed) {
217 XRectangle ext, lext;
218 XmbTextExtents(layout->fontset, layout->text, layout->length, &ext, &lext);
219 layout->width = ext.width;
220 layout->height = ext.height;
223 *width = layout->width;
224 *height = layout->height;
227 void ui_text_setwidth(UiTextLayout *layout, int width) {
228 layout->maxwidth = width;
232 /* -------------------- drawing functions -------------------- */
234 void ui_graphics_color(UiGraphics *g, int red, int green, int blue) {
235 UiXlibGraphics *gr = (UiXlibGraphics*)g;
237 color.flags= DoRed | DoGreen | DoBlue;
238 color.red = red * 257;
239 color.green = green * 257;
240 color.blue = blue * 257;
241 XAllocColor(gr->display, gr->colormap, &color);
242 XSetForeground(gr->display, gr->gc, color.pixel);
245 void ui_draw_line(UiGraphics *g, int x1, int y1, int x2, int y2) {
246 UiXlibGraphics *gr = (UiXlibGraphics*)g;
247 XDrawLine(gr->display, XtWindow(gr->widget), gr->gc, x1, y1, x2, y2);
250 void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
251 UiXlibGraphics *gr = (UiXlibGraphics*)g;
253 XFillRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h);
255 XDrawRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h);
259 void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
260 UiXlibGraphics *gr = (UiXlibGraphics*)g;
262 ui_text_getsize(text, &width, &height);
263 if(text->maxwidth > 0) {
267 clip.width = text->maxwidth;
268 clip.height = height;
269 XSetClipRectangles(gr->display, gr->gc, 0, 0, &clip, 1, Unsorted);
274 XtWindow(gr->widget),
282 XSetClipMask(gr->display, gr->gc, None);