Code Viewer

./Classes/__listview.js File Size: 54.01 KB

  1. /*
  2. * -----
  3. * Screenshot Sender - __listview.js
  4. * -----
  5. * Class for working with list-view controls.
  6. * -----
  7. */
  8.  
  9. //List-view control messages
  10. var LVM_FIRST = 0x1000;
  11. var LVM_DELETEITEM = (LVM_FIRST + 8);
  12. var LVM_DELETEALLITEMS = (LVM_FIRST + 9);
  13. var LVM_ENSUREVISIBLE = (LVM_FIRST + 19);
  14. var LVM_FINDITEMW = (LVM_FIRST + 83);
  15. var LVM_GETHEADER = (LVM_FIRST + 31);
  16. var LVM_GETIMAGELIST = (LVM_FIRST + 2);
  17. var LVM_GETITEMCOUNT = (LVM_FIRST + 4);
  18. var LVM_GETITEMTEXTW = (LVM_FIRST + 115);
  19. var LVM_GETITEMW = (LVM_FIRST + 75);
  20. var LVM_INSERTITEMW = (LVM_FIRST + 77);
  21. var LVM_SETCOLUMNW = (LVM_FIRST + 96);
  22. var LVM_SETIMAGELIST = (LVM_FIRST + 3);
  23. var LVM_SETITEMW = (LVM_FIRST + 76);
  24. var LVM_SORTITEMS = (LVM_FIRST + 48);
  25. //LVITEM masks
  26. var LVIF_IMAGE = 0x2;
  27. var LVIF_PARAM = 0x4;
  28. var LVIF_STATE = 0x8;
  29. var LVIF_TEXT = 0x1;
  30. //LVCOLUMN masks
  31. var LVCF_FMT = 0x1;
  32. var LVCF_IMAGE = 0x10;
  33. var LVCF_ORDER = 0x20;
  34. var LVCF_SUBITEM = 0x8;
  35. var LVCF_TEXT = 0x4;
  36. var LVCF_WIDTH = 0x2;
  37. //LVCOLUMN fmt
  38. var LVCFMT_IMAGE = 0x800;
  39. //LVFINDINFO flags
  40. var LVFI_NEARESTXY = 0x40;
  41. var LVFI_PARAM = 0x1;
  42. var LVFI_PARTIAL = 0x8;
  43. var LVFI_STRING = 0x2;
  44. var LVFI_WRAP = 0x20;
  45. //LVFINDINFO vkDirection values
  46. var VK_LEFT = 0x25;
  47. var VK_RIGHT = 0x27;
  48. var VK_UP = 0x26;
  49. var VK_DOWN = 0x28;
  50. var VK_HOME = 0x24;
  51. var VK_END = 0x23;
  52. var VK_PRIOR = 0x21;
  53. var VK_NEXT = 0x22;
  54. //Header control messages
  55. var HDM_FIRST = 0x1200;
  56. var HDM_GETITEMW = (HDM_FIRST + 11);
  57. var HDM_SETITEMW = (HDM_FIRST + 12);
  58. //HDITEM mask
  59. var HDI_FORMAT = 0x4;
  60. //HDITEM fmt
  61. var HDF_SORTDOWN = 0x200;
  62. var HDF_SORTUP = 0x400;
  63. //Image list types
  64. var LVSIL_NORMAL = 0;
  65. var LVSIL_SMALL = 1;
  66.  
  67. //Global object to store the ListViews in, identified by handle.
  68. ListViews = {};
  69.  
  70. /**
  71. *      LISTVIEW CLASS
  72. *      by Mattias Buelens
  73. *      Version: 1.0.001(30 Mar 2008)
  74. *
  75. *      Description:  This class contains several functions for Plus! Live script developers who use ListView controls in their windows,
  76. *                            but can't accomplish everything they need by only using the methods from Plus! Live. This class contains functions
  77. *                              to do list-view sorting, add images to the list-view control and use them for list items, modify list-view columns
  78. *                              and more.
  79. *      Installation: Copy this script file(ListView.js) to your script's directory.
  80. *      Usage:                        Create a new instance of this class with "new ListView(PlusWnd, CtrlId) " where PlusWnd is a Plus! Live window and
  81. *                              CtrlId is the ID of the list-view control, or with "new ListView(hWnd) " where hWnd is the list-view control handle.
  82. *                              Then, you can use any child method of this class in your script.
  83. *      Copyright:    You can use this class in any of your scripts, both private and public scripts, modify this class in any way and
  84. *                              release modifications of this class in any way, as long as you provide some credit to the original creator(Mattias
  85. *                              Buelens) to show your appreciation, you don't even need to keep this whole header and function descriptions.
  86. *
  87. *      Happy scripting!;-)
  88. */
  89. var ListView = function (PlusWnd, CtrlId) {
  90.         this.Helpers.AssignParent(this);
  91.         this.SortOptions.Callback = this.SortOptions.Callback_Default;
  92.         //Check the given parameters
  93.         if (this.Helpers.IsWindow(PlusWnd) && typeof CtrlId === "string") {
  94.                 //PlusWnd and CtrlId are given
  95.                 this.Handle = PlusWnd.GetControlHandle(CtrlId);
  96.         } else if (typeof PlusWnd === "number") {
  97.                 //hWnd is given
  98.                 this.Handle = PlusWnd;
  99.         }
  100.         //Save this class in a global array, for outside access(e.g. list-view sorting)
  101.         ListViews[this.Handle] = this;
  102.  
  103.         //Load ImageList
  104.         this.hImageList = this.Helpers.SendMessage(this.Handle, LVM_GETIMAGELIST, LVSIL_NORMAL, 0);
  105.        
  106.         //Create ImageList if it doesn't exist
  107.         if (this.hImageList === 0) this.hImageList = Interop.Call("comctl32", "ImageList_Create", 100, 100, 0x20 /* ILC_COLOR32 */, 0, 10);
  108.        
  109. }
  110.  
  111. ListView.prototype = {
  112.         /**
  113.          *      Handle(number)
  114.          *
  115.          *      Desc: Contains the handle of the list-view control after class creation
  116.          */
  117.         "Handle" : 0,
  118.         /**
  119.          *      SortOptions(object)
  120.          *
  121.          *      Desc: Specifies the current options for the sorting
  122.          */
  123.         "SortOptions" : {
  124.                 "Column" : -1,
  125.                 "Ascending" : true,
  126.                 "Flags" : 0,
  127.                 "Callback_Default" : function (sText1, sText2, bAscending) {
  128.                         sText1 = String(sText1).toLowerCase();
  129.                         sText2 = String(sText2).toLowerCase();
  130.                         var Result = (sText1 == sText2) ? 0 : (sText1 < sText2) ? -1 : 1;
  131.                         if (!bAscending) Result *= -1;
  132.                         return Result;
  133.                 },
  134.                 "Callback" : null
  135.         },
  136.         /**
  137.          *      InsertItem
  138.          *
  139.          *      Desc: Inserts an item to the list with the specified options.
  140.          *      Params:       ItemText       Text to be inserted in the first column of the list for the item.
  141.          *                        ImageIdx      Index of the image in the small image list to use for this item.
  142.          *                        ItemIdx       Optional. The index at which the item should be inserted. Defaults to the end of the list.
  143.          *      Return value: The index of the new item when successful, otherwise -1.
  144.          */
  145.         "InsertItem" : function (ItemText, ImageIdx, ItemIdx) {
  146.                 //Make sure the parameters are alright
  147.                 ItemText = (typeof ItemText === "string") ? ItemText : "";
  148.                 ImageIdx = (typeof ImageIdx === "number") ? ImageIdx : false;
  149.                 ItemIdx = (typeof ItemIdx === "number") ? ItemIdx : this.GetItemCount();
  150.                 lParam = ItemIdx; //Set the lParam to be the item index, used for sorting and identifying the item
  151.  
  152.                 //Do it!!!
  153.                 var LVITEM = this.Helpers.LVITEM(0, ItemIdx, ItemText, ImageIdx, lParam);
  154.                 var Result = this.Helpers.SendMessage(LVM_INSERTITEMW, 0, LVITEM.DataPtr);
  155.  
  156.                 //Clean up
  157.                 LVITEM.Size = 0;
  158.  
  159.                 return Result;
  160.         },
  161.         /**
  162.          *      SetSubItem
  163.          *
  164.          *      Desc: Sets the subitem for an item in the list.
  165.          *      Params:       ItemIdx                Index of the item to set the subitem of.
  166.          *                        SubItemIdx    Subitem index to set the subitem. If the column wasn't modified, this is the same as the column index.
  167.          *                        SubItemText   Text to be set for the subitem.
  168.          *      Return value: True indicates success, false otherwise.
  169.          */
  170.         "SetSubItem" : function (ItemIdx, SubItemIdx, SubItemText) {
  171.                 //Make sure the parameters are alright
  172.                 ItemIdx = (typeof ItemIdx === "number") ? ItemIdx : 0;
  173.                 SubItemIdx = (typeof SubItemIdx === "number") ? SubItemIdx : 0;
  174.                 SubItemText = (typeof SubItemText === "string") ? SubItemText : false;
  175.  
  176.                 //Do it!!!
  177.                 var LVITEM = this.Helpers.LVITEM(0, ItemIdx, SubItemText, false, false, SubItemIdx);
  178.                 var Result = this.Helpers.SendMessage(LVM_SETITEMW, 0, LVITEM.DataPtr);
  179.  
  180.                 //Clean up
  181.                 LVITEM.Size = 0;
  182.  
  183.                 return Result;
  184.         },
  185.         /**
  186.          *      SetColumn
  187.          *
  188.          *      Desc: Sets various options, such as text and image, for a column in the list.
  189.          *      Params:       ColumnIdx            Index of the column to modify.
  190.          *                        ColumnText    Text to set as column header. Set to false or leave blank if you don't want to modify this.
  191.          *                        ImageIdx            Index of the image in the small image list to use for this column.
  192.          *                                                Set to false or leave blank if you don't want to modify this.
  193.          *                        SubItemIdx    Index of the subitem to assign the column with. Set to false or leave blank if you don't want to modify this.
  194.          *      Return value: True indicates success, false otherwise.
  195.          */
  196.         "SetColumn" : function (ColumnIdx, ColumnText, ImageIdx, SubItemIdx) {
  197.                 //Make sure the parameters are alright
  198.                 ColumnIdx = (typeof ColumnIdx === "number") ? ColumnIdx : 0;
  199.                 ColumnText = (typeof ColumnText === "string") ? ColumnText : false;
  200.                 ImageIdx = (typeof ImageIdx === "number") ? ImageIdx : false;
  201.                 SubItemIdx = (typeof SubItemIdx === "number") ? SubItemIdx : false;
  202.  
  203.                 //Do it!!!
  204.                 var LVCOLUMN = this.Helpers.LVCOLUMN(0, ColumnText, SubItemIdx, ImageIdx);
  205.                 var Result = this.Helpers.SendMessage(LVM_SETCOLUMNW, ColumnIdx, LVCOLUMN);
  206.  
  207.                 //Clean up
  208.                 LVCOLUMN.Size = 0;
  209.  
  210.                 return Result;
  211.         },
  212.         /**
  213.          *      SortList
  214.          *
  215.          *      Desc: Sorts the list using a callback function. For more information, go to
  216.          *                  http://msdn2.microsoft.com/en-us/library/bb761227(VS.85).aspx
  217.          *      Params:       ColumnIdx            Index of the column to sort. If this is a negative number, the current sorted column is used.
  218.          *                        Callback            Callback function to determine the sorting order.
  219.          *                                                This function receives the text of two subitems in it first two parameters and a boolean as third parameter
  220.          *                                                whether the sorting should be done ascending or descending. The function should return a negative value if the
  221.          *                                                first item should precede the second, a positive value if the first item should follow the second, or zero if
  222.          *                                                the items are equivalent. This function will also be bound to the ListView class instance it was called on, that
  223.          *                                                means that any "this" references will refer to this ListView object, thus it can read and change properties and
  224.          *                                                call functions from the ListView class. Defaults to the last callback function used or, if this is the first time
  225.          *                                                this function was called, a default callback function.
  226.          *                        Ascending          Boolean specifying whether the new sorting should be ascending (true) or descending (false).
  227.          *                                                If this is a negative number, the default behaviour for sorting is followed, i.e. the sorting order will be
  228.          *                                                ascending when a new column is sorted, else the order is reversed.
  229.          *                        Flags   Extra flags. Defaults to the last used flag. This can be (only) one of the following values:
  230.          *                                                0x0 - Default value. Pass the subitem text to the callback function as first and second parameters.
  231.          *                                                0x1 - Pass the original index of the item to the callback function, i.e. the index the item had when
  232.          *                                                      it was created.
  233.          *                                                0x2 - Pass the current index of the item to the callback function, i.e. the index the item has with the current
  234.          *                                                      sorting and other changes made.
  235.          *      Return value: True indicates success, false otherwise.
  236.          */
  237.         "SortList" : function (ColumnIdx, CallbackFunc, Ascending, Flags) {
  238.                 //Make sure the parameters are alright
  239.                 ColumnIdx = (typeof ColumnIdx === "number") ? ( (ColumnIdx < 0) ? ( (this.SortOptions.Column >= 0) ? this.SortOptions.Column : 0) : ColumnIdx) : 0;
  240.                 CallbackFunc = (typeof CallbackFunc === "function") ? CallbackFunc : this.SortOptions.Callback_Default;
  241.                 Flags = (typeof Flags === "undefined") ? this.SortOptions.Flags : 1*Flags;
  242.  
  243.                 //Set the sort order
  244.                 if (typeof Ascending === "number" && Ascending < 0) {
  245.                         //Unset the sort header
  246.                         this.SetColumnSort(this.SortOptions.Column, false);
  247.                 } else if (typeof Ascending !== "undefined") {
  248.                         //Set the sort order to the given order
  249.                         this.SortOptions.Ascending = (Ascending == true);
  250.                         //Unset the sort header
  251.                         this.SetColumnSort(this.SortOptions.Column, false);
  252.                 } else {
  253.                         if (this.SortOptions.Column === ColumnIdx) {
  254.                                 //Inverse the order
  255.                                 this.SortOptions.Ascending = !this.SortOptions.Ascending;
  256.                         } else {
  257.                                 //Start ascending
  258.                                 this.SortOptions.Ascending = true;
  259.                                 //Unset the sort header
  260.                                 this.SetColumnSort(this.SortOptions.Column, false);
  261.                         }
  262.                 }
  263.  
  264.                 //Set the sort options
  265.                 this.SortOptions.Column = ColumnIdx;
  266.                 this.SortOptions.Flags = Flags;
  267.                 this.SortOptions.Callback = this.Helpers.BindFunction(CallbackFunc, this);
  268.  
  269.                 //Get a function pointer - This will fail on Plus! versions who don't have the Interop.GetCallbackPtr function.
  270.                 try {
  271.                         var FuncPtr = Interop.GetCallbackPtr("ListViewCallback_Sort");
  272.                 } catch (e) {
  273.                         return false;
  274.                 }
  275.                 //Sort the list
  276.                 var Result = this.Helpers.SendMessage(LVM_SORTITEMS, this.Handle, FuncPtr);
  277.                 //Set the sort header
  278.                 this.SetColumnSort(ColumnIdx, true, this.SortOptions.Ascending);
  279.  
  280.                 return Result;
  281.         },
  282.         /**
  283.          *      SetColumnSort
  284.          *
  285.          *      Desc: Sets the column header to reflect the current sorting state.
  286.          *      Params:       ColumnIdx            Index of the column to modify.
  287.          *                        Sort      Boolean defining if this column should reflect a sorted state.
  288.          *                        SortDown            Boolean specifying whether this column should reflect an ascending (true) or descending (false) sort state.
  289.          *      Return value: True indicates success, false otherwise.
  290.          */
  291.         "SetColumnSort" : function (ColumnIdx, Sort, SortDown) {
  292.                 //Make sure the parameters are alright
  293.                 ColumnIdx = (typeof ColumnIdx === "number") ? ColumnIdx : 0;
  294.                 Sort = (typeof Sort === "undefined") ? false : (Sort == true);
  295.                 SortDown = (typeof SortDown === "undefined") ? false : (SortDown == true);
  296.  
  297.                 //Get the header control
  298.                 hHeader = this.Helpers.SendMessage(LVM_GETHEADER);
  299.                 //Create a HDITEM structure
  300.                 var HDITEM = Interop.Allocate(24);
  301.                 HDITEM.WriteDWORD(0, HDI_FORMAT);
  302.                 //Get the HDITEM information
  303.                 Interop.Call("user32", "SendMessageW", hHeader, HDM_GETITEMW, ColumnIdx, HDITEM.DataPtr);
  304.                 //Unset the sort down and sort up flags
  305.                 var fmt = HDITEM.ReadDWORD(20) & ~(HDF_SORTDOWN | HDF_SORTUP);
  306.                 //If the column should be set, set the correct flag.
  307.                 HDITEM.WriteDWORD(20, fmt | (Sort ? (SortDown ? HDF_SORTDOWN : HDF_SORTUP) : 0) );
  308.                 //Set the HDITEM
  309.                 var Result = Interop.Call("user32", "SendMessageW", hHeader, HDM_SETITEMW, ColumnIdx, HDITEM.DataPtr);
  310.  
  311.                 //Clean up
  312.                 HDITEM.Size = 0;
  313.  
  314.                 return Result;
  315.         },
  316.         /**
  317.          *      GetItemText
  318.          *
  319.          *      Desc: Gets the text from an item or subitem.
  320.          *      Params:       ItemIdx                Index of the item.
  321.          *                        SubItemIdx    Index of the subitem.
  322.          *      Return value: The text assigned with the item or subitem.
  323.          */
  324.         "GetItemText" : function (ItemIdx, SubItemIdx) {
  325.                 //Make sure the parameters are alright
  326.                 ItemIdx = (typeof ItemIdx === "number") ? ItemIdx : 0;
  327.                 SubItemIdx = (typeof SubItemIdx === "number") ? SubItemIdx : 0;
  328.  
  329.                 //Get an LVITEM structure
  330.                 var LVITEM = this.Helpers.LVITEM(0, false, false, false, false, SubItemIdx);
  331.                 //Create a text buffer
  332.                 var nTextLength = 1024;
  333.                 var pszText = Interop.Allocate(nTextLength);
  334.                 LVITEM.WriteDWORD(20, pszText.DataPtr); //pszText
  335.                 LVITEM.WriteDWORD(24, nTextLength); //cchTextMax
  336.                 LVITEM.WriteDWORD(0, LVITEM.ReadDWORD(0) | LVIF_TEXT); //mask
  337.  
  338.                 //Do it!!!
  339.                 var Result = this.Helpers.SendMessage(LVM_GETITEMTEXTW, ItemIdx, LVITEM.DataPtr);
  340.                 var sItemText = pszText.ReadString(0, true);
  341.  
  342.                 //Clean up
  343.                 LVITEM.Size = 0;
  344.                 pszText.Size = 0;
  345.  
  346.                 return sItemText;
  347.         },
  348.         /**
  349.          *      GetItemData
  350.          *
  351.          *      Desc: Gets the item data from an item.
  352.          *      Params:       ItemIdx                Index of the item.
  353.          *      Return value: The item data assigned with the item, as a number.
  354.          */
  355.         "GetItemData" : function (ItemIdx) {
  356.                 //Make sure the parameters are alright
  357.                 ItemIdx = (typeof ItemIdx === "number") ? ItemIdx : 0;
  358.  
  359.                 //Get an LVITEM structure
  360.                 var LVITEM = this.Helpers.LVITEM(0, ItemIdx);
  361.                 //Prepare the structure
  362.                 LVITEM.WriteDWORD(0, LVITEM.ReadDWORD(0) | LVIF_PARAM); //mask
  363.  
  364.                 //Do it!!!
  365.                 var Result = this.Helpers.SendMessage(LVM_GETITEMW, 0, LVITEM.DataPtr);
  366.                 var lParam = LVITEM.ReadDWORD(32); //lParam
  367.  
  368.                 //Clean up
  369.                 LVITEM.Size = 0;
  370.  
  371.                 return lParam;
  372.         },
  373.         /**
  374.          *      EnsureVisible
  375.          *
  376.          *      Desc: Ensures that an item is either entirely or partially visible, scrolling the list-view control if necessary.
  377.          *      Params:       ItemIdx                The index of the item which has to be made visible.
  378.          *                        PartialOK          Boolean specifying if the item must be entirely visible. False means that it must be entirely visible,
  379.          *                                                true means that the item can be only partially visible. Default is false (entirely visible).
  380.          *      Return value: True indicates success, false otherwise.
  381.          */
  382.         "EnsureVisible" : function (ItemIdx, PartialOK) {
  383.                 //Make sure the parameters are alright
  384.                 ItemIdx = (typeof ItemIdx === "number") ? ItemIdx : 0;
  385.                 PartialOK = (typeof PartialOK === "undefined") ? false : (PartialOK == true);
  386.  
  387.                 //Do it!!!
  388.                 return this.Helpers.SendMessage(LVM_ENSUREVISIBLE, ItemIdx, PartialOK);
  389.         },
  390.         /**
  391.          *      FindItem
  392.          *
  393.          *      Desc: Searches the list for the first item which matches the given options.
  394.          *      Params:       Options        Associative array or object specifying the options used to perform the search.
  395.          *                                          The following keys are supported:
  396.          *                                                - "wrap"                  Boolean specifying whether to continue the search at the beginning if
  397.          *                                                                              no match is found. Default is false.
  398.          *                                                - "text"                  Searches the list for an item which item text matches this string.
  399.          *                                                - "text-partial"      Boolean specifying whether the text is partial. If this is set to true,
  400.          *                                                                              an item beginning with "text" will match.
  401.          *                                                - "lparam"        Searches the list for an item which lParam matches this number.
  402.          *                                                - "point"               Searches the list for an item which is the nearest to this point.
  403.          *                                                                              Can be a POINT memory structure or an [X,Y] array.
  404.          *                                                - "point-dir"  Specifies the direction to look for an item from the point coordinates.
  405.          *                                                                              Can be one of the following value:
  406.          *                                                                              - VK_LEFT
  407.          *                                                                              - VK_RIGHT
  408.          *                                                                       - VK_UP
  409.          *                                                                              - VK_DOWN
  410.          *                                                                              - VK_HOME
  411.          *                                                                              - VK_END
  412.          *                                                                              - VK_PRIOR
  413.          *                                                                              - VK_NEXT
  414.          *                                          Examples of this parameter:
  415.          *                                                { text: "Romeo and ", text-partial: true }
  416.          *                                                      Will match an item like "Romeo and Juliette"
  417.          *                                                { point:[50, 20], point-dir: VK_UP }
  418.          *                                                      Will match the item closest to the point (50, 20), searching upwards.
  419.          *                        StartIdx      The index to start searching from. Set this to false to start from the beginning.
  420.          *      Return value: The index of the found item when successful, otherwise -1.
  421.          */
  422.         "FindItem" : function (Options, StartIdx) {
  423.                 //Make sure the parameters are alright
  424.                 if (typeof Options !== "object") return -1;
  425.                 StartIdx = (typeof StartIdx === "number") ? StartIdx : -1;
  426.  
  427.                 //Create an LVFINDINFO structure
  428.                 var LVFINDINFO = Interop.Allocate(24);
  429.                 var Flags = 0;
  430.                 if (typeof Options["text"] === "string") {
  431.                         //psz
  432.                         Flags |= LVFI_STRING;
  433.                         if (typeof Options["text-partial"] !== "undefined" && Partial) {
  434.                                 Flags |= LVFI_PARTIAL;
  435.                         }
  436.                         var psz = Interop.Allocate( (Options["text"].length+1) *2);
  437.                         psz.WriteString(0, Options["text"]);
  438.                         LVFINDINFO.WriteDWORD(4, psz.DataPtr);
  439.                 }
  440.                 if (typeof Options["lparam"] === "number") {
  441.                         //lParam
  442.                         Flags |= LVFI_PARAM;
  443.                         LVFINDINFO.WriteDWORD(8, Options["lparam"]);
  444.                 }
  445.                 if (typeof Options["point"] === "object") {
  446.                         //pt and vkDirection
  447.                         Flags |= LVFI_NEARESTXY;
  448.                         Direction = (typeof Options["point-dir"] === "undefined") ? VK_DOWN : 1 * Options["point-dir"];
  449.                         if (typeof Options["point"].Size === "number") {
  450.                                 LVFINDINFO.WriteDWORD(12, Options["point"].ReadDWORD(0));
  451.                                 LVFINDINFO.WriteDWORD(16, Options["point"].ReadDWORD(4));
  452.                         } else if (typeof Options["point"].length === "number") {
  453.                                 LVFINDINFO.WriteDWORD(12, Options["point"][0]);
  454.                                 LVFINDINFO.WriteDWORD(16, Options["point"][1]);
  455.                         }
  456.                 }
  457.                 if (typeof Options["wrap"] !== "undefined") {
  458.                         //flags: wrap
  459.                         Flags |= (Option["wrap"]) ? LVFI_WRAP : 0;
  460.                 }
  461.                 //Flags
  462.                 LVFINDINFO.WriteDWORD(0, Flags);
  463.  
  464.                 //Do it!!!
  465.                 var ItemIdx = this.Helpers.SendMessage(LVM_FINDITEMW, StartIdx, LVFINDINFO.DataPtr);
  466.  
  467.                 //Clean up
  468.                 LVFINDINFO.Size = 0;
  469.                 if (typeof psz === "object") psz.Size = 0;
  470.  
  471.                 return ItemIdx;
  472.         },
  473.         /**
  474.          *      GetItemCount
  475.          *
  476.          *      Desc: Counts the items in the list.
  477.          *      Params: none
  478.          *      Return value: The items count.
  479.          */
  480.         "GetItemCount" : function () {
  481.                 return this.Helpers.SendMessage(LVM_GETITEMCOUNT);
  482.         },
  483.         /**
  484.          *      DeleteItem
  485.          *
  486.          *      Desc: Removes an item from the list.
  487.          *      Params:       ItemIdx                The index of the item to remove.
  488.          *      Return value: True indicates success, false otherwise.
  489.          */
  490.         "DeleteItem" : function (ItemIdx) {
  491.                 //Make sure the parameters are alright
  492.                 ItemIdx = (typeof ItemIdx === "number") ? ItemIdx : false;
  493.                 if (ItemIdx == false) return false;
  494.  
  495.                 //Do it!!!
  496.                 return this.Helpers.SendMessage(LVM_DELETEITEM, ItemIdx, 0);
  497.         },
  498.         /**
  499.          *      DeleteAll
  500.          *
  501.          *      Desc: Removes all items from the list.
  502.          *      Params: none
  503.          *      Return value: True indicates success, false otherwise.
  504.          */
  505.         "DeleteAll" : function () {
  506.                 return this.Helpers.SendMessage(LVM_DELETEALLITEMS);
  507.         },
  508.         /**
  509.          *      AddImageToList
  510.          *
  511.          *      Desc: Adds an image to the image list of the list-view control.
  512.          *      Params:       Image    Valid GDI image handle for the image to add.
  513.          *                        IsSmall       Boolean specifying whether to add the image to the large (false) or small (true) image list.
  514.          *      Return value: The index of the new image in the image list.
  515.          */
  516.         "AddImageToList" : function (Image, IsSmall) {
  517.                 //Make sure the parameters are alright
  518.                 Image = (typeof Image === "number") ? Image : 0;
  519.                 IsSmall = (typeof IsSmall === "undefined") ? false : (IsSmall == true);
  520.                 iImageList = IsSmall ? LVSIL_SMALL : LVSIL_NORMAL;
  521.                
  522.                 //Add the image to the image list
  523.                 var iImage = Interop.Call("comctl32", "ImageList_Add", this.hImageList, Image, 0);
  524.  
  525.                 //Set the image list
  526.                 var Result = this.Helpers.SendMessage(LVM_SETIMAGELIST, iImageList, this.hImageList);
  527.  
  528.                 return iImage;
  529.         },
  530.         /**
  531.          *      RemoveImageFromList
  532.          *
  533.          *      Desc: Removes an image from the image list of a list-view control.
  534.          *      Params:       Image    Index of the image in the image list to remove.
  535.          *                        IsSmall       Boolean specifying whether to add the image to the large (false) or small (true) image list.
  536.          *      Return value: True indicates success, false otherwise.
  537.          */
  538.         "RemoveImageFromList" : function (Image, IsSmall) {
  539.                 //Make sure the parameters are alright
  540.                 Image = (typeof Image === "undefined") ? 0 : 1*Image;
  541.                 IsSmall = (typeof IsSmall === "undefined") ? false : (IsSmall == true);
  542.                 ImageList = IsSmall ? LVSIL_SMALL : LVSIL_NORMAL;
  543.  
  544.                 //Get the image list
  545.                 var hImageList = this.Helpers.SendMessage(LVM_GETIMAGELIST, ImageList, 0);
  546.  
  547.                 //Remove the image from the list
  548.                 return (Interop.Call("comctl32", "ImageList_Remove", hImageList, Image) !== 0);
  549.         },
  550.         /**
  551.          *      Destroy
  552.          *
  553.          *      Desc: Removes this instance of the ListView class from the global array.
  554.          *      Params: none
  555.          */
  556.         "Destroy" : function () {
  557.                 try { delete ListViews[this.Handle]; } catch(e) { }
  558.         },
  559.         /**
  560.          *      Helper functions
  561.          *
  562.          *      Desc: Functions used by the main functions to do their stuff. No need to look at these.
  563.          */
  564.         "Helpers" : {
  565.                 "Parent" : 0,
  566.                 //Save the pointer of the parent ListView object
  567.                 "AssignParent" : function (oParent) {
  568.                         this.Parent = oParent;
  569.                 },
  570.                 //Checks if a given PlusWnd or Window ID is a valid window
  571.                 "IsWindow" : function (PlusWnd) {
  572.                         return(typeof PlusWnd === "object" && typeof PlusWnd.Handle === "number" && Interop.Call("user32", "IsWindow", PlusWnd.Handle) );
  573.                 },
  574.                 //Sends a control message
  575.                 "SendMessage" : function (Message, wParam, lParam, hWnd) {
  576.                         hWnd = (typeof hWnd === "undefined") ? this.Parent.Handle : 1*hWnd;
  577.                         wParam = (typeof wParam === "undefined") ? 0 : wParam;
  578.                         lParam = (typeof lParam === "undefined") ? 0 : lParam;
  579.  
  580.                         return Interop.Call("user32", "SendMessageW", hWnd, Message, wParam, lParam);
  581.                 },
  582.                 //ToArray - Converts any iterable object to an array. - based on $A from Prototype(www.prototypejs.org)
  583.                 "ToArray" : function (iterable) {
  584.                         if (!iterable) return[];
  585.                         if (iterable.toArray) return iterable.toArray();
  586.                         var length = iterable.length, results = new Array(length);
  587.                         while (length--) results[length] = iterable[length];
  588.                         return results;
  589.                 },
  590.                 //BindFunction - Binds a function to an object. - based on Function.bind from Prototype(www.prototypejs.org)
  591.                 "BindFunction" : function () {
  592.                         var $A = this.ToArray;
  593.                         var args = $A(arguments);
  594.                         var func = args.shift();
  595.                         var obj = args.shift();
  596.                         return function () {
  597.                                 return func.apply(obj, args.concat($A(arguments)));
  598.                         }
  599.                 },
  600.                 //Creates an LVITEM structure
  601.                 "LVITEM" : function (_LVITEM, ItemIdx, ItemText, ImageIdx, lParam, SubItemIdx) {
  602.                         //Get a memory block for this!
  603.                         var LVITEM;
  604.                         var LVITEM_Size = 36;
  605.                         if (typeof _LVITEM === "number" && _LVITEM > 0) {
  606.                                 //Move the memory block so we can access it
  607.                                 LVITEM = Interop.Allocate(LVITEM_Size);
  608.                                 var Result = Interop.Call("kernel32", "RtlMoveMemory", LVITEM.DataPtr, _LVITEM, LVITEM.Size);
  609.                         } else if (typeof LVITEM === "object" && typeof LVITEM.Size === "number") {
  610.                                 //Modify the given LVITEM DataBloc
  611.                                 LVITEM = _LVITEM;
  612.                         } else {
  613.                                 //Create a new block
  614.                                 LVITEM = Interop.Allocate(LVITEM_Size);
  615.                         }
  616.  
  617.                         var Mask = LVITEM.ReadDWORD(0);
  618.                         //iIndex
  619.                         LVITEM.WriteDWORD(4, ItemIdx);
  620.                         //pszText
  621.                         if (typeof ItemText === "string") {
  622.                                 //Write the string in a memory block
  623.                                 var pszText = Interop.Allocate( (ItemText.length+1) *2);
  624.                                 pszText.WriteString(0, ItemText, true);
  625.                                 Mask |= LVIF_TEXT; //Set the mask
  626.                                 LVITEM.WriteDWORD(20, pszText.DataPtr);
  627.                         }
  628.                         //iImage
  629.                         if (typeof ImageIdx === "number") {
  630.                                 Mask |= LVIF_IMAGE;
  631.                                 LVITEM.WriteDWORD(28, ImageIdx);
  632.                         }
  633.                         //lParam
  634.                         if (typeof lParam === "number") {
  635.                                 Mask |= LVIF_PARAM;
  636.                                 LVITEM.WriteDWORD(32, lParam);
  637.                         }
  638.                         //iSubItem
  639.                         if (typeof SubItemIdx === "number") {
  640.                                 LVITEM.WriteDWORD(8, SubItemIdx);
  641.                         }
  642.                         //mask
  643.                         LVITEM.WriteDWORD(0, Mask);
  644.  
  645.                         return LVITEM;
  646.                 },
  647.                 //Creates an LVCOLUMN structure
  648.                 "LVCOLUMN" : function (_LVCOLUMN, ColumnText, SubItemIdx, ImageIdx) {
  649.                         //Get a memory block for this!
  650.                         var LVCOLUMN;
  651.                         var LVCOLUMN_Size = 32;
  652.                         if (typeof _LVCOLUMN === "number" && _LVCOLUMN > 0) {
  653.                                 //Move the memory block so we can access it
  654.                                 LVCOLUMN = Interop.Allocate(LVCOLUMN_Size);
  655.                                 var Result = Interop.Call("kernel32", "RtlMoveMemory", LVCOLUMN.DataPtr, _LVCOLUMN, LVCOLUMN.Size);
  656.                         } else if (typeof LVCOLUMN === "object" && typeof LVCOLUMN.Size === "number") {
  657.                                 //Modify the given LVITEM DataBloc
  658.                                 LVCOLUMN = _LVCOLUMN;
  659.                         } else {
  660.                                 //Create a new block
  661.                                 LVCOLUMN = Interop.Allocate(LVCOLUMN_Size);
  662.                         }
  663.  
  664.                         var Mask = LVCOLUMN.ReadDWORD(0);
  665.                         var fmt = LVCOLUMN.ReadDWORD(4);
  666.                         //pszText
  667.                         if (typeof ColumnText === "string") {
  668.                                 //Write the string in a memory block
  669.                                 var pszText = Interop.Allocate( (ColumnText.length+1) *2);
  670.                                 pszText.WriteString(0, ColumnText, true);
  671.                                 Mask |= LVCF_TEXT; //Set the mask
  672.                                 LVCOLUMN.WriteDWORD(12, pszText.DataPtr);
  673.                         }
  674.                         //iSubItem
  675.                         if (typeof SubItemIdx === "number") {
  676.                                 Mask |= LVCF_SUBITEM;
  677.                                 LVCOLUMN.WriteDWORD(20, SubItemIdx);
  678.                         }
  679.                         //iImage
  680.                         if (typeof ImageIdx === "number") {
  681.                                 Mask |= LVCF_IMAGE;
  682.                                 fmt |= LVCFMT_IMAGE;
  683.                                 LVCOLUMN.WriteDWORD(24, ImageIdx);
  684.                         }
  685.                         //mask
  686.                         LVCOLUMN.WriteDWORD(0, Mask);
  687.                         //fmt
  688.                         LVCOLUMN.WriteDWORD(4, fmt);
  689.  
  690.                         return LVCOLUMN;
  691.                 }
  692.         }
  693. }
  694.  
  695. /**
  696. *      ListViewCallback
  697. *
  698. *      Desc: Callback function for sorting list-view controls. Used internally by the ListView class.
  699. */
  700. function ListViewCallback_Sort(ItemIdx1, ItemIdx2, Handle) {
  701.         var Lst = ListViews[Handle], Result = 0;
  702.         if (typeof Lst !== "object") return 0;
  703.         switch (Lst.SortOptions.Flags) {
  704.                 case 0x1: //As original index
  705.                         Result = Lst.SortOptions.Callback(
  706.                                 Lst.FindItem({'lparam':ItemIdx1}),
  707.                                 Lst.FindItem({'lparam':ItemIdx2}),
  708.                                 Lst.SortOptions.Ascending
  709.                         );
  710.                         break;
  711.                 case 0x2: //As index
  712.                         Result = Lst.SortOptions.Callback(
  713.                                 ItemIdx1,
  714.                                 ItemIdx2,
  715.                                 Lst.SortOptions.Ascending
  716.                         );
  717.                         break;
  718.                 default: //As subitem text
  719.                         Result = Lst.SortOptions.Callback(
  720.                                 Lst.GetItemText(Lst.FindItem({'lparam':ItemIdx1}), Lst.SortOptions.Column),
  721.                                 Lst.GetItemText(Lst.FindItem({'lparam':ItemIdx2}), Lst.SortOptions.Column),
  722.                                 Lst.SortOptions.Ascending
  723.                         );
  724.         }
  725.         return Result;
  726. }

Version

  • 5.0.0070_20100325_publicbeta1

Developers

Project Details

  • Folders8
  • Files122
  • Total Lines11,867
  • Repository Version70

User Count

  • 162