1 /*------------------------------------------------------
3 * wmCalendar (c)2003 Matthias Laabs
5 * mattlaabs@users.sourceforge.net
8 -----------------------------------------------------*/
10 #include "wmCalendar.h"
12 /*------------------------------------------------------
14 -----------------------------------------------------*/
15 int main(int argc
, char *argv
[])
19 ParseCMDLine(argc
, argv
);
20 if(get_debug())printf("debugging mode:\nstarting wmCalendar version %s\n", getVersion());
21 if(get_debug())printf("using libical version %s\n", ICAL_VERSION
);
22 dockapp_open_window("", "wmCalendar", 64, 64, argc
, argv
);
24 gtk_init(&argc
, &argv
);
27 /* Loop until we die */
29 if(n
== 0){ /* do updates only every COUNTERth time */
36 /* check for events every time */
46 /*------------------------------------------------------
48 -----------------------------------------------------*/
60 /*------------------------------------------------------
62 -----------------------------------------------------*/
64 XClassHint class_hints
;
65 XpmColorSymbol colors
[2];
68 class_hints
.res_name
="wmCalendar";
69 class_hints
.res_class
="wmCalendar";
70 /* Compute widths of months and digits */
71 for(i
= 0; i
< 12; ++i
)
72 for( j
= 0; j
< 6; ++j
)
73 xdMonth
[j
][i
] = xeMonth
[j
][i
] - xsMonth
[j
][i
] + 1;
74 for(i
= 0; i
< 12; ++i
)
75 for( j
= 0; j
< 2; ++j
)
76 xdYear
[j
][i
] = xeYear
[j
][i
] - xsYear
[j
][i
] + 1;
77 for(i
= 0; i
< MAXBUTTON
; ++i
)
78 xdButton
[i
] = xeButton
[i
] - xsButton
[i
] + 1;
80 dockapp_set_eventmask();
81 dockapp_xpm2pixmap( wmCalendar_master2_xpm
, &bg
[1], &mask
, colors
, 0);
82 dockapp_xpm2pixmap( wmCalendar_master_xpm
, &bg
[0], &mask
, colors
, 0);
83 pixmap
= dockapp_XCreatePixmap(64, 64);
84 dockapp_setshape(mask
, 0, 0);
86 XFreePixmap(display
, mask
);
87 dockapp_set_background(pixmap
);
93 /*------------------------------------------------------
95 -----------------------------------------------------*/
96 char* read_stream(char *s
, size_t size
, void *d
){
97 char *c
= fgets(s
, size
, (FILE*)d
);
102 /*------------------------------------------------------
104 -----------------------------------------------------*/
105 int getClickedDay(int bx
, int by
)
107 int dayofweek
, week
, day
;
108 dayofweek
= bx
/ 9 + 1;
109 week
= (by
- 20) / 7;
110 tgr
= get_civil(t
, calendartype
);
111 day
= dayofweek
+ 7 * week
- ((day_of_week(tgr
) - t
.day
+ 37 + get_start_of_week()) % 7);
112 if(day
<= days_in_month(t
.month
, t
.year
, calendartype
) && day
> 0){
116 tgr
= get_civil(tgr
, calendartype
);
124 /*------------------------------------------------------
126 -----------------------------------------------------*/
127 int getClickedButton(int bx
, int by
)
129 if(bx
< 12 && by
< 12) /* arrow left : month back*/
131 else if(bx
> 50 && by
< 12) /* arrow right : month forward*/
133 else if(bx
> 52 && by
> 56) /* show/hide buttons */
135 else if(showbuttons
){
136 if(bx
< 46 && bx
> 37 && by
> 55) /* toggle gregorian/persian/... */
138 else if(bx
< 52 && bx
> 46 && by
> 55) /* open settings dialog */
140 else if(bx
< 37 && bx
> 28 && by
> 55) /* toggle moonphase on/off */
142 else if(bx
< 28 && bx
> 19 && by
> 55) /* start calendar application */
150 /*------------------------------------------------------
152 -----------------------------------------------------*/
153 void buttonPress(int btype
, int bx
, int by
, long etime
){
155 if(get_debug() > 1) printf("push button %d\n", btype
);
158 case 1: /* leftmouse - button or mark */
159 switch ( getClickedButton(bx
, by
)){
160 case BT_BACK
: /* arrow left : month back*/
164 case BT_FORWARD
: /* arrow right : month forward*/
168 case BT_OPENCLOSE
: /* show/hide buttons */
169 showbuttons
= 1 - showbuttons
;
172 case BT_CALTYPE
: /* toggle gregorian/persian/... */
173 calendartype
= (calendartype
+ 1) % CALTYPES
;
176 case BT_MOON
:/* toggle moonphase on/off */
177 showmoon
= 1 - showmoon
;
180 case BT_SETTINGS
: /* open settings dialog */
184 case BT_APP
: /* start calendar application */
185 if(appDoubleClick
&& (etime
- appClickTime
< DBLCLKTIME
)){
186 applaunch
= malloc(250);
187 strcpy(applaunch
, (const char*)get_application());
188 strcat(applaunch
, " &");
195 appClickTime
= etime
;
199 default: /* no button clicked check for marking days*/
200 if(getClickedDay(bx
, by
)){
201 if(!icaltime_is_null_time(mark
))
202 mark
= icaltime_null_time();
209 case 4: /* mousewheel up : month back*/
213 case 5: /* mousewheel down : month forward*/
217 case 2: /* Middlemouse click : go to actual month*/
221 case 3: /* rightmouse click : show day entries*/
222 if(getClickedDay(bx
, by
))
227 told
.month
= 0; /* invalid month -> forces clearScreen at next draw*/
232 /*------------------------------------------------------
234 -----------------------------------------------------*/
235 void ParseCMDLine(int argc
, char *argv
[]){
238 for (i
= 1; i
< argc
; i
++){
239 if (!strcmp(argv
[i
], "--version") || !strcmp(argv
[i
], "-v")){
240 printf("wmCalendar %s\n", getVersion());
243 else if (!strcmp(argv
[i
], "--help") || !strcmp(argv
[i
], "-h")){
247 else if (!strcmp(argv
[i
], "--farsi") || !strcmp(argv
[i
], "-f"))
249 else if (!strcmp(argv
[i
], "--persian") || !strcmp(argv
[i
], "-p"))
251 else if (!strcmp(argv
[i
], "--debug") || !strcmp(argv
[i
], "-d"))
262 /*------------------------------------------------------
264 -----------------------------------------------------*/
267 printf("wmCalendar %s (c) 2003 Matthias Laabs (mattlaabs@users.sourceforge.net)\n", getVersion());
268 printf("This is free software under GPL!\n");
269 printf("\nOptions:");
270 printf("\n -h --help\n Show summary of options.");
271 printf("\n -v --version\n Show version of program.");
272 printf("\n -d --debug\n Show debugging info.");
273 printf("\n -f --farsi\n Show months and numbers in farsi.");
274 printf("\n -p --persian\n Changes to persian calendar.");
275 printf("\n\nUsage: \nLeft and right arrow or mousewheel change month.\n");
276 printf("Middle mouseclick goes back to current month.\n");
277 printf("Right mouseclick opens dayview.\nClick on Moon to toggle moonphase-view on/off\n");
278 printf("Click on screwdriver to open settings menu\nClick on 'G' to toggle between Gregorian, Persian and Islamic calendar\n");
279 printf("Doubleclick evolution/mozilla icon to start application\nClick on blue arrow to show/hide buttons\n");
280 printf("Leftclick a day to highlight for converting between calendars\n");
285 /*------------------------------------------------------
287 -----------------------------------------------------*/
290 long CurrentLocalTime
;
293 CurrentLocalTime
= time(CurrentTime
);
294 Time
= localtime(&CurrentLocalTime
);
295 t
.day
= Time
->tm_mday
;
296 t
.month
= Time
->tm_mon
+ 1;
297 t
.year
= Time
->tm_year
+ 1900;
301 if(calendartype
== 1)
302 t
= civil_persian(t
);
303 else if(calendartype
== 2)
304 t
= civil_islamic(t
);
305 /* set t to currently shown month and year */
306 t
.month
+= monthOffset
;
319 /*------------------------------------------------------
321 -----------------------------------------------------*/
322 void drawMonthYear(){
324 int Year1
, Year2
, Year3
, Year4
;
327 monthset
= calendartype
+ get_lang() * CALTYPES
;
328 /* calculate digits of the year */
330 Year3
= ((t
.year
- Year4
) / 10) % 10;
331 Year2
= ((t
.year
- Year4
- 10 * Year3
) / 100) % 10;
332 Year1
= (t
.year
- Year4
- 10 * Year3
- 100 * Year2
) / 1000;
334 xoff
= 30 - (xdMonth
[monthset
][t
.month
-1] + xdYear
[get_lang()][Year3
] + xdYear
[get_lang()][Year4
]) / 2;
337 dockapp_copyarea(bg
[1], pixmap
, xsMonth
[monthset
][t
.month
- 1], yMonth
[monthset
],
338 xdMonth
[monthset
][t
.month
- 1], ydMonth
, xoff
, yoff
);
339 xoff
+= xdMonth
[monthset
][t
.month
- 1] + 3;
341 /* draw Year 3rd and 4th digit */
342 dockapp_copyarea(bg
[1], pixmap
,xsYear
[get_lang()][Year3
], yYear
, xdYear
[get_lang()][Year3
], ydYear
, xoff
, yoff
);
343 xoff
+= xdYear
[get_lang()][Year3
];
344 dockapp_copyarea(bg
[1], pixmap
,xsYear
[get_lang()][Year4
], yYear
, xdYear
[get_lang()][Year4
], ydYear
, xoff
, yoff
);
345 dockapp_copy2window(pixmap
);
350 /*------------------------------------------------------
352 -----------------------------------------------------*/
358 xoff
= drawButton(ARROW_CLOSE
, xoff
, yoff
);
359 xoff
= drawButton(SETTINGS
, xoff
, yoff
);
360 xoff
= drawButton(MOON
+ showmoon
, xoff
, yoff
);
361 xoff
= drawButton(CALENDAR
+ calendartype
, xoff
, yoff
);
362 xoff
= drawButton(APPLAUNCH
+ getAppicon(), xoff
, yoff
);
365 drawButton(ARROW_OPEN
, xoff
, yoff
);
367 dockapp_copy2window(pixmap
);
372 /*------------------------------------------------------
374 -----------------------------------------------------*/
375 int drawButton(int btype
, int xoff
, int yoff
){
376 xoff
-= xdButton
[btype
];
377 dockapp_copyarea(bg
[0], pixmap
, xsButton
[btype
], yButton
[btype
], xdButton
[btype
],
378 ydButton
, xoff
, yoff
);
384 /*------------------------------------------------------
386 -----------------------------------------------------*/
390 int amount
, startday
;
397 tgr
= get_civil(tgr
, calendartype
);
398 amount
= days_in_month(t
.month
, t
.year
, calendartype
);
399 startday
= (day_of_week(tgr
) + get_start_of_week()) % 7;
400 for(i
= 1; i
<= amount
; i
++){
401 dayOfWeek
= (startday
+ i
) % 7;
402 if((dayOfWeek
== 0) && (i
!= 1))
403 yoff
+= 7; /* increase y-offset on Monday except it is day #1*/
407 tgr
= get_civil(tgr
, calendartype
);
410 moonphase
= moon(tgr
);
412 if(daysEqual(tgr
, mark
))
413 drawNumber(i
, yoff
, dayOfWeek
, 3, 0);
414 else if(moonphase
== 0 || !showmoon
) /* draw number */
415 drawNumber(i
, yoff
, dayOfWeek
, getDayType(tgr
), daysEqual(tgr
, today
));
416 else /* draw moonphase */
417 dockapp_copyarea(bg
[0], pixmap
, xsMoon
[moonphase
- 1], yMoon
, xdMoon
, ydMoon
,
418 dayOfWeek
* 9, yoff
);
421 dockapp_copy2window(pixmap
);
426 /*------------------------------------------------------
428 -----------------------------------------------------*/
429 void drawNumber(int number
,int yoff
, int dayOfWeek
, int type
, int today
){
432 offset
= yNumbers
+ 20 * type
+ 10 * today
;
433 if(number
< 10){ /* draw single digit numbers*/
434 dockapp_copyarea(bg
[0], pixmap
, xeNumbers
[get_lang()][1] - 5, offset
, 2, ydNumbers
,
435 dayOfWeek
* 9, yoff
);
436 dockapp_copyarea(bg
[0], pixmap
, xeNumbers
[get_lang()][1] - 5, offset
, 2, ydNumbers
,
437 dayOfWeek
* 9 + 7, yoff
);
438 dockapp_copyarea(bg
[0], pixmap
, xeNumbers
[get_lang()][number
], offset
, xdNumbers
, ydNumbers
,
439 dayOfWeek
* 9 + 2, yoff
);
441 else{ /* draw double digit numbers*/
442 digit2
= number
% 10;
443 digit1
= (number
- digit2
) / 10;
444 dockapp_copyarea(bg
[0], pixmap
, xeNumbers
[get_lang()][digit1
], offset
, xdNumbers
, ydNumbers
,
445 dayOfWeek
* 9, yoff
);
446 dockapp_copyarea(bg
[0], pixmap
, xeNumbers
[get_lang()][digit2
], offset
, xdNumbers
, ydNumbers
,
447 dayOfWeek
* 9 + 4, yoff
);
453 /*------------------------------------------------------
455 -----------------------------------------------------*/
458 if(dockapp_nextevent_or_timeout(&event
, 150)) {
460 switch (event
.type
) {
462 buttonPress(event
.xbutton
.button
,event
.xbutton
.x
, event
.xbutton
.y
, event
.xbutton
.time
);
472 /*------------------------------------------------------
474 -----------------------------------------------------*/
477 if(daysEqual(t
, told
))
479 /* only neccessary if day has changed*/
480 dockapp_copyarea(bg
[0], pixmap
, 0, 0, 64, 64, 0, 0);
481 dockapp_copyarea(bg
[0], pixmap
, 9 * (7 - get_start_of_week()),70 + get_lang() * 12, 63, 8, 0, 12);
482 dockapp_copy2window(pixmap
);
483 calendar(); /* reread calendardata */