Debian Codegraph @

keyboard-indicator/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 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21 #include <math.h>
22 #include <string.h>
23
24 #include "applet-struct.h"
25 #include "applet-draw.h"
26
27
28 void cd_xkbd_update_icon (const gchar *cGroupName, const gchar *cShortGroupName, gboolean bRedrawSurface)
29 {
30 //g_print ("%s (%s;%s;%d)\n", __func__, cGroupName, cShortGroupName, bRedrawSurface);
31
32 if (bRedrawSurface) // group has changed -> update the icon and label
33 {
34 if (! cShortGroupName)
35 {
36 cShortGroupName = myData.cShortGroupName;
37 }
38 else
39 {
40 g_free (myData.cShortGroupName);
41 myData.cShortGroupName = g_strdup (cShortGroupName);
42 }
43 if (! cGroupName)
44 {
45 cGroupName = myData.cGroupName;
46 }
47 else
48 {
49 g_free (myData.cGroupName);
50 myData.cGroupName = g_strdup (cGroupName);
51 }
52
53 //\__________________ On sauvegarde l'ancienne surface/texture.
54 cairo_dock_free_image_buffer (myData.pOldImage);
55 myData.pOldImage = myData.pCurrentImage;
56 myData.pCurrentImage = NULL;
57
58 //\__________________ On cree la nouvelle surface (la taille du texte peut avoir change).
59 int iWidth, iHeight;
60 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
61 if (iWidth <= 1 && iHeight <= 1) // peut arriver au lancement en mode desklet.
62 {
63 return;
64 }
65 int w, h;
66 cairo_surface_t *pSurface = cairo_dock_create_surface_from_text_full (cShortGroupName,
67 &myConfig.textDescription,
68 1.,
69 0, /// iWidth
70 &w, &h);
71 myData.pCurrentImage = g_new0 (CairoDockImageBuffer, 1);
72 cairo_dock_load_image_buffer_from_surface (myData.pCurrentImage, pSurface, w, h);
73
74 //\__________________ On lance une transition entre ancienne et nouvelle surface/texture, ou on dessine direct.
75 if (myConfig.iTransitionDuration != 0 && myData.pOldImage != NULL)
76 {
77 CD_APPLET_SET_TRANSITION_ON_MY_ICON (cd_xkbd_render_step_cairo,
78 cd_xkbd_render_step_opengl,
79 g_bUseOpenGL, // bFastPace : vite si opengl, lent si cairo.
80 myConfig.iTransitionDuration,
81 TRUE); // bRemoveWhenFinished
82 }
83 else
84 {
85 if (CD_APPLET_MY_CONTAINER_IS_OPENGL)
86 {
87 CD_APPLET_START_DRAWING_MY_ICON_OR_RETURN ();
88 cd_xkbd_render_step_opengl (myIcon, myApplet);
89 CD_APPLET_FINISH_DRAWING_MY_ICON;
90 }
91 else
92 {
93 cd_xkbd_render_step_cairo (myIcon, myApplet);
94 }
95 CD_APPLET_REDRAW_MY_ICON;
96 }
97
98 //\__________________ update the label.
99 CD_APPLET_SET_NAME_FOR_MY_ICON (cGroupName);
100 }
101 else // only the indicators have changed -> trigger a redraw event only (overlay update).
102 {
103 CD_APPLET_REDRAW_MY_ICON;
104 }
105
106 //\__________________ lock indicators
107 if (myConfig.bShowKbdIndicator)
108 {
109 cd_debug ("XKBD: caps-lock: %d; num-lock: %d", myData.iCurrentIndic & 1, myData.iCurrentIndic & 2);
110 if (myData.iCurrentIndic & 1) // caps-lock
111 {
112 if (! (myData.iPreviousIndic & 1)) // TODO: cairo_dock_search_icon_s_path in init? or here the first time (save to data) and reset in reload?
113 {
114 if (myConfig.cEmblemCapsLock && (myData.cEmblemCapsLock ||
115 (myData.cEmblemCapsLock = cairo_dock_search_icon_s_path (myConfig.cEmblemCapsLock, // search for an icon only the first time
116 MAX (myIcon->image.iWidth/2, myIcon->image.iHeight/2)))))
117 CD_APPLET_ADD_OVERLAY_ON_MY_ICON (myData.cEmblemCapsLock, CAIRO_OVERLAY_UPPER_RIGHT);
118 else
119 CD_APPLET_ADD_OVERLAY_ON_MY_ICON (MY_APPLET_SHARE_DATA_DIR"/caps-lock.png", CAIRO_OVERLAY_UPPER_RIGHT);
120 }
121 }
122 else
123 {
124 if (myData.iPreviousIndic & 1)
125 CD_APPLET_REMOVE_OVERLAY_ON_MY_ICON (CAIRO_OVERLAY_UPPER_RIGHT);
126 }
127
128 if (myData.iCurrentIndic & 2) // num-lock
129 {
130 if (! (myData.iPreviousIndic & 2))
131 {
132 if (myConfig.cEmblemNumLock &&(myData.cEmblemNumLock ||
133 (myData.cEmblemNumLock = cairo_dock_search_icon_s_path (myConfig.cEmblemNumLock,
134 MAX (myIcon->image.iWidth/2, myIcon->image.iHeight/2)))))
135 CD_APPLET_ADD_OVERLAY_ON_MY_ICON (myData.cEmblemNumLock, CAIRO_OVERLAY_UPPER_LEFT);
136 else
137 CD_APPLET_ADD_OVERLAY_ON_MY_ICON (MY_APPLET_SHARE_DATA_DIR"/num-lock.png", CAIRO_OVERLAY_UPPER_LEFT);
138 }
139 }
140 else
141 {
142 if (myData.iPreviousIndic & 2)
143 CD_APPLET_REMOVE_OVERLAY_ON_MY_ICON (CAIRO_OVERLAY_UPPER_LEFT);
144 }
145 myData.iPreviousIndic = myData.iCurrentIndic;
146 }
147 }
148
149
150 gboolean cd_xkbd_render_step_opengl (Icon *pIcon, GldiModuleInstance *myApplet)
151 {
152 g_return_val_if_fail (myData.pCurrentImage != NULL, FALSE);
153 CD_APPLET_ENTER;
154 double f = CD_APPLET_GET_TRANSITION_FRACTION ();
155 cd_debug ("%s (%.2f; %.2fx%.2f)", __func__, f, myIcon->fWidth, myIcon->fHeight);
156
157 int iWidth, iHeight;
158 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
159
160 gldi_gl_container_set_perspective_view_for_icon (myIcon);
161 glScalef (1., -1., 1.);
162
163 _cairo_dock_enable_texture ();
164 _cairo_dock_set_blend_alpha ();
165 _cairo_dock_set_alpha (1.);
166
167 // fond
168 if (myData.bgImage.iTexture != 0)
169 cairo_dock_apply_texture_at_size (myData.bgImage.iTexture, iWidth, iHeight);
170
171 double fTheta = - 45. + f * 90.; // -45 -> 45
172 glTranslatef (0., 0., - iWidth * sqrt(2)/2 * cos (fTheta/180.*G_PI)); // pour faire tenir le cube dans la fenetre.
173 glEnable (GL_DEPTH_TEST);
174
175 // image precedente.
176 int w=0, h;
177 if (fTheta < 25 && myData.pOldImage != NULL) // inutile de dessiner si elle est derriere l'image courante, par l'effet de perspective (en fait 22.5, mais bizarrement ca a l'air un peu trop tot).
178 {
179 w = iWidth * myConfig.fTextRatio; // fill horizontally
180 h = myData.pOldImage->iHeight * (double)w/myData.pOldImage->iWidth; // keep ratio
181 if (h > iHeight * myConfig.fTextRatio)
182 {
183 w *= iHeight * myConfig.fTextRatio / h;
184 h = iHeight * myConfig.fTextRatio;
185 }
186
187 glPushMatrix ();
188 glRotatef (45. + fTheta, 0., 1., 0.); // 0 -> 90
189 glTranslatef (0., (-iHeight + h)/2, w/2); // H center, V bottom
190 cairo_dock_apply_texture_at_size (myData.pOldImage->iTexture, w, h);
191 glPopMatrix ();
192 }
193
194 // image courante a 90deg.
195 w = iWidth * myConfig.fTextRatio; // fill horizontally
196 h = myData.pCurrentImage->iHeight * (double)w/myData.pCurrentImage->iWidth; // keep ratio
197 if (h > iHeight * myConfig.fTextRatio)
198 {
199 w *= iHeight * myConfig.fTextRatio / h;
200 h = iHeight * myConfig.fTextRatio;
201 }
202
203 /**glRotatef (45. + fTheta, 0., 1., 0.); // 0 -> 90
204 glTranslatef (- (w ? w : iWidth)/2, 0., 0.);
205 glRotatef (-90., 0., 1., 0.);*/
206 glRotatef (-45. + fTheta, 0., 1., 0.); // -90 -> 0
207 glTranslatef (0., (-iHeight + h)/2, w/2); // H center, V bottom
208 cairo_dock_apply_texture_at_size (myData.pCurrentImage->iTexture, w, h);
209
210 glDisable (GL_DEPTH_TEST);
211 _cairo_dock_disable_texture ();
212
213 if (myDock)
214 {
215 gldi_gl_container_set_ortho_view (myContainer);
216 }
217
218 CD_APPLET_LEAVE (TRUE);
219 }
220
221
222 gboolean cd_xkbd_render_step_cairo (Icon *pIcon, GldiModuleInstance *myApplet)
223 {
224 CD_APPLET_ENTER;
225 double f = CD_APPLET_GET_TRANSITION_FRACTION ();
226
227 //g_print ("%s (%.2f)\n", __func__, f);
228 int iWidth, iHeight;
229 CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
230 CD_APPLET_LEAVE_IF_FAIL (iHeight != 0, TRUE);
231
232 ///cairo_dock_erase_cairo_context (myDrawContext);
233 CD_APPLET_START_DRAWING_MY_ICON_OR_RETURN_CAIRO (FALSE);
234
235 if (myData.bgImage.pSurface != NULL)
236 {
237 cairo_set_source_surface (
238 myDrawContext,
239 myData.bgImage.pSurface,
240 0.,
241 0.);
242 cairo_paint (myDrawContext);
243 }
244
245 double dx, dy, fScale;
246 if (myData.pOldImage != NULL && 1-f > .01)
247 {
248 fScale = (double)iWidth / myData.pOldImage->iWidth; // scale to fill the icon horizontally
249 if (fScale * myData.pOldImage->iHeight > iHeight) // if the text is too height, scale down
250 {
251 fScale = (double)iHeight / myData.pOldImage->iHeight; // that's smaller than the previous value.
252 }
253 dx = (iWidth - fScale * myData.pOldImage->iWidth)/2; // center horizontally
254 dy = iHeight - fScale * myData.pOldImage->iHeight; // bottom (we draw the caps/num lock on top).
255
256 cairo_save (myDrawContext);
257 cairo_translate (myDrawContext, dx, dy);
258 cairo_scale (myDrawContext, fScale, fScale); // keep ratio
259 cairo_set_source_surface (
260 myDrawContext,
261 myData.pOldImage->pSurface,
262 0,
263 0);
264 cairo_paint_with_alpha (myDrawContext, 1-f);
265 cairo_restore (myDrawContext);
266 }
267
268 if (myData.pCurrentImage != NULL)
269 {
270 fScale = (double)iWidth / myData.pCurrentImage->iWidth; // scale to fill the icon horizontally
271 if (fScale * myData.pCurrentImage->iHeight > iHeight) // if the text is too height, scale down
272 {
273 fScale = (double)iHeight / myData.pCurrentImage->iHeight; // that's smaller than the previous value.
274 }
275 dx = (iWidth - fScale * myData.pCurrentImage->iWidth)/2; // center horizontally
276 dy = iHeight - fScale * myData.pCurrentImage->iHeight; // bottom (we draw the caps/num lock on top).
277
278 cairo_save (myDrawContext);
279 cairo_translate (myDrawContext, dx, dy);
280 cairo_scale (myDrawContext, fScale, fScale); // keep ratio
281 cairo_set_source_surface (
282 myDrawContext,
283 myData.pCurrentImage->pSurface,
284 0,
285 0);
286 cairo_paint_with_alpha (myDrawContext, f);
287 cairo_restore (myDrawContext);
288 }
289
290 CD_APPLET_FINISH_DRAWING_MY_ICON_CAIRO;
291 CD_APPLET_LEAVE (TRUE);
292 }