Debian Codegraph @

Global-Menu/src/applet-draw.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * based on indicator-messages.c written by :
6 * Ted Gould <[email protected]>
7 * Cody Russell <[email protected]>
8 * E-mail : see the 'copyright' file.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26
27 #include "applet-struct.h"
28 #include "applet-draw.h"
29
30 #define _compute_alpha(pImage, iAnimIter, bEnabled) (bEnabled ? \
31 cairo_dock_image_buffer_is_animated (pImage) ? \
32 1. : \
33 1. - .4 * sin (G_PI * iAnimIter / (CD_ANIM_STEPS - 1))\
34 : .6)
35 static void _apply_button_opengl (CairoDockImageBuffer *pImage, int x, int y, gboolean bEnabled, gint iAnimIter)
36 {
37 double fAlpha = _compute_alpha (pImage, iAnimIter, bEnabled);
38 _cairo_dock_set_alpha (fAlpha);
39 cairo_dock_apply_image_buffer_texture_with_offset (pImage, x, y);
40 }
41
42 static void _apply_button_cairo (CairoDockImageBuffer *pImage, int x, int y, gboolean bEnabled, gint iAnimIter)
43 {
44 double fAlpha = _compute_alpha (pImage, iAnimIter, bEnabled);
45 cairo_dock_apply_image_buffer_surface_with_offset (pImage, myDrawContext,
46 x, y,
47 fAlpha);
48 }
49
50 static gboolean cd_app_menu_render_step_opengl (Icon *pIcon, GldiModuleInstance *myApplet)
51 {
52 CD_APPLET_ENTER;
53 double f = CD_APPLET_GET_TRANSITION_FRACTION ();
54
55 int iWidth, iHeight;
56 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
57 CD_APPLET_LEAVE_IF_FAIL (iHeight != 0, TRUE);
58
59 _cairo_dock_enable_texture ();
60 _cairo_dock_set_blend_alpha ();
61
62 // icon position/size
63 int x, y, w, h;
64 if (myConfig.bDisplayControls)
65 {
66 w = MIN (iWidth, iHeight);
67 h = w;
68 }
69 else
70 {
71 w = iWidth;
72 h = iHeight;
73 }
74 if (iWidth > iHeight) // horizontal alignment
75 {
76 x = (-iWidth + w)/2; // on the left
77 y = 0; // vertically centered
78 }
79 else // vertical alignment
80 {
81 x = 0; // horizontally centered
82 y = (iHeight - h)/2; // on top
83 }
84
85 // draw current icon
86 const CairoDockImageBuffer *pImage = NULL, *pPrevImage = NULL;
87
88 Icon *pAppli = cairo_dock_get_appli_icon (myData.pCurrentWindow);
89 if (pAppli)
90 {
91 pImage = gldi_appli_icon_get_image_buffer (pAppli);
92 }
93 GLuint iTexture = (pImage ? pImage->iTexture : 0);
94
95 Icon *pPrevIcon = cairo_dock_get_appli_icon (myData.pPreviousWindow);
96 if (pPrevIcon)
97 {
98 pPrevImage = gldi_appli_icon_get_image_buffer (pPrevIcon);
99 }
100 GLuint iPrevTexture = (pPrevImage ? pPrevImage->iTexture : 0);
101
102 if (iPrevTexture != 0)
103 {
104 _cairo_dock_set_alpha (1-f);
105 glBindTexture (GL_TEXTURE_2D, iPrevTexture);
106 _cairo_dock_apply_current_texture_at_size_with_offset (w, h, x, y);
107 }
108 if (iTexture != 0)
109 {
110 _cairo_dock_set_alpha (f);
111 glBindTexture (GL_TEXTURE_2D, iTexture);
112 _cairo_dock_apply_current_texture_at_size_with_offset (w, h, x, y);
113 }
114 _cairo_dock_set_alpha (1);
115
116 // draw window buttons
117 if (myConfig.bDisplayControls)
118 {
119 // minimize button
120 if (iWidth > iHeight) // horizontal alignment
121 x += w;
122 else
123 y -= w;
124
125 if (myData.bReversedButtonsOrder)
126 _apply_button_opengl (&myData.closeButton, x, y, myData.pCurrentWindow && myData.bCanClose, myData.iAnimIterClose);
127 else
128 _apply_button_opengl (&myData.minimizeButton, x, y, myData.pCurrentWindow && myData.bCanMinimize, myData.iAnimIterMin);
129
130 // restore/maximize button
131 if (iWidth > iHeight) // horizontal alignment
132 x += w;
133 else
134 y -= w;
135
136 if (myData.bReversedButtonsOrder)
137 _apply_button_opengl (&myData.minimizeButton, x, y, myData.pCurrentWindow && myData.bCanMinimize, myData.iAnimIterMin);
138 else
139 {
140 if (myData.pCurrentWindow && myData.pCurrentWindow->bIsMaximized)
141 _apply_button_opengl (&myData.restoreButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterRestore);
142 else
143 _apply_button_opengl (&myData.maximizeButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterMax);
144 }
145
146 // close button
147 if (iWidth > iHeight) // horizontal alignment
148 x += w;
149 else
150 y -= h;
151
152 if (myData.bReversedButtonsOrder)
153 {
154 if (myData.pCurrentWindow && myData.pCurrentWindow->bIsMaximized)
155 _apply_button_opengl (&myData.restoreButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterRestore);
156 else
157 _apply_button_opengl (&myData.maximizeButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterMax);
158 }
159 else
160 _apply_button_opengl (&myData.closeButton, x, y, myData.pCurrentWindow && myData.bCanClose, myData.iAnimIterClose);
161 }
162 _cairo_dock_disable_texture ();
163
164 CD_APPLET_LEAVE (TRUE);
165 }
166
167 static gboolean cd_app_menu_render_step_cairo (Icon *pIcon, GldiModuleInstance *myApplet)
168 {
169 CD_APPLET_ENTER;
170 double f = CD_APPLET_GET_TRANSITION_FRACTION ();
171
172 int iWidth, iHeight;
173 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
174 CD_APPLET_LEAVE_IF_FAIL (iHeight != 0, TRUE);
175
176 CD_APPLET_START_DRAWING_MY_ICON_OR_RETURN_CAIRO (FALSE);
177
178 // items size
179 int x, y, w, h;
180 if (myConfig.bDisplayControls) // we need to draw the icon + 3 buttons
181 {
182 if (iWidth > iHeight) // horizontal alignment
183 {
184 w = MIN (iWidth / 4, iHeight);
185 h = w;
186 }
187 else
188 {
189 w = MIN (iHeight / 4, iWidth);
190 h = w;
191 }
192 }
193 else // just draw the icon on the whole surface.
194 {
195 w = iWidth;
196 h = iHeight;
197 }
198
199 // draw current icon
200 if (iWidth > iHeight) // horizontal alignment
201 {
202 x = 0; // on the left
203 y = (-iHeight + h)/2; // vertically centered
204 }
205 else // vertical alignment
206 {
207 x = (iWidth - w)/2; // horizontally centered
208 y = 0; // on top
209 }
210
211 const CairoDockImageBuffer *pImage = NULL, *pPrevImage = NULL;
212
213 Icon *pPrevIcon = cairo_dock_get_appli_icon (myData.pPreviousWindow);
214 if (pPrevIcon)
215 {
216 pPrevImage = gldi_appli_icon_get_image_buffer (pPrevIcon);
217 }
218 if (pPrevImage && pPrevImage->pSurface)
219 {
220 cairo_dock_apply_image_buffer_surface_at_size (pPrevImage, myDrawContext, w, h, x, y, 1-f);
221 }
222
223 Icon *pAppli = cairo_dock_get_appli_icon (myData.pCurrentWindow);
224 if (pAppli)
225 {
226 pImage = gldi_appli_icon_get_image_buffer (pAppli);
227 }
228 if (pImage && pImage->pSurface)
229 {
230 cairo_dock_apply_image_buffer_surface_at_size (pImage, myDrawContext, w, h, x, y, f);
231 }
232
233 // draw window buttons
234 if (myConfig.bDisplayControls)
235 {
236 // minimize button
237 if (iWidth > iHeight) // horizontal alignment
238 x += w;
239 else
240 y += h;
241
242 if (myData.bReversedButtonsOrder)
243 _apply_button_cairo (&myData.closeButton, x, y, myData.pCurrentWindow && myData.bCanClose, myData.iAnimIterClose);
244 else
245 _apply_button_cairo (&myData.minimizeButton, x, y, myData.pCurrentWindow && myData.bCanMinimize, myData.iAnimIterMin);
246
247 // restore/maximize button
248 if (iWidth > iHeight) // horizontal alignment
249 x += w;
250 else
251 y += h;
252
253 if (myData.bReversedButtonsOrder)
254 _apply_button_cairo (&myData.minimizeButton, x, y, myData.pCurrentWindow && myData.bCanMinimize, myData.iAnimIterMin);
255 else
256 {
257 if (myData.pCurrentWindow && myData.pCurrentWindow->bIsMaximized)
258 _apply_button_cairo (&myData.maximizeButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterMax);
259 else
260 _apply_button_cairo (&myData.restoreButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterRestore);
261 }
262
263 // close button
264 if (iWidth > iHeight) // horizontal alignment
265 x += w;
266 else
267 y += h;
268
269 if (myData.bReversedButtonsOrder)
270 {
271 if (myData.pCurrentWindow && myData.pCurrentWindow->bIsMaximized)
272 _apply_button_cairo (&myData.maximizeButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterMax);
273 else
274 _apply_button_cairo (&myData.restoreButton, x, y, myData.pCurrentWindow && myData.bCanMaximize, myData.iAnimIterRestore);
275 }
276 else
277 _apply_button_cairo (&myData.closeButton, x, y, myData.pCurrentWindow && myData.bCanClose, myData.iAnimIterClose);
278 }
279
280 CD_APPLET_FINISH_DRAWING_MY_ICON_CAIRO;
281 CD_APPLET_LEAVE (TRUE);
282 }
283
284
285 void cd_app_menu_load_button_images (void)
286 {
287 int iWidth, iHeight;
288 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
289 g_return_if_fail (iHeight != 0);
290
291 cairo_dock_unload_image_buffer (&myData.minimizeButton);
292 cairo_dock_unload_image_buffer (&myData.maximizeButton);
293 cairo_dock_unload_image_buffer (&myData.restoreButton);
294 cairo_dock_unload_image_buffer (&myData.closeButton);
295
296 if (myConfig.bDisplayControls)
297 {
298 int w, h;
299 w = MIN (iWidth, iHeight);
300 h = w;
301
302 cairo_dock_load_image_buffer (&myData.minimizeButton,
303 myConfig.cMinimizeImage,
304 w, h, CAIRO_DOCK_ANIMATED_IMAGE);
305 cairo_dock_image_buffer_set_timelength (&myData.minimizeButton, 3.); // 4s
306
307 cairo_dock_load_image_buffer (&myData.maximizeButton,
308 myConfig.cMaximizeImage,
309 w, h, CAIRO_DOCK_ANIMATED_IMAGE);
310 cairo_dock_image_buffer_set_timelength (&myData.maximizeButton, 3.);
311
312 cairo_dock_load_image_buffer (&myData.restoreButton,
313 myConfig.cRestoreImage,
314 w, h, CAIRO_DOCK_ANIMATED_IMAGE);
315 cairo_dock_image_buffer_set_timelength (&myData.restoreButton, 3.);
316
317 cairo_dock_load_image_buffer (&myData.closeButton,
318 myConfig.cCloseImage,
319 w, h, CAIRO_DOCK_ANIMATED_IMAGE);
320 cairo_dock_image_buffer_set_timelength (&myData.closeButton, 3.);
321 }
322 }
323
324
325 void cd_app_menu_default_image (void)
326 {
327 int iWidth, iHeight;
328 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
329 g_return_if_fail (iHeight != 0);
330
331 int w, h;
332 if (myConfig.bDisplayControls)
333 {
334 w = MIN (iWidth, iHeight);
335 h = w;
336 }
337 else
338 {
339 w = iWidth;
340 h = iHeight;
341 }
342 cairo_dock_load_image_buffer (&myData.defaultIcon,
343 MY_APPLET_SHARE_DATA_DIR"/"MY_APPLET_ICON_FILE,
344 w, h, 0);
345 }
346
347
348 void cd_app_menu_redraw_icon (void)
349 {
350 // load the buttons and the default icon
351 if (myData.pCurrentWindow == NULL && myData.defaultIcon.iWidth == 0)
352 {
353 cd_app_menu_default_image ();
354 }
355
356 if (myData.minimizeButton.iWidth == 0)
357 {
358 cd_app_menu_load_button_images ();
359 }
360
361 // set and launch a transition
362 CD_APPLET_SET_TRANSITION_ON_MY_ICON (cd_app_menu_render_step_cairo,
363 cd_app_menu_render_step_opengl,
364 g_bUseOpenGL, // bFastPace : vite si opengl, lent si cairo.
365 myConfig.iTransitionDuration,
366 TRUE); // bRemoveWhenFinished
367 }
368
369
370 void cd_app_menu_redraw_buttons (void)
371 {
372 if (CD_APPLET_MY_CONTAINER_IS_OPENGL)
373 {
374 CD_APPLET_START_DRAWING_MY_ICON_OR_RETURN ();
375 cd_app_menu_render_step_opengl (myIcon, myApplet);
376 CD_APPLET_FINISH_DRAWING_MY_ICON;
377 }
378 else
379 {
380 cd_app_menu_render_step_cairo (myIcon, myApplet);
381 }
382 }
383
384
385 void cd_app_menu_resize (void)
386 {
387 int iWidth, iHeight;
388 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
389 if (myContainer->bIsHorizontal)
390 cairo_dock_resize_applet (myApplet, MAX (iWidth, myData.iNbButtons*iHeight), iHeight);
391 else
392 cairo_dock_resize_applet (myApplet, iWidth, MAX (myData.iNbButtons*iWidth, iHeight));
393 }
394
395
396 CDButtonEnum cd_app_menu_find_button (GldiModuleInstance *myApplet)
397 {
398 int iNumButton = -1;
399 int iMouseX, iMouseY;
400 if (myDesklet) /// TODO: handle the opengl picking...
401 {
402 iMouseX = myDesklet->iMouseX2d;
403 iMouseY = myDesklet->iMouseY2d;
404 }
405 else
406 {
407 iMouseX = myContainer->iMouseX - myIcon->fDrawX;
408 iMouseY = myContainer->iMouseY - myIcon->fDrawY;
409 }
410
411 int w, h;
412 if (myContainer->bIsHorizontal)
413 {
414 w = myIcon->fWidth * myIcon->fScale;
415 h = myIcon->fHeight * myIcon->fScale;
416 }
417 else
418 {
419 h = myIcon->fWidth * myIcon->fScale;
420 w = myIcon->fHeight * myIcon->fScale;
421 int tmp = iMouseX;
422 iMouseX = iMouseY;
423 iMouseY = tmp;
424 }
425 g_return_val_if_fail (w + h != 0, iNumButton); // it can crash with Arithmetic exception if we switch from the dock to a desklet
426 if (w >= h) // horizontal alignment
427 {
428 iNumButton = iMouseX / (w/myData.iNbButtons);
429 }
430 else // vertical alignment
431 {
432 iNumButton = iMouseY / (h/myData.iNbButtons);
433 }
434
435 if (!myConfig.bDisplayControls)
436 iNumButton++;
437
438 if (myData.bReversedButtonsOrder) // 1->1 ; 2->4 ; 3->2 ; 4->3
439 { // Menu (1) - Close (4) - Min (2) - Max (3) instead of
440 // Menu (1) - Min (2) - Max (3) - Close (4)
441 if (iNumButton == CD_BUTTON_MINIMIZE) // 2 is now 4
442 iNumButton = CD_BUTTON_CLOSE;
443 else if (iNumButton != CD_BUTTON_MENU)
444 iNumButton--; // 3 is now 2 and 4 is now 3
445 }
446
447 return iNumButton;
448 }