MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / ControlGetListBox

Method ControlGetListBox

source/script_gui.cpp:1750–1823  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1748
1749
1750ResultType GuiType::ControlGetListBox(ResultToken &aResultToken, GuiControlType &aControl, ValueModeType aMode)
1751{
1752 bool position_mode = (aMode == Value_Mode || (aMode == Submit_Mode && (aControl.attrib & GUI_CONTROL_ATTRIB_ALTSUBMIT)));
1753 if (GetWindowLong(aControl.hwnd, GWL_STYLE) & (LBS_EXTENDEDSEL|LBS_MULTIPLESEL))
1754 {
1755 LRESULT sel_count = SendMessage(aControl.hwnd, LB_GETSELCOUNT, 0, 0);
1756 if (sel_count < 1) // <=0 to check for LB_ERR too (but it should be impossible in this case).
1757 _o_return_empty;
1758 int *item = (int *)malloc(sel_count * sizeof(int)); // dynamic since there can be a very large number of items.
1759 if (!item)
1760 _o_throw_oom;
1761 sel_count = SendMessage(aControl.hwnd, LB_GETSELITEMS, (WPARAM)sel_count, (LPARAM)item);
1762 if (sel_count < 1) // 0 or LB_ERR, but both these conditions should be impossible in this case.
1763 {
1764 free(item);
1765 _o_return_empty;
1766 }
1767
1768 // Create object for storing the selected items.
1769 auto ret = Array::Create();
1770 if (!ret)
1771 {
1772 free(item);
1773 _o_throw_oom;
1774 }
1775
1776 for (LRESULT length = sel_count - 1, i = 0; i < sel_count; ++i)
1777 {
1778 if (position_mode) // Caller wants the positions, not the text.
1779 ret->Append(item[i] + 1); // +1 to convert from zero-based to 1-based.
1780 else // Store item text vs. position.
1781 {
1782 LRESULT item_length = SendMessage(aControl.hwnd, LB_GETTEXTLEN, (WPARAM)item[i], 0);
1783 if (item_length == LB_ERR) // Realistically impossible based on MSDN.
1784 {
1785 free(item);
1786 ret->Release();
1787 _o_throw(_T("LB_GETTEXTLEN")); // Short msg since so rare.
1788 }
1789 LPTSTR temp = tmalloc(item_length+1);
1790 if (!temp)
1791 {
1792 free(item);
1793 ret->Release();
1794 _o_throw_oom; // Short msg since so rare.
1795 }
1796 LRESULT lr = SendMessage(aControl.hwnd, LB_GETTEXT, (WPARAM)item[i], (LPARAM)temp);
1797 if (lr > 0) // Given the way it was called, LB_ERR (-1) should be impossible based on MSDN docs.
1798 ret->Append(temp, item_length);
1799 free(temp);
1800 }
1801 }
1802
1803 free(item);
1804 _o_return(ret);
1805 }
1806 else // Single-select ListBox style.
1807 {

Callers

nothing calls this directly

Calls 3

TokenSetResultFunction · 0.85
AppendMethod · 0.45
ReleaseMethod · 0.45

Tested by

no test coverage detected