home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / Makensisw / toolbar.cpp < prev    next >
C/C++ Source or Header  |  2004-01-02  |  9KB  |  229 lines

  1. /*
  2.   Copyright (c) 2003 Sunil Kamath
  3.   Modified by Joost Verburg
  4.  
  5.   This software is provided 'as-is', without any express or implied
  6.   warranty.  In no event will the authors be held liable for any damages
  7.   arising from the use of this software.
  8.  
  9.   Permission is granted to anyone to use this software for any purpose,
  10.   including commercial applications, and to alter it and redistribute it
  11.   freely, subject to the following restrictions:
  12.  
  13.   1. The origin of this software must not be misrepresented; you must not
  14.      claim that you wrote the original software. If you use this software
  15.      in a product, an acknowledgment in the product documentation would be
  16.      appreciated but is not required.
  17.   2. Altered source versions must be plainly marked as such, and must not be
  18.      misrepresented as being the original software.
  19.   3. This notice may not be removed or altered from any source distribution.
  20.  
  21. */
  22. #define TOOLBAR_CPP
  23.  
  24. #include <windows.h>
  25. #include "resource.h"
  26. #include "makensisw.h"
  27. #include "noclib.h"
  28. #include "toolbar.h"
  29.  
  30. NTOOLBAR g_toolbar;
  31. extern NSCRIPTDATA g_sdata;
  32. extern NTOOLTIP g_tip;
  33.  
  34. TBBUTTON CreateToolBarButton(int iBitmap, int idCommand, BYTE fsState, BYTE fsStyle, DWORD dwData, int iString)
  35. {
  36.   TBBUTTON tbButton;
  37.   tbButton.iBitmap = iBitmap;
  38.   tbButton.idCommand = idCommand;
  39.   tbButton.fsState = fsState;
  40.   tbButton.fsStyle = fsStyle;
  41.   tbButton.dwData = dwData;
  42.   tbButton.iString = iString;
  43.  
  44.   return tbButton;
  45. }
  46.  
  47. void CreateToolBar()
  48. {
  49.   static TBBUTTON tbButton[BUTTONCOUNT];
  50.   tbButton[TBB_LOADSCRIPT]  = CreateToolBarButton(IDB_LOADSCRIPT,    IDM_LOADSCRIPT, TBSTATE_ENABLED,        TBSTYLE_BUTTON,   0, 0);
  51.   tbButton[TBB_SAVE]        = CreateToolBarButton(IDB_SAVE,          IDM_SAVE,       TBSTATE_ENABLED,        TBSTYLE_BUTTON,   0, 0);
  52.   tbButton[TBB_SEP1]        = CreateToolBarButton(0,                 0,              TBSTATE_ENABLED,        TBSTYLE_SEP,      0, 0);
  53.   tbButton[TBB_COPY]        = CreateToolBarButton(IDB_COPY,          IDM_COPY,       TBSTATE_INDETERMINATE,  TBSTYLE_BUTTON,   0, 0);
  54.   tbButton[TBB_FIND]        = CreateToolBarButton(IDB_FIND,          IDM_FIND,       TBSTATE_ENABLED,        TBSTYLE_BUTTON,   0, 0);
  55.   tbButton[TBB_CLEARLOG]    = CreateToolBarButton(IDB_CLEARLOG,      IDM_CLEARLOG,   TBSTATE_ENABLED,        TBSTYLE_BUTTON,   0, 0);
  56.   tbButton[TBB_SEP2]        = CreateToolBarButton(0,                 0,              TBSTATE_ENABLED,        TBSTYLE_SEP,      0, 0);
  57.   tbButton[TBB_RECOMPILE]   = CreateToolBarButton(IDB_RECOMPILE,     IDM_RECOMPILE,  TBSTATE_INDETERMINATE,  TBSTYLE_BUTTON,   0, 0);
  58.   tbButton[TBB_COMPRESSOR]  = CreateToolBarButton(IDB_COMPRESSOR,    IDM_COMPRESSOR, TBSTATE_ENABLED,        TBSTYLE_DROPDOWN, 0, 0);
  59.   tbButton[TBB_TEST]        = CreateToolBarButton(IDB_TEST,          IDM_TEST,       TBSTATE_INDETERMINATE,  TBSTYLE_BUTTON,   0, 0);
  60.   tbButton[TBB_EDITSCRIPT]  = CreateToolBarButton(IDB_EDITSCRIPT,    IDM_EDITSCRIPT, TBSTATE_INDETERMINATE,  TBSTYLE_BUTTON,   0, 0);
  61.   tbButton[TBB_BROWSESCR]   = CreateToolBarButton(IDB_BROWSESCR,     IDM_BROWSESCR,  TBSTATE_INDETERMINATE,  TBSTYLE_BUTTON,   0, 0);
  62.   tbButton[TBB_SEP3]        = CreateToolBarButton(0,                 0,              TBSTATE_ENABLED,        TBSTYLE_SEP,      0, 0);
  63.   tbButton[TBB_NSISHOME]    = CreateToolBarButton(IDB_NSISHOME,      IDM_NSISHOME,   TBSTATE_ENABLED,        TBSTYLE_BUTTON,   0, 0);
  64.   tbButton[TBB_DOCS]        = CreateToolBarButton(IDB_DOCS,          IDM_DOCS,       TBSTATE_ENABLED,        TBSTYLE_BUTTON,   0, 0);
  65.  
  66.   g_toolbar.hwnd = CreateWindowEx (
  67.     0L,
  68.     TOOLBARCLASSNAME,
  69.     "",
  70.     WS_CHILD | WS_VISIBLE | TBSTYLE_TRANSPARENT | TBSTYLE_FLAT,
  71.     0, 0, 0, 30,
  72.     g_sdata.hwnd,
  73.     (HMENU) TOOLBAR_ID,
  74.     g_sdata.hInstance,
  75.     NULL );
  76.   
  77.   SendMessage(g_toolbar.hwnd, TB_BUTTONSTRUCTSIZE, sizeof(tbButton[0]), 0);
  78.   SendMessage(g_toolbar.hwnd, TB_ADDBUTTONS, BUTTONCOUNT, (LONG) &tbButton);
  79.  
  80.   // For Comctl32.dll version detection
  81.   HMODULE hMod = GetModuleHandle("comctl32.dll");
  82.  
  83.   if (GetProcAddress(hMod, "InitCommonControlsEx")) { // Version 4.70
  84.     // Modern toolbar, 24-bit bitmaps
  85.  
  86.     g_toolbar.imagelist = ImageList_LoadImage(g_sdata.hInstance, MAKEINTRESOURCE(IDB_TOOLBAR24), 16, 0, RGB(255, 0, 255), IMAGE_BITMAP, LR_CREATEDIBSECTION);
  87.     g_toolbar.imagelistd = ImageList_LoadImage(g_sdata.hInstance, MAKEINTRESOURCE(IDB_TOOLBAR24D), 16, 0, RGB(255, 0, 255), IMAGE_BITMAP, LR_CREATEDIBSECTION);
  88.     g_toolbar.imagelisth = ImageList_LoadImage(g_sdata.hInstance, MAKEINTRESOURCE(IDB_TOOLBAR24H),  16, 0, RGB(255, 0, 255), IMAGE_BITMAP, LR_CREATEDIBSECTION);
  89.  
  90.     SendMessage(g_toolbar.hwnd, TB_SETIMAGELIST, 0, (LPARAM) g_toolbar.imagelist);
  91.     SendMessage(g_toolbar.hwnd, TB_SETDISABLEDIMAGELIST, 0, (LPARAM) g_toolbar.imagelistd);
  92.     SendMessage(g_toolbar.hwnd, TB_SETHOTIMAGELIST, 0, (LPARAM) g_toolbar.imagelisth);
  93.  
  94.     // Version 4.71
  95.     if (GetProcAddress(hMod, "DllGetVersion")) {
  96.         SendMessage(g_toolbar.hwnd, TB_SETEXTENDEDSTYLE, 0, (LPARAM) (DWORD) TBSTYLE_EX_DRAWDDARROWS);
  97.     }
  98.       
  99.    }
  100.    else
  101.    {
  102.     // Old Windows 95 toolbar, 256 color bitmap with system palette
  103.  
  104.     TBADDBITMAP tbBitmap;
  105.  
  106.     tbBitmap.hInst = g_sdata.hInstance;
  107.     tbBitmap.nID = IDB_TOOLBAR;
  108.   
  109.     SendMessage(g_toolbar.hwnd, TB_ADDBITMAP, IMAGECOUNT, (LONG) &tbBitmap);
  110.   }
  111.  
  112.   FreeLibrary(hMod);
  113.  
  114.   HMENU toolmenu = GetSubMenu(g_sdata.menu, SCRIPT_MENU_INDEX);
  115.   g_toolbar.dropdownmenu = GetSubMenu(toolmenu, COMPRESSOR_MENU_INDEX);
  116.   RECT rect;
  117.   SendMessage(g_toolbar.hwnd, TB_GETITEMRECT, TBB_COMPRESSOR, (LPARAM) (LPRECT) &rect);
  118.   g_toolbar.dropdownpoint.x = rect.left;
  119.   g_toolbar.dropdownpoint.y = rect.bottom+1;
  120. }
  121.  
  122. void UpdateToolBarCompressorButton()
  123. {
  124.   int iBitmap;
  125.   int iString;
  126.   char   szBuffer[64];
  127.   char   temp[32];
  128.   TOOLINFO ti;
  129.  
  130.   my_memset(&ti, 0, sizeof(TOOLINFO));
  131.  
  132.   if(g_sdata.compressor >= COMPRESSOR_SCRIPT && g_sdata.compressor <= COMPRESSOR_BEST) {
  133.     iBitmap = compressor_bitmaps[(int)g_sdata.compressor];
  134.     iString = compressor_strings[(int)g_sdata.compressor];
  135.   }
  136.   else {
  137.     return;
  138.   }
  139.   LoadString(g_sdata.hInstance,
  140.              IDS_COMPRESSOR,
  141.              temp,
  142.              sizeof(temp));
  143.   my_memset(szBuffer, 0, sizeof(szBuffer));
  144.   lstrcat(szBuffer,temp);
  145.   lstrcat(szBuffer," (");
  146.   LoadString(g_sdata.hInstance,
  147.              iString,
  148.              temp,
  149.              sizeof(temp));
  150.   lstrcat(szBuffer,temp);
  151.   lstrcat(szBuffer,")");
  152.  
  153.   SendMessage(g_toolbar.hwnd, TB_CHANGEBITMAP, (WPARAM) IDM_COMPRESSOR, (LPARAM) MAKELPARAM(iBitmap, 0));
  154.  
  155.   ti.cbSize = sizeof(TOOLINFO);
  156.   ti.uFlags = 0;
  157.   ti.hinst = g_sdata.hInstance;
  158.   ti.hwnd = g_toolbar.hwnd;
  159.   ti.uId = (UINT)TBB_COMPRESSOR;
  160.   SendMessage(g_tip.tip, TTM_GETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &ti);
  161.   ti.lpszText = (LPSTR)szBuffer;
  162.   SendMessage(g_tip.tip, TTM_SETTOOLINFO, 0, (LPARAM) (LPTOOLINFO) &ti);
  163. }
  164.  
  165. void AddToolBarButtonTooltip(int id, int iString)
  166. {
  167.   TOOLINFO ti;
  168.   char   szBuffer[64];
  169.   RECT rect;
  170.  
  171.   my_memset(&ti, 0, sizeof(TOOLINFO));
  172.  
  173.   SendMessage(g_toolbar.hwnd, TB_GETITEMRECT, id, (LPARAM) (LPRECT) &rect);
  174.  
  175.   ti.cbSize = sizeof(TOOLINFO);
  176.   ti.uFlags = 0;
  177.   ti.hwnd = g_toolbar.hwnd;
  178.   ti.hinst = g_sdata.hInstance;
  179.   ti.uId = (UINT)id;
  180.  
  181.   LoadString(g_sdata.hInstance,
  182.              iString,
  183.              szBuffer,
  184.              sizeof(szBuffer));
  185.   ti.lpszText = (LPSTR) szBuffer;
  186.   ti.rect.left =rect.left;
  187.   ti.rect.top = rect.top;
  188.   ti.rect.right = rect.right;
  189.   ti.rect.bottom = rect.bottom;
  190.  
  191.   SendMessage(g_tip.tip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
  192. }
  193.  
  194. void AddToolBarTooltips()
  195. {
  196.   AddToolBarButtonTooltip(TBB_LOADSCRIPT, IDS_LOADSCRIPT);
  197.   AddToolBarButtonTooltip(TBB_SAVE, IDS_SAVE);
  198.   AddToolBarButtonTooltip(TBB_COPY, IDS_COPY);
  199.   AddToolBarButtonTooltip(TBB_FIND, IDS_FIND);
  200.   AddToolBarButtonTooltip(TBB_RECOMPILE, IDS_RECOMPILE);
  201.   AddToolBarButtonTooltip(TBB_COMPRESSOR, IDS_COMPRESSOR);
  202.   AddToolBarButtonTooltip(TBB_TEST, IDS_TEST);
  203.   AddToolBarButtonTooltip(TBB_EDITSCRIPT, IDS_EDITSCRIPT);
  204.   AddToolBarButtonTooltip(TBB_BROWSESCR, IDS_BROWSESCR);
  205.   AddToolBarButtonTooltip(TBB_CLEARLOG, IDS_CLEARLOG);
  206.   AddToolBarButtonTooltip(TBB_NSISHOME, IDS_NSISHOME);
  207.   AddToolBarButtonTooltip(TBB_DOCS, IDS_DOCS);
  208. }
  209.  
  210. void EnableToolBarButton(int id, BOOL enabled)
  211. {
  212.   UINT state = (enabled?TBSTATE_ENABLED:TBSTATE_INDETERMINATE);
  213.  
  214.   SendMessage(g_toolbar.hwnd, TB_SETSTATE, id, MAKELPARAM(state, 0));
  215. }
  216.  
  217. void ShowToolbarDropdownMenu()
  218. {
  219.   RECT rect;
  220.   GetWindowRect(g_toolbar.hwnd, (LPRECT) &rect);
  221.   TrackPopupMenu(g_toolbar.dropdownmenu,
  222.                 NULL,
  223.                 rect.left + (int)(short)g_toolbar.dropdownpoint.x,
  224.                 rect.top + (int)(short)g_toolbar.dropdownpoint.y,
  225.                 0,
  226.                 g_sdata.hwnd,
  227.                 0);
  228. }
  229.