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/>.
23
#include "applet-struct.h"
24
#include "applet-config.h"
25
#include "applet-digital.h"
28
#define CD_CLOCK_DATE_BUFFER_LENGTH 50
29
static char s_cDateBuffer[CD_CLOCK_DATE_BUFFER_LENGTH+1];
31
void cd_clock_configure_digital (CairoDockModuleInstance *myApplet) {
32
cd_debug ("%s", __func__);
34
GError *erreur = NULL;
35
GKeyFile *pKeyFile = g_key_file_new (); //On ouvre le fichier de conf
36
if (myConfig.cDigital == NULL)
37
myConfig.cDigital = g_strdup ("default");
39
gchar *cConfPath = g_strdup_printf ("%s/digital/%s/config", MY_APPLET_SHARE_DATA_DIR, myConfig.cDigital);
40
cd_debug ("Clock: Using %s digital theme", cConfPath);
41
g_key_file_load_from_file (pKeyFile, cConfPath, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &erreur);
44
cd_warning ("Attention : %s", erreur->message);
45
g_error_free (erreur);
50
myData.pDigitalClock.bSecondCapable = cairo_dock_get_boolean_key_value (pKeyFile, "configuration", "seconds", FALSE, FALSE, NULL, NULL);
51
myData.pDigitalClock.iFrameSpacing = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "framespacing", FALSE, 2, NULL, NULL);
53
myData.pDigitalClock.i12modeWidth = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12width", FALSE, 6, NULL, NULL);
54
myData.pDigitalClock.i12modeHeight = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12height", FALSE, 6, NULL, NULL);
55
myData.pDigitalClock.i12modeXOffset = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12offsetX", FALSE, 4, NULL, NULL);
56
myData.pDigitalClock.i12modeYOffset = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12offsetY", FALSE, 1, NULL, NULL);
57
myData.pDigitalClock.i12modeFrame = cairo_dock_get_integer_key_value (pKeyFile, "configuration", "12frame", FALSE, 4, NULL, NULL);
59
int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
60
//Avec secondes 3 frames, une par unités (hh : mm : ss)
61
//Sans secondes 4 frames, une par nombre (1|2:4|5)
62
for (i = 0; i < j; i++) {
63
gchar *cGroupName = g_strdup_printf ("frame_%d", i);
64
myData.pDigitalClock.pFrame[i].iWidth = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "width", FALSE, 4, NULL, NULL);
65
myData.pDigitalClock.pFrame[i].iHeight = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "height", FALSE, 4, NULL, NULL);
66
myData.pDigitalClock.pFrame[i].iXOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetX", FALSE, 0, NULL, NULL);
67
myData.pDigitalClock.pFrame[i].iYOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetY", FALSE, 0, NULL, NULL);
70
cGroupName = g_strdup_printf ("text_%d", i);
71
myData.pDigitalClock.pText[i].iXOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetX", FALSE, -1, NULL, NULL);
72
myData.pDigitalClock.pText[i].iYOffset = cairo_dock_get_integer_key_value (pKeyFile, cGroupName, "offsetY", FALSE, -1, NULL, NULL);
76
g_key_file_free (pKeyFile);
79
cd_clock_digital_load_frames (myApplet);
82
void cd_clock_digital_load_frames (CairoDockModuleInstance *myApplet) {
83
cd_debug ("%s", __func__);
85
int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
86
double fFrameWidth = 1, fFrameHeight = 1;
87
//Avec secondes 3 frames, une par unités (hh : mm : ss)
88
//Sans secondes 4 frames, une par nombre (1|2:4|5)
89
for (i = 0; i < j; i++) {
90
fFrameWidth = myIcon->fWidth / (double) myData.pDigitalClock.pFrame[i].iWidth;
91
cd_debug ("Clock: frame %d width %.02f (%.02f %d)", i+1, fFrameWidth, myIcon->fWidth, myData.pDigitalClock.pFrame[i].iWidth);
92
fFrameWidth = fFrameWidth - myData.pDigitalClock.iFrameSpacing;
93
fFrameHeight = myIcon->fHeight; /// myData.pDigitalClock.pFrame[i].iHeight;
94
double fImgW=0, fImgH=0;
96
if (myConfig.cDigital == NULL)
97
myConfig.cDigital = g_strdup ("default");
99
gchar *cImagePath = g_strdup_printf ("%s/digital/%s/frame_%d.svg", MY_APPLET_SHARE_DATA_DIR, myConfig.cDigital, i);
100
cd_debug ("Clock: Loading %s frame (%.02fx%.02f)", cImagePath, fFrameWidth, fFrameHeight);
101
cairo_t *pCairoContext = cairo_dock_create_context_from_window (myContainer);
102
myData.pDigitalClock.pFrame[i].pFrameSurface = cairo_dock_create_surface_from_image (cImagePath,
104
fFrameWidth, fFrameHeight,
108
cairo_destroy (pCairoContext);
113
void cd_clock_draw_frames (CairoDockModuleInstance *myApplet) {
114
cd_debug ("%s", __func__);
116
cairo_set_source_rgba (myDrawContext, 0., 0., 0., 0.);
117
cairo_set_operator (myDrawContext, CAIRO_OPERATOR_SOURCE);
118
cairo_paint (myDrawContext);
119
cairo_set_operator (myDrawContext, CAIRO_OPERATOR_OVER);
121
int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
123
//Avec secondes 3 frames, une par unité (hh | mm | ss)
124
//Sans secondes 4 frames, une par nombre (1|2 | 4|5)
125
for (i = 0; i < j; i++) {
126
fX = (myIcon->fWidth / j) * i + myData.pDigitalClock.pFrame[i].iXOffset;
127
fY = myData.pDigitalClock.pFrame[i].iYOffset;
128
cd_debug ("Clock: frame:%d ; fX:%.02f ; fY:%.02f", i+1, fX, fY);
129
cairo_set_source_surface (myDrawContext, myData.pDigitalClock.pFrame[i].pFrameSurface, fX, fY);
130
cairo_paint (myDrawContext);
133
CD_APPLET_REDRAW_MY_ICON;
136
void cd_clock_put_text_on_frames (CairoDockModuleInstance *myApplet, int width, int height, double fMaxScale, struct tm *pTime) {
137
cd_debug ("%s", __func__);
139
cairo_t *pSourceContext = myDrawContext;
140
GString *sFormat = g_string_new ("");
142
if (myConfig.b24Mode) {
143
if (myData.pDigitalClock.bSecondCapable)
144
g_string_printf (sFormat, "%%T");
146
g_string_printf (sFormat, " %%R");
149
if (myData.pDigitalClock.bSecondCapable)
150
g_string_printf (sFormat, "%%r%%s");
152
g_string_printf (sFormat, "%%I:%%M");
155
if (myConfig.iShowDate == CAIRO_DOCK_INFO_ON_ICON)
156
cd_clock_draw_date_on_frame (myApplet);
157
//Erf! Comment bien gérer ca ...
159
strftime (s_cDateBuffer, CD_CLOCK_DATE_BUFFER_LENGTH, sFormat->str, pTime);
160
g_string_free (sFormat, TRUE);
162
/// Attention : soit on passe un char a cd_clock_fill_text_surface(), soit un char *, il faut choisir.
163
/// Ne pas modifier le pointeur d'une chaine ! utiliser un pointeur secondaire qu'on balade dessus, sinon au free ca fait mal ;-)
164
gchar *cTime = g_strdup (s_cDateBuffer), *cT1 = NULL;
165
if (myData.pDigitalClock.bSecondCapable) { //On coupe aux ':' donc on arrive a 12|45|32
166
cT1 = g_strdup (cTime);
167
gchar *str = strchr (cT1, ':'); //On récupère [12]:45:32
170
cd_clock_fill_text_surface (myApplet, cT1, 0);
172
cT1 = g_strdup (cTime);
173
str = strrchr (cT1, ':'); //On récupère [12:45]:32
176
str = strchr (cT1, ':'); //On récupère 12:[45]:32
178
cd_clock_fill_text_surface (myApplet, str, 1);
180
cT1 = g_strdup (cTime);
181
str = strrchr (cT1, ':'); //On récupère 12:45:[32]
183
cd_clock_fill_text_surface (myApplet, str, 2);
185
else { //On coupe au ':' puis on sépare les chiffres donc on arrive a 1|2 | 4|5
186
cT1 = g_strdup (cTime);
187
gchar *str = strchr (cT1, ':'); //On récupère [12]:45
190
cd_clock_fill_text_surface (myApplet, *cT1, 0);
192
cd_clock_fill_text_surface (myApplet, *cT1, 1);
194
cT1 = g_strdup (cTime);
195
str = strrchr (cT1, ':'); //On récupère 12:[45]
197
cd_clock_fill_text_surface (myApplet, *str, 2);
199
cd_clock_fill_text_surface (myApplet, *str, 3);
204
int i = 0, j = (myData.pDigitalClock.bSecondCapable == TRUE ? 3 : 4);
205
for (i = 0; i < j; i++) {
206
//On dessine le texte dans les frames
207
cd_clock_draw_text_from_surface (myApplet, i);
210
//On ajoute le am/pm s'il le faut
211
if (! myConfig.b24Mode) {
212
if (pTime->tm_hour > 12)
213
cd_clock_draw_ampm (myApplet, "PM");
215
cd_clock_draw_ampm (myApplet, "AM");
219
void cd_clock_draw_ampm (CairoDockModuleInstance *myApplet, gchar *cMark) {
220
cd_debug ("Adding %s to the last frame", cMark);
221
//Ca s'est gérer dans la config.
222
//Vérifier les structures necessaires.
225
void cd_clock_draw_text_from_surface (CairoDockModuleInstance *myApplet, int iNumber) {
226
cd_debug ("Printing text #%d on corresponding frame", iNumber);
227
//Il faudra surment scale down la surface avant de cairo_print
228
//TODO prendre le code sur slider.
231
void cd_clock_fill_text_surface (CairoDockModuleInstance *myApplet, gchar *cStr, int iNumber) {
232
cd_debug ("Filling the #%d surface with %s", iNumber, cStr);
233
//Aucune idée de comment faire!
234
//TODO demander a fabounet des indices ici.
237
void cd_clock_draw_date_on_frame (CairoDockModuleInstance *myApplet) {
238
cd_debug ("Add date on frame");