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

Method TV_GetRelatedItem

source/script2.cpp:16341–16412  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16339
16340
16341ResultType GuiControlType::TV_GetRelatedItem(ResultToken &aResultToken, int aID, int aFlags, ExprTokenType *aParam[], int aParamCount)
16342// TV.GetParent/Child/Selection/Next/Prev(hitem):
16343// The above all return the HTREEITEM (or 0 on failure).
16344// When TV.GetNext's second parameter is present, the search scope expands to include not just siblings,
16345// but also children and parents, which allows a tree to be traversed from top to bottom without the script
16346// having to do something fancy.
16347{
16348 GuiControlType &control = *this;
16349 HWND control_hwnd = control.hwnd;
16350
16351 HTREEITEM hitem = (HTREEITEM)ParamIndexToOptionalIntPtr(0, NULL);
16352
16353 if (ParamIndexIsOmitted(1))
16354 {
16355 WPARAM flag;
16356 switch (aID)
16357 {
16358 case FID_TV_GetSelection: flag = TVGN_CARET; break; // TVGN_CARET is the focused item.
16359 case FID_TV_GetParent: flag = TVGN_PARENT; break;
16360 case FID_TV_GetChild: flag = TVGN_CHILD; break;
16361 case FID_TV_GetPrev: flag = TVGN_PREVIOUS; break;
16362 case FID_TV_GetNext: flag = !hitem ? TVGN_ROOT : TVGN_NEXT; break; // TV_GetNext(no-parameters) yields very first item in Tree (TVGN_ROOT).
16363 // Above: It seems best to treat hitem==0 as "get root", even though it sacrifices some error detection,
16364 // because not doing so would be inconsistent with the fact that TV.GetNext(0, "Full") does get the root
16365 // (which needs to be retained to make script loops to traverse entire tree easier).
16366 //case FID_TV_GetCount:
16367 default:
16368 // There's a known bug mentioned at MSDN that a TreeView might report a negative count when there
16369 // are more than 32767 items in it (though of course HTREEITEM values are never negative since they're
16370 // defined as unsigned pseudo-addresses). But apparently, that bug only applies to Visual Basic and/or
16371 // older OSes, because testing shows that SendMessage(TVM_GETCOUNT) returns 32800+ when there are more
16372 // than 32767 items in the tree, even without casting to unsigned. So I'm not sure exactly what the
16373 // story is with this, so for now just casting to UINT rather than something less future-proof like WORD:
16374 // Older note, apparently unneeded at least on XP SP2: Cast to WORD to convert -1 through -32768 to the
16375 // positive counterparts.
16376 _o_return((UINT)SendMessage(control_hwnd, TVM_GETCOUNT, 0, 0));
16377 }
16378 // Apparently there's no direct call to get the topmost ancestor of an item, presumably because it's rarely
16379 // needed. Therefore, no such mode is provide here yet (the syntax TV.GetParent(hitem, true) could be supported
16380 // if it's ever needed).
16381 _o_return(SendMessage(control_hwnd, TVM_GETNEXTITEM, flag, (LPARAM)hitem));
16382 }
16383
16384 // Since above didn't return, this TV.GetNext's 2-parameter mode, which has an expanded scope that includes
16385 // not just siblings, but also children and parents. This allows a tree to be traversed from top to bottom
16386 // without the script having to do something fancy.
16387 TCHAR first_char_upper = ctoupper(*omit_leading_whitespace(ParamIndexToString(1, _f_number_buf))); // Resolve parameter #2.
16388 bool search_checkmark;
16389 if (first_char_upper == 'C')
16390 search_checkmark = true;
16391 else if (first_char_upper == 'F')
16392 search_checkmark = false;
16393 else // Reserve other option letters/words for future use by being somewhat strict.
16394 _o_throw_param(1);
16395
16396 // When an actual item was specified, search begins at the item *after* it. Otherwise (when NULL):
16397 // It's a special mode that always considers the root node first. Otherwise, there would be no way
16398 // to start the search at the very first item in the tree to find out whether it's checked or not.

Callers

nothing calls this directly

Calls 3

ctoupperFunction · 0.85
omit_leading_whitespaceFunction · 0.85
GetNextTreeItemFunction · 0.85

Tested by

no test coverage detected