private void updateByteSelectionUI()
 {
     for (int i = 0; i < DataStackPanel.Children.Count; i++)
     {
         StackPanel dataLineStack = (StackPanel)DataStackPanel.Children[i];
         for (int j = 0; j < dataLineStack.Children.Count; j++)
         {
             ByteControl item  = (ByteControl)dataLineStack.Children[j];
             int         index = ((getBaseVisibleIndex() + j) + (i * (int)BYTE_PER_LINE));
             if (selectedHexItems[index] == true)
             {
                 if (item.getControlState() != 2)
                 {
                     item.selectedState();
                     AsciiControl testItem = getAsciiControl((int)item.Tag);
                     testItem.selectedState();
                 }
             }
             else
             {
                 item.defaultState();
                 getAsciiControl((int)item.Tag).defaultState();
             }
         }
     }
 }
        /// <summary>
        /// Update Hex Editor Ascii, Hex, and Offset tables
        /// </summary>

        public void updateHexEditor()
        {
            //initializeHexEditor();
            for (int i = 0; i < StringDataStackPanel.Children.Count; i++)
            {
                StackPanel dataLineStack = (StackPanel)StringDataStackPanel.Children[i];
                for (int j = 0; j < dataLineStack.Children.Count; j++)
                {
                    AsciiControl asciiVal = (AsciiControl)dataLineStack.Children[j];
                    asciiVal.AsciiVal = chip8.MainMemory[j + ((i + pos1) * (int)BYTE_PER_LINE)];
                }
            }

            for (int i = 0; i < DataStackPanel.Children.Count; i++)
            {
                StackPanel dataLineStack = (StackPanel)DataStackPanel.Children[i];
                for (int j = 0; j < dataLineStack.Children.Count; j++)
                {
                    ByteControl val = (ByteControl)dataLineStack.Children[j];
                    val.ByteVal = chip8.MainMemory[j + ((i + pos1) * (int)BYTE_PER_LINE)];
                }
            }

            for (int i = 0; i < RamIndexStackPanel.Children.Count; i++)
            {
                Label val   = (Label)RamIndexStackPanel.Children[i];
                int   index = (pos1 + i) * (int)BYTE_PER_LINE;
                val.Content = "0x" + (index).ToString("X").PadLeft(8, '0');
            }
        }
        public void loadStringData()
        {
            StringDataStackPanel.Children.Clear();

            for (int i = 0; i < (int)getMaxVisibleLine(); i++)
            {
                StackPanel dataLineStack = new StackPanel();
                dataLineStack.Height      = ROW_HEIGHT;
                dataLineStack.Orientation = Orientation.Horizontal;

                for (int j = 0; j < BYTE_PER_LINE; j++)
                {
                    //
                    AsciiControl asciiVal = new AsciiControl();
                    asciiVal.Tag = (i * 16) + j;
                    asciiVal.MouseRightButtonDown += new MouseButtonEventHandler(mouseRightDownHandler);
                    asciiVal.MouseWheel           += new MouseWheelEventHandler(MouseWheelHandler);
                    asciiVal.AsciiVal              = chip8.MainMemory[(i * 16) + j];
                    asciiVal.MouseEnter           += new MouseEventHandler(mouseEnterEventHandler);
                    asciiVal.MouseLeave           += new MouseEventHandler(mouseLeaveEventHandler);
                    asciiVal.MouseLeftButtonDown  += new MouseButtonEventHandler(mouseLeftDownHandler);
                    asciiVal.MouseLeftButtonUp    += new MouseButtonEventHandler(mouseLeftUpHandler);
                    asciiVal.HexCopy.Click        += new RoutedEventHandler(hexCopyHandler);
                    asciiVal.AsciiCopy.Click      += new RoutedEventHandler(AsciiCopyHandler);
                    dataLineStack.Children.Add(asciiVal);
                }

                StringDataStackPanel.Children.Add(dataLineStack);
            }
        }
        private AsciiControl getAsciiControl(int index)
        {
            for (int i = 0; i < StringDataStackPanel.Children.Count; i++)
            {
                StackPanel dataLineStack = (StackPanel)StringDataStackPanel.Children[i];
                for (int j = 0; j < dataLineStack.Children.Count; j++)
                {
                    AsciiControl item = (AsciiControl)dataLineStack.Children[j];
                    if ((int)item.Tag == index)
                    {
                        return(item);
                    }
                }
            }

            return(null);
        }
        private void mouseLeaveEventHandler(object sender, MouseEventArgs e)
        {
            ByteControl  byteItem  = sender as ByteControl;
            AsciiControl asciiItem = sender as AsciiControl;

            if (asciiItem != null)
            {
                //if (asciiItem.Background != Brushes.Blue) { }
                //asciiItem.Background = Brushes.White;
                if (asciiItem.getControlState() == 1)
                {
                    asciiItem.defaultState();
                }
            }
            else
            {
                if (byteItem.getControlState() == 1)
                {
                    byteItem.defaultState();
                }
            }
        }
        private void mouseLeftDownHandler(object sender, MouseEventArgs e)
        {
            selectedHexItems = new bool[Chip8.MAIN_MEMOY_SIZE];
            selectionFlag    = true;
            ByteControl  byteItem  = sender as ByteControl;
            AsciiControl asciiItem = sender as AsciiControl;

            if (asciiItem != null)
            {
                //asciiItem.Background = background;
                //asciiItem.TextColor(foreground);
                startSelection = getBaseVisibleIndex() + (int)asciiItem.Tag;
                selectedHexItems[startSelection] = true;
                updateByteSelectionUI();
            }
            else
            {
                startSelection = getBaseVisibleIndex() + (int)byteItem.Tag;
                selectedHexItems[startSelection] = true;
                updateByteSelectionUI();
            }
        }
        private void mouseRightDownHandler(object sender, MouseEventArgs e)
        {
            ByteControl  byteItem  = sender as ByteControl;
            AsciiControl asciiItem = sender as AsciiControl;

            if (byteItem != null)
            {
                if (byteItem.getControlState() == 2)
                {
                    byteItem.HexCopy.IsEnabled   = true;
                    byteItem.AsciiCopy.IsEnabled = true;
                }
            }
            else
            {
                if (asciiItem.getControlState() == 2)
                {
                    asciiItem.HexCopy.IsEnabled   = true;
                    asciiItem.AsciiCopy.IsEnabled = true;
                }
            }
        }
        private void mouseEnterEventHandler(object sender, MouseEventArgs e)
        {
            ByteControl  byteItem  = sender as ByteControl;
            AsciiControl asciiItem = sender as AsciiControl;

            if (selectionFlag) // indicates that the user is dragging the mouse pointer to select more items in the editor
            {
                if (asciiItem != null)
                {
                    endSelection = getBaseVisibleIndex() + (int)asciiItem.Tag;
                    updateSelection();
                }
                else
                {
                    endSelection = getBaseVisibleIndex() + (int)byteItem.Tag;
                    updateSelection();
                }
            }
            else
            {
                //auto highlight items when mouse is hovering over them
                if (asciiItem != null)
                {
                    if (asciiItem.Background != Brushes.Blue)
                    {
                        asciiItem.highlightedState();
                    }
                }
                else
                {
                    if (byteItem.getControlState() != 2)
                    {
                        byteItem.highlightedState();
                    }
                }
            }
        }