JWM Source Documentation
traybutton.c
Go to the documentation of this file.
1 
10 #include "jwm.h"
11 #include "traybutton.h"
12 #include "tray.h"
13 #include "icon.h"
14 #include "image.h"
15 #include "error.h"
16 #include "root.h"
17 #include "main.h"
18 #include "font.h"
19 #include "button.h"
20 #include "misc.h"
21 #include "screen.h"
22 #include "desktop.h"
23 #include "popup.h"
24 #include "timing.h"
25 #include "command.h"
26 #include "cursor.h"
27 #include "settings.h"
28 #include "event.h"
29 #include "action.h"
30 
31 typedef struct TrayButtonType {
32 
34 
35  char *label;
36  char *popup;
37  char *iconName;
39 
40  int mousex;
41  int mousey;
43 
46 
48 
49 static TrayButtonType *buttons = NULL;
50 
51 static void Create(TrayComponentType *cp);
52 static void Destroy(TrayComponentType *cp);
53 static void SetSize(TrayComponentType *cp, int width, int height);
54 static void Resize(TrayComponentType *cp);
55 static void Draw(TrayComponentType *cp);
56 
58  int x, int y, int button);
60  int x, int y, int button);
62  int x, int y, int mask);
63 static void SignalTrayButton(const TimeType *now,
64  int x, int y, Window w, void *data);
65 
68 {
69  TrayButtonType *bp;
70  for(bp = buttons; bp; bp = bp->next) {
71  if(bp->label) {
72  bp->cp->requestedWidth
73  = GetStringWidth(FONT_TRAY, bp->label) + 4;
75  } else {
76  bp->cp->requestedWidth = 0;
77  bp->cp->requestedHeight = 0;
78  }
79  if(bp->iconName) {
80  bp->icon = LoadNamedIcon(bp->iconName, 1, 1);
81  if(JLIKELY(bp->icon)) {
82  bp->cp->requestedWidth += bp->icon->width + 4;
83  if(bp->label) {
84  bp->cp->requestedWidth -= 2;
85  }
86  bp->cp->requestedHeight
87  = Max(bp->icon->height + 4, bp->cp->requestedHeight);
88  } else {
89  Warning(_("could not load tray icon: \"%s\""), bp->iconName);
90  }
91  }
92  }
93 }
94 
97 {
98  TrayButtonType *bp;
99  while(buttons) {
100  bp = buttons->next;
102  if(buttons->label) {
103  Release(buttons->label);
104  }
105  if(buttons->iconName) {
106  Release(buttons->iconName);
107  }
108  DestroyActions(buttons->actions);
109  if(buttons->popup) {
110  Release(buttons->popup);
111  }
112  Release(buttons);
113  buttons = bp;
114  }
115 }
116 
119  const char *label,
120  const char *popup,
121  unsigned int width,
122  unsigned int height)
123 {
124 
125  TrayButtonType *bp;
127 
128  if(JUNLIKELY((label == NULL || strlen(label) == 0)
129  && (iconName == NULL || strlen(iconName) == 0))) {
130  Warning(_("no icon or label for TrayButton"));
131  return NULL;
132  }
133 
134  bp = Allocate(sizeof(TrayButtonType));
135  bp->next = buttons;
136  buttons = bp;
137 
138  bp->icon = NULL;
139  bp->iconName = CopyString(iconName);
140  bp->label = CopyString(label);
141  bp->actions = NULL;
142  bp->popup = CopyString(popup);
143 
144  cp = CreateTrayComponent();
145  cp->object = bp;
146  bp->cp = cp;
147  cp->requestedWidth = width;
148  cp->requestedHeight = height;
149 
152 
153  cp->Create = Create;
154  cp->Destroy = Destroy;
155  cp->SetSize = SetSize;
156  cp->Resize = Resize;
157  cp->Redraw = Draw;
158 
161  if(popup || label) {
163  }
164 
166 
167  return cp;
168 
169 }
170 
173  const char *action,
174  int mask)
175 {
177  AddAction(&bp->actions, action, mask);
178 }
179 
181 void SetSize(TrayComponentType *cp, int width, int height)
182 {
183  TrayButtonType *bp;
184  int labelWidth, labelHeight;
185  int iconWidth, iconHeight;
186 
187  bp = (TrayButtonType*)cp->object;
188 
189  if(bp->label) {
190  labelWidth = GetStringWidth(FONT_TRAY, bp->label) + 6;
191  labelHeight = GetStringHeight(FONT_TRAY) + 6;
192  } else {
193  labelWidth = 4;
194  labelHeight = 4;
195  }
196 
197  if(bp->icon && bp->icon->width && bp->icon->height) {
198  /* With an icon. */
199  int ratio;
200 
201  iconWidth = bp->icon->width;
202  iconHeight = bp->icon->height;
203 
204  /* Fixed point with 16 bit fraction. */
205  ratio = (iconWidth << 16) / iconHeight;
206 
207  if(width > 0) {
208 
209  /* Compute height from width. */
210  iconWidth = width - labelWidth;
211  iconHeight = (iconWidth << 16) / ratio;
212  height = Max(labelHeight, iconHeight + 4);
213 
214  } else if(height > 0) {
215 
216  /* Compute width from height. */
217  iconHeight = height - 4;
218  iconWidth = (iconHeight * ratio) >> 16;
219  width = iconWidth + labelWidth;
220 
221  } else {
222 
223  /* Use best size. */
224  height = Max(labelHeight, iconHeight + 4);
225  iconWidth = ((height - 4) * ratio) >> 16;
226  width = iconWidth + labelWidth;
227 
228  }
229 
230  } else {
231  /* No icon. */
232  if(width > 0) {
233  height = labelHeight;
234  } else if(height > 0) {
235  width = labelWidth;
236  } else {
237  height = labelHeight;
238  width = labelWidth;
239  }
240  }
241 
242  cp->width = width;
243  cp->height = height;
244 
245 }
246 
249 {
251  cp->width, cp->height, rootDepth);
252  Draw(cp);
253 }
254 
257 {
258  Destroy(cp);
259  Create(cp);
260 }
261 
264 {
265  if(cp->pixmap != None) {
267  }
268 }
269 
272 {
273 
274  ButtonNode button;
275  TrayButtonType *bp;
276 
277  bp = (TrayButtonType*)cp->object;
278 
279  ClearTrayDrawable(cp);
280  ResetButton(&button, cp->pixmap);
281  if(cp->grabbed) {
282  button.type = BUTTON_TRAY_ACTIVE;
283  } else {
284  button.type = BUTTON_TRAY;
285  }
286  button.width = cp->width;
287  button.height = cp->height;
289  button.x = 0;
290  button.y = 0;
291  button.font = FONT_TRAY;
292  button.text = bp->label;
293  button.icon = bp->icon;
294  DrawButton(&button);
295 
296 }
297 
299 void ProcessButtonPress(TrayComponentType *cp, int x, int y, int button)
300 {
301  const TrayButtonType *bp = (TrayButtonType*)cp->object;
302  ProcessActionPress(bp->actions, cp, x, y, button);
303 }
304 
306 void ProcessButtonRelease(TrayComponentType *cp, int x, int y, int button)
307 {
308  const TrayButtonType *bp = (TrayButtonType*)cp->object;
309  ProcessActionRelease(bp->actions, cp, x, y, button);
310 }
311 
313 void ProcessMotionEvent(TrayComponentType *cp, int x, int y, int mask)
314 {
316  bp->mousex = cp->screenx + x;
317  bp->mousey = cp->screeny + y;
319 }
320 
322 void SignalTrayButton(const TimeType *now, int x, int y, Window w, void *data)
323 {
324  TrayButtonType *bp = (TrayButtonType*)data;
325  const char *popup;
326 
327  if(bp->popup) {
328  popup = bp->popup;
329  } else if(bp->label) {
330  popup = bp->label;
331  } else {
332  return;
333  }
334  if(bp->cp->tray->window == w &&
335  abs(bp->mousex - x) < settings.doubleClickDelta &&
336  abs(bp->mousey - y) < settings.doubleClickDelta) {
337  if(GetTimeDifference(now, &bp->mouseTime) >= settings.popupDelay) {
338  ShowPopup(x, y, popup, POPUP_BUTTON);
339  }
340  }
341 }
342 
345 {
346  const TrayButtonType *bp;
347  for(bp = buttons; bp; bp = bp->next) {
349  }
350 }

joewing.net / Projects / JWM