JWM Source Documentation
button.c
Go to the documentation of this file.
1 
10 #include "jwm.h"
11 #include "button.h"
12 #include "border.h"
13 #include "main.h"
14 #include "icon.h"
15 #include "image.h"
16 #include "misc.h"
17 #include "settings.h"
18 
21 {
22 
23  ColorType fg;
24  long bg1, bg2;
25  long up, down;
26  DecorationsType decorations;
27 
28  Drawable drawable;
29  GC gc;
30  int x, y;
31  int width, height;
32  int xoffset, yoffset;
33 
34  int iconWidth, iconHeight;
35  int textWidth, textHeight;
36 
37  Assert(bp);
38 
39  drawable = bp->drawable;
40  x = bp->x;
41  y = bp->y;
42  width = bp->width;
43  height = bp->height;
44  gc = JXCreateGC(display, drawable, 0, NULL);
45 
46  /* Determine the colors to use. */
47  switch(bp->type) {
48  case BUTTON_LABEL:
49  fg = COLOR_MENU_FG;
50  bg1 = colors[COLOR_MENU_BG];
51  bg2 = colors[COLOR_MENU_BG];
52  up = colors[COLOR_MENU_UP];
53  down = colors[COLOR_MENU_DOWN];
54  decorations = settings.menuDecorations;
55  break;
56  case BUTTON_MENU_ACTIVE:
62  decorations = settings.menuDecorations;
63  break;
64  case BUTTON_TRAY:
70  decorations = settings.trayDecorations;
71  break;
72  case BUTTON_TRAY_ACTIVE:
78  decorations = settings.trayDecorations;
79  break;
80  case BUTTON_TASK:
81  fg = COLOR_TASKLIST_FG;
86  decorations = settings.taskListDecorations;
87  break;
88  case BUTTON_TASK_ACTIVE:
94  decorations = settings.taskListDecorations;
95  break;
96  case BUTTON_MENU:
97  default:
98  fg = COLOR_MENU_FG;
99  bg1 = colors[COLOR_MENU_BG];
100  bg2 = colors[COLOR_MENU_BG];
101  up = colors[COLOR_MENU_UP];
102  down = colors[COLOR_MENU_DOWN];
103  decorations = settings.menuDecorations;
104  break;
105  }
106 
107  /* Draw the background. */
108  if(bp->fill) {
109 
110  /* Draw the button background. */
111  JXSetForeground(display, gc, bg1);
112  if(bg1 == bg2) {
113  /* single color */
114  JXFillRectangle(display, drawable, gc, x, y, width, height);
115  } else {
116  /* gradient */
117  DrawHorizontalGradient(drawable, gc, bg1, bg2,
118  x, y, width, height);
119  }
120 
121  }
122 
123  /* Draw the border. */
124  if(bp->border) {
125  if(decorations == DECO_MOTIF) {
126  JXSetForeground(display, gc, up);
127  JXDrawLine(display, drawable, gc, x, y, x + width - 1, y);
128  JXDrawLine(display, drawable, gc, x, y, x, y + height - 1);
129  JXSetForeground(display, gc, down);
130  JXDrawLine(display, drawable, gc, x, y + height - 1,
131  x + width - 1, y + height - 1);
132  JXDrawLine(display, drawable, gc, x + width - 1, y,
133  x + width - 1, y + height - 1);
134  } else {
135  JXSetForeground(display, gc, down);
136  JXDrawRectangle(display, drawable, gc, x, y, width - 1, height - 1);
137  }
138  }
139 
140  /* Determine the size of the icon (if any) to display. */
141  iconWidth = 0;
142  iconHeight = 0;
143  if(bp->icon) {
144  if(bp->icon == &emptyIcon) {
145  iconWidth = Min(width - 4, height - 4);
146  iconHeight = iconWidth;
147  } else {
148  const int ratio = (bp->icon->width << 16) / bp->icon->height;
149  const int maxIconWidth = Min(width, height) - 4;
150  iconHeight = height - 4;
151  iconWidth = (iconHeight * ratio) >> 16;
152  if(iconWidth > maxIconWidth) {
153  iconWidth = maxIconWidth;
154  iconHeight = (iconWidth << 16) / ratio;
155  }
156  }
157  }
158 
159  /* Determine how much room is left for text. */
160  textWidth = 0;
161  textHeight = 0;
162  if(bp->text && (width > height || !bp->icon)) {
163  textWidth = GetStringWidth(bp->font, bp->text);
164  textHeight = GetStringHeight(bp->font);
165  if(iconWidth > 0 && textWidth + iconWidth + 7 > width) {
166  textWidth = width - iconWidth - 7;
167  } else if(iconWidth == 0 && textWidth + 5 > width) {
168  textWidth = width - 5;
169  }
170  textWidth = textWidth < 0 ? 0 : textWidth;
171  }
172 
173  /* Determine the offset of the text in the button. */
174  if(bp->alignment == ALIGN_CENTER || width <= height) {
175  xoffset = (width - iconWidth - textWidth + 1) / 2;
176  if(xoffset < 0) {
177  xoffset = 0;
178  }
179  } else {
180  xoffset = 2;
181  }
182 
183  /* Display the icon. */
184  if(bp->icon) {
185  yoffset = (height - iconHeight + 1) / 2;
186  PutIcon(bp->icon, drawable, colors[fg],
187  x + xoffset, y + yoffset,
188  iconWidth, iconHeight);
189  xoffset += iconWidth + 2;
190  }
191 
192  /* Display the label. */
193  if(textWidth > 0) {
194  yoffset = (height - textHeight + 1) / 2;
195  RenderString(drawable, bp->font, fg,
196  x + xoffset, y + yoffset,
197  textWidth, bp->text);
198  }
199 
200  JXFreeGC(display, gc);
201 
202 }
203 
205 void ResetButton(ButtonNode *bp, Drawable d)
206 {
207 
208  Assert(bp);
209 
210  bp->type = BUTTON_MENU;
211  bp->drawable = d;
212  bp->font = FONT_TRAY;
213  bp->alignment = ALIGN_LEFT;
214  bp->x = 0;
215  bp->y = 0;
216  bp->width = 1;
217  bp->height = 1;
218  bp->icon = NULL;
219  bp->text = NULL;
220  bp->fill = 1;
221  bp->border = 0;
222 
223 }

joewing.net / Projects / JWM