[?] ListView visible items

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[?] ListView visible items

Dmitry Zamotkin-3
How to determine number of visible items in a ListView? I mean number of
rows currently displayed by a ListView, not a size of a model.

--
Dmitry Zamotkin


Reply | Threaded
Open this post in threaded view
|

Re: [?] ListView visible items

Christopher J. Demers
Dmitry Zamotkin <[hidden email]> wrote in message
news:9ihphs$t3g$[hidden email]...
> How to determine number of visible items in a ListView? I mean number of
> rows currently displayed by a ListView, not a size of a model.
>

I could not find an easy way to do this, so I came up with a slightly
creative approach.  Perhaps someone else knows a better way, maybe there is
an API that could be added.  I left the values as floats since I was not
sure if you were interested in half items.  I think this code could be used
to accomplish what you want.

Here is one approach:
=========
"I use this kludge to grab a ChoicePrompter list view
 since I did not want to create my own view just for this test."
cp := ChoicePrompter create: 'Default view' on: 'cat' choices: #('cat' 'dog'
'frog' 'bird' 'fish' 'owl') caption: nil.
cp view show.
"Get the listBox view."
lv := cp view viewNamed: 'choices'.
"Calculate the number of items that could be
 displayed based on the row height and the list height."
"This way requires at least one item in the list."
(lv height / (lv itemRect: 1) height) asFloat.
"This way does not require any items in the list
 but I have made an assumption about row spacing."
(lv height / ((lv canvas textExtent: ' ') y + 2)) asFloat.
=========

Chris


Reply | Threaded
Open this post in threaded view
|

Re: [?] ListView visible items

Blair McGlashan
"Christopher J. Demers" <[hidden email]> wrote in
message news:9iipi1$jb672$[hidden email]...
> Dmitry Zamotkin <[hidden email]> wrote in message
> news:9ihphs$t3g$[hidden email]...
> > How to determine number of visible items in a ListView? I mean number of
> > rows currently displayed by a ListView, not a size of a model.
> >
>
> I could not find an easy way to do this, so I came up with a slightly
> creative approach.  Perhaps someone else knows a better way, maybe there
is
> an API that could be added.  ...

ListViews have a special message for this: LVM_GETCOUNTPERPAGE. For a
ListBox, how about:

"---------------------------Begin file in-----------------"!
Win32Constants at: 'LB_GETTOPINDEX' put: 16r18E!
!ListBox methodsFor!

topIndex
 "Answer the 1-based index of the topmost visible item in the list.
 Note that even if there are no items, the answer will be one."

 ^(self sendMessage: LB_GETTOPINDEX) + 1!

visibleRowCount
 "Answer the number of fully visible rows in the receiver, up to a
 maximum of the number of items that are actually in the list.
 N.B. Partially visible rows at the bottom are not included,
 an improvement to include those is left as an exercise for the reader."

 | rect bottom |
 rect := self clientRectangle.
 bottom := self itemFromPoint: rect bottomLeft.
 ^bottom isNil
  ifTrue: ["Off the end of the list"
   self count - self topIndex + 1]
  ifFalse: [bottom - (self itemFromPoint: rect topLeft)]! !
!ListBox categoriesFor: #topIndex!accessing!public! !
!ListBox categoriesFor: #visibleRowCount!accessing!public! !
"------------------------------End file in -------------------"!

This might not be quite what Dmitry needs, but it is a start.

Regards

Blair


Reply | Threaded
Open this post in threaded view
|

Re: [?] ListView visible items

Dmitry Zamotkin-3
Blair, thanks.

Dmitry.

"Blair McGlashan" <[hidden email]> wrote in message
news:9ik467$jjv2g$[hidden email]...
> "Christopher J. Demers" <[hidden email]> wrote in
> message news:9iipi1$jb672$[hidden email]...
> > Dmitry Zamotkin <[hidden email]> wrote in message
> > news:9ihphs$t3g$[hidden email]...
> > > How to determine number of visible items in a ListView? I mean number
of
> > > rows currently displayed by a ListView, not a size of a model.