Get/move scrollbar - api

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Get/move scrollbar - api

Post by WilliamL »

I can get the scrollbar position (at least the top line in the gadget) but I can't seem to figure out the correct form for RevealDataBrowserItem.

If you can correct this for me I will update this code.

Code: Select all

;     OSStatus GetDataBrowserScrollPosition (
;        ControlRef browser,
;        UInt32 *top,
;        UInt32 *left
;     );
;     
;     
;     OSStatus RevealDataBrowserItem (
;        ControlRef browser,
;        DataBrowserItemID item,
;        DataBrowserPropertyID propertyID, (kDataBrowserNoItem)
;        DataBrowserRevealOptions options
;     );
;     Reveal Options
;     Specify how To position an item in a Data browser.
;     typedef UInt8 DataBrowserRevealOptions;
;     enum {
;        kDataBrowserRevealOnly = 0,
;        kDataBrowserRevealAndCenterInView = 1 << 0,
;        kDataBrowserRevealWithoutSelecting = 1 << 1
;     };
;     Constants
;     kDataBrowserRevealOnly
;     Move the content of the Data browser As little As possible To make the item visible, And show the item in a selected state.
;     Available in Mac OS X v10.0 And later.
;     Declared in HIDataBrowser.h.
;     kDataBrowserRevealAndCenterInView
;     Reveal the item so that, If possible, the item is centered in the Data browser.
;     Available in Mac OS X v10.0 And later.
;     Declared in HIDataBrowser.h.
;     kDataBrowserRevealWithoutSelecting
;     Reveal the item but do Not Select it.
;     Available in Mac OS X v10.0 And later.
;     Declared in HIDataBrowse

ImportC ""
    GetDataBrowserScrollPosition(Control.i, *top.UNICODE, *left.UNICODE)
    RevealDataBrowserItem(Control.i, ItemID.i, PropertyID.i, Options.i) ; see above
EndImport

Define top.u,left.u
Define lh=19 ; line height in listviewgadget (a guess)
w=350
h=200
If OpenWindow(0, 100, 100, w,h, "Scrollbar position", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListViewGadget(0, 5, 5, w-5,h-5)
  AddGadgetColumn(0, 1, "", 200)
  AddGadgetItem(0, -1,"Click on number to see scrollbar position")
  For X = 1 To 50
    AddGadgetItem(0, -1, Str(x))
  Next

  
    Repeat
        Event = WaitWindowEvent()
        Select event
        Case #PB_Event_Gadget
            Select EventGadget()
            Case 0
                GetDataBrowserScrollPosition(GadgetID(0), @top.u, @left.u)
                Debug Str(top)+"/"+Str(lh)+"="+Str(top/lh)+" line at top of scrollbar (start at 0)"
                ;move the scrollbar to center of page?
                RevealDataBrowserItem(GadgetID(0), top.u, kDataBrowserNoItem, kDataBrowserRevealAndCenterInView)
            EndSelect
        EndSelect
    Until Event = #PB_Event_CloseWindow
EndIf
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Get/move scrollbar - api

Post by Shardik »

WilliamL wrote:I can't seem to figure out the correct form for RevealDataBrowserItem
That's an easy one. In your above code you haven't set the values for the variables
kDataBrowserNoItem and kDataBrowserRevealAndCenterInView. Therefore
they are initialized with 0. For kDataBrowserNoItem this happens to be the correct
value but not for kDataBrowserRevealAndCenterInView. You can lookup the
correct values in the Data Browser Reference in chapter Constants and sub chapter
No Item Constant where you'll find that the constant kDataBrowserNoItem is
defined as
Data Browser Reference wrote:enum {
kDataBrowserNoItem = 0L
};
and in the sub chapter Reveal Options you'll find the following definitions:
Data Browser Reference wrote:typedef UInt8 DataBrowserRevealOptions;
enum {
kDataBrowserRevealOnly = 0,
kDataBrowserRevealAndCenterInView = 1 << 0,
kDataBrowserRevealWithoutSelecting = 1 << 1
};
You should define these constants in PureBasic in this way:

Code: Select all

#kDataBrowserNoItem = 0

Enumeration
  #kDataBrowserRevealOnly
  #kDataBrowserRevealAndCenterInView
  #kDataBrowserRevealWithoutSelecting
EndEnumeration
I have written a small demonstration program which centers the selected item number
in the center of the ListView and selects it. You may try out the the other two constants
of the enumeration in the RevealDataBrowserItem() function and observe their effect: :wink:

Code: Select all

ImportC ""
  GetDataBrowserScrollPosition(ControlRef.L, *top.UNICODE, *left.UNICODE)
  RevealDataBrowserItem(ControlRef.L, ItemID.L, PropertyID.L, RevealOptions.L)
EndImport

#kDataBrowserNoItem = 0

Enumeration
  #kDataBrowserRevealOnly
  #kDataBrowserRevealAndCenterInView
  #kDataBrowserRevealWithoutSelecting
EndEnumeration

OpenWindow(0, 0, 0, 150, 140, "ListView", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListViewGadget(0, 5, 5, WindowWidth(0) - 10, WindowHeight(0) - 40)
SpinGadget(1, 5, WindowHeight(0) - 30, 50, 25, 1, 50)
SetGadgetState(1, 1)
SetGadgetText(1, Str(GetGadgetState(1)))
ButtonGadget(2, 70, WindowHeight(0) - 30, 70, 25, "Display")

For i= 1 To 50
  AddGadgetItem(0, -1, "Item #" + Str(i))
Next

Repeat
  Event = WaitWindowEvent()

  If Event = #PB_Event_Gadget
    Select EventGadget()
      Case 1
        SetGadgetText(1, Str(GetGadgetState(1)))
      Case 2
        RevealDataBrowserItem(GadgetID(0), GetGadgetState(1), #kDataBrowserNoItem, #kDataBrowserRevealAndCenterInView)
    EndSelect
  EndIf
Until Event = #PB_Event_CloseWindow
However if the choosen item should only be visible but need not to be in the center,
you may use the following simple example without any API:

Code: Select all

OpenWindow(0, 0, 0, 150, 140, "ListView", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListViewGadget(0, 5, 5, WindowWidth(0) - 10, WindowHeight(0) - 40)
SpinGadget(1, 5, WindowHeight(0) - 30, 50, 25, 1, 50)
SetGadgetState(1, 1)
SetGadgetText(1, Str(GetGadgetState(1)))
ButtonGadget(2, 70, WindowHeight(0) - 30, 70, 25, "Display")

For i= 1 To 50
  AddGadgetItem(0, -1, "Zeile " + Str(i))
Next

Repeat
  WindowEvent = WaitWindowEvent()

  If WindowEvent = #PB_Event_Gadget
    Select EventGadget()
      Case 1
        SetGadgetText(1, Str(GetGadgetState(1)))
      Case 2
        SetGadgetState(0, GetGadgetState(1) - 1)
    EndSelect
  EndIf
Until WindowEvent = #PB_Event_CloseWindow
WilliamL
Addict
Addict
Posts: 1252
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Get/move scrollbar - api

Post by WilliamL »

Thanks Shardik!

It looks like I was close. For some reason I thought the constants were already defined by the system. I'm studying your info and I think it will help me the next time. :)
MacBook Pro-M1 (2021), Sequoia 15.4, PB 6.20
Post Reply