4fcb845e4e7ba12b66a49203cea20f2ac1746ea4
[geeqie.git] / src / pan-view / pan-calendar.cc
1 /*
2  * Copyright (C) 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (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  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "pan-calendar.h"
23
24 #include <cmath>
25
26 #include "misc.h"
27 #include "pan-util.h"
28 #include "pan-view.h"
29 #include "pan-view-filter.h"
30 #include "pixbuf-util.h"
31
32 #define PAN_CAL_POPUP_COLOR 220, 220, 220
33 enum {
34         PAN_CAL_POPUP_ALPHA = 255,
35         PAN_CAL_POPUP_BORDER = 1
36 };
37 #define PAN_CAL_POPUP_BORDER_COLOR 0, 0, 0
38 #define PAN_CAL_POPUP_TEXT_COLOR 0, 0, 0
39
40 enum {
41         PAN_CAL_DAY_WIDTH = 100,
42         PAN_CAL_DAY_HEIGHT = 80
43 };
44
45 #define PAN_CAL_DAY_COLOR 255, 255, 255
46 enum {
47         PAN_CAL_DAY_ALPHA = 220,
48         PAN_CAL_DAY_BORDER = 2
49 };
50 #define PAN_CAL_DAY_BORDER_COLOR 0, 0, 0
51 #define PAN_CAL_DAY_TEXT_COLOR 0, 0, 0
52
53 #define PAN_CAL_MONTH_COLOR 255, 255, 255
54 enum {
55         PAN_CAL_MONTH_ALPHA = 200,
56         PAN_CAL_MONTH_BORDER = 4
57 };
58 #define PAN_CAL_MONTH_BORDER_COLOR 0, 0, 0
59 #define PAN_CAL_MONTH_TEXT_COLOR 0, 0, 0
60
61 enum {
62         PAN_CAL_DOT_SIZE = 3,
63         PAN_CAL_DOT_GAP = 2
64 };
65 #define PAN_CAL_DOT_COLOR 128, 128, 128
66 enum {
67         PAN_CAL_DOT_ALPHA = 128
68 };
69
70 #define PAN_CAL_DAY_OF_WEEK_COLOR 128, 128, 128
71
72 /*
73  *-----------------------------------------------------------------------------
74  * calendar
75  *-----------------------------------------------------------------------------
76  */
77
78 void pan_calendar_update(PanWindow *pw, PanItem *pi_day)
79 {
80         PanItem *pbox;
81         PanItem *pi;
82         GList *list;
83         GList *work;
84         gint x1;
85         gint y1;
86         gint x2;
87         gint y2;
88         gint x3;
89         gint y3;
90         gint x;
91         gint y;
92         gint w;
93         gint h;
94         gint grid;
95         gint column;
96
97         while ((pi = pan_item_find_by_key(pw, PAN_ITEM_NONE, "day_bubble"))) pan_item_remove(pw, pi);
98
99         if (!pi_day || pi_day->type != PAN_ITEM_BOX ||
100             !pi_day->key || strcmp(pi_day->key, "day") != 0) return;
101
102         list = pan_layout_intersect(pw, pi_day->x, pi_day->y, pi_day->width, pi_day->height);
103
104         work = list;
105         while (work)
106                 {
107                 PanItem *dot;
108                 GList *node;
109
110                 dot = static_cast<PanItem *>(work->data);
111                 node = work;
112                 work = work->next;
113
114                 if (dot->type != PAN_ITEM_BOX || !dot->fd ||
115                     !dot->key || strcmp(dot->key, "dot") != 0)
116                         {
117                         list = g_list_delete_link(list, node);
118                         }
119                 }
120
121         grid = static_cast<gint>(sqrt(g_list_length(list)) + 0.5);
122
123         x = pi_day->x + pi_day->width + 4;
124         y = pi_day->y;
125
126         pbox = pan_item_box_new(pw, nullptr, x, y, PAN_BOX_BORDER, PAN_BOX_BORDER,
127                                 PAN_CAL_POPUP_BORDER,
128                                 PAN_CAL_POPUP_COLOR, PAN_CAL_POPUP_ALPHA,
129                                 PAN_CAL_POPUP_BORDER_COLOR, PAN_CAL_POPUP_ALPHA);
130         pan_item_set_key(pbox, "day_bubble");
131
132         if (pi_day->fd)
133                 {
134                 PanItem *plabel;
135                 gchar *buf;
136
137                 buf = pan_date_value_string(pi_day->fd->date, PAN_DATE_LENGTH_WEEK);
138                 plabel = pan_item_text_new(pw, x, y, buf, static_cast<PanTextAttrType>(PAN_TEXT_ATTR_BOLD | PAN_TEXT_ATTR_HEADING),
139                                            PAN_BORDER_3,
140                                            PAN_CAL_POPUP_TEXT_COLOR, 255);
141                 pan_item_set_key(plabel, "day_bubble");
142                 g_free(buf);
143
144                 pan_item_size_by_item(pbox, plabel, 0);
145
146                 y += plabel->height;
147                 }
148
149         if (list)
150                 {
151                 column = 0;
152
153                 x += PAN_BOX_BORDER;
154                 y += PAN_BOX_BORDER;
155
156                 work = list;
157                 while (work)
158                         {
159                         PanItem *dot;
160
161                         dot = static_cast<PanItem *>(work->data);
162                         work = work->next;
163
164                         if (dot->fd)
165                                 {
166                                 PanItem *pimg;
167
168                                 pimg = pan_item_thumb_new(pw, file_data_ref(dot->fd), x, y);
169                                 pan_item_set_key(pimg, "day_bubble");
170
171                                 pan_item_size_by_item(pbox, pimg, PAN_BOX_BORDER);
172
173                                 column++;
174                                 if (column < grid)
175                                         {
176                                         x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
177                                         }
178                                 else
179                                         {
180                                         column = 0;
181                                         x = pbox->x + PAN_BOX_BORDER;
182                                         y += PAN_THUMB_SIZE + PAN_THUMB_GAP;
183                                         }
184                                 }
185                         }
186                 }
187
188         x1 = pi_day->x + pi_day->width - 8;
189         y1 = pi_day->y + 8;
190         x2 = pbox->x + 1;
191         y2 = pbox->y + MIN(42, pbox->height);
192         x3 = pbox->x + 1;
193         y3 = MAX(pbox->y, y2 - 30);
194         util_clip_triangle(x1, y1, x2, y2, x3, y3,
195                            &x, &y, &w, &h);
196
197         pi = pan_item_tri_new(pw, nullptr, x, y, w, h,
198                               x1, y1, x2, y2, x3, y3,
199                               PAN_CAL_POPUP_COLOR, PAN_CAL_POPUP_ALPHA);
200         pan_item_tri_border(pi, PAN_BORDER_1 | PAN_BORDER_3, PAN_CAL_POPUP_BORDER_COLOR, PAN_CAL_POPUP_ALPHA);
201         pan_item_set_key(pi, "day_bubble");
202         pan_item_added(pw, pi);
203
204         pan_item_box_shadow(pbox, PAN_SHADOW_OFFSET * 2, PAN_SHADOW_FADE * 2);
205         pan_item_added(pw, pbox);
206
207         pan_layout_resize(pw);
208 }
209
210 void pan_calendar_compute(PanWindow *pw, FileData *dir_fd, gint *width, gint *height)
211 {
212         GList *list;
213         GList *work;
214         gint x;
215         gint y;
216         time_t tc;
217         gint count;
218         gint day_max;
219         gint grid;
220         gint year = 0;
221         gint month = 0;
222         gint end_year = 0;
223         gint end_month = 0;
224         gint day_of_week;
225
226         list = pan_list_tree(dir_fd, SORT_NONE, TRUE, TRUE, pw->ignore_symlinks);
227         pan_filter_fd_list(&list, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
228
229         if (pw->cache_list && pw->exif_date_enable)
230                 {
231                 pw->cache_list = pan_cache_sort(pw->cache_list, SORT_NAME, TRUE, TRUE);
232                 list = filelist_sort(list, SORT_NAME, TRUE, TRUE);
233                 pan_cache_sync_date(pw, list);
234                 }
235
236         pw->cache_list = pan_cache_sort(pw->cache_list, SORT_TIME, TRUE, TRUE);
237         list = filelist_sort(list, SORT_TIME, TRUE, TRUE);
238
239         day_max = 0;
240         count = 0;
241         tc = 0;
242         work = list;
243         while (work)
244                 {
245                 FileData *fd;
246
247                 fd = static_cast<FileData *>(work->data);
248                 work = work->next;
249
250                 if (!pan_date_compare(fd->date, tc, PAN_DATE_LENGTH_DAY))
251                         {
252                         count = 0;
253                         tc = fd->date;
254                         }
255                 else
256                         {
257                         count++;
258                         if (day_max < count) day_max = count;
259                         }
260                 }
261
262         DEBUG_1("biggest day contains %d images", day_max);
263
264         grid = static_cast<gint>(sqrt(static_cast<gdouble>(day_max)) + 0.5) * (PAN_THUMB_SIZE + PAN_SHADOW_OFFSET * 2 + PAN_THUMB_GAP);
265
266         if (list)
267                 {
268                 auto fd = static_cast<FileData *>(list->data);
269
270                 year = pan_date_value(fd->date, PAN_DATE_LENGTH_YEAR);
271                 month = pan_date_value(fd->date, PAN_DATE_LENGTH_MONTH);
272                 }
273
274         work = g_list_last(list);
275         if (work)
276                 {
277                 auto fd = static_cast<FileData *>(work->data);
278                 end_year = pan_date_value(fd->date, PAN_DATE_LENGTH_YEAR);
279                 end_month = pan_date_value(fd->date, PAN_DATE_LENGTH_MONTH);
280                 }
281
282         *width = PAN_BOX_BORDER * 2;
283         *height = PAN_BOX_BORDER * 2;
284
285         x = PAN_BOX_BORDER;
286         y = PAN_BOX_BORDER;
287
288         work = list;
289         while (work && (year < end_year || (year == end_year && month <= end_month)))
290                 {
291                 PanItem *pi_month;
292                 PanItem *pi_text;
293                 PanItem *pi_day_number;
294                 gint day;
295                 gint days;
296                 gint col;
297                 gint row;
298                 time_t dt;
299                 gchar *buf;
300
301                 /* figure last second of this month */
302                 dt = pan_date_to_time((month == 12) ? year + 1 : year, (month == 12) ? 1 : month + 1, 1);
303                 dt -= 60 * 60 * 24;
304
305                 /* anything to show this month? */
306                 if (!pan_date_compare((static_cast<FileData *>(work->data))->date, dt, PAN_DATE_LENGTH_MONTH))
307                         {
308                         month ++;
309                         if (month > 12)
310                                 {
311                                 year++;
312                                 month = 1;
313                                 }
314                         continue;
315                         }
316
317                 days = pan_date_value(dt, PAN_DATE_LENGTH_DAY);
318                 dt = pan_date_to_time(year, month, 1);
319                 col = pan_date_value(dt, PAN_DATE_LENGTH_WEEK);
320                 col = col - (date_get_first_day_of_week() - 1);
321                 if (col < 0) col = col + 7;
322                 row = 1;
323
324                 x = PAN_BOX_BORDER;
325
326                 pi_month = pan_item_box_new(pw, nullptr, x, y, PAN_CAL_DAY_WIDTH * 7, PAN_CAL_DAY_HEIGHT / 4,
327                                             PAN_CAL_MONTH_BORDER,
328                                             PAN_CAL_MONTH_COLOR, PAN_CAL_MONTH_ALPHA,
329                                             PAN_CAL_MONTH_BORDER_COLOR, PAN_CAL_MONTH_ALPHA);
330                 buf = pan_date_value_string(dt, PAN_DATE_LENGTH_MONTH);
331                 pi_text = pan_item_text_new(pw, x, y, buf,
332                                             static_cast<PanTextAttrType>(PAN_TEXT_ATTR_BOLD | PAN_TEXT_ATTR_HEADING),
333                                             PAN_BORDER_3,
334                                             PAN_CAL_MONTH_TEXT_COLOR, 255);
335                 g_free(buf);
336                 pi_text->x = pi_month->x + (pi_month->width - pi_text->width) / 2;
337
338                 pi_month->height = pi_text->y + pi_text->height - pi_month->y;
339
340                 x = PAN_BOX_BORDER + col * PAN_CAL_DAY_WIDTH;
341                 y = pi_month->y + pi_month->height + PAN_BOX_BORDER;
342
343                 for (day = 1; day <= days; day++)
344                         {
345                         FileData *fd;
346                         PanItem *pi_day;
347                         gint dx;
348                         gint dy;
349                         gint n = 0;
350                         gchar fake_path[20];
351
352                         dt = pan_date_to_time(year, month, day);
353
354                         /*
355                          * Create a FileData entry that represents the given day.
356                          * It does not correspond to any real file
357                          */
358
359                         g_snprintf(fake_path, sizeof(fake_path), "//%04d-%02d-%02d", year, month, day);
360                         fd = file_data_new_no_grouping(fake_path);
361                         fd->date = dt;
362                         pi_day = pan_item_box_new(pw, fd, x, y, PAN_CAL_DAY_WIDTH, PAN_CAL_DAY_HEIGHT,
363                                                   PAN_CAL_DAY_BORDER,
364                                                   PAN_CAL_DAY_COLOR, PAN_CAL_DAY_ALPHA,
365                                                   PAN_CAL_DAY_BORDER_COLOR, PAN_CAL_DAY_ALPHA);
366                         pan_item_set_key(pi_day, "day");
367
368                         dx = x + PAN_CAL_DOT_GAP * 2;
369                         dy = y + PAN_CAL_DOT_GAP * 2;
370
371                         fd = static_cast<FileData *>((work) ? work->data : nullptr);
372                         while (fd && pan_date_compare(fd->date, dt, PAN_DATE_LENGTH_DAY))
373                                 {
374                                 PanItem *pi;
375
376                                 pi = pan_item_box_new(pw, fd, dx, dy, PAN_CAL_DOT_SIZE, PAN_CAL_DOT_SIZE,
377                                                       0,
378                                                       PAN_CAL_DOT_COLOR, PAN_CAL_DOT_ALPHA,
379                                                       0, 0, 0, 0);
380                                 pan_item_set_key(pi, "dot");
381
382                                 dx += PAN_CAL_DOT_SIZE + PAN_CAL_DOT_GAP;
383                                 if (dx + PAN_CAL_DOT_SIZE > pi_day->x + pi_day->width - PAN_CAL_DOT_GAP * 2)
384                                         {
385                                         dx = x + PAN_CAL_DOT_GAP * 2;
386                                         dy += PAN_CAL_DOT_SIZE + PAN_CAL_DOT_GAP;
387                                         }
388                                 if (dy + PAN_CAL_DOT_SIZE > pi_day->y + pi_day->height - PAN_CAL_DOT_GAP * 2)
389                                         {
390                                         /* must keep all dots within respective day even if it gets ugly */
391                                         dy = y + PAN_CAL_DOT_GAP * 2;
392                                         }
393
394                                 n++;
395
396                                 work = work->next;
397                                 fd = static_cast<FileData *>((work) ? work->data : nullptr);
398                                 }
399
400                         if (n > 0)
401                                 {
402                                 PanItem *pi;
403
404                                 pi_day->color_r = MAX(pi_day->color_r - 61 - n * 3, 80);
405                                 pi_day->color_g = pi_day->color_r;
406
407                                 buf = g_strdup_printf("( %d )", n);
408                                 pi = pan_item_text_new(pw, x, y, buf, PAN_TEXT_ATTR_NONE,
409                                                        PAN_BORDER_3,
410                                                        PAN_CAL_DAY_TEXT_COLOR, 255);
411                                 g_free(buf);
412
413                                 pi->x = pi_day->x + (pi_day->width - pi->width) / 2;
414                                 pi->y = pi_day->y + (pi_day->height - pi->height) / 2;
415                                 }
416
417                         buf = g_strdup_printf("%d", day);
418                         pi_day_number = pan_item_text_new(pw, x + 4, y + 4, buf, static_cast<PanTextAttrType>(PAN_TEXT_ATTR_BOLD | PAN_TEXT_ATTR_HEADING),
419                                           PAN_BORDER_3,
420                                           PAN_CAL_DAY_TEXT_COLOR, 255);
421                         g_free(buf);
422
423                         day_of_week = date_get_first_day_of_week() + col;
424                         if (day_of_week > 7) day_of_week = day_of_week - 7;
425
426                         buf = date_get_abbreviated_day_name(day_of_week);
427                         pan_item_text_new(pw, x + 4 + pi_day_number->width + 4, y + 4, buf, PAN_TEXT_ATTR_NONE,
428                                           PAN_BORDER_3,
429                                           PAN_CAL_DAY_OF_WEEK_COLOR, 255);
430                         g_free(buf);
431
432                         pan_item_size_coordinates(pi_day, PAN_BOX_BORDER, width, height);
433
434                         col++;
435                         if (col > 6)
436                                 {
437                                 col = 0;
438                                 row++;
439                                 x = PAN_BOX_BORDER;
440                                 y += PAN_CAL_DAY_HEIGHT;
441                                 }
442                         else
443                                 {
444                                 x += PAN_CAL_DAY_WIDTH;
445                                 }
446                         }
447
448                 if (col > 0) y += PAN_CAL_DAY_HEIGHT;
449                 y += PAN_BOX_BORDER * 2;
450
451                 month ++;
452                 if (month > 12)
453                         {
454                         year++;
455                         month = 1;
456                         }
457                 }
458
459         *width += grid;
460         *height = MAX(*height, grid + PAN_BOX_BORDER * 2 * 2);
461
462         g_list_free(list);
463 }
464 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */