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"
29 #include "ui_tabcomp.h"
32 PanViewSearchUi *pan_search_ui_new(PanWindow *pw)
34 PanViewSearchUi *ui = g_new0(PanViewSearchUi, 1);
38 // Build the actual search UI.
39 ui->search_box = gtk_hbox_new(FALSE, PREF_PAD_SPACE);
40 pref_spacer(ui->search_box, 0);
41 pref_label_new(ui->search_box, _("Find:"));
43 hbox = gtk_hbox_new(TRUE, PREF_PAD_SPACE);
44 gtk_box_pack_start(GTK_BOX(ui->search_box), hbox, TRUE, TRUE, 0);
45 gtk_widget_show(hbox);
47 combo = tab_completion_new_with_history(&ui->search_entry, "", "pan_view_search", -1,
48 pan_search_activate_cb, pw);
49 gtk_box_pack_start(GTK_BOX(hbox), combo, TRUE, TRUE, 0);
50 gtk_widget_show(combo);
52 ui->search_label = gtk_label_new("");
53 gtk_box_pack_start(GTK_BOX(hbox), ui->search_label, TRUE, TRUE, 0);
54 gtk_widget_show(ui->search_label);
56 // Build the spin-button to show/hide the search UI.
57 ui->search_button = gtk_toggle_button_new();
58 gtk_button_set_relief(GTK_BUTTON(ui->search_button), GTK_RELIEF_NONE);
59 gtk_button_set_focus_on_click(GTK_BUTTON(ui->search_button), FALSE);
60 hbox = gtk_hbox_new(FALSE, PREF_PAD_GAP);
61 gtk_container_add(GTK_CONTAINER(ui->search_button), hbox);
62 gtk_widget_show(hbox);
63 ui->search_button_arrow = gtk_arrow_new(GTK_ARROW_UP, GTK_SHADOW_NONE);
64 gtk_box_pack_start(GTK_BOX(hbox), ui->search_button_arrow, FALSE, FALSE, 0);
65 gtk_widget_show(ui->search_button_arrow);
66 pref_label_new(hbox, _("Find"));
68 g_signal_connect(G_OBJECT(ui->search_button), "clicked",
69 G_CALLBACK(pan_search_toggle_cb), pw);
74 void pan_search_ui_destroy(PanViewSearchUi **ui_ptr)
76 if (ui_ptr == NULL || *ui_ptr == NULL) return;
82 static void pan_search_status(PanWindow *pw, const gchar *text)
84 gtk_label_set_text(GTK_LABEL(pw->search_ui->search_label), (text) ? text : "");
87 static gint pan_search_by_path(PanWindow *pw, const gchar *path)
95 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
97 list = pan_item_find_by_path(pw, type, path, FALSE, FALSE);
98 if (!list) return FALSE;
100 found = g_list_find(list, pw->click_pi);
101 if (found && found->next)
111 pan_info_update(pw, pi);
112 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
114 buf = g_strdup_printf("%s ( %d / %d )",
115 (path[0] == G_DIR_SEPARATOR) ? _("path found") : _("filename found"),
116 g_list_index(list, pi) + 1,
117 g_list_length(list));
118 pan_search_status(pw, buf);
126 static gboolean pan_search_by_partial(PanWindow *pw, const gchar *text)
134 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
136 list = pan_item_find_by_path(pw, type, text, TRUE, FALSE);
137 if (!list) list = pan_item_find_by_path(pw, type, text, FALSE, TRUE);
142 needle = g_utf8_strdown(text, -1);
143 list = pan_item_find_by_path(pw, type, needle, TRUE, TRUE);
146 if (!list) return FALSE;
148 found = g_list_find(list, pw->click_pi);
149 if (found && found->next)
159 pan_info_update(pw, pi);
160 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
162 buf = g_strdup_printf("%s ( %d / %d )",
164 g_list_index(list, pi) + 1,
165 g_list_length(list));
166 pan_search_status(pw, buf);
174 static gboolean valid_date_separator(gchar c)
176 return (c == '/' || c == '-' || c == ' ' || c == '.' || c == ',');
179 static GList *pan_search_by_date_val(PanWindow *pw, PanItemType type,
180 gint year, gint month, gint day,
186 work = g_list_last(pw->list_static);
194 if (pi->fd && (pi->type == type || type == PAN_ITEM_NONE) &&
195 ((!key && !pi->key) || (key && pi->key && strcmp(key, pi->key) == 0)))
199 tl = localtime(&pi->fd->date);
204 match = (tl->tm_year == year - 1900);
205 if (match && month >= 0) match = (tl->tm_mon == month - 1);
206 if (match && day > 0) match = (tl->tm_mday == day);
208 if (match) list = g_list_prepend(list, pi);
213 return g_list_reverse(list);
216 static gboolean pan_search_by_date(PanWindow *pw, const gchar *text)
232 if (!text) return FALSE;
237 if (!g_unichar_isdigit(*ptr) && !valid_date_separator(*ptr)) return FALSE;
242 if (t == -1) return FALSE;
244 if (!lt) return FALSE;
246 if (valid_date_separator(*text))
249 mptr = (gchar *)text;
253 year = (gint)strtol(text, &mptr, 10);
254 if (mptr == text) return FALSE;
257 if (*mptr != '\0' && valid_date_separator(*mptr))
262 month = strtol(mptr, &dptr, 10);
265 if (valid_date_separator(*dptr))
267 month = lt->tm_mon + 1;
275 if (dptr != mptr && *dptr != '\0' && valid_date_separator(*dptr))
279 day = strtol(dptr, &eptr, 10);
289 year = lt->tm_year + 1900;
300 month < -1 || month == 0 || month > 12 ||
301 day < -1 || day == 0 || day > 31) return FALSE;
303 t = pan_date_to_time(year, month, day);
304 if (t < 0) return FALSE;
306 if (pw->layout == PAN_LAYOUT_CALENDAR)
308 list = pan_search_by_date_val(pw, PAN_ITEM_BOX, year, month, day, "day");
314 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
315 list = pan_search_by_date_val(pw, type, year, month, day, NULL);
320 found = g_list_find(list, pw->search_pi);
321 if (found && found->next)
334 if (pw->layout == PAN_LAYOUT_CALENDAR && pi && pi->type == PAN_ITEM_BOX)
336 pan_info_update(pw, NULL);
337 pan_calendar_update(pw, pi);
338 image_scroll_to_point(pw->imd,
339 pi->x + pi->width / 2,
340 pi->y + pi->height / 2, 0.5, 0.5);
344 pan_info_update(pw, pi);
345 image_scroll_to_point(pw->imd,
346 pi->x - PAN_BOX_BORDER * 5 / 2,
352 buf = pan_date_value_string(t, PAN_DATE_LENGTH_MONTH);
357 buf = g_strdup_printf("%d %s", day, tmp);
363 buf = pan_date_value_string(t, PAN_DATE_LENGTH_YEAR);
368 buf_count = g_strdup_printf("( %d / %d )",
369 g_list_index(list, pi) + 1,
370 g_list_length(list));
374 buf_count = g_strdup_printf("(%s)", _("no match"));
377 message = g_strdup_printf("%s %s %s", _("Date:"), buf, buf_count);
380 pan_search_status(pw, message);
388 void pan_search_activate_cb(const gchar *text, gpointer data)
390 PanWindow *pw = data;
394 tab_completion_append_to_history(pw->search_ui->search_entry, text);
396 if (pan_search_by_path(pw, text)) return;
398 if ((pw->layout == PAN_LAYOUT_TIMELINE ||
399 pw->layout == PAN_LAYOUT_CALENDAR) &&
400 pan_search_by_date(pw, text))
405 if (pan_search_by_partial(pw, text)) return;
407 pan_search_status(pw, _("no match"));
410 void pan_search_activate(PanWindow *pw)
414 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(pw->search_ui->search_entry)));
415 pan_search_activate_cb(text, pw);
419 void pan_search_toggle_cb(GtkWidget *button, gpointer data)
421 PanWindow *pw = data;
422 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);
431 gtk_arrow_set(GTK_ARROW(ui->search_button_arrow), GTK_ARROW_UP, GTK_SHADOW_NONE);
435 gtk_widget_show(ui->search_box);
436 gtk_arrow_set(GTK_ARROW(ui->search_button_arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
437 gtk_widget_grab_focus(ui->search_entry);
441 void pan_search_toggle_visible(PanWindow *pw, gboolean enable)
443 PanViewSearchUi *ui = pw->search_ui;
448 if (gtk_widget_get_visible(ui->search_box))
450 gtk_widget_grab_focus(ui->search_entry);
454 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), TRUE);
459 if (gtk_widget_get_visible(ui->search_entry))
461 if (gtk_widget_has_focus(ui->search_entry))
463 gtk_widget_grab_focus(GTK_WIDGET(pw->imd->widget));
465 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), FALSE);