home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / System / SysFunc.nsh < prev    next >
Text File  |  2003-03-26  |  11KB  |  396 lines

  1. ; Some useful functions based on System plugin
  2. ; (c) brainsucker, 2002
  3. ; (r) BSForce
  4.  
  5. ; Check for double includes
  6. !ifndef SysFunc.NSH.Included
  7. !define SysFunc.NSH.Included
  8.  
  9. !include "${NSISDIR}\Contrib\System\System.nsh"
  10.  
  11. !verbose 3      ; For WinMessages especially
  12.   !include "${NSISDIR}\Include\WinMessages.nsh"
  13. !verbose 4
  14.  
  15. ; ================= GetInstallerExeName implementation =================
  16.  
  17. ; Adopted Get Parameter function -> now it gets full installer.exe path
  18. ; input - nothing, output -> full path at the top of the stack
  19. Function GetInstallerExeName
  20.    Push $R0
  21.    Push $R1
  22.    Push $R2
  23.    StrCpy $R0 $CMDLINE 1
  24.    StrCpy $R1 '"'
  25.    StrCpy $R2 1
  26.    StrCmp $R0 '"' loop
  27.      StrCpy $R1 ' ' ; we're scanning for a space instead of a quote
  28.    loop:
  29.      StrCpy $R0 $CMDLINE 1 $R2
  30.      StrCmp $R0 $R1 loop2
  31.      StrCmp $R0 "" loop2
  32.      IntOp $R2 $R2 + 1
  33.      Goto loop
  34.    loop2:
  35.  
  36.    ; Ok, have we found last exename character or string end?
  37.    StrCmp $R0 "" "" +2
  38.         IntOp $R2 $R2 - 1       ; last exename char
  39.    StrCmp $R1 ' ' +3    ; was first character the '"', or something other?
  40.         StrCpy $R1 1    ; it was quote
  41.         Goto +2
  42.         StrCpy $R1 0    
  43.    IntOp $R2 $R2 - $R1
  44.    StrCpy $R0 $CMDLINE $R2 $R1  
  45.  
  46.    SearchPath $R0 $R0      ; expand file name to full path
  47.  
  48.    Pop $R2
  49.    Pop $R1
  50.    Exch $R0
  51. FunctionEnd
  52.  
  53. ; ================= systemGetFileSysTime implementation =================
  54.  
  55. !macro smGetFileSysTime FILENAME
  56.         Push ${FILENAME}
  57.         Call systemGetFileSysTime
  58.         Pop  $R0
  59. !macroend
  60.  
  61. ; -----------------------------------------------------------------
  62. ; systemGetFileSysTime (params on stack):
  63. ;       FILENAME        -       name of file to get file time
  64. ; returns to stack (SYSTEMTIME struct addr)
  65. ; -----------------------------------------------------------------
  66.  
  67. ; uses original method from NSIS
  68. Function systemGetFileSysTime
  69.     System::Store "s r1"
  70.  
  71.     StrCpy $R0 0     
  72.  
  73.     ; create WIN32_FIND_DATA struct
  74.     System::Call '*${stWIN32_FIND_DATA} .r2'
  75.  
  76.     ; Find file info
  77.     System::Call '${sysFindFirstFile}(r1, r2) .r3'
  78.  
  79.     ; ok?
  80.     IntCmp $3 ${INVALID_HANDLE_VALUE} sgfst_exit
  81.  
  82.     ; close file search
  83.     System::Call '${sysFindClose}(r3)'
  84.  
  85.     ; Create systemtime struct for local time
  86.     System::Call '*${stSYSTEMTIME} .R0'
  87.  
  88.     ; Get File time
  89.     System::Call '*$2${stWIN32_FIND_DATA} (,,, .r3)'
  90.  
  91.     ; Convert file time (UTC) to local file time
  92.     System::Call '${sysFileTimeToLocalFileTime}(r3, .r1)'
  93.  
  94.     ; Convert file time to system time
  95.     System::Call '${sysFileTimeToSystemTime}(r1, R0)'
  96.  
  97. sgfst_exit:
  98.     ; free used memory for WIN32_FIND_DATA struct
  99.     System::Free $2    
  100.  
  101.     System::Store "P0 l"
  102. FunctionEnd
  103.  
  104. ; ================= systemMessageBox implementation =================
  105.  
  106. ; return to $R0
  107. !macro smMessageBox MODULE MSG CAPTION STYLE ICON
  108.      Push "${ICON}"
  109.      Push "${STYLE}"
  110.      Push "${CAPTION}"
  111.      Push "${MSG}"
  112.      Push "${MODULE}"
  113.      Call systemMessageBox
  114.      Pop $R0
  115. !macroend
  116.  
  117. ; -----------------------------------------------------------------
  118. ; systemMessageBox (params on stack):
  119. ;       Module: either handle ("i HANDLE", HANDLE could be 0) or "modulename" 
  120. ;       Msg: text of message
  121. ;       Caption: caption of message box window
  122. ;       Style: style, buttons etc
  123. ;       Icon: either icon handle ("i HANDLE") or resource name 
  124. ; returns to stack
  125. ; -----------------------------------------------------------------
  126. Function systemMessageBox
  127.      System::Store "s r2r3r4r5r6"
  128.  
  129.      ; may be Module is module handle?
  130.      StrCpy $1 $2
  131.      IntCmp $1 0 0 smbnext smbnext
  132.  
  133.      ; Load module and get handle
  134.      System::Call '${sysLoadLibrary}($2) .r1'
  135.      IntCmp $1 0 0 smbnext smbnext
  136.  
  137.      ; Get module handle. This may look stupid (to call GetModuleHandle in case
  138.      ; when the LoadLibrary doesn't works, but LoadLibrary couldn't return 
  139.      ; a handle to starting process (for 'i 0').
  140.      System::Call '${sysGetModuleHandle}($2) .r1'
  141.  
  142.      ; Indicate that LoadLibrary wasn't used
  143.      StrCpy $2 1
  144. smbnext:
  145.      ; Create MSGBOXPARAMS structure
  146.      System::Call '*${stMSGBOXPARAMS}(, $HWNDPARENT, r1, r3, r4, "$5|${MB_USERICON}", $6, _) .r0'
  147.      ; call MessageBoxIndirect
  148.      System::Call '${sysMessageBoxIndirect}(r0) .R0'
  149.      ; free MSGBOXPARAMS structure
  150.  
  151.      System::Free $0
  152.  
  153.      ; have we used load library at start?
  154.      IntCmp $2 0 0 smbskipfree smbskipfree
  155.      ; No, then free the module
  156.      System::Call '${sysFreeLibrary}(r1)'
  157.  
  158. smbskipfree:
  159.      System::Store "P0 l"   
  160. FunctionEnd
  161.  
  162. ; ================= systemSplash implementation =================
  163.  
  164. ; returns to $R0
  165. !macro smSystemSplash DELAY FILE
  166.     Push ${FILE}
  167.     Push ${DELAY}
  168.     call systemSplash
  169.     Pop $R0    
  170. !macroend
  171.  
  172. ; -----------------------------------------------------------------
  173. ; systemSplash (params on stack):
  174. ;       Delay - time in ms to show the splash
  175. ;       File - bitmap (& audio) file name (without extension)
  176. ; returns to stack
  177. ; -----------------------------------------------------------------
  178.  
  179. Function _systemSplashWndCB
  180.    ; Callback receives 4 values
  181.    System::Store "s r2r5r7r9"
  182.  
  183.    ; Message branching
  184.    IntCmp $5 ${WM_CLOSE} m_Close
  185.    IntCmp $5 ${WM_TIMER} m_Timer
  186.    IntCmp $5 ${WM_LBUTTONDOWN} m_Lbtn
  187.    IntCmp $5 ${WM_CREATE} m_Create
  188.    IntCmp $5 ${WM_PAINT} m_Paint
  189.    goto default
  190.  
  191. m_Create:
  192.    ; Create structures
  193.    System::Call "*${stRECT} (_) .R8"
  194.    System::Call "*${stBITMAP} (_, &l0 .R7) .R9"
  195.  
  196.    ; Get bitmap info
  197.    System::Call "${sysGetObject} (r6, R7, R9)" 
  198.    
  199.    ; Get desktop info
  200.    System::Call "${sysSystemParametersInfo} (${SPI_GETWORKAREA}, 0, R8, 0)" 
  201.  
  202.    ; Style (callbacked)
  203.    System::Call "${sysSetWindowLong} (r2, ${GWL_STYLE}, 0) .s" 
  204.    !insertmacro SINGLE_CALLBACK 5 $R7 1 _systemSplashWndCB
  205.  
  206.    ; Calculate and set window pos
  207.  
  208.    ; Get bmWidth(R2) and bmHeight(R3)
  209.    System::Call "*$R9${stBITMAP} (,.R2,.R3)"
  210.    ; Get left(R4), top(R5), right(R6), bottom(R7)
  211.    System::Call "*$R8${stRECT} (.R4,.R5,.R6,.R7)"
  212.  
  213.    ; Left pos
  214.    IntOp $R0 $R6 - $R4
  215.    IntOp $R0 $R0 - $R2
  216.    IntOp $R0 $R0 / 2
  217.    IntOp $R0 $R0 + $R4
  218.  
  219.    ; Top pos
  220.    IntOp $R1 $R7 - $R5
  221.    IntOp $R1 $R1 - $R3
  222.    IntOp $R1 $R1 / 2
  223.    IntOp $R1 $R1 + $R5
  224.  
  225.    System::Call "${sysSetWindowPos} (r2, 0, R0, R1, R2, R3, ${SWP_NOZORDER}) .s" 
  226.    !insertmacro SINGLE_CALLBACK 6 $R7 1 _systemSplashWndCB
  227.  
  228.    ; Show window
  229.    System::Call "${sysShowWindow} (r2, ${SW_SHOW}) .s" 
  230.    !insertmacro SINGLE_CALLBACK 7 $R7 1 _systemSplashWndCB
  231.  
  232.    ; Set Timer
  233.    System::Call "${sysSetTimer} (r2, 1, r8,)"
  234.  
  235.    ; Free used memory
  236.    System::Free $R8
  237.    System::Free $R9
  238.  
  239.    StrCpy $R0 0
  240.    goto exit
  241.  
  242. m_Paint:
  243.    ; Create structures
  244.    System::Call "*${stRECT} (_) .R8"
  245.    System::Call "*${stPAINTSTRUCT} (_) .R9"
  246.  
  247.    ; Begin Paint
  248.    System::Call "${sysBeginPaint} (r2, R9) .R7"
  249.  
  250.    ; CreateCompatibleDC
  251.    System::Call "${sysCreateCompatibleDC} (R7) .R6"
  252.  
  253.    ; GetClientRect
  254.    System::Call "${sysGetClientRect} (r2, R8)"
  255.   
  256.    ; Select new bitmap
  257.    System::Call "${sysSelectObject} (R6, r6) .R5"
  258.  
  259.    ; Get left(R0), top(R1), right(R2), bottom(R3)
  260.    System::Call "*$R8${stRECT} (.R0,.R1,.R2,.R3)"
  261.      
  262.    ; width=right-left  
  263.    IntOp $R2 $R2 - $R0
  264.    ; height=bottom-top
  265.    IntOp $R3 $R3 - $R1
  266.  
  267.    System::Call "${sysBitBlt} (R7, R0, R1, R2, R3, R6, 0, 0, ${SRCCOPY})" 
  268.  
  269.    ; Select old bitmap
  270.    System::Call "${sysSelectObject} (R6, R5)"
  271.    
  272.    ; Delete compatible DC
  273.    System::Call "${sysDeleteDC} (R6)"
  274.  
  275.    ; End Paint
  276.    System::Call "${sysEndPaint} (r2, R9)"
  277.  
  278.    ; Free used memory
  279.    System::Free $R8
  280.    System::Free $R9
  281.  
  282.    StrCpy $R0 0
  283.    goto exit
  284.  
  285. m_Timer:
  286. m_Lbtn:
  287.    StrCpy $4 0
  288.    IntCmp $5 ${WM_TIMER} destroy
  289.         StrCpy $4 1
  290.  
  291. destroy:
  292.    System::Call "${sysDestroyWindow} (r2) .s"
  293.    !insertmacro SINGLE_CALLBACK 12 $R4 1 _systemSplashWndCB
  294.  
  295. default:
  296.    ; Default
  297.    System::Call "${sysDefWindowProc} (r2, r5, r7, r9) .s"
  298.    !insertmacro SINGLE_CALLBACK 14 $R0 1 _systemSplashWndCB
  299.    goto exit
  300.  
  301. m_Close:
  302.    StrCpy $R0 0
  303.    goto exit
  304.  
  305. exit:
  306.    ; Restore
  307.    System::Store "p4P0 l R0r4"
  308.  
  309.    ; Return from callback
  310.    System::Call "$3" $R0
  311. FunctionEnd
  312.  
  313. Function systemSplash
  314.  
  315.    ; Save registers and get input 
  316.    System::Store "s r8r9"
  317.  
  318.    ; Get module instance
  319.    System::Call "${sysGetModuleHandle} (i) .r7"
  320.  
  321.    ; Get arrow cursor
  322.    System::Call "${sysLoadCursor} (0, i ${IDC_ARROW}) .R9" 
  323.  
  324.    ; Get callback
  325.    System::Get "${sysWNDPROC}"
  326.    Pop $3
  327.  
  328.    ; Create window class
  329.    System::Call "*${stWNDCLASS} (0,r3,0,0,r7,0,R9,0,i 0,'_sp') .R9"
  330.  
  331.    ; Register window class
  332.    System::Call "${sysRegisterClass} (R9) .R9"
  333.    IntCmp $R9 0 errorexit ; Class registered ok?
  334.  
  335.    ; Load Image (LR_CREATEDIBSECTION|LR_LOADFROMFILE = 0x2010)
  336.    System::Call '${sysLoadImage} (, s, ${IMAGE_BITMAP}, 0, 0, ${LR_CREATEDIBSECTION}|${LR_LOADFROMFILE}) .r6' "$9.bmp"
  337.    IntCmp $6 0 errorexit        ; Image loaded ok?
  338.  
  339.    ; Start the sound (SND_ASYNC|SND_FILENAME|SND_NODEFAULT = 0x20003)
  340.    System::Call "${sysPlaySound} (s,,${SND_ASYNC}|${SND_FILENAME}|${SND_NODEFAULT})" "$9.wav" 
  341.  
  342.    ; Create window
  343.    System::Call "${sysCreateWindowEx} (${WS_EX_TOOLWINDOW}, s, s,,,,,, $HWNDPARENT,,r7,) .s" "_sp" "_sp" 
  344.    !insertmacro SINGLE_CALLBACK 1 $5 1 _systemSplashWndCB
  345.  
  346.    ; Create MSG struct
  347.    System::Call "*${stMSG} (_) i.R9"
  348.  
  349.    ; -------------------------
  350. repeat:
  351.         ; Check for window
  352.         System::Call "${sysIsWindow} (r5) .s"
  353.         !insertmacro SINGLE_CALLBACK 2 $R8 1 _systemSplashWndCB
  354.         IntCmp $R8 0 finish
  355.  
  356.         ; Get message
  357.         System::Call "${sysGetMessage} (R9, r5,_) .s"
  358.         !insertmacro SINGLE_CALLBACK 3 $R8 1 _systemSplashWndCB
  359.         IntCmp $R8 0 finish
  360.  
  361.         ; Dispatch message
  362.         System::Call "${sysDispatchMessage} (R9) .s"
  363.         !insertmacro SINGLE_CALLBACK 4 $R8 1 _systemSplashWndCB
  364.  
  365.         ; Repeat dispatch cycle
  366.         goto repeat
  367.    ; -------------------------
  368.  
  369. finish:
  370.    ; Stop the sound
  371.    System::Call "${sysPlaySound} (i 0, i 0, i 0)"
  372.  
  373.    ; Delete bitmap object
  374.    System::Call "${sysDeleteObject} (r6)"
  375.  
  376.    ; Delete the callback queue
  377.    System::Free $3
  378.  
  379.    ; Dialog return
  380.    StrCpy $R0 $4
  381.    goto exit
  382.  
  383. ; Exit in case of error
  384. errorexit:
  385.    StrCpy $R0 -1
  386.    goto exit
  387.  
  388. exit:
  389.    ; Restore register and put output
  390.    System::Store "P0 l"
  391. FunctionEnd
  392.  
  393. !verbose 4
  394.  
  395. !endif