]> git.lyx.org Git - features.git/blob - development/Win32/packaging/installer2/src/main.nsh
Wininstaller2: auto select downloading dictionary of the same language as the UI...
[features.git] / development / Win32 / packaging / installer2 / src / main.nsh
1 #-------------------------------------------------------------
2 # - - - - - - - - - - - - Preamble - - - - - - - - - - - - - -
3 #-------------------------------------------------------------
4
5   # Do a Cyclic Redundancy Check to make sure the installer
6   # was not corrupted by the download.
7   CRCCheck force
8   # Make the installer as small as possible
9   SetCompressor /SOLID lzma
10   # Make it a Unicode installer
11   Unicode true
12   # Enable support for high DPI resolution
13   ManifestDPIAware true
14
15   Name "LyX ${APP_VERSION}" # Name of the Program to be displayed during installation
16   BrandingText " " # Remove branding text ('Nullsoft Install System vX.XX')
17
18   # ===== Variables =====
19
20     Var LatexPath # Used to store path to "latex.exe"
21     Var StartMenuFolder # Used to store the start menu folder
22
23     # FIXME Variables needed due to "old" set of language strings, delete
24     Var LaTeXInstalled
25     Var LaTeXName
26     Var OldVersionNumber
27     Var AppPre
28     Var AppSuff
29     # R3
30
31     # FIXME Compile time constants needed due to "old" set of language strings, delete
32     !define APP_SERIES_KEY2 ${APP_VERSION}
33     !define APP_DIR_USERDATA "LyX${VERSION_MAJOR}.${VERSION_MINOR}"
34
35   # ========= Multiuser settings =========
36
37     !define MULTIUSER_MUI # Multiuser uses modern UI 2
38     !define MULTIUSER_EXECUTIONLEVEL Highest # Can install with any privileges (admin, user)
39     !if ${APP_ARCHITECTURE} = 64
40       !define MULTIUSER_USE_PROGRAMFILES64 # Install 64 bit program in "Program files" instead of "Program files (x86)"
41     !endif
42
43     !define MULTIUSER_INSTALLMODE_COMMANDLINE # Allow setting installmode in commandline (/AllUsers or /CurrentUser)
44
45     # Set default install mode based on a non empty registry entry (if exists in HKLM, then all users, if HKCU then current user)
46     !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_KEY "SOFTWARE\LyX"
47     !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_VALUENAME "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
48
49     !define MULTIUSER_INSTALLMODE_INSTDIR "LyX ${VERSION_MAJOR}.${VERSION_MINOR}" # Default install dir if not over-installing
50
51     # Retrieve default install location from following registry entry (HKCU/HKLM  is chosen automatically)
52     !define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_KEY "SOFTWARE\LyX"
53     !define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_VALUENAME "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
54
55     !include MultiUser.nsh # Header file: Installer configuration for multi-user Windows environments
56     # When MULTIUSER_INSTALLMODE_COMMANDLINE is defined, MultiUser.nsh defines ${StrStr} and ${UnStrStr} functions
57
58   # ============= Modern UI 2 general settings ===============
59
60     #!include MUI2.nsh # included in MultiUser # Header file for creating modern looking installer
61     !define MUI_ABORTWARNING # Show a message box with a warning when the user wants to close the installer.
62     !define MUI_ICON "..\installer\icons\lyx.ico" # FIXME path # The icon for the installer.
63     !define MUI_UNICON "..\installer\icons\lyx.ico" # FIXME path # The icon for the uninstaller
64     !define MUI_HEADERIMAGE # Display an image on the header of the page.
65     !define MUI_HEADERIMAGE_BITMAP "..\installer\graphics\header.bmp" # FIXME path # Header image source
66     !define MUI_HEADERIMAGE_RIGHT # Display header image on the right and not left
67     !define MUI_WELCOMEFINISHPAGE_BITMAP "..\installer\graphics\wizard.bmp" # FIXME path # Source for image on welcome and finish page of the installer
68     !define MUI_UNWELCOMEFINISHPAGE_BITMAP "..\installer\graphics\wizard.bmp" # FIXME path # Source for image on welcome and finish page of the uninstaller
69
70   # ======== Pages definitions ==========
71
72     # Installer
73
74     !define MUI_WELCOMEPAGE_TEXT $(TEXT_WELCOME) # Use custom welcome text
75     !insertmacro MUI_PAGE_WELCOME # Welcome page
76
77     !insertmacro MUI_PAGE_LICENSE "..\installer\license.rtf" # FIXME path # License page
78
79     !define MUI_PAGE_CUSTOMFUNCTION_PRE PrepareShellCTX # Prepare shell context before the multiuser page is created, so that the installer searches for default install folder in the right context (x64/x32)
80     !insertmacro MULTIUSER_PAGE_INSTALLMODE # Multiuser page
81
82     !define MUI_PAGE_CUSTOMFUNCTION_LEAVE VerifyInstDir # Custom leave-function for directory page to verify installdir
83     !insertmacro MUI_PAGE_DIRECTORY # Directory page (where to install)
84
85     !define MUI_PAGE_CUSTOMFUNCTION_PRE DefaultDesktopFileAssoc # Custom pre-function for components page to search for already present dictionaries (in dictionaries.nsh)
86     !insertmacro MUI_PAGE_COMPONENTS # Components page (what to install)
87
88     # Default startmenu folder if registry contains no information from a previous installment, same as the default install dir
89     !define MUI_STARTMENUPAGE_DEFAULTFOLDER "${MULTIUSER_INSTALLMODE_INSTDIR}"
90     # Define registry entry to store the startmenu folder, also reads this entry in as default startmenu folder in case of over-installing
91     !define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX" # Context dependent on what the user chooses on multiuser page
92     !define MUI_STARTMENUPAGE_REGISTRY_KEY "SOFTWARE\LyX"
93     !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "SMDir${VERSION_MAJOR}${VERSION_MINOR}"
94     !define MUI_PAGE_CUSTOMFUNCTION_PRE PrepareShellCTX # Prepare shell context before the startmenu page is created, so that the installer searches for default startmenu folder in the right context
95     !define MUI_PAGE_CUSTOMFUNCTION_SHOW RetrieveSMState # Custom show-function for startmenu page to disable startmenu dir creation in case of over-installment where previously disabled
96     !insertmacro MUI_PAGE_STARTMENU startmenuPage $StartMenuFolder # Page to choose start menu folder entry, the folder is stored in VARIABLE
97
98     # Latexfolder page
99     !include "src\CustomPages\LaTeXFolder.nsdinc"
100     Page custom LatexFolder_Show LatexFolder_Leave
101
102     !insertmacro MUI_PAGE_INSTFILES # Progress page
103
104     !define MUI_FINISHPAGE_SHOWREADME # Showreadme-checkbox appears
105     !define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED # Showreadme-checkbox disabled by default
106     !define MUI_FINISHPAGE_SHOWREADME_FUNCTION StartLyX # Custom function for the showreadme-checkbox (start latex instead of show readme)
107     !define MUI_FINISHPAGE_SHOWREADME_TEXT $(FinishPageRun) # Custom text for the label on the showreadme-checkbox
108     !define MUI_FINISHPAGE_LINK $(TEXT_FINISH_WEBSITE) # Text for the link on the bottom of finish page
109     !define MUI_FINISHPAGE_LINK_LOCATION "https://www.lyx.org/" # Website opens by clicking on the link on finish page
110     !insertmacro MUI_PAGE_FINISH # Finish page
111
112     # Uninstaller
113
114     !insertmacro MUI_UNPAGE_WELCOME # Welcome page
115     !insertmacro MUI_UNPAGE_CONFIRM # Confirmation page
116
117     !define MUI_PAGE_CUSTOMFUNCTION_PRE un.ComponentsPre # Custom function for components page to prepare strings for the description of a section
118     !insertmacro MUI_UNPAGE_COMPONENTS # Components page
119
120     !insertmacro MUI_UNPAGE_INSTFILES # Progress page
121     !insertmacro MUI_UNPAGE_FINISH # Finish page
122
123   # ======= Other includes =======
124
125     !include "src\lang\TranslatedLanguages.nsh" # languages strings
126
127     #!include LogicLib.nsh # included in MUI2 # Allows using logic commands (such as ${If}..${Else}..${EndIf})
128     #!include LangFile.nsh # included in MUI2 # Header file to create language files that can be included with a single command.
129     !include x64.nsh # Header file to check if target system is 64 bit or not with ${RunningX64}, also defines ${DisableX64FSRedirection} and ${EnableX64FSRedirection}
130     !include NSISList.nsh # Header file to create and work with lists in NSIS (plugin)
131
132     #!include StrFunc.nsh # included in MultiUser # Provides some additional string functions
133     ${StrRep} # Define this function from StrFunc.nsh for further use
134     ${StrStrAdv} # Define this function from StrFunc.nsh for further use
135     ${UnStrStrAdv} # Define this function from StrFunc.nsh for further use
136     ${UnStrRep} # Define this function from StrFunc.nsh for further use
137
138   # ========= Installer Attributes ===========
139
140     VIProductVersion "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION}.${VERSION_BUILD}" # Must be X.X.X.X format
141     VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "LyX"
142     VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductVersion" "LyX ${APP_VERSION} installer"
143     VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "${APP_VERSION}"
144     VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "LyX - The Document Processor"
145     VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "LyX is Copyright © 1995 by Matthias Ettrich, 1995-${COPYRIGHT_YEAR} by the LyX Team"
146     VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "LyX Team"
147     VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalTrademarks" ""
148
149 #-------------------------------------------------------------
150 # - - - - - - - - - - Helper Functions - - - - - - - - - - - -
151 #-------------------------------------------------------------
152
153   # Forward declarations
154   
155   !macro SearchAllRegistry resultVar resultStateIdx resultRegIdx subKey searchFor startStateIdx startRegIdx
156     Push ${subKey}
157     Push ${searchFor}
158     Push ${startStateIdx}
159     Push ${startRegIdx}
160     Call SearchAllRegistry
161     Pop ${resultStateIdx}
162     Pop ${resultRegIdx}
163     Pop ${resultVar}
164   !macroend
165   
166   !define SearchAllRegistry "!insertmacro SearchAllRegistry"
167   
168   !macro SearchRegistry resultVar resultRegIdx subKey searchFor startRegIdx
169     Push ${subKey}
170     Push ${searchFor}
171     Push ${startRegIdx}
172     Call SearchRegistry
173     Pop ${resultRegIdx}
174     Pop ${resultVar}
175   !macroend
176   
177   !define SearchRegistry "!insertmacro SearchRegistry"
178   
179   # Functions
180   
181   Function SearchAllRegistry
182     Exch $9 # startRegIdx
183     Exch
184     Exch $8 # startStateIdx
185     Exch 2
186     Exch $7 # searchFor
187     Exch 3
188     Exch $6 # subKey
189     Push $5
190   
191     ${Do}
192       ${Select} $8
193         ${Case} 0
194           SetShellVarContext all
195           ${If} ${RunningX64}
196             SetRegView 64
197           ${Else}
198             IntOp $8 $8 + 1
199           ${EndIf}
200         ${Case} 1
201           SetShellVarContext all
202           ${If} ${RunningX64}
203             SetRegView 32
204           ${EndIf}
205         ${Case} 2
206           SetShellVarContext current
207           ${If} ${RunningX64}
208             SetRegView 64
209           ${Else}
210             IntOp $8 $8 + 1
211           ${EndIf}
212         ${Case} 3
213           SetShellVarContext current
214           ${If} ${RunningX64}
215             SetRegView 32
216           ${EndIf}
217       ${EndSelect}
218       ${SearchRegistry} $5 $9 $6 $7 $9
219       ${If} $5 != ""
220         ${Break}
221       ${EndIf}
222       IntOp $8 $8 + 1
223       StrCpy $9 0
224     ${LoopUntil} $8 >= 4
225   
226     Exch $5 # resultVar
227     Exch 4
228     Pop $7
229     Pop $6
230     Exch $9 # resultRegIdx
231     Exch
232     Exch $8 # resultStateIdx
233   FunctionEnd
234   
235   Function SearchRegistry
236     Exch $9 # startRegIdx
237     Exch
238     Exch $8 # searchFor
239     Exch 2
240     Exch $7 #subKey
241     Push $6
242     Push $5
243   
244     ${Do}
245       EnumRegKey $6 SHCTX $7 $9
246       ${StrStr} $5 $6 $8
247       IntOp $9 $9 + 1
248       ${If} $5 != ""
249         ${Break}
250       ${EndIf}
251     ${LoopUntil} $6 == ""
252   
253     Exch $5 # resultVar
254     Exch 4
255     Pop $8
256     Pop $6
257     Pop $7
258     Exch $9 # resultRegIdx
259   FunctionEnd
260   
261   !macro PrepareShellCTX
262     ${if} $MultiUser.InstallMode == "CurrentUser"
263       SetShellVarContext current
264     ${Else}
265       SetShellVarContext all
266     ${EndIf}
267     ${If} ${RunningX64}
268       SetRegView ${APP_ARCHITECTURE}
269     ${EndIf}
270   !macroend
271
272   Function PrepareShellCTX
273     !insertmacro PrepareShellCTX
274   FunctionEnd
275
276   Function un.PrepareShellCTX
277     !insertmacro PrepareShellCTX
278   FunctionEnd
279
280 #-------------------------------------------------------------
281 # - - - - - - - - - - - - Sections - - - - - - - - - - - - - -
282 #-------------------------------------------------------------
283
284 # ============ Installer Page Functions ============
285
286 Function .onInit # Callback function, called at the very beginning, when user double clicks on the installer
287
288   !if ${APP_ARCHITECTURE} = 64 # If 64 bit installer
289     # Abort if not 64 bit Windows
290     ${IfNot} ${RunningX64}
291       MessageBox MB_OK|MB_ICONSTOP "Cannot install a 64 bit application on 32 bit Windows, please use the 32 bit installer" /SD IDOK
292       Quit
293     ${EndIf}
294   !EndIf
295
296   # Check Windows version
297   ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
298   ${if} $0 S<= "6.0" # Atleast Win7 is needed
299     MessageBox MB_OK|MB_ICONSTOP "LyX ${APP_VERSION} requires Windows 7 or newer." /SD IDOK
300     Quit
301   ${endif}
302
303   Call PrepareShellCTX # MULTIUSER_INIT should search in the right registry view
304   !insertmacro MULTIUSER_INIT # Verify multiuser privileges
305
306   # Check if the same version of LyX is installed anywhere (admin/user, x64/x32) on the computer
307   StrCpy $1 0
308   StrCpy $2 0
309   ${DoUntil} $1 >= 4
310     ${SearchAllRegistry} $0 $1 $2 "SOFTWARE\LyX" ${APP_VERSION_DOTLESS} $1 $2 # Helper Function from above
311     ${If} $0 == ${APP_VERSION_DOTLESS}
312       ReadRegStr $0 SHCTX "SOFTWARE\LYX\$0" ""
313       ${If} ${FileExists} "$0\Uninstall-LyX.exe"
314         ${IfCmd} MessageBox MB_YESNO|MB_DEFBUTTON2|MB_ICONEXCLAMATION "$(AlreadyInstalled)" /SD IDNO IDNO ${||} Quit ${|}
315       ${Else}
316         DeleteRegKey SHCTX "SOFTWARE\LYX\$0"
317       ${EndIf}
318     ${EndIf}
319   ${Loop} 
320
321   # Check if a newer version of the same LyX series is installed anywhere (admin/user, x64/x32) on the computer
322   StrCpy $1 0
323   StrCpy $2 0
324   ${DoUntil} $1 >= 4
325     ${SearchAllRegistry} $R9 $1 $2 "SOFTWARE\LyX" "${VERSION_MAJOR}${VERSION_MINOR}" $1 $2 # Helper Function from above
326     StrCpy $OldVersionNumber $R9 # FIXME remove
327     ${If} $R9 S> ${APP_VERSION_DOTLESS} # $R9 is used in $(NewerInstalled)
328       ReadRegStr $0 SHCTX "SOFTWARE\LYX\$0" ""
329       ${If} ${FileExists} "$0\Uninstall-LyX.exe"
330         MessageBox MB_OK|MB_ICONSTOP "$(NewerInstalled)" /SD IDOK
331         Quit
332       ${Else}
333         DeleteRegKey SHCTX "SOFTWARE\LYX\$0"
334       ${EndIf}
335     ${EndIf}
336   ${Loop}
337 FunctionEnd
338
339 Function CheckIfRunning # Check that LyX in $INSTDIR is not currently running, called from Function VerifyInstDir and Section -CheckSilent (if silentinstall)
340   ${If} ${RunningX64}
341     ${DisableX64FSRedirection} # We need the following process to be 64 bit on 64 bit system
342   ${EndIf}
343   nsExec::ExecToStack "powershell (Get-Process LyX).Path"
344   Pop $0 # Exit code
345   Pop $0 # Result string
346   ${If} ${RunningX64}
347     ${EnableX64FSRedirection} # Need to be anabled asap or installer might crash
348   ${EndIf}
349   ${StrStr} $0 $0 "$INSTDIR\bin\LyX.exe"
350   ${If} $0 != ""
351     MessageBox MB_OK|MB_ICONSTOP "$(UnInstallRunning)" /SD IDOK
352     Abort # Abort leaving the page (when called from the page callback) / Abort install (when called from the section)
353   ${EndIf}
354 FunctionEnd
355
356 Function VerifyInstDir # Custom Function, called when leaving directory page
357   # if the $INSTDIR does not contain "LyX" we must add a subfolder to avoid that LyX will e.g.
358   # be installed directly to "C:\Program Files" - the uninstaller will then delete the whole
359   # "C:\Program Files" directory
360   ${StrStr} $0 $INSTDIR LyX
361   ${If} $0 == ""
362     StrCpy $INSTDIR "$INSTDIR\${MULTIUSER_INSTALLMODE_INSTDIR}" # Append e.g. "LyX 2.3" to $INSTDIR
363     ${NSD_SetText} $mui.DirectoryPage.Directory $INSTDIR # Refresh Textbox
364     Abort # Abort leaving the page
365   ${EndIf}
366
367   Call CheckIfRunning
368 FunctionEnd
369
370 Function RetrieveSMState # Custom function, called after the Startmenu page has been created
371   ${IfNot} ${FileExists} "$INSTDIR\Uninstall-LyX.exe"
372     Return # Not over-installing
373   ${EndIf}
374
375   Call PrepareShellCTX # Helper function from above
376   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "SMDir${VERSION_MAJOR}${VERSION_MINOR}"
377   ${If} $0 == ""
378     ${If} ${Silent}
379       StrCpy $StartMenuFolder ">$StartMenuFolder"
380     ${Else}
381       GetDlgItem $0 $mui.StartMenuPage 1005 # Get the HWND of the checkbox, MUI2 does not provide variable for that
382       SendMessage $0 ${BM_CLICK} 0 0 # NSIS delivers no other possibility here other than to send a click event to the checkbox
383     ${EndIf}
384   ${EndIf}
385 FunctionEnd
386
387 Function LatexFolder_Show # Custom Function, called before showing the LaTeXFolder page to create it
388   Call FindLatex # detect latex  
389   Pop $0 # Result string from FindLatex
390   # $0 should contain the path to latex.exe, it is used in the fnc_LaTeXFolder_Create
391   ${StrStr} $1 $0 "\latex.exe"
392   ${If} $1 != "" # If latex was actually found
393     ${StrRep} $0 $0 $1 ""
394     # $R9 is used in $(EnterLaTeXFolder) on the page
395     ${StrStr} $1 $0 "\miktex\bin"
396     ${If} $1 != ""
397       StrCpy $R9 "MiKTeX"
398     ${Else}
399       StrCpy $R9 "TeXLive"
400     ${EndIf}
401     StrCpy $LaTeXName $R9 # FIXME remove
402     StrCpy $1 $(EnterLaTeXFolder) # $1 is used on the page
403   ${Else}
404     StrCpy $1 $(EnterLaTeXFolderNone)
405   ${EndIf}
406   ${StrRep} $1 $1 "\r\n" "$\r$\n" # FIXME remove
407
408   Call fnc_LaTeXFolder_Show # In LaTeXFolder.nsdinc, defined by NSIS Dialog Designer
409 FunctionEnd
410
411 Function FindLatex # Searches latex.exe, called from pre and leave functions of latexfolder page
412   # Find "LaTeX Path" value in registry written by a previous installment of the same LyX series, prioritized, because could be user preference
413   Call PrepareShellCTX
414   ${If} ${RunningX64}
415     SetRegView 64
416   ${EndIf}
417   StrCpy $1 0
418   StrCpy $2 0
419   ${DoUntil} $2 > 1
420     ${SearchRegistry} $0 $1 "SOFTWARE\LyX" "${VERSION_MAJOR}${VERSION_MINOR}" $1
421     ${If} $0 != ""
422       ReadRegStr $0 SHCTX "SOFTWARE\LyX\$0" "LaTeX Path"
423       StrCpy $0 "$0\latex.exe"
424       ${If} ${FileExists} $0
425         Push $0
426         Return
427       ${EndIf}
428     ${ElseIf} ${RunningX64}
429       IntOp $2 $2 + 1
430       SetRegView 32
431     ${Else}
432       ${Break}
433     ${EndIf}
434   ${Loop}
435
436   nsExec::ExecToStack "where latex.exe" # Ask CMD to find "latex.exe" for us, works if latexpath is added to PATH
437   Pop $0 # Exit code: 0 = success, 1 = fail
438   ${If} $0 = 0
439     Return
440   ${EndIf}
441   Pop $0 # If search failed, this is garbage, otherwise path to latex
442
443   # Search the registry for TexLive
444   StrCpy $1 0
445   StrCpy $2 0
446   ${DoUntil} $1 >= 4
447     ${SearchAllRegistry} $0 $1 $2 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "TeXLive" $1 $2 # Helper Function from above
448     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "UninstallString"
449     ${StrRep} $0 $0 '"' ""
450     ${StrRep} $0 $0 "tlpkg\installer\uninst.bat" "bin\win32\latex.exe" # TeXLive is not 64 bit yet
451     ${If} ${FileExists} $0
452       Push $0
453       Return
454     ${EndIf}
455   ${Loop}
456
457   # Search the registry for MiKTex  
458   StrCpy $1 0
459   StrCpy $2 0
460   ${DoUntil} $1 >= 4
461     ${SearchAllRegistry} $0 $1 $2 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "MiKTeX" $1 $2 # Helper Function from above
462     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "InstallLocation"
463     StrCpy $0 "$0\miktex\bin\latex.exe"
464     ${If} ${FileExists} $0
465       Push $0
466       Return
467     ${EndIf}
468     ${If} ${RunningX64} # MiKTeX can also be x64
469       ${StrRep} $0 $0 "latex.exe" "x64\latex.exe"
470       ${If} ${FileExists} $0
471         Push $0
472         Return
473       ${EndIf}
474     ${EndIf}
475   ${Loop}
476
477   # No latex was found
478   Push "C:\"
479
480 FunctionEnd
481
482 Function LatexFolder_Leave # Custom function, called when trying to leave LatexFolder page to verify that "latex.exe" really exists in the given path
483   ${NSD_GetText} $hCtl_LaTeXFolder_DirRequest1_Txt $LatexPath
484   ${NSD_GetState} $hCtl_LaTeXFolder_CheckBox1 $0
485
486   ${If} $0 = ${BST_CHECKED}
487     StrCpy $LatexPath ""
488   ${ElseIfNot} ${FileExists} "$LatexPath\latex.exe"
489     MessageBox MB_OK $(InvalidLaTeXFolder)
490     Call FindLatex # Search for latex again
491     Pop $0 # Result string from FindLatex
492     ${StrStrAdv} $0 $0 "\latex.exe" ">" "<" "0" "0" "0" # Get everything before "\latex.exe"
493     ${NSD_SetText} $hCtl_LaTeXFolder_DirRequest1_Txt $0 # Refresh textbox
494     Abort # Abort leaving the page
495   ${EndIf}
496 FunctionEnd
497
498 # ========= Installer Sections =========
499
500 Section -CheckSilent # This section checks if it's a silent install and calls needed callback- and custom-pagefunctions, which are not called in this case
501   ${IfNot} ${Silent}
502     Return
503   ${EndIf}
504
505   # .onInit is called
506
507   Call CheckIfRunning
508
509   Call RetrieveSMState
510
511   Call FindLatex # Search for latex
512   Pop $0 # Result string from FindLatex
513   ${StrStrAdv} $0 $0 "\latex.exe" ">" "<" "0" "0" "0" # Get everything before "\latex.exe"
514   ${If} ${FileExists} "$0\latex.exe"
515     StrCpy $LatexPath $0
516   ${Else}
517     MessageBox MB_OK $(InvalidLaTeXFolder)
518     Quit
519   ${EndIf}
520
521   Call DefaultDesktopFileAssoc
522 SectionEnd
523
524 Section -OverInstallReg # If over-installing, we need to delete the registry keys written by the previous installment
525   Call PrepareShellCTX # Helper function from above
526   StrCpy $1 0
527   ${Do}
528     EnumRegKey $2 SHCTX "SOFTWARE\LyX" $1
529     ${StrStr} $3 $2 "${VERSION_MAJOR}${VERSION_MINOR}"
530     ReadRegStr $0 SHCTX "SOFTWARE\LyX\$3" ""
531     ${If} $0 == $INSTDIR
532       DeleteRegKey SHCTX "SOFTWARE\LyX\$3"
533       DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX$3"
534     ${EndIf}
535     IntOp $1 $1 + 1
536   ${LoopUntil} $2 == ""
537 SectionEnd
538
539 Section -ProgramFiles
540
541   # Install the core LyX files
542   
543   # Initializes the plug-ins dir ($PLUGINSDIR) if not already initialized.
544   # $PLUGINSDIR is automatically deleted when the installer exits.
545   InitPluginsDir
546   
547   # Binaries
548   SetOutPath "$INSTDIR\bin"
549   File "${FILES_LYX}\bin\LyX.exe"
550   File "${FILES_LYX}\bin\tex2lyx.exe"
551   # Visual C++ redistributable (if native build)
552   File "${FILES_DEPS}\bin\concrt140.dll"
553   File "${FILES_DEPS}\bin\msvcp140.dll"
554   File "${FILES_DEPS}\bin\vcamp140.dll"
555   File "${FILES_DEPS}\bin\vccorlib140.dll"
556   File "${FILES_DEPS}\bin\vcomp140.dll"
557   File "${FILES_DEPS}\bin\vcruntime140.dll"
558   !if ${APP_ARCHITECTURE} = 64
559     File "${FILES_DEPS}\bin\vcruntime140_1.dll"
560   !endif
561   # MinGW redistributable (if cross platform with MinGW)
562   #File "${FILES_DEPS}\bin\iconv.dll"
563   #File "${FILES_DEPS}\bin\libbz2-1.dll"
564   #File "${FILES_DEPS}\bin\libfreetype-6.dll"
565   #File "${FILES_DEPS}\bin\libgcc_s_sjlj-1.dll"
566   #File "${FILES_DEPS}\bin\libglib-2.0-0.dll"
567   #File "${FILES_DEPS}\bin\libharfbuzz-0.dll"
568   #File "${FILES_DEPS}\bin\libintl-8.dll"
569   #File "${FILES_DEPS}\bin\libjpeg-62.dll"
570   #File "${FILES_DEPS}\bin\libpcre-1.dll"
571   #File "${FILES_DEPS}\bin\libpcre2-16-0.dll"
572   #File "${FILES_DEPS}\bin\libpng16-16.dll"
573   #File "${FILES_DEPS}\bin\libstdc++-6.dll"
574   #File "${FILES_DEPS}\bin\libtiff-5.dll"
575   #File "${FILES_DEPS}\bin\libwinpthread-1.dll"
576   #File "${FILES_DEPS}\bin\libwebp-7.dll"
577   #File "${FILES_DEPS}\bin\libwebpdemux-2.dll"
578   #File "${FILES_DEPS}\bin\zlib1.dll"
579   # Netpbm
580   File "${FILES_DEPS}\bin\libnetpbm10.dll"
581   File "${FILES_DEPS}\bin\pnmcrop.exe"
582   # Rsvg
583   File "${FILES_DEPS}\bin\rsvg-convert.exe"
584   # Pdfview
585   File "${FILES_DEPS}\bin\pdfview.exe"
586   # Qt libraries
587   File "${FILES_QT}\bin\Qt5Concurrent.dll"
588   File "${FILES_QT}\bin\Qt5Core.dll"
589   File "${FILES_QT}\bin\Qt5Gui.dll"
590   File "${FILES_QT}\bin\Qt5Network.dll"
591   File "${FILES_QT}\bin\Qt5OpenGL.dll"
592   File "${FILES_QT}\bin\Qt5PrintSupport.dll"
593   File "${FILES_QT}\bin\Qt5Svg.dll"
594   File "${FILES_QT}\bin\Qt5Widgets.dll"
595   File "${FILES_QT}\bin\Qt5WinExtras.dll"
596
597   # Qt plugin DLLs
598   SetOutPath "$INSTDIR\bin\imageformats"
599   File "${FILES_QT}\bin\imageformats\qgif.dll"
600   File "${FILES_QT}\bin\imageformats\qico.dll"
601   File "${FILES_QT}\bin\imageformats\qjpeg.dll"
602   File "${FILES_QT}\bin\imageformats\qsvg.dll"
603   File "${FILES_QT}\bin\imageformats\qicns.dll"
604   File "${FILES_QT}\bin\imageformats\qtiff.dll"
605   File "${FILES_QT}\bin\imageformats\qwbmp.dll"
606   File "${FILES_QT}\bin\imageformats\qwebp.dll"
607
608   SetOutPath "$INSTDIR\bin\iconengines"
609   File "${FILES_QT}\bin\iconengines\qsvgicon.dll"
610
611   SetOutPath "$INSTDIR\bin\platforms"
612   File "${FILES_QT}\bin\platforms\qminimal.dll"
613   File "${FILES_QT}\bin\platforms\qwindows.dll"
614
615   SetOutPath "$INSTDIR\bin\styles"
616   File "${FILES_QT}\bin\styles\qwindowsvistastyle.dll"
617
618   # Resources
619   SetOutPath "$INSTDIR\Resources"
620   # recursively copy all files under Resources
621   File /r "${FILES_LYX}\Resources\"
622   File /r "${FILES_DEPS}\Resources\" # Already present files will be skiped
623
624   # Python
625   SetOutPath "$INSTDIR\Python"
626   # recursively copy all files under Python
627   File /r "${FILES_DEPS}\Python\"
628
629   # Components of ImageMagick
630   SetOutPath "$INSTDIR\imagemagick"
631   File /r "${FILES_DEPS}\imagemagick\"
632
633   # Components of Ghostscript
634   SetOutPath "$INSTDIR\ghostscript"
635   File /r "${FILES_DEPS}\ghostscript\"
636
637   # Install unoconv
638   SetOutPath "$INSTDIR\Python\Lib"
639   File "${FILES_DEPS}\bin\unoconv.py"
640   
641   WriteUninstaller "$INSTDIR\Uninstall-LyX.exe"
642 SectionEnd
643
644 Section -TexRessources # Section installs cls files from Ressources\tex and updates latex filebase
645   # First make sure that latex was found in LatexFolder_Leave function
646   ${If} $LatexPath == ""
647     Return
648   ${EndIf}
649
650   # Miktex or Texlive?
651   ${StrStr} $0 $LatexPath "\miktex\bin"
652   ${If} $0 == "" # TexLive
653     ${StrRep} $0 $LatexPath "\bin\win32" "\texmf-dist\tex\latex\lyx"
654     ${If} ${FileExists} "$0\broadway.cls"
655       Return
656     ${EndIf}
657     CopyFiles "$INSTDIR\Resources\tex\*.*" $0
658     ExecWait '$LatexPath\texhash' # Update package file list
659
660   ${Else} # Miktex
661     ${StrRep} $0 $LatexPath $0 "\tex\latex\lyx"
662     ${If} ${FileExists} "$0\broadway.cls"
663       Return
664     ${EndIf}
665     CopyFiles /SILENT "$INSTDIR\Resources\tex\*.*" $0 # First try to put into Miktex install dir
666
667     ${IfNot} ${FileExists} "$0\broadway.cls" # Check if copying succeeded
668       # Didn't suceed to put into install dir, put into users dir
669       nsExec::ExecToStack "$LatexPath\mpm.exe --version" # get the Miktex version
670       Pop $0 # Exit code
671       Pop $0 # Result
672       ${StrStrAdv} $0 $0 "MiKTeX Package Manager " ">" ">" "0" "0" "0" # $0 contains "2.9.7420 ......" now (everything after "MiKTeX Package Manager ")
673       ${StrStrAdv} $0 $0 "." ">" "<" "0" "1" "0" # $0 contains "2.9" now (everything before the second dot)
674       SetShellVarContext current
675       ${If} ${FileExists} "$APPDATA\MiKTeX\$0\tex\latex\lyx\broadway.cls" # UserInstall-dir can not be changed in Miktex currently, so this should work
676         Return
677       ${EndIf}
678       CopyFiles /SILENT "$INSTDIR\Resources\tex\*.*" "$APPDATA\MiKTeX\$0\tex\latex\lyx" # Put into user install dir
679     ${EndIf}
680
681     # Update package file list
682     nsExec::ExecToLog "$LatexPath\initexmf.exe --admin --update-fndb" # Try as admin
683     Pop $0 # Exit code
684     ${If} $0 <> 0 # Not successful
685       nsExec::ExecToLog "$LatexPath\initexmf.exe --update-fndb" # Do as user
686       Pop $0
687     ${EndIf}
688   ${EndIf}
689 SectionEnd
690
691 Section -CompilePython
692   # Compile all Python files to byte-code
693   # The user using the scripts may not have write access
694   FileOpen $R0 "$INSTDIR\compilepy.py" w
695   FileWrite $R0 "import compileall$\r$\n"
696   FileWrite $R0 "compileall.compile_dir('$INSTDIR\python\Lib')$\r$\n"
697   FileWrite $R0 "compileall.compile_dir('$INSTDIR\Resources')$\r$\n"
698   FileClose $R0
699   DetailPrint $(TEXT_CONFIGURE_PYTHON)
700   nsExec::ExecToLog '"$INSTDIR\python\python.exe" "$INSTDIR\compilepy.py"'
701   Pop $0 # Exit code
702   Delete "$INSTDIR\compilepy.py"
703 SectionEnd
704
705 Section -LyxrcDist
706   FileOpen $R0 "$INSTDIR\Resources\lyxrc.dist" a
707   FileSeek $R0 0 END
708   # set some general things
709   FileWrite $R0 '\screen_zoom 120$\r$\n'
710   FileWrite $R0 '\path_prefix "$LatexPath;$$LyXDir\bin;$$LyXDir\Python;$$LyXDir\Python\Lib;$$LyXDir\imagemagick;$$LyXDir\ghostscript\bin'
711
712   # Do not overwrite $R0 in this Section!!!
713   # =======================================
714
715   # Find additional software and add their install locations to the path_prefix
716   # This list contains all software, which write the string value "InstallLocation" in their corresponding keys in "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
717   # To add additional software matching this requirement, just append it to the string after "${List.Append} editors "
718   ${List.Create} editors
719   ${List.Append} editors "jEdit,PSPad,WinShell,TeXnicCenter,WinEdt"
720   ${List.Count} $R1 editors
721   IntOp $R1 $R1 - 1
722   ${For} $R2 0 $R1
723     ${List.Get} $0 editors $R2
724     ${SearchAllRegistry} $0 $1 $1 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" $0 0 0 # Helper Function from above
725     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "InstallLocation"
726     ${If} $0 != ""
727       StrCpy $1 $0 "" -1 # Get the last char
728       ${If} $1 == "\" # If ends with "\"
729         StrCpy $0 $0 -1 # Remove "\"
730       ${EndIf}
731       FileWrite $R0 ";$0"
732     ${EndIf}
733   ${Next}
734   ${List.Destroy} editors
735
736   # Following Software does not create "InstallLocation"
737   # Vim  
738   ${SearchAllRegistry} $0 $1 $1 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "Vim" 0 0 # Helper Function from above
739   ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "DisplayIcon"
740   ${StrStrAdv} $0 $0 "\gvim.exe" ">" "<" "0" "0" "0" # get everything before "\gvim.exe"
741   ${If} $0 != ""
742     FileWrite $R0 ";$0"
743   ${EndIf}
744
745   # JabRef, Hard to locate, JabRef uses MSI installer, lets try to find it through the .bib extension
746   SetShellVarContext all
747   ${If} ${RunningX64}
748     SetRegView 64
749   ${EndIf}
750   ${For} $R1 0 1 # Try as admin and then as user
751     ReadRegStr $0 SHCTX "SOFTWARE\Classes\.bib" ""
752     ReadRegStr $0 SHCTX "Software\Classes\$0\shell\open\command" ""
753     ${StrRep} $0 $0 '"' "" # Remove quotes
754     ${StrStrAdv} $0 $0 "\JabRef.exe" ">" "<" "0" "0" "0" # Get everything before "\JabRef.exe"
755     ${If} ${FileExists} "$0\JabRef.exe"
756       FileWrite $R0 ";$0"
757       ${Break}
758     ${EndIf}
759     SetShellVarContext current # Retry as user
760   ${Next}
761
762   # Gnumeric
763   SetShellVarContext all
764   ${If} ${RunningX64}
765     SetRegView 64
766   ${EndIf}
767   ${For} $R1 0 1 # Try as admin and then as user
768     ReadRegStr $0 SHCTX "Software\Classes\Applications\gnumeric.exe\shell\Open\command" ""
769     ${StrRep} $0 $0 '"' "" # Remove quotes
770     ${StrStrAdv} $0 $0 "\gnumeric.exe" ">" "<" "0" "0" "0" # Get everything before "\gnumeric.exe"
771     ${If} $0 != ""
772       FileWrite $R0 ";$0"
773       ${Break}
774     ${EndIf}
775     SetShellVarContext current # Retry as user
776   ${Next}
777
778   # Pandoc, same as JabRef, can only attempt to ask cmd if added to PATH, the user can't disable that while installing pandoc though
779   nsExec::ExecToStack "where pandoc.exe"
780   Pop $1 # Exit code
781   Pop $0 # Return sring
782   ${If} $1 = 0
783     ${StrStrAdv} $0 $0 "\pandoc.exe" ">" "<" "0" "0" "0" # Get everything before "\pandoc.exe"
784     ${If} $0 != ""
785       FileWrite $R0 ";$0"
786     ${EndIf}
787   ${EndIf}
788
789   ${Map.Create} softwarePathsMap # Create a map (NSISList plugin) to temp store paths of 3rd party software
790
791   # LilyPond
792   ${SearchAllRegistry} $0 $1 $1 "SOFTWARE" "LilyPond" 0 0 # Helper Function from above
793   ReadRegStr $0 SHCTX "SOFTWARE\$0" "Install_Dir"
794   ${If} $0 != ""
795     FileWrite $R0 ";$0\usr\bin"
796     ${Map.Set} softwarePathsMap "LilyPond" "$0\usr\bin"
797   ${EndIf}
798
799   # Gimp
800   ${SearchAllRegistry} $2 $1 $1 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "GIMP" 0 0 # Helper Function from above
801   ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$2" "InstallLocation"
802   ${If} $0 != ""
803     FileWrite $R0 ";$0bin" # Install Location ends with '\' for Gimp in Registry
804     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$2" "DisplayIcon"
805     ${StrStrAdv} $0 $0 ".exe" ">" "<" "0" "0" "0" # Get everything before the ".exe"
806     ${StrStrAdv} $0 $0 "\" "<" ">" "0" "0" "0" # Get everything after the last "\", $0 contains "gimp-10" now
807     ${Map.Set} softwarePathsMap "ImageEditor" $0
808   ${EndIf}
809
810   # Krita
811   ${SearchAllRegistry} $0 $1 $1 "SOFTWARE" "Krita" 0 0 # Helper Function from above
812   ReadRegStr $0 SHCTX "SOFTWARE\$0" "InstallLocation"
813   ${If} $0 != ""
814     FileWrite $R0 ";$0\bin"
815     ${Map.Set} softwarePathsMap "ImageEditor" "krita"
816   ${EndIf}
817
818   # Photoshop
819   SetShellVarContext all
820   ${If} ${RunningX64}
821     SetRegView 64
822   ${EndIf}
823   ${For} $R1 0 1 # Try as admin and then as user
824     ReadRegStr $0 SHCTX "Software\Classes\Applications\Photoshop.exe\shell\Open\command" ""
825     ${StrRep} $0 $0 '"' "" # Remove quotes
826     ${StrStrAdv} $0 $0 "\photoshop.exe" ">" "<" "0" "0" "0" # Get everything before "\photoshop.exe"
827     ${If} $0 != ""
828       FileWrite $R0 ";$0"
829       ${Break}
830     ${EndIf}
831     SetShellVarContext current # Retry as user
832   ${Next}
833
834   FileWrite $R0 '"$\r$\n'
835
836   ClearErrors
837   
838   # use pdfview for all types of PDF files
839   FileWrite $R0 '\format "pdf5" "pdf" "PDF (LuaTeX)" "u" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
840                 \format "pdf4" "pdf" "PDF (XeTeX)" "X" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
841                 \format "pdf3" "pdf" "PDF (dvipdfm)" "m" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
842                 \format "pdf2" "pdf" "PDF (pdflatex)" "F" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
843                 \format "pdf" "pdf" "PDF (ps2pdf)" "P" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n'
844
845   # If krita or gimp was found previously
846   ${Map.Get} $0 softwarePathsMap "ImageEditor"
847   ${If} $0 != "__NULL" # If "ImageEditor" is set in map    
848     FileWrite $R0 '\format "gif" "gif" "GIF" "" "auto" "$0" "" "image/gif"$\r$\n\
849                   \format "jpg" "jpg, jpeg" "JPEG" "" "auto" "$0" "" "image/jpeg"$\r$\n\
850                   \format "png" "png" "PNG" "" "auto" "$0" "" "image/x-png"$\r$\n'
851   ${EndIf}
852   
853   FileWrite $R0 '\converter "wmf" "eps" "magick -density 300 $$$$i $$$$o" ""$\r$\n\
854     \converter "emf" "eps" "magick -density 300 $$$$i $$$$o" ""$\r$\n'
855
856   # If LilyPond was found previously
857   ${Map.Get} $0 softwarePathsMap "LilyPond"
858   ${If} $0 != "__NULL" # If "LilyPond" is set in map
859     FileWrite $R0 '\format "lilypond-book" "lytex" "LilyPond book (LaTeX)" "" "" "auto" "document,menu=export" ""$\r$\n\
860                   \converter "lilypond-book" "pdflatex" "python \"$0\\lilypond-book.py\" --safe --pdf --latex-program=pdflatex --lily-output-dir=ly-pdf $$$$i" ""$\r$\n\
861                   \converter "lilypond-book" "xetex" "python \"$0\\lilypond-book.py\" --safe --pdf --latex-program=xelatex --lily-output-dir=ly-pdf $$$$i" ""$\r$\n\
862                   \converter "lilypond-book" "luatex" "python \"$0\\lilypond-book.py\" --safe --pdf --latex-program=lualatex --lily-output-dir=ly-pdf $$$$i" ""$\r$\n\
863                   \converter "lilypond-book" "latex" "python \"$0\\lilypond-book.py\" --safe --lily-output-dir=ly-eps $$$$i" ""$\r$\n'
864   ${EndIf}
865
866   ${Map.Destroy} softwarePathsMap
867
868   FileClose $R0
869   
870   ${If} ${Errors}
871     MessageBox MB_OK|MB_ICONEXCLAMATION "$(ModifyingConfigureFailed)" /SD IDOK
872     ClearErrors
873   ${EndIf}
874 SectionEnd
875
876 Section "$(SecFileAssocTitle)" SecFileAssoc # Registry information to assiciate ".lyx" extension with this LyX installation
877   Call PrepareShellCTX # Helper function from above
878
879   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" "" "$INSTDIR\bin\LyX.exe" # Writes in both 64 and 32 bit registry
880   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" "Version" ${APP_VERSION} # Writes in both 64 and 32 bit registry
881   
882   WriteRegStr SHCTX "Software\Classes\.lyx" "" "LyX.Document"
883   WriteRegStr SHCTX "Software\Classes\.lyx" "Content Type" "application/lyx"
884
885   WriteRegStr SHCTX "Software\Classes\LyX.Document" "" "LyX Document"
886   WriteRegStr SHCTX "Software\Classes\LyX.Document\DefaultIcon" "" "$INSTDIR\bin\LyX.exe,1"
887   WriteRegStr SHCTX "Software\Classes\LyX.Document\Shell\open\command" "" '"$INSTDIR\bin\LyX.exe" "%1"'
888
889   # we need to update also the automatically created entry about the lyx.exe
890   # otherwise .lyx-files will could be opened with an older LyX version
891   ReadRegStr $0 SHCTX "Software\Classes\Applications\LyX.exe\shell\open\command" ""
892   ${if} $0 != "" # if something was found
893     WriteRegStr SHCTX "Software\Classes\Applications\LyX.exe\shell\open\command" "" '"$INSTDIR\bin\LyX.exe" "%1"'
894   ${endif}
895
896   WriteRegStr SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}" $INSTDIR # Save user preference for over-installing
897 SectionEnd
898
899 Section "$(SecDesktopTitle)" SecDesktop
900   SetOutPath "$INSTDIR\bin" # Need to set this so that the shortcut to lyx.exe is executed in the bin folder
901   CreateShortCut "$DESKTOP\LyX ${VERSION_MAJOR}.${VERSION_MINOR}.lnk" "$INSTDIR\bin\LyX.exe" "" "" "" "" "" "LyX - The Document Processor" # Last parameter is the comment of the shortcut
902   Call PrepareShellCTX
903   WriteRegStr SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}" $INSTDIR # Save user preference for over-installing
904 SectionEnd
905
906 Section -StartMenuFolder # Creates starm menu shortcut and website links to lyx website and lyx wiki
907   Call PrepareShellCTX # Helper function from above, need to call be before the following macro, because the macro writes registry string (storing the default startmenu folder)
908   !insertmacro MUI_STARTMENU_WRITE_BEGIN startmenuPage # This macro also assures that the folder is not empty for us
909     SetOutPath "$INSTDIR\bin" # Need to set this so that the shortcut to lyx.exe is executed in the bin folder
910     CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
911     CreateShortCut "$SMPROGRAMS\$StartMenuFolder\LyX.lnk" "$INSTDIR\bin\LyX.exe" "" "" "" "" "" "LyX - The Document Processor" # Last parameter is the comment of the shortcut
912     SetOutPath $INSTDIR
913     CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Uninstall LyX.lnk" "$INSTDIR\Uninstall-LyX.exe"
914     WriteINIStr "$SMPROGRAMS\$StartMenuFolder\LyX Website.url" "InternetShortcut" "URL" "https://www.lyx.org/"
915     WriteINIStr "$SMPROGRAMS\$StartMenuFolder\LyX Wiki.url" "InternetShortcut" "URL" "https://wiki.lyx.org"
916   !insertmacro MUI_STARTMENU_WRITE_END
917 SectionEnd
918
919 !include "src\dictionaries.nsh"
920
921 Section -SoftwareRegistry # Registry information in "SOFTWARE" subkey
922   Call PrepareShellCTX # Helper function from above
923
924   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "latestVersion"
925   ${If} $0 S< ${APP_VERSION_DOTLESS}
926     WriteRegStr SHCTX "SOFTWARE\LyX" "latestVersion" ${APP_VERSION_DOTLESS}
927   ${EndIf}
928   WriteRegStr SHCTX "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" "" $INSTDIR
929   WriteRegStr SHCTX "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" "LaTeX Path" $LatexPath
930   WriteRegStr SHCTX "SOFTWARE\LyX" "InstDir${VERSION_MAJOR}${VERSION_MINOR}" $INSTDIR # Remember install dir for the next over-installment of the same series
931 SectionEnd
932
933 Section -UninstallInfoRegistry # Registry information in "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" subkey
934   Call PrepareShellCTX # Helper function from above
935
936   ${If} $MultiUser.InstallMode == "AllUsers"
937     WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayName" "LyX ${APP_VERSION}"
938   ${Else}
939     WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayName" "LyX ${APP_VERSION} $(TEXT_INSTALL_CURRENTUSER)"
940   ${EndIf}
941
942   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "UninstallString" '"$INSTDIR\Uninstall-LyX.exe"'
943   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayVersion" "${APP_VERSION}"
944   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "VersionMajor" "${VERSION_MAJOR}"
945   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "VersionMinor" "${VERSION_MINOR}"
946   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayIcon" "$INSTDIR\bin\LyX.exe"
947   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "URLUpdateInfo" "https://www.lyx.org/"
948   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "URLInfoAbout" "https://www.lyx.org/AboutLyX"
949   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "Publisher" "LyX Team"
950   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "HelpLink" "https://www.lyx.org/MailingLists"
951   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "NoModify" 0x00000001
952   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "NoRepair" 0x00000001
953   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "InstallLocation" "$INSTDIR"
954
955   ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
956   IntFmt $0 "0x%08X" $0
957   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "EstimatedSize" $0
958 SectionEnd
959
960 Section -ConfigureScript # Runs the configure.py script
961   ${StrStr} $0 $LatexPath "\miktex\bin"
962   # R9 is used in $(TEXT_CONFIGURE_LYX)
963   ${If} $0 == "" # TexLive
964     StrCpy $R9 "TeXLive"
965   ${Else}
966     StrCpy $R9 "MiKTeX"
967   ${EndIf}
968   StrCpy $LaTeXInstalled $R9 # FIXME remove
969   DetailPrint $(TEXT_CONFIGURE_LYX) # Uses R9 to display the name of the installed latex distribution
970   
971   Call PrepareShellCTX
972   SetShellVarContext current # Otherwise $APPDATA would return C:\ProgrammData instead of C:\Users\username\AppData\Roaming
973   SetOutPath "$APPDATA\LyX${VERSION_MAJOR}.${VERSION_MINOR}" # Need to run configure from the user dir, because it creates .lst files and some folders.
974   nsExec::ExecToLog '"$INSTDIR\Python\python.exe" "$INSTDIR\Resources\configure.py"'
975   Pop $0 # Return value
976 SectionEnd
977
978 Function DefaultDesktopFileAssoc # Custom function, called before the components page, reads the states of SecDesktop and SecFileAssoc from registry (if overinstalling) and sets them
979
980   Call CheckDictionaries # Search for already installed dictionaries, in dictionaries.nsh
981
982   ${IfNot} ${FileExists} "$INSTDIR\Uninstall-LyX.exe"
983     Return # Not over-installing
984   ${EndIf}
985
986   Call PrepareShellCTX
987   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}"
988   ${If} $0 == ""
989     SectionSetFlags ${SecFileAssoc} 0 # 0 means unselected
990   ${EndIf}
991   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}"
992   ${If} $0 == ""
993     SectionSetFlags ${SecDesktop} 0 # 0 means unselected
994   ${EndIf}
995 FunctionEnd
996
997 Function StartLyX
998   ShellExecAsUser::ShellExecAsUser "" "$INSTDIR\bin\LyX.exe"
999 FunctionEnd
1000
1001 # Installer Section Descriptions
1002
1003 !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
1004   !insertmacro MUI_DESCRIPTION_TEXT ${SecFileAssoc} "$(SecFileAssocDescription)"
1005   !insertmacro MUI_DESCRIPTION_TEXT ${SecDesktop} "$(SecDesktopDescription)"
1006   !insertmacro MUI_DESCRIPTION_TEXT ${SecDictionaries} "$(SecDictionariesDescription)" # In dictionaries.nsh
1007   !insertmacro MUI_DESCRIPTION_TEXT ${SecThesaurus} "$(SecThesaurusDescription)" # In dictionaries.nsh
1008 !insertmacro MUI_FUNCTION_DESCRIPTION_END
1009
1010 # ===== Uninstaller ======
1011
1012 Function un.onInit # Callback function, called when the uninstaller initializes
1013   # Check that LyX is not currently running
1014   ${If} ${RunningX64}
1015     ${DisableX64FSRedirection} # We need the following process to be 64 bit on 64 bit system
1016   ${EndIf}
1017   nsExec::ExecToStack "powershell (Get-Process LyX).Path"
1018   Pop $0 # Exit code
1019   Pop $0 # Result string
1020   ${If} ${RunningX64}
1021     ${EnableX64FSRedirection} # Need to be enabled asap or installer might crash
1022   ${EndIf}
1023   ${UnStrStr} $0 $0 "$INSTDIR\bin\LyX.exe"
1024   ${If} $0 != ""
1025     MessageBox MB_OK|MB_ICONSTOP "$(UnInstallRunning)" /SD IDOK
1026     Quit # Quit uninstaller
1027   ${EndIf}
1028
1029   Call un.PrepareShellCTX
1030   !insertmacro MULTIUSER_UNINIT
1031
1032   # Ascertain whether the user has sufficient privileges to uninstall.
1033   # abort when LyX was installed with admin permissions but the user doesn't have administrator privileges
1034   Call un.PrepareShellCTX # Helperfunction from above
1035   ReadRegStr $0 HKLM "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" ""
1036   ${If} $0 == $INSTDIR
1037     ${If} $MultiUser.Privileges != "Admin"
1038     ${AndIf} $MultiUser.Privileges != "Power"
1039       MessageBox MB_OK|MB_ICONSTOP "$(UnNotAdminLabel)" /SD IDOK
1040       Abort
1041     ${Else}
1042       StrCpy $MultiUser.InstallMode "AllUsers"
1043     ${EndIf}
1044   ${Else}
1045     ReadRegStr $0 HKCU "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" ""
1046     ${If} $0 == ""
1047       MessageBox MB_OK|MB_ICONEXCLAMATION "$(UnNotInRegistryLabel)" /SD IDOK
1048     ${Else}
1049       StrCpy $MultiUser.InstallMode "CurrentUser"
1050     ${EndIf}
1051   ${EndIf}
1052
1053   # question message if the user really wants to uninstall LyX
1054   ${IfCmd} MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "$(UnReallyRemoveLabel)" /SD IDYES IDNO ${||} Quit ${|}
1055 FunctionEnd
1056
1057 Function un.ComponentsPre # Callback Function, called before the components page is loaded
1058   # R9 and R8 are used in $(SecUnPreferencesDescription)
1059   SetShellVarContext current
1060   ${UnStrStrAdv} $R9 $PROFILE "\" "<" "<" "0" "0" "0" # Get everything before the last "\" of $PROFILE ("C:\Users\username") -> $R9: "C:\Users"
1061   StrCpy $AppPre $R9 # FIXME remove
1062   ${UnStrRep} $R8 $APPDATA "$PROFILE\" "" # "C:\Users\username\" gets replaced with "" in "C:\Users\username\AppData\Roaming" -> $R8: "Appdata\Roaming"
1063   StrCpy $AppSuff $R8 # FIXME remove
1064 FunctionEnd
1065
1066 Section "un.LyX" un.SecUnProgramFiles # Deletes files in $INSTDIR and registry entries
1067   SectionIn RO
1068   # LaTeX class files that were installed together with LyX
1069   # will not be uninstalled because other LyX versions will
1070   # need them and these few files don't harm to stay in LaTeX 
1071
1072   Call un.PrepareShellCTX # Helperfunction from above
1073
1074   # Subfolders and unistaller
1075   RMDir /r "$INSTDIR\bin"
1076   RMDir /r "$INSTDIR\Resources"
1077   RMDir /r "$INSTDIR\Python"
1078   RMDir /r "$INSTDIR\imagemagick"
1079   RMDir /r "$INSTDIR\ghostscript"
1080   Delete "$INSTDIR\Uninstall-LyX.exe"
1081   # Install directory
1082   SetOutPath $PLUGINSDIR
1083   RMDir $INSTDIR
1084
1085   # Startmenu
1086   !insertmacro MUI_STARTMENU_GETFOLDER startmenuPage $StartMenuFolder # Reads registry value written with macro MUI_STARTMENU_WRITE_BEGIN
1087   ShellLink::GetShortCutTarget "$SMPROGRAMS\$StartMenuFolder\LyX.lnk" # Check if the shortcut really is pointing to the current installation
1088   Pop $0
1089   ${IfNot} ${FileExists} $0 # If still exists, then belongs to another lyx installment, so don't delete
1090     RMDir /r "$SMPROGRAMS\$StartMenuFolder"
1091     DeleteRegValue SHCTX "SOFTWARE\LyX" "SMDir${VERSION_MAJOR}${VERSION_MINOR}"
1092   ${EndIf}
1093
1094   # Desktop icon
1095   ShellLink::GetShortCutTarget "$DESKTOP\LyX ${VERSION_MAJOR}.${VERSION_MINOR}.lnk"
1096   Pop $0
1097   ${UnStrStr} $0 $0 $INSTDIR
1098   ${If} $0 != ""
1099     Delete "$DESKTOP\LyX ${VERSION_MAJOR}.${VERSION_MINOR}.lnk"
1100   ${EndIf}
1101   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}"
1102   ${If} $0 == $INSTDIR
1103     DeleteRegValue SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}"
1104   ${EndIf}
1105
1106   # File association
1107   ReadRegStr $0 SHCTX "Software\Classes\LyX.Document\DefaultIcon" ""
1108   ${UnStrStr} $0 $0 $INSTDIR
1109   ${If} $0 != ""
1110     DeleteRegKey SHCTX "Software\Classes\LyX.Document"
1111     DeleteRegKey SHCTX "Software\Classes\.lyx"
1112   ${EndIf}
1113   ReadRegStr $0 SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" ""
1114   ${UnStrStr} $0 $0 $INSTDIR
1115   ${If} $0 != ""
1116     DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" # Deletes key in both 64 and 32 bit registry
1117   ${EndIf}
1118   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}"
1119   ${If} $0 == $INSTDIR
1120     DeleteRegValue SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}"
1121   ${EndIf}
1122   DeleteRegKey SHCTX "Software\Classes\Applications\LyX.exe"
1123
1124   # Uninstaller info registry
1125   DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}"
1126
1127   # Software registry
1128   DeleteRegKey SHCTX "SOFTWARE\LyX\${APP_VERSION_DOTLESS}"
1129   DeleteRegValue SHCTX "SOFTWARE\LyX" "latestVersion"
1130   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
1131   ${If} $0 == $INSTDIR
1132     DeleteRegValue SHCTX "SOFTWARE\LyX" "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
1133   ${EndIf}
1134   DeleteRegValue SHCTX "SOFTWARE\LyX" "latestVersion"
1135   # Now that "latestVersion" is gone we need to find if there is another (newest) LyX version installed and write it's verion to "latestVersion"
1136   # The keys in the registry are sorted alphabetically, so we dont need to compare, the latest found will be the newest version
1137   StrCpy $0 0
1138   StrCpy $1 ""
1139   ${Do}
1140     StrCpy $2 $1
1141     EnumRegKey $2 SHCTX "SOFTWARE\LyX" $0
1142     IntOp $0 $0 + 1
1143   ${LoopUntil} $1 == ""
1144   ${If} $2 != ""
1145     WriteRegStr SHCTX "SOFTWARE\LyX" "latestVersion" $2
1146   ${EndIf}
1147   DeleteRegKey /ifempty SHCTX "SOFTWARE\LyX"
1148 SectionEnd
1149
1150 Section /o "un.$(UnLyXPreferencesTitle)" un.SecUnPreferences # Deletes user preferences folders for all users (if admin) and registry entry made by LyX to HKCU
1151   Call un.PrepareShellCTX
1152   SetShellVarContext current
1153   # If installed as user, only delete folder and registry key of current user
1154   ${If} $MultiUser.InstallMode == "CurrentUser"
1155     RMDir /r "$APPDATA\LyX${VERSION_MAJOR}.${VERSION_MINOR}"
1156
1157   ${Else} # If installed as admin, things are a little more complicated
1158     ${UnStrStrAdv} $2 $PROFILE "\" "<" ">" "0" "0" "0" # Get everything after the last "\" of $PROFILE ("C:\Users\username") -> $2: "username"
1159     ${UnStrStrAdv} $3 $PROFILE "\" "<" "<" "0" "0" "0" # Get everything before the last "\" of $PROFILE ("C:\Users\username") -> $3: "C:\Users"
1160     ClearErrors
1161     FindFirst $1 $0 "$3\*" # Find any folder/file in "C:\Users"
1162     ${DoUntil} ${Errors} # Errors are set while FindFirst/FindNext if the search is completed (there are no more files)
1163       ${UnStrRep} $0 $APPDATA $2 $0 # Replace "username" in $APPDATA ("C:\Users\username\AppData\Roaming") with what was found
1164       ${If} ${FileExists} "$0\LyX${VERSION_MAJOR}.${VERSION_MINOR}\*" # Check if anything exists in the LyX userdir
1165         RMDir /r "$0\LyX${VERSION_MAJOR}.${VERSION_MINOR}"
1166       ${EndIf}
1167       FindNext $1 $0
1168     ${Loop}
1169     FindClose $1
1170   ${EndIf}
1171
1172   # Can only delete for current user, because Admin can't access registry of other users
1173   DeleteRegKey HKCU "Software\LyX\LyX${VERSION_MAJOR}.${VERSION_MINOR}"
1174   DeleteRegKey /ifempty HKCU "Software\LyX"
1175 SectionEnd
1176
1177 # Uninstaller Section Descriptions
1178
1179 !insertmacro MUI_UNFUNCTION_DESCRIPTION_BEGIN
1180   !insertmacro MUI_DESCRIPTION_TEXT ${un.SecUnProgramFiles} "$(SecUnProgramFilesDescription)"
1181   !insertmacro MUI_DESCRIPTION_TEXT ${un.SecUnPreferences} "$(SecUnPreferencesDescription)"
1182 !insertmacro MUI_UNFUNCTION_DESCRIPTION_END