~cairo-dock-team/cairo-dock-plug-ins/experimental

« back to all changes in this revision

Viewing changes to Doncky/src/applet-draw.c

  • Committer: nochka85
  • Date: 2010-01-31 14:38:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1490.
  • Revision ID: nochka85@nochka85-desktop-20100131143819-j6nno0cl7yxssexq
Doncky - 1st release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
//\________________ Add your name in the copyright file (and / or modify your name here)
 
21
 
 
22
#include <cairo-dock.h>
 
23
#include <math.h>
 
24
#include "applet-struct.h"
 
25
#include "applet-draw.h"
 
26
#include "applet-xml.h"
 
27
#include "applet-nvidia.h"
 
28
#include "applet-cpusage.h"
 
29
 
 
30
 
 
31
char* ltrim( char* str, const char* t )  // Couper tout depuis la gauche
 
32
{
 
33
        char* curStr = NULL;
 
34
 
 
35
        char look[ 256 ] = { 1, 0 };
 
36
        while( *t )
 
37
                look[ (unsigned char)*t++ ] = 1;
 
38
 
 
39
        curStr = str;
 
40
        while( *curStr && look[ *curStr ] )
 
41
                ++curStr;
 
42
        strcpy( str, curStr );
 
43
        
 
44
        return str; 
 
45
 
46
 
 
47
char* rtrim( char* str, const char* t )  // Couper tout depuis la droite
 
48
{
 
49
        char* curEnd = str, *end = str;
 
50
 
 
51
        char look[ 256 ] = { 1, 0 };
 
52
        while( *t )
 
53
                look[ (unsigned char)*t++ ] = 1;
 
54
 
 
55
        while( *end )
 
56
        {
 
57
                if ( !look[ *end ] )
 
58
                        curEnd = end + 1;
 
59
                ++end;
 
60
        }
 
61
        *curEnd = '\0';
 
62
 
 
63
        return str;
 
64
}
 
65
 
 
66
 
 
67
 
 
68
double _Ko_to_Mo (CairoDockModuleInstance *myApplet , double fValueInKo)
 
69
{
 
70
        fValueInKo = fValueInKo / 1024;
 
71
        return fValueInKo;
 
72
}
 
73
 
 
74
double _Ko_to_Go (CairoDockModuleInstance *myApplet , double fValueInKo)
 
75
{
 
76
        fValueInKo = _Ko_to_Mo (myApplet ,fValueInKo);
 
77
        fValueInKo = _Ko_to_Mo (myApplet ,fValueInKo);
 
78
        return fValueInKo;
 
79
}
 
80
 
 
81
 
 
82
gchar* _AdjustPrecision0 (CairoDockModuleInstance *myApplet , double fValue)
 
83
{
 
84
        gchar *cReturnValue;
 
85
        gint iInteger;
 
86
        gint iDecimal;
 
87
        
 
88
        iInteger = (int)fValue;
 
89
        iDecimal = (int)((fValue-iInteger)*10);
 
90
        
 
91
        if (iDecimal >= 5)
 
92
                iInteger++;
 
93
        
 
94
        cReturnValue =  g_strdup_printf ("%i", iInteger);
 
95
        
 
96
        return cReturnValue;
 
97
        g_free (cReturnValue);
 
98
}
 
99
 
 
100
gchar* _AdjustPrecision1 (CairoDockModuleInstance *myApplet , double fValue)
 
101
{
 
102
        gchar *cReturnValue;
 
103
        gint iInteger;
 
104
        gint iDecimal;
 
105
        gint iAfterDecimal;
 
106
        
 
107
        iAfterDecimal = (int)(((fValue*10)-(int)(fValue*10))*10);
 
108
        iInteger = (int)fValue;
 
109
        iDecimal = (int)((fValue-iInteger)*10);
 
110
        
 
111
        if (iAfterDecimal >= 5)
 
112
                iDecimal++;
 
113
        if (iDecimal == 10)
 
114
                cReturnValue =  g_strdup_printf ("%i.0", iInteger+1);
 
115
        else
 
116
                cReturnValue =  g_strdup_printf ("%i.%i", iInteger, iDecimal);
 
117
        
 
118
        return cReturnValue;
 
119
        g_free (cReturnValue);
 
120
}
 
121
 
 
122
gchar* _AdjustPrecision2 (CairoDockModuleInstance *myApplet , double fValue)
 
123
{
 
124
        gchar *cReturnValue;
 
125
        gint iInteger;
 
126
        gint iDecimal;
 
127
        gint iAfterDecimal;
 
128
        
 
129
        iAfterDecimal = (int)(((fValue*100)-(int)(fValue*100))*10);
 
130
        iInteger = (int)fValue;
 
131
        iDecimal = (int)((fValue-iInteger)*100);
 
132
        
 
133
        if (iAfterDecimal >= 5)
 
134
                iDecimal++;
 
135
        if (iDecimal == 100)
 
136
                cReturnValue =  g_strdup_printf ("%i.00", iInteger+1);
 
137
        else
 
138
        {
 
139
                if (iDecimal < 10)
 
140
                        cReturnValue =  g_strdup_printf ("%i.0%i", iInteger, iDecimal);
 
141
                else
 
142
                        cReturnValue =  g_strdup_printf ("%i.%i", iInteger, iDecimal);
 
143
        }
 
144
        
 
145
        return cReturnValue;
 
146
        g_free (cReturnValue);
 
147
}
 
148
 
 
149
 
 
150
void cd_doncky_periodic_refresh (CairoDockModuleInstance *myApplet)
 
151
{
 
152
        myData.pPeriodicRefreshTask = cairo_dock_new_task (0,
 
153
                (CairoDockGetDataAsyncFunc) cd_launch_command,
 
154
                (CairoDockUpdateSyncFunc) cd_retrieve_command_result,
 
155
                myApplet);
 
156
        cairo_dock_launch_task (myData.pPeriodicRefreshTask);
 
157
}
 
158
 
 
159
 
 
160
void cd_launch_command (CairoDockModuleInstance *myApplet)
 
161
{
 
162
        
 
163
        GList *it;
 
164
        TextZone *pTextZone;
 
165
        
 
166
        for (it = myData.pTextZoneList; it != NULL; it = it->next)
 
167
        {
 
168
                pTextZone = it->data;
 
169
                
 
170
                if (!pTextZone->bImgDraw && pTextZone->cImgPath != NULL) // L'image n'a pas encore été chargée
 
171
                {
 
172
                        
 
173
                        if (pTextZone->iWidth == 0 && pTextZone->iHeight ==0) // On n'a pas forcé le ratio
 
174
                        {
 
175
                                if (pTextZone->iImgSize == 0) // Oups !! -> Pas de taille de spécifié -> On en impose une !
 
176
                                        pTextZone->iImgSize = 50;
 
177
                                        
 
178
                                double fImgW=0, fImgH=0;
 
179
                                CairoDockLoadImageModifier iLoadingModifier = 0;  /// CAIRO_DOCK_FILL_SPACE
 
180
                                iLoadingModifier |= CAIRO_DOCK_KEEP_RATIO;
 
181
                                
 
182
                                cairo_t *pCairoContext = cairo_dock_create_context_from_container (myContainer);
 
183
                                pTextZone->pImgSurface = cairo_dock_create_surface_from_image (pTextZone->cImgPath,
 
184
                                        pCairoContext, // myDrawContext,
 
185
                                        1.,
 
186
                                        pTextZone->iImgSize, pTextZone->iImgSize,
 
187
                                        iLoadingModifier,                                       
 
188
                                        &fImgW, &fImgH,
 
189
                                        NULL, NULL);
 
190
                                cairo_destroy (pCairoContext);
 
191
                                                        
 
192
                                //\_______________ On garde l'aire de la surface/texture.                       
 
193
                                pTextZone->iWidth = (int)fImgW;
 
194
                                pTextZone->iHeight = (int)fImgH;
 
195
                        }
 
196
                                                
 
197
                        pTextZone->pImgSurface = cairo_dock_create_surface_for_icon (pTextZone->cImgPath, myDrawContext,
 
198
                                        pTextZone->iWidth, pTextZone->iHeight);
 
199
                                        
 
200
                        pTextZone->bImgDraw = TRUE; // L'image est désormais chargée -> On peut la dessiner
 
201
                }
 
202
                
 
203
                
 
204
                if (pTextZone->iRefresh != 0)
 
205
                                pTextZone->iTimer++;
 
206
                        
 
207
                
 
208
                if (pTextZone->bRefresh)
 
209
                {
 
210
                        if (pTextZone->cInternal == NULL) // C'est une commande bash !
 
211
                        {
 
212
                                // cd_debug ("Doncky-debug : ----------------------> JE RAFFRAICHIS LA COMMANDE `%s`", pTextZone->cCommand);
 
213
                                pTextZone->cResult = cairo_dock_launch_command_sync (pTextZone->cCommand);
 
214
                        }
 
215
                        else // C'est une commande interne !
 
216
                        {
 
217
                        
 
218
                                if (strcmp (pTextZone->cInternal, "testsm") == 0)
 
219
                                {
 
220
                                        cd_sysmonitor_get_nvidia_info (myApplet);
 
221
                                        
 
222
                                        pTextZone->cResult = g_strdup_printf ("myData.fCpuPercent : %i%% \nmyData.cpu_user : %lli \nmyData.cpu_user_nice : %lli \nmyData.cpu_system : %lli \nmyData.cpu_idle : %lli \nmyData.iNbCPU : %i \n"
 
223
                                                "myData.fRamPercent : %i%% \nmyData.ramUsed : %lli \nmyData.ramTotal : %lli \nmyData.ramCached : %lli \nmyData.ramFree : %lli \n"
 
224
                                                "myData.swapTotal : %lli \nmyData.swapFree : %lli \nmyData.fSwapPercent : %i%% \nmyData.swapUsed : %lli \n"
 
225
                                                "myData.cGPUName : %s \nmyData.iVideoRam : %iMB \nmyData.cDriverVersion : %s \nmyData.iGPUTemp : %i°C",
 
226
                                                (int)myData.fCpuPercent,                                                
 
227
                                                myData.cpu_user,
 
228
                                                myData.cpu_user_nice,
 
229
                                                myData.cpu_system,
 
230
                                                myData.cpu_idle,
 
231
                                                myData.iNbCPU,
 
232
                                                (int)myData.fRamPercent,
 
233
                                                myData.ramUsed,
 
234
                                                myData.ramTotal,
 
235
                                                myData.ramCached,
 
236
                                                myData.ramFree,
 
237
                                                myData.swapTotal,
 
238
                                                myData.swapFree,
 
239
                                                (int)myData.fSwapPercent,
 
240
                                                myData.swapUsed,
 
241
                                                myData.cGPUName,
 
242
                                                myData.iVideoRam,
 
243
                                                myData.cDriverVersion,
 
244
                                                myData.iGPUTemp);
 
245
                                }
 
246
                                                                                
 
247
                                else if (strcmp (pTextZone->cInternal, "cpuperc") == 0)
 
248
                                {
 
249
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fCpuPercent));
 
250
                                        
 
251
                                        if (atof(pTextZone->cResult) < 10)
 
252
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision1 (myApplet, myData.fCpuPercent));
 
253
                                        else
 
254
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fCpuPercent));
 
255
                                }
 
256
                                
 
257
                                else if (strcmp (pTextZone->cInternal, "cpuperc2f") == 0) // Restreint à 2 chiffres : de 00 à 99 !
 
258
                                {
 
259
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fCpuPercent));
 
260
                                        
 
261
                                        if (atof(pTextZone->cResult) < 10)
 
262
                                                pTextZone->cResult = g_strdup_printf ("0%s", _AdjustPrecision0 (myApplet, myData.fCpuPercent));
 
263
                                        else if (atof(pTextZone->cResult) == 100)
 
264
                                                pTextZone->cResult = g_strdup_printf ("99");
 
265
                                        else
 
266
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fCpuPercent));
 
267
                                }
 
268
                                        
 
269
                                else if (strcmp (pTextZone->cInternal, "memperc") == 0)
 
270
                                {
 
271
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fRamPercent));
 
272
                                        
 
273
                                        if (atof(pTextZone->cResult) < 10)
 
274
                                                pTextZone->cResult = g_strdup_printf ("0%s", _AdjustPrecision1 (myApplet, myData.fRamPercent));
 
275
                                        else if (atof(pTextZone->cResult) == 100)
 
276
                                                pTextZone->cResult = g_strdup_printf ("99");                                            
 
277
                                        else
 
278
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fRamPercent));
 
279
                                }
 
280
                                
 
281
                                else if (strcmp (pTextZone->cInternal, "memperc2f") == 0) // Restreint à 2 chiffres : de 00 à 99 !
 
282
                                {
 
283
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fRamPercent));
 
284
                                        
 
285
                                        if (atof(pTextZone->cResult) < 10)
 
286
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision1 (myApplet, myData.fRamPercent));
 
287
                                        else
 
288
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fRamPercent));
 
289
                                }
 
290
        
 
291
                                                                                
 
292
                                else if (strcmp (pTextZone->cInternal, "mem") == 0) // en Mo
 
293
                                {
 
294
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Mo(myApplet, myData.ramTotal /100. * myData.fRamPercent));
 
295
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, atof(pTextZone->cResult)));
 
296
                                }
 
297
                                
 
298
                                else if (strcmp (pTextZone->cInternal, "memg") == 0) // Identique à mem mais en Go
 
299
                                {
 
300
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Go(myApplet, myData.ramTotal /100. * myData.fRamPercent));
 
301
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision2 (myApplet, atof(pTextZone->cResult)));
 
302
                                }
 
303
 
 
304
                                else if (strcmp (pTextZone->cInternal, "memmax") == 0) // en Mo
 
305
                                {
 
306
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Mo(myApplet, myData.ramTotal));
 
307
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, atof(pTextZone->cResult)));
 
308
                                }
 
309
                                
 
310
                                else if (strcmp (pTextZone->cInternal, "memmaxg") == 0) // Identique à memmax mais en Go
 
311
                                {
 
312
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Go(myApplet, myData.ramTotal));
 
313
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision2 (myApplet, atof(pTextZone->cResult)));
 
314
                                }
 
315
                                
 
316
                                                                
 
317
                                else if (strcmp (pTextZone->cInternal, "swapperc") == 0)
 
318
                                {
 
319
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fSwapPercent));
 
320
                                        
 
321
                                        if (atof(pTextZone->cResult) < 10)
 
322
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision1 (myApplet, myData.fSwapPercent));
 
323
                                        else
 
324
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fSwapPercent));
 
325
                                }
 
326
                                
 
327
                                else if (strcmp (pTextZone->cInternal, "swapperc2f") == 0) // Restreint à 2 chiffres : de 00 à 99 !
 
328
                                {
 
329
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fSwapPercent));
 
330
                                        
 
331
                                        if (atof(pTextZone->cResult) < 10)
 
332
                                                pTextZone->cResult = g_strdup_printf ("0%s", _AdjustPrecision0 (myApplet, myData.fSwapPercent));
 
333
                                        else if (atof(pTextZone->cResult) == 100)
 
334
                                                pTextZone->cResult = g_strdup_printf ("99");
 
335
                                        else
 
336
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, myData.fSwapPercent));
 
337
                                }
 
338
                                
 
339
                                else if (strcmp (pTextZone->cInternal, "swap") == 0) // en Mo
 
340
                                {
 
341
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Mo(myApplet, myData.swapUsed));
 
342
                                        
 
343
                                        if (atof(pTextZone->cResult) < 10)
 
344
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision2 (myApplet, atof(pTextZone->cResult)));
 
345
                                        else if (atof(pTextZone->cResult) < 100)
 
346
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision1 (myApplet, atof(pTextZone->cResult)));
 
347
                                        else
 
348
                                                pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, atof(pTextZone->cResult)));
 
349
                                }
 
350
                                
 
351
                                else if (strcmp (pTextZone->cInternal, "swapg") == 0) // Identique à swap mais en Go
 
352
                                {
 
353
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Go(myApplet, myData.swapUsed));
 
354
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision2 (myApplet, atof(pTextZone->cResult)));
 
355
                                }
 
356
                                
 
357
                                else if (strcmp (pTextZone->cInternal, "swapmax") == 0) // en Mo
 
358
                                {
 
359
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Mo(myApplet, myData.swapTotal));
 
360
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision0 (myApplet, atof(pTextZone->cResult)));
 
361
                                }
 
362
                                
 
363
                                else if (strcmp (pTextZone->cInternal, "swapmaxg") == 0) // Identique à swapmax mais en Go
 
364
                                {
 
365
                                        pTextZone->cResult = g_strdup_printf ("%f", _Ko_to_Go(myApplet, myData.swapTotal));
 
366
                                        pTextZone->cResult = g_strdup_printf ("%s", _AdjustPrecision2 (myApplet, atof(pTextZone->cResult)));
 
367
                                }
 
368
                                
 
369
                                else if (strcmp (pTextZone->cInternal, "nvtemp") == 0)
 
370
                                        pTextZone->cResult = g_strdup_printf ("%i", myData.iGPUTemp);
 
371
                                        
 
372
                                else if (strcmp (pTextZone->cInternal, "nvname") == 0)
 
373
                                {
 
374
                                        cd_sysmonitor_get_nvidia_info (myApplet);                                       
 
375
                                        pTextZone->cResult = g_strdup_printf ("%s", myData.cGPUName);                                   
 
376
                                }
 
377
                                
 
378
                                else if (strcmp (pTextZone->cInternal, "nvdriver") == 0)
 
379
                                {
 
380
                                        cd_sysmonitor_get_nvidia_info (myApplet);                                       
 
381
                                        pTextZone->cResult = g_strdup_printf ("%s", myData.cDriverVersion);                                     
 
382
                                }
 
383
                                
 
384
                                else if (strcmp (pTextZone->cInternal, "nvram") == 0)
 
385
                                {
 
386
                                        cd_sysmonitor_get_nvidia_info (myApplet);                                       
 
387
                                        pTextZone->cResult = g_strdup_printf ("%i", myData.iVideoRam);                                  
 
388
                                }
 
389
                                
 
390
                                else if (strcmp (pTextZone->cInternal, "uptime") == 0)
 
391
                                {
 
392
                                        gchar *cUpTime = NULL, *cActivityTime = NULL;
 
393
                                        cd_sysmonitor_get_uptime (&cUpTime, &cActivityTime);            
 
394
                                        pTextZone->cResult = g_strdup_printf ("%s", cUpTime);                   
 
395
                                }
 
396
                        }                       
 
397
                }
 
398
        }       
 
399
}
 
400
 
 
401
void cd_retrieve_command_result (CairoDockModuleInstance *myApplet)
 
402
{
 
403
        GList *it;
 
404
        TextZone *pTextZone;
 
405
        
 
406
 
 
407
        for (it = myData.pTextZoneList; it != NULL; it = it->next)
 
408
        {
 
409
                pTextZone = it->data;
 
410
                        
 
411
                if (pTextZone->iRefresh != 0 || pTextZone->bRefresh)
 
412
                {
 
413
                        if (pTextZone->bRefresh && pTextZone->cResult != NULL)
 
414
                        {
 
415
                                pTextZone->cText = g_strdup_printf ("%s",pTextZone->cResult);
 
416
                        }
 
417
                                                    
 
418
                        if (pTextZone->iRefresh != 0 && pTextZone->iTimer >= pTextZone->iRefresh)
 
419
                        {
 
420
                                pTextZone->bRefresh = TRUE;
 
421
                                pTextZone->iTimer = 0; // On remet le timer à 0
 
422
                        }
 
423
                        else
 
424
                                pTextZone->bRefresh = FALSE;
 
425
                }
 
426
        }       
 
427
        cd_applet_update_my_icon (myApplet); // Quand tous les textes sont chargés, on peut dessiner
 
428
        myData.pPeriodicRefreshTask = NULL;
 
429
}
 
430
 
 
431
 
 
432
void cd_applet_draw_my_desklet (CairoDockModuleInstance *myApplet, int iWidth, int iHeight)
 
433
{
 
434
        if (iWidth < 20 || iHeight < 20)  // inutile de dessiner tant que le desklet n'a pas atteint sa taille definitive.
 
435
                return;
 
436
        cd_debug ("%s (%dx%d)", __func__, iWidth, iHeight);
 
437
        PangoLayout *pLayout = pango_cairo_create_layout (myDrawContext);
 
438
        PangoRectangle ink, log;
 
439
        
 
440
        // On efface la surface cairo actuelle
 
441
        cairo_dock_erase_cairo_context (myDrawContext); 
 
442
        
 
443
        // dessin du fond (optionnel).
 
444
        if (myConfig.bDisplayBackground)
 
445
        {
 
446
                cairo_save (myDrawContext);
 
447
                cairo_translate (myDrawContext,
 
448
                                .5*myConfig.iBorderThickness,
 
449
                                .5*myConfig.iBorderThickness);          
 
450
                cairo_pattern_t *pGradationPattern = cairo_pattern_create_linear (0.,
 
451
                        0.,
 
452
                        0.,
 
453
                        iHeight);
 
454
                cairo_pattern_add_color_stop_rgba (pGradationPattern,
 
455
                        0.,
 
456
                        myConfig.fBackgroundColor1[0],
 
457
                        myConfig.fBackgroundColor1[1],
 
458
                        myConfig.fBackgroundColor1[2],
 
459
                        myConfig.fBackgroundColor1[3]);
 
460
                cairo_pattern_add_color_stop_rgba (pGradationPattern,
 
461
                        1.,
 
462
                        myConfig.fBackgroundColor2[0],
 
463
                        myConfig.fBackgroundColor2[1],
 
464
                        myConfig.fBackgroundColor2[2],
 
465
                        myConfig.fBackgroundColor2[3]);
 
466
                cairo_set_source (myDrawContext, pGradationPattern);
 
467
                
 
468
                if (myConfig.iBackgroundRadius != 0)  // On a besoin d'un rayon
 
469
                {
 
470
                        cairo_dock_draw_rounded_rectangle (myDrawContext,
 
471
                                myConfig.iBackgroundRadius,
 
472
                                0.,
 
473
                                iWidth - myConfig.iBorderThickness - 2 * myConfig.iBackgroundRadius,
 
474
                                iHeight - myConfig.iBorderThickness);
 
475
                }
 
476
                else  // Il ne faut pas de rayon
 
477
                {
 
478
                        cairo_rectangle (myDrawContext,
 
479
                                0., 0.,
 
480
                                iWidth - myConfig.iBorderThickness,
 
481
                                iHeight - myConfig.iBorderThickness);
 
482
                }
 
483
                cairo_fill (myDrawContext);
 
484
                cairo_pattern_destroy (pGradationPattern);
 
485
                cairo_restore (myDrawContext);
 
486
        }
 
487
        
 
488
        
 
489
        
 
490
        
 
491
        
 
492
        
 
493
        // ################################################################################################################################################################
 
494
        // ############ DEBUT DU DESSIN DU TEXTE
 
495
        // ################################################################################################################################################################
 
496
        
 
497
        int iMargin = .5 * myConfig.iBorderThickness + (1. - sqrt (2) / 2) * myConfig.iBackgroundRadius;  // marge a gauche et au-dessus, pour ne pas mordre sur le coin arrondi.
 
498
        
 
499
        myData.fCurrentX = iMargin + myConfig.iTextMargin;  // position du curseur sur la ligne.
 
500
        myData.fCurrentY = iMargin + myConfig.iTextMargin;  // position de la ligne courante.
 
501
        
 
502
        GList *it = myData.pTextZoneList;
 
503
        TextZone *pTextZone;
 
504
        
 
505
        GList *it1;
 
506
        gboolean bFirstPass=TRUE;
 
507
 
 
508
        double fCurrentLineWidth = 0; // Dimension de la ligne complète (= avec plusieurs zones de textes)
 
509
        double fCurrentLineHeight = 0;
 
510
        gboolean bFirstTextInLine = TRUE;
 
511
                
 
512
                
 
513
                
 
514
        if (myData.pTextZoneList == NULL)
 
515
                return;
 
516
        
 
517
        it = myData.pTextZoneList;
 
518
        do
 
519
        {
 
520
                it1=it;
 
521
                do // boucle sur it de it1 jusqu'à retour chariot => line_width
 
522
                {
 
523
                        pTextZone = it->data;
 
524
                        
 
525
                        if (pTextZone->cFont == NULL || pTextZone->cFont =="") // Si aucune font -> on prend celle de la config
 
526
                                pTextZone->cFont = g_strdup_printf("%s", myConfig.cDefaultFont);
 
527
                        
 
528
                        
 
529
                        if (pTextZone->cText != NULL) // On récupère la largeur d'un espace pour caler le texte à droite
 
530
                        {
 
531
                                PangoFontDescription *fd = pango_font_description_from_string (pTextZone->cFont);
 
532
                                pango_layout_set_font_description (pLayout, fd);
 
533
                                pango_font_description_free (fd);
 
534
                                
 
535
                                pango_layout_set_text (pLayout, " ", -1);
 
536
                                pango_layout_get_pixel_extents (pLayout, &ink, &log);
 
537
 
 
538
                                pTextZone->iFontSize = (int)log.width;
 
539
                        }
 
540
 
 
541
                        
 
542
                        PangoFontDescription *fd = pango_font_description_from_string (pTextZone->cFont);
 
543
                        pango_layout_set_font_description (pLayout, fd);
 
544
                        pango_font_description_free (fd);
 
545
                        
 
546
                        pango_layout_set_text (pLayout, pTextZone->cText, -1);
 
547
                        pango_layout_get_pixel_extents (pLayout, &ink, &log);
 
548
                        
 
549
                        if (pTextZone->cText != NULL)
 
550
                                fCurrentLineWidth += log.width;
 
551
                        else if (pTextZone->bLimitedBar || pTextZone->bImgDraw)
 
552
                                fCurrentLineWidth += pTextZone->iWidth;
 
553
                        
 
554
                        
 
555
                        
 
556
                        // A voir comment définir l'alignement en Z
 
557
                        if (bFirstTextInLine)
 
558
                                fCurrentLineHeight = log.height;
 
559
                        else
 
560
                        {
 
561
                                if (log.height > fCurrentLineHeight)
 
562
                                        fCurrentLineHeight = log.height;                                
 
563
                        }
 
564
                        
 
565
                        
 
566
                        
 
567
                        
 
568
                        it = it->next;
 
569
                } while (it != NULL && ! pTextZone->bEndOfLine);
 
570
                
 
571
                
 
572
                // boucle sur it de it1 jusqu'à retour chariot => dessin
 
573
                it = it1;
 
574
                do
 
575
                {
 
576
        
 
577
                        pTextZone = it->data;
 
578
                        
 
579
                        if (pTextZone->cFont == NULL || pTextZone->cFont =="") // Si aucune font -> on prend celle de la config
 
580
                                pTextZone->cFont = g_strdup_printf("%s", myConfig.cDefaultFont);
 
581
                        
 
582
                        PangoFontDescription *fd = pango_font_description_from_string (pTextZone->cFont);
 
583
                        pango_layout_set_font_description (pLayout, fd);
 
584
                        pango_font_description_free (fd);
 
585
                        
 
586
                        
 
587
                        if (pTextZone->fTextColor[3]== 0 && pTextZone->cText != NULL) // Si aucune couleur alors qu'un texte est défini -> on prend celle de la config
 
588
                                cairo_set_source_rgba (myDrawContext, myConfig.fDefaultTextColor[0], myConfig.fDefaultTextColor[1], myConfig.fDefaultTextColor[2], myConfig.fDefaultTextColor[3]);
 
589
                        else // sinon, on utilise la couleur du .xml
 
590
                                cairo_set_source_rgba (myDrawContext, pTextZone->fTextColor[0], pTextZone->fTextColor[1], pTextZone->fTextColor[2], pTextZone->fTextColor[3]);
 
591
                        
 
592
                        myData.cCurrentText = g_strdup_printf ("%s",pTextZone->cText);
 
593
                        
 
594
                        pango_layout_set_text (pLayout, myData.cCurrentText, -1);
 
595
                        pango_layout_get_pixel_extents (pLayout, &ink, &log);
 
596
 
 
597
                        
 
598
                        if (bFirstTextInLine) // On calcule le décalage nécéssaire pour respecter l'alignement souhaité
 
599
                        {
 
600
                                int iWidth, iHeight;
 
601
                                CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
 
602
                                
 
603
                                if (strcmp (pTextZone->cAlign, "left") == 0)
 
604
                                        myData.fCurrentX = iMargin + myConfig.iTextMargin;
 
605
                                else if (strcmp (pTextZone->cAlign, "center") == 0)
 
606
                                {
 
607
                                        if (pTextZone->cImgPath != NULL)
 
608
                                                myData.fCurrentX = (iWidth/2) - (pTextZone->iWidth/2);
 
609
                                        else
 
610
                                                myData.fCurrentX = (iWidth/2)- (fCurrentLineWidth/2);
 
611
                                }
 
612
                                else if (strcmp (pTextZone->cAlign, "right") == 0)
 
613
                                {
 
614
                                        if (pTextZone->cImgPath != NULL)
 
615
                                                myData.fCurrentX = iWidth - iMargin - myConfig.iTextMargin - pTextZone->iWidth - pTextZone->iFontSize;
 
616
                                        else
 
617
                                                myData.fCurrentX = iWidth - iMargin - myConfig.iTextMargin - fCurrentLineWidth - pTextZone->iFontSize;
 
618
                                }
 
619
                        }
 
620
                        
 
621
                        if (pTextZone->bBar || pTextZone->bLimitedBar) // On dessine la barre
 
622
                        {
 
623
                                int value;
 
624
                                myData.cCurrentText = g_strdup_printf ("%s",pTextZone->cText);
 
625
                                
 
626
                                if (pTextZone->bBar)
 
627
                                {
 
628
                                        int iWidth, iHeight;
 
629
                                        CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
 
630
                                        pTextZone->iWidth =  iWidth - iMargin*2 - myConfig.iTextMargin - (myData.fCurrentX + 8);
 
631
                                }
 
632
                                
 
633
                                        
 
634
                                
 
635
                                int i;
 
636
                                for (i=0 ; i < 2 ; i++)
 
637
                                {
 
638
                                        if (i==0)
 
639
                                                value = (int)((atof(myData.cCurrentText) / 100)*pTextZone->iWidth);
 
640
                                        else
 
641
                                                value = pTextZone->iWidth;                                              
 
642
                                        
 
643
                                        // On dessine la valeur PUIS le cadre                                                   
 
644
                                        cairo_save (myDrawContext);
 
645
                                        
 
646
                                        if (pTextZone->fTextColor[3]== 0 && pTextZone->cText != NULL) // Si aucune couleur
 
647
                                                cairo_set_source_rgba (myDrawContext,
 
648
                                                        myConfig.fDefaultTextColor[0],
 
649
                                                        myConfig.fDefaultTextColor[1],
 
650
                                                        myConfig.fDefaultTextColor[2],
 
651
                                                        myConfig.fDefaultTextColor[3]);
 
652
                                        else
 
653
                                                cairo_set_source_rgba (myDrawContext,
 
654
                                                        pTextZone->fTextColor[0],
 
655
                                                        pTextZone->fTextColor[1],
 
656
                                                        pTextZone->fTextColor[2],
 
657
                                                        pTextZone->fTextColor[3]);
 
658
                                                                                        
 
659
                                        cairo_set_line_width (myDrawContext, 0.5);
 
660
                                        cairo_translate (myDrawContext,
 
661
                                                myData.fCurrentX + 8, // On décale légèrement la barre du texte
 
662
                                                myData.fCurrentY + log.height*4/5  - pTextZone->iHeight);
 
663
                                        cairo_dock_draw_rounded_rectangle (myDrawContext,
 
664
                                                0,
 
665
                                                0.,
 
666
                                                value,
 
667
                                                pTextZone->iHeight);
 
668
                                        
 
669
                                        if (i==0)
 
670
                                                cairo_fill (myDrawContext);
 
671
                                        else
 
672
                                                cairo_stroke (myDrawContext);
 
673
                                        cairo_restore (myDrawContext);                                  
 
674
                                }
 
675
                        }
 
676
                        else if (pTextZone->bImgDraw) // Il y a une image
 
677
                        {
 
678
                                if (pTextZone->pImgSurface != NULL)
 
679
                                {
 
680
                                        cairo_set_source_surface (myDrawContext, pTextZone->pImgSurface,
 
681
                                                        myData.fCurrentX,
 
682
                                                        myData.fCurrentY);
 
683
                                        cairo_paint (myDrawContext);
 
684
                                }                       
 
685
                        }
 
686
                        else // C'est un texte !
 
687
                        {
 
688
                                cairo_move_to (myDrawContext,
 
689
                                        myData.fCurrentX,
 
690
                                        myData.fCurrentY + (fCurrentLineHeight *4/5) - (log.height * 4/5)); // *4/5 pour se caler sur le bas du texte avec la font la plus grosse
 
691
                                pango_cairo_show_layout (myDrawContext, pLayout); // On dessine la ligne sur notre desklet
 
692
                        }
 
693
                                
 
694
                        if (pTextZone->bEndOfLine) // On passe à la ligne du dessous
 
695
                        {
 
696
                                if (pTextZone->bNextNewLine) // Sinon, on reste sur la même ligne -> Utile pour avoir 1 zone alignée à gauche et 1 zone alignée à droite par exemple ;-)
 
697
                                {
 
698
                                        if (pTextZone->bImgDraw)
 
699
                                                myData.fCurrentY += pTextZone->iHeight + myConfig.iSpaceBetweenLines;
 
700
                                        else
 
701
                                                myData.fCurrentY += log.height + myConfig.iSpaceBetweenLines;
 
702
                                }
 
703
                                //~ else if (pTextZone->bNextNewLine && (pTextZone->bBar || pTextZone->bLimitedBar) )
 
704
                                        //~ myData.fCurrentY += myConfig.iSpaceBetweenLines + pTextZone->iHeight;
 
705
                                                                
 
706
                                myData.fCurrentX = iMargin + myConfig.iTextMargin;
 
707
                                
 
708
                                bFirstTextInLine = TRUE;
 
709
                                fCurrentLineWidth = 0;
 
710
                        }
 
711
                        else // On laisse le curseur à la position actuelle
 
712
                        {
 
713
                                if (pTextZone->bBar || pTextZone->bLimitedBar)  // On prend en compte un éventuelle barre
 
714
                                        myData.fCurrentX += pTextZone->iWidth + 16 ;
 
715
                                else  // valable pour les textes et les images
 
716
                                {
 
717
                                        double w;
 
718
                                        pango_layout_get_pixel_extents (pLayout, &ink, &log);
 
719
                                
 
720
                                        w = log.width + log.x;
 
721
                                        myData.fCurrentX += w;
 
722
                                }
 
723
                                
 
724
                                bFirstTextInLine = FALSE;
 
725
                        } 
 
726
                        
 
727
                        it = it->next;
 
728
                } while (it != NULL && ! pTextZone->bEndOfLine);
 
729
 
 
730
        } while (it != NULL);
 
731
        
 
732
        // ################################################################################################################################################################
 
733
        // ############ FIN DU DESSIN DU TEXTE
 
734
        // ################################################################################################################################################################
 
735
        
 
736
        
 
737
        // dessin du cadre (optionnel).
 
738
        if (myConfig.bDisplayBackground)
 
739
        {
 
740
                cairo_save (myDrawContext);
 
741
                cairo_set_source_rgba (myDrawContext,
 
742
                        myConfig.fBorderColor[0],
 
743
                        myConfig.fBorderColor[1],
 
744
                        myConfig.fBorderColor[2],
 
745
                        myConfig.fBorderColor[3]);
 
746
                cairo_set_line_width (myDrawContext, myConfig.iBorderThickness);
 
747
                cairo_translate (myDrawContext,
 
748
                        .5*myConfig.iBorderThickness,
 
749
                        .5*myConfig.iBorderThickness);
 
750
                cairo_dock_draw_rounded_rectangle (myDrawContext,
 
751
                        myConfig.iBackgroundRadius,
 
752
                        0.,
 
753
                        iWidth - 2*myConfig.iBackgroundRadius - myConfig.iBorderThickness,
 
754
                        iHeight - myConfig.iBorderThickness);
 
755
                cairo_stroke (myDrawContext);
 
756
                cairo_restore (myDrawContext);
 
757
        }
 
758
        
 
759
        g_object_unref (pLayout);
 
760
        
 
761
        
 
762
        // on met a jour la texture OpenGL.
 
763
        if (CD_APPLET_MY_CONTAINER_IS_OPENGL)
 
764
        {
 
765
                cairo_dock_update_icon_texture (myIcon);
 
766
        }
 
767
}
 
768
 
 
769
 
 
770
void cd_applet_update_my_icon (CairoDockModuleInstance *myApplet)
 
771
{
 
772
        if (myDesklet)
 
773
        {
 
774
                // taille de la texture.
 
775
                int iWidth, iHeight;
 
776
                CD_APPLET_GET_MY_ICON_EXTENT (&iWidth, &iHeight);
 
777
                
 
778
                cd_applet_draw_my_desklet (myApplet, iWidth, iHeight);          
 
779
                
 
780
                CD_APPLET_REDRAW_MY_ICON;
 
781
        }
 
782
}