2
* This file is a part of the Cairo-Dock project
4
* Copyright : (C) see the 'copyright' file.
5
* E-mail : see the 'copyright' file.
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.
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/>.
20
/**********************************************************************************
22
This file is a part of the cairo-dock clock applet,
23
released under the terms of the GNU General Public License.
25
Written by Rémy Robertson (for any bug report, please mail me to changfu@cairo-dock.org)
27
**********************************************************************************/
31
#include "applet-struct.h"
32
#include "applet-config.h"
33
#include "applet-digital.h"
36
#define CD_CLOCK_DATE_BUFFER_LENGTH 50
37
static char s_cDateBuffer[CD_CLOCK_DATE_BUFFER_LENGTH+1];
39
void cd_clock_configure_digital (CairoDockModuleInstance *myApplet) {
40
cd_debug ("%s", __func__);
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");
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);
52
cd_warning ("Attention : %s", erreur->message);
53
g_error_free (erreur);
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);
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);
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);
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);
84
g_key_file_free (pKeyFile);
87
cd_clock_digital_load_frames (myApplet);
90
void cd_clock_digital_load_frames (CairoDockModuleInstance *myApplet) {
91
cd_debug ("%s", __func__);
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;
104
if (myConfig.cDigital == NULL)
105
myConfig.cDigital = g_strdup ("default");
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
113
fFrameWidth, fFrameHeight,
117
cairo_destroy (pCairoContext);
122
void cd_clock_draw_frames (CairoDockModuleInstance *myApplet) {
123
cd_debug ("%s", __func__);
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);
130
int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
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);
142
CD_APPLET_REDRAW_MY_ICON;
145
void cd_clock_put_text_on_frames (CairoDockModuleInstance *myApplet, int width, int height, double fMaxScale, struct tm *pTime) {
146
cd_debug ("%s", __func__);
148
cairo_t *pSourceContext = myDrawContext;
149
GString *sFormat = g_string_new ("");
151
if (myConfig.b24Mode) {
152
if (myData.pDigitalClock.bSecondCapable)
153
g_string_printf (sFormat, "%%T");
155
g_string_printf (sFormat, " %%R");
158
if (myData.pDigitalClock.bSecondCapable)
159
g_string_printf (sFormat, "%%r%%s");
161
g_string_printf (sFormat, "%%I:%%M");
164
if (myConfig.iShowDate == CAIRO_DOCK_INFO_ON_ICON)
165
cd_clock_draw_date_on_frame (myApplet);
166
//Erf! Comment bien gérer ca ...
168
strftime (s_cDateBuffer, CD_CLOCK_DATE_BUFFER_LENGTH, sFormat->str, pTime);
169
g_string_free (sFormat, TRUE);
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
179
cd_clock_fill_text_surface (myApplet, cT1, 0);
181
cT1 = g_strdup (cTime);
182
str = strrchr (cT1, ':'); //On récupère [12:45]:32
185
str = strchr (cT1, ':'); //On récupère 12:[45]:32
187
cd_clock_fill_text_surface (myApplet, str, 1);
189
cT1 = g_strdup (cTime);
190
str = strrchr (cT1, ':'); //On récupère 12:45:[32]
192
cd_clock_fill_text_surface (myApplet, str, 2);
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
199
cd_clock_fill_text_surface (myApplet, *cT1, 0);
201
cd_clock_fill_text_surface (myApplet, *cT1, 1);
203
cT1 = g_strdup (cTime);
204
str = strrchr (cT1, ':'); //On récupère 12:[45]
206
cd_clock_fill_text_surface (myApplet, *str, 2);
208
cd_clock_fill_text_surface (myApplet, *str, 3);
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);
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");
224
cd_clock_draw_ampm (myApplet, "AM");
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.
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.
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.
246
void cd_clock_draw_date_on_frame (CairoDockModuleInstance *myApplet) {
247
g_print ("Add date on frame\n");