1 /*
2 * wmslib/src/but/text.c, part of wmslib (Library functions)
3 * Copyright (C) 1994-1996 William Shubert.
4 * See "configure.h.in" for more copyright information.
5 */
6
7 #include <configure.h>
8
9 #ifdef X11_DISP
10
11 #ifdef STDC_HEADERS
12 #include <stdlib.h>
13 #include <unistd.h>
14 #endif /* STDC_HEADERS */
15 #include <stdio.h>
16 #include <X11/Xlib.h>
17 #include <X11/Xutil.h>
18 #include <X11/cursorfont.h>
19 #include <X11/Xatom.h>
20 #include <X11/keysym.h>
21 #include <sys/time.h>
22 #ifdef HAVE_SYS_SELECT_H
23 #include <sys/select.h>
24 #endif
25 #include <wms.h>
26 #include <but/but.h>
27 #include <but/text.h>
28 #include <wms/str.h>
29
30
31 /**********************************************************************
32 * Data Structures
33 **********************************************************************/
34 typedef struct Txt_struct {
35 Str text;
36 ButTextAlign align;
37 int font;
38 int color;
39 bool stipple;
40 } Txt;
41
42
43 /**********************************************************************
44 * Forward Declarations
45 **********************************************************************/
46 static void draw(But *but, int x,int y, int w,int h);
47 static ButOut destroy(But *but);
48
49
50 /**********************************************************************
51 * Globals
52 **********************************************************************/
53 static const ButAction action = {
54 NULL,NULL,NULL,NULL,
55 NULL,NULL, draw, destroy, but_flags, NULL};
56
57
58 /**********************************************************************
59 * Functions
60 **********************************************************************/
butText_create(ButWin * win,int layer,int flags,const char * text,ButTextAlign align)61 But *butText_create(ButWin *win, int layer, int flags,
62 const char *text, ButTextAlign align) {
63 But *but;
64 Txt *t;
65
66 t = wms_malloc(sizeof(Txt));
67 but = but_create(win, t, &action);
68 but->layer = layer;
69 but->flags = flags;
70
71 str_init(&t->text);
72 t->align = align;
73 t->font = 0;
74 t->color = BUT_FG;
75 t->stipple = FALSE;
76 but_init(but);
77 if (text != NULL)
78 butText_set(but, text);
79 return(but);
80 }
81
82
butText_set(But * but,const char * text)83 void butText_set(But *but, const char *text) {
84 Txt *t = but->iPacket;
85
86 assert(but->action == &action);
87 if (text == NULL)
88 text = "";
89 str_copyChars(&t->text, text);
90 but_draw(but);
91 }
92
93
butText_setFont(But * but,int fontnum)94 void butText_setFont(But *but, int fontnum) {
95 Txt *t = but->iPacket;
96
97 assert(but->action == &action);
98 t->font = fontnum;
99 but_draw(but);
100 }
101
102
butText_setColor(But * but,int color,bool stipple)103 void butText_setColor(But *but, int color, bool stipple) {
104 Txt *t = but->iPacket;
105
106 if (color == BUT_NOCHANGE)
107 color = t->color;
108 if ((color != t->color) || (stipple != t->stipple)) {
109 t->color = color;
110 t->stipple = stipple;
111 but_draw(but);
112 }
113 }
114
115
destroy(But * but)116 static ButOut destroy(But *but) {
117 Txt *t = but->iPacket;
118
119 str_deinit(&t->text);
120 return(0);
121 }
122
123
draw(But * but,int x,int y,int w,int h)124 static void draw(But *but, int x,int y, int w,int h) {
125 Txt *t = but->iPacket;
126 ButEnv *env = but->win->env;
127 XFontStruct *fs = env->fonts[t->font];
128 int th;
129 const char *text = str_chars(&t->text);
130
131 if (t->stipple) {
132 XSetFillStyle(env->dpy, env->gc, FillStippled);
133 XSetForeground(env->dpy, env->gc, env->colors[t->color]);
134 } else {
135 butEnv_setXFg(env, t->color);
136 }
137 x = but->x;
138 if (t->align == butText_center)
139 x += (but->w - butEnv_textWidth(env, text, t->font)) / 2;
140 else if (t->align == butText_right)
141 x += (but->w - butEnv_textWidth(env, text, t->font));
142 th = fs->ascent + fs->descent;
143 butWin_write(but->win, x, but->y + (but->h - th) / 2, text, t->font);
144 if (t->stipple) {
145 butEnv_stdFill(env);
146 }
147 }
148
149
butText_resize(But * but,int x,int y,int h)150 int butText_resize(But *but, int x, int y, int h) {
151 Txt *txt = but->iPacket;
152 int w = butEnv_textWidth(but->win->env, str_chars(&txt->text), txt->font);
153
154 switch (txt->align) {
155 case butText_left:
156 break;
157 case butText_center:
158 x -= w/2;
159 break;
160 case butText_right:
161 x -= w;
162 break;
163 default:
164 break;
165 }
166 but_resize(but, x, y, w, h);
167 return(w);
168 }
169
170
butText_get(But * but)171 const char *butText_get(But *but) {
172 Txt *t;
173
174 assert(MAGIC(but));
175 assert(but->action == &action);
176 t = but->iPacket;
177 return(str_chars(&t->text));
178 }
179
180 #endif
181