Add PanColor to simplify pan functions
[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, {255, 0, 0, 128});
208                 }
209
210         pw->list = g_list_concat(group->items, pw->list);
211         group->items = nullptr;
212
213         group->circumference = 0;
214         work = group->children;
215         while (work)
216                 {
217                 FlowerGroup *child;
218
219                 child = static_cast<FlowerGroup *>(work->data);
220                 work = work->next;
221
222                 group->circumference += child->diameter;
223                 }
224
225         work = g_list_last(group->children);
226         while (work)
227                 {
228                 FlowerGroup *child;
229
230                 child = static_cast<FlowerGroup *>(work->data);
231                 work = work->prev;
232
233                 pan_flower_build(pw, child, group);
234                 }
235
236         g_list_free(group->children);
237         g_free(group);
238 }
239
240 static FlowerGroup *pan_flower_group(PanWindow *pw, FileData *dir_fd, gint x, gint y)
241 {
242         FlowerGroup *group;
243         GList *f;
244         GList *d;
245         GList *work;
246         PanItem *pi_box;
247         gint x_start;
248         gint y_height;
249         gint grid_size;
250         gint grid_count;
251
252         if (!filelist_read(dir_fd, &f, &d)) return nullptr;
253         if (!f && !d) return nullptr;
254
255         f = filelist_sort(f, SORT_NAME, TRUE, TRUE);
256         d = filelist_sort(d, SORT_NAME, TRUE, TRUE);
257
258         pan_filter_fd_list(&f, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
259
260         pi_box = pan_item_text_new(pw, x, y, dir_fd->path, PAN_TEXT_ATTR_NONE,
261                                    PAN_BORDER_3,
262                                    {PAN_TEXT_COLOR, 255});
263
264         y += pi_box->height;
265
266         pi_box = pan_item_box_new(pw, file_data_ref(dir_fd),
267                                   x, y,
268                                   PAN_BOX_BORDER * 2, PAN_BOX_BORDER * 2,
269                                   PAN_BOX_OUTLINE_THICKNESS,
270                                   {PAN_BOX_COLOR, PAN_BOX_ALPHA},
271                                   {PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA});
272
273         x += PAN_BOX_BORDER;
274         y += PAN_BOX_BORDER;
275
276         grid_size = static_cast<gint>(sqrt(g_list_length(f)) + 0.9);
277         grid_count = 0;
278         x_start = x;
279         y_height = y;
280
281         work = f;
282         while (work)
283                 {
284                 FileData *fd;
285                 PanItem *pi;
286
287                 fd = static_cast<FileData *>(work->data);
288                 work = work->next;
289
290                 if (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE)
291                         {
292                         pi = pan_item_image_new(pw, fd, x, y, 10, 10);
293                         x += pi->width + PAN_THUMB_GAP;
294                         if (pi->height > y_height) y_height = pi->height;
295                         }
296                 else
297                         {
298                         pi = pan_item_thumb_new(pw, fd, x, y);
299                         x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
300                         y_height = PAN_THUMB_SIZE;
301                         }
302
303                 grid_count++;
304                 if (grid_count >= grid_size)
305                         {
306                         grid_count = 0;
307                         x = x_start;
308                         y += y_height + PAN_THUMB_GAP;
309                         y_height = 0;
310                         }
311
312                 pan_item_size_by_item(pi_box, pi, PAN_BOX_BORDER);
313                 }
314
315         group = g_new0(FlowerGroup, 1);
316         group->items = pw->list;
317         pw->list = nullptr;
318
319         group->width = pi_box->width;
320         group->height = pi_box->y + pi_box->height;
321         group->diameter = static_cast<gint>(hypot(group->width, group->height));
322
323         group->children = nullptr;
324
325         work = d;
326         while (work)
327                 {
328                 FileData *fd;
329                 FlowerGroup *child;
330
331                 fd = static_cast<FileData *>(work->data);
332                 work = work->next;
333
334                 if (!pan_is_ignored(fd->path, pw->ignore_symlinks))
335                         {
336                         child = pan_flower_group(pw, fd, 0, 0);
337                         if (child) group->children = g_list_prepend(group->children, child);
338                         }
339                 }
340
341         if (!f && !group->children)
342                 {
343                 g_list_free_full(group->items, reinterpret_cast<GDestroyNotify>(pan_item_free));
344                 g_free(group);
345                 group = nullptr;
346                 }
347
348         g_list_free(f);
349         filelist_free(d);
350
351         return group;
352 }
353
354 void pan_flower_compute(PanWindow *pw, FileData *dir_fd,
355                         gint *width, gint *height,
356                         gint *scroll_x, gint *scroll_y)
357 {
358         FlowerGroup *group;
359         GList *list;
360
361         group = pan_flower_group(pw, dir_fd, 0, 0);
362         pan_flower_build(pw, group, nullptr);
363
364         pan_flower_size(pw, width, height);
365
366         list = pan_item_find_by_fd(pw, PAN_ITEM_BOX, dir_fd, FALSE, FALSE);
367         if (list)
368                 {
369                 auto pi = static_cast<PanItem *>(list->data);
370                 *scroll_x = pi->x + pi->width / 2;
371                 *scroll_y = pi->y + pi->height / 2;
372                 }
373         g_list_free(list);
374 }
375
376 static void pan_folder_tree_path(PanWindow *pw, FileData *dir_fd,
377                                  gint *x, gint *y, gint *level,
378                                  PanItem *parent,
379                                  gint *width, gint *height)
380 {
381         GList *f;
382         GList *d;
383         GList *work;
384         PanItem *pi_box;
385         gint y_height = 0;
386
387         if (!filelist_read(dir_fd, &f, &d)) return;
388         if (!f && !d) return;
389
390         f = filelist_sort(f, SORT_NAME, TRUE, TRUE);
391         d = filelist_sort(d, SORT_NAME, TRUE, TRUE);
392
393         pan_filter_fd_list(&f, pw->filter_ui->filter_elements, pw->filter_ui->filter_classes);
394
395         *x = PAN_BOX_BORDER + ((*level) * MAX(PAN_BOX_BORDER, PAN_THUMB_GAP));
396
397         pi_box = pan_item_text_new(pw, *x, *y, dir_fd->path, PAN_TEXT_ATTR_NONE,
398                                    PAN_BORDER_3,
399                                    {PAN_TEXT_COLOR, 255});
400
401         *y += pi_box->height;
402
403         pi_box = pan_item_box_new(pw, file_data_ref(dir_fd),
404                                   *x, *y,
405                                   PAN_BOX_BORDER, PAN_BOX_BORDER,
406                                   PAN_BOX_OUTLINE_THICKNESS,
407                                   {PAN_BOX_COLOR, PAN_BOX_ALPHA},
408                                   {PAN_BOX_OUTLINE_COLOR, PAN_BOX_OUTLINE_ALPHA});
409
410         *x += PAN_BOX_BORDER;
411         *y += PAN_BOX_BORDER;
412
413         work = f;
414         while (work)
415                 {
416                 FileData *fd;
417                 PanItem *pi;
418
419                 fd = static_cast<FileData *>(work->data);
420                 work = work->next;
421
422                 if (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE)
423                         {
424                         pi = pan_item_image_new(pw, fd, *x, *y, 10, 10);
425                         *x += pi->width + PAN_THUMB_GAP;
426                         if (pi->height > y_height) y_height = pi->height;
427                         }
428                 else
429                         {
430                         pi = pan_item_thumb_new(pw, fd, *x, *y);
431                         *x += PAN_THUMB_SIZE + PAN_THUMB_GAP;
432                         y_height = PAN_THUMB_SIZE;
433                         }
434
435                 pan_item_size_by_item(pi_box, pi, PAN_BOX_BORDER);
436                 }
437
438         if (f) *y = pi_box->y + pi_box->height;
439
440         g_list_free(f);
441
442         work = d;
443         while (work)
444                 {
445                 FileData *fd;
446
447                 fd = static_cast<FileData *>(work->data);
448                 work = work->next;
449
450                 if (!pan_is_ignored(fd->path, pw->ignore_symlinks))
451                         {
452                         *level = *level + 1;
453                         pan_folder_tree_path(pw, fd, x, y, level, pi_box, width, height);
454                         *level = *level - 1;
455                         }
456                 }
457
458         filelist_free(d);
459
460         pan_item_size_by_item(parent, pi_box, PAN_BOX_BORDER);
461
462         if (*y < pi_box->y + pi_box->height + PAN_BOX_BORDER)
463                 *y = pi_box->y + pi_box->height + PAN_BOX_BORDER;
464
465         pan_item_size_coordinates(pi_box, PAN_BOX_BORDER, width, height);
466 }
467
468 void pan_folder_tree_compute(PanWindow *pw, FileData *dir_fd, gint *width, gint *height)
469 {
470         gint x;
471         gint y;
472         gint level;
473         gint w;
474         gint h;
475
476         level = 0;
477         x = PAN_BOX_BORDER;
478         y = PAN_BOX_BORDER;
479         w = PAN_BOX_BORDER * 2;
480         h = PAN_BOX_BORDER * 2;
481
482         pan_folder_tree_path(pw, dir_fd, &x, &y, &level, nullptr, &w, &h);
483
484         if (width) *width = w;
485         if (height) *height = h;
486 }
487 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */