fixed path to external icons
[shermanaquarium] / sherman-aquarium / shermans / date.c
1
2 #define NUM_FIGURES 10
3 #define NUM_MONTHS 12
4 #define NUM_DAYS 7
5
6 #include <string.h>
7 #include <stdio.h>
8 #include <time.h>
9 #include <gai/gai.h>
10 #include "aquarium.h"
11 #include "date.h"
12 #include "draw.h"
13
14 static SA_Image figures, months, days;
15 static Date_settings date_settings;
16 static AquariumData *ad;
17
18 static int month_width[]={16,16,20,18,20,16,16,20,17,18,19,18};
19
20 Date_settings *date_get_settings_ptr(void)
21 {
22     return &date_settings;
23 }
24
25
26 void date_init()
27 {
28     ad = aquarium_get_settings_ptr();
29
30     if(!date_settings.on)
31         return;
32
33     if(figures.image!=NULL)
34         date_exit();
35
36     load_image("clock/date/figures.png",&figures, NUM_FIGURES);
37
38     load_image("clock/date/weekdays.png",&days, NUM_DAYS);
39
40     load_image("clock/date/months.png", &months, NUM_MONTHS);
41
42
43     change_colour_to(date_settings.c.r,
44                      date_settings.c.g,
45                      date_settings.c.b,
46                      figures.image,
47                      figures.pixbuf, TRUE);
48
49
50     change_colour_to(date_settings.c.r,
51                      date_settings.c.g,
52                      date_settings.c.b,
53                      days.image,
54                      days.pixbuf, TRUE);
55
56     change_colour_to(date_settings.c.r,
57                      date_settings.c.g,
58                      date_settings.c.b,
59                      months.image,
60                      months.pixbuf,TRUE);
61
62
63 }
64
65 void date_exit(void)
66 {
67     if(figures.pixbuf!=NULL)
68         g_object_unref(figures.pixbuf);
69     if(days.pixbuf!=NULL)
70         g_object_unref(days.pixbuf);
71     if(months.pixbuf!=NULL)
72         g_object_unref(months.pixbuf);
73
74     memset(&figures,0,sizeof(SA_Image));
75     memset(&days,0,sizeof(SA_Image));
76     memset(&months,0,sizeof(SA_Image));
77
78 }
79
80 void date_update(int beforeorafter)
81 {
82     int x=0,y=0;
83     int wsize;
84     time_t now;
85     struct tm *mt;
86
87     if(!date_settings.on)
88         return;
89
90
91     if(beforeorafter==date_settings.draw) {
92         now = time(NULL);
93         mt = localtime(&now);
94
95         wsize=2*figures.width+days.width+month_width[mt->tm_mon]+1;
96
97
98         /* If before day 10 in month */
99         if(mt->tm_mday <10) wsize-=figures.width;
100
101     
102         switch(date_settings.vert)
103             {
104             case TOP:
105                 y=2;
106                 break;
107             case CENTER:
108                 y=(ad->ymax-figures.height)/2;
109                 break;
110             case BOTTOM:
111                 y=ad->ymax-figures.height-1;
112                 break;
113             }
114
115         switch(date_settings.horz)
116             {
117             case LEFT:
118                 x=0;
119                 break;
120             case CENTER:
121                 x=(ad->xmax-wsize)/2;
122                 break;
123             case RIGHT:
124                 x=ad->xmax-wsize;
125             }
126
127         draw_pic_alpha(days.image,
128                        days.width,
129                        days.height,
130                        x,y,mt->tm_wday,date_settings.c.alpha);
131         x+=days.width+2;
132     
133         if(mt->tm_mday >9){
134             draw_pic_alpha(figures.image,
135                            figures.width,
136                            figures.height,
137                            x,y,mt->tm_mday/10,date_settings.c.alpha);
138             x+=figures.width-1;
139         }
140         draw_pic_alpha(figures.image,
141                        figures.width,
142                        figures.height,
143                        x,y,mt->tm_mday%10,date_settings.c.alpha);
144
145         x+=figures.width;
146
147         draw_pic_alpha(months.image,
148                        months.width,
149                        months.height,
150                        x,y,mt->tm_mon,date_settings.c.alpha);
151     }
152
153 }