2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
22 #include "pan-view-search.h"
25 #include "pan-calendar.h"
28 #include "ui-tabcomp.h"
31 PanViewSearchUi *pan_search_ui_new(PanWindow *pw)
33 auto ui = g_new0(PanViewSearchUi, 1);
37 // Build the actual search UI.
38 ui->search_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
39 pref_spacer(ui->search_box, 0);
40 pref_label_new(ui->search_box, _("Find:"));
42 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
43 gtk_box_pack_start(GTK_BOX(ui->search_box), hbox, TRUE, TRUE, 0);
44 gtk_widget_show(hbox);
46 combo = tab_completion_new_with_history(&ui->search_entry, "", "pan_view_search", -1,
47 pan_search_activate_cb, pw);
48 gtk_box_pack_start(GTK_BOX(hbox), combo, TRUE, TRUE, 0);
49 gtk_widget_show(combo);
51 ui->search_label = gtk_label_new("");
52 gtk_box_pack_start(GTK_BOX(hbox), ui->search_label, TRUE, TRUE, 0);
53 gtk_widget_show(ui->search_label);
55 // Build the spin-button to show/hide the search UI.
56 ui->search_button = gtk_toggle_button_new();
57 gtk_button_set_relief(GTK_BUTTON(ui->search_button), GTK_RELIEF_NONE);
58 gtk_button_set_focus_on_click(GTK_BUTTON(ui->search_button), FALSE);
59 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
60 gtk_container_add(GTK_CONTAINER(ui->search_button), hbox);
61 gtk_widget_show(hbox);
62 ui->search_button_arrow = gtk_image_new_from_icon_name("pan-up", GTK_ICON_SIZE_BUTTON);
63 gtk_box_pack_start(GTK_BOX(hbox), ui->search_button_arrow, FALSE, FALSE, 0);
64 gtk_widget_show(ui->search_button_arrow);
65 pref_label_new(hbox, _("Find"));
67 g_signal_connect(G_OBJECT(ui->search_button), "clicked",
68 G_CALLBACK(pan_search_toggle_cb), pw);
73 void pan_search_ui_destroy(PanViewSearchUi **ui_ptr)
75 if (ui_ptr == nullptr || *ui_ptr == nullptr) return;
81 static void pan_search_status(PanWindow *pw, const gchar *text)
83 gtk_label_set_text(GTK_LABEL(pw->search_ui->search_label), (text) ? text : "");
86 static gint pan_search_by_path(PanWindow *pw, const gchar *path)
94 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
96 list = pan_item_find_by_path(pw, type, path, FALSE, FALSE);
97 if (!list) return FALSE;
99 found = g_list_find(list, pw->click_pi);
100 if (found && found->next)
103 pi = static_cast<PanItem *>(found->data);
107 pi = static_cast<PanItem *>(list->data);
110 pan_info_update(pw, pi);
111 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
113 buf = g_strdup_printf("%s ( %d / %d )",
114 (path[0] == G_DIR_SEPARATOR) ? _("path found") : _("filename found"),
115 g_list_index(list, pi) + 1,
116 g_list_length(list));
117 pan_search_status(pw, buf);
125 static gboolean pan_search_by_partial(PanWindow *pw, const gchar *text)
133 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
135 list = pan_item_find_by_path(pw, type, text, TRUE, FALSE);
136 if (!list) list = pan_item_find_by_path(pw, type, text, FALSE, TRUE);
141 needle = g_utf8_strdown(text, -1);
142 list = pan_item_find_by_path(pw, type, needle, TRUE, TRUE);
145 if (!list) return FALSE;
147 found = g_list_find(list, pw->click_pi);
148 if (found && found->next)
151 pi = static_cast<PanItem *>(found->data);
155 pi = static_cast<PanItem *>(list->data);
158 pan_info_update(pw, pi);
159 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
161 buf = g_strdup_printf("%s ( %d / %d )",
163 g_list_index(list, pi) + 1,
164 g_list_length(list));
165 pan_search_status(pw, buf);
173 static gboolean valid_date_separator(gchar c)
175 return (c == '/' || c == '-' || c == ' ' || c == '.' || c == ',');
178 static GList *pan_search_by_date_val(PanWindow *pw, PanItemType type,
179 gint year, gint month, gint day,
182 GList *list = nullptr;
185 work = g_list_last(pw->list_static);
190 pi = static_cast<PanItem *>(work->data);
193 if (pi->fd && (pi->type == type || type == PAN_ITEM_NONE) &&
194 ((!key && !pi->key) || (key && pi->key && strcmp(key, pi->key) == 0)))
198 tl = localtime(&pi->fd->date);
203 match = (tl->tm_year == year - 1900);
204 if (match && month >= 0) match = (tl->tm_mon == month - 1);
205 if (match && day > 0) match = (tl->tm_mday == day);
207 if (match) list = g_list_prepend(list, pi);
212 return g_list_reverse(list);
215 static gboolean pan_search_by_date(PanWindow *pw, const gchar *text)
217 PanItem *pi = nullptr;
218 GList *list = nullptr;
231 if (!text) return FALSE;
233 ptr = const_cast<gchar *>(text);
236 if (!g_unichar_isdigit(*ptr) && !valid_date_separator(*ptr)) return FALSE;
241 if (t == -1) return FALSE;
243 if (!lt) return FALSE;
245 if (valid_date_separator(*text))
248 mptr = const_cast<gchar *>(text);
252 year = static_cast<gint>(strtol(text, &mptr, 10));
253 if (mptr == text) return FALSE;
256 if (*mptr != '\0' && valid_date_separator(*mptr))
261 month = strtol(mptr, &dptr, 10);
264 if (valid_date_separator(*dptr))
266 month = lt->tm_mon + 1;
274 if (dptr != mptr && *dptr != '\0' && valid_date_separator(*dptr))
278 day = strtol(dptr, &eptr, 10);
288 year = lt->tm_year + 1900;
299 month < -1 || month == 0 || month > 12 ||
300 day < -1 || day == 0 || day > 31) return FALSE;
302 t = pan_date_to_time(year, month, day);
303 if (t < 0) return FALSE;
305 if (pw->layout == PAN_LAYOUT_CALENDAR)
307 list = pan_search_by_date_val(pw, PAN_ITEM_BOX, year, month, day, "day");
313 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
314 list = pan_search_by_date_val(pw, type, year, month, day, nullptr);
319 found = g_list_find(list, pw->search_pi);
320 if (found && found->next)
323 pi = static_cast<PanItem *>(found->data);
327 pi = static_cast<PanItem *>(list->data);
333 if (pw->layout == PAN_LAYOUT_CALENDAR && pi && pi->type == PAN_ITEM_BOX)
335 pan_info_update(pw, nullptr);
336 pan_calendar_update(pw, pi);
337 image_scroll_to_point(pw->imd,
338 pi->x + pi->width / 2,
339 pi->y + pi->height / 2, 0.5, 0.5);
343 pan_info_update(pw, pi);
344 image_scroll_to_point(pw->imd,
345 pi->x - PAN_BOX_BORDER * 5 / 2,
351 buf = pan_date_value_string(t, PAN_DATE_LENGTH_MONTH);
356 buf = g_strdup_printf("%d %s", day, tmp);
362 buf = pan_date_value_string(t, PAN_DATE_LENGTH_YEAR);
367 buf_count = g_strdup_printf("( %d / %d )",
368 g_list_index(list, pi) + 1,
369 g_list_length(list));
373 buf_count = g_strdup_printf("(%s)", _("no match"));
376 message = g_strdup_printf("%s %s %s", _("Date:"), buf, buf_count);
379 pan_search_status(pw, message);
387 void pan_search_activate_cb(const gchar *text, gpointer data)
389 auto pw = static_cast<PanWindow *>(data);
393 tab_completion_append_to_history(pw->search_ui->search_entry, text);
395 if (pan_search_by_path(pw, text)) return;
397 if ((pw->layout == PAN_LAYOUT_TIMELINE ||
398 pw->layout == PAN_LAYOUT_CALENDAR) &&
399 pan_search_by_date(pw, text))
404 if (pan_search_by_partial(pw, text)) return;
406 pan_search_status(pw, _("no match"));
409 void pan_search_activate(PanWindow *pw)
413 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(pw->search_ui->search_entry)));
414 pan_search_activate_cb(text, pw);
418 void pan_search_toggle_cb(GtkWidget *button, gpointer data)
420 auto pw = static_cast<PanWindow *>(data);
421 PanViewSearchUi *ui = pw->search_ui;
425 visible = gtk_widget_get_visible(ui->search_box);
426 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)) == visible) return;
430 gtk_widget_hide(ui->search_box);
432 parent = gtk_widget_get_parent(ui->search_button_arrow);
434 gtk_widget_destroy(ui->search_button_arrow);
435 ui->search_button_arrow = gtk_image_new_from_icon_name("pan-up", GTK_ICON_SIZE_BUTTON);
437 gtk_box_pack_start(GTK_BOX(parent), ui->search_button_arrow, FALSE, FALSE, 0);
438 gtk_box_reorder_child(GTK_BOX(parent), ui->search_button_arrow, 0);
440 gtk_widget_show(ui->search_button_arrow);
444 gtk_widget_show(ui->search_box);
446 parent = gtk_widget_get_parent(ui->search_button_arrow);
448 gtk_widget_destroy(ui->search_button_arrow);
449 ui->search_button_arrow = gtk_image_new_from_icon_name("pan-down", GTK_ICON_SIZE_BUTTON);
451 gtk_box_pack_start(GTK_BOX(parent), ui->search_button_arrow, FALSE, FALSE, 0);
452 gtk_box_reorder_child(GTK_BOX(parent), ui->search_button_arrow, 0);
454 gtk_widget_show(ui->search_button_arrow);
455 gtk_widget_grab_focus(ui->search_entry);
459 void pan_search_toggle_visible(PanWindow *pw, gboolean enable)
461 PanViewSearchUi *ui = pw->search_ui;
466 if (gtk_widget_get_visible(ui->search_box))
468 gtk_widget_grab_focus(ui->search_entry);
472 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), TRUE);
477 if (gtk_widget_get_visible(ui->search_entry))
479 if (gtk_widget_has_focus(ui->search_entry))
481 gtk_widget_grab_focus(GTK_WIDGET(pw->imd->widget));
483 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), FALSE);