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