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"
29 #include "main-defines.h"
31 #include "pan-calendar.h"
36 #include "ui-tabcomp.h"
38 PanViewSearchUi *pan_search_ui_new(PanWindow *pw)
40 auto ui = g_new0(PanViewSearchUi, 1);
44 // Build the actual search UI.
45 ui->search_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
46 pref_spacer(ui->search_box, 0);
47 pref_label_new(ui->search_box, _("Find:"));
49 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
50 gq_gtk_box_pack_start(GTK_BOX(ui->search_box), hbox, TRUE, TRUE, 0);
51 gtk_widget_show(hbox);
53 combo = tab_completion_new_with_history(&ui->search_entry, "", "pan_view_search", -1,
54 pan_search_activate_cb, pw);
55 gq_gtk_box_pack_start(GTK_BOX(hbox), combo, TRUE, TRUE, 0);
56 gtk_widget_show(combo);
58 ui->search_label = gtk_label_new("");
59 gq_gtk_box_pack_start(GTK_BOX(hbox), ui->search_label, TRUE, TRUE, 0);
60 gtk_widget_show(ui->search_label);
62 // Build the spin-button to show/hide the search UI.
63 ui->search_button = gtk_toggle_button_new();
64 gtk_button_set_relief(GTK_BUTTON(ui->search_button), GTK_RELIEF_NONE);
65 gtk_widget_set_focus_on_click(ui->search_button, FALSE);
66 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
67 gtk_container_add(GTK_CONTAINER(ui->search_button), hbox);
68 gtk_widget_show(hbox);
69 ui->search_button_arrow = gtk_image_new_from_icon_name(GQ_ICON_PAN_UP, GTK_ICON_SIZE_BUTTON);
70 gq_gtk_box_pack_start(GTK_BOX(hbox), ui->search_button_arrow, FALSE, FALSE, 0);
71 gtk_widget_show(ui->search_button_arrow);
72 pref_label_new(hbox, _("Find"));
74 g_signal_connect(G_OBJECT(ui->search_button), "clicked",
75 G_CALLBACK(pan_search_toggle_cb), pw);
80 void pan_search_ui_destroy(PanViewSearchUi **ui_ptr)
82 if (ui_ptr == nullptr || *ui_ptr == nullptr) return;
88 static void pan_search_status(PanWindow *pw, const gchar *text)
90 gtk_label_set_text(GTK_LABEL(pw->search_ui->search_label), (text) ? text : "");
93 static gint pan_search_by_path(PanWindow *pw, const gchar *path)
101 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
103 list = pan_item_find_by_path(pw, type, path, FALSE, FALSE);
104 if (!list) return FALSE;
106 found = g_list_find(list, pw->click_pi);
107 if (found && found->next)
110 pi = static_cast<PanItem *>(found->data);
114 pi = static_cast<PanItem *>(list->data);
117 pan_info_update(pw, pi);
118 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
120 buf = g_strdup_printf("%s ( %d / %d )",
121 (path[0] == G_DIR_SEPARATOR) ? _("path found") : _("filename found"),
122 g_list_index(list, pi) + 1,
123 g_list_length(list));
124 pan_search_status(pw, buf);
132 static gboolean pan_search_by_partial(PanWindow *pw, const gchar *text)
140 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
142 list = pan_item_find_by_path(pw, type, text, TRUE, FALSE);
143 if (!list) list = pan_item_find_by_path(pw, type, text, FALSE, TRUE);
148 needle = g_utf8_strdown(text, -1);
149 list = pan_item_find_by_path(pw, type, needle, TRUE, TRUE);
152 if (!list) return FALSE;
154 found = g_list_find(list, pw->click_pi);
155 if (found && found->next)
158 pi = static_cast<PanItem *>(found->data);
162 pi = static_cast<PanItem *>(list->data);
165 pan_info_update(pw, pi);
166 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
168 buf = g_strdup_printf("%s ( %d / %d )",
170 g_list_index(list, pi) + 1,
171 g_list_length(list));
172 pan_search_status(pw, buf);
180 static gboolean valid_date_separator(gchar c)
182 return (c == '/' || c == '-' || c == ' ' || c == '.' || c == ',');
185 static GList *pan_search_by_date_val(PanWindow *pw, PanItemType type,
186 gint year, gint month, gint day,
189 GList *list = nullptr;
192 work = g_list_last(pw->list_static);
197 pi = static_cast<PanItem *>(work->data);
200 if (pi->fd && (pi->type == type || type == PAN_ITEM_NONE) &&
201 ((!key && !pi->key) || (key && pi->key && strcmp(key, pi->key) == 0)))
205 tl = localtime(&pi->fd->date);
210 match = (tl->tm_year == year - 1900);
211 if (match && month >= 0) match = (tl->tm_mon == month - 1);
212 if (match && day > 0) match = (tl->tm_mday == day);
214 if (match) list = g_list_prepend(list, pi);
219 return g_list_reverse(list);
222 static gboolean pan_search_by_date(PanWindow *pw, const gchar *text)
224 PanItem *pi = nullptr;
225 GList *list = nullptr;
238 if (!text) return FALSE;
240 ptr = const_cast<gchar *>(text);
243 if (!g_unichar_isdigit(*ptr) && !valid_date_separator(*ptr)) return FALSE;
248 if (t == -1) return FALSE;
250 if (!lt) return FALSE;
252 if (valid_date_separator(*text))
255 mptr = const_cast<gchar *>(text);
259 year = static_cast<gint>(strtol(text, &mptr, 10));
260 if (mptr == text) return FALSE;
263 if (*mptr != '\0' && valid_date_separator(*mptr))
268 month = strtol(mptr, &dptr, 10);
271 if (valid_date_separator(*dptr))
273 month = lt->tm_mon + 1;
281 if (dptr != mptr && *dptr != '\0' && valid_date_separator(*dptr))
285 day = strtol(dptr, &eptr, 10);
295 year = lt->tm_year + 1900;
306 month < -1 || month == 0 || month > 12 ||
307 day < -1 || day == 0 || day > 31) return FALSE;
309 t = pan_date_to_time(year, month, day);
310 if (t < 0) return FALSE;
312 if (pw->layout == PAN_LAYOUT_CALENDAR)
314 list = pan_search_by_date_val(pw, PAN_ITEM_BOX, year, month, day, "day");
320 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
321 list = pan_search_by_date_val(pw, type, year, month, day, nullptr);
326 found = g_list_find(list, pw->search_pi);
327 if (found && found->next)
330 pi = static_cast<PanItem *>(found->data);
334 pi = static_cast<PanItem *>(list->data);
340 if (pw->layout == PAN_LAYOUT_CALENDAR && pi && pi->type == PAN_ITEM_BOX)
342 pan_info_update(pw, nullptr);
343 pan_calendar_update(pw, pi);
344 image_scroll_to_point(pw->imd,
345 pi->x + pi->width / 2,
346 pi->y + pi->height / 2, 0.5, 0.5);
350 pan_info_update(pw, pi);
351 image_scroll_to_point(pw->imd,
352 pi->x - PAN_BOX_BORDER * 5 / 2,
358 buf = pan_date_value_string(t, PAN_DATE_LENGTH_MONTH);
363 buf = g_strdup_printf("%d %s", day, tmp);
369 buf = pan_date_value_string(t, PAN_DATE_LENGTH_YEAR);
374 buf_count = g_strdup_printf("( %d / %d )",
375 g_list_index(list, pi) + 1,
376 g_list_length(list));
380 buf_count = g_strdup_printf("(%s)", _("no match"));
383 message = g_strdup_printf("%s %s %s", _("Date:"), buf, buf_count);
386 pan_search_status(pw, message);
394 void pan_search_activate_cb(const gchar *text, gpointer data)
396 auto pw = static_cast<PanWindow *>(data);
400 tab_completion_append_to_history(pw->search_ui->search_entry, text);
402 if (pan_search_by_path(pw, text)) return;
404 if ((pw->layout == PAN_LAYOUT_TIMELINE ||
405 pw->layout == PAN_LAYOUT_CALENDAR) &&
406 pan_search_by_date(pw, text))
411 if (pan_search_by_partial(pw, text)) return;
413 pan_search_status(pw, _("no match"));
416 void pan_search_activate(PanWindow *pw)
420 text = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(pw->search_ui->search_entry)));
421 pan_search_activate_cb(text, pw);
425 void pan_search_toggle_cb(GtkWidget *button, gpointer data)
427 auto pw = static_cast<PanWindow *>(data);
428 PanViewSearchUi *ui = pw->search_ui;
432 visible = gtk_widget_get_visible(ui->search_box);
433 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)) == visible) return;
437 gtk_widget_hide(ui->search_box);
439 parent = gtk_widget_get_parent(ui->search_button_arrow);
441 g_object_unref(ui->search_button_arrow);
442 ui->search_button_arrow = gtk_image_new_from_icon_name(GQ_ICON_PAN_UP, GTK_ICON_SIZE_BUTTON);
444 gq_gtk_box_pack_start(GTK_BOX(parent), ui->search_button_arrow, FALSE, FALSE, 0);
445 gtk_box_reorder_child(GTK_BOX(parent), ui->search_button_arrow, 0);
447 gtk_widget_show(ui->search_button_arrow);
451 gtk_widget_show(ui->search_box);
453 parent = gtk_widget_get_parent(ui->search_button_arrow);
455 g_object_unref(ui->search_button_arrow);
456 ui->search_button_arrow = gtk_image_new_from_icon_name(GQ_ICON_PAN_DOWN, GTK_ICON_SIZE_BUTTON);
458 gq_gtk_box_pack_start(GTK_BOX(parent), ui->search_button_arrow, FALSE, FALSE, 0);
459 gtk_box_reorder_child(GTK_BOX(parent), ui->search_button_arrow, 0);
461 gtk_widget_show(ui->search_button_arrow);
462 gtk_widget_grab_focus(ui->search_entry);
466 void pan_search_toggle_visible(PanWindow *pw, gboolean enable)
468 PanViewSearchUi *ui = pw->search_ui;
473 if (gtk_widget_get_visible(ui->search_box))
475 gtk_widget_grab_focus(ui->search_entry);
479 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), TRUE);
484 if (gtk_widget_get_visible(ui->search_entry))
486 if (gtk_widget_has_focus(ui->search_entry))
488 gtk_widget_grab_focus(GTK_WIDGET(pw->imd->widget));
490 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), FALSE);