# Possible Bug in ComboBoxAttachment?

**URL:** <https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991>\
**Category:** General JUCE discussion\
**Created:** [June 20, 2017, 1:05pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991 "2017-06-20T13:05:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![soundbytes1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/soundbytes1/32/2105_2.png) [@soundbytes1](https://forum.juce.com/u/soundbytes1)\
**Post date:** [June 20, 2017, 1:05pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/1 "2017-06-20T13:05:15Z")

</div>

It seems to me that the AudioProcessorValueTreeState::ComboBoxAttachment requires the user to observe some strange and undocumented restrictions on the itemID.

- value range **must** begin with 0
- itemID **must** be Item index + 1

The setValue method uses the item index to select the item.

```
    void setValue (float newValue) override
    {
        const ScopedLock selfCallbackLock (selfCallbackMutex);

        {
            ScopedValueSetter<bool> svs (ignoreCallbacks, true);
            combo.setSelectedItemIndex (roundToInt (newValue), sendNotificationSync);
        }
    }

```

The comboBoxChanged method on the other hand evaluates the itemID to calculate the a new parameter value.

```
    void comboBoxChanged (ComboBox* comboBox) override
    {
        const ScopedLock selfCallbackLock (selfCallbackMutex);

        if (! ignoreCallbacks)
        {
            beginParameterChange();
            setNewUnnormalisedValue ((float) comboBox->getSelectedId() - 1.0f);
            endParameterChange();
        }
    }

```

I would propose to make the implementation more flexible by always using the item ID (just replace setSelectedItemIndex with setSelectedId). Or if that should not be an option at least include the existing restrictions with the documentation. The proposed change will allow to use arbitrary parameter value ranges with arbitrary step size. the only remaining restrictions would be:

- only integer values allowed
- no zero allowed  
the latter can be even worked around using appropriate Value\<\>itemID conversion like this:

> ```
> int getItemIdFromVal(float val) { return val != 0 ? roundToInt(val) : INT_MAX; }
> float getValFromItemId(int itemId) { return itemId != INT_MAX ? (float) itemId : 0.f; }
> 
> ```

---

<div class="post-metadata">

**Author:** ![martinrobinson-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/martinrobinson-2/32/630_2.png) [@martinrobinson-2](https://forum.juce.com/u/martinrobinson-2)\
**Post date:** [June 20, 2017, 6:24pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/2 "2017-06-20T18:24:15Z")

</div>

Linking this thread…

> [@AudioProcessorValueTreeState::ComboBoxAttachment consistency](https://forum.juce.com/t/audioprocessorvaluetreestate-comboboxattachment-consistency/21434):
>
> Using the AudioProcessorValueTreeState class I’m building some lists of options to be controlled by ComboBoxAttachment objects. One issue is that since this stores the values in the plugin state as integers it is easy to break compatibility between plug-in versions if the lists change. In fact it is OK if you add items to the end of a list but not reorder them, since the integer values need to retain their “meaning”. My use-case is that I’m listing some FFT sizes and want initially to expose on…

---

<div class="post-metadata">

**Author:** ![soundbytes1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/soundbytes1/32/2105_2.png) [@soundbytes1](https://forum.juce.com/u/soundbytes1)\
**Post date:** [June 20, 2017, 7:05pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/3 "2017-06-20T19:05:56Z")

</div>

Thanks for the link. Seems I am not the only one to find that odd.  
I have now modified some of the AudioProcessorValueTreeState dependent classes and added my own LabelListener class to solve these problems for me.  
In your case you might be able to use the fft window size as itemID to maintain the semantics of the implementation and to sort the combobox entries just as you find useful since the item index does not get used to retrieve the item value.  
I have even gone a step further and implemented a populate routine that fills the combobox with the appropriate text values provided by the parameter-\>getText function. This allows the combobox to be populated as soon as it gets connected with the parameter through a comboBoxAttachment.  
I am going to upload the solution to github as soon as I have an example jucer project ready.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 25, 2017, 10:31pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/4 "2017-07-25T22:31:01Z")

</div>

I share your observation, that the implementation is inconsistent and error prone. However, I would draw slightly different conclusions:

1. It is a bug to rely on the ID when updating the parameter in `comboBoxChanged` but relying on the index when updating the comboBox in `setValue`. The most flexible for the user is to use the ID, but in any case, it needs to use the same information in both directions.

2. Using INT\_MAX as base for the conversion will not work for numerical reasons. The INT uses the same number of bits like a float (IIRC). Taking conversion errors into account, it cannot map reliably every state from int to float and back. But IMHO it is not needed anyway because of the next point:

3. The callback `setValue` uses `setUnnormalisedValue`, hence it takes the range of the parameter into account. So the user is free to create it’s own mapping, allowing more states than the combobox displays. The drawback is, it is undefined, if a number in the host is selected, that doesn’t exist as ID in the combobox (which means, the combobox is not updated).

TL;DR:  
agreeing with @martinrobinson-2 and @soundbytes1, ComboboxAttachment has to use consistently either the ID of combobox items or the index.

Argument for ID: allows the developer to change the order of items retaining the selections in existing states  
Argument for index: avoids numbers that can’t be mapped on an item in the combobox.

How to fix this is up to @jules or @t0m, who was the last to work on that according to git.

---

<div class="post-metadata">

**Author:** ![soundbytes1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/soundbytes1/32/2105_2.png) [@soundbytes1](https://forum.juce.com/u/soundbytes1)\
**Post date:** [July 25, 2017, 11:20pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/5 "2017-07-25T23:20:54Z")

</div>

> [@daniel](#):
>
> Using INT\_MAX as base for the conversion will not work for numerical reasons.

I am using INT\_MAX simply as a substitute for the 0 value so that it can be used in the context of the ComboBox. This allows the user to include 0 as a parameter value, which otherwise would not be possible.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [July 26, 2017, 10:44am UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/6 "2017-07-26T10:44:52Z")

</div>

Ah sorry, my bad. But I think the current solution shifting the IDs by one works just as well.

---

<div class="post-metadata">

**Author:** ![soundbytes1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/soundbytes1/32/2105_2.png) [@soundbytes1](https://forum.juce.com/u/soundbytes1)\
**Post date:** [July 26, 2017, 3:21pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/7 "2017-07-26T15:21:12Z")

</div>

Depends. I needed a solution that works with a parameter range (-3 … 3).

---

<div class="post-metadata">

**Author:** ![mbayard](https://avatars.discourse-cdn.com/v4/letter/m/9d8465/32.png) [@mbayard](https://forum.juce.com/u/mbayard)\
**Post date:** [January 17, 2018, 6:52pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/8 "2018-01-17T18:52:54Z")

</div>

Was there ever any explanation of this behavior? It sure looks like a bug. It would be really nice to use the ID as the parameter value.

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [May 17, 2018, 6:12am UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/9 "2018-05-17T06:12:29Z")

</div>

> [@soundbytes1](#):
>
> ComboBoxAttachment requires the user to observe some strange and undocumented restrictions on the itemID.
> 
> - value range **must** begin with 0
> - itemID **must** be Item index + 1

> [@daniel](#):
>
> the implementation is inconsistent and error prone

I agree with the above. I just lost some time tracking down the ‘bug’.  
can you at least add a couple of documented assertions in ComboBoxAttachement?

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [May 18, 2018, 7:28pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/10 "2018-05-18T19:28:29Z")

</div>

How about normalising the float parameter value using the range, then using that to determine the selected index?

```
void setValue (float newValue) override
{
    const ScopedLock selfCallbackLock (selfCallbackMutex);

    if (auto* p = state.getParameter (paramID))
    {
        auto normValue = state.getParameterRange (paramID)
                              .convertTo0to1 (newValue);
        auto index = roundToInt (normValue * (combo.getNumItems() - 1));

        if (index != combo.getSelectedItemIndex())
        {
            ScopedValueSetter<bool> svs (ignoreCallbacks, true);
            combo.setSelectedItemIndex (index, sendNotificationSync);
        }
    }
}

void comboBoxChanged (ComboBox*) override
{
    const ScopedLock selfCallbackLock (selfCallbackMutex);

    if (! ignoreCallbacks)
    {
        if (auto* p = state.getParameter (paramID))
        {
            auto newValue = (float) combo.getSelectedItemIndex() / (combo.getNumItems() - 1);
            jassert (isPositiveAndNotGreaterThan (newValue, 1.0f));

            if (p->getValue() != newValue)
            {
                beginParameterChange();

                p->setValueNotifyingHost (newValue);

                endParameterChange();
            }
        }
    }
}

```

I’ve not given this much testing yet, but this should remove any dependency on IDs. The only assumption here is that your combo box labels are distributed evenly across your (arbitrary) range.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 18, 2018, 8:10pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/11 "2018-05-18T20:10:55Z")

</div>

That sounds good to me.  
However, another option would be to allow ranges like  
`NormalisableRange<float> (0, 16, 1)`, but allowing to connect to a Combobox with less items. That way the developer could reserve items for later use (since you cannot hide items in a ComboBox).  
On the other hand, that could be the argument to rely on itemIDs.

The only thing I know is, it should be consistent to use either index or ID…

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [May 21, 2018, 5:48pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/12 "2018-05-21T17:48:11Z")

</div>

I went with being ID agnostic - it’s much less confusing for JUCE users.

[https://github.com/WeAreROLI/JUCE/commit/009eb887efaa38b5f00dd513c4f6eaadafaa98ef](https://github.com/WeAreROLI/JUCE/commit/009eb887efaa38b5f00dd513c4f6eaadafaa98ef)

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [March 7, 2019, 7:11pm UTC](https://forum.juce.com/t/possible-bug-in-comboboxattachment/22991/13 "2019-03-07T19:11:23Z")

</div>

> [@daniel](#):
>
> another option would be to allow ranges like  
> `NormalisableRange<float> (0, 16, 1)` , but allowing to connect to a Combobox with less items. That way the developer could reserve items for later use (since you cannot hide items in a ComboBox).

I had forgotten that it wasn’t possible and just got caught doing it.  
could we get an assertion for that?  
something like:

`jassert (c.getNumItems() == state.getParameter (paramID)->getNumSteps());`

added to `AudioProcessorValueTreeState::ComboBoxAttachment::Pimpl()` ?
