Skip to content

Commit

Permalink
ScrollListboxIntoView: Add it with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-b committed Feb 3, 2025
1 parent ac37ee7 commit c5781ac
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Packages/MIES/MIES_GuiUtilities.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -2435,3 +2435,38 @@ Function ResizeControlsSafe(STRUCT WMWinHookStruct &s)
// return zero so that other hooks are called as well
return 0
End

/// @brief Scroll in the given ListBox the row into view
///
/// @retval 0 if scrolling was done, 1 if not
Function ScrollListboxIntoView(string win, string ctrl, variable row)

variable startRow, numVisRows

ControlInfo/W=$win $ctrl
ASSERT(V_flag == CONTROL_TYPE_LISTBOX, "Expected a listbox")

WAVE/Z/SDFR=$S_DataFolder listWave = $S_Value
ASSERT(WaveExists(listWave), "Missing list wave")

ASSERT(!IsNaN(row), "Expected row to be not NaN")
row = limit(row, 0, DimSize(listWave, ROWS))

numVisRows = trunc(V_height / V_rowHeight)

if(row < V_startRow)
// move row to the first visible row
ListBox $ctrl, row=row, win=$win

return 0
endif

if(row >= (V_startRow + numVisRows))
// move row to the last visible row
ListBox $ctrl, row=(row - numVisRows + 1), win=$win

return 0
endif

return 1
End
70 changes: 70 additions & 0 deletions Packages/tests/Basic/UTF_GuiUtilities.ipf
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,73 @@ static Function NoNullReturnFromGetSetVariableString()
End

/// @}

/// ScrollListboxIntoView
/// @{

static Function GetTopRow_IGNORE(string win, string ctrl)

Controlinfo/W=$win $ctrl

return V_startRow
End

static Function TestScrollListboxIntoView()

string win, ctrl
variable topRow, ret

Make/T listWave = num2str(p)

NewPanel/N=testpanelLB
win = S_name

ListBox list, listWave=listWave, size={300, 100}
ctrl = "list"

try
ScrollListboxIntoView(win, ctrl, NaN)
FAIL()
catch
CHECK_NO_RTE()
endtry

topRow = GetTopRow_IGNORE(win, ctrl)
CHECK_EQUAL_VAR(topRow, 0)

// clips to zero
ret = ScrollListboxIntoView(win, ctrl, -1)
CHECK_EQUAL_VAR(ret, 1)
DoUpdate/W=$win

topRow = GetTopRow_IGNORE(win, ctrl)
CHECK_EQUAL_VAR(topRow, 0)

// clips to available rows
ret = ScrollListboxIntoView(win, ctrl, 500)
CHECK_EQUAL_VAR(ret, 0)
DoUpdate/W=$win

topRow = GetTopRow_IGNORE(win, ctrl)
CHECK_EQUAL_VAR(topRow, 124)

// moves to the top if lower than current
ret = ScrollListboxIntoView(win, ctrl, 50)
CHECK_EQUAL_VAR(ret, 0)
DoUpdate/W=$win

topRow = GetTopRow_IGNORE(win, ctrl)
CHECK_EQUAL_VAR(topRow, 50)

// and to the bottom if larger
ret = ScrollListboxIntoView(win, ctrl, 75)
CHECK_EQUAL_VAR(ret, 0)
DoUpdate/W=$win

topRow = GetTopRow_IGNORE(win, ctrl)
CHECK_EQUAL_VAR(topRow, 71)

KillOrMoveToTrash(wv = listWave)
End

/// @}

0 comments on commit c5781ac

Please sign in to comment.