Page 2 of 2

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 4:35 am
by Danilo
Dude wrote:The OP wants to get the top visible item in the list, not the first item in the list. GetGadgetState(0) doesn't do that.
I know what the OP wants, and it was already answered by Shardik.

RASHAD's kludge did not work here (before my small correction), so I didn't see any difference to a simple GetGadgetState(0).

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 4:50 am
by Dude
Danilo wrote:I know what the OP wants
Sorry Danilo, I didn't realise that. I misunderstood what you were saying.

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 6:21 am
by Blue
Danilo wrote:I can't see any difference of RASHAD's code to:

Code: Select all

Debug GetGadgetState(0)
That's all it does. It returns the selected item, not the top index.

Oh well... :lol: :lol:

Hi Danilo
.
Rashad's solution (or kludge as you call it) works as long as there's a selected line in the viewport of the gadget. Run the code in the initial message of this post to better see what I'm looking for. That code does exactly what i want, but of course it uses an API call, which is cheating since the objective is to do it without any API call. That may not be totally possible (although Rashad may prove me wrong!) and you may wonder "but WHY bother?".
It's just curiosity : i spent a long time trying to get that done, and got frustrated that i couldn't. So i figured i'd ask others if they had better luck, or ideas, or skills.

So far, Rashad's code is the closest, as it solves part of the problem. And you've got to admit, kludge or not, his approach is clever...

Rashad's original code (with the (-20) displacement) worked on my 8.1 machine as is.
I'm just wondering why, on yours, the testing point had to be moved another 10 pixels back...
But that made me realize that his code may not provide the expected cross-platform result after all, since other systems surely don't draw Listview gadgets the same way Windows does.


   

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 7:39 am
by Danilo
Blue wrote:Rashad's original code (with the (-20) displacement) worked on my 8.1 machine as is.
I'm just wondering why, on yours, the testing point had to be moved another 10 pixels back...
But that made me realize that his code may not provide the expected cross-platform result after all, since other systems surely don't draw Listview gadgets the same way Windows does.
The vertical scrollbar width may be different between OS, and different between system DPI settings.

Code: Select all

Debug GetSystemMetrics_(#SM_CXVSCROLL)
Outputs 21 with my 125% DPI setting (2560x1440 @ 27"). When switching to 100% DPI, it returns 17 here.
Of course, that's completely different on other configurations and OS. '-24' works for me (21 + 2 px border + 1).

Just make sure the pixel you check is never part of a character, and always the gadget background vs. selection (depends on system, text length, (system) fonts, DPI settings, gadget borders, etc.)
In my personal opinion that way is not reliable, but of course you can use what you like...

Anyway... ;)

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 8:40 am
by Shardik
I have always been an admirer of RASHAD's creative solutions but unfortunately they are rarely usable for cross-platform solutions even if they should work when utilizing only native PB functions. RASHAD's current example simply can't work in MacOS because it seems not possible to use Point() in MacOS for reading the color of a pixel in a Gadget. Point() always returns 0 independant of its position in the ListViewGadget...

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 11:58 am
by infratec
Hi,

for performnace reasons:
Put the StartDrawing() and StopDrawing() outside of the for next loop.

Bernd

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 12:07 pm
by RASHAD
For some reasons Blue wants to use the native commands
I said I prefer the API solutions
I will not argue that point with him(In this case he is the BOSS)
- We will check the color at 2 pixels from start to avoid the width of
the scroll bar and the DPI problems (I do not think that any text will be available at 2 pixels from start )
But to be sure just add a space preceding the text for all items
- I just eliminated the not needed CPU cycles (for flicker case)
- I am thinking right now for another kluge :mrgreen:
Wish me luck :)

Code: Select all

Procedure GetIndex()
  Sel = GetGadgetState(0)
  gColor = GetGadgetColor(0,#PB_Gadget_BackColor)
  StartDrawing(WindowOutput(0))
      For index = Sel To 0 Step -1
        SetGadgetState(0,index)
        Color = Point(GadgetX(0)+2, GadgetY(0)+2)
            If Color <> gColor
               Debug index
               Break
            EndIf
      Next
  StopDrawing()
  SetGadgetState(0,Sel)
EndProcedure  
  
OpenWindow(0, 0, 0, 270, 140, "ListViewGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListViewGadget(0, 10, 10, 250, 120)
  For a = 0 To 20
    AddGadgetItem (0, -1, "Item " + Str(a) + " of the Listview")
  Next
 SetGadgetState(0,0)
 SetGadgetColor(0,#PB_Gadget_BackColor,$FEFEFE)
 SetActiveGadget(0)
Repeat           
  Select WaitWindowEvent()      
      Case #PB_Event_CloseWindow
            Quit = 1
      
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 0
                GetIndex()
            
          EndSelect
  EndSelect 
Until Quit = 1
End
Edit : I see that Bernd are reading my mind :lol:

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 2:40 pm
by Shardik
RASHAD wrote:For some reasons Blue wants to use the native commands
I said I prefer the API solutions
I will not argue that point with him(In this case he is the BOSS)
Accepted...

But I tried to explain that

Code: Select all

        Color = Point(GadgetX(0)+2, GadgetY(0)+2)
is not working on MacOS at all (Color is always 0 independant of the x/y coordinates) because it seems to be impossible on MacOS to use Point() with StartDrawing(WindowOutput(0)) to read a color from the window or its gadgets... :wink:

Your examples are working fine on my Windows 7...

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 8:17 pm
by Blue
RASHAD wrote:For some reasons Blue wants to use the native commands
I said I prefer the API solutions
I will not argue that point with him(In this case he is the BOSS)
Gee.... It's NOT that i WANT to use the native commands, it is that i WANTED TO KNOW if it's possible to use only native commands. That may just seem as a nuance in speech, but i'd think a rather important one. In the end, this topic may prove to be an exercise in futility (and it seems to be hurling in that direction), but at least now i'll know. I'll go to bed less stupid tonight. :D
Shardik, replying to RASHAD, wrote:

Code: Select all

        Color = Point(GadgetX(0)+2, GadgetY(0)+2)
is not working on MacOS at all ... :wink:
Your examples are working fine on my Windows 7...
It's good to discover that. In the real world (i.e. production apps), Shardik's multi-OS proposition certainly appears as the only RELIABLE and COMPLETE solution. One thing is obvious : using OS-specific calls makes the code solid and fast, and smaller. Which satisfies RASAHD's preferences, and most other sane programmers around here. Including me.

Nonetheless, i still think it is worth exploring such subjects, if only because it provides opportunities to truly appreciate RASHAD's inventiveness. That's what keeps the fire alive in programming.

I, for one, now know that i'll keep using the API call (learned years ago from Danilo, or Netmaestro, or Luis or some other PB propeller head ) i illustrated in my opening message. And, promise, promise, i'll stop looking for a PB-only solution :|



  

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 9:27 pm
by luis
Blue wrote:Gee.... It's NOT that i WANT to use the native commands, it is that i WANTED TO KNOW if it's possible to use only native commands.
The fact is PB wraps the OS native controls on each platform but expose not much of them.
If you look at the PB documentation of the various gadgets you see you can generally add and remove items, select them, get the current one, click on them and get a handful of events.
That's what you need for the everyday basic usage, but as soon you want to add a tiny extra feature to your SW (as it happens in most not trivial software with a GUI) you need to use the OS API to complement the simple PB gadgets' API.

Case in point, on the the listview gadget you can more or less do this:

Code: Select all

- AddGadgetItem(): Add an item. 
- RemoveGadgetItem(): Remove an item. 
- ClearGadgetItems(): Remove all the items 
- CountGadgetItems(): Returns the number of items currently in the #Gadget. 
- GetGadgetItemData(): Get the value that was stored with the gadget item. 
- GetGadgetItemState(): Returns nonzero if the item is selected, zero otherwise. 
- GetGadgetItemText(): Get the content of the given item. 
- GetGadgetState(): Get the index of the selected item or -1 if there is no selected item. 
- GetGadgetText(): Get the content of the selected item. 
- SetGadgetItemData(): store a value with the given item. 
- SetGadgetItemState(): Selects or deselects the given item. 
- SetGadgetItemText(): Set the text of the given item. 
- SetGadgetState(): Change the selected item. If -1 is specified, the selection will be removed. 
- SetGadgetText(): Selects the item with the given text (the text must match exactly). 
and some more from the common list of gadget commands: GadgetX/Y/Width/Height(), Get/SetActiveGadget(), IsGadget(), FreeGadget(), DisableGadget() and so on ...


and you get this events:

Code: Select all

  #PB_EventType_LeftClick 
  #PB_EventType_LeftDoubleClick
  #PB_EventType_RightClick
Now compare that to the list of properties and events for listview (listbox) on .NET

https://msdn.microsoft.com/en-us/librar ... 10%29.aspx

Again, case in point -> https://msdn.microsoft.com/en-us/librar ... 10%29.aspx

In PB the query/setting of gadget attributes and generated events should be seriously beefed up to be able to do much real life programming using just PB commands.
That's why many, many questions in the forum about GUI manipulation are answered by using OS API, and thanks to GadgetID() you can talk to the underlying native object.

Re: determining item at top of Listview gadget

Posted: Mon Apr 06, 2015 10:13 pm
by Blue
luis wrote:[...]The fact is PB wraps the OS native controls on each platform but expose very little of them.
Yes, of course. Computer Science 101.
It's all a matter of semantics, or rather perspective, closely linked to specific points in time. 8)

Looking at one's code in the IDE, we see something remarkably close to English mixed in (for me) with French and other mysterious letter-numbers assemblies, which make sense (at least today!) only to the one who wrote them. In the end, we know it all gets mashed into an indigestible mixture of zeroes and ones, which machines, all consuming electricity, gladly gobble to turn into similar but, oh! so different! outputs. Which won't stop us (meaning me, here, mostly) from forever discussing the fine points of the process...

Gosh the torture we put ourselves through ! :shock:

A similar process is at play with human words on this forum, where we sometimes have to labour so hard to get a simple point across, :cry: that it makes me want, at times, to write in all capital letters. :lol:
luis wrote:[...]

Code: Select all

  #PB_EventType_LeftClick 
  #PB_EventType_LeftDoubleClick
  #PB_EventType_RightClick
Now compare that to the list of properties and events for listview on .NET
https://msdn.microsoft.com/en-us/librar ... -snippet-1
Eloquent comparison, indeed, Luis : 90 public events exposed by .Net against 3 in PB ! :shock:
There's plenty of room left to grow for PB.
It would be interesting to see a statistical analysis of usage frequency for all those events and properties, however.



@RASHAD : Nice streamlining of the code in your second version.
You could also think of moving the GadgetX(0) and GadgetY(0) out of the loop too, replacing them with x y vars, since they require function calls.

  

Re: determining item at top of Listview gadget

Posted: Tue Apr 07, 2015 2:51 am
by RASHAD
Hi luis
You should refer to MS ListBox not ListView
The conclusion you are absolutely 100% right

Re: determining item at top of Listview gadget

Posted: Tue Apr 07, 2015 4:09 am
by Blue
Hello Wizard Rashad .

Suppose you wanted to make use of one of the myriad other properties or events available to a Listview gadget, but not yet implemented by PB, could you extend the existing gadget (as provided by PB) without rewriting the whole thing from scratch ?

Re: determining item at top of Listview gadget

Posted: Tue Apr 07, 2015 11:02 am
by luis
RASHAD wrote:Hi luis
You should refer to MS ListBox not ListView
Hi RASHAD, you are right, thank you.
ListView inherits from ListBox , but ListBox it's the right match for PB ListView (the respective nomenclatures are pretty confusing when confronted).
Edited my post.