ui/motif/graphics.c

Thu, 21 Jan 2016 20:40:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 21 Jan 2016 20:40:14 +0100
changeset 104
3efe0210e27e
parent 103
6a6718269c22
child 114
909fe96e5659
permissions
-rw-r--r--

added grid spacing (WPF)

96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "graphics.h"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "container.h"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 static void ui_drawingarea_expose(Widget widget, XtPointer u, XtPointer c) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 UiDrawEvent *drawevent = u;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct *)c;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 XEvent *event = cbs->event;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 Display *dpy = XtDisplay(widget);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 UiEvent ev;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 ev.obj = drawevent->obj;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 ev.window = drawevent->obj->window;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 ev.document = drawevent->obj->ctx->document;
103
6a6718269c22 added drawing area (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 98
diff changeset
46 ev.eventdata = NULL;
6a6718269c22 added drawing area (Qt)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 98
diff changeset
47 ev.intval = 0;
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 XtVaGetValues(
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 widget,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 XmNwidth,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 &drawevent->gr.g.width,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 XmNheight,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 &drawevent->gr.g.height,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 NULL);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 XGCValues gcvals;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 gcvals.foreground = BlackPixelOfScreen(XtScreen(widget));
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 drawevent->gr.gc = XCreateGC(dpy, XtWindow(widget), (GCForeground), &gcvals);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 drawevent->callback(&ev, &drawevent->gr.g, drawevent->userdata);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 UiContainer *ct = uic_get_current_container(obj);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 int n = 0;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 Arg args[16];
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 Widget parent = ct->prepare(ct, args, &n, TRUE);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 Widget drawingarea = XmCreateDrawingArea(parent, "drawingarea", args, n);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 if(f) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 UiDrawEvent *event = malloc(sizeof(UiDrawEvent));
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 event->obj = obj;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 event->callback = f;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 event->userdata = userdata;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 event->gr.display = XtDisplay(drawingarea);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 event->gr.widget = drawingarea;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 Colormap colormap;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 XtVaGetValues(drawingarea, XmNcolormap, &colormap, NULL);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 event->gr.colormap = colormap;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 XtAddCallback(
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 drawingarea,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 XmNexposeCallback,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 ui_drawingarea_expose,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 event);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 XtManageChild(drawingarea);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 return drawingarea;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96
98
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
97 static void ui_drawingarea_input(Widget widget, XtPointer u, XtPointer c) {
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
98 XmDrawingAreaCallbackStruct *cbs = (XmDrawingAreaCallbackStruct*)c;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
99 XEvent *xevent = cbs->event;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
100 UiMouseEventData *event = u;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
101
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
102 if (cbs->reason == XmCR_INPUT) {
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
103 if (xevent->xany.type == ButtonPress) {
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
104 UiMouseEvent me;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
105 me.x = xevent->xbutton.x;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
106 me.y = xevent->xbutton.y;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
107 // TODO: configurable double click time
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
108 me.type = xevent->xbutton.time - event->last_event > 300 ? UI_PRESS : UI_PRESS2;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
109
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
110 UiEvent e;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
111 e.obj = event->obj;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
112 e.window = event->obj->window;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
113 e.document = event->obj->ctx->document;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
114 e.eventdata = &me;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
115 e.intval = 0;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
116 event->callback(&e, event->userdata);
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
117
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
118
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
119 event->last_event = me.type == UI_PRESS2 ? 0 : xevent->xbutton.time;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
120 }
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
121 }
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
122
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
123 }
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
124
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
125 void ui_mouse_handler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
126 if(f) {
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
127 UiMouseEventData *event = malloc(sizeof(UiMouseEventData));
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
128 event->obj = obj;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
129 event->callback = f;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
130 event->userdata = u;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
131 event->last_event = 0;
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
132
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
133 XtAddCallback(widget, XmNinputCallback, ui_drawingarea_input, event);
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
134 }
efaae97bd95b added mouse event handler (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 97
diff changeset
135 }
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 /* -------------------- text layout functions -------------------- */
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 UiTextLayout* ui_text(UiGraphics *g) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 UiTextLayout *text = malloc(sizeof(UiTextLayout));
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
140 memset(text, 0, sizeof(UiTextLayout));
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 text->text = NULL;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 text->length = 0;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 text->widget = ((UiXlibGraphics*)g)->widget;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 text->fontset = NULL;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 return text;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 static void create_default_fontset(UiTextLayout *layout) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 char **missing = NULL;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 int num_missing = 0;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 char *def = NULL;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 Display *dpy = XtDisplay(layout->widget);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 XFontSet fs = XCreateFontSet(
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 dpy,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 "-dt-interface system-medium-r-normal-s*utf*:,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-1,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-10,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-15,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-2,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-3,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-4,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-5,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-iso8859-9,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-e,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-r,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-ru,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-u,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 "-misc-liberation sans-medium-r-normal--0-0-0-0-p-0-koi8-uni,"
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 "-misc-fixed-medium-r-normal--14-130-75-75-c-140-jisx0208",
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 &missing, &num_missing, &def);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 layout->fontset = fs;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 void ui_text_free(UiTextLayout *text) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 // TODO
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 void ui_text_setstring(UiTextLayout *layout, char *str) {
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
179 ui_text_setstringl(layout, str, strlen(str));
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 void ui_text_setstringl(UiTextLayout *layout, char *str, int len) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 layout->text = str;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 layout->length = len;
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
185 layout->changed = 1;
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 void ui_text_setfont(UiTextLayout *layout, char *font, int size) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 create_default_fontset(layout);//TODO
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
190 layout->changed = 1;
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 void ui_text_getsize(UiTextLayout *layout, int *width, int *height) {
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
194 if(layout->changed) {
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
195 XRectangle ext, lext;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
196 XmbTextExtents(layout->fontset, layout->text, layout->length, &ext, &lext);
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
197 layout->width = ext.width;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
198 layout->height = ext.height;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
199 layout->changed = 0;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
200 }
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
201 *width = layout->width;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
202 *height = layout->height;
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 void ui_text_setwidth(UiTextLayout *layout, int width) {
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
206 layout->maxwidth = width;
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 /* -------------------- drawing functions -------------------- */
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 void ui_graphics_color(UiGraphics *g, int red, int green, int blue) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 UiXlibGraphics *gr = (UiXlibGraphics*)g;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 XColor color;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 color.flags= DoRed | DoGreen | DoBlue;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 color.red = red * 257;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 color.green = green * 257;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 color.blue = blue * 257;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 XAllocColor(gr->display, gr->colormap, &color);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 XSetForeground(gr->display, gr->gc, color.pixel);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 UiXlibGraphics *gr = (UiXlibGraphics*)g;
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 if(fill) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 XFillRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 } else {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 XDrawRectangle(gr->display, XtWindow(gr->widget), gr->gc, x, y, w, h);
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 }
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 UiXlibGraphics *gr = (UiXlibGraphics*)g;
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
234 int width, height;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
235 ui_text_getsize(text, &width, &height);
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
236 if(text->maxwidth > 0) {
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
237 XRectangle clip;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
238 clip.x = x;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
239 clip.y = y;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
240 clip.width = text->maxwidth;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
241 clip.height = height;
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
242 XSetClipRectangles(gr->display, gr->gc, 0, 0, &clip, 1, Unsorted);
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
243 }
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
244
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 XmbDrawString(
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 gr->display,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 XtWindow(gr->widget),
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 text->fontset,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 gr->gc,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 x,
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
251 y + height,
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 text->text,
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 text->length);
97
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
254
1a786201465f implemented text layout functions (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 96
diff changeset
255 XSetClipMask(gr->display, gr->gc, None);
96
93785a7bda56 added drawing area (Motif)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }

mercurial