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