SYSTRAY.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author:
  6. ** Created:
  7. **/
  8. #include "Main.h"
  9. #include <stdarg.h>
  10. #include "strutil.h"
  11. #define SYSTRAY_ICON 502
  12. #define SYSTRAY_WINDOW hMainWindow
  13. int systray_intray = 0;
  14. static BOOL systray_add(HWND hwnd, UINT uID, LPWSTR lpszTip);
  15. static BOOL systray_del(HWND hwnd, UINT uID);
  16. static BOOL systray_mod(HWND hwnd, UINT uID, LPWSTR lpszTip);
  17. static HICON hhIcon;
  18. static int hicon_index=-1;
  19. void systray_minimize(wchar_t *tip)
  20. {
  21. extern int geticonid(int x);
  22. int newindex=geticonid(config_sticon);
  23. if (!SYSTRAY_WINDOW) return;
  24. if (!hhIcon || hicon_index != newindex) {
  25. if (hhIcon) {
  26. DestroyIcon(hhIcon);
  27. }
  28. if(newindex == -666) {
  29. wchar_t tmp[MAX_PATH] = {0};
  30. StringCchPrintfW(tmp,MAX_PATH,L"%s\\winamp.ico",CONFIGDIR);
  31. if(!PathFileExistsW(tmp)) {
  32. hhIcon = (HICON)LoadImageW(hMainInstance,MAKEINTRESOURCEW(ICON_XP),IMAGE_ICON,16,16,LR_SHARED);
  33. }
  34. else {
  35. hhIcon = (HICON)LoadImageW(0,tmp,IMAGE_ICON,16,16,LR_LOADFROMFILE);
  36. }
  37. }
  38. else {
  39. hhIcon = (HICON)LoadImageW(hMainInstance,MAKEINTRESOURCEW(newindex),IMAGE_ICON,16,16,LR_SHARED);
  40. }
  41. }
  42. hicon_index=newindex;
  43. if (!systray_intray) {
  44. systray_add(SYSTRAY_WINDOW,SYSTRAY_ICON,tip);
  45. systray_intray = 1;
  46. }
  47. else {
  48. systray_mod(SYSTRAY_WINDOW,SYSTRAY_ICON,tip);
  49. }
  50. }
  51. void systray_restore(void)
  52. {
  53. if (systray_intray) {
  54. systray_del(SYSTRAY_WINDOW,SYSTRAY_ICON);
  55. systray_intray = 0;
  56. if (hhIcon) {
  57. DestroyIcon(hhIcon);
  58. hhIcon=NULL;
  59. hicon_index=-1;
  60. }
  61. }
  62. }
  63. static void mktipstr(wchar_t *out, wchar_t *in, size_t outlen)
  64. {
  65. wchar_t *nextOut;
  66. size_t outpos=0;
  67. while (outpos < outlen-1 && *in)
  68. {
  69. if (*in == L'&')
  70. {
  71. if ((outpos+=2) >= outlen-1) break;
  72. *out++=L'&';
  73. *out++=L'&';
  74. }
  75. CopyCharW(out, in);
  76. nextOut = CharNextW(out);
  77. in = CharNextW(in);
  78. outpos+=(nextOut-out);
  79. out=nextOut;
  80. }
  81. *out=0;
  82. }
  83. static BOOL systray_add(HWND hwnd, UINT uID, LPWSTR lpszTip)
  84. {
  85. NOTIFYICONDATAW tnid = {0};
  86. tnid.cbSize = sizeof(NOTIFYICONDATAW);
  87. tnid.hWnd = hwnd;
  88. tnid.uID = uID;
  89. tnid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
  90. tnid.uCallbackMessage = WM_WA_SYSTRAY;
  91. tnid.hIcon = hhIcon;
  92. mktipstr(tnid.szTip,lpszTip,sizeof(tnid.szTip)/sizeof(wchar_t));
  93. return Shell_NotifyIconW(NIM_ADD, &tnid);
  94. }
  95. static BOOL systray_del(HWND hwnd, UINT uID)
  96. {
  97. NOTIFYICONDATAW tnid = {0};
  98. tnid.cbSize = sizeof(NOTIFYICONDATAW);
  99. tnid.hWnd = hwnd;
  100. tnid.uID = uID;
  101. return(Shell_NotifyIconW(NIM_DELETE, &tnid));
  102. }
  103. static BOOL systray_mod(HWND hwnd, UINT uID, LPWSTR lpszTip)
  104. {
  105. NOTIFYICONDATAW tnid = {0};
  106. tnid.cbSize = sizeof(NOTIFYICONDATAW);
  107. tnid.hWnd = hwnd;
  108. tnid.uID = uID;
  109. tnid.uFlags = NIF_TIP|NIF_ICON;
  110. tnid.hIcon = hhIcon;
  111. mktipstr(tnid.szTip,lpszTip,sizeof(tnid.szTip)/sizeof(wchar_t));
  112. return (Shell_NotifyIconW(NIM_MODIFY, &tnid));
  113. }