forked from TypesettingTools/Aegisub
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathedit.cpp
More file actions
1342 lines (1131 loc) · 42.9 KB
/
Copy pathedit.cpp
File metadata and controls
1342 lines (1131 loc) · 42.9 KB
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
// Copyright (c) 2005-2010, Niels Martin Hansen
// Copyright (c) 2005-2010, Rodrigo Braz Monteiro
// Copyright (c) 2010, Amar Takhar
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Aegisub Project http://www.aegisub.org/
#include "command.h"
#include "../ass_dialogue.h"
#include "../ass_file.h"
#include "../ass_karaoke.h"
#include "../ass_style.h"
#include "../compat.h"
#include "../dialog_search_replace.h"
#include "../dialogs.h"
#include "../format.h"
#include "../include/aegisub/context.h"
#include "../initial_line_state.h"
#include "../libresrc/libresrc.h"
#include "../options.h"
#include "../project.h"
#include "../selection_controller.h"
#include "../subs_controller.h"
#include "../subs_edit_ctrl.h"
#include "../text_selection_controller.h"
#include "../utils.h"
#include "../video_controller.h"
#include <libaegisub/address_of_adaptor.h>
#include <libaegisub/of_type_adaptor.h>
#include <libaegisub/make_unique.h>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/indirected.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/regex.hpp>
#include <boost/tokenizer.hpp>
#include <wx/clipbrd.h>
#include <wx/fontdlg.h>
#include <wx/textentry.h>
namespace {
using namespace boost::adaptors;
using cmd::Command;
struct validate_sel_nonempty : public Command {
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) override {
return c->selectionController->GetSelectedSet().size() > 0;
}
};
struct validate_video_and_sel_nonempty : public Command {
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) override {
return c->project->VideoProvider() && !c->selectionController->GetSelectedSet().empty();
}
};
struct validate_sel_multiple : public Command {
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) override {
return c->selectionController->GetSelectedSet().size() > 1;
}
};
template<typename String>
AssDialogue *get_dialogue(String data) {
boost::trim(data);
try {
// Try to interpret the line as an ASS line
return new AssDialogue(data);
}
catch (...) {
// Line didn't parse correctly, assume it's plain text that
// should be pasted in the Text field only
auto d = new AssDialogue;
d->End = 0;
d->Text = data;
return d;
}
}
template<typename Paster>
void paste_lines(agi::Context *c, bool paste_over, Paster&& paste_line) {
std::string data = GetClipboard();
if (data.empty()) return;
AssDialogue *first = nullptr;
Selection newsel;
boost::char_separator<char> sep("\r\n");
for (auto curdata : boost::tokenizer<boost::char_separator<char>>(data, sep)) {
AssDialogue *inserted = paste_line(get_dialogue(curdata));
if (!inserted)
break;
newsel.insert(inserted);
if (!first)
first = inserted;
}
if (first) {
c->ass->Commit(_("paste"), paste_over ? AssFile::COMMIT_DIAG_FULL : AssFile::COMMIT_DIAG_ADDREM);
if (!paste_over)
c->selectionController->SetSelectionAndActive(std::move(newsel), first);
}
}
AssDialogue *paste_over(wxWindow *parent, std::vector<bool>& pasteOverOptions, AssDialogue *new_line, AssDialogue *old_line) {
if (pasteOverOptions.empty()) {
if (!ShowPasteOverDialog(parent)) return nullptr;
pasteOverOptions = OPT_GET("Tool/Paste Lines Over/Fields")->GetListBool();
}
if (pasteOverOptions[0]) old_line->Comment = new_line->Comment;
if (pasteOverOptions[1]) old_line->Layer = new_line->Layer;
if (pasteOverOptions[2]) old_line->Start = new_line->Start;
if (pasteOverOptions[3]) old_line->End = new_line->End;
if (pasteOverOptions[4]) old_line->Style = new_line->Style;
if (pasteOverOptions[5]) old_line->Actor = new_line->Actor;
if (pasteOverOptions[6]) old_line->Margin[0] = new_line->Margin[0];
if (pasteOverOptions[7]) old_line->Margin[1] = new_line->Margin[1];
if (pasteOverOptions[8]) old_line->Margin[2] = new_line->Margin[2];
if (pasteOverOptions[9]) old_line->Effect = new_line->Effect;
if (pasteOverOptions[10]) old_line->Text = new_line->Text;
return old_line;
}
struct parsed_line {
AssDialogue *line;
std::vector<std::unique_ptr<AssDialogueBlock>> blocks;
parsed_line(AssDialogue *line) : line(line), blocks(line->ParseTags()) { }
parsed_line(parsed_line&& r) = default;
const AssOverrideTag *find_tag(int blockn, std::string const& tag_name, std::string const& alt) const {
for (auto ovr : blocks | sliced(0, blockn + 1) | reversed | agi::of_type<AssDialogueBlockOverride>()) {
for (auto const& tag : ovr->Tags | reversed) {
if (tag.Name == tag_name || tag.Name == alt)
return &tag;
}
}
return nullptr;
}
template<typename T>
T get_value(int blockn, T initial, std::string const& tag_name, std::string const& alt = "") const {
auto tag = find_tag(blockn, tag_name, alt);
if (tag)
return tag->Params[0].template Get<T>(initial);
return initial;
}
int block_at_pos(int pos) const {
auto const& text = line->Text.get();
int n = 0;
int max = text.size() - 1;
bool in_block = false;
for (int i = 0; i <= max; ++i) {
if (text[i] == '{') {
if (!in_block && i > 0 && pos >= 0)
++n;
in_block = true;
}
else if (text[i] == '}' && in_block) {
in_block = false;
if (pos > 0 && (i + 1 == max || text[i + 1] != '{'))
n++;
}
else if (!in_block) {
if (--pos == 0)
return n + (i < max && text[i + 1] == '{');
}
}
return n - in_block;
}
int set_tag(std::string const& tag, std::string const& value, int norm_pos, int orig_pos) {
int blockn = block_at_pos(norm_pos);
AssDialogueBlockPlain *plain = nullptr;
AssDialogueBlockOverride *ovr = nullptr;
while (blockn >= 0 && !plain && !ovr) {
AssDialogueBlock *block = blocks[blockn].get();
switch (block->GetType()) {
case AssBlockType::PLAIN:
plain = static_cast<AssDialogueBlockPlain *>(block);
break;
case AssBlockType::DRAWING:
--blockn;
break;
case AssBlockType::COMMENT:
--blockn;
orig_pos = line->Text.get().rfind('{', orig_pos);
break;
case AssBlockType::OVERRIDE:
ovr = static_cast<AssDialogueBlockOverride*>(block);
break;
}
}
// If we didn't hit a suitable block for inserting the override just put
// it at the beginning of the line
if (blockn < 0)
orig_pos = 0;
std::string insert(tag + value);
int shift = insert.size();
if (plain || blockn < 0) {
line->Text = line->Text.get().substr(0, orig_pos) + "{" + insert + "}" + line->Text.get().substr(orig_pos);
shift += 2;
blocks = line->ParseTags();
}
else if (ovr) {
std::string alt;
if (tag == "\\c") alt = "\\1c";
// Remove old of same
bool found = false;
for (size_t i = 0; i < ovr->Tags.size(); i++) {
std::string const& name = ovr->Tags[i].Name;
if (tag == name || alt == name) {
shift -= ((std::string)ovr->Tags[i]).size();
if (found) {
ovr->Tags.erase(ovr->Tags.begin() + i);
i--;
}
else {
ovr->Tags[i].Params[0].Set(value);
found = true;
}
}
}
if (!found)
ovr->AddTag(insert);
line->UpdateText(blocks);
}
else
assert(false);
return shift;
}
};
int normalize_pos(std::string const& text, int pos) {
int plain_len = 0;
bool in_block = false;
for (int i = 0, max = text.size() - 1; i < pos && i <= max; ++i) {
if (text[i] == '{')
in_block = true;
if (!in_block)
++plain_len;
if (text[i] == '}' && in_block)
in_block = false;
}
return plain_len;
}
template<typename Func>
void update_lines(const agi::Context *c, wxString const& undo_msg, Func&& f) {
const auto active_line = c->selectionController->GetActiveLine();
const int sel_start = c->textSelectionController->GetSelectionStart();
const int sel_end = c->textSelectionController->GetSelectionEnd();
const int norm_sel_start = normalize_pos(active_line->Text, sel_start);
const int norm_sel_end = normalize_pos(active_line->Text, sel_end);
int active_sel_shift = 0;
for (const auto line : c->selectionController->GetSelectedSet()) {
int shift = f(line, sel_start, sel_end, norm_sel_start, norm_sel_end);
if (line == active_line)
active_sel_shift = shift;
}
auto const& sel = c->selectionController->GetSelectedSet();
c->ass->Commit(undo_msg, AssFile::COMMIT_DIAG_TEXT, -1, sel.size() == 1 ? *sel.begin() : nullptr);
if (active_sel_shift != 0)
c->textSelectionController->SetSelection(sel_start + active_sel_shift, sel_end + active_sel_shift);
}
void toggle_override_tag(const agi::Context *c, bool (AssStyle::*field), const char *tag, wxString const& undo_msg) {
update_lines(c, undo_msg, [&](AssDialogue *line, int sel_start, int sel_end, int norm_sel_start, int norm_sel_end) {
AssStyle const* const style = c->ass->GetStyle(line->Style);
bool state = style ? style->*field : AssStyle().*field;
parsed_line parsed(line);
int blockn = parsed.block_at_pos(norm_sel_start);
state = parsed.get_value(blockn, state, tag);
int shift = parsed.set_tag(tag, state ? "0" : "1", norm_sel_start, sel_start);
if (sel_start != sel_end)
parsed.set_tag(tag, state ? "1" : "0", norm_sel_end, sel_end + shift);
return shift;
});
}
void show_color_picker(const agi::Context *c, agi::Color (AssStyle::*field), const char *tag, const char *alt, const char *alpha) {
agi::Color initial_color;
const auto active_line = c->selectionController->GetActiveLine();
const int sel_start = c->textSelectionController->GetSelectionStart();
const int sel_end = c->textSelectionController->GetSelectionStart();
const int norm_sel_start = normalize_pos(active_line->Text, sel_start);
auto const& sel = c->selectionController->GetSelectedSet();
using line_info = std::pair<agi::Color, parsed_line>;
std::vector<line_info> lines;
for (auto line : sel) {
AssStyle const* const style = c->ass->GetStyle(line->Style);
agi::Color color = (style ? style->*field : AssStyle().*field);
parsed_line parsed(line);
int blockn = parsed.block_at_pos(norm_sel_start);
int a = parsed.get_value(blockn, (int)color.a, alpha, "\\alpha");
color = parsed.get_value(blockn, color, tag, alt);
color.a = a;
if (line == active_line)
initial_color = color;
lines.emplace_back(color, std::move(parsed));
}
int active_shift = 0;
int commit_id = -1;
bool ok = GetColorFromUser(c->parent, initial_color, true, [&](agi::Color new_color) {
for (auto& line : lines) {
int shift = line.second.set_tag(tag, new_color.GetAssOverrideFormatted(), norm_sel_start, sel_start);
if (new_color.a != line.first.a) {
shift += line.second.set_tag(alpha, agi::format("&H%02X&", (int)new_color.a), norm_sel_start, sel_start + shift);
line.first.a = new_color.a;
}
if (line.second.line == active_line)
active_shift = shift;
}
commit_id = c->ass->Commit(_("set color"), AssFile::COMMIT_DIAG_TEXT, commit_id, sel.size() == 1 ? *sel.begin() : nullptr);
if (active_shift)
c->textSelectionController->SetSelection(sel_start + active_shift, sel_start + active_shift);
});
if (!ok && commit_id != -1) {
c->subsController->Undo();
c->textSelectionController->SetSelection(sel_start, sel_end);
}
}
struct edit_color_primary final : public Command {
CMD_NAME("edit/color/primary")
CMD_ICON(button_color_one)
STR_MENU("Primary Color...")
STR_DISP("Primary Color")
STR_HELP("Set the primary fill color (\\c) at the cursor position")
void operator()(agi::Context *c) override {
show_color_picker(c, &AssStyle::primary, "\\c", "\\1c", "\\1a");
}
};
struct edit_color_secondary final : public Command {
CMD_NAME("edit/color/secondary")
CMD_ICON(button_color_two)
STR_MENU("Secondary Color...")
STR_DISP("Secondary Color")
STR_HELP("Set the secondary (karaoke) fill color (\\2c) at the cursor position")
void operator()(agi::Context *c) override {
show_color_picker(c, &AssStyle::secondary, "\\2c", "", "\\2a");
}
};
struct edit_color_outline final : public Command {
CMD_NAME("edit/color/outline")
CMD_ICON(button_color_three)
STR_MENU("Outline Color...")
STR_DISP("Outline Color")
STR_HELP("Set the outline color (\\3c) at the cursor position")
void operator()(agi::Context *c) override {
show_color_picker(c, &AssStyle::outline, "\\3c", "", "\\3a");
}
};
struct edit_color_shadow final : public Command {
CMD_NAME("edit/color/shadow")
CMD_ICON(button_color_four)
STR_MENU("Shadow Color...")
STR_DISP("Shadow Color")
STR_HELP("Set the shadow color (\\4c) at the cursor position")
void operator()(agi::Context *c) override {
show_color_picker(c, &AssStyle::shadow, "\\4c", "", "\\4a");
}
};
struct edit_style_bold final : public Command {
CMD_NAME("edit/style/bold")
CMD_ICON(button_bold)
STR_MENU("Toggle Bold")
STR_DISP("Toggle Bold")
STR_HELP("Toggle bold (\\b) for the current selection or at the current cursor position")
void operator()(agi::Context *c) override {
toggle_override_tag(c, &AssStyle::bold, "\\b", _("toggle bold"));
}
};
struct edit_style_italic final : public Command {
CMD_NAME("edit/style/italic")
CMD_ICON(button_italics)
STR_MENU("Toggle Italics")
STR_DISP("Toggle Italics")
STR_HELP("Toggle italics (\\i) for the current selection or at the current cursor position")
void operator()(agi::Context *c) override {
toggle_override_tag(c, &AssStyle::italic, "\\i", _("toggle italic"));
}
};
struct edit_style_underline final : public Command {
CMD_NAME("edit/style/underline")
CMD_ICON(button_underline)
STR_MENU("Toggle Underline")
STR_DISP("Toggle Underline")
STR_HELP("Toggle underline (\\u) for the current selection or at the current cursor position")
void operator()(agi::Context *c) override {
toggle_override_tag(c, &AssStyle::underline, "\\u", _("toggle underline"));
}
};
struct edit_style_strikeout final : public Command {
CMD_NAME("edit/style/strikeout")
CMD_ICON(button_strikeout)
STR_MENU("Toggle Strikeout")
STR_DISP("Toggle Strikeout")
STR_HELP("Toggle strikeout (\\s) for the current selection or at the current cursor position")
void operator()(agi::Context *c) override {
toggle_override_tag(c, &AssStyle::strikeout, "\\s", _("toggle strikeout"));
}
};
struct edit_font final : public Command {
CMD_NAME("edit/font")
CMD_ICON(button_fontname)
STR_MENU("Font Face...")
STR_DISP("Font Face")
STR_HELP("Select a font face and size")
void operator()(agi::Context *c) override {
const parsed_line active(c->selectionController->GetActiveLine());
const int insertion_point = normalize_pos(active.line->Text, c->textSelectionController->GetInsertionPoint());
auto font_for_line = [&](parsed_line const& line) -> wxFont {
const int blockn = line.block_at_pos(insertion_point);
const AssStyle *style = c->ass->GetStyle(line.line->Style);
const AssStyle default_style;
if (!style)
style = &default_style;
return wxFont(
line.get_value(blockn, (int)style->fontsize, "\\fs"),
wxFONTFAMILY_DEFAULT,
line.get_value(blockn, style->italic, "\\i") ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL,
line.get_value(blockn, style->bold, "\\b") ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL,
line.get_value(blockn, style->underline, "\\u"),
to_wx(line.get_value(blockn, style->font, "\\fn")));
};
const wxFont initial = font_for_line(active);
const wxFont font = wxGetFontFromUser(c->parent, initial);
if (!font.Ok() || font == initial) return;
update_lines(c, _("set font"), [&](AssDialogue *line, int sel_start, int sel_end, int norm_sel_start, int norm_sel_end) {
parsed_line parsed(line);
const wxFont startfont = font_for_line(parsed);
int shift = 0;
auto do_set_tag = [&](const char *tag_name, std::string const& value) {
shift += parsed.set_tag(tag_name, value, norm_sel_start, sel_start + shift);
};
if (font.GetFaceName() != startfont.GetFaceName())
do_set_tag("\\fn", from_wx(font.GetFaceName()));
if (font.GetPointSize() != startfont.GetPointSize())
do_set_tag("\\fs", std::to_string(font.GetPointSize()));
if (font.GetWeight() != startfont.GetWeight())
do_set_tag("\\b", std::to_string(font.GetWeight() == wxFONTWEIGHT_BOLD));
if (font.GetStyle() != startfont.GetStyle())
do_set_tag("\\i", std::to_string(font.GetStyle() == wxFONTSTYLE_ITALIC));
if (font.GetUnderlined() != startfont.GetUnderlined())
do_set_tag("\\i", std::to_string(font.GetUnderlined()));
return shift;
});
}
};
struct edit_find_replace final : public Command {
CMD_NAME("edit/find_replace")
CMD_ICON(find_replace_menu)
STR_MENU("Find and R&eplace...")
STR_DISP("Find and Replace")
STR_HELP("Find and replace words in subtitles")
void operator()(agi::Context *c) override {
c->videoController->Stop();
DialogSearchReplace::Show(c, true);
}
};
static void copy_lines(agi::Context *c) {
SetClipboard(join(c->selectionController->GetSortedSelection()
| transformed(static_cast<std::string(*)(AssDialogue*)>([](AssDialogue *d) { return d->GetEntryData(); })),
"\r\n"));
}
static void delete_lines(agi::Context *c, wxString const& commit_message) {
auto const& sel = c->selectionController->GetSelectedSet();
// Find a line near the active line not being deleted to make the new active line
AssDialogue *pre_sel = nullptr;
AssDialogue *post_sel = nullptr;
bool hit_selection = false;
for (auto& diag : c->ass->Events) {
if (sel.count(&diag))
hit_selection = true;
else if (hit_selection && !post_sel) {
post_sel = &diag;
break;
}
else
pre_sel = &diag;
}
// Remove the selected lines, but defer the deletion until after we select
// different lines. We can't just change the selection first because we may
// need to create a new dialogue line for it, and we can't select dialogue
// lines until after they're committed.
std::vector<std::unique_ptr<AssDialogue>> to_delete;
c->ass->Events.remove_and_dispose_if([&sel](AssDialogue const& e) {
return sel.count(const_cast<AssDialogue *>(&e));
}, [&](AssDialogue *e) {
to_delete.emplace_back(e);
});
AssDialogue *new_active = post_sel;
if (!new_active)
new_active = pre_sel;
// If we didn't get a new active line then we just deleted all the dialogue
// lines, so make a new one
if (!new_active) {
new_active = new AssDialogue;
c->ass->Events.push_back(*new_active);
}
c->ass->Commit(commit_message, AssFile::COMMIT_DIAG_ADDREM);
c->selectionController->SetSelectionAndActive({ new_active }, new_active);
}
struct edit_line_copy final : public validate_sel_nonempty {
CMD_NAME("edit/line/copy")
CMD_ICON(copy_button)
STR_MENU("&Copy Lines")
STR_DISP("Copy Lines")
STR_HELP("Copy subtitles to the clipboard")
void operator()(agi::Context *c) override {
// Ideally we'd let the control's keydown handler run and only deal
// with the events not processed by it, but that doesn't seem to be
// possible with how wx implements key event handling - the native
// platform processing is evoked only if the wx event is unprocessed,
// and there's no way to do something if the native platform code leaves
// it unprocessed
if (wxTextEntryBase *ctrl = dynamic_cast<wxTextEntryBase*>(c->parent->FindFocus()))
ctrl->Copy();
else {
copy_lines(c);
}
}
};
struct edit_line_cut: public validate_sel_nonempty {
CMD_NAME("edit/line/cut")
CMD_ICON(cut_button)
STR_MENU("Cu&t Lines")
STR_DISP("Cut Lines")
STR_HELP("Cut subtitles")
void operator()(agi::Context *c) override {
if (wxTextEntryBase *ctrl = dynamic_cast<wxTextEntryBase*>(c->parent->FindFocus()))
ctrl->Cut();
else {
copy_lines(c);
delete_lines(c, _("cut lines"));
}
}
};
struct edit_line_delete final : public validate_sel_nonempty {
CMD_NAME("edit/line/delete")
CMD_ICON(delete_button)
STR_MENU("De&lete Lines")
STR_DISP("Delete Lines")
STR_HELP("Delete currently selected lines")
void operator()(agi::Context *c) override {
delete_lines(c, _("delete lines"));
}
};
static void duplicate_lines(agi::Context *c, int shift) {
auto const& sel = c->selectionController->GetSelectedSet();
auto in_selection = [&](AssDialogue const& d) { return sel.count(const_cast<AssDialogue *>(&d)); };
Selection new_sel;
AssDialogue *new_active = nullptr;
auto start = c->ass->Events.begin();
auto end = c->ass->Events.end();
while (start != end) {
// Find the first line in the selection
start = std::find_if(start, end, in_selection);
if (start == end) break;
// And the last line in this contiguous selection
auto insert_pos = std::find_if_not(start, end, in_selection);
auto last = std::prev(insert_pos);
// Duplicate each of the selected lines, inserting them in a block
// after the selected block
do {
auto old_diag = &*start;
auto new_diag = new AssDialogue(*old_diag);
c->ass->Events.insert(insert_pos, *new_diag);
new_sel.insert(new_diag);
if (!new_active)
new_active = new_diag;
if (shift) {
int cur_frame = c->videoController->GetFrameN();
int old_start = c->videoController->FrameAtTime(new_diag->Start, agi::vfr::START);
int old_end = c->videoController->FrameAtTime(new_diag->End, agi::vfr::END);
// If the current frame isn't within the range of the line then
// splitting doesn't make any sense, so instead just duplicate
// the line and set the new one to just this frame
if (cur_frame < old_start || cur_frame > old_end) {
new_diag->Start = c->videoController->TimeAtFrame(cur_frame, agi::vfr::START);
new_diag->End = c->videoController->TimeAtFrame(cur_frame, agi::vfr::END);
}
/// @todo This does dumb things when old_start == old_end
else if (shift < 0) {
old_diag->End = c->videoController->TimeAtFrame(cur_frame - 1, agi::vfr::END);
new_diag->Start = c->videoController->TimeAtFrame(cur_frame, agi::vfr::START);
}
else {
old_diag->End = c->videoController->TimeAtFrame(cur_frame, agi::vfr::END);
new_diag->Start = c->videoController->TimeAtFrame(cur_frame + 1, agi::vfr::START);
}
/// @todo also split \t and \move?
}
} while (start++ != last);
// Skip over the lines we just made
start = insert_pos;
}
if (new_sel.empty()) return;
c->ass->Commit(shift ? _("split") : _("duplicate lines"), AssFile::COMMIT_DIAG_ADDREM);
c->selectionController->SetSelectionAndActive(std::move(new_sel), new_active);
}
struct edit_line_duplicate final : public validate_sel_nonempty {
CMD_NAME("edit/line/duplicate")
STR_MENU("&Duplicate Lines")
STR_DISP("Duplicate Lines")
STR_HELP("Duplicate the selected lines")
void operator()(agi::Context *c) override {
duplicate_lines(c, 0);
}
};
struct edit_line_duplicate_shift final : public validate_video_and_sel_nonempty {
CMD_NAME("edit/line/split/after")
STR_MENU("Split lines after current frame")
STR_DISP("Split lines after current frame")
STR_HELP("Split the current line into a line which ends on the current frame and a line which starts on the next frame")
CMD_TYPE(COMMAND_VALIDATE)
void operator()(agi::Context *c) override {
duplicate_lines(c, 1);
}
};
struct edit_line_duplicate_shift_back final : public validate_video_and_sel_nonempty {
CMD_NAME("edit/line/split/before")
STR_MENU("Split lines before current frame")
STR_DISP("Split lines before current frame")
STR_HELP("Split the current line into a line which ends on the previous frame and a line which starts on the current frame")
CMD_TYPE(COMMAND_VALIDATE)
void operator()(agi::Context *c) override {
duplicate_lines(c, -1);
}
};
static void combine_lines(agi::Context *c, void (*combiner)(AssDialogue *, AssDialogue *), wxString const& message) {
auto sel = c->selectionController->GetSortedSelection();
AssDialogue *first = sel[0];
combiner(first, nullptr);
for (size_t i = 1; i < sel.size(); ++i) {
combiner(first, sel[i]);
first->End = std::max(first->End, sel[i]->End);
delete sel[i];
}
c->selectionController->SetSelectionAndActive({first}, first);
c->ass->Commit(message, AssFile::COMMIT_DIAG_ADDREM | AssFile::COMMIT_DIAG_FULL);
}
static void combine_karaoke(AssDialogue *first, AssDialogue *second) {
if (second)
first->Text = first->Text.get() + "{\\k" + std::to_string((second->End - second->Start) / 10) + "}" + second->Text.get();
else
first->Text = "{\\k" + std::to_string((first->End - first->Start) / 10) + "}" + first->Text.get();
}
static void combine_concat(AssDialogue *first, AssDialogue *second) {
if (second)
first->Text = first->Text.get() + " " + second->Text.get();
}
static void combine_drop(AssDialogue *, AssDialogue *) { }
struct edit_line_join_as_karaoke final : public validate_sel_multiple {
CMD_NAME("edit/line/join/as_karaoke")
STR_MENU("As &Karaoke")
STR_DISP("As Karaoke")
STR_HELP("Join selected lines in a single one, as karaoke")
void operator()(agi::Context *c) override {
combine_lines(c, combine_karaoke, _("join as karaoke"));
}
};
struct edit_line_join_concatenate final : public validate_sel_multiple {
CMD_NAME("edit/line/join/concatenate")
STR_MENU("&Concatenate")
STR_DISP("Concatenate")
STR_HELP("Join selected lines in a single one, concatenating text together")
void operator()(agi::Context *c) override {
combine_lines(c, combine_concat, _("join lines"));
}
};
struct edit_line_join_keep_first final : public validate_sel_multiple {
CMD_NAME("edit/line/join/keep_first")
STR_MENU("Keep &First")
STR_DISP("Keep First")
STR_HELP("Join selected lines in a single one, keeping text of first and discarding remaining")
void operator()(agi::Context *c) override {
combine_lines(c, combine_drop, _("join lines"));
}
};
static bool try_paste_lines(agi::Context *c) {
std::string data = GetClipboard();
boost::trim_left(data);
if (!boost::starts_with(data, "Dialogue:")) return false;
EntryList<AssDialogue> parsed;
boost::char_separator<char> sep("\r\n");
for (auto curdata : boost::tokenizer<boost::char_separator<char>>(data, sep)) {
boost::trim(curdata);
try {
parsed.push_back(*new AssDialogue(curdata));
}
catch (...) {
parsed.clear_and_dispose([](AssDialogue *e) { delete e; });
return false;
}
}
AssDialogue *new_active = &*parsed.begin();
Selection new_selection;
for (auto& line : parsed)
new_selection.insert(&line);
auto pos = c->ass->iterator_to(*c->selectionController->GetActiveLine());
c->ass->Events.splice(pos, parsed, parsed.begin(), parsed.end());
c->ass->Commit(_("paste"), AssFile::COMMIT_DIAG_ADDREM);
c->selectionController->SetSelectionAndActive(std::move(new_selection), new_active);
return true;
}
struct edit_line_paste final : public Command {
CMD_NAME("edit/line/paste")
CMD_ICON(paste_button)
STR_MENU("&Paste Lines")
STR_DISP("Paste Lines")
STR_HELP("Paste subtitles")
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *) override {
bool can_paste = false;
if (wxTheClipboard->Open()) {
can_paste = wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT);
wxTheClipboard->Close();
}
return can_paste;
}
void operator()(agi::Context *c) override {
if (wxTextEntryBase *ctrl = dynamic_cast<wxTextEntryBase*>(c->parent->FindFocus())) {
if (!try_paste_lines(c))
ctrl->Paste();
}
else {
auto pos = c->ass->iterator_to(*c->selectionController->GetActiveLine());
paste_lines(c, false, [=](AssDialogue *new_line) -> AssDialogue * {
c->ass->Events.insert(pos, *new_line);
return new_line;
});
}
}
};
struct edit_line_paste_over final : public Command {
CMD_NAME("edit/line/paste/over")
STR_MENU("Paste Lines &Over...")
STR_DISP("Paste Lines Over")
STR_HELP("Paste subtitles over others")
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) override {
bool can_paste = !c->selectionController->GetSelectedSet().empty();
if (can_paste && wxTheClipboard->Open()) {
can_paste = wxTheClipboard->IsSupported(wxDF_TEXT) || wxTheClipboard->IsSupported(wxDF_UNICODETEXT);
wxTheClipboard->Close();
}
return can_paste;
}
void operator()(agi::Context *c) override {
auto const& sel = c->selectionController->GetSelectedSet();
std::vector<bool> pasteOverOptions;
// Only one line selected, so paste over downwards from the active line
if (sel.size() < 2) {
auto pos = c->ass->iterator_to(*c->selectionController->GetActiveLine());
paste_lines(c, true, [&](AssDialogue *new_line) -> AssDialogue * {
std::unique_ptr<AssDialogue> deleter(new_line);
if (pos == c->ass->Events.end()) return nullptr;
AssDialogue *ret = paste_over(c->parent, pasteOverOptions, new_line, &*pos);
if (ret)
++pos;
return ret;
});
}
else {
// Multiple lines selected, so paste over the selection
auto sorted_selection = c->selectionController->GetSortedSelection();
auto pos = begin(sorted_selection);
paste_lines(c, true, [&](AssDialogue *new_line) -> AssDialogue * {
std::unique_ptr<AssDialogue> deleter(new_line);
if (pos == end(sorted_selection)) return nullptr;
AssDialogue *ret = paste_over(c->parent, pasteOverOptions, new_line, *pos);
if (ret) ++pos;
return ret;
});
}
}
};
namespace {
std::string trim_text(std::string text) {
boost::regex start(R"(^( | |\\[nNh])+)");
boost::regex end(R"(( | |\\[nNh])+$)");
text = regex_replace(text, start, "", boost::format_first_only);
text = regex_replace(text, end, "", boost::format_first_only);
return text;
}
void expand_times(AssDialogue *src, AssDialogue *dst) {
dst->Start = std::min(dst->Start, src->Start);
dst->End = std::max(dst->End, src->End);
}
bool check_start(AssDialogue *d1, AssDialogue *d2) {
if (boost::starts_with(d1->Text.get(), d2->Text.get())) {
d1->Text = trim_text(d1->Text.get().substr(d2->Text.get().size()));
expand_times(d1, d2);
return true;
}
return false;
}
bool check_end(AssDialogue *d1, AssDialogue *d2) {
if (boost::ends_with(d1->Text.get(), d2->Text.get())) {
d1->Text = trim_text(d1->Text.get().substr(0, d1->Text.get().size() - d2->Text.get().size()));
expand_times(d1, d2);
return true;
}
return false;
}
}
struct edit_line_recombine final : public validate_sel_multiple {
CMD_NAME("edit/line/recombine")
STR_MENU("Recom&bine Lines")
STR_DISP("Recombine Lines")
STR_HELP("Recombine subtitles which have been split and merged")
void operator()(agi::Context *c) override {
auto const& sel_set = c->selectionController->GetSelectedSet();
if (sel_set.size() < 2) return;
auto active_line = c->selectionController->GetActiveLine();
std::vector<AssDialogue*> sel(sel_set.begin(), sel_set.end());
boost::sort(sel, [](const AssDialogue *a, const AssDialogue *b) {
return a->Start < b->Start;
});
for (auto &diag : sel)
diag->Text = trim_text(diag->Text);
auto end = sel.end() - 1;
for (auto cur = sel.begin(); cur != end; ++cur) {
auto d1 = *cur;
auto d2 = cur + 1;
// 1, 1+2 (or 2+1), 2 gets turned into 1, 2, 2 so kill the duplicate
if (d1->Text == (*d2)->Text) {
expand_times(d1, *d2);
delete d1;
continue;
}
// 1, 1+2, 1 turns into 1, 2, [empty]
if (d1->Text.get().empty()) {
delete d1;