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