1
/*********************************************************************************
3
This file is a part of the cairo-dock program,
4
released under the terms of the GNU General Public License.
6
Written by Necropotame (for any bug report, please mail me to fabounet@users.berlios.de)
8
*********************************************************************************/
11
#include <libxml/tree.h>
12
#include <libxml/parser.h>
14
//#include <cairo-dock-struct.h>
15
#include <cairo-dock-log.h>
16
#include <cairo-dock-applet-facility.h>
17
#include <cairo-dock-draw.h>
18
#include <cairo-dock-draw-opengl.h>
19
#include <cairo-dock-themes-manager.h>
20
#include <cairo-dock-surface-factory.h>
21
#include <cairo-dock-dock-factory.h>
22
#include <cairo-dock-keyfile-utilities.h>
23
#include <cairo-dock-config.h>
24
#include <cairo-dock-renderer-manager.h>
25
#include <cairo-dock-internal-icons.h>
26
#include <cairo-dock-gui-factory.h>
27
#include <cairo-dock-gauge2.h>
29
extern gchar *g_cCairoDockDataDir;
30
extern gboolean g_bUseOpenGL;
32
////////////////////////////////////////////
33
/////////////// LOAD GAUGE /////////////////
34
////////////////////////////////////////////
35
void cairo_dock_xml_open_file2 (const gchar *filePath, const gchar *mainNodeName,xmlDocPtr *myXmlDoc,xmlNodePtr *myXmlNode)
37
xmlDocPtr doc = xmlParseFile (filePath);
40
cd_warning ("Impossible de lire le fichier XML.");
46
xmlNodePtr node = xmlDocGetRootElement (doc);
47
if (node == NULL || xmlStrcmp (node->name, (const xmlChar *) mainNodeName) != 0)
49
cd_warning ("Le format du fichier XML n'est pas valide.");
59
static void _cairo_dock_init_gauge_image (const gchar *cImagePath, GaugeImage2 *pGaugeImage2)
61
// chargement du fichier.
62
pGaugeImage2->pSvgHandle = rsvg_handle_new_from_file (cImagePath, NULL);
64
//On récupère la taille de l'image.
65
RsvgDimensionData SizeInfo;
66
rsvg_handle_get_dimensions (pGaugeImage2->pSvgHandle, &SizeInfo);
67
pGaugeImage2->sizeX = SizeInfo.width;
68
pGaugeImage2->sizeY = SizeInfo.height;
70
static GaugeImage2 *_cairo_dock_new_gauge_image (const gchar *cImagePath)
72
cd_debug ("%s (%s)", __func__, cImagePath);
73
GaugeImage2 *pGaugeImage2 = g_new0 (GaugeImage2, 1);
75
_cairo_dock_init_gauge_image (cImagePath, pGaugeImage2);
79
static void _cairo_dock_load_gauge_image (cairo_t *pSourceContext, GaugeImage2 *pGaugeImage2, int iWidth, int iHeight)
81
cd_message ("%s (%dx%d)", __func__, iWidth, iHeight);
82
if (pGaugeImage2->pSurface != NULL)
83
cairo_surface_destroy (pGaugeImage2->pSurface);
84
if (pGaugeImage2->iTexture != 0)
85
glDeleteTextures (1, &pGaugeImage2->iTexture);
87
if (pGaugeImage2->pSvgHandle != NULL)
89
pGaugeImage2->pSurface = _cairo_dock_create_blank_surface (pSourceContext,
93
cairo_t* pDrawingContext = cairo_create (pGaugeImage2->pSurface);
95
cairo_scale (pDrawingContext,
96
(double) iWidth / (double) pGaugeImage2->sizeX,
97
(double) iHeight / (double) pGaugeImage2->sizeY);
99
rsvg_handle_render_cairo (pGaugeImage2->pSvgHandle, pDrawingContext);
100
cairo_destroy (pDrawingContext);
104
pGaugeImage2->iTexture = cairo_dock_create_texture_from_surface (pGaugeImage2->pSurface);
109
pGaugeImage2->pSurface = NULL;
110
pGaugeImage2->iTexture = 0;
113
static void _cairo_dock_load_gauge_needle (cairo_t *pSourceContext, GaugeIndicator2 *pGaugeIndicator2, int iWidth, int iHeight)
115
cd_message ("%s (%dx%d)", __func__, iWidth, iHeight);
116
GaugeImage2 *pGaugeImage2 = pGaugeIndicator2->pImageNeedle;
117
g_return_if_fail (pGaugeImage2 != NULL);
119
if (pGaugeImage2->pSurface != NULL)
120
cairo_surface_destroy (pGaugeImage2->pSurface);
121
if (pGaugeImage2->iTexture != 0)
122
glDeleteTextures (1, &pGaugeImage2->iTexture);
124
if (pGaugeImage2->pSvgHandle != NULL)
126
int iSize = MIN (iWidth, iHeight);
127
g_print ("size:%d ; %d\n", iSize, pGaugeImage2->sizeX);
128
pGaugeIndicator2->fNeedleScale = (double)iSize / (double) pGaugeImage2->sizeX; // car l'aiguille est a l'horizontale dans le fichier svg.
129
pGaugeIndicator2->iNeedleWidth = (double) pGaugeIndicator2->iNeedleRealWidth * pGaugeIndicator2->fNeedleScale;
130
pGaugeIndicator2->iNeedleHeight = (double) pGaugeIndicator2->iNeedleRealHeight * pGaugeIndicator2->fNeedleScale;
132
cairo_surface_t *pNeedleSurface = _cairo_dock_create_blank_surface (pSourceContext, pGaugeIndicator2->iNeedleWidth, pGaugeIndicator2->iNeedleHeight);
133
g_return_if_fail (cairo_surface_status (pNeedleSurface) == CAIRO_STATUS_SUCCESS);
135
cairo_t* pDrawingContext = cairo_create (pNeedleSurface);
136
g_return_if_fail (cairo_status (pDrawingContext) == CAIRO_STATUS_SUCCESS);
138
cairo_scale (pDrawingContext, pGaugeIndicator2->fNeedleScale, pGaugeIndicator2->fNeedleScale);
139
cairo_translate (pDrawingContext, pGaugeIndicator2->iNeedleOffsetX, pGaugeIndicator2->iNeedleOffsetY);
140
rsvg_handle_render_cairo (pGaugeImage2->pSvgHandle, pDrawingContext);
141
cairo_destroy (pDrawingContext);
143
pGaugeImage2->iTexture = cairo_dock_create_texture_from_surface (pNeedleSurface);
144
cairo_surface_destroy (pNeedleSurface);
148
pGaugeImage2->pSurface = NULL;
149
pGaugeImage2->iTexture = 0;
152
static gboolean cairo_dock_load_gauge_theme (Gauge2 *pGauge, cairo_t *pSourceContext, const gchar *cThemePath)
154
cd_debug ("%s (%s)", __func__, cThemePath);
155
int iWidth = pGauge->dataRenderer.iWidth, iHeight = pGauge->dataRenderer.iHeight;
156
if (iWidth == 0 || iHeight == 0)
159
g_return_val_if_fail (cThemePath != NULL, FALSE);
161
xmlDocPtr pGaugeTheme;
162
xmlNodePtr pGaugeMainNode;
163
gchar *cXmlFile = g_strdup_printf("%s/theme.xml",cThemePath);
164
cairo_dock_xml_open_file2 (cXmlFile, "gauge",&pGaugeTheme,&pGaugeMainNode);
166
g_return_val_if_fail (pGaugeTheme != NULL, FALSE);
169
xmlChar *cAttribute, *cNodeContent, *cTextNodeContent;
170
GString *sImagePath = g_string_new ("");
171
GaugeImage2 *pGaugeImage2;
172
GaugeIndicator2 *pGaugeIndicator2 = NULL;
173
xmlNodePtr pGaugeNode;
174
for (pGaugeNode = pGaugeMainNode->xmlChildrenNode; pGaugeNode != NULL; pGaugeNode = pGaugeNode->next)
176
if (xmlStrcmp (pGaugeNode->name, (const xmlChar *) "name") == 0)
178
pGauge->cThemeName = xmlNodeGetContent(pGaugeNode);
179
cd_debug("gauge : Nom du theme(%s)",pGauge->pIndicatorList);
181
else if (xmlStrcmp (pGaugeNode->name, (const xmlChar *) "rank") == 0)
183
cNodeContent = xmlNodeGetContent (pGaugeNode);
184
CAIRO_DATA_RENDERER (pGauge)->iRank = atoi (cNodeContent);
185
xmlFree (cNodeContent);
187
else if (xmlStrcmp (pGaugeNode->name, (const xmlChar *) "file") == 0)
189
cNodeContent = xmlNodeGetContent (pGaugeNode);
190
g_string_printf (sImagePath, "%s/%s", cThemePath, cNodeContent);
191
ap = xmlHasProp (pGaugeNode, "key");
192
cAttribute = xmlNodeGetContent(ap->xmlChildrenNode);
193
if (xmlStrcmp (cAttribute, "background") == 0)
195
pGauge->pImageBackground = _cairo_dock_new_gauge_image (sImagePath->str);
196
_cairo_dock_load_gauge_image (pSourceContext, pGauge->pImageBackground, iWidth, iHeight);
198
else if (xmlStrcmp (cAttribute, "foreground") == 0)
200
pGauge->pImageForeground = _cairo_dock_new_gauge_image (sImagePath->str);
201
_cairo_dock_load_gauge_image (pSourceContext, pGauge->pImageForeground, iWidth, iHeight);
203
xmlFree (cNodeContent);
204
xmlFree (cAttribute);
206
else if (xmlStrcmp (pGaugeNode->name, (const xmlChar *) "indicator") == 0)
208
if (CAIRO_DATA_RENDERER (pGauge)->iRank == 0)
210
CAIRO_DATA_RENDERER (pGauge)->iRank = 1;
212
for (node = pGaugeNode->next; node != NULL; node = node->next)
214
if (xmlStrcmp (node->name, (const xmlChar *) "indicator") == 0)
215
CAIRO_DATA_RENDERER (pGauge)->iRank ++;
219
pGaugeIndicator2 = g_new0 (GaugeIndicator2, 1);
220
pGaugeIndicator2->direction = 1;
222
cd_debug ("gauge : On charge un indicateur");
223
xmlNodePtr pGaugeSubNode;
224
for (pGaugeSubNode = pGaugeNode->xmlChildrenNode; pGaugeSubNode != NULL; pGaugeSubNode = pGaugeSubNode->next)
226
cNodeContent = xmlNodeGetContent (pGaugeSubNode);
227
if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "posX") == 0)
228
pGaugeIndicator2->posX = atof (cNodeContent);
229
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "posY") == 0)
230
pGaugeIndicator2->posY = atof (cNodeContent);
231
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "text") == 0)
233
xmlNodePtr pTextSubNode;
234
for (pTextSubNode = pGaugeSubNode->xmlChildrenNode; pTextSubNode != NULL; pTextSubNode = pTextSubNode->next)
236
cTextNodeContent = xmlNodeGetContent (pTextSubNode);
237
if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "textX") == 0)
238
pGaugeIndicator2->textX = atof (cNodeContent);
239
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "textY") == 0)
240
pGaugeIndicator2->textY = atof (cNodeContent);
241
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "textWidth") == 0)
242
pGaugeIndicator2->textWidth = atof (cNodeContent);
243
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "textHeight") == 0)
244
pGaugeIndicator2->textHeight = atof (cNodeContent);
245
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "textColorR") == 0)
246
pGaugeIndicator2->textColor[0] = atof (cNodeContent);
247
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "textColorG") == 0)
248
pGaugeIndicator2->textColor[1] = atof (cNodeContent);
249
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "textColorB") == 0)
250
pGaugeIndicator2->textColor[2] = atof (cNodeContent);
253
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "direction") == 0)
254
pGaugeIndicator2->direction = atof (cNodeContent);
255
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "posStart") == 0)
256
pGaugeIndicator2->posStart = atof (cNodeContent);
257
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "posStop") == 0)
258
pGaugeIndicator2->posStop = atof (cNodeContent);
259
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "nb images") == 0)
260
pGaugeIndicator2->iNbImages = atoi (cNodeContent);
261
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "offset x") == 0)
263
pGaugeIndicator2->iNeedleOffsetX = atoi (cNodeContent);
265
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "width") == 0)
267
pGaugeIndicator2->iNeedleRealWidth = atoi (cNodeContent);
269
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "height") == 0)
271
pGaugeIndicator2->iNeedleRealHeight = atoi (cNodeContent);
272
pGaugeIndicator2->iNeedleOffsetY = .5 * pGaugeIndicator2->iNeedleRealHeight;
274
else if(xmlStrcmp (pGaugeSubNode->name, (const xmlChar *) "file") == 0)
276
cd_debug("gauge : On charge un fichier (%s)",cNodeContent);
277
ap = xmlHasProp(pGaugeSubNode, "key");
278
cAttribute = xmlNodeGetContent(ap->xmlChildrenNode);
279
if (strcmp (cAttribute, "needle") == 0 && pGaugeIndicator2->pImageNeedle == NULL)
281
g_string_printf (sImagePath, "%s/%s", cThemePath, cNodeContent);
282
pGaugeIndicator2->pImageNeedle = _cairo_dock_new_gauge_image (sImagePath->str);
284
else if (strcmp (cAttribute,"image") == 0)
286
if (pGaugeIndicator2->iNbImages == 0)
288
pGaugeIndicator2->iNbImages = 1;
290
for (node = pGaugeSubNode->next; node != NULL; node = node->next)
292
if (xmlStrcmp (node->name, (const xmlChar *) "file") == 0)
293
pGaugeIndicator2->iNbImages ++;
296
if (pGaugeIndicator2->pImageList == NULL)
297
pGaugeIndicator2->pImageList = g_new0 (GaugeImage2, pGaugeIndicator2->iNbImages);
299
if (pGaugeIndicator2->iNbImageLoaded < pGaugeIndicator2->iNbImages)
301
g_string_printf (sImagePath, "%s/%s", cThemePath, cNodeContent);
302
_cairo_dock_init_gauge_image (sImagePath->str, &pGaugeIndicator2->pImageList[pGaugeIndicator2->iNbImageLoaded]);
303
pGaugeIndicator2->iNbImageLoaded ++;
306
xmlFree (cAttribute);
308
xmlFree (cNodeContent);
310
if (pGaugeIndicator2->pImageNeedle != NULL) // taille par defaut de l'aiguille si non fournie.
312
if (pGaugeIndicator2->iNeedleRealHeight == 0)
314
pGaugeIndicator2->iNeedleRealHeight = .12*pGaugeIndicator2->pImageNeedle->sizeY; // 12px utiles sur les 100
315
pGaugeIndicator2->iNeedleOffsetY = pGaugeIndicator2->iNeedleRealHeight/2;
317
if (pGaugeIndicator2->iNeedleRealWidth == 0)
319
pGaugeIndicator2->iNeedleRealWidth = pGaugeIndicator2->pImageNeedle->sizeY; // 100px utiles sur les 100
320
pGaugeIndicator2->iNeedleOffsetX = 10;
323
pGauge->pIndicatorList = g_list_append (pGauge->pIndicatorList, pGaugeIndicator2);
326
xmlFreeDoc (pGaugeTheme);
328
g_string_free (sImagePath, TRUE);
330
g_return_val_if_fail (CAIRO_DATA_RENDERER (pGauge)->iRank != 0 && pGaugeIndicator2 != NULL, FALSE);
331
CAIRO_DATA_RENDERER (pGauge)->bCanRenderValueAsText = (pGaugeIndicator2->textWidth != 0 && pGaugeIndicator2->textHeight != 0);
335
void cairo_dock_load_gauge2 (Gauge2 *pGauge, cairo_t *pSourceContext, CairoContainer *pContainer, CairoGaugeAttribute *pAttribute)
337
gboolean bLoadOK = cairo_dock_load_gauge_theme (pGauge, pSourceContext, pAttribute->cThemePath);
340
////////////////////////////////////////////
341
/////////////// DRAW GAUGE /////////////////
342
////////////////////////////////////////////
343
static void _draw_gauge_needle (cairo_t *pSourceContext, Gauge2 *pGauge, GaugeIndicator2 *pGaugeIndicator2, double fValue)
345
GaugeImage2 *pGaugeImage2 = pGaugeIndicator2->pImageNeedle;
346
if(pGaugeImage2 != NULL)
348
double fAngle = (pGaugeIndicator2->posStart + fValue * (pGaugeIndicator2->posStop - pGaugeIndicator2->posStart)) * G_PI / 180.;
349
if (pGaugeIndicator2->direction < 0)
352
double fHalfX = pGauge->pImageBackground->sizeX / 2.0f * (1 + pGaugeIndicator2->posX);
353
double fHalfY = pGauge->pImageBackground->sizeY / 2.0f * (1 - pGaugeIndicator2->posY);
355
cairo_save (pSourceContext);
357
cairo_scale (pSourceContext,
358
(double) CAIRO_DATA_RENDERER (pGauge)->iWidth / (double) pGaugeImage2->sizeX,
359
(double) CAIRO_DATA_RENDERER (pGauge)->iHeight / (double) pGaugeImage2->sizeY);
360
cairo_translate (pSourceContext, fHalfX, fHalfY);
361
cairo_rotate (pSourceContext, -G_PI/2 + fAngle);
363
rsvg_handle_render_cairo (pGaugeImage2->pSvgHandle, pSourceContext);
365
cairo_restore (pSourceContext);
368
static void _draw_gauge_image (cairo_t *pSourceContext, Gauge2 *pGauge, GaugeIndicator2 *pGaugeIndicator2, double fValue)
370
int iNumImage = fValue * (pGaugeIndicator2->iNbImages - 1) + 0.5;
371
g_return_if_fail (iNumImage < pGaugeIndicator2->iNbImages);
373
GaugeImage2 *pGaugeImage2 = &pGaugeIndicator2->pImageList[iNumImage];
374
if (pGaugeImage2->pSurface == NULL)
376
int iWidth = pGauge->dataRenderer.iWidth, iHeight = pGauge->dataRenderer.iHeight;
377
_cairo_dock_load_gauge_image (pSourceContext, pGaugeImage2, iWidth, iHeight);
380
if (pGaugeImage2->pSurface != NULL)
382
cairo_set_source_surface (pSourceContext, pGaugeImage2->pSurface, 0.0f, 0.0f);
383
cairo_paint (pSourceContext);
386
static void cairo_dock_draw_one_gauge (cairo_t *pSourceContext, Gauge2 *pGauge, int iDataOffset)
388
GaugeImage2 *pGaugeImage2;
389
//\________________ On affiche le fond.
390
if(pGauge->pImageBackground != NULL)
392
pGaugeImage2 = pGauge->pImageBackground;
393
cairo_set_source_surface (pSourceContext, pGaugeImage2->pSurface, 0.0f, 0.0f);
394
cairo_paint (pSourceContext);
397
//\________________ On represente l'indicateur de chaque valeur.
398
GList *pIndicatorElement;
401
GaugeIndicator2 *pIndicator;
402
CairoDataRenderer *pRenderer = CAIRO_DATA_RENDERER (pGauge);
403
CairoDataToRenderer *pData = cairo_data_renderer_get_data (pRenderer);
405
for (i = iDataOffset, pIndicatorElement = pGauge->pIndicatorList; i < pData->iNbValues && pIndicatorElement != NULL; i++, pIndicatorElement = pIndicatorElement->next)
407
pIndicator = pIndicatorElement->data;
408
fValue = cairo_data_renderer_get_normalized_current_value (pRenderer, i);
410
if (pIndicator->pImageNeedle != NULL) // c'est une aiguille.
412
_draw_gauge_needle (pSourceContext, pGauge, pIndicator, fValue);
414
else // c'est une image.
416
_draw_gauge_image (pSourceContext, pGauge, pIndicator, fValue);
419
if (pIndicator->textWidth != 0 && pIndicator->textHeight != 0) // cet indicateur a un emplacement pour le texte de la valeur.
421
cairo_data_renderer_format_value (pRenderer, fValue, i);
422
g_print (" >>>%s\n", pRenderer->cFormatBuffer);
423
cairo_save (pSourceContext);
424
cairo_set_source_rgb (pSourceContext, pIndicator->textColor[0], pIndicator->textColor[1], pIndicator->textColor[2]);
425
cairo_set_line_width (pSourceContext, 20.);
427
cairo_text_extents_t textExtents;
428
cairo_text_extents (pSourceContext, pRenderer->cFormatBuffer, &textExtents);
429
cairo_move_to (pSourceContext,
430
pIndicator->textX * pRenderer->iWidth - textExtents.width / 2,
431
pIndicator->textY * pRenderer->iHeight + textExtents.height / 2);
432
cairo_show_text (pSourceContext, pRenderer->cFormatBuffer);
433
cairo_restore (pSourceContext);
437
//\________________ On affiche l'avant-plan.
438
if(pGauge->pImageForeground != NULL)
440
pGaugeImage2 = pGauge->pImageForeground;
441
cairo_set_source_surface (pSourceContext, pGaugeImage2->pSurface, 0.0f, 0.0f);
442
cairo_paint (pSourceContext);
445
void cairo_dock_render_gauge2 (Gauge2 *pGauge, cairo_t *pCairoContext)
447
g_return_if_fail (pGauge != NULL && pGauge->pIndicatorList != NULL && pCairoContext != NULL);
448
g_return_if_fail (cairo_status (pCairoContext) == CAIRO_STATUS_SUCCESS);
450
CairoDataRenderer *pRenderer = CAIRO_DATA_RENDERER (pGauge);
451
CairoDataToRenderer *pData = cairo_data_renderer_get_data (pRenderer);
452
int iNbDrawings = (int) ceil (1. * pData->iNbValues / pRenderer->iRank);
453
int i, iDataOffset = 0;
454
for (i = 0; i < iNbDrawings; i ++)
456
if (iNbDrawings > 1) // on va dessiner la jauges plusieurs fois, la 1ere en grand et les autres en petit autour.
458
cairo_save (pCairoContext);
461
cairo_scale (pCairoContext, 2./3, 2./3);
465
cairo_translate (pCairoContext, 2 * pRenderer->iWidth / 3, 2 * pRenderer->iHeight / 3);
466
cairo_scale (pCairoContext, 1./3, 1./3);
470
cairo_translate (pCairoContext, 2 * pRenderer->iWidth / 3, 0.);
471
cairo_scale (pCairoContext, 1./3, 1./3);
475
cairo_translate (pCairoContext, 0., 2 * pRenderer->iHeight / 3);
476
cairo_scale (pCairoContext, 1./3, 1./3);
478
else // 5 valeurs faut pas pousser non plus.
482
cairo_dock_draw_one_gauge (pCairoContext, pGauge, iDataOffset);
485
cairo_restore (pCairoContext);
487
iDataOffset += pRenderer->iRank;
491
///////////////////////////////////////////////
492
/////////////// RENDER OPENGL /////////////////
493
///////////////////////////////////////////////
494
static void _draw_gauge_image_opengl (Gauge2 *pGauge, GaugeIndicator2 *pGaugeIndicator2, double fValue)
496
int iNumImage = fValue * (pGaugeIndicator2->iNbImages - 1) + 0.5;
497
g_return_if_fail (iNumImage < pGaugeIndicator2->iNbImages);
499
GaugeImage2 *pGaugeImage2 = &pGaugeIndicator2->pImageList[iNumImage];
500
int iWidth = pGauge->dataRenderer.iWidth, iHeight = pGauge->dataRenderer.iHeight;
501
if (pGaugeImage2->iTexture == 0)
503
_cairo_dock_load_gauge_image (NULL, pGaugeImage2, iWidth, iHeight); // pas besoin d'un cairo_context pour creer une cairo_image_surface.
506
if (pGaugeImage2->iTexture != 0)
508
cairo_dock_apply_texture_at_size (pGaugeImage2->iTexture, iWidth, iHeight);
511
static void _draw_gauge_needle_opengl (Gauge2 *pGauge, GaugeIndicator2 *pGaugeIndicator2, double fValue)
513
GaugeImage2 *pGaugeImage2 = pGaugeIndicator2->pImageNeedle;
514
g_return_if_fail (pGaugeImage2 != NULL);
516
if (pGaugeImage2->iTexture == 0)
518
int iWidth = pGauge->dataRenderer.iWidth, iHeight = pGauge->dataRenderer.iHeight;
519
_cairo_dock_load_gauge_needle (NULL, pGaugeIndicator2, iWidth, iHeight); // pas besoin d'un cairo_context pour creer une cairo_image_surface.
522
if(pGaugeImage2->iTexture != 0)
524
double fAngle = (pGaugeIndicator2->posStart + fValue * (pGaugeIndicator2->posStop - pGaugeIndicator2->posStart));
525
if (pGaugeIndicator2->direction < 0)
527
double fHalfX = pGauge->pImageBackground->sizeX / 2.0f * (0 + pGaugeIndicator2->posX);
528
double fHalfY = pGauge->pImageBackground->sizeY / 2.0f * (0 + pGaugeIndicator2->posY);
532
glTranslatef (fHalfX, fHalfY, 0.);
533
glRotatef (90. - fAngle, 0., 0., 1.);
534
glTranslatef (pGaugeIndicator2->iNeedleWidth/2 - pGaugeIndicator2->fNeedleScale * pGaugeIndicator2->iNeedleOffsetX, 0., 0.);
535
glScalef (pGaugeIndicator2->iNeedleWidth, pGaugeIndicator2->iNeedleHeight, 1.);
536
cairo_dock_apply_texture (pGaugeImage2->iTexture);
541
static void cairo_dock_draw_one_gauge_opengl (Gauge2 *pGauge, int iDataOffset)
543
int iWidth = pGauge->dataRenderer.iWidth, iHeight = pGauge->dataRenderer.iHeight;
544
GaugeImage2 *pGaugeImage2;
547
glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA); // ne me demandez pas pourquoi...
549
glEnable (GL_TEXTURE_2D);
550
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
552
glPolygonMode (GL_FRONT, GL_FILL);
554
//\________________ On affiche le fond.
555
if(pGauge->pImageBackground != NULL)
557
pGaugeImage2 = pGauge->pImageBackground;
558
cairo_dock_apply_texture_at_size (pGaugeImage2->iTexture, iWidth, iHeight);
561
//\________________ On represente l'indicateur de chaque valeur.
562
GList *pIndicatorElement;
565
GaugeIndicator2 *pIndicator;
566
CairoDataRenderer *pRenderer = CAIRO_DATA_RENDERER (pGauge);
567
CairoDataToRenderer *pData = cairo_data_renderer_get_data (pRenderer);
569
for (i = iDataOffset, pIndicatorElement = pGauge->pIndicatorList; i < pData->iNbValues && pIndicatorElement != NULL; i++, pIndicatorElement = pIndicatorElement->next)
571
pIndicator = pIndicatorElement->data;
572
fValue = cairo_data_renderer_get_normalized_current_value_with_latency (pRenderer, i);
574
if (pIndicator->pImageNeedle != NULL) // c'est une aiguille.
576
_draw_gauge_needle_opengl (pGauge, pIndicator, fValue);
578
else // c'est une image.
580
_draw_gauge_image_opengl (pGauge, pIndicator, fValue);
583
if (pIndicator->textWidth != 0 && pIndicator->textHeight != 0) // cet indicateur a un emplacement pour le texte de la valeur.
585
cairo_data_renderer_format_value (pRenderer, fValue, i);
586
g_print (" >>>%s\n", pRenderer->cFormatBuffer);
587
/*cairo_save (pSourceContext);
588
cairo_set_source_rgb (pSourceContext, pIndicator->textColor[0], pIndicator->textColor[1], pIndicator->textColor[2]);
589
cairo_set_line_width (pSourceContext, 20.);
591
cairo_text_extents_t textExtents;
592
cairo_text_extents (pSourceContext, pRenderer->cFormatBuffer, &textExtents);
593
cairo_move_to (pSourceContext,
594
pIndicator->textX * pRenderer->iWidth - textExtents.width / 2,
595
pIndicator->textY * pRenderer->iHeight + textExtents.height / 2);
596
cairo_show_text (pSourceContext, pRenderer->cFormatBuffer);
597
cairo_restore (pSourceContext);*/
601
//\________________ On affiche l'avant-plan.
602
if(pGauge->pImageForeground != NULL)
604
pGaugeImage2 = pGauge->pImageForeground;
605
cairo_dock_apply_texture_at_size (pGaugeImage2->iTexture, iWidth, iHeight);
608
glDisable (GL_TEXTURE_2D);
609
glDisable (GL_BLEND);
611
void cairo_dock_render_gauge_opengl2 (Gauge2 *pGauge)
613
g_return_if_fail (pGauge != NULL && pGauge->pIndicatorList != NULL);
615
CairoDataRenderer *pRenderer = CAIRO_DATA_RENDERER (pGauge);
616
CairoDataToRenderer *pData = cairo_data_renderer_get_data (pRenderer);
617
int iNbDrawings = (int) ceil (1. * pData->iNbValues / pRenderer->iRank);
618
int i, iDataOffset = 0;
619
for (i = 0; i < iNbDrawings; i ++)
621
if (iNbDrawings > 1) // on va dessiner la jauges plusieurs fois, la 1ere en grand et les autres en petit autour.
626
glTranslatef (-pRenderer->iWidth / 6, pRenderer->iHeight / 6, 0.);
627
glScalef (2./3, 2./3, 1.);
631
glTranslatef (pRenderer->iWidth / 3, - pRenderer->iHeight / 3, 0.);
632
glScalef (1./3, 1./3, 1.);
636
glTranslatef (pRenderer->iWidth / 3, pRenderer->iHeight / 3, 0.);
637
glScalef (1./3, 1./3, 1.);
641
glTranslatef (-pRenderer->iWidth / 3, -pRenderer->iHeight / 3, 0.);
642
glScalef (1./3, 1./3, 1.);
644
else // 5 valeurs faut pas pousser non plus.
648
cairo_dock_draw_one_gauge_opengl (pGauge, iDataOffset);
653
iDataOffset += pRenderer->iRank;
658
//////////////////////////////////////////////
659
/////////////// RELOAD GAUGE /////////////////
660
//////////////////////////////////////////////
661
void cairo_dock_reload_gauge2 (Gauge2 *pGauge, cairo_t *pSourceContext)
663
//g_print ("%s (%dx%d)\n", __func__, iWidth, iHeight);
664
g_return_if_fail (pGauge != NULL);
666
CairoDataRenderer *pRenderer = CAIRO_DATA_RENDERER (pGauge);
667
int iWidth = pGauge->dataRenderer.iWidth, iHeight = pGauge->dataRenderer.iHeight;
668
if (pGauge->pImageBackground != NULL)
670
_cairo_dock_load_gauge_image (pSourceContext, pGauge->pImageBackground, iWidth, iHeight);
673
if (pGauge->pImageForeground != NULL)
675
_cairo_dock_load_gauge_image (pSourceContext, pGauge->pImageForeground, iWidth, iHeight);
678
GaugeIndicator2 *pGaugeIndicator2;
679
GaugeImage2 *pGaugeImage2;
681
GList *pElement, *pElement2;
682
for (pElement = pGauge->pIndicatorList; pElement != NULL; pElement = pElement->next)
684
pGaugeIndicator2 = pElement->data;
685
for (i = 0; i < pGaugeIndicator2->iNbImages; i ++)
687
pGaugeImage2 = &pGaugeIndicator2->pImageList[i];
688
_cairo_dock_load_gauge_image (pSourceContext, pGaugeImage2, iWidth, iHeight);
690
if (g_bUseOpenGL && pGaugeIndicator2->pImageNeedle)
692
_cairo_dock_load_gauge_needle (pSourceContext, pGaugeIndicator2, iWidth, iHeight);
697
////////////////////////////////////////////
698
/////////////// FREE GAUGE /////////////////
699
////////////////////////////////////////////
700
static void _cairo_dock_free_gauge_image(GaugeImage2 *pGaugeImage2, gboolean bFree)
702
if (pGaugeImage2 == NULL)
706
if(pGaugeImage2->pSvgHandle != NULL)
707
rsvg_handle_free (pGaugeImage2->pSvgHandle);
708
if(pGaugeImage2->pSurface != NULL)
709
cairo_surface_destroy (pGaugeImage2->pSurface);
710
if (pGaugeImage2->iTexture != 0)
711
glDeleteTextures (1, &pGaugeImage2->iTexture);
714
g_free (pGaugeImage2);
716
static void _cairo_dock_free_gauge_indicator(GaugeIndicator2 *pGaugeIndicator2)
718
if (pGaugeIndicator2 == NULL)
723
for (i = 0; i < pGaugeIndicator2->iNbImages; i ++)
725
_cairo_dock_free_gauge_image (&pGaugeIndicator2->pImageList[i], FALSE);
727
g_free (pGaugeIndicator2->pImageList);
729
_cairo_dock_free_gauge_image (pGaugeIndicator2->pImageNeedle, TRUE);
731
g_free (pGaugeIndicator2);
733
void cairo_dock_free_gauge2 (Gauge2 *pGauge)
739
_cairo_dock_free_gauge_image(pGauge->pImageBackground, TRUE);
740
_cairo_dock_free_gauge_image(pGauge->pImageForeground, TRUE);
743
for (pElement = pGauge->pIndicatorList; pElement != NULL; pElement = pElement->next)
745
_cairo_dock_free_gauge_indicator (pElement->data);
747
g_list_free (pGauge->pIndicatorList);
753
void cairo_dock_add_watermark_on_gauge2 (cairo_t *pSourceContext, Gauge2 *pGauge, gchar *cImagePath, double fAlpha)
755
g_return_if_fail (pGauge != NULL && cImagePath != NULL);
757
CairoDataRenderer *pRenderer = CAIRO_DATA_RENDERER (pGauge);
758
cairo_surface_t *pWatermarkSurface = cairo_dock_create_surface_for_icon (cImagePath, pSourceContext, pRenderer->iWidth/2, pRenderer->iHeight/2);
760
if (pGauge->pImageBackground == NULL)
762
pGauge->pImageBackground = g_new0 (GaugeImage2, 1);
763
pGauge->pImageBackground->sizeX = pRenderer->iWidth;
764
pGauge->pImageBackground->sizeY = pRenderer->iHeight;
766
pGauge->pImageBackground->pSurface = cairo_surface_create_similar (cairo_get_target (pSourceContext),
767
CAIRO_CONTENT_COLOR_ALPHA,
772
cairo_t *pCairoContext = cairo_create (pGauge->pImageBackground->pSurface);
773
cairo_set_operator (pCairoContext, CAIRO_OPERATOR_OVER);
775
cairo_set_source_surface (pCairoContext, pWatermarkSurface, pRenderer->iWidth/4, pRenderer->iHeight/4);
776
cairo_paint_with_alpha (pCairoContext, fAlpha);
778
cairo_destroy (pCairoContext);
780
cairo_surface_destroy (pWatermarkSurface);
783
//////////////////////////////////////////
784
/////////////// RENDERER /////////////////
785
//////////////////////////////////////////
786
Gauge2 *cairo_dock_new_gauge (void)
788
Gauge2 *pGauge = g_new0 (Gauge2, 1);
789
pGauge->dataRenderer.interface.new = cairo_dock_new_gauge;
790
pGauge->dataRenderer.interface.load = cairo_dock_load_gauge2;
791
pGauge->dataRenderer.interface.render = cairo_dock_render_gauge2;
792
pGauge->dataRenderer.interface.render_opengl = cairo_dock_render_gauge_opengl2;
793
pGauge->dataRenderer.interface.free = cairo_dock_free_gauge2;
794
pGauge->dataRenderer.interface.reload = cairo_dock_reload_gauge2;
798
/////////////////////////////////////////////////
799
/////////////// LIST OF THEMES /////////////////
800
/////////////////////////////////////////////////
801
/*GHashTable *cairo_dock_list_available_gauges (void)
803
gchar *cGaugeShareDir = g_strdup_printf ("%s/%s", CAIRO_DOCK_SHARE_DATA_DIR, CAIRO_DOCK_GAUGES_DIR);
804
gchar *cGaugeUserDir = g_strdup_printf ("%s/%s/%s", g_cCairoDockDataDir, CAIRO_DOCK_EXTRAS_DIR, CAIRO_DOCK_GAUGES_DIR);
805
GHashTable *pGaugeTable = cairo_dock_list_themes (cGaugeShareDir, cGaugeUserDir, CAIRO_DOCK_GAUGES_DIR);
807
g_free (cGaugeShareDir);
808
g_free (cGaugeUserDir);
812
const gchar *cairo_dock_get_gauge_key_value(gchar *cAppletConfFilePath, GKeyFile *pKeyFile, gchar *cGroupName, gchar *cKeyName, gboolean *bFlushConfFileNeeded, gchar *cDefaultThemeName)
814
gchar *cChosenThemeName = cairo_dock_get_string_key_value (pKeyFile, cGroupName, cKeyName, bFlushConfFileNeeded, cDefaultThemeName, NULL, NULL);
815
gchar *cGaugeShareDir = g_strdup_printf ("%s/%s", CAIRO_DOCK_SHARE_DATA_DIR, CAIRO_DOCK_GAUGES_DIR);
816
gchar *cGaugeUserDir = g_strdup_printf ("%s/%s/%s", g_cCairoDockDataDir, CAIRO_DOCK_EXTRAS_DIR, CAIRO_DOCK_GAUGES_DIR);
817
gchar *cGaugePath = cairo_dock_get_theme_path (cChosenThemeName, cGaugeShareDir, cGaugeUserDir, CAIRO_DOCK_GAUGES_DIR);
818
g_free (cGaugeShareDir);
819
g_free (cGaugeUserDir);
821
cd_debug ("Theme de la jauge : %s", cGaugePath);