Scrollbar width?

Mac OSX specific forum
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Scrollbar width?

Post by Poshu »

I'm looking for a way to get the width of a vertical scrollbar in a scrollarea (pretty much like a getsystemmetrics(#SM_CXVSCROLL) for windows). Does anyone know how to do that?
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Re: Scrollbar width?

Post by uwekel »

I'm not sure whether it works on MacOS, but on Linux it goes that way:

Code: Select all

If OpenWindow(0, 0, 0, 200, 200, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ScrollBarGadget(0, 10, 10, 0, 100, 0, 100, 10, #PB_ScrollBar_Vertical)
  w = GadgetWidth(0, #PB_Gadget_RequiredSize)
  ResizeGadget(0, #PB_Ignore, #PB_Ignore, w, #PB_Ignore)
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: Scrollbar width?

Post by Poshu »

nop, debug w returns 0 on mavericks T_T; (I did not know of #PB_Gadget_RequiredSize though, it might become quite useful to me, thanks)

I've hidden the horizontal scrollingbar using setHasHorizontalScroller: , that will do for now.
User avatar
Shardik
Addict
Addict
Posts: 2076
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Scrollbar width?

Post by Shardik »

It is much more difficult to obtain the width of a vertical scrollbar than I had expected. It is easy with Snow Leopard but beginning with Lion the scrollbar default display changed to an overlay with the scrollbar being only visible during the scrolling process.

Therefore I check for the scrollbar style and return 0 as scrollbar width for the overlay style and the actual width if the vertical scrollbar is always visible. Unfortunately there exists a catch: there are 3 options which you can select in System Preferences / General / Show scroll bars:
- Automatically based on mouse or trackpad
- When scrolling
- Always

When selecting "Automatically based on mouse or trackpad", the NSScroller object reports NSScrollerStyleOverlay although PB's ScrollAreaGadget displays an always visible vertical scrollbar (with different width as with option "Always"!) and I found no API function to detect whether the option "Automatically based on mouse or trackpad" is enabled...

The example code below displays one ScrollAreaGadget with a normal and one with a small scrollbar and displays a TextGadget underneath the respective ScrollAreaGadget in the width of the usable content. The example code was tested on Snow Leopard and Mountain Lion on an iMac summer 2010. Unfortunately I have no Mac with retina display to test with.

Code: Select all

EnableExplicit

#NSScrollerKnobSlot = 6
#NSScrollerStyleOverlay = 1
#NSSmallControlSize = 1

Procedure GetVerticalScrollBarWidth(ScrollAreaGadgetID.I)
  Protected Rect.NSRect
  Protected VerticalScrollBar.I
  Protected VerticalScrollBarWidth.I

  VerticalScrollBar = CocoaMessage(0, GadgetID(ScrollAreaGadgetID), "verticalScroller")
  CocoaMessage(@Rect, VerticalScrollBar, "rectForPart:", #NSScrollerKnobSlot)
  VerticalScrollBarWidth = Int(Rect\size\width)

  If OSVersion() > #PB_OS_MacOSX_10_6
    If CocoaMessage(0, VerticalScrollBar, "scrollerStyle") = #NSScrollerStyleOverlay
      VerticalScrollBarWidth = 0
    Else
      VerticalScrollBarWidth + 1
    EndIf
  EndIf

  ProcedureReturn VerticalScrollBarWidth
EndProcedure

Define ContentWidth.I
Define VerticalScrollBarWidth.I

OpenWindow(0, 270, 100, 240, 210, "ScrollAreaGadget")
ScrollAreaGadget(0, 10, 10, WindowWidth(0) - 20, 70, WindowWidth(0) - 37, 200)
SetGadgetColor(0, #PB_Gadget_BackColor, $6BB7BD)
VerticalScrollBarWidth = GetVerticalScrollBarWidth(0)
ContentWidth = GadgetWidth(0) - VerticalScrollBarWidth
TextGadget(1, 10, 10, GadgetWidth(0), 25, "Width of vertical scroll bar: " +
  Str(VerticalScrollBarWidth))
CloseGadgetList()

TextGadget(2, 10, 81, ContentWidth, 18, "Usable content width: " + Str(ContentWidth), #PB_Text_Center)
SetGadgetColor(2, #PB_Gadget_BackColor, $6BB7BD)

ScrollAreaGadget(3, 10, 110, WindowWidth(0) - 20, 70, WindowWidth(0) - 33, 200)
SetGadgetColor(3, #PB_Gadget_BackColor, $6BB7BD)
CocoaMessage(0, CocoaMessage(0, GadgetID(3), "verticalScroller"),
  "setControlSize:", #NSSmallControlSize)
VerticalScrollBarWidth = GetVerticalScrollBarWidth(3)
ContentWidth = GadgetWidth(3) - VerticalScrollBarWidth
TextGadget(4, 10, 10, GadgetWidth(3), 25, "Width of vertical scroll bar: " +
  Str(VerticalScrollBarWidth))
CloseGadgetList()

TextGadget(5, 10, 181, ContentWidth, 18, "Usable content width: " + Str(ContentWidth), #PB_Text_Center)
SetGadgetColor(5, #PB_Gadget_BackColor, $6BB7BD)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by Shardik on Sun Jan 05, 2014 7:55 pm, edited 1 time in total.
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: Scrollbar width?

Post by Poshu »

Thanks Shardik!
with Lion the scrollbar default display changed to an overlay with the scrollbar being only visible during the scrolling process.
Not here on maverick, the scrollbar is always displayed (and I'd rather have it hide itself T_T)
User avatar
Shardik
Addict
Addict
Posts: 2076
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Scrollbar width?

Post by Shardik »

Poshu wrote:Not here on maverick, the scrollbar is always displayed (and I'd rather have it hide itself T_T)
What are your settings in System Preferences / General / Show scroll bar ?

Oh, I found out that your described scrollbar behaviour in Mavericks seems to be a known consequence of changes Apple has implemented in Mavericks:
http://stackoverflow.com/questions/2000 ... -mavericks
Chris Mason wrote:Apple has apparently changed NSScrollView in 10.9 so it draws in layers -- for special scrolling effects, or speed, or maybe just to give us something to do, because we're all so under-worked.
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: Scrollbar width?

Post by Poshu »

Shardik wrote: What are your settings in System Preferences / General / Show scroll bar ?
Yup, default setting is now always shown (too bad).

Any way to make it hidden when not used on a gadget? I was hopping setAutohidesScrollers: would, but it doesn't (or I can't use it...)
Post Reply