-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiSearch_5.3-1_final.patch
1975 lines (1862 loc) · 72.9 KB
/
MultiSearch_5.3-1_final.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From ab01125b00e5f1c831efc97c4235acc64d8fd0e1 Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Wed, 20 Jan 2021 13:59:46 -0600
Subject: =?UTF-8?q?MultiSearch=20=E2=80=93=C2=A0an=20AND-chained=20multi-w?=
=?UTF-8?q?ord=20searching=20in=20any=20listbox.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
lib/keybind.c | 1 +
lib/keybind.h | 1 +
lib/widget.h | 2 +
lib/widget/Makefile.am | 2 +
lib/widget/dialog-switch.c | 3 +-
lib/widget/dialog.c | 49 ++--
lib/widget/dialog.h | 1 +
lib/widget/filtering_listbox.c | 462 +++++++++++++++++++++++++++++++++
lib/widget/filtering_listbox.h | 46 ++++
lib/widget/forwarding_input.c | 160 ++++++++++++
lib/widget/forwarding_input.h | 36 +++
lib/widget/group.c | 11 +-
lib/widget/history.c | 97 ++++---
lib/widget/history.h | 4 +-
lib/widget/input.c | 60 +++--
lib/widget/input.h | 2 +
lib/widget/input_complete.c | 73 ++++--
lib/widget/listbox-window.c | 24 +-
lib/widget/listbox-window.h | 4 +-
lib/widget/listbox.c | 75 +++++-
lib/widget/listbox.h | 8 +
lib/widget/widget-common.c | 2 +
lib/widget/widget-common.h | 15 +-
src/editor/choosesyntax.c | 2 +-
src/editor/editcmd_dialogs.c | 14 +-
src/editor/editwidget.c | 8 +-
src/file_history.c | 3 +-
src/keybind-defaults.c | 5 +-
src/selcodepage.c | 2 +-
src/usermenu.c | 2 +-
30 files changed, 1029 insertions(+), 145 deletions(-)
create mode 100644 lib/widget/filtering_listbox.c
create mode 100644 lib/widget/filtering_listbox.h
create mode 100644 lib/widget/forwarding_input.c
create mode 100644 lib/widget/forwarding_input.h
diff --git a/lib/keybind.c b/lib/keybind.c
index abd44d3e2..df3cbf110 100644
--- a/lib/keybind.c
+++ b/lib/keybind.c
@@ -93,6 +93,7 @@ static name_keymap_t command_names[] = {
ADD_KEYMAP_NAME (SearchContinue),
ADD_KEYMAP_NAME (Replace),
ADD_KEYMAP_NAME (ReplaceContinue),
+ ADD_KEYMAP_NAME (MultiSearch),
ADD_KEYMAP_NAME (Help),
ADD_KEYMAP_NAME (Shell),
ADD_KEYMAP_NAME (Edit),
diff --git a/lib/keybind.h b/lib/keybind.h
index af019df09..817158412 100644
--- a/lib/keybind.h
+++ b/lib/keybind.h
@@ -82,6 +82,7 @@ enum
CK_SearchContinue,
CK_Replace,
CK_ReplaceContinue,
+ CK_MultiSearch,
CK_SearchStop,
CK_Help,
CK_Edit,
diff --git a/lib/widget.h b/lib/widget.h
index e3bb5cac2..cfe556891 100644
--- a/lib/widget.h
+++ b/lib/widget.h
@@ -30,9 +30,11 @@ typedef struct WGroup WGroup;
#include "lib/widget/groupbox.h"
#include "lib/widget/label.h"
#include "lib/widget/listbox.h"
+#include "lib/widget/filtering_listbox.h"
#include "lib/widget/menu.h"
#include "lib/widget/radio.h"
#include "lib/widget/input.h"
+#include "lib/widget/forwarding_input.h"
#include "lib/widget/listbox-window.h"
#include "lib/widget/quick.h"
#include "lib/widget/wtools.h"
diff --git a/lib/widget/Makefile.am b/lib/widget/Makefile.am
index 90f023bbc..9a4616c38 100644
--- a/lib/widget/Makefile.am
+++ b/lib/widget/Makefile.am
@@ -16,8 +16,10 @@ libmcwidget_la_SOURCES = \
history.c history.h \
input.c input.h \
input_complete.c \
+ forwarding_input.c forwarding_input.h \
listbox-window.c listbox-window.h \
listbox.c listbox.h \
+ filtering_listbox.c filtering_listbox.h \
label.c label.h \
menu.c menu.h \
mouse.c mouse.h \
diff --git a/lib/widget/dialog-switch.c b/lib/widget/dialog-switch.c
index 93868b19d..a9a51ce0d 100644
--- a/lib/widget/dialog-switch.c
+++ b/lib/widget/dialog-switch.c
@@ -241,7 +241,8 @@ dialog_switch_list (void)
else
title = g_strdup ("");
- listbox_add_item (listbox->list, LISTBOX_APPEND_BEFORE, get_hotkey (i++), title, h, FALSE);
+ listbox_add_item (LISTBOX (listbox->list), LISTBOX_APPEND_BEFORE, get_hotkey (i++), title,
+ h, FALSE);
g_free (title);
}
diff --git a/lib/widget/dialog.c b/lib/widget/dialog.c
index b8a08f029..c8d940be9 100644
--- a/lib/widget/dialog.c
+++ b/lib/widget/dialog.c
@@ -87,30 +87,6 @@ dlg_default_get_colors (const Widget * w)
return CONST_DIALOG (w)->colors;
}
-/* --------------------------------------------------------------------------------------------- */
-/**
- * Read histories from the ${XDG_CACHE_HOME}/mc/history file
- */
-static void
-dlg_read_history (WDialog * h)
-{
- char *profile;
- ev_history_load_save_t event_data;
-
- if (num_history_items_recorded == 0) /* this is how to disable */
- return;
-
- profile = mc_config_get_full_path (MC_HISTORY_FILE);
- event_data.cfg = mc_config_init (profile, TRUE);
- event_data.receiver = NULL;
-
- /* create all histories in dialog */
- mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
-
- mc_config_deinit (event_data.cfg);
- g_free (profile);
-}
-
/* --------------------------------------------------------------------------------------------- */
static void
@@ -473,6 +449,31 @@ do_refresh (void)
/* --------------------------------------------------------------------------------------------- */
+/**
+ * Read histories from the ${XDG_CACHE_HOME}/mc/history file
+ */
+void
+dlg_read_history (WDialog * h)
+{
+ char *profile;
+ ev_history_load_save_t event_data;
+
+ if (num_history_items_recorded == 0) /* this is how to disable */
+ return;
+
+ profile = mc_config_get_full_path (MC_HISTORY_FILE);
+ event_data.cfg = mc_config_init (profile, TRUE);
+ event_data.receiver = NULL;
+
+ /* create all histories in dialog */
+ mc_event_raise (h->event_group, MCEVENT_HISTORY_LOAD, &event_data);
+
+ mc_config_deinit (event_data.cfg);
+ g_free (profile);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
void
dlg_stop (WDialog * h)
{
diff --git a/lib/widget/dialog.h b/lib/widget/dialog.h
index 1d08b8e1a..d67735e71 100644
--- a/lib/widget/dialog.h
+++ b/lib/widget/dialog.h
@@ -109,6 +109,7 @@ int dlg_run (WDialog * d);
void dlg_destroy (WDialog * h);
void dlg_run_done (WDialog * h);
+void dlg_read_history (WDialog * h);
void dlg_save_history (WDialog * h);
void dlg_process_event (WDialog * h, int key, Gpm_Event * event);
diff --git a/lib/widget/filtering_listbox.c b/lib/widget/filtering_listbox.c
new file mode 100644
index 000000000..b5ce2ab1c
--- /dev/null
+++ b/lib/widget/filtering_listbox.c
@@ -0,0 +1,462 @@
+/*
+ A class extending WListbox with dynamic filtering (i.e.: removal) of entries.
+
+ Copyright (C) <2021>
+ Free Software Foundation, Inc.
+
+ Written by:
+ Sebastian Gniazdowski <sgniazdowski@gmail.com>, 2021.
+
+ This file is part of the Midnight Commander.
+
+ The Midnight Commander is free software: you can redistribute it
+ and/or modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ The Midnight Commander is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** \file filtering_listbox.c
+ * \brief A WListbox inheriting class that adds dynamic filtering of entries.
+ * \author Sebastian Gniazdowski
+ * \date 2021
+ *
+ * In order to enable/disable (i.e.: toggle) MultiSearch on an listbox you can send a message:
+ * send_message (listbox, NULL, MSG_ACTION, CK_MultiSearch, NULL);
+ *
+ * It'll extend owning dialog with a new MultiSearch input and pair it up with the listbox, so that
+ * some of keys (basically all that aren't in input's keymap) are forwarded to listbox.
+ */
+
+#include <config.h>
+
+#include "lib/global.h"
+#include "lib/widget.h"
+#include "lib/tty/tty.h"
+
+/*** global variables ****************************************************************************/
+
+/*** file scope macro definitions ****************************************************************/
+
+/*** file scope type declarations ****************************************************************/
+
+/*** file scope variables ************************************************************************/
+
+/*** file scope functions ************************************************************************/
+/* --------------------------------------------------------------------------------------------- */
+
+static WLEntry *
+filt_listbox_shallow_copy_entry (WLEntry * src, gboolean take_ownership)
+{
+ WLEntry *copy;
+ copy = g_new (WLEntry, 1);
+ *copy = *src;
+
+ /* Who has the ownership of the data? */
+ src->free_text = src->free_text && !take_ownership;
+ src->free_data = src->free_data && !take_ownership;
+ copy->free_text = copy->free_text && take_ownership;
+ copy->free_data = copy->free_data && take_ownership;
+
+ return copy;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+static void
+filt_listbox_make_one_line_room (WFilteringListbox * sl, gboolean should_add_free_room)
+{
+ WListbox *l = LISTBOX (sl);
+ Widget *w = WIDGET (l), *owner = WIDGET (WIDGET (w)->owner);
+ WRect r_dialog, r_listbox;
+ int new_dialog_height, new_dialog_ypos, new_listbox_height, take_give_from_to_owner = 1;
+
+ /*
+ * IF the enlarged dialog won't fit the screen, don't resize it but the listbox instead.
+ * Do it also when requested if the listbox is large (for small listboxes always try to
+ * enlarge the dialog).
+ */
+ if ((sl->resize_strategy == FILT_LIST_DIALOG_AUTO_RESIZE && LINES <= owner->lines + 2) ||
+ (sl->resize_strategy == FILT_LIST_KEEP_DIALOG_SIZE && owner->lines > 7))
+ take_give_from_to_owner = 0;
+
+ /* Increase the height of the dialog by 1, so that the new input fits. */
+ if (should_add_free_room)
+ {
+ new_dialog_height = owner->lines + take_give_from_to_owner;
+ new_listbox_height = w->lines + (-1 + take_give_from_to_owner);
+ new_dialog_ypos = owner->y - take_give_from_to_owner;
+ }
+ else
+ {
+ new_dialog_height = owner->lines - take_give_from_to_owner;
+ new_listbox_height = w->lines - (-1 + take_give_from_to_owner);
+ new_dialog_ypos = owner->y + take_give_from_to_owner;
+ }
+ rect_init (&r_dialog, new_dialog_ypos, owner->x, new_dialog_height, owner->cols);
+ rect_init (&r_listbox, w->y, w->x, new_listbox_height, w->cols);
+
+ /*
+ * Doing widget_set_size_rect(w, &r_listbox) causes problems as it invokes
+ * drawing of the widget owner.
+ */
+ send_message (w, NULL, MSG_RESIZE, 0, &r_listbox);
+ send_message (owner, NULL, MSG_RESIZE, 0, &r_dialog);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+static void
+filt_listbox_show_multi_search_widget (WFilteringListbox * sl)
+{
+ WListbox *l = LISTBOX (sl);
+ Widget *w = WIDGET (l), *owner = WIDGET (WIDGET (l)->owner);
+ WForwardingInput *multi_search_in;
+ int distance_y = owner->y + owner->lines - (w->y + w->lines) + 1;
+ int distance_x = w->cols > 40 ? 5 : 1, small = w->cols <= 15 ? 1 : 0;
+
+ filt_listbox_make_one_line_room (sl, 1);
+ multi_search_in = forwarding_input_new (owner->lines - distance_y, distance_x,
+ input_colors, w->cols - 2 - distance_x + small, "",
+ "multi_search", INPUT_COMPLETE_NONE, w);
+ group_add_widget_autopos (GROUP (owner), multi_search_in, WPOS_KEEP_TOP | WPOS_CENTER_HORZ,
+ NULL);
+ /* Initialize input widget. */
+ send_message (WIDGET (multi_search_in), w, MSG_INIT, 0, NULL);
+ dlg_read_history (DIALOG (w->owner));
+
+ /* Draw dialog and listbox, and then input. */
+ widget_draw (WIDGET (w->owner));
+ widget_draw (w);
+ widget_draw (WIDGET (multi_search_in));
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+static void
+filt_listbox_hide_multi_search_widget (WFilteringListbox * sl)
+{
+ WListbox *l = LISTBOX (sl);
+ Widget *w = WIDGET (sl);
+ Widget *in;
+ in = widget_find_by_type (WIDGET (WIDGET (l)->owner), forw_input_callback);
+ if (in != NULL)
+ {
+ group_remove_widget (in);
+ filt_listbox_make_one_line_room (sl, 0);
+ group_select_next_widget (WIDGET (l)->owner);
+
+ /*
+ * Repainting is needed because some part of the resized dialog can be left on the
+ * background.
+ */
+ if (sl->resize_strategy != FILT_LIST_KEEP_DIALOG_SIZE || w->lines <= 7)
+ repaint_screen ();
+
+ widget_draw (WIDGET (w->owner));
+ widget_draw (w);
+ widget_destroy (in);
+ }
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* Return TRUE if given listbox is in WST_FILTER state. */
+static gboolean
+filt_listbox_is_filter_state (WFilteringListbox * sl)
+{
+ return widget_get_state (WIDGET (sl), WST_FILTER);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+static void
+filt_listbox_filter_list (WFilteringListbox * sl, const char *text)
+{
+ WListbox *l = LISTBOX (sl);
+ int i, size;
+ GList *le;
+ char **query_terms;
+
+ /*
+ * Remove the list and allocate a new one. The elements are only shallowly freed because the
+ * internal data is still used (and kept in list_keep field).
+ */
+ if (l->list != NULL)
+ g_queue_free_full (l->list, g_free);
+ l->list = g_queue_new ();
+
+ /* Split the query into space delimeted strings. */
+ query_terms = g_strsplit (text, " ", 10);
+
+ /*
+ * Get the size of the listbox and iterate over it testing each element against «all» words in
+ * query_terms.
+ */
+ size = g_queue_get_length (sl->list_keep);
+ le = g_queue_peek_head_link (sl->list_keep);
+ for (i = 0; i < size; i++, le = g_list_next (le))
+ {
+ WLEntry *e = LENTRY (le->data);
+ gboolean match = TRUE;
+
+ /* Test the query against the list entry. */
+ for (gchar ** p = query_terms; *p != NULL; p++)
+ {
+ if (**p != '\0' && !strcasestr (e->text, *p))
+ {
+ match = FALSE;
+ break;
+ }
+ }
+
+ /* If all the terms matched, then add the element to the list. */
+ if (match)
+ g_queue_push_tail (l->list, filt_listbox_shallow_copy_entry (e, FALSE));
+ }
+ if (listbox_is_empty (l))
+ {
+ listbox_add_item (l, LISTBOX_APPEND_AT_END, 0, "<no search results>", NULL, FALSE);
+ LENTRY (g_queue_peek_head_link (l->list)->data)->index = -2;
+ }
+ size = g_queue_get_length (l->list);
+ if (l->pos >= size)
+ listbox_select_entry (l, size - 1);
+ else
+ listbox_select_entry (l, l->pos);
+
+ g_strfreev (query_terms);
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/* Restores original elements of the list (from sl->list_keep) and turns off the WST_FILTER state. */
+static gboolean
+filt_listbox_set_to_normal_state (WFilteringListbox * sl)
+{
+ WListbox *l = LISTBOX (sl);
+ /* The listbox is already in non-filter state? */
+ if (!widget_get_state (WIDGET (l), WST_FILTER))
+ {
+ /* Return doing no change, just signal the error. */
+ return FALSE;
+ }
+
+ /* The keep-list must be allocated (even if it's empty). */
+ g_assert (sl->list_keep != NULL);
+
+ /* Mark the new state. */
+ widget_set_state (WIDGET (l), WST_FILTER, FALSE);
+
+ /* Release the filtered list and replace it with the original, complete list (it owns the
+ * internal data, hence the release is a shallow one). */
+ g_queue_free_full (l->list, g_free);
+ l->list = sl->list_keep;
+ sl->list_keep = NULL;
+ return TRUE;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/*
+ * Sets the listbox into «filter» state. In this state, there's a separate copy of all list
+ * entries, while the original (and displayed) field ->list is being a filtered version of the
+ * full copy.
+ */
+static gboolean
+filt_listbox_set_to_filter_state (WFilteringListbox * sl)
+{
+ WListbox *l = LISTBOX (sl);
+ GList *le;
+
+ /* The listbox is already in filter state? */
+ if (widget_get_state (WIDGET (l), WST_FILTER))
+ {
+ /* Return doing no change, just signal the error. */
+ return FALSE;
+ }
+
+ /* Mark the new state. */
+ widget_set_state (WIDGET (l), WST_FILTER, TRUE);
+
+ /* No list copy when entering filter mode. */
+ g_assert (sl->list_keep == NULL);
+ sl->list_keep = g_queue_new ();
+
+ /* Skip empty lists. */
+ if (listbox_is_empty (l))
+ return TRUE;
+
+ /*
+ * Remember the original position in the list in the field. It'll be used to determine the
+ * virtual_pos field.
+ */
+ listbox_init_indices (l);
+
+ /* Perform a shallow copy of the original list. */
+ for (le = g_queue_peek_head_link (l->list); le != NULL; le = g_list_next (le))
+ {
+ WLEntry *copy;
+ copy = filt_listbox_shallow_copy_entry (LENTRY (le->data), TRUE);
+ g_queue_push_tail (sl->list_keep, copy);
+ }
+
+ return TRUE;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+/*
+ * If the list is filtered it replaces from the back list (list_keep). It returns whether such
+ * change occurred – FALSE means that the list was already unfiltered.
+ */
+gboolean
+filt_listbox_ensure_unfiltered_state (WFilteringListbox * sl)
+{
+ gboolean ret = FALSE;
+ if (filt_listbox_is_filter_state (sl))
+ ret = filt_listbox_set_to_normal_state (sl);
+ return ret;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+gboolean
+filt_listbox_conditionally_enable_multi_search_init (WFilteringListbox * sl)
+{
+ gboolean start_with_multi_search_active;
+
+ /* Option of starting the listbox with MultiSearch pre-activated. */
+ start_with_multi_search_active =
+ mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION,
+ "multi_search_active_by_default", 1);
+
+ /* CK_MultiSearch toggles the state. */
+ if (start_with_multi_search_active)
+ send_message (WIDGET (sl), NULL, MSG_ACTION, CK_MultiSearch, NULL);
+ else
+ /* Only init embedded position indices. */
+ listbox_init_indices (LISTBOX (sl));
+
+ /* Return if did enable MultiSearch. */
+ return start_with_multi_search_active;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+/*** public functions ****************************************************************************/
+/* --------------------------------------------------------------------------------------------- */
+
+WFilteringListbox *
+filtering_listbox_new (int y, int x, int height, int width,
+ gboolean deletable, lcback_fn callback,
+ filt_listbox_resize_strategy_t resize)
+{
+ WFilteringListbox *object;
+ Widget *w_ref;
+
+ /* Allocate memory for the object body. */
+ object = g_new (WFilteringListbox, 1);
+
+ /* Forward the call to construct the inherited object. */
+ listbox_init (&object->base, y, x, height, width, deletable, callback);
+
+ /* Alter fields of base class. */
+ w_ref = WIDGET (object);
+ w_ref->callback = filt_listbox_callback; /* Set custom callback handler */
+
+ /* Set extending fields of this class. */
+ object->list_keep = NULL; /* No back buffer at startup */
+ object->resize_strategy = resize; /* Save resize strategy */
+ object->initialized = FALSE;
+
+ return object;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+cb_ret_t
+filt_listbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
+{
+ WFilteringListbox *sl = FILT_LISTBOX (w); /* s* - from `screen`, a "screened" listbox */
+ cb_ret_t ret = MSG_NOT_HANDLED;
+ long activity;
+
+ switch (msg)
+ {
+ case MSG_INIT:
+ if (!sl->initialized)
+ {
+ filt_listbox_conditionally_enable_multi_search_init (sl);
+ sl->initialized = TRUE;
+ }
+ /* WListbox doesn't have MSG_INIT, so don't forward. */
+ ret = MSG_HANDLED;
+ break;
+ case MSG_ACTION:
+ if (parm == CK_MultiSearch)
+ {
+ gboolean retval;
+ /* Toggle the multi term searching of any listbox. */
+ if (filt_listbox_is_filter_state (sl))
+ {
+ /* Remove the input widget from the dialog. */
+ filt_listbox_hide_multi_search_widget (sl);
+ /* Restore original (unfiltered) listbox contents. */
+ retval = filt_listbox_set_to_normal_state (sl);
+ }
+ else
+ {
+ /* Add input widget for the filter query at the bottom of the dialog window. */
+ filt_listbox_show_multi_search_widget (sl);
+ /* ... and then turn on the filter state. */
+ retval = filt_listbox_set_to_filter_state (sl);
+ }
+ if (!retval)
+ message (D_ERROR | D_CENTER, MSG_ERROR,
+ "An internal error #3 occurred (filtered listbox support).");
+
+ ret = MSG_HANDLED;
+ }
+ break;
+
+ case MSG_KEY:
+ activity = widget_lookup_key (WIDGET (sl), parm);
+ if (activity == CK_MultiSearch)
+ ret = send_message (w, NULL, MSG_ACTION, CK_MultiSearch, NULL);
+ break;
+
+ case MSG_NOTIFY:
+ if (widget_get_state (w, WST_FILTER))
+ {
+ filt_listbox_filter_list (sl, (char *) data);
+ ret = MSG_HANDLED;
+ }
+ widget_draw (w);
+ /* Protect against normal, non ForwardingInput messages, which might not have sender set. */
+ if (sender)
+ widget_draw (sender);
+ break;
+
+ case MSG_DESTROY:
+ filt_listbox_ensure_unfiltered_state (sl);
+ /* ret is unhandled -> the message will be forwarded to base class. */
+ break;
+ default:
+ break;
+ }
+
+ /* Forward action to base class in case it's not yet handled. */
+ if (ret == MSG_NOT_HANDLED)
+ ret = listbox_callback (w, sender, msg, parm, data);
+
+ return ret;
+}
+
+/* --------------------------------------------------------------------------------------------- */
diff --git a/lib/widget/filtering_listbox.h b/lib/widget/filtering_listbox.h
new file mode 100644
index 000000000..636e75819
--- /dev/null
+++ b/lib/widget/filtering_listbox.h
@@ -0,0 +1,46 @@
+#ifndef MC__FILTERING_LISTBOX_H
+#define MC__FILTERING_LISTBOX_H
+
+/*** typedefs(not structures) and defined constants **********************************************/
+
+/* Casting macros. */
+#define FILT_LISTBOX(x) ((WFilteringListbox *)(x))
+#define CONST_FILT_LISTBOX(x) ((const WFilteringListbox *)(x))
+
+/*** enums ***************************************************************************************/
+
+typedef enum filt_listbox_resize_strategy_e
+{
+ FILT_LIST_EXTEND_DIALOG = 0,
+ FILT_LIST_KEEP_DIALOG_SIZE,
+ FILT_LIST_DIALOG_AUTO_RESIZE
+} filt_listbox_resize_strategy_t;
+
+/*** structures declarations (and typedefs of structures)*****************************************/
+
+typedef struct WFilteringListbox_s
+{
+ WListbox base;
+ gboolean initialized; /* Whether MSG_INIT has been received. */
+
+ /* Fields for new logic. */
+ GQueue *list_keep; /* Unfiltered list (used in WST_FILTER state). */
+ filt_listbox_resize_strategy_t resize_strategy;
+} WFilteringListbox;
+
+/*** global variables defined in .c file *********************************************************/
+
+/*** declarations of public functions ************************************************************/
+
+WFilteringListbox *filtering_listbox_new (int y, int x, int height, int width,
+ gboolean deletable, lcback_fn callback,
+ filt_listbox_resize_strategy_t resize);
+gboolean filt_listbox_ensure_unfiltered_state (WFilteringListbox * l);
+gboolean filt_listbox_conditionally_enable_multi_search_init (WFilteringListbox * l);
+void filt_listbox_select_entry (WFilteringListbox * sl, int dest);
+cb_ret_t filt_listbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
+ void *data);
+
+/*** inline functions ****************************************************************************/
+
+#endif /* MC__FILTERING_LISTBOX_H */
diff --git a/lib/widget/forwarding_input.c b/lib/widget/forwarding_input.c
new file mode 100644
index 000000000..f69440a10
--- /dev/null
+++ b/lib/widget/forwarding_input.c
@@ -0,0 +1,160 @@
+/*
+ A key forwarding extended input class.
+
+ Copyright (C) 2021
+ Free Software Foundation, Inc.
+
+ Written by:
+ Sebastian Gniazdowski <sgniazdowski@gmail.com>, 2021.
+
+ This file is part of the Midnight Commander.
+
+ The Midnight Commander is free software: you can redistribute it
+ and/or modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ The Midnight Commander is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** \file forwarding-input.c
+ * \brief This input class has a feature of forwarding unrecognized keys to a widget passed at
+ * creation.
+ * \author Sebastian Gniazdowski
+ * \date 2021
+ *
+ * It's being used by MultiSearch to allow moving across listbox while typing to the input.
+ */
+
+#include <config.h>
+
+#include "lib/global.h"
+#include "lib/widget.h"
+
+/*** global variables ****************************************************************************/
+
+/*** file scope macro definitions ****************************************************************/
+
+/*** file scope type declarations ****************************************************************/
+
+/*** file scope variables ************************************************************************/
+
+/*** file scope functions ************************************************************************/
+/* --------------------------------------------------------------------------------------------- */
+
+/* --------------------------------------------------------------------------------------------- */
+/*** public functions ****************************************************************************/
+/* --------------------------------------------------------------------------------------------- */
+
+WForwardingInput *
+forwarding_input_new (int y, int x, const int *colors,
+ int len, const char *text, const char *histname,
+ input_complete_t completion_flags, Widget * forward_to_widget)
+{
+ WForwardingInput *object;
+ Widget *w_ref;
+
+ /* Allocate memory for the object body. */
+ object = g_new (WForwardingInput, 1);
+
+ /* Call upper constructor to initialize the inherited object. */
+ input_init (&object->base, y, x, colors, len, text, histname, completion_flags);
+
+ /* Alter fields of base class. */
+ w_ref = WIDGET (object);
+ w_ref->callback = forw_input_callback; /* Set custom callback handler */
+
+ /* Set extending fields of this class. */
+ object->forward_to_widget = forward_to_widget;
+ return object;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+cb_ret_t
+forw_input_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
+{
+ WForwardingInput *in = FORW_INPUT (w);
+ gboolean key_message = FALSE;
+ cb_ret_t ret;
+
+ switch (msg)
+ {
+ case MSG_KEY:
+ ret = forw_input_handle_char (in, parm);
+ key_message = TRUE;
+ break;
+ default:
+ break;
+ }
+
+ /*
+ * Simply pass on all messages to the base class (except for MSG_KEY, which might have
+ * been possibly already sent from forw_input_handle_char() function).
+ */
+
+ if (!key_message)
+ ret = input_callback (WIDGET (in), sender, msg, parm, data);
+
+ return ret;
+}
+
+/* --------------------------------------------------------------------------------------------- */
+
+cb_ret_t
+forw_input_handle_char (WForwardingInput * in, int key)
+{
+ cb_ret_t ret = MSG_NOT_HANDLED;
+ gboolean sent_to_base = FALSE;
+ long activity;
+ char *str_cp;
+
+ /* Save to detect if a change happened. */
+ str_cp = g_strdup (in->base.buffer);
+
+ /* Is this key recognized by the base object? */
+ activity = widget_lookup_key (WIDGET (in), key);
+ if (activity != CK_IgnoreKey && activity != CK_Complete)
+ {
+ /* Yes -> send the key to the upper class. */
+ ret = input_callback (WIDGET (in), WIDGET (in), MSG_KEY, key, NULL);
+ sent_to_base = TRUE;
+ }
+ /* Should we try to forward the key to any paired widget? */
+ if (in->forward_to_widget != NULL && ret == MSG_NOT_HANDLED)
+ {
+ /* Is it maybe recognized by forward_to_widget paired object? */
+ activity = widget_lookup_key (WIDGET (in->forward_to_widget), key);
+ if (activity != CK_IgnoreKey)
+ {
+ /* Yes - forward the key to the paired widget (most probably WListbox). */
+ ret = send_message (WIDGET (in->forward_to_widget), NULL, MSG_KEY, key, NULL);
+ }
+ }
+
+ /*
+ * If not handled yet, then send the key to the base object for general recognition (if
+ * not already done that).
+ */
+
+ if (!sent_to_base && ret == MSG_NOT_HANDLED)
+ {
+ ret = input_callback (WIDGET (in), WIDGET (in), MSG_KEY, key, NULL);
+ sent_to_base = TRUE; /* currently unused */
+ }
+
+ /* Send update signal to paired widget «iff» input's text has changed. */
+ if (in->forward_to_widget != NULL && g_strcmp0 (str_cp, in->base.buffer) != 0)
+ send_message (WIDGET (in->forward_to_widget), NULL, MSG_NOTIFY, key, in->base.buffer);
+ g_free (str_cp);
+
+ return ret;
+}
+
+/* --------------------------------------------------------------------------------------------- */
diff --git a/lib/widget/forwarding_input.h b/lib/widget/forwarding_input.h
new file mode 100644
index 000000000..dc4e49423
--- /dev/null
+++ b/lib/widget/forwarding_input.h
@@ -0,0 +1,36 @@
+#ifndef MC__FORWARDING_INPUT_H
+#define MC__FORWARDING_INPUT_H
+
+/*** typedefs(not structures) and defined constants **********************************************/
+
+/* Casting macros. */
+#define FORW_INPUT(x) ((WForwardingInput *)(x))
+#define CONST_FORW_INPUT(x) ((const WForwardingInput *)(x))
+
+typedef struct
+{
+ WInput base;
+
+ /* Fields for new logic. */
+ Widget *forward_to_widget; /* The paired widget to receive unhandled keys */
+} WForwardingInput;
+
+/*** enums ***************************************************************************************/
+
+/*** structures declarations (and typedefs of structures)*****************************************/
+
+/*** global variables defined in .c file *********************************************************/
+
+/*** declarations of public functions ************************************************************/
+
+WForwardingInput *forwarding_input_new (int y, int x, const int *colors,
+ int len, const char *text, const char *histname,
+ input_complete_t completion_flags,
+ Widget * forward_to_widget);
+
+cb_ret_t forw_input_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data);
+cb_ret_t forw_input_handle_char (WForwardingInput * in, int key);
+
+/*** inline functions ****************************************************************************/
+
+#endif /* MC__FORWARDING_INPUT_H */
diff --git a/lib/widget/group.c b/lib/widget/group.c
index f8e318bd9..f16de833f 100644
--- a/lib/widget/group.c
+++ b/lib/widget/group.c
@@ -749,15 +749,20 @@ group_add_widget_autopos (WGroup * g, void *w, widget_pos_flags_t pos_flags, con
* @param w Widget object
*/
void
-group_remove_widget (void *w)
+group_remove_widget (void *wid)
{
+ Widget *w = WIDGET (wid);
WGroup *g;
GList *d;
/* Don't accept NULL widget. This shouldn't happen */
assert (w != NULL);
- g = WIDGET (w)->owner;
+ /* Invoke widget's pre unlink callback. */
+ if (w->pre_unlink_func != NULL)
+ w->pre_unlink_func (w);
+
+ g = w->owner;
d = g_list_find (g->widgets, w);
if (d == g->current)
@@ -774,7 +779,7 @@ group_remove_widget (void *w)
group_select_current_widget (g);
}
- WIDGET (w)->owner = NULL;
+ w->owner = NULL;
}
/* --------------------------------------------------------------------------------------------- */
diff --git a/lib/widget/history.c b/lib/widget/history.c
index 775d02b1b..2ee719ca7 100644
--- a/lib/widget/history.c
+++ b/lib/widget/history.c
@@ -66,44 +66,53 @@ typedef struct
/*** file scope functions ************************************************************************/
static cb_ret_t
-history_dlg_reposition (WDialog * dlg_head)
+history_dlg_reposition (WDialog * dlg_head, WRect * resize)
{
- history_dlg_data *data;
- int x = 0, y, he, wi;
WRect r;
- /* guard checks */
- if ((dlg_head == NULL) || (dlg_head->data == NULL))
- return MSG_NOT_HANDLED;
-
- data = (history_dlg_data *) dlg_head->data;
-
- y = data->y;
- he = data->count + 2;
-
- if (he <= y || y > (LINES - 6))
+ if (resize == NULL)
{
- he = MIN (he, y - 1);
- y -= he;
+ history_dlg_data *data;
+ int x = 0, y, he, wi;
+
+ /* guard checks */
+ if ((dlg_head == NULL) || (dlg_head->data == NULL))
+ return MSG_NOT_HANDLED;
+
+ data = (history_dlg_data *) dlg_head->data;
+
+ y = data->y;
+ he = data->count + 2;
+
+ if (he <= y || y > (LINES - 6))