- About
- API
  - General notes
  - DropdownMenu
  - CheckBox
  - Tri-state checkbox
  - ColorPicker
  - Button
  - Slider
- Examples

## About

LibRedDropdown is one more library that allows devs to add some standard GUI elements into their addons. Initially this lib was created to replace Blizzard's UIDropDownMenu; now it contains 6 controls.

## API

### General notes

Getting lib instance:

    local libRedDropdown = LibStub("LibRedDropdown-1.0");

All element constructors are static, you should call it like `lib.CreateControl()` (using dot). All element methods are instance methods, you should call it like `element:SomeMethod()` (using colon).
Basically, you should call `:SetParent` and `:SetPoint` methods for all elements after creating.

### DropdownMenu

Replacement for Blizzard's UIDropDownMenu.

#### Constructor

    local dropdownMenu = libRedDropdown.CreateDropdownMenu();

Method | Description
dropdownMenu:SetList(table entities, boolean dontUpdateInternalList) | Sets list of entities. See examples below.
dropdownMenu:GetButtonByText(string text) | Returns button with specified text on it (or `nil` if no button found).

### CheckBox

Checkbox with clickable label. It's inherited from Blizzard's CheckButton so you can use all its methods

#### Constructor

    local checkbox = libRedDropdown.CreateCheckBox();

Method | Description
checkbox:SetText(string text) | Sets text of label.
checkbox:GetText() | Gets text of label.
checkbox:GetTextObject() | Returns text object of label (FontString).
checkbox:SetOnClickHandler(function func) | Sets `OnClick` handler. See examples below.

### Tri-state checkbox

Checkbox with three states: disabled, enabled#1 and enabled#2

#### Constructor

    local triStateCheckbox = libRedDropdown.CreateCheckBoxTristate();

Method | Description
triStateCheckbox:SetTextEntries(table entries) | Sets text values for each state. See examples below.
triStateCheckbox:SetTriState(number state) | Sets state of checkbox. See examples below.
triStateCheckbox:GetTriState() | Returns the state of checkbox (number). See examples below.
triStateCheckbox:SetOnClickHandler(function func) | Sets `OnClick` handler. See examples below.

### ColorPicker

Small color frame with text label

#### Constructor

    local colorPicker = libRedDropdown.CreateColorPicker();

Method | Description
colorPicker:GetTextObject() | Returns text object of label (FontString). See examples below.
colorPicker:SetText(string text) | Sets text of label. See examples below.
colorPicker:GetText() | Returns text of label (string). See examples below.
colorPicker:SetColor(number red, number green, number blue) | Sets color. Color values are between 0.0 and 1.0 See examples below.
colorPicker:GetColor() | Returns color (number red, number green, number blue). See examples below.

### Button

Just a button. Has sharper edges than Blizzard's one!

#### Constructor

    local button = libRedDropdown.CreateButton();

Method | Description
button:GetTextObject() | Returns text object of label (FontString).
button:SetText(string text) | Sets text of label. See examples below.
button:GetText() | Returns text of label (string). See examples below.
button:SetGray(boolean gray) | "Disables" button. It's not actually disabled, it's just grayed out.
button:IsGrayed() | Returns "disabled" state (boolean).

### Slider

Slider with textbox. See examples below.

#### Constructor

    local slider = libRedDropdown.CreateSlider();

Method | Description
slider:GetTextObject() | Returns text object of label (FontString).
slider:GetBaseSliderObject() | Returns slider object (Slider).
slider:GetEditboxObject() | Returns editbox object (EditBox).
slider:GetLowTextObject() | Returns text object of label of minimum value (FontString).
slider:GetHighTextObject() | Returns text object of label of maximum value (FontString).

## Examples

### dropdownMenu:SetList(table entities, boolean dontUpdateInternalList)

table entites: it is a table with information about buttons that DropdownMenu should display. Valid fields of each entity:

- .text: button's text (string)
- .font: button's text font (string)
- .icon: button's icon (number)
- .func: function that will be executed on user click (function)
- .onEnter: function that will be executed on mouse enter (function)
- .onLeave: function that will be executed on mouse leave (function)
- .disabled: set to true to disable button - will be grayed out (boolean)
- .dontCloseOnClick: set to true to prevent DropdownMenu from hiding when user clicks on button in list (boolean)

boolean dontUpdateInternalList: prevents this method from changing internal list of buttons. Used internally by searchbox.

    local t = { };
    for i = 1, 100 do
      local spellName, spellIcon = GetSpellInfo(i);
      table.insert(t, {
        icon = spellIcon,
        text = spellName,
        onEnter = function(self)
          GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
          GameTooltip:SetSpellByID(i);
          GameTooltip:Show();
        end,
        onLeave = function() GameTooltip:Hide(); end,
        func = function() print(i, spellName); end,
      });
    end
    dropdownMenu:SetList(t);

### checkbox:SetOnClickHandler(function func)

function func: function to execute on click

    checkbox:SetOnClickHandler(function() print("Clicked!"); end);

### Tri-state checkbox

    local triStateCheckbox = libRedDropdown.CreateCheckBoxTristate();
    triStateCheckbox:SetTextEntries({
      "Disabled",
      "Enabled#1",
      "Enabled#2",
    });
    triStateCheckbox:SetOnClickHandler(function(self)
      if (self:GetTriState() == 0) then
        print("Disabled");
      elseif (self:GetTriState() == 1) then
        print("Enabled#1");
      else
        print("Enabled#2");
      end
    end);
    triStateCheckbox:SetTriState(1); -- Set to "Enabled#1"

### ColorPicker

    local SOME_VALUE = { 1, 1, 0 };
    local colorPicker = libRedDropdown.CreateColorPicker();
    colorPicker:SetParent(UIParent);
    colorPicker:SetPoint("CENTER", 0, 0);
    colorPicker:SetText("Label");
    colorPicker:SetColor(1, 0, 0);
    colorPicker:SetScript("OnClick", function()
      ColorPickerFrame:Hide();
      local function callback(restore)
        local r, g, b;
        if (restore) then
          r, g, b = unpack(restore);
        else
          r, g, b = ColorPickerFrame:GetColorRGB();
        end
        colorPicker:SetColor(r, g, b);
        local red, green, blue = colorPicker:GetColor();
        SOME_VALUE = { red, green, blue };
      end
      ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc =
          callback, callback, callback;
      ColorPickerFrame:SetColorRGB(unpack(SOME_VALUE));
      ColorPickerFrame.hasOpacity = false;
      ColorPickerFrame.previousValues = SOME_VALUE;
      ColorPickerFrame:Show();
    end);

### Button

    local button = libRedDropdown.CreateButton();
    button:SetParent(UIParent);
    button:SetText("Click me!");
    button:SetWidth(110);
    button:SetHeight(20);
    button:SetPoint("CENTER", 0, 0);
    button:SetScript("OnClick", function(self, ...)
      print(string.format("Button with label '%s' is clicked!", self:GetText()));
    end);

### Slider

    local minValue, maxValue = 0.3, 3;
    local slider = libRedDropdown.CreateSlider();
    slider:SetParent(UIParent);
    slider:SetWidth(200);
    slider:SetPoint("CENTER", 0, 0);
    slider:GetTextObject():SetText("Label");
    slider:GetBaseSliderObject():SetValueStep(0.1);
    slider:GetBaseSliderObject():SetMinMaxValues(minValue, maxValue);
    slider:GetBaseSliderObject():SetValue(1.5);
    slider:GetBaseSliderObject():SetScript("OnValueChanged", function(self, value)
      local actualValue = tonumber(string_format("%.1f", value));
      slider:GetEditboxObject():SetText(tostring(actualValue));
    end);
    slider:GetEditboxObject():SetText("1.5");
    slider:GetEditboxObject():SetScript("OnEnterPressed", function(self, value)
      if (slider:GetEditboxObject():GetText() ~= "") then
        local v = tonumber(slider:GetEditboxObject():GetText());
        if (v == nil) then
          -- Value must be a number
          slider:GetEditboxObject():SetText(tostring(1.5));
        else
          if (v > maxValue) then
            v = maxValue;
          end
          if (v < minValue) then
            v = minValue;
          end
          slider:GetBaseSliderObject():SetValue(v);
        end
        slider:GetEditboxObject():ClearFocus();
      end
    end);
    slider.lowtext:SetText(tostring(minValue));
    slider.hightext:SetText(tostring(maxValue));