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;
78 PanViewSearchUi *ui = *ui_ptr; // For convenience.
80 // Note that g_clear_object handles already-NULL pointers.
81 g_clear_object(&ui->search_label);
82 g_clear_object(&ui->search_button);
83 g_clear_object(&ui->search_box);
84 g_clear_object(&ui->search_button_arrow);
85 g_clear_object(&ui->search_button);
91 static void pan_search_status(PanWindow *pw, const gchar *text)
93 gtk_label_set_text(GTK_LABEL(pw->search_ui->search_label), (text) ? text : "");
96 static gint pan_search_by_path(PanWindow *pw, const gchar *path)
104 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
106 list = pan_item_find_by_path(pw, type, path, FALSE, FALSE);
107 if (!list) return FALSE;
109 found = g_list_find(list, pw->click_pi);
110 if (found && found->next)
120 pan_info_update(pw, pi);
121 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
123 buf = g_strdup_printf("%s ( %d / %d )",
124 (path[0] == G_DIR_SEPARATOR) ? _("path found") : _("filename found"),
125 g_list_index(list, pi) + 1,
126 g_list_length(list));
127 pan_search_status(pw, buf);
135 static gboolean pan_search_by_partial(PanWindow *pw, const gchar *text)
143 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
145 list = pan_item_find_by_path(pw, type, text, TRUE, FALSE);
146 if (!list) list = pan_item_find_by_path(pw, type, text, FALSE, TRUE);
151 needle = g_utf8_strdown(text, -1);
152 list = pan_item_find_by_path(pw, type, needle, TRUE, TRUE);
155 if (!list) return FALSE;
157 found = g_list_find(list, pw->click_pi);
158 if (found && found->next)
168 pan_info_update(pw, pi);
169 image_scroll_to_point(pw->imd, pi->x + pi->width / 2, pi->y + pi->height / 2, 0.5, 0.5);
171 buf = g_strdup_printf("%s ( %d / %d )",
173 g_list_index(list, pi) + 1,
174 g_list_length(list));
175 pan_search_status(pw, buf);
183 static gboolean valid_date_separator(gchar c)
185 return (c == '/' || c == '-' || c == ' ' || c == '.' || c == ',');
188 static GList *pan_search_by_date_val(PanWindow *pw, PanItemType type,
189 gint year, gint month, gint day,
195 work = g_list_last(pw->list_static);
203 if (pi->fd && (pi->type == type || type == PAN_ITEM_NONE) &&
204 ((!key && !pi->key) || (key && pi->key && strcmp(key, pi->key) == 0)))
208 tl = localtime(&pi->fd->date);
213 match = (tl->tm_year == year - 1900);
214 if (match && month >= 0) match = (tl->tm_mon == month - 1);
215 if (match && day > 0) match = (tl->tm_mday == day);
217 if (match) list = g_list_prepend(list, pi);
222 return g_list_reverse(list);
225 static gboolean pan_search_by_date(PanWindow *pw, const gchar *text)
241 if (!text) return FALSE;
246 if (!g_unichar_isdigit(*ptr) && !valid_date_separator(*ptr)) return FALSE;
251 if (t == -1) return FALSE;
253 if (!lt) return FALSE;
255 if (valid_date_separator(*text))
258 mptr = (gchar *)text;
262 year = (gint)strtol(text, &mptr, 10);
263 if (mptr == text) return FALSE;
266 if (*mptr != '\0' && valid_date_separator(*mptr))
271 month = strtol(mptr, &dptr, 10);
274 if (valid_date_separator(*dptr))
276 month = lt->tm_mon + 1;
284 if (dptr != mptr && *dptr != '\0' && valid_date_separator(*dptr))
288 day = strtol(dptr, &eptr, 10);
298 year = lt->tm_year + 1900;
309 month < -1 || month == 0 || month > 12 ||
310 day < -1 || day == 0 || day > 31) return FALSE;
312 t = pan_date_to_time(year, month, day);
313 if (t < 0) return FALSE;
315 if (pw->layout == PAN_LAYOUT_CALENDAR)
317 list = pan_search_by_date_val(pw, PAN_ITEM_BOX, year, month, day, "day");
323 type = (pw->size > PAN_IMAGE_SIZE_THUMB_LARGE) ? PAN_ITEM_IMAGE : PAN_ITEM_THUMB;
324 list = pan_search_by_date_val(pw, type, year, month, day, NULL);
329 found = g_list_find(list, pw->search_pi);
330 if (found && found->next)
343 if (pw->layout == PAN_LAYOUT_CALENDAR && pi && pi->type == PAN_ITEM_BOX)
345 pan_info_update(pw, NULL);
346 pan_calendar_update(pw, pi);
347 image_scroll_to_point(pw->imd,
348 pi->x + pi->width / 2,
349 pi->y + pi->height / 2, 0.5, 0.5);
353 pan_info_update(pw, pi);
354 image_scroll_to_point(pw->imd,
355 pi->x - PAN_BOX_BORDER * 5 / 2,
361 buf = pan_date_value_string(t, PAN_DATE_LENGTH_MONTH);
366 buf = g_strdup_printf("%d %s", day, tmp);
372 buf = pan_date_value_string(t, PAN_DATE_LENGTH_YEAR);
377 buf_count = g_strdup_printf("( %d / %d )",
378 g_list_index(list, pi) + 1,
379 g_list_length(list));
383 buf_count = g_strdup_printf("(%s)", _("no match"));
386 message = g_strdup_printf("%s %s %s", _("Date:"), buf, buf_count);
389 pan_search_status(pw, message);
397 void pan_search_activate_cb(const gchar *text, gpointer data)
399 PanWindow *pw = data;
403 tab_completion_append_to_history(pw->search_ui->search_entry, text);
405 if (pan_search_by_path(pw, text)) return;
407 if ((pw->layout == PAN_LAYOUT_TIMELINE ||
408 pw->layout == PAN_LAYOUT_CALENDAR) &&
409 pan_search_by_date(pw, text))
414 if (pan_search_by_partial(pw, text)) return;
416 pan_search_status(pw, _("no match"));
419 void pan_search_activate(PanWindow *pw)
423 text = g_strdup(gtk_entry_get_text(GTK_ENTRY(pw->search_ui->search_entry)));
424 pan_search_activate_cb(text, pw);
428 void pan_search_toggle_cb(GtkWidget *button, gpointer data)
430 PanWindow *pw = data;
431 PanViewSearchUi *ui = pw->search_ui;
434 visible = gtk_widget_get_visible(ui->search_box);
435 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)) == visible) return;
439 gtk_widget_hide(ui->search_box);
440 gtk_arrow_set(GTK_ARROW(ui->search_button_arrow), GTK_ARROW_UP, GTK_SHADOW_NONE);
444 gtk_widget_show(ui->search_box);
445 gtk_arrow_set(GTK_ARROW(ui->search_button_arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
446 gtk_widget_grab_focus(ui->search_entry);
450 void pan_search_toggle_visible(PanWindow *pw, gboolean enable)
452 PanViewSearchUi *ui = pw->search_ui;
457 if (gtk_widget_get_visible(ui->search_box))
459 gtk_widget_grab_focus(ui->search_entry);
463 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), TRUE);
468 if (gtk_widget_get_visible(ui->search_entry))
470 if (gtk_widget_has_focus(ui->search_entry))
472 gtk_widget_grab_focus(GTK_WIDGET(pw->imd->widget));
474 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->search_button), FALSE);