Пример #1
0
        /// <summary>Called when the control should be rendered</summary>
        public override void Render(Device device, float elapsedTime)
        {
            ControlState state = ControlState.Normal;

            if (!isComboOpen)
            {
                state = ControlState.Hidden;
            }

            // Dropdown box
            Element e = elementList[ComboBox.DropdownLayer] as Element;

            // If we have not initialized the scroll bar page size,
            // do that now.
            if (!isScrollBarInit)
            {
                FontNode fNode = DialogResourceManager.GetGlobalInstance().GetFontNode((int)e.FontIndex);
                if ((fNode != null) && (fNode.Height > 0))
                {
                    scrollbarControl.PageSize = (int)(dropDownTextRect.Height / fNode.Height);
                }
                else
                {
                    scrollbarControl.PageSize = dropDownTextRect.Height;
                }

                isScrollBarInit = true;
            }

            if (isComboOpen)
            {
                scrollbarControl.Render(device, elapsedTime);
            }

            // Blend current color
            e.TextureColor.Blend(state, elapsedTime);
            e.FontColor.Blend(state, elapsedTime);
            parentDialog.DrawSprite(e, dropDownRect);

            // Selection outline
            Element selectionElement = elementList[ComboBox.SelectionLayer] as Element;

            selectionElement.TextureColor.Current = e.TextureColor.Current;
            selectionElement.FontColor.Current    = selectionElement.FontColor.States[(int)ControlState.Normal];

            FontNode font            = DialogResourceManager.GetGlobalInstance().GetFontNode((int)e.FontIndex);
            int      currentY        = dropDownTextRect.Top;
            int      remainingHeight = dropDownTextRect.Height;

            for (int i = scrollbarControl.TrackPosition; i < itemList.Count; i++)
            {
                ComboBoxItem cbi = (ComboBoxItem)itemList[i];

                // Make sure there's room left in the dropdown
                remainingHeight -= (int)font.Height;
                if (remainingHeight < 0)
                {
                    // Not visible, store that item
                    cbi.IsItemVisible = false;
                    itemList[i]       = cbi; // Store this back in list
                    continue;
                }

                cbi.ItemRect = new System.Drawing.Rectangle(dropDownTextRect.Left, currentY,
                                                            dropDownTextRect.Width, (int)font.Height);
                cbi.IsItemVisible = true;
                currentY         += (int)font.Height;
                itemList[i]       = cbi; // Store this back in list

                if (isComboOpen)
                {
                    if (focusedIndex == i)
                    {
                        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(
                            dropDownRect.Left, cbi.ItemRect.Top - 2, dropDownRect.Width,
                            cbi.ItemRect.Height + 4);
                        parentDialog.DrawSprite(selectionElement, rect);
                        parentDialog.DrawText(cbi.ItemText, selectionElement, cbi.ItemRect);
                    }
                    else
                    {
                        parentDialog.DrawText(cbi.ItemText, e, cbi.ItemRect);
                    }
                }
            }

            int offsetX = 0;
            int offsetY = 0;

            state = ControlState.Normal;
            if (IsVisible == false)
            {
                state = ControlState.Hidden;
            }
            else if (IsEnabled == false)
            {
                state = ControlState.Disabled;
            }
            else if (isPressed)
            {
                state   = ControlState.Pressed;
                offsetX = 1;
                offsetY = 2;
            }
            else if (isMouseOver)
            {
                state   = ControlState.MouseOver;
                offsetX = -1;
                offsetY = -2;
            }
            else if (hasFocus)
            {
                state = ControlState.Focus;
            }

            float blendRate = (state == ControlState.Pressed) ? 0.0f : 0.8f;

            // Button
            e = elementList[ComboBox.ComboButtonLayer] as Element;

            // Blend current color
            e.TextureColor.Blend(state, elapsedTime, blendRate);

            System.Drawing.Rectangle windowRect = buttonRect;
            windowRect.Offset(offsetX, offsetY);
            // Draw sprite
            parentDialog.DrawSprite(e, windowRect);

            if (isComboOpen)
            {
                state = ControlState.Pressed;
            }

            // Main text box
            e = elementList[ComboBox.MainLayer] as Element;

            // Blend current color
            e.TextureColor.Blend(state, elapsedTime, blendRate);
            e.FontColor.Blend(state, elapsedTime, blendRate);

            // Draw sprite
            parentDialog.DrawSprite(e, textRect);

            if (selectedIndex >= 0 && selectedIndex < itemList.Count)
            {
                try
                {
                    ComboBoxItem cbi = (ComboBoxItem)itemList[selectedIndex];
                    parentDialog.DrawText(cbi.ItemText, e, textRect);
                }
                catch { } // Ignore
            }
        }
Пример #2
0
        /// <summary>Called when the control should handle the mouse</summary>
        public override bool HandleMouse(NativeMethods.WindowMessage msg, System.Drawing.Point pt, IntPtr wParam, IntPtr lParam)
        {
            if (!IsEnabled || !IsVisible)
            {
                return(false); // Nothing to do
            }
            // Let the scroll bar handle it first
            if (scrollbarControl.HandleMouse(msg, pt, wParam, lParam))
            {
                return(true);
            }

            // Ok, scrollbar didn't handle it, move on
            switch (msg)
            {
            case NativeMethods.WindowMessage.MouseMove:
            {
                if (isComboOpen && dropDownRect.Contains(pt))
                {
                    // Determine which item has been selected
                    for (int i = 0; i < itemList.Count; i++)
                    {
                        ComboBoxItem cbi = (ComboBoxItem)itemList[i];
                        if (cbi.IsItemVisible && cbi.ItemRect.Contains(pt))
                        {
                            focusedIndex = i;
                        }
                    }
                    return(true);
                }
                break;
            }

            case NativeMethods.WindowMessage.LeftButtonDoubleClick:
            case NativeMethods.WindowMessage.LeftButtonDown:
            {
                if (ContainsPoint(pt))
                {
                    // Pressed while inside the control
                    isPressed = true;
                    Parent.SampleFramework.Window.Capture = true;

                    if (!hasFocus)
                    {
                        Dialog.RequestFocus(this);
                    }

                    // Toggle dropdown
                    if (hasFocus)
                    {
                        isComboOpen = !isComboOpen;
                        if (!isComboOpen)
                        {
                            if (!parentDialog.IsUsingKeyboardInput)
                            {
                                Dialog.ClearFocus();
                            }
                        }
                    }

                    return(true);
                }

                // Perhaps this click is within the dropdown
                if (isComboOpen && dropDownRect.Contains(pt))
                {
                    // Determine which item has been selected
                    for (int i = scrollbarControl.TrackPosition; i < itemList.Count; i++)
                    {
                        ComboBoxItem cbi = (ComboBoxItem)itemList[i];
                        if (cbi.IsItemVisible && cbi.ItemRect.Contains(pt))
                        {
                            selectedIndex = focusedIndex = i;
                            RaiseChangedEvent(this, true);

                            isComboOpen = false;

                            if (!parentDialog.IsUsingKeyboardInput)
                            {
                                Dialog.ClearFocus();
                            }

                            break;
                        }
                    }
                    return(true);
                }
                // Mouse click not on main control or in dropdown, fire an event if needed
                if (isComboOpen)
                {
                    focusedIndex = selectedIndex;
                    RaiseChangedEvent(this, true);
                    isComboOpen = false;
                }

                // Make sure the control is no longer 'pressed'
                isPressed = false;

                // Release focus if appropriate
                if (!parentDialog.IsUsingKeyboardInput)
                {
                    Dialog.ClearFocus();
                }

                break;
            }

            case NativeMethods.WindowMessage.LeftButtonUp:
            {
                if (isPressed && ContainsPoint(pt))
                {
                    // Button click
                    isPressed = false;
                    Parent.SampleFramework.Window.Capture = false;
                    return(true);
                }
                break;
            }

            case NativeMethods.WindowMessage.MouseWheel:
            {
                int zdelta = (short)NativeMethods.HiWord((uint)wParam.ToInt32()) / Dialog.WheelDelta;
                if (isComboOpen)
                {
                    scrollbarControl.Scroll(-zdelta * System.Windows.Forms.SystemInformation.MouseWheelScrollLines);
                }
                else
                {
                    if (zdelta > 0)
                    {
                        if (focusedIndex > 0)
                        {
                            focusedIndex--;
                            selectedIndex = focusedIndex;
                            if (!isComboOpen)
                            {
                                RaiseChangedEvent(this, true);
                            }
                        }
                    }
                    else
                    {
                        if (focusedIndex + 1 < NumberItems)
                        {
                            focusedIndex++;
                            selectedIndex = focusedIndex;
                            if (!isComboOpen)
                            {
                                RaiseChangedEvent(this, true);
                            }
                        }
                    }
                }
                return(true);
            }
            }

            // Didn't handle it
            return(false);
        }