~cairo-dock-team/ubuntu/oneiric/cairo-dock/2.3.0-3

« back to all changes in this revision

Viewing changes to src/cairo-dock-applet-facility.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100809232612-pocdxliaxjdetm37
Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

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
 
#include <math.h>
21
 
#include <string.h>
22
 
#include <stdlib.h>
23
 
#include <cairo.h>
24
 
 
25
 
#include "cairo-dock-load.h"
26
 
#include "cairo-dock-draw.h"
27
 
#include "cairo-dock-config.h"
28
 
#include "cairo-dock-launcher-factory.h"
29
 
#include "cairo-dock-surface-factory.h"
30
 
#include "cairo-dock-animations.h"
31
 
#include "cairo-dock-themes-manager.h"
32
 
#include "cairo-dock-keyfile-utilities.h"
33
 
#include "cairo-dock-applet-factory.h"
34
 
#include "cairo-dock-log.h"
35
 
#include "cairo-dock-dock-factory.h"
36
 
#include "cairo-dock-callbacks.h"
37
 
#include "cairo-dock-dock-manager.h"
38
 
#include "cairo-dock-draw-opengl.h"
39
 
#include "cairo-dock-icons.h"
40
 
#include "cairo-dock-container.h"
41
 
#include "cairo-dock-gui-manager.h"
42
 
#include "cairo-dock-applet-facility.h"
43
 
 
44
 
extern gchar *g_cCurrentThemePath;
45
 
extern gchar *g_cCairoDockDataDir;
46
 
extern gchar *g_cConfFile;
47
 
extern CairoDockImageBuffer g_pIconBackgroundBuffer;
48
 
///extern cairo_surface_t *g_pIconBackgroundImageSurface;
49
 
///extern double g_iIconBackgroundImageWidth, g_iIconBackgroundImageHeight;
50
 
 
51
 
extern gboolean g_bUseOpenGL;
52
 
 
53
 
 
54
 
void cairo_dock_set_icon_surface_full (cairo_t *pIconContext, cairo_surface_t *pSurface, double fScale, double fAlpha, Icon *pIcon, CairoContainer *pContainer)
55
 
{
56
 
        g_return_if_fail (cairo_status (pIconContext) == CAIRO_STATUS_SUCCESS);
57
 
        
58
 
        //\________________ On efface l'ancienne image.
59
 
        cairo_dock_erase_cairo_context (pIconContext);
60
 
        
61
 
        //\________________ On met le background de l'icone si necessaire
62
 
        if (pIcon != NULL &&
63
 
                pIcon->pIconBuffer != NULL &&
64
 
                g_pIconBackgroundBuffer.pSurface != NULL &&
65
 
                (! CAIRO_DOCK_IS_SEPARATOR (pIcon)/* && (myIcons.bBgForApplets || ! CAIRO_DOCK_IS_APPLET(pIcon))*/))
66
 
        {
67
 
                cd_message (">>> %s prendra un fond d'icone", pIcon->cName);
68
 
 
69
 
                cairo_save (pIconContext);
70
 
                int iWidth, iHeight;
71
 
                cairo_dock_get_icon_extent (pIcon, pContainer, &iWidth, &iHeight);
72
 
                cairo_scale(pIconContext,
73
 
                        iWidth / g_pIconBackgroundBuffer.iWidth,
74
 
                        iHeight / g_pIconBackgroundBuffer.iHeight);
75
 
                cairo_set_source_surface (pIconContext,
76
 
                        g_pIconBackgroundBuffer.pSurface,
77
 
                        0.,
78
 
                        0.);
79
 
                cairo_set_operator (pIconContext, CAIRO_OPERATOR_DEST_OVER);
80
 
                cairo_paint (pIconContext);
81
 
                cairo_restore (pIconContext);
82
 
        }
83
 
        
84
 
        //\________________ On applique la nouvelle image.
85
 
        if (pSurface != NULL && fScale > 0)
86
 
        {
87
 
                cairo_save (pIconContext);
88
 
                if (fScale != 1 && pIcon != NULL)
89
 
                {
90
 
                        int iWidth, iHeight;
91
 
                        cairo_dock_get_icon_extent (pIcon, pContainer, &iWidth, &iHeight);
92
 
                        cairo_translate (pIconContext, .5 * iWidth * (1 - fScale) , .5 * iHeight * (1 - fScale));
93
 
                        cairo_scale (pIconContext, fScale, fScale);
94
 
                }
95
 
                
96
 
                cairo_set_source_surface (
97
 
                        pIconContext,
98
 
                        pSurface,
99
 
                        0.,
100
 
                        0.);
101
 
                
102
 
                if (fAlpha != 1)
103
 
                        cairo_paint_with_alpha (pIconContext, fAlpha);
104
 
                else
105
 
                        cairo_paint (pIconContext);
106
 
                cairo_restore (pIconContext);
107
 
        }
108
 
        if (g_bUseOpenGL)
109
 
                cairo_dock_update_icon_texture (pIcon);
110
 
}
111
 
 
112
 
 
113
 
void cairo_dock_set_icon_surface_with_reflect (cairo_t *pIconContext, cairo_surface_t *pSurface, Icon *pIcon, CairoContainer *pContainer)
114
 
{
115
 
        cairo_dock_set_icon_surface_full (pIconContext, pSurface, 1., 1., pIcon, pContainer);
116
 
        
117
 
        cairo_dock_add_reflection_to_icon (pIconContext, pIcon, pContainer);
118
 
}
119
 
 
120
 
void cairo_dock_set_image_on_icon (cairo_t *pIconContext, const gchar *cImagePath, Icon *pIcon, CairoContainer *pContainer)
121
 
{
122
 
        if (cImagePath != pIcon->cFileName)
123
 
        {
124
 
                g_free (pIcon->cFileName);
125
 
                pIcon->cFileName = g_strdup (cImagePath);
126
 
        }
127
 
        int iWidth, iHeight;
128
 
        cairo_dock_get_icon_extent (pIcon, pContainer, &iWidth, &iHeight);
129
 
        cairo_surface_t *pImageSurface = cairo_dock_create_surface_for_icon (cImagePath,
130
 
                pIconContext,
131
 
                iWidth,
132
 
                iHeight);
133
 
        
134
 
        cairo_dock_set_icon_surface_with_reflect (pIconContext, pImageSurface, pIcon, pContainer);
135
 
        
136
 
        cairo_surface_destroy (pImageSurface);
137
 
}
138
 
 
139
 
void cairo_dock_set_icon_surface_with_bar (cairo_t *pIconContext, cairo_surface_t *pSurface, double fValue, Icon *pIcon, CairoContainer *pContainer)
140
 
{
141
 
        g_return_if_fail (cairo_status (pIconContext) == CAIRO_STATUS_SUCCESS);
142
 
        
143
 
        //\________________ On efface l'ancienne image.
144
 
        cairo_dock_erase_cairo_context (pIconContext);
145
 
        
146
 
        //\________________ On applique la nouvelle image.
147
 
        if (pSurface != NULL)
148
 
        {
149
 
                cairo_set_source_surface (
150
 
                        pIconContext,
151
 
                        pSurface,
152
 
                        0.,
153
 
                        0.);
154
 
                cairo_paint (pIconContext);
155
 
        }
156
 
        
157
 
        //\________________ On dessine la barre.
158
 
        cairo_dock_draw_bar_on_icon (pIconContext, fValue, pIcon, pContainer);
159
 
        
160
 
        if (g_bUseOpenGL)
161
 
                cairo_dock_update_icon_texture (pIcon);
162
 
}
163
 
 
164
 
void cairo_dock_draw_bar_on_icon (cairo_t *pIconContext, double fValue, Icon *pIcon, CairoContainer *pContainer)
165
 
{
166
 
        int iWidth, iHeight;
167
 
        cairo_dock_get_icon_extent (pIcon, pContainer, &iWidth, &iHeight);
168
 
        
169
 
        cairo_pattern_t *pGradationPattern = cairo_pattern_create_linear (0.,
170
 
                0.,
171
 
                iWidth,
172
 
                0.);  // de gauche a droite.
173
 
        g_return_if_fail (cairo_pattern_status (pGradationPattern) == CAIRO_STATUS_SUCCESS);
174
 
        
175
 
        cairo_pattern_set_extend (pGradationPattern, CAIRO_EXTEND_NONE);
176
 
        cairo_pattern_add_color_stop_rgba (pGradationPattern,
177
 
                (fValue >= 0 ? 0. : 1.),
178
 
                1.,
179
 
                0.,
180
 
                0.,
181
 
                1.);
182
 
        cairo_pattern_add_color_stop_rgba (pGradationPattern,
183
 
                (fValue >= 0 ? 1. : 0.),
184
 
                0.,
185
 
                1.,
186
 
                0.,
187
 
                1.);
188
 
        
189
 
        cairo_save (pIconContext);
190
 
        cairo_set_operator (pIconContext, CAIRO_OPERATOR_OVER);
191
 
        
192
 
        cairo_set_line_width (pIconContext, 6);
193
 
        cairo_set_line_cap (pIconContext, CAIRO_LINE_CAP_ROUND);
194
 
        
195
 
        cairo_move_to (pIconContext, 3, iHeight - 3);
196
 
        cairo_rel_line_to (pIconContext, (iWidth - 6) * fabs (fValue), 0);
197
 
        
198
 
        cairo_set_source (pIconContext, pGradationPattern);
199
 
        cairo_stroke (pIconContext);
200
 
        
201
 
        cairo_pattern_destroy (pGradationPattern);
202
 
        cairo_restore (pIconContext);
203
 
}
204
 
 
205
 
void cairo_dock_set_hours_minutes_as_quick_info (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, int iTimeInSeconds)
206
 
{
207
 
        int hours = iTimeInSeconds / 3600;
208
 
        int minutes = (iTimeInSeconds % 3600) / 60;
209
 
        if (hours != 0)
210
 
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dh%02d", hours, abs (minutes));
211
 
        else
212
 
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%dmn", minutes);
213
 
}
214
 
 
215
 
void cairo_dock_set_minutes_secondes_as_quick_info (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, int iTimeInSeconds)
216
 
{
217
 
        int minutes = iTimeInSeconds / 60;
218
 
        int secondes = iTimeInSeconds % 60;
219
 
        cd_debug ("%s (%d:%d)\n", __func__, minutes, secondes);
220
 
        if (minutes != 0)
221
 
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%d:%02d", minutes, abs (secondes));
222
 
        else
223
 
                cairo_dock_set_quick_info_full (pSourceContext, pIcon, pContainer, "%s0:%02d", (secondes < 0 ? "-" : ""), abs (secondes));
224
 
}
225
 
 
226
 
gchar *cairo_dock_get_human_readable_size (long long int iSizeInBytes)
227
 
{
228
 
        double fValue;
229
 
        if (iSizeInBytes >> 10 == 0)
230
 
        {
231
 
                return g_strdup_printf ("%dB", (int) iSizeInBytes);
232
 
        }
233
 
        else if (iSizeInBytes >> 20 == 0)
234
 
        {
235
 
                fValue = (double) (iSizeInBytes) / 1024.;
236
 
                return g_strdup_printf (fValue < 10 ? "%.1fK" : "%.0fK", fValue);
237
 
        }
238
 
        else if (iSizeInBytes >> 30 == 0)
239
 
        {
240
 
                fValue = (double) (iSizeInBytes >> 10) / 1024.;
241
 
                return g_strdup_printf (fValue < 10 ? "%.1fM" : "%.0fM", fValue);
242
 
        }
243
 
        else if (iSizeInBytes >> 40 == 0)
244
 
        {
245
 
                fValue = (double) (iSizeInBytes >> 20) / 1024.;
246
 
                return g_strdup_printf (fValue < 10 ? "%.1fG" : "%.0fG", fValue);
247
 
        }
248
 
        else
249
 
        {
250
 
                fValue = (double) (iSizeInBytes >> 30) / 1024.;
251
 
                return g_strdup_printf (fValue < 10 ? "%.1fT" : "%.0fT", fValue);
252
 
        }
253
 
}
254
 
 
255
 
void cairo_dock_set_size_as_quick_info (cairo_t *pSourceContext, Icon *pIcon, CairoContainer *pContainer, long long int iSizeInBytes)
256
 
{
257
 
        gchar *cSize = cairo_dock_get_human_readable_size (iSizeInBytes);
258
 
        cairo_dock_set_quick_info (pSourceContext, pIcon, pContainer, cSize);
259
 
        g_free (cSize);
260
 
}
261
 
 
262
 
 
263
 
 
264
 
gchar *cairo_dock_get_theme_path_for_module (const gchar *cAppletConfFilePath, GKeyFile *pKeyFile, const gchar *cGroupName, const gchar *cKeyName, gboolean *bFlushConfFileNeeded, const gchar *cDefaultThemeName, const gchar *cShareThemesDir, const gchar *cExtraDirName)
265
 
{
266
 
        gchar *cThemeName = cairo_dock_get_string_key_value (pKeyFile, cGroupName, cKeyName, bFlushConfFileNeeded, cDefaultThemeName, NULL, NULL);
267
 
        
268
 
        gchar *cUserThemesDir = (cExtraDirName != NULL ? g_strdup_printf ("%s/%s/%s", g_cCairoDockDataDir, CAIRO_DOCK_EXTRAS_DIR, cExtraDirName) : NULL);
269
 
        CairoDockThemeType iType = cairo_dock_extract_theme_type_from_name (cThemeName);
270
 
        gchar *cThemePath = cairo_dock_get_theme_path (cThemeName, cShareThemesDir, cUserThemesDir, cExtraDirName, iType);
271
 
        
272
 
        if (iType != CAIRO_DOCK_ANY_THEME)
273
 
        {
274
 
                g_key_file_set_string (pKeyFile, cGroupName, cKeyName, cThemeName);
275
 
                cairo_dock_write_keys_to_file (pKeyFile, cAppletConfFilePath);
276
 
        }
277
 
        g_free (cThemeName);
278
 
        g_free (cUserThemesDir);
279
 
        return cThemePath;
280
 
}
281
 
 
282
 
 
283
 
GtkWidget *cairo_dock_create_sub_menu (const gchar *cLabel, GtkWidget *pMenu, const gchar *cImage)
284
 
{
285
 
        GtkWidget *pMenuItem, *image, *pSubMenu = gtk_menu_new ();
286
 
        if (cImage == NULL)
287
 
        {
288
 
                pMenuItem = gtk_menu_item_new_with_label (cLabel);
289
 
        }
290
 
        else
291
 
        {
292
 
                pMenuItem = gtk_image_menu_item_new_with_label (cLabel);
293
 
                if (*cImage == '/')
294
 
                {
295
 
                        GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file_at_size (cImage, 24, 24, NULL);
296
 
                        image = gtk_image_new_from_pixbuf (pixbuf);
297
 
                        g_object_unref (pixbuf);
298
 
                }
299
 
                else
300
 
                {
301
 
                        image = gtk_image_new_from_stock (cImage, GTK_ICON_SIZE_MENU);
302
 
                }
303
 
                gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (pMenuItem), image);
304
 
        }
305
 
        gtk_menu_shell_append (GTK_MENU_SHELL (pMenu), pMenuItem); 
306
 
        gtk_menu_item_set_submenu (GTK_MENU_ITEM (pMenuItem), pSubMenu);
307
 
        return pSubMenu; 
308
 
}
309
 
 
310
 
 
311
 
 
312
 
//Utile pour jouer des fichiers son depuis le dock.
313
 
//A utiliser avec l'Objet UI 'u' dans les .conf
314
 
void cairo_dock_play_sound (const gchar *cSoundPath)
315
 
{
316
 
        cd_debug ("%s (%s)", __func__, cSoundPath);
317
 
        if (cSoundPath == NULL)
318
 
        {
319
 
                cd_warning ("No sound to play, halt.");
320
 
                return;
321
 
        }
322
 
        
323
 
        GError *erreur = NULL;
324
 
        gchar *cSoundCommand = NULL;
325
 
        if (g_file_test ("/usr/bin/play", G_FILE_TEST_EXISTS))
326
 
                cSoundCommand = g_strdup_printf("play \"%s\"", cSoundPath);
327
 
                
328
 
        else if (g_file_test ("/usr/bin/aplay", G_FILE_TEST_EXISTS))
329
 
                cSoundCommand = g_strdup_printf("aplay \"%s\"", cSoundPath);
330
 
        
331
 
        else if (g_file_test ("/usr/bin/paplay", G_FILE_TEST_EXISTS))
332
 
                cSoundCommand = g_strdup_printf("paplay \"%s\"", cSoundPath);
333
 
        
334
 
        cairo_dock_launch_command (cSoundCommand);
335
 
        
336
 
        g_free (cSoundCommand);
337
 
}
338
 
 
339
 
void cairo_dock_get_gnome_version (int *iMajor, int *iMinor, int *iMicro) {
340
 
        gchar *cContent = NULL;
341
 
        gsize length = 0;
342
 
        GError *erreur = NULL;
343
 
        g_file_get_contents ("/usr/share/gnome-about/gnome-version.xml", &cContent, &length, &erreur);
344
 
        
345
 
        if (erreur != NULL) {
346
 
                cd_warning (erreur->message);
347
 
                g_error_free (erreur);
348
 
                erreur = NULL;
349
 
                *iMajor = 0;
350
 
                *iMinor = 0;
351
 
                *iMicro = 0;
352
 
                return;
353
 
        }
354
 
        
355
 
        gchar **cLineList = g_strsplit (cContent, "\n", -1);
356
 
        gchar *cOneLine = NULL, *cMajor = NULL, *cMinor = NULL, *cMicro = NULL;
357
 
        int i, iMaj = 0, iMin = 0, iMic = 0;
358
 
        for (i = 0; cLineList[i] != NULL; i ++) {
359
 
                cOneLine = cLineList[i];
360
 
                if (*cOneLine == '\0')
361
 
                        continue;
362
 
                
363
 
                //Seeking for Major
364
 
                cMajor = g_strstr_len (cOneLine, -1, "<platform>");  //<platform>2</platform>
365
 
                if (cMajor != NULL) {
366
 
                        cMajor += 10; //On saute <platform>
367
 
                        gchar *str = strchr (cMajor, '<');
368
 
                        if (str != NULL)
369
 
                                *str = '\0'; //On bloque a </platform>
370
 
                        iMaj = atoi (cMajor);
371
 
                }
372
 
                else { //Gutsy xml's doesn't have <platform> but <major>
373
 
                        cMajor = g_strstr_len (cOneLine, -1, "<major>");  //<major>2</major>
374
 
                        if (cMajor != NULL) {
375
 
                                cMajor += 7; //On saute <major>
376
 
                                gchar *str = strchr (cMajor, '<');
377
 
                                if (str != NULL)
378
 
                                        *str = '\0'; //On bloque a </major>
379
 
                                iMaj = atoi (cMajor);
380
 
                        }
381
 
                }
382
 
                
383
 
                //Seeking for Minor
384
 
                cMinor = g_strstr_len (cOneLine, -1, "<minor>");  //<minor>22</minor>
385
 
                if (cMinor != NULL) {
386
 
                        cMinor += 7; //On saute <minor>
387
 
                        gchar *str = strchr (cMinor, '<');
388
 
                        if (str != NULL)
389
 
                                *str = '\0'; //On bloque a </minor>
390
 
                        iMin = atoi (cMinor);
391
 
                }
392
 
                
393
 
                //Seeking for Micro
394
 
                cMicro = g_strstr_len (cOneLine, -1, "<micro>");  //<micro>3</micro>
395
 
                if (cMicro != NULL) {
396
 
                        cMicro += 7; //On saute <micro>
397
 
                        gchar *str = strchr (cMicro, '<');
398
 
                        if (str != NULL)
399
 
                                *str = '\0'; //On bloque a </micro>
400
 
                        iMic = atoi (cMicro);
401
 
                }
402
 
                
403
 
                if (iMaj != 0 && iMin != 0 && iMic != 0)
404
 
                        break; //On s'enfou du reste
405
 
        }
406
 
        
407
 
        cd_debug ("Gnome Version %d.%d.%d", iMaj, iMin, iMic);
408
 
        
409
 
        *iMajor = iMaj;
410
 
        *iMinor = iMin;
411
 
        *iMicro = iMic;
412
 
        
413
 
        g_free (cContent);
414
 
        g_strfreev (cLineList);
415
 
}
416
 
 
417
 
void cairo_dock_pop_up_about_applet (GtkMenuItem *menu_item, CairoDockModuleInstance *pModuleInstance)
418
 
{
419
 
        cairo_dock_popup_module_instance_description (pModuleInstance);
420
 
}
421
 
 
422
 
 
423
 
void cairo_dock_open_module_config_on_demand (int iClickedButton, GtkWidget *pInteractiveWidget, CairoDockModuleInstance *pModuleInstance, CairoDialog *pDialog)
424
 
{
425
 
        if (iClickedButton == 0 || iClickedButton == -1)  // bouton OK ou touche Entree.
426
 
        {
427
 
                /**cairo_dock_build_main_ihm (g_cConfFile, FALSE);
428
 
                cairo_dock_present_module_instance_gui (pModuleInstance);*/
429
 
                cairo_dock_show_module_instance_gui (pModuleInstance, -1);
430
 
        }
431
 
}