-
Notifications
You must be signed in to change notification settings - Fork 4
/
gtk_findreplace.c
1215 lines (1017 loc) · 48 KB
/
gtk_findreplace.c
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
// #include "gtk_windef.h"
#include "gtk_findreplace.h"
/* find replace keypress handler */
static gboolean on_fr_keypress (GtkWidget *widget, GdkEventKey *event,
kwinst *app);
GtkWidget *create_find_dlg (kwinst *app)
{
GtkWidget *vbox; /* vbox container */
GtkWidget *frame1;
GtkWidget *frame2;
GtkWidget *table;
GtkWidget *table2;
GtkWidget *label;
GtkWidget *hbtweak;
GtkWidget *hbox;
GtkWidget *btnfind;
GtkWidget *btnclose;
guint i;
/* create toplevel window */
if (!(app->findrepwin = gtk_window_new (GTK_WINDOW_TOPLEVEL))) {
err_dialog ("create_find_dlg() gtk_window_new failure.");
return NULL;
}
gtk_window_set_position (GTK_WINDOW (app->findrepwin), GTK_WIN_POS_CENTER);
gtk_window_set_default_size (GTK_WINDOW (app->findrepwin), 264, 264);
gtk_window_set_title (GTK_WINDOW (app->findrepwin), "Find Text");
/* for inclusion in main app set modal and transient_for (to keep on top)
* gtk_window_set_modal (GtkWindow *window, gboolean modal);
* gtk_window_set_transient_for(GtkWindow *window, GtkWindow *parent)
*/
gtk_window_set_modal (GTK_WINDOW(app->findrepwin), TRUE);
gtk_window_set_transient_for (GTK_WINDOW(app->findrepwin),
GTK_WINDOW(app->window));
gtk_container_set_border_width (GTK_CONTAINER (app->findrepwin), 5);
g_signal_connect (app->findrepwin, "destroy",
G_CALLBACK (btnclose_activate), app);
app->dlgid = DLGFIND; /* set dialog id to find dialog */
/* main vbox container
* spacing profided on frames as containers
*/
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (app->findrepwin), vbox);
gtk_widget_show (vbox);
/* frame1 - Search Box */
frame1 = gtk_frame_new (NULL);
gtk_frame_set_label (GTK_FRAME (frame1), "Find");
gtk_frame_set_label_align (GTK_FRAME (frame1), 0.0, 0.5);
gtk_frame_set_shadow_type (GTK_FRAME (frame1), GTK_SHADOW_ETCHED_OUT);
gtk_container_set_border_width (GTK_CONTAINER (frame1), 5);
/* table inside frame1 */
table = gtk_table_new (3, 2, TRUE);
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 3);
gtk_container_set_border_width (GTK_CONTAINER (table), 5);
gtk_container_add (GTK_CONTAINER (frame1), table);
/* label for combo entry */
label = gtk_label_new_with_mnemonic ("_Text to Find:");
hbtweak = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (hbtweak), label, FALSE, FALSE, 0);
gtk_table_attach_defaults (GTK_TABLE (table), hbtweak, 0, 1, 0, 1);
gtk_widget_show (hbtweak);
gtk_table_set_row_spacing (GTK_TABLE (table), 0, 0);
/* combo box entry GTK_COMBO_BOX_TEXT */
app->entryfind = gtk_combo_box_text_new_with_entry();
gtk_label_set_mnemonic_widget (GTK_LABEL (label), app->entryfind);
gtk_table_attach_defaults (GTK_TABLE (table), app->entryfind, 0, 2, 1, 2);
gtk_table_set_row_spacing (GTK_TABLE (table), 1, 10);
for (i = 0; i < app->nfentries; i++)
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(app->entryfind),
app->findtext[i]);
/* regex checkbox */
app->chkregex = gtk_check_button_new_with_mnemonic ("Regular e_xpression");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkregex), app->optregex);
gtk_table_attach_defaults (GTK_TABLE (table), app->chkregex, 0, 1, 2, 3);
/* regex edit button */
app->btnregex = gtk_button_new_with_mnemonic ("_Edit...");
gtk_widget_set_size_request (app->btnregex, 80, 24);
gtk_widget_set_sensitive (app->btnregex, app->optregex);
hbtweak = gtk_hbox_new (FALSE, 0);
gtk_box_pack_end (GTK_BOX (hbtweak), app->btnregex, FALSE, FALSE, 0);
gtk_table_attach_defaults (GTK_TABLE (table), hbtweak, 1, 2, 2, 3);
// gtk_container_add (GTK_CONTAINER (frame1), app->entryfind);
gtk_widget_show (app->entryfind);
gtk_widget_show (label);
gtk_widget_show (hbtweak);
gtk_widget_show (app->chkregex);
gtk_widget_show (app->btnregex);
gtk_widget_show (table);
gtk_widget_show (frame1);
gtk_box_pack_start (GTK_BOX (vbox), frame1, FALSE, FALSE, 0);
/* frame2 - Options */
frame2 = gtk_frame_new (NULL);
gtk_frame_set_label (GTK_FRAME (frame2), "Options");
gtk_frame_set_label_align (GTK_FRAME (frame2), 0.0, 0.5);
gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_ETCHED_OUT);
gtk_container_set_border_width (GTK_CONTAINER (frame2), 5);
gtk_widget_show (frame2);
gtk_box_pack_start (GTK_BOX (vbox), frame2, FALSE, FALSE, 0);
/* table in frame2 */
table2 = gtk_table_new (3, 2, TRUE);
gtk_table_set_row_spacings (GTK_TABLE (table2), 5);
gtk_table_set_col_spacings (GTK_TABLE (table2), 3);
gtk_container_set_border_width (GTK_CONTAINER (table2), 5);
gtk_container_add (GTK_CONTAINER (frame2), table2);
/* options checkboxs */
app->chkcase = gtk_check_button_new_with_mnemonic ("C_ase sensitive");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkcase), app->optcase);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkcase, 0, 1, 0, 1);
gtk_widget_show (app->chkcase);
app->chkwhole = gtk_check_button_new_with_mnemonic ("_Whole words only");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkwhole), app->optwhole);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkwhole, 0, 1, 1, 2);
gtk_widget_show (app->chkwhole);
app->chkfrom = gtk_check_button_new_with_mnemonic ("From c_ursor");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkfrom), app->optfrom);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkfrom, 0, 1, 2, 3);
gtk_widget_show (app->chkfrom);
app->chkback = gtk_check_button_new_with_mnemonic ("Find _backwards");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkback), app->optback);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkback, 1, 2, 0, 1);
gtk_widget_show (app->chkback);
chk_existing_selection (app); /* sets app->optselect, create marks */
app->chkselect = gtk_check_button_new_with_mnemonic ("_Selected text");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkselect), app->optselect);
// gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkselect), TRUE);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkselect, 1, 2, 1, 2);
gtk_widget_show (app->chkselect);
gtk_widget_show (table2);
/* hbox (replacing table) button spacing */
hbox = gtk_hbox_new (FALSE, 0);
gtk_box_set_spacing (GTK_BOX (hbox), 5);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
/* button sizes 80 x 24 */
btnfind = gtk_button_new_with_mnemonic ("_Find");
app->btnfind = btnfind; /* FIXME - remove btnfind? */
gtk_widget_set_size_request (btnfind, 80, 24);
// gtk_box_pack_end (GTK_BOX (hbox), btnfind, FALSE, FALSE, 0); // (moved after btnclose)
gtk_widget_set_sensitive (btnfind, app->findcbchgd);
gtk_widget_show (btnfind);
btnclose = gtk_button_new_with_mnemonic ("_Close");
gtk_widget_set_size_request (btnclose, 80, 24);
gtk_box_pack_end (GTK_BOX (hbox), btnclose, FALSE, FALSE, 0);
gtk_box_pack_end (GTK_BOX (hbox), btnfind, FALSE, FALSE, 0); // pack on rt-side.
gtk_widget_show (btnclose);
gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
g_signal_connect (app->findrepwin, "show",
G_CALLBACK (on_window_show), app);
g_signal_connect (app->chkregex, "toggled",
G_CALLBACK (chkregex_toggled), app);
g_signal_connect (app->chkcase, "toggled",
G_CALLBACK (chkcase_toggled), app);
g_signal_connect (app->chkwhole, "toggled",
G_CALLBACK (chkwhole_toggled), app);
g_signal_connect (app->chkfrom, "toggled",
G_CALLBACK (chkfrom_toggled), app);
g_signal_connect (app->chkback, "toggled",
G_CALLBACK (chkback_toggled), app);
g_signal_connect (app->chkselect, "toggled",
G_CALLBACK (chkselect_toggled), app);
g_signal_connect (btnfind, "clicked",
G_CALLBACK (btnfind_activate), app);
g_signal_connect (btnclose, "clicked",
G_CALLBACK (btnclose_activate), app);
g_signal_connect (app->findrepwin, "key_press_event",
G_CALLBACK (on_fr_keypress), app);
g_signal_connect (app->entryfind, "changed",
G_CALLBACK (entry_find_activate), app);
// g_signal_connect_swapped (btnclose, "clicked",
// G_CALLBACK (delete_event),
// window);
gtk_widget_show (app->findrepwin);
// gtk_widget_show_all (app->findrepwin);
return (app->findrepwin);
}
GtkWidget *create_replace_dlg (kwinst *app)
{
GtkWidget *vbox; /* vbox container */
GtkWidget *frame1;
GtkWidget *framer;
GtkWidget *frame2;
GtkWidget *table;
GtkWidget *table2;
GtkWidget *label;
GtkWidget *hbtweak;
GtkWidget *hbox;
GtkWidget *btnreplace;
GtkWidget *btnclose;
guint i;
/* create toplevel window */
if (!(app->findrepwin = gtk_window_new (GTK_WINDOW_TOPLEVEL))) {
err_dialog ("create_replace_dlg() gtk_window_new failure.");
return NULL;
}
gtk_window_set_position (GTK_WINDOW (app->findrepwin), GTK_WIN_POS_CENTER);
gtk_window_set_default_size (GTK_WINDOW (app->findrepwin), 264, 264);
gtk_window_set_title (GTK_WINDOW (app->findrepwin), "Replace Text");
gtk_window_set_modal (GTK_WINDOW(app->findrepwin), TRUE);
gtk_window_set_transient_for (GTK_WINDOW(app->findrepwin),
GTK_WINDOW(app->window));
gtk_container_set_border_width (GTK_CONTAINER (app->findrepwin), 5);
g_signal_connect (app->findrepwin, "destroy",
G_CALLBACK (btnclose_activate), app);
app->dlgid = DLGREPL; /* set dialog id to replace dialog */
/* main vbox container
* spacing profided on frames as containers
*/
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER (app->findrepwin), vbox);
gtk_widget_show (vbox);
/* frame1 - Search Box */
frame1 = gtk_frame_new (NULL);
gtk_frame_set_label (GTK_FRAME (frame1), "Find");
gtk_frame_set_label_align (GTK_FRAME (frame1), 0.0, 0.5);
gtk_frame_set_shadow_type (GTK_FRAME (frame1), GTK_SHADOW_ETCHED_OUT);
gtk_container_set_border_width (GTK_CONTAINER (frame1), 5);
/* table inside frame1 */
table = gtk_table_new (3, 2, TRUE);
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 3);
gtk_container_set_border_width (GTK_CONTAINER (table), 5);
gtk_container_add (GTK_CONTAINER (frame1), table);
/* label for combo entry */
label = gtk_label_new_with_mnemonic ("_Text to find:");
hbtweak = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (hbtweak), label, FALSE, FALSE, 0);
gtk_table_attach_defaults (GTK_TABLE (table), hbtweak, 0, 1, 0, 1);
gtk_widget_show (hbtweak);
gtk_table_set_row_spacing (GTK_TABLE (table), 0, 0);
/* combo box entry (GtkComboBoxText *) */
app->entryfind = gtk_combo_box_text_new_with_entry();
gtk_label_set_mnemonic_widget (GTK_LABEL (label), app->entryfind);
gtk_table_attach_defaults (GTK_TABLE (table), app->entryfind, 0, 2, 1, 2);
gtk_table_set_row_spacing (GTK_TABLE (table), 1, 10);
for (i = 0; i < app->nfentries; i++)
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(app->entryfind),
app->findtext[i]);
/* regex checkbox */
app->chkregex = gtk_check_button_new_with_mnemonic ("Regular e_xpression");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkregex), app->optregex);
gtk_table_attach_defaults (GTK_TABLE (table), app->chkregex, 0, 1, 2, 3);
/* regex edit button - set active state FALSE - gray Edit... button */
app->btnregex = gtk_button_new_with_mnemonic ("_Edit...");
gtk_widget_set_size_request (app->btnregex, 80, 24);
if (!app->optregex)
gtk_widget_set_sensitive (app->btnregex, FALSE);
hbtweak = gtk_hbox_new (FALSE, 0);
gtk_box_pack_end (GTK_BOX (hbtweak), app->btnregex, FALSE, FALSE, 0);
gtk_table_attach_defaults (GTK_TABLE (table), hbtweak, 1, 2, 2, 3);
// gtk_container_add (GTK_CONTAINER (frame1), app->entryfind);
gtk_widget_show (app->entryfind);
gtk_widget_show (label);
gtk_widget_show (hbtweak);
gtk_widget_show (app->chkregex);
gtk_widget_show (app->btnregex);
gtk_widget_show (table);
gtk_widget_show (frame1);
gtk_box_pack_start (GTK_BOX (vbox), frame1, FALSE, FALSE, 0);
/* framer - Replace Box */
framer = gtk_frame_new (NULL);
gtk_frame_set_label (GTK_FRAME (framer), "Replace With");
gtk_frame_set_label_align (GTK_FRAME (framer), 0.0, 0.5);
gtk_frame_set_shadow_type (GTK_FRAME (framer), GTK_SHADOW_ETCHED_OUT);
gtk_container_set_border_width (GTK_CONTAINER (framer), 5);
/* table inside framer */
table = gtk_table_new (3, 2, TRUE);
gtk_table_set_row_spacings (GTK_TABLE (table), 5);
gtk_table_set_col_spacings (GTK_TABLE (table), 3);
gtk_container_set_border_width (GTK_CONTAINER (table), 5);
gtk_container_add (GTK_CONTAINER (framer), table);
/* label for combo entry */
label = gtk_label_new_with_mnemonic ("Replace_ment text:");
hbtweak = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (hbtweak), label, FALSE, FALSE, 0);
gtk_table_attach_defaults (GTK_TABLE (table), hbtweak, 0, 1, 0, 1);
gtk_widget_show (hbtweak);
gtk_table_set_row_spacing (GTK_TABLE (table), 0, 0);
/* combo box entry (GtkComboBoxText *) */
app->entryreplace = gtk_combo_box_text_new_with_entry();
gtk_label_set_mnemonic_widget (GTK_LABEL (label), app->entryreplace);
gtk_table_attach_defaults (GTK_TABLE (table), app->entryreplace, 0, 2, 1, 2);
gtk_table_set_row_spacing (GTK_TABLE (table), 1, 10);
for (i = 0; i < app->nrentries; i++)
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(app->entryreplace),
app->reptext[i]);
/* regex checkbox */
app->chkplace = gtk_check_button_new_with_mnemonic ("Use p_laceholders");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkplace), app->optplace);
gtk_table_attach_defaults (GTK_TABLE (table), app->chkplace, 0, 1, 2, 3);
/* regex edit button */
app->btnplace = gtk_button_new_with_mnemonic ("Insert Place_holder");
gtk_widget_set_size_request (app->btnplace, 110, 24);
if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (app->chkplace)))
gtk_widget_set_sensitive (app->btnplace, FALSE);
hbtweak = gtk_hbox_new (FALSE, 0);
gtk_box_pack_end (GTK_BOX (hbtweak), app->btnplace, FALSE, FALSE, 0);
gtk_table_attach_defaults (GTK_TABLE (table), hbtweak, 1, 2, 2, 3);
// gtk_container_add (GTK_CONTAINER (framer), app->entryreplace);
gtk_widget_show (app->entryreplace);
gtk_widget_show (label);
gtk_widget_show (hbtweak);
gtk_widget_show (app->chkplace);
gtk_widget_show (app->btnplace);
gtk_widget_show (table);
gtk_widget_show (framer);
gtk_box_pack_start (GTK_BOX (vbox), framer, FALSE, FALSE, 0);
/* frame2 - Options */
frame2 = gtk_frame_new (NULL);
gtk_frame_set_label (GTK_FRAME (frame2), "Options");
gtk_frame_set_label_align (GTK_FRAME (frame2), 0.0, 0.5);
gtk_frame_set_shadow_type (GTK_FRAME (frame2), GTK_SHADOW_ETCHED_OUT);
gtk_container_set_border_width (GTK_CONTAINER (frame2), 5);
gtk_widget_show (frame2);
gtk_box_pack_start (GTK_BOX (vbox), frame2, FALSE, FALSE, 0);
/* table in frame2 */
table2 = gtk_table_new (3, 2, TRUE);
gtk_table_set_row_spacings (GTK_TABLE (table2), 5);
gtk_table_set_col_spacings (GTK_TABLE (table2), 3);
gtk_container_set_border_width (GTK_CONTAINER (table2), 5);
gtk_container_add (GTK_CONTAINER (frame2), table2);
/* options checkboxs */
app->chkcase = gtk_check_button_new_with_mnemonic ("C_ase sensitive");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkcase), app->optcase);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkcase, 0, 1, 0, 1);
gtk_widget_show (app->chkcase);
app->chkwhole = gtk_check_button_new_with_mnemonic ("_Whole words only");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkwhole), app->optwhole);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkwhole, 0, 1, 1, 2);
gtk_widget_show (app->chkwhole);
app->chkfrom = gtk_check_button_new_with_mnemonic ("From c_ursor");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkfrom), app->optfrom);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkfrom, 0, 1, 2, 3);
gtk_widget_show (app->chkfrom);
app->chkback = gtk_check_button_new_with_mnemonic ("Find _backwards");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkback), app->optback);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkback, 1, 2, 0, 1);
gtk_widget_show (app->chkback);
chk_existing_selection (app); /* sets app->optselect, create marks */
app->chkselect = gtk_check_button_new_with_mnemonic ("_Selected text");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkselect), app->optselect);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkselect, 1, 2, 1, 2);
gtk_widget_show (app->chkselect);
app->chkprompt = gtk_check_button_new_with_mnemonic ("_Prompt on replace");
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (app->chkprompt), app->optprompt);
gtk_table_attach_defaults (GTK_TABLE (table2), app->chkprompt, 1, 2, 2, 3);
gtk_widget_show (app->chkprompt);
gtk_widget_show (table2);
/* hbox (replacing table) button spacing */
hbox = gtk_hbox_new (FALSE, 0);
gtk_box_set_spacing (GTK_BOX (hbox), 5);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
btnreplace = gtk_button_new_with_mnemonic ("_Replace");
app->btnreplace = btnreplace; /* FIXME - remove btnreplace? */
gtk_widget_set_size_request (btnreplace, 80, 24);
// gtk_box_pack_end (GTK_BOX (hbox), btnreplace, FALSE, FALSE, 0); // (moved after btnclose)
gtk_widget_set_sensitive (btnreplace, app->findcbchgd);
gtk_widget_show (btnreplace);
btnclose = gtk_button_new_with_mnemonic ("_Close");
gtk_widget_set_size_request (btnclose, 80, 24);
gtk_box_pack_end (GTK_BOX (hbox), btnclose, FALSE, FALSE, 0);
gtk_box_pack_end (GTK_BOX (hbox), btnreplace, FALSE, FALSE, 0); // pack on rt-side.
gtk_widget_show (btnclose);
gtk_box_pack_end (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
gtk_widget_show (hbox);
g_signal_connect (app->findrepwin, "show",
G_CALLBACK (on_window_show), app);
g_signal_connect (app->chkregex, "toggled",
G_CALLBACK (chkregex_toggled), app);
g_signal_connect (app->chkplace, "toggled",
G_CALLBACK (chkplace_toggled), app);
g_signal_connect (app->chkcase, "toggled",
G_CALLBACK (chkcase_toggled), app);
g_signal_connect (app->chkwhole, "toggled",
G_CALLBACK (chkwhole_toggled), app);
g_signal_connect (app->chkfrom, "toggled",
G_CALLBACK (chkfrom_toggled), app);
g_signal_connect (app->chkback, "toggled",
G_CALLBACK (chkback_toggled), app);
g_signal_connect (app->chkselect, "toggled",
G_CALLBACK (chkselect_toggled), app);
g_signal_connect (app->chkprompt, "toggled",
G_CALLBACK (chkprompt_toggled), app);
g_signal_connect (btnreplace, "clicked",
G_CALLBACK (btnreplace_activate), app);
g_signal_connect (btnclose, "clicked",
G_CALLBACK (btnclose_activate), app);
/* pass keypress to handler */
g_signal_connect (app->findrepwin, "key_press_event",
G_CALLBACK (on_fr_keypress), app);
/* set replace button sensitive, require entries in both */
g_signal_connect (app->entryfind, "changed",
G_CALLBACK (entry_set_find_sensitive), app);
g_signal_connect (app->entryfind, "changed",
G_CALLBACK (entry_replace_activate), app);
g_signal_connect (app->entryreplace, "changed",
G_CALLBACK (entry_set_repl_sensitive), app);
g_signal_connect (app->entryreplace, "changed",
G_CALLBACK (entry_replace_activate), app);
gtk_widget_show (app->findrepwin);
// gtk_widget_show_all (app->findrepwin);
return (app->findrepwin);
}
/** on window create/show function */
void on_window_show (GtkWidget *widget, kwinst *app)
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
GtkTextIter iter;
if (app->markfrom) { /* free reset existing mark */
gtk_text_buffer_delete_mark (buffer, app->markfrom);
app->markfrom = NULL;
}
/* get iter at insert, create app->markfrom text mark */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
gtk_text_buffer_get_insert (buffer));
app->markfrom = gtk_text_buffer_create_mark (buffer, "markfrom",
&iter, FALSE);
if (!app->markfrom)
g_print ("setting 'markfrom' - FAILED!\n");
if (widget) {}
}
/* entry helper callbacks for replace button set sensitive */
void entry_set_find_sensitive (GtkWidget *widget, kwinst *app) {
app->findcbchgd = TRUE;
if (widget) {}
}
void entry_set_repl_sensitive (GtkWidget *widget, kwinst *app) {
app->replcbchgd = TRUE;
if (widget) {}
}
/* entry comboboxes */
void entry_find_activate (GtkWidget *widget, kwinst *app) {
app->findcbchgd = TRUE; /* just set, or add set callback to find dlg */
/* and change to if (app->findcbchgd) */
gtk_widget_set_sensitive (app->btnfind, app->findcbchgd);
if (widget) {}
}
void entry_replace_activate (GtkWidget *widget, kwinst *app) {
if (app->findcbchgd && app->replcbchgd) /* require both for replace */
gtk_widget_set_sensitive (app->btnreplace, app->findcbchgd);
if (widget) {}
}
/* option checkboxs */
void chkregex_toggled (GtkWidget *widget, kwinst *app)
{
app->optregex = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
gtk_widget_set_sensitive (app->btnregex, app->optregex);
}
void chkplace_toggled (GtkWidget *widget, kwinst *app)
{
app->optplace = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
gtk_widget_set_sensitive (app->btnplace, app->optplace);
}
void chkcase_toggled (GtkWidget *widget, kwinst *app)
{
app->optcase = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
}
void chkwhole_toggled (GtkWidget *widget, kwinst *app)
{
app->optwhole = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
}
void chkfrom_toggled (GtkWidget *widget, kwinst *app)
{
app->optfrom = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
}
void chkback_toggled (GtkWidget *widget, kwinst *app)
{
app->optback = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
}
void chkselect_toggled (GtkWidget *widget, kwinst *app)
{
app->optselect = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
}
void chkprompt_toggled (GtkWidget *widget, kwinst *app)
{
app->optprompt = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
}
/* dialog buttons */
void btnregex_activate (GtkWidget *widget, kwinst *app)
{
if (app) {}
if (widget) {}
}
void btnplace_activate (GtkWidget *widget, kwinst *app)
{
if (app) {}
if (widget) {}
}
/** check if selection exists when find/replace dlg called,
* create marks for beginning and end to restore after
* find/replace operations
*/
void chk_existing_selection (kwinst *app)
{
GtkTextIter sel_start, sel_end;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
gboolean selected = FALSE;
/* check whether existing selection active */
selected = gtk_text_buffer_get_selection_bounds (buffer,
&sel_start, &sel_end);
if (selected && !gtk_text_iter_equal (&sel_start, &sel_end)) {
app->optselect = TRUE;
app->selstart = gtk_text_buffer_create_mark (buffer,
"selstart", &sel_start, FALSE);
app->selend = gtk_text_buffer_create_mark (buffer,
"selend", &sel_end, FALSE);
}
else {
app->optselect = FALSE;
app->selstart = app->selend = NULL;
}
}
/** delete the last_pos mark */
/* (moved to gtk_appdata.[ch] 20170801)
void delete_mark_last_pos (kwinst *app)
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
if (app->last_pos)
gtk_text_buffer_delete_mark (buffer, app->last_pos);
app->last_pos = NULL;
}
*/
/** case insensitive forward search for implementation without
* GtkSourceView.
*/
gboolean gtk_text_iter_forward_search_nocase (GtkTextIter *iter,
const gchar *text,
GtkTextSearchFlags flags,
GtkTextIter *mstart,
GtkTextIter *mend)
{
gunichar c;
gchar *lctext = g_strdup (text); /* copy text */
gsize textlen = g_strlen (text); /* get length */
str2lower (lctext);
for (;;) { /* iterate over all chars in range */
gsize len = textlen; /* get char at iter */
c = g_unichar_tolower (gtk_text_iter_get_char (iter));
if (c == (gunichar)lctext[0]) /* compare 1st in lctext */
{
*mstart = *iter; /* set start iter to current */
for (gsize i = 0; i < len; i++)
{
c = g_unichar_tolower (gtk_text_iter_get_char (iter));
/* compare/advance -- order IS important */
if (c != (gunichar)lctext[i] ||
!gtk_text_iter_forward_char (iter))
goto next; /* start next search */
}
*mend = *iter; /* set end iter */
if (lctext) g_free (lctext); /* free lctext */
return TRUE; /* return true */
}
next:; /* if at end of selecton break */
if (!gtk_text_iter_forward_char (iter))
break;
}
if (lctext) g_free (lctext); /* free lctext */
if (mstart || mend || flags) {}
return FALSE;
}
/** case insensitive backward search for implementation without
* GtkSourceView.
*/
gboolean gtk_text_iter_backward_search_nocase (GtkTextIter *iter,
const gchar *text,
GtkTextSearchFlags flags,
GtkTextIter *mstart,
GtkTextIter *mend)
{
gunichar c;
gchar *lctext = g_strdup (text); /* copy text */
gsize textlen = g_strlen (text); /* get length */
str2lower (lctext); /* convert to lower-case */
*mend = *iter; /* initialize end iterator */
while (gtk_text_iter_backward_char (iter)) {
gsize len = textlen - 1; /* index for last in lctext */
c = g_unichar_tolower (gtk_text_iter_get_char (iter));
if (c == (gunichar)lctext[len]) /* initial comparison */
{
/* iterate over remaining chars in lctext/compare */
while (len-- && gtk_text_iter_backward_char (iter))
{
c = g_unichar_tolower (gtk_text_iter_get_char (iter));
if (c != (gunichar)lctext[len]) {
/* reset iter to right of char */
gtk_text_iter_forward_char (iter);
goto prev;
}
}
*mstart = *iter; /* set start iter before last char */
if (lctext) g_free (lctext); /* free lctext */
return TRUE; /* return success */
}
prev:;
*mend = *iter; /* set end iter after next search char */
}
if (lctext) g_free (lctext); /* free lctext */
if (mstart || mend || flags) {}
return FALSE; /* no match */
}
/** common find function for both find/replace dialogs, locates text
* within app->buffer and sets app->last_pos for next search begin,
* test match against dialog options, sets app->txtfound on success.
*/
void find (kwinst *app, const gchar *text)
{
if (!text || !*text) return;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
GtkTextIter iter, mstart, mend;
gboolean found = FALSE, wrapped = FALSE;
/* start infinite loop here, loop until all options satisfied or end
* of buffer reached, then break setting app->txtfound as needed.
* TODO: handle if existing selection was made from end->start, requires
* checking iter from gtk_text_buffer_get_insert and selstart/selend.
* TODO: activate find/replace on [enter]/[kp_return] from findtext and
* replacetext combo box entry.
* TODO: F3 - btnfind_activate (think about Prompt on Replace)
^ TODO: case insensitive doesn't keep last whole-word highlighted if
* subsequent search does not match.
*/
for (;;) { /* loop until word found matching search options or end */
// gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER(buffer));
if (!app->last_pos) { /* find first occurrence */
if (wrapped) { /* search again from start */
if (!app->optback) { /* search again forward dir */
if (app->optselect) { /* search again in selection */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
app->selstart);
}
else if (app->optfrom) { /* search again at cursor */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
app->markfrom);
}
else { /* search again from beginning of buffer */
gtk_text_buffer_get_start_iter (buffer, &iter);
}
}
else { /* search again backward directions */
if (app->optselect) { /* search again selection */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
app->selend);
}
else if (app->optfrom) { /* search again cursor */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
app->markfrom);
}
else { /* search again from end of buffer */
gtk_text_buffer_get_end_iter (buffer, &iter);
}
}
wrapped = FALSE;
}
else if (!app->optback) { /* forward search */
if (app->optfrom) { /* if search from cursor */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
gtk_text_buffer_get_insert (buffer));
}
else if (app->optselect) /* search from select start */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
app->selstart);
else /* otherwise get start iter */
gtk_text_buffer_get_start_iter (buffer, &iter);
}
else { /* backwards search */
if (app->optfrom) /* get iter at insert */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
gtk_text_buffer_get_insert (buffer));
else if (app->optselect) /* get iter at selend */
gtk_text_buffer_get_iter_at_mark (buffer, &iter,
app->selend);
else /* start at end of buffer */
gtk_text_buffer_get_end_iter (buffer, &iter);
}
}
else /* find next occurrence */
gtk_text_buffer_get_iter_at_mark (buffer, &iter, app->last_pos);
/* case sensitive forward/reverse search from iter for 'text' setting
* iters mstart & mend pointing to first & last char in matched text.
*/
if (app->optback) { /* search backward */
if (app->optcase) { /* case sensitive */
found = gtk_text_iter_backward_search (&iter, text, 0,
&mstart, &mend, NULL);
}
else { /* case insensitive backwards search */
#ifdef HAVESOURCEVIEW
found = gtk_source_iter_backward_search (&iter, text,
GTK_SOURCE_SEARCH_CASE_INSENSITIVE,
&mstart, &mend, NULL);
#else
found = gtk_text_iter_backward_search_nocase (&iter, text, 0,
&mstart, &mend);
#endif
}
}
else { /* search forward */
if (app->optcase) { /* case sensitive */
found = gtk_text_iter_forward_search (&iter, text, 0,
&mstart, &mend, NULL);
}
else { /* case insensitive forward search */
#ifdef HAVESOURCEVIEW
found = gtk_source_iter_forward_search (&iter, text,
GTK_SOURCE_SEARCH_CASE_INSENSITIVE,
&mstart, &mend, NULL);
#else
found = gtk_text_iter_forward_search_nocase (&iter, text, 0,
&mstart, &mend);
#endif
}
}
if (found) /* text found in buffer, now refine match w/options */
{
/* if optselect, in both forward/backward search check iter
* against mend/mstart to determine if search is within
* original selection range.
*/
if (app->optselect) { /* if srch in selected-text */
GtkTextIter iterx;
if (app->optback) { /* if searching backwards */
gtk_text_buffer_get_iter_at_mark (buffer, &iterx,
app->selstart);
if (gtk_text_iter_compare (&mstart, &iterx) < 0 ) {
delete_mark_last_pos (app);
app->txtfound = FALSE;
/* handle search reached beginning of selection */
status_update_str (app, "Search reached beginning of selection...");
return; /* or btnclose_activate (NULL, app); */
}
}
else { /* for forward search in selection */
gtk_text_buffer_get_iter_at_mark (buffer, &iterx,
app->selend);
if (gtk_text_iter_compare (&iterx, &mend) < 0 ) {
delete_mark_last_pos (app);
app->txtfound = FALSE;
/* handle search reached end of selection */
status_update_str (app, "Search reached end of selection...");
return; /* or btnclose_activate (NULL, app); */
}
}
} /* TODO: restore original selection if no find in range */
/* create or move last_pos GtkTextMark */
if (app->last_pos == NULL)
app->last_pos = gtk_text_buffer_create_mark (buffer, "last_pos",
app->optback ? &mstart : &mend, FALSE);
else
gtk_text_buffer_move_mark (buffer, app->last_pos,
app->optback ? &mstart : &mend);
/* flags checking whether match is at word start/end */
gboolean word_start = gtk_text_iter_starts_word (&mstart),
word_end = gtk_text_iter_ends_word (&mend);
/* handle whole-word search option */
if (app->optwhole) {
if (!word_start || !word_end) { /* not word start/end */
/* place cursor at end to cancel select-highlight */
gtk_text_buffer_place_cursor (buffer, &mend);
continue; /* find next text match */
}
}
app->txtfound = TRUE; /* match found satisfying options */
/* select range, setting start, end iters, last_pos mark */
gtk_text_buffer_select_range (buffer, &mstart, &mend);
/* scroll window to mark to insure match is visible */
#ifndef TOMARK
gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (app->view),
app->last_pos, 0.0, TRUE, 0.95, 0.8);
#else
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (app->view),
app->last_pos);
#endif
break;
}
else { /* 'text' not found in buffer */
/* there was a previous find in text, and search was from cursor
* or confined to a selection
*/
if (app->txtfound) {
app->txtfound = FALSE;
/* TODO: closing dialog removes selection depending on
* how fast the user presses enter or clicks OK. find
* solution for timing issue (I've seen posts on this )
*/
if (app->optselect || app->optfrom) {
if (dlg_yes_no_msg (app, "Search reached end of range.\n\n"
"Continue search from beginning?",
"Search Term Not Found",
FALSE)) {
/* reset last position */
delete_mark_last_pos (app);
wrapped = TRUE;
goto wrapsrch;
}
else /* close the dialog */
btnclose_activate (NULL, app);
}
else { /* search from beginning or end */
/* reset last position */
delete_mark_last_pos (app);
status_update_str (app, "No further matches in search area...");
}
}
else { /* no match ever found */
status_update_str (app, "String not found in search area...");
// dlg_info ("No text matched within search.",
// "Search Term Not Found");
}
break;
wrapsrch:;
}
// gtk_text_buffer_end_user_action (buffer);
}
}
void btnfind_activate (GtkWidget *widget, kwinst *app)
{ /* TODO: call chk_existing_selection to set selstart and selend
* text marks to be used on restoring the selection bounds at the end
* of the function. This will facilitate further searches or replacements
* without having to set selection bounds manually. So the same for the
* replace dialog. Just a single function taking "app" as a parameter so
* it holds the selstart and selend GktTextMarks. Make sure the set
* selection bounds is the last possible call so that nothing else can
* occur in the interim to dismiss the selection.
*/
guint i;
/* get find & replace entries */
gchar *findtext = gtk_combo_box_text_get_active_text (
GTK_COMBO_BOX_TEXT(app->entryfind));
/* add new terms to find/replace lists (no dups) */
for (i = 0; i < app->nfentries; i++)
if (g_strcmp0 (app->findtext[i], findtext) == 0) goto fdup;
/* add to array of entries & increment indexes */
app->findtext[app->nfentries++] = findtext;
gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT(app->entryfind), findtext);
chk_realloc_ent (app); /* check/realloc find/rep text */