-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFoxitCoedit.ahk
1439 lines (1131 loc) · 37.8 KB
/
FoxitCoedit.ahk
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
AUTOEXEC_FoxitCoedit: ; Workaround for Autohotkey's ugly auto-exec feature. Must be first line.
; -- naming: Foxit PDF reader/editor from two machines, co-operatively edit the same PDF file
/* APIs:
FoxitCoedit_LaunchUI()
Example:
fxhk_DefineComboHotkey("AppsKey", "s", "FoxitCoedit_LaunchUI")
*/
;;;;;;;; Foco global vars ;;;;;;;;;;
global g_foco ; The single object responsible for the Foco GUI
global gu_focoLblHeadline
global g_HwndFOCOGui
;global gu_focoBtnTest
global gu_focoMleInfo
global gu_focoLblActivate
global gu_focoCkbLside
global gu_focoCkbRside
global gu_focoLblPeerFollow
global gu_focoDdlPeerFollowMe
global gu_focoBtnSavePdf
global gu_focoBtnResync
global gu_focoBottomStatus
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
return ; End of auto-execute section.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#Include %A_LineFile%\..\pdfreader.ahk
#Include %A_LineFile%\..\libs\class.PeersCoedit.ahk
class FoxitCoedit
{
static Id := "FoxitCoedit"
; static vars as constant >>>
static BtnTextResync := "R&e-sync"
static is_simulate_reopen_pdf_fail := false ; debugging purpose
static PdfBackups := 5
; static vars as constant <<<
isGuiVisible := false
state := "" ; "Detecting" , "EditorDetected", "CoeditActivated", "CoeditHandshaked", "Freezing"
; --[2024-06-05] Freezing unused yet.
tsSyncSuccss := "" ; A_Now
coedit := "" ; the PeersCoedit class instance
timer := ""
pedHwnd := "" ; ped: pdf editor
pedWinTitle := ""
pedExepath := ""
prev_mletext := ""
txtHeadline := ""
txtBottomline := ""
ischk_Lside := 0 ; 0 or 1, reflecting Coedit-sideA-Activate
ischk_Rside := 0 ; 0 or 1, reflecting Coedit-sideB-Activate
was_doc_modified := 0 ; pdf modified and unsaved state(denoted by asterisk symbol)
prev_peerHwnd := 0
is_showing_syncerr_msgbox := false ; just for optimized Error popup
mine_prev_PdfPageNum := ""
mine_pagenum_seq := 0
;
peer_prev_pagenum_seq := 0
peer_prev_pagenum := ""
;
msec_passive_followed := 0 ;msec_freeze_following_before := 0
; peer-follow-me user preference
static PEERFM_ALWAYS := 1
static PEERFM_AFTERSAVEPDF := 2
static PEERFM_NO := 3
peerfm_selection := FoxitCoedit.PEERFM_ALWAYS
is_closing_pdf := false
was_activepdf := -1 ; -1 as an invalid value
dbg(msg, lv) {
AmDbg_output(FoxitCoedit.Id, msg, lv)
}
dbg0(msg) {
FoxitCoedit.dbg(msg, 0)
}
dbg1(msg) {
FoxitCoedit.dbg(msg, 1)
}
dbg2(msg) {
FoxitCoedit.dbg(msg, 2)
}
__New()
{
this.coedit := new PeersCoedit()
if(FoxitCoeditCfg.PdfBackups>0)
{
; User can define FoxitCoeditCfg.PdfBackups in custom_env.ahk .
FoxitCoedit.PdfBackups := FoxitCoeditCfg.PdfBackups
}
}
pdfpath[]
{
get {
return this.coedit.docpath
}
}
peerHwnd[]
{
get {
return this.state=="CoeditHandshaked" ? this.coedit.peerdict.HWND : ""
}
}
peerDocModified[]
{
get {
return this.state=="CoeditHandshaked" ? this.coedit.peerdict.is_modified : ""
}
}
CreateGui()
{
GuiName := FoxitCoedit.Id
Gui_New(GuiName)
Gui_ChangeOpt(GuiName, "+Resize +MinSize")
Gui_AssociateHwndVarname(GuiName, "g_HwndFOCOGui")
fullwidth := 400
Gui_Add_TxtLabel(GuiName, "gu_focoLblHeadline", fullwidth, "", "...")
Gui_Add_Editbox( GuiName, "gu_focoMleInfo", fullwidth, "xm r10 readonly -0x1000" , "...")
; -- 0x1000 ES_WANTRETURN: we don't want this style bcz we want Enter to trigger default btn, even if Editbox has focus.
Gui_Add_TxtLabel(GuiName, "gu_focoLblActivate", 0, "xm", "Activate Coedit for above pdf")
Gui_Add_Checkbox(GuiName, "gu_focoCkbLside", 0, "x+10 yp " gui_g("Foco_CkbActivateCoedit"), "as &Left-side")
Gui_Add_Checkbox(GuiName, "gu_focoCkbRside", 0, "x+10 yp " gui_g("Foco_CkbActivateCoedit"), "as &Right-side")
Gui_Add_TxtLabel(GuiName, "gu_focoLblPeerFollow", 0, "xm y+30", "Peer PDF page &follows me:")
Gui_Add_DropDownList(GuiName, "gu_focoDdlPeerFollowMe", 137, "x+5 yp-2 AltSubmit " gui_g("Foco_OnDdlPeerFollowMe")
, "Always||Only after saving PDF|No")
save_btn_width := 98
sync_btn_width := 98
Gui_Add_Button( GuiName, "gu_focoBtnSavePdf", save_btn_width, "xm y+6 default " gui_g("Foco_OnBtnSavePdf"), "&Save pdf now")
xgap := fullwidth - save_btn_width - sync_btn_width
Gui_Add_Button( GuiName, "gu_focoBtnResync", sync_btn_width, Format("x+{} ", xgap) gui_g("Foco_OnBtnResync"), FoxitCoedit.BtnTextResync)
Gui_Add_Editbox( GuiName, "gu_focoBottomStatus", fullwidth, "xm readonly -E0x200", "...") ; -E0x200: turn off WS_EX_CLIENTEDG
GuiControl_SetColor(GuiName, "gu_focoBottomStatus", "8464B4") ; set grey text
;
; init base facility
;
this.state := "Detecting"
this.RefreshUic()
; The timer must be run all the time, bcz, even if the GUI is hidden,
; it should be able to detect situations like [handshake lost], [conflicting modifying doc] etc.
this.timer := Func("FoxitCoedit.RootTimerCallback").Bind(this) ; a BoundFunc object
dev_StartTimerPeriodic(this.timer, 1000, true)
}
ShowGui()
{
GuiName := FoxitCoedit.Id
if(this.isGuiVisible)
{
dev_WinActivateHwnd(g_HwndFOCOGui) ; bring it to front
return ; already shown
}
if(!g_HwndFOCOGui) {
this.CreateGui()
}
Gui_Show(GuiName, "AutoSize", FoxitCoedit.Id)
Editbox_ClearSelection(GuiName, "gu_focoMleInfo")
; -- to avoid seeing all text in Mle defaultly selected on first sight, effective only after Gui_Show()
this.isGuiVisible := true
}
HideGui()
{
Gui_Hide(FoxitCoedit.Id)
; dev_StopTimer(this.timer) ; should NOT stop the timer
this.isGuiVisible := false
}
RefreshUic()
{
GuiName := FoxitCoedit.Id
; assume all false(disabled)
focoLblActive := focoCkbLside := focoCkbRside := false
focoBtnSavePdf := focoDdlPeerFollowMe := focoBtnSync := false
if(this.state=="Detecting")
{
this.txtHeadline := "Detecting Foxit Reader/Editor..."
this.txtBottomline := "..."
}
else if(this.state=="EditorDetected")
{
this.txtHeadline := "Detected Foxit Reader/Editor:"
this.txtBottomline := "Hint: Click ""as Left-side"" or ""as Right-side"" to coedit that PDF."
focoLblActive := focoCkbLside := focoCkbRside := true
}
else if(this.state=="CoeditActivated" or this.state=="CoeditHandshaked")
{
focoLblActive := true
dev_assert(not (this.ischk_Lside and this.ischk_Rside))
if(this.ischk_Lside)
focoCkbLside := true
if(this.ischk_Rside)
focoCkbRside := true
if(this.coedit.state!="Syncing")
{
focoBtnSavePdf := focoDdlPeerFollowMe := focoBtnSync := true
}
}
else
{
dev_assert(this.state=="Freezing")
; -- all UIC should be disabled.
}
GuiControl_Enable(GuiName, "gu_focoLblActivate", focoLblActive)
GuiControl_Enable(GuiName, "gu_focoCkbLside", focoCkbLside)
GuiControl_Enable(GuiName, "gu_focoCkbRside", focoCkbRside)
GuiControl_Enable(GuiName, "gu_focoDdlPeerFollowMe", focoDdlPeerFollowMe)
GuiControl_Enable(GuiName, "gu_focoBtnSavePdf", focoBtnSavePdf)
GuiControl_Enable(GuiName, "gu_focoBtnResync", focoBtnSync)
GuiControl_SetText(GuiName, "gu_focoLblHeadline", this.txtHeadline)
GuiControl_SetText(GuiName, "gu_focoBottomStatus", this.txtBottomline)
this.RefreshMleDetail()
}
FormatHwnd(hwnd) ; static
{
if(hwnd=="" or hwnd==0)
val := ""
else if(hwnd=="*")
val := "*"
else
val := Format("0x{:0X}", hwnd)
return val
}
RefreshMleDetail()
{
GuiName := FoxitCoedit.Id
detail := ""
if(this.state=="Detecting")
{
detail := "No Foxit Reader/Editor is running yet.`n`n"
. "Just run Foxit and open a PDF file, this program will detect it automatically."
}
else
{
detail .= Format("HWND:`n{}", FoxitCoedit.FormatHwnd(this.pedHwnd))
if(this.peerHwnd)
{
detail .= Format(" (peer: {})", FoxitCoedit.FormatHwnd(this.peerHwnd))
}
detail .= "`n`n"
detail .= Format("TITLE:`n{}`n`n", this.pedWinTitle)
if(this.pdfpath)
{
detail .= "FILEPATH:`n" this.pdfpath
detail .= "`n`n"
}
}
detail .= "PdfBackups: " FoxitCoedit.PdfBackups
; detail .= "`n`n"
if(this.prev_mletext != detail)
{
; This `if` to avoid clearing out user mouse text selection.
this.prev_mletext := detail
GuiControl_SetText(GuiName, "gu_focoMleInfo", detail)
}
}
IsPdfModified()
{
wndtitle := dev_WinGetTitle_byHwnd(this.pedHwnd)
; Note: the Foxit's wndtitle text MAY diverge from the PDF filename,
; -- this can happen if the PDF has explicit Title property.
; For example, "[LLEBPF2023] OReilly - Learning eBPF by Liz Rice.pdf"
; has Foxit wndtitle text "Learning EBPF - Foxit Reader".
; When the PDF is modified, the wndtitle becomes:
; "Learning EBPF * - Foxit Reader"
;
; So we need to compare this.pedWinTitle instead of this.pdfpath .
is_ast := dev_IsSubStr(wndtitle, "*")
if(is_ast)
{
wndtitle_noast := FoxitCoedit.StripAsterisk(wndtitle)
if(wndtitle_noast==this.pedWinTitle)
return true
}
return false
}
IsPdfModified_msgbox()
{
if(this.IsPdfModified())
return true
dev_MsgBoxInfo("The PDF file looks unmodified, not action needed.")
return false
}
ActivateCoedit(which_side, pdfpath)
{
fndoc := { "syncsucc" : Func("FoxitCoedit.fndocSyncSucc").Bind(this)
, "savedoc" : Func("FoxitCoedit.fndocSavePdf").Bind(this)
, "closedoc" : Func("FoxitCoedit.fndocClosePdf").Bind(this)
, "opendoc" : Func("FoxitCoedit.fndocOpenPdf").Bind(this)
, "notify_ssstate" : Func("FoxitCoedit.fndocNotifySSState").Bind(this) }
this.prev_peerHwnd := 0
this.coedit.Activate(which_side, pdfpath, fndoc) ; this does not block
this.state := "CoeditActivated"
this.StoreMinesideIni("", "")
; Override Ctrl+S for Foxit Reader/Editor
fxhk_DefineHotkeyCondComment("^s", "FoxitCoedit_SaveDoc_hotkey", "assigned by FoxitCoedit"
, false , "foxit_IsWinActive", "FoxitCoedit_CtrlS")
}
DeactivateCoedit()
{
dev_assert(this.coedit)
this.coedit.IniWriteMine("HWND", "") ; must before .Deactivate()
this.coedit.Deactivate()
this.state := "Detecting"
fxhk_UnDefineHotkey("^s", "FoxitCoedit_SaveDoc_hotkey")
}
StoreMinesideIni(is_doc_modified, myHwnd)
{
; The parameters determine what values we'd like the peer to see.
this.coedit.IniWriteMine("is_modified", is_doc_modified)
this.coedit.IniWriteMine("HWND"
, (myHwnd=="" or myHwnd==0) ? "" : Format("0x{:08X}", myHwnd))
}
CtrlS()
{
if(this.IsTargetPdfActive())
{
if(not this.IsPdfModified_msgbox())
return
; PDF has modification asterisk:
this.ShowGui()
}
else
{
Send ^s
return
}
}
OnBtnSavePdf()
{
GuiName := FoxitCoedit.Id
Gui_ChangeOpt(GuiName, "+OwnDialogs")
if(not this.IsPdfModified_msgbox())
{
return
}
is_ctrldown := dev_IsCtrlKeyDown()
if(is_ctrldown)
this.HideGui()
oldstate := this.state
this.state := "Freezing"
GuiControl_Enable(GuiName, "gu_focoBtnSavePdf", false)
GuiControl_SetText(GuiName, "gu_focoBtnSavePdf", "Saving...")
GuiControl_SetText(GuiName, "gu_focoBtnResync", "Cancel Saving")
;
is_succ := this.coedit.LaunchSaveDocSession(ret_is_conn_lost, ret_errmsg)
; -- this will block for some time, but can be canceled by calling this.coedit.>CancelSavingSession() .
;
GuiControl_SetText(GuiName, "gu_focoBtnSavePdf", "&Save pdf now")
GuiControl_Enable(GuiName, "gu_focoBtnSavePdf", true)
GuiControl_SetText(GuiName, "gu_focoBtnResync", FoxitCoedit.BtnTextResync)
GuiControl_Enable(GuiName, "gu_focoBtnResync") ; may have been disabled in OnBtnResync()
this.state := oldstate
if(is_succ)
{
; Make a backup of the pdf file
pb := new PiledBackup(this.coedit.docpath
, this.coedit.docpath (this.ischk_Lside ? ".backupA" : ".backupB")
, FoxitCoedit.PdfBackups)
this.dbg1("Making backup to folder: " pb.dirbackup)
pb.SaveOneBackup()
}
else
{
if(is_ctrldown)
{
; Since fail, we should represent the UI for user to take further action.
this.ShowGui()
}
if(ret_is_conn_lost)
{
this.DisplaySyncLostPopup(ret_errmsg)
this.ResyncCoedit()
}
else
{
dev_assert(0, "FoxitCoedit bug: Re-entrant calling of OnBtnSavePdf !")
}
}
this.RefreshUic()
}
RootTimerCallback()
{
GuiName := FoxitCoedit.Id
if(this.state=="Detecting" or this.state=="EditorDetected")
{
this.InitDetectFoxitPresent()
}
else if(this.state=="CoeditActivated")
{
GuiControl_SetText(GuiName, "gu_focoLblHeadline", "[ Activated ] Waiting for peer...")
GuiControl_SetText(GuiName, "gu_focoBottomStatus", "Waiting for peer to connect...")
}
else if(this.state=="CoeditHandshaked")
{
this.RefreshMineFoxitHwnd()
this.RefreshUic()
GuiControl_SetText(GuiName, "gu_focoLblHeadline", this.txtHeadline)
GuiControl_SetText(GuiName, "gu_focoBottomStatus", this.txtBottomline)
;
; To detect HWND lost
;
if(this.peerHwnd)
this.prev_peerHwnd := this.peerHwnd
if(this.prev_peerHwnd and this.peerHwnd=="")
{
; If we once saw prev_peerHwnd valid, but now it becomes null, then the peer is lost.
; HWND=* is not considered lost.
if(not this.is_showing_syncerr_msgbox)
{
; If OnBtnSavePdf() has been showing similar popup, we will not do that repeatedly.
this.ModalMsgBox_ShowWarning("Peer HWND lost. Handshake lost! Click OK to re-sync.", FoxitCoedit.Id)
this.ResyncCoedit()
}
return
}
if(not this.IsTargetPdfActive())
{
; Foxit user is now viewing another pdf, so nothing more to do.
return
}
;
; Check editing conflict
;
is_modified := this.RecheckPdfModifiedState()
if(is_modified and this.peerDocModified)
{
temp_timer := Func("FoxitCoedit.RecheckPdfModifiedState").Bind(this) ; a BoundFunc object
dev_StartTimerPeriodic(temp_timer, 2000, true)
; -- this temp_timer is required, bcz, when ModalMsgBox_ShowWarning() blocks current AHK-thread,
; and RootTimerCallback() no longer executes every second. So an extra timer is required
; to check for this.IsPdfModified(), so that we can reset [INI] to is_modified=0 during the pop-up.
this.ModalMsgBox_ShowWarning("Both sides pdf are being modified, you are doing conflicting editing!`n`n"
. "This warning keeps pop-up until you discard one-side's modification.`n`n"
. "Suggestion: If you decide to discard this-side's modification, go to Foxit, close the pdf without saving it, then reopen that pdf.`n`n"
, FoxitCoedit.Id)
dev_StopTimer(temp_timer)
return
}
;
; Notify my PDF page number to peer, and/or, follow peer's PDF page number.
;
Critical On
if(not this.is_closing_pdf)
{
if(this.peerfm_selection==FoxitCoedit.PEERFM_ALWAYS)
{
this.record_pagenum_for_peer()
}
this.FollowPeerzPageNum()
}
Critical Off
}
}
IsTargetPdfActive()
{
; Foxit EXE can open multiple PDFs as individual tabs.
; The target pdf we are following may be switched to background so that it
; becomes "not active". In such case, the Foxit window-title will not reflect
; the target pdf's filename.
if(not this.pedHwnd)
return false
dev_assert(this.pedWinTitle)
nowtitle := dev_WinGetTitle_byHwnd(this.pedHwnd)
if(FoxitCoedit.IsTitleStemMatch(nowtitle, this.pedWinTitle))
return true
else
return false
}
IsTitleStemMatch(wintitle1, wintitle2) ; static
{
stem1 := FoxitCoedit.TitleStemFromWinTitle(wintitle1)
stem2 := FoxitCoedit.TitleStemFromWinTitle(wintitle2)
return stem1==stem2 ? true : false
}
FindFoxitHwnd() ; static
{
hwnd := dev_WinGet_Hwnd("ahk_class classFoxitReader")
if(hwnd) {
}
else {
hwnd := dev_WinGet_Hwnd("ahk_class classFoxitPhantom")
}
return hwnd ; may return null
}
InitDetectFoxitPresent() ; only before Activate.
{
GuiName := FoxitCoedit.Id
this.pedHwnd := FoxitCoedit.FindFoxitHwnd()
if(this.pedHwnd)
{
this.state := "EditorDetected"
wintitle := dev_WinGetTitle_byHwnd(this.pedHwnd)
this.pedWinTitle := FoxitCoedit.StripAsterisk(wintitle)
this.pedExepath := dev_GetExeFilepath("ahk_id " this.pedHwnd)
this.RefreshMleDetail()
}
else
{
this.state := "Detecting"
}
this.RefreshUic()
}
StripAsterisk(wndtitle) ; static
{
return StrReplace(wndtitle, " *", "")
}
TitleStemFromWinTitle(wintitle) ; static
{
; Wintitle example:
; "The Unix Manual.pdf - Foxit Reader"
; "Learning EBPF - Foxit PDF Editor"
; "Learning EBPF * - Foxit PDF Editor"
;
; so we strip off "- Foxit" suffix.
foundpos := InStr(wintitle, "- Foxit")
if(foundpos>0)
stem := SubStr(wintitle, 1, foundpos-1)
else
stem := wintitle
; Strip of trailing modification asterisk
return FoxitCoedit.StripAsterisk(stem)
}
get_ckbstate(which_ctlid)
{
dev_assert(which_ctlid=="gu_focoCkbLside" or which_ctlid=="gu_focoCkbRside")
return which_ctlid=="gu_focoCkbLside" ? this.ischk_Lside : this.ischk_Rside
}
set_ckbstate(which_ctlid, state)
{
dev_assert(which_ctlid=="gu_focoCkbLside" or which_ctlid=="gu_focoCkbRside")
if(which_ctlid=="gu_focoCkbLside")
this.ischk_Lside := state
else
this.ischk_Rside := state
Checkbox_SetCheckState(FoxitCoedit.Id, which_ctlid, state)
}
CkbActivateCoedit()
{
; We'll distinguish left-side or right-side according to A_GuiControl
GuiName := FoxitCoedit.Id
Gui_ChangeOpt(GuiName, "+OwnDialogs")
ctlid_ckb := A_GuiControl
isLeftside := (ctlid_ckb=="gu_focoCkbLside") ? true : false
ischecked := this.get_ckbstate(ctlid_ckb)
this.set_ckbstate(ctlid_ckb, ischecked)
; -- Do it bcz we want it to be a BS_CHECKBOX instead of a BS_AUTOCHECKBOX.
; We must set checkbox's UI state according to our own class member.
if(not ischecked)
{
; Was not checked, and user is now checking/ticking it.
; Ask user the real location of the PDF file, bcz AHK code here has no way to know it automatically.
pdfnam := this.TitleStemFromWinTitle(this.pedWinTitle)
pdfnam_safe := dev_ReplaceBadChars(pdfnam, "/<:>", "_")
; -- If pdfnam is sth like "Caution: Danger", that will cause dev_OpenSelectFileDialog() error.
pdfpath_real := dev_OpenSelectFileDialog(pdfnam_safe
, "Please tell me the actual filepath of the PDF file on the disk"
, "PDF files (*.pdf)")
if(not pdfpath_real)
return ; user cancels, do nothing
if(not FileExist(pdfpath_real))
{
dev_MsgBoxWarning("The filepath you picked does not exist yet:`n`n" pdfpath_real
, FoxitCoedit.Id)
return
}
this.dbg1("Now activate FoxitCoedit for: " pdfpath_real)
this.ActivateCoedit(isLeftside ? "sideA" : "sideB", pdfpath_real)
this.LoadStaticCfg()
this.set_ckbstate(ctlid_ckb, true)
this.RefreshMleDetail()
}
else
{
this.DeactivateCoedit()
this.set_ckbstate(ctlid_ckb, false)
}
this.RefreshUic()
}
OnBtnResync()
{
GuiName := FoxitCoedit.Id
if(this.coedit.state=="ProSaving" or this.coedit.state=="PasReload")
{
this.coedit.CancelSavingSession()
GuiControl_Disable(GuiName, "gu_focoBtnResync")
}
else
{
this.ResyncCoedit()
}
}
ResyncCoedit()
{
this.state := "CoeditActivated"
this.prev_peerHwnd := 0
this.StoreMinesideIni("0", "")
this.coedit.ResetSyncState()
this.RefreshUic()
}
fndocSyncSucc()
{
this.state := "CoeditHandshaked"
this.tsSyncSuccss := A_Now
this.txtHeadline := "[ Activated ] Handshaked"
this.txtBottomline := Format("[{}] Start monitoring PDF.", dev_GetDateTimeStr("_", this.tsSyncSuccss))
this.mine_prev_PdfPageNum := ""
this.mine_pagenum_seq := 0
this.is_closing_pdf := false
this.was_doc_modified := this.IsPdfModified()
this.StoreMinesideIni(this.was_doc_modified, this.pedHwnd)
this.RefreshUic()
}
fndocSavePdf()
{
GuiName := FoxitCoedit.Id
this.dbg1("FoxitCoedit.fndocSavePdf() executing...")
if(this.IsPdfModified())
{
; And wait until wintitle's "*" disappears.
msec_start := dev_GetTickCount64()
Loop
{
this.Try_SaveCurrentPdf()
dev_Sleep(500)
if(not this.IsPdfModified())
{
this.dbg1("FoxitCoedit.fndocSavePdf() success , PDF saved.")
if(this.peerfm_selection==FoxitCoedit.PEERFM_ALWAYS
or this.peerfm_selection==FoxitCoedit.PEERFM_AFTERSAVEPDF)
{
if(not this.is_closing_pdf)
{
this.record_pagenum_for_peer(true)
}
}
return true
}
msec_used := dev_GetTickCount64() - msec_start
info := Format("Mineside PDF saving (+{} seconds)...", msec_used//1000)
GuiControl_SetText(GuiName, "gu_focoBottomStatus", info)
; todo? (ProSaving side) detect cancel flag, then break.
}
dev_assert(0) ; should not get here.
}
this.dbg1("FoxitCoedit.fndocSavePdf() success , no modify.")
}
fndocClosePdf()
{
Critical On
this.is_closing_pdf := true
this.was_activepdf := this.IsTargetPdfActive()
Critical Off
GuiName := FoxitCoedit.Id
hwnd := this.pedHwnd
this.dbg1(Format("FoxitCoedit.fndocClosePdf() executing..."))
msec_start := dev_GetTickCount64()
wintitle := "ahk_id " hwnd
close_ok := false
dev_WinClose(wintitle)
Loop
{
info := Format("[{}] Closing mineside PDF, +{} seconds"
, dev_GetDateTimeStr(), (dev_GetTickCount64()-msec_start)//1000)
this.txtBottomline := info
GuiControl_SetText(GuiName, "gu_focoBottomStatus", info)
close_ok := dev_WinWaitClose(wintitle, 1000) ; this delays 1 second
if(close_ok)
{
this.dbg1("FoxitCoedit.fndocClosePdf() success.")
return true
}
if(this.coedit.cancel_flag)
{
this.dbg1("FoxitCoedit.fndocClosePdf() canceled by user.")
throw Exception("You canceled mineside PDF saving.")
}
}
}
fndocOpenPdf()
{
GuiName := FoxitCoedit.Id
this.dbg1("FoxitCoedit.fndocOpenPdf() executing... ")
if(FoxitCoedit.is_simulate_reopen_pdf_fail)
{
info := "FoxitCoedit.is_simulate_reopen_pdf_fail==true, so re-open pdf fails!"
this.dbg1(info)
throw Exception(info)
}
is_succ := false
exepath := this.pedExepath
; First, ensure that no process of exepath is running.
if(WinExist("ahk_exe " exepath))
{
throw Exception(Format("Unexpected! fndocOpenPdf() sees ""{}"" still running.", exepath))
}
Run % exepath
; Second, check that the new process really runs.
Loop, 100
{
dev_Sleep(500)
if(this.coedit.cancel_flag)
{
this.dbg1("FoxitCoedit.fndocOpenPdf() canceled by user.")
throw Exception("You canceled mineside PDF Re-opening.")
}
query_title := "ahk_exe " exepath
newhwnd := dev_GetHwndByWintitle(query_title)
if(not newhwnd)
continue
newtitle := dev_WinGetTitle_byHwnd(newhwnd)
newpdf_title := FoxitCoedit.TitleStemFromWinTitle(newtitle)
oldpdf_title := FoxitCoedit.TitleStemFromWinTitle(this.pedWinTitle)
; note: When the Foxit Editor 11 launching involves some bigs PDFs, the first-seen
; newtitle may be the small progress-bar's title, and we need to ignore it.
if( newpdf_title==oldpdf_title )
{
this.dbg2("[Dbginfo] See revived Foxit new process's title: " newpdf_title)
this.dbg1("FoxitCoedit.fndocOpenPdf() success.")
; Third, grab new-process's HWND
this.pedHwnd := dev_WinGet_Hwnd("ahk_exe " exepath)
this.dbg1(Format("Foxit HWND updated to be: {}", this.pedHwnd))
this.coedit.IniWriteMine("HWND", Format("0x{:X}", this.pedHwnd))
this.RefreshMleDetail() ; bcz the HWND has changed
; Make a diagnose
dbgHwnd := FoxitCoedit.FindFoxitHwnd()
if(dbgHwnd!=this.pedHwnd)
{
this.dbg1(Format("Strange! Hwnd by ahk_exe({}) != Hwnd by wndclass({})", this.pedHwnd, dbgHwnd))
}
is_succ := true
break
}
else
{
this.dbg2("[Dbginfo] Skip Foxit new process's title: " newtitle)
; On an Foxit Editor 11 machine opening two document-tabs, and we are editing
; 2nd tab document, I once see this:
;
; 2*[20240419_15:50:29.477] (+1.996s) Waiting peer's writing doc, success.
; 2*[20240419_15:50:29.477] (+0.000s) Now re-opening doc...
; 1*[20240419_15:50:29.477] (+0.000s) FoxitCoedit.fndocOpenPdf() executing...
; 2*[20240419_15:50:29.977] (+0.500s) [Dbginfo] Skip Foxit new process's title: Progress
; 2*[20240419_15:50:30.476] (+0.499s) [Dbginfo] Skip Foxit new process's title: Progress [53%]
; 2*[20240419_15:50:30.975] (+0.499s) [Dbginfo] Skip Foxit new process's title: Progress [61%]
; 2*[20240419_15:50:31.474] (+0.499s) [Dbginfo] Skip Foxit new process's title: Progress [71%]
; 2*[20240419_15:50:31.973] (+0.499s) [Dbginfo] Skip Foxit new process's title: Progress [80%]
; 2*[20240419_15:50:32.473] (+0.500s) [Dbginfo] Skip Foxit new process's title: Progress [88%]
; 2*[20240419_15:50:32.972] (+0.499s) [Dbginfo] Skip Foxit new process's title: Progress [96%]
; 2*[20240419_15:50:33.471] (+0.499s) [Dbginfo] Skip Foxit new process's title: [TLPI] The Linux (...this is first tab doc...) - Foxit PDF Editor
; 1*[20240419_15:50:33.970] (+0.499s) FoxitCoedit.fndocOpenPdf() success.
; 1*[20240419_15:50:33.970] (+0.000s) Foxit HWND updated to be: 0xce09d2
; 2*[20240419_15:50:33.970] (+0.000s) Done re-opening doc...
; 1*[20240419_15:50:33.970] (+0.000s) Mineside just refreshed the doc. (passeq=2)
; [2024-05-29]
if(not this.was_activepdf)
{
; There is no hope that Foxit UI title can match our target pdf.
; Just assume success.
is_succ := true
break
}
}
}
this.is_closing_pdf := false
this.was_activepdf := -1
if(is_succ)
{
this.FollowPeerzPageNum()
return true
}
else
{
throw Exception(Format("Bad! Foxit process ""{}"" did not launch.", exepath))
}
}
Try_SaveCurrentPdf()
{
hwnd := this.pedHwnd
wParam := 0xE103 ; We know it by using SpyEx on Foxit main-window.
this.dbg2(Format("Sending WM_COMMAND to HWND=0x{:X}, wParam=0x{:X} (Save PDF)", hwnd, wParam))
dev_PostMessage(hwnd, win32c.WM_COMMAND, wParam, 0)
; -- Using WM_COMMAND is much more reliable than simulating Ctrl+S keypress.
; Effective for both Foxit Reader 7 and Foxit Editor 11
}
RefreshMineFoxitHwnd()
{
; Imagine, user may close and re-open Foxit Reader/Editor process out of FoxitCoedit's control
; (e.g. Foxit crashes and user reopens it). In this case, our recorded this.pedHwnd becomes
; invalid, and, to avoid the hassle of requiring both sides to re-sync, we can make better
; UI experience by auto-detecting new Foxit window and updating our this.pedHwnd automatically.
; We'll do this in our Timer proc.