Fix include-what-you-use warnings
[geeqie.git] / src / pan-view / pan-folder.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-folder.h"
23
24 #include <cmath>
25
26 #include "filedata.h"
27 #include "pan-item.h"
28 #include "pan-types.h"
29 #include "pan-util.h"
30 #include "pan-view-filter.h"
31 #include "typedefs.h"
32
33 static void pan_flower_size(PanWindow *pw, gint *width, gint *height)
34 {
35         GList *work;
36         gint x1;
37         gint y1;
38         gint x2;
39         gint y2;
40
41         x1 = 0;
42         y1 = 0;
43         x2 = 0;
44         y2 = 0;
45
46         work = pw->list;
47         while (work)
48                 {
49                 PanItem *pi;
50
51                 pi = static_cast<PanItem *>(work->data);
52                 work = work->next;
53
54                 if (x1 > pi->x) x1 = pi->x;
55                 if (y1 > pi->y) y1 = pi->y;
56                 if (x2 < pi->x + pi->width) x2 = pi->x + pi->width;
57                 if (y2 < pi->y + pi->height) y2 = pi->y + pi->height;
58                 }
59
60         x1 -= PAN_BOX_BORDER;
61         y1 -= PAN_BOX_BORDER;
62         x2 += PAN_BOX_BORDER;
63         y2 += PAN_BOX_BORDER;
64
65         work = pw->list;
66         while (work)
67                 {
68                 PanItem *pi;
69
70                 pi = static_cast<PanItem *>(work->data);
71                 work = work->next;
72
73                 pi->x -= x1;
74                 pi->y -= y1;
75
76                 if (pi->type == PAN_ITEM_TRIANGLE && pi->data)
77                         {
78                         gint *coord;
79
80                         coord = static_cast<gint *>(pi->data);
81                         coord[0] -= x1;
82                         coord[1] -= y1;
83                         coord[2] -= x1;
84                         coord[3] -= y1;
85                         coord[4] -= x1;
86                         coord[5] -= y1;
87                         }
88                 }
89
90         if (width) *width = x2 - x1;
91         if (height) *height = y2 - y1;
92 }
93
94 struct FlowerGroup {
95         GList *items;
96         GList *children;
97         gint x;
98         gint y;
99         gint width;
100         gint height;
101
102         gdouble angle;
103         gint circumference;
104         gint diameter;
105 };
106
107 static void pan_flower_move(FlowerGroup *group, gint x, gint y)
108 {
109         GList *work;
110
111         work = group->items;
112         while (work)
113                 {
114                 PanItem *pi;
115
116                 pi = static_cast<PanItem *>(work->data);
117                 work = work->next;
118
119                 pi->x += x;
120                 pi->y += y;
121                 }
122
123         group->x += x;
124         group->y += y;
125 }
126
127 #define PI 3.14159265
128
129 static void pan_flower_position(FlowerGroup *group, FlowerGroup *parent,
130                                                              gint *result_x, gint *result_y)
131 {
132         gint x;
133         gint y;
134         gint radius;
135         gdouble a;
136
137         radius = parent->circumference / (2*PI);
138         radius = MAX(radius, parent->diameter / 2 + group->diameter / 2);
139
140         a = 2*PI * group->diameter / parent->circumference;
141
142         x = static_cast<gint>(static_cast<gdouble>(radius) * cos(parent->angle + a / 2));
143         y = static_cast<gint>(static_cast<gdouble>(radius) * sin(parent->angle + a / 2));
144
145         parent->angle += a;
146
147         x += parent->x;
148         y += parent->y;
149
150         x += parent->width / 2;
151         y += parent->height / 2;
152
153         x -= group->width / 2;
154         y -= group->height / 2;
155
156         *result_x = x;
157         *result_y = y;
158 }
159
160 static void pan_flower_build(PanWindow *pw, FlowerGroup *group, FlowerGroup *parent)
161 {
162         GList *work;
163         gint x;
164         gint y;
165
166         if (!group) return;
167
168         if (parent && parent->children)
169                 {
170                 pan_flower_position(group, parent, &x, &y);
171                 }
172         else
173                 {
174                 x = 0;
175                 y = 0;
176                 }
177
178         pan_flower_move(group, x, y);
179
180         if (parent)
181                 {
182                 PanItem *pi;
183                 gint px;
184                 gint py;
185                 gint gx;
186                 gint gy;
187                 gint x1;
188                 gint y1;
189                 gint x2;
190                 gint y2;
191
192                 px = parent->x + parent->width / 2;
193                 py = parent->y + parent->height / 2;
194
195                 gx = group->x + group->width / 2;
196                 gy = group->y + group->height / 2;
197
198                 x1 = MIN(px, gx);
199                 y1 = MIN(py, gy);
200
201                 x2 = MAX(px, gx + 5);
202                 y2 = MAX(py, gy + 5);
203
204                 pi = pan_item_tri_new(pw, nullptr, x1, y1, x2 - x1, y2 - y1,
205                                       px, py, gx, gy, gx + 5, gy + 5,
206                                       255, 40, 40, 128);
207                 pan_item_tri_border(pi, PAN_BORDER_1 | PAN_BORDER_3,
208                                     255, 0, 0, 128);
209                 }
210
211         pw->list = g_list_concat(group->items, pw->list);
212         group->items = nullptr;
213
214         group->circumference = 0;
215         work = group->children;
216         while (work)
217                 {
218                 FlowerGroup *child;
219
220                 child = static_cast<FlowerGroup *>(work->data);
221                 work = work->next;
222
223                 group->circumference += child->diameter;
224                 }
225
226         work = g_list_last(group->children);
227         while (work)
228                 {
229                 FlowerGroup *child;
230
231                 child = static_cast<FlowerGroup *>(work->data);
232                 work = work->prev;
233
234                 pan_flower_build(pw, child, group);
235                 }
236
237         g_list_free(group->children);
238         g_free(group);
239 }
240
241 static FlowerGroup *pan_flower_group(PanWindow *pw, FileData *dir_fd, gint x, gint y)
242 {
243         FlowerGroup *group;
244         GList *f;
245         GList *d;
246         GList *work;
247         PanItem *pi_box;
248         gint x_start;
249         gint y_height;
250         gint grid_size;
251         gint grid_count;
252
253         if (!filelist_read(dir_fd, &f, &d)) return nullptr;
254         if (!f && !d) return nullptr;
255
256         f = filelist_sort(f, SORT_NAME, TRUE, TRUE);
257         d = filelist_sort(d, SORT_NAME, TRUE, TRUE);
258
259         pan_filter_fd_list(&f, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
260
261         pi_box = pan_item_text_new(pw, x, y, dir_fd->path, PAN_TEXT_ATTR_NONE,
262                                    PAN_BORDER_3,
263                                    PAN_TEXT_COLOR, 255);
264
265         y += pi_box->height;
266
267         pi_box = pan_item_box_new(pw, file_data_ref(dir_fd),
268                                   x, y,
269                                   PAN_BOX_BORDER * 2, PAN_BOX_BORDER * 2,
270                                   PAN_BOX_OUTLINE_THICKNESS,
271                                   PAN_BOX_COLOR, PAN_BOX_ALPHA,
272                                   PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA);
273
274         x += PAN_BOX_BORDER;
275         y += PAN_BOX_BORDER;
276
277         grid_size = static_cast<gint>(sqrt(g_list_length(f)) + 0.9);
278         grid_count = 0;
279         x_start = x;
280         y_height = y;
281
282         work = f;
283         while (work)
284                 {
285                 FileData *fd;
286                 PanItem *pi;
287
288                 fd = static_cast<FileData *>(work->data);
289                 work = work->next;
290
291                 if (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE)
292                         {
293                         pi = pan_item_image_new(pw, fd, x, y, 10, 10);
294                         x += pi->width + PAN_THUMB_GAP;
295                         if (pi->height > y_height) y_height = pi->height;
296                         }
297                 else
298                         {
299                         pi = pan_item_thumb_new(pw, fd, x, y);
300                         x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
301                         y_height = PAN_THUMB_SIZE;
302                         }
303
304                 grid_count++;
305                 if (grid_count >= grid_size)
306                         {
307                         grid_count = 0;
308                         x = x_start;
309                         y += y_height + PAN_THUMB_GAP;
310                         y_height = 0;
311                         }
312
313                 pan_item_size_by_item(pi_box, pi, PAN_BOX_BORDER);
314                 }
315
316         group = g_new0(FlowerGroup, 1);
317         group->items = pw->list;
318         pw->list = nullptr;
319
320         group->width = pi_box->width;
321         group->height = pi_box->y + pi_box->height;
322         group->diameter = static_cast<gint>(hypot(group->width, group->height));
323
324         group->children = nullptr;
325
326         work = d;
327         while (work)
328                 {
329                 FileData *fd;
330                 FlowerGroup *child;
331
332                 fd = static_cast<FileData *>(work->data);
333                 work = work->next;
334
335                 if (!pan_is_ignored(fd->path, pw->ignore_symlinks))
336                         {
337                         child = pan_flower_group(pw, fd, 0, 0);
338                         if (child) group->children = g_list_prepend(group->children, child);
339                         }
340                 }
341
342         if (!f && !group->children)
343                 {
344                 g_list_free_full(group->items, reinterpret_cast<GDestroyNotify>(pan_item_free));
345                 g_free(group);
346                 group = nullptr;
347                 }
348
349         g_list_free(f);
350         filelist_free(d);
351
352         return group;
353 }
354
355 void pan_flower_compute(PanWindow *pw, FileData *dir_fd,
356                         gint *width, gint *height,
357                         gint *scroll_x, gint *scroll_y)
358 {
359         FlowerGroup *group;
360         GList *list;
361
362         group = pan_flower_group(pw, dir_fd, 0, 0);
363         pan_flower_build(pw, group, nullptr);
364
365         pan_flower_size(pw, width, height);
366
367         list = pan_item_find_by_fd(pw, PAN_ITEM_BOX, dir_fd, FALSE, FALSE);
368         if (list)
369                 {
370                 auto pi = static_cast<PanItem *>(list->data);
371                 *scroll_x = pi->x + pi->width / 2;
372                 *scroll_y = pi->y + pi->height / 2;
373                 }
374         g_list_free(list);
375 }
376
377 static void pan_folder_tree_path(PanWindow *pw, FileData *dir_fd,
378                                  gint *x, gint *y, gint *level,
379                                  PanItem *parent,
380                                  gint *width, gint *height)
381 {
382         GList *f;
383         GList *d;
384         GList *work;
385         PanItem *pi_box;
386         gint y_height = 0;
387
388         if (!filelist_read(dir_fd, &f, &d)) return;
389         if (!f && !d) return;
390
391         f = filelist_sort(f, SORT_NAME, TRUE, TRUE);
392         d = filelist_sort(d, SORT_NAME, TRUE, TRUE);
393
394         pan_filter_fd_list(&f, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
395
396         *x = PAN_BOX_BORDER + ((*level) * MAX(PAN_BOX_BORDER, PAN_THUMB_GAP));
397
398         pi_box = pan_item_text_new(pw, *x, *y, dir_fd->path, PAN_TEXT_ATTR_NONE,
399                                    PAN_BORDER_3,
400                                    PAN_TEXT_COLOR, 255);
401
402         *y += pi_box->height;
403
404         pi_box = pan_item_box_new(pw, file_data_ref(dir_fd),
405                                   *x, *y,
406                                   PAN_BOX_BORDER, PAN_BOX_BORDER,
407                                   PAN_BOX_OUTLINE_THICKNESS,
408                                   PAN_BOX_COLOR, PAN_BOX_ALPHA,
409                                   PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA);
410
411         *x += PAN_BOX_BORDER;
412         *y += PAN_BOX_BORDER;
413
414         work = f;
415         while (work)
416                 {
417                 FileData *fd;
418                 PanItem *pi;
419
420                 fd = static_cast<FileData *>(work->data);
421                 work = work->next;
422
423                 if (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE)
424                         {
425                         pi = pan_item_image_new(pw, fd, *x, *y, 10, 10);
426                         *x += pi->width + PAN_THUMB_GAP;
427                         if (pi->height > y_height) y_height = pi->height;
428                         }
429                 else
430                         {
431                         pi = pan_item_thumb_new(pw, fd, *x, *y);
432                         *x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
433                         y_height = PAN_THUMB_SIZE;
434                         }
435
436                 pan_item_size_by_item(pi_box, pi, PAN_BOX_BORDER);
437                 }
438
439         if (f) *y = pi_box->y + pi_box->height;
440
441         g_list_free(f);
442
443         work = d;
444         while (work)
445                 {
446                 FileData *fd;
447
448                 fd = static_cast<FileData *>(work->data);
449                 work = work->next;
450
451                 if (!pan_is_ignored(fd->path, pw->ignore_symlinks))
452                         {
453                         *level = *level + 1;
454                         pan_folder_tree_path(pw, fd, x, y, level, pi_box, width, height);
455                         *level = *level - 1;
456                         }
457                 }
458
459         filelist_free(d);
460
461         pan_item_size_by_item(parent, pi_box, PAN_BOX_BORDER);
462
463         if (*y < pi_box->y + pi_box->height + PAN_BOX_BORDER)
464                 *y = pi_box->y + pi_box->height + PAN_BOX_BORDER;
465
466         pan_item_size_coordinates(pi_box, PAN_BOX_BORDER, width, height);
467 }
468
469 void pan_folder_tree_compute(PanWindow *pw, FileData *dir_fd, gint *width, gint *height)
470 {
471         gint x;
472         gint y;
473         gint level;
474         gint w;
475         gint h;
476
477         level = 0;
478         x = PAN_BOX_BORDER;
479         y = PAN_BOX_BORDER;
480         w = PAN_BOX_BORDER * 2;
481         h = PAN_BOX_BORDER * 2;
482
483         pan_folder_tree_path(pw, dir_fd, &x, &y, &level, nullptr, &w, &h);
484
485         if (width) *width = w;
486         if (height) *height = h;
487 }
488 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */