1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/***************************************************************************
* Functions to handle drawing buttons.
* Copyright (C) 2004 Joe Wingbermuehle
***************************************************************************/
#include "jwm.h"
#include "button.h"
#include "font.h"
#include "color.h"
#include "main.h"
#include "icon.h"
#include "image.h"
static void GetScaledIconSize(IconNode *ip, int maxsize,
int *width, int *height);
/***************************************************************************
***************************************************************************/
void DrawButton(ButtonNode *bp) {
long outlinePixel;
long topPixel, bottomPixel;
ColorType fg, bg;
Drawable drawable;
GC gc;
int x, y;
int width, height;
int xoffset, yoffset;
int iconWidth, iconHeight;
int textWidth, textHeight;
Assert(bp);
drawable = bp->drawable;
gc = bp->gc;
x = bp->x;
y = bp->y;
width = bp->width;
height = bp->height;
switch(bp->type) {
case BUTTON_LABEL:
fg = COLOR_MENU_FG;
bg = COLOR_MENU_BG;
outlinePixel = colors[COLOR_MENU_BG];
topPixel = colors[COLOR_MENU_BG];
bottomPixel = colors[COLOR_MENU_BG];
break;
case BUTTON_MENU_ACTIVE:
fg = COLOR_MENU_ACTIVE_FG;
bg = COLOR_MENU_ACTIVE_BG;
outlinePixel = colors[COLOR_MENU_ACTIVE_DOWN];
topPixel = colors[COLOR_MENU_ACTIVE_UP];
bottomPixel = colors[COLOR_MENU_ACTIVE_DOWN];
break;
case BUTTON_TASK:
fg = COLOR_TASK_FG;
bg = COLOR_TASK_BG;
outlinePixel = colors[COLOR_TASK_DOWN];
topPixel = colors[COLOR_TASK_UP];
bottomPixel = colors[COLOR_TASK_DOWN];
break;
case BUTTON_TASK_ACTIVE:
fg = COLOR_TASK_ACTIVE_FG;
bg = COLOR_TASK_ACTIVE_BG;
outlinePixel = colors[COLOR_TASK_ACTIVE_DOWN];
topPixel = colors[COLOR_TASK_ACTIVE_DOWN];
bottomPixel = colors[COLOR_TASK_ACTIVE_UP];
break;
case BUTTON_MENU:
default:
fg = COLOR_MENU_FG;
bg = COLOR_MENU_BG;
outlinePixel = colors[COLOR_MENU_DOWN];
topPixel = colors[COLOR_MENU_UP];
bottomPixel = colors[COLOR_MENU_DOWN];
break;
}
JXSetForeground(display, gc, colors[bg]);
JXFillRectangle(display, drawable, gc, x + 2, y + 2, width - 3, height - 3);
JXSetForeground(display, gc, outlinePixel);
JXDrawLine(display, drawable, gc, x + 1, y, x + width - 1, y);
JXDrawLine(display, drawable, gc, x + 1, y + height, x + width - 1,
y + height);
JXDrawLine(display, drawable, gc, x, y + 1, x, y + height - 1);
JXDrawLine(display, drawable, gc, x + width, y + 1, x + width,
y + height - 1);
JXSetForeground(display, gc, topPixel);
JXDrawLine(display, drawable, gc, x + 1, y + 1, x + width - 2, y + 1);
JXDrawLine(display, drawable, gc, x + 1, y + 2, x + 1, y + height - 2);
JXSetForeground(display, gc, bottomPixel);
JXDrawLine(display, drawable, gc, x + 1, y + height - 1, x + width - 1,
y + height - 1);
JXDrawLine(display, drawable, gc, x + width - 1, y + 1, x + width - 1,
y + height - 2);
iconWidth = 0;
iconHeight = 0;
if(bp->icon) {
if(width < height) {
GetScaledIconSize(bp->icon, width - 4, &iconWidth, &iconHeight);
} else {
GetScaledIconSize(bp->icon, height - 4, &iconWidth, &iconHeight);
}
}
textWidth = 0;
textHeight = 0;
if(bp->text) {
textWidth = GetStringWidth(bp->font, bp->text);
textHeight = GetStringHeight(bp->font);
if(textWidth + iconWidth + 10 > width) {
textWidth = width - iconWidth - 10;
if(textWidth < 0) {
textWidth = 0;
}
}
}
switch(bp->alignment) {
case ALIGN_RIGHT:
xoffset = width - iconWidth - textWidth + 4;
if(xoffset < 4) {
xoffset = 4;
}
break;
case ALIGN_CENTER:
xoffset = width / 2 - (iconWidth + textWidth) / 2;
if(xoffset < 0) {
xoffset = 0;
}
break;
case ALIGN_LEFT:
default:
xoffset = 4;
break;
}
if(bp->icon) {
yoffset = height / 2 - iconHeight / 2;
PutIcon(bp->icon, drawable, x + xoffset, y + yoffset,
iconWidth, iconHeight);
xoffset += iconWidth + 2;
}
if(bp->text) {
yoffset = height / 2 - textHeight / 2;
RenderString(drawable, bp->font, fg, x + xoffset, y + yoffset,
textWidth, NULL, bp->text);
}
}
/***************************************************************************
***************************************************************************/
void ResetButton(ButtonNode *bp, Drawable d, GC g) {
Assert(bp);
bp->type = BUTTON_MENU;
bp->drawable = d;
bp->gc = g;
bp->font = FONT_TRAY;
bp->alignment = ALIGN_LEFT;
bp->x = 0;
bp->y = 0;
bp->width = 1;
bp->height = 1;
bp->icon = NULL;
bp->text = NULL;
}
/***************************************************************************
***************************************************************************/
void GetScaledIconSize(IconNode *ip, int maxsize,
int *width, int *height) {
double ratio;
Assert(ip);
Assert(width);
Assert(height);
/* width to height */
Assert(ip->image->height > 0);
ratio = (double)ip->image->width / ip->image->height;
if(ip->image->width > ip->image->height) {
/* Compute size wrt width */
*width = maxsize * ratio;
*height = *width / ratio;
} else {
/* Compute size wrt height */
*height = maxsize / ratio;
*width = *height * ratio;
}
}
|