// This selects an item by tile index
        public void SelectItem(int tileindex)
        {
            // Not when selecting is prevented
            if (preventselection)
            {
                return;
            }

            // Search for item
            ImageBrowserItem target = null;             //mxd

            foreach (ImageBrowserItem item in items)
            {
                if (item.Icon.TileIndex == tileindex)                //mxd
                {
                    target = item;
                    break;
                }
            }

            if (target != null)
            {
                // Select the item
                list.SetSelectedItem(target);
            }
        }
 private void OnItemDoubleClicked(ImageBrowserItem item)
 {
     if (ItemDoubleClicked != null)
     {
         ItemDoubleClicked(this, item);
     }
 }
        public void ScrollToItem(ImageBrowserItem item)
        {
            int index = items.IndexOf(item);

            if (index < 0)
            {
                return;
            }

            Rectangle rec = rectangles[index];

            //mxd. Already visible?
            int ymin = scrollbar.Value;
            int ymax = ymin + this.ClientRectangle.Height;

            if (rec.Top - 3 >= ymin && rec.Bottom + 3 <= ymax)
            {
                return;
            }

            int yscroll = Math.Max(0, Math.Min(rec.Top - 3, scrollbar.Maximum - ClientRectangle.Height));

            scrollbar.Value = yscroll;
            Refresh();
        }
 //mxd. Clears the list without redrawing it
 public void Clear()
 {
     selection.Clear();
     items.Clear();
     lastselecteditem = null;
     rectangles.Clear();
 }
        //mxd
        public void ClearSelection()
        {
            selection.Clear();
            lastselecteditem = null;

            OnSelectionChanged(selection);
            Refresh();
        }
        public void SetItems(IEnumerable <ImageBrowserItem> items)
        {
            this.items.Clear();
            lastselecteditem = null;
            selection.Clear();
            this.items.AddRange(items);

            OnSelectionChanged(selection);
            UpdateRectangles();
        }
        //mxd
        private int SortItems(ImageBrowserItem item1, ImageBrowserItem item2)
        {
            if (usedimagesfirst.Checked && item1.Icon.UsedInMap != item2.Icon.UsedInMap)
            {
                // Push used items to the top
                return(item1.Icon.UsedInMap ? -1 : 1);
            }

            return(item1.CompareTo(item2));
        }
 // Doublelicking an item
 private void list_ItemDoubleClicked(object sender, ImageBrowserItem item)
 {
     if (!preventselection && (list.SelectedItems.Count > 0))
     {
         if (SelectedItemDoubleClicked != null)
         {
             SelectedItemDoubleClicked(item);
         }
     }
 }
 //mxd. This validates an item's texture size
 private static bool ValidateItemSize(ImageBrowserItem i, int w, int h)
 {
     if (!i.Icon.IsPreviewLoaded)
     {
         return(true);
     }
     if (w > 0 && i.Icon.Width != w)
     {
         return(false);
     }
     if (h > 0 && i.Icon.Height != h)
     {
         return(false);
     }
     return(true);
 }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.Focus();

            if (!allowselection)
            {
                return;
            }
            if (!allowmultipleselection || !General.Interface.CtrlState)
            {
                selection.Clear();
            }

            int x = e.X;
            int y = scrollbar.Value + e.Y;

            int clickedIndex = GetIndexAt(x, y);
            var item         = (clickedIndex >= 0 && clickedIndex < items.Count ? items[clickedIndex] : null);

            if (item == null)
            {
                selection.Clear();
            }
            else if (allowmultipleselection && General.Interface.CtrlState && selection.Contains(item))
            {
                selection.Remove(item);
                lastselecteditem = null;
            }
            else if (allowmultipleselection && General.Interface.ShiftState && lastselecteditem != null)
            {
                int bef   = items.IndexOf(lastselecteditem);
                var start = Math.Min(bef, clickedIndex);
                var count = Math.Abs(clickedIndex - bef) + 1;
                selection.AddRange(items.GetRange(start, count).Where(i => !selection.Contains(i)));
            }
            else
            {
                selection.Add(item);
                lastselecteditem = item;
            }

            OnSelectionChanged(selection);
            Refresh();
        }
        // This validates an item
        private bool ValidateItem(ImageBrowserItem item, ImageBrowserItem previtem)
        {
            //mxd. Don't show duplicate items
            if (previtem != null && item.TileName == previtem.TileName)
            {
                return(false);                                                                   //mxd
            }
            //mxd. Filter by Po2 setting. 0 - Any, 1 - only Po2, 2 - only nPo2
            switch (po2combo.SelectedIndex)
            {
            case 1: if (!item.IsPowerOf2)
                {
                    return(false);
                }
                break;

            case 2: if (item.IsPowerOf2)
                {
                    return(false);
                }
                break;
            }

            //mxd. Filter by Opacity setting. 0 - Any, 1 - only with alpha, 2 - only without alpha
            switch (alphacombo.SelectedIndex)
            {
            case 1: if (!item.HasAlpha)
                {
                    return(false);
                }
                break;

            case 2: if (item.HasAlpha)
                {
                    return(false);
                }
                break;
            }

            // Filter by name
            return(item.TileName.ToUpperInvariant().Contains(objectname.Text.ToUpperInvariant()));
        }
 public void SetSelectedItem(ImageBrowserItem item)
 {
     SetSelectedItems(new List <ImageBrowserItem> {
         item
     });
 }
        // This fills the list based on the objectname filter
        private void RefillList(bool selectfirst)
        {
            visibleitems = new List <ImageBrowserItem>();

            //mxd. Store info about currently selected item
            string selectedname = string.Empty;

            if (!selectfirst && keepselected == -1 && list.SelectedItems.Count > 0)
            {
                selectedname = list.SelectedItems[0].TileName;
            }

            // Clear list first
            list.Clear();

            //mxd. Anything to do?
            if (items.Count == 0)
            {
                return;
            }

            //mxd. Filtering by texture size?
            int w = filterWidth.GetResult(-1);
            int h = filterHeight.GetResult(-1);

            // Go for all items
            ImageBrowserItem previtem = null;             //mxd

            for (int i = items.Count - 1; i > -1; i--)
            {
                // Add item if valid
                if (ValidateItem(items[i], previtem) && ValidateItemSize(items[i], w, h))
                {
                    visibleitems.Add(items[i]);
                    previtem = items[i];
                }
            }

            // Fill list
            visibleitems.Sort(SortItems);
            list.SetItems(visibleitems);

            // Update Filter group title
            if (visibleitems.Count != items.Count)
            {
                filtergroup.Text = " Filtering (" + visibleitems.Count + " out of " + items.Count + " images shown) ";
            }
            else
            {
                filtergroup.Text = " Filtering ";
            }

            // Make selection?
            if (!preventselection && list.Items.Count > 0)
            {
                // Select specific item?
                if (keepselected > -1)
                {
                    list.SetSelectedItem(list.Items[keepselected]);
                }
                // Select first item?
                else if (selectfirst)
                {
                    SelectFirstItem();
                }
                //mxd. Try reselecting the same/next closest item
                else if (!string.IsNullOrEmpty(selectedname))
                {
                    ImageBrowserItem bestmatch = null;
                    int charsmatched           = 1;
                    foreach (ImageBrowserItem item in list.Items)
                    {
                        if (item.TileName[0] == selectedname[0])
                        {
                            if (item.TileName == selectedname)
                            {
                                bestmatch = item;
                                break;
                            }

                            for (int i = 1; i < Math.Min(item.TileName.Length, selectedname.Length); i++)
                            {
                                if (item.TileName[i] != selectedname[i])
                                {
                                    if (i > charsmatched)
                                    {
                                        bestmatch    = item;
                                        charsmatched = i;
                                    }
                                    break;
                                }
                            }
                        }
                    }

                    // Select found item
                    if (bestmatch != null)
                    {
                        list.SetSelectedItem(bestmatch);
                    }
                    else
                    {
                        SelectFirstItem();
                    }
                }
            }

            // Raise event
            if ((SelectedItemChanged != null) && !preventselection)
            {
                SelectedItemChanged(list.SelectedItems.Count > 0 ? list.SelectedItems[0] : null);
            }
        }