~ubuntu-branches/ubuntu/saucy/cairo-dock-plug-ins/saucy

« back to all changes in this revision

Viewing changes to clock/src/applet-digital.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
Import upstream version 2.0.8.1

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
/**********************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock clock applet, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Rémy Robertson (for any bug report, please mail me to changfu@cairo-dock.org)
 
26
 
 
27
**********************************************************************************/
 
28
#include <stdlib.h>
 
29
#include <math.h>
 
30
 
 
31
#include "applet-struct.h"
 
32
#include "applet-config.h"
 
33
#include "applet-digital.h"
 
34
 
 
35
 
 
36
#define CD_CLOCK_DATE_BUFFER_LENGTH 50
 
37
static char s_cDateBuffer[CD_CLOCK_DATE_BUFFER_LENGTH+1];
 
38
 
 
39
void cd_clock_configure_digital (CairoDockModuleInstance *myApplet) {
 
40
        cd_debug ("%s", __func__);
 
41
        
 
42
        GError *erreur = NULL;
 
43
        GKeyFile *pKeyFile = g_key_file_new (); //On ouvre le fichier de conf
 
44
        if (myConfig.cDigital == NULL)
 
45
                myConfig.cDigital = g_strdup ("default");
 
46
        
 
47
        gchar *cConfPath = g_strdup_printf ("%s/digital/%s/config", MY_APPLET_SHARE_DATA_DIR, myConfig.cDigital);
 
48
        cd_debug ("Clock: Using %s digital theme", cConfPath);
 
49
        g_key_file_load_from_file (pKeyFile, cConfPath, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &erreur);
 
50
        
 
51
        if (erreur != NULL) {
 
52
                cd_warning ("Attention : %s", erreur->message);
 
53
                g_error_free (erreur);
 
54
                erreur = NULL;
 
55
                return;
 
56
        }
 
57
        
 
58
        myData.pDigitalClock.bSecondCapable = cairo_dock_get_boolean_key_value (pKeyFile, "configuration", "seconds", FALSE, FALSE, NULL, NULL);
 
59
        myData.pDigitalClock.iFrameSpacing = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "framespacing", FALSE, 2, NULL, NULL);
 
60
        
 
61
        myData.pDigitalClock.i12modeWidth = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12width", FALSE, 6, NULL, NULL);
 
62
        myData.pDigitalClock.i12modeHeight = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12height", FALSE, 6, NULL, NULL);
 
63
        myData.pDigitalClock.i12modeXOffset = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12offsetX", FALSE, 4, NULL, NULL);
 
64
        myData.pDigitalClock.i12modeYOffset = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12offsetY", FALSE, 1, NULL, NULL);
 
65
        myData.pDigitalClock.i12modeFrame = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12frame", FALSE, 4, NULL, NULL);
 
66
        
 
67
        int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
 
68
        //Avec secondes 3 frames, une par unités (hh : mm : ss)
 
69
        //Sans secondes 4 frames, une par nombre (1|2:4|5)
 
70
        for (i = 0; i < j; i++) {
 
71
                gchar *cGroupName = g_strdup_printf ("frame_%d", i);
 
72
                myData.pDigitalClock.pFrame[i].iWidth = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "width", FALSE, 4, NULL, NULL);
 
73
                myData.pDigitalClock.pFrame[i].iHeight = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "height", FALSE, 4, NULL, NULL);
 
74
                myData.pDigitalClock.pFrame[i].iXOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetX", FALSE, 0, NULL, NULL);
 
75
                myData.pDigitalClock.pFrame[i].iYOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetY", FALSE, 0, NULL, NULL);
 
76
                g_free (cGroupName);
 
77
                
 
78
                cGroupName = g_strdup_printf ("text_%d", i);
 
79
                myData.pDigitalClock.pText[i].iXOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetX", FALSE, -1, NULL, NULL);
 
80
                myData.pDigitalClock.pText[i].iYOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetY", FALSE, -1, NULL, NULL);
 
81
                g_free (cGroupName);
 
82
        }
 
83
        
 
84
        g_key_file_free (pKeyFile);
 
85
        g_free (cConfPath);
 
86
        
 
87
        cd_clock_digital_load_frames (myApplet);
 
88
}
 
89
 
 
90
void cd_clock_digital_load_frames (CairoDockModuleInstance *myApplet) {
 
91
        cd_debug ("%s", __func__);
 
92
        
 
93
        int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
 
94
        double fFrameWidth = 1, fFrameHeight = 1;
 
95
        //Avec secondes 3 frames, une par unités (hh : mm : ss)
 
96
        //Sans secondes 4 frames, une par nombre (1|2:4|5)
 
97
        for (i = 0; i < j; i++) {
 
98
                fFrameWidth = myIcon->fWidth / (double) myData.pDigitalClock.pFrame[i].iWidth;
 
99
                cd_debug ("Clock: frame %d width %.02f (%.02f %d)", i+1, fFrameWidth, myIcon->fWidth, myData.pDigitalClock.pFrame[i].iWidth);
 
100
                fFrameWidth = fFrameWidth - myData.pDigitalClock.iFrameSpacing;
 
101
                fFrameHeight = myIcon->fHeight; /// myData.pDigitalClock.pFrame[i].iHeight;
 
102
                double fImgW=0, fImgH=0;
 
103
                
 
104
                if (myConfig.cDigital == NULL)
 
105
                        myConfig.cDigital = g_strdup ("default");
 
106
                        
 
107
                gchar *cImagePath = g_strdup_printf ("%s/digital/%s/frame_%d.svg", MY_APPLET_SHARE_DATA_DIR, myConfig.cDigital, i);
 
108
                cd_debug ("Clock: Loading %s frame (%.02fx%.02f)", cImagePath, fFrameWidth, fFrameHeight);
 
109
                cairo_t *pCairoContext = cairo_dock_create_context_from_window (myContainer);
 
110
                myData.pDigitalClock.pFrame[i].pFrameSurface = cairo_dock_create_surface_from_image (cImagePath,
 
111
                        pCairoContext,  // myDrawContext
 
112
                        1.,
 
113
                        fFrameWidth, fFrameHeight,
 
114
                        FALSE,
 
115
                        &fImgW, &fImgH,
 
116
                        NULL, NULL);
 
117
                cairo_destroy (pCairoContext);
 
118
                g_free (cImagePath);
 
119
        }
 
120
}
 
121
 
 
122
void cd_clock_draw_frames (CairoDockModuleInstance *myApplet) {
 
123
        cd_debug ("%s", __func__);
 
124
        
 
125
        cairo_set_source_rgba (myDrawContext, 0., 0., 0., 0.);
 
126
        cairo_set_operator (myDrawContext, CAIRO_OPERATOR_SOURCE);
 
127
        cairo_paint (myDrawContext);
 
128
        cairo_set_operator (myDrawContext, CAIRO_OPERATOR_OVER);
 
129
        
 
130
        int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
 
131
        double fX, fY;
 
132
        //Avec secondes 3 frames, une par unité (hh | mm | ss)
 
133
        //Sans secondes 4 frames, une par nombre (1|2 | 4|5)
 
134
        for (i = 0; i < j; i++) {
 
135
                fX = (myIcon->fWidth / j) * i + myData.pDigitalClock.pFrame[i].iXOffset;
 
136
                fY = myData.pDigitalClock.pFrame[i].iYOffset;
 
137
                cd_debug ("Clock: frame:%d ; fX:%.02f ; fY:%.02f", i+1, fX, fY);
 
138
                cairo_set_source_surface (myDrawContext, myData.pDigitalClock.pFrame[i].pFrameSurface, fX, fY);
 
139
                cairo_paint (myDrawContext);
 
140
        }
 
141
        
 
142
        CD_APPLET_REDRAW_MY_ICON;
 
143
}
 
144
 
 
145
void cd_clock_put_text_on_frames (CairoDockModuleInstance *myApplet, int width, int height, double fMaxScale, struct tm *pTime) {
 
146
        cd_debug ("%s", __func__);
 
147
        
 
148
        cairo_t *pSourceContext = myDrawContext;
 
149
        GString *sFormat = g_string_new ("");
 
150
        
 
151
        if (myConfig.b24Mode) {
 
152
                if (myData.pDigitalClock.bSecondCapable)
 
153
                        g_string_printf (sFormat, "%%T");
 
154
                else
 
155
                        g_string_printf (sFormat, " %%R");
 
156
        }
 
157
        else {
 
158
                if (myData.pDigitalClock.bSecondCapable)
 
159
                        g_string_printf (sFormat, "%%r%%s");
 
160
                else
 
161
                        g_string_printf (sFormat, "%%I:%%M");
 
162
        }
 
163
 
 
164
        if (myConfig.iShowDate == CAIRO_DOCK_INFO_ON_ICON)
 
165
                cd_clock_draw_date_on_frame (myApplet);
 
166
                //Erf! Comment bien gérer ca ...
 
167
        
 
168
        strftime (s_cDateBuffer, CD_CLOCK_DATE_BUFFER_LENGTH, sFormat->str, pTime);
 
169
        g_string_free (sFormat, TRUE);
 
170
        
 
171
        /// Attention : soit on passe un char a cd_clock_fill_text_surface(), soit un char *, il faut choisir.
 
172
        /// Ne pas modifier le pointeur d'une chaine ! utiliser un pointeur secondaire qu'on balade dessus, sinon au free ca fait mal ;-)
 
173
        gchar *cTime = g_strdup (s_cDateBuffer), *cT1 = NULL;
 
174
        if (myData.pDigitalClock.bSecondCapable) { //On coupe aux ':' donc on arrive a 12|45|32
 
175
                cT1 = g_strdup (cTime);
 
176
                gchar *str = strchr (cT1, ':'); //On récupère [12]:45:32
 
177
                if (str != NULL)
 
178
                        *str = '\0';
 
179
                cd_clock_fill_text_surface (myApplet, cT1, 0);
 
180
                
 
181
                cT1 = g_strdup (cTime);
 
182
                str = strrchr (cT1, ':'); //On récupère [12:45]:32
 
183
                if (str != NULL)
 
184
                        *str = '\0';
 
185
                str = strchr (cT1, ':'); //On récupère 12:[45]:32
 
186
                str++;
 
187
                cd_clock_fill_text_surface (myApplet, str, 1);
 
188
                
 
189
                cT1 = g_strdup (cTime);
 
190
                str = strrchr (cT1, ':'); //On récupère 12:45:[32]
 
191
                str++;
 
192
                cd_clock_fill_text_surface (myApplet, str, 2);
 
193
        }
 
194
        else { //On coupe au ':' puis on sépare les chiffres donc on arrive a 1|2 | 4|5
 
195
                cT1 = g_strdup (cTime);
 
196
                gchar *str = strchr (cT1, ':'); //On récupère [12]:45
 
197
                if (str != NULL)
 
198
                        *str = '\0';
 
199
                cd_clock_fill_text_surface (myApplet, *cT1, 0);
 
200
                cT1++; /// GLUPS !
 
201
                cd_clock_fill_text_surface (myApplet, *cT1, 1);
 
202
                
 
203
                cT1 = g_strdup (cTime);
 
204
                str = strrchr (cT1, ':'); //On récupère 12:[45]
 
205
                str++;
 
206
                cd_clock_fill_text_surface (myApplet, *str, 2);
 
207
                str++;
 
208
                cd_clock_fill_text_surface (myApplet, *str, 3);
 
209
        }
 
210
        g_free (cTime);
 
211
        g_free (cT1);
 
212
        
 
213
        int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
 
214
        for (i = 0; i < j; i++) {
 
215
                //On dessine le texte dans les frames
 
216
                cd_clock_draw_text_from_surface (myApplet, i);
 
217
        }
 
218
        
 
219
        //On ajoute le am/pm s'il le faut
 
220
        if (! myConfig.b24Mode) {
 
221
                if (pTime->tm_hour > 12)
 
222
                        cd_clock_draw_ampm (myApplet, "PM");
 
223
                else
 
224
                        cd_clock_draw_ampm (myApplet, "AM");
 
225
        }
 
226
}
 
227
 
 
228
void cd_clock_draw_ampm (CairoDockModuleInstance *myApplet, gchar *cMark) {
 
229
        g_print ("Adding %s to the last frame\n", cMark);
 
230
        //Ca s'est gérer dans la config.
 
231
        //Vérifier les structures necessaires.
 
232
}
 
233
 
 
234
void cd_clock_draw_text_from_surface (CairoDockModuleInstance *myApplet, int iNumber) {
 
235
        g_print ("Printing text #%d on corresponding frame\n", iNumber);
 
236
        //Il faudra surment scale down la surface avant de cairo_print
 
237
        //TODO prendre le code sur slider.
 
238
}
 
239
 
 
240
void cd_clock_fill_text_surface (CairoDockModuleInstance *myApplet, gchar *cStr, int iNumber) {
 
241
        g_print ("Filling the #%d surface with %s\n", iNumber, cStr);
 
242
        //Aucune idée de comment faire!
 
243
        //TODO demander a fabounet des indices ici.
 
244
}
 
245
 
 
246
void cd_clock_draw_date_on_frame (CairoDockModuleInstance *myApplet) {
 
247
        g_print ("Add date on frame\n");
 
248
}