[Bug?] SetGadgetFont() on TreeGadget() with icons

Mac OSX specific forum
Christian
Enthusiast
Enthusiast
Posts: 154
Joined: Mon Dec 08, 2003 7:50 pm
Location: Germany

[Bug?] SetGadgetFont() on TreeGadget() with icons

Post by Christian »

Hi,

when using the TreeGadget with icons and a custom set font / font size I observed a small thing: the text is not centered vertically with regard to the icons anymore. It's not a big thing, but it looks somehow ... ugly. (PB 5.51 x64 on macOS Sierra 10.12)

Here is a small example code:

Code: Select all

EnableExplicit

Define WindowEvent.q

LoadFont(0, "Verdana", 12)

If OpenWindow(0, 0, 0, 200, 500, "TreeGadget", #PB_Window_ScreenCentered)
  TreeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
    SetGadgetFont(0, FontID(0))
 
  CreateImage(0, 16, 16)

  If StartDrawing(ImageOutput(0))
    Box(0, 0, OutputWidth(), OutputHeight(), $CC3299)
    StopDrawing()
  EndIf

  CreateImage(1, 16, 16)

  If StartDrawing(ImageOutput(1))
    Box(0, 0, OutputWidth(), OutputHeight(), $7A96E9)
    StopDrawing()
  EndIf
 
  AddGadgetItem(0, -1, "Item", ImageID(0))
  AddGadgetItem(0, -1, "Subitem", ImageID(1), 1)
  SetGadgetItemState(0, 0, #PB_Tree_Expanded)

  Repeat
    WindowEvent = WaitWindowEvent()

    Select WindowEvent
    EndSelect
  Until WindowEvent = #PB_Event_CloseWindow
EndIf
Is this a bug or do I overlook something? Does anyone have an idea for a simple workaround?

Thanks and best regards,
Christian
Last edited by Christian on Mon Jan 02, 2017 6:24 pm, edited 1 time in total.
WilliamL
Addict
Addict
Posts: 1215
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: [Bug?] SetGadgetFont() on TreeGadget() with icons

Post by WilliamL »

Oh, I see what you mean. With the custom icons, the sub items are indented the custom icon width. Looks nice to me but it is not the same as the Help example. I tried it on 5.42LTS and 5.51b1x64 and the sub items are indented the same.

...maybe a bug.. or a feature. :)
MacBook Pro-M1 (2021), Sonoma 14.3.1 (CLT 15.3), PB 6.10b7 M1
Christian
Enthusiast
Enthusiast
Posts: 154
Joined: Mon Dec 08, 2003 7:50 pm
Location: Germany

Re: [Bug?] SetGadgetFont() on TreeGadget() with icons

Post by Christian »

Thanks for your answer, William. But, actually I don't mean the subitem indentation. What I mean is the vertical position of the text, which is not in the center (vertically) of the row height of any item. Here is a picture:

Image

On the left is what purebasic does. On the right, how it should look like. It's only 2 or 3 pixels in this example with a custom font. But in my opinion it looks not nice how PB does it.

So, a bug?
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Bug?] SetGadgetFont() on TreeGadget() with icons

Post by Shardik »

Christian wrote:So, a bug?
It looks indeed like a bug.

You may workaround this bug by using my old workaround for older PB versions (up to 5.11, in 5.24 this is fixed) which were not able to indent icons together with their node text. For your convenience I have taken that example and modified it to match your example and the vertical alignment of the node text now seems to look as you would like it (tested with MacOS 10.6.8 'Snow Leopard' and PB 5.44 x86):

Code: Select all

EnableExplicit

#NSSwitchButton = 3

Define i.I
Define Indentation.CGFloat = 25

Dim ButtonCell.I(0)

ProcedureC CellDisplayCallback(Object.I, Selector.I, OutlineView.I,
  TableColumn.I, Item.I)
  Shared ButtonCell.I()
  Protected Row.I = CocoaMessage(0, GadgetID(0), "rowForItem:", Item)

  If ButtonCell(Row) <> 0
    ProcedureReturn ButtonCell(Row)
  EndIf
EndProcedure

Procedure SetGadgetCallback(GadgetID.I, GadgetCallback.I, Method.S)
  Protected AppDelegate.I
  Protected DelegateClass.I
  Protected PlaceholderList.S

  PlaceholderList = LSet("@:", CountString(Method, ":"), "@")
  AppDelegate = CocoaMessage(0, CocoaMessage(0, 0,
    "NSApplication sharedApplication"), "delegate")
  DelegateClass = CocoaMessage(0, AppDelegate, "class")
  class_addMethod_(DelegateClass, sel_registerName_(Method), GadgetCallback,
    PlaceholderList)
  CocoaMessage(0, GadgetID(GadgetID), "setDelegate:", AppDelegate)
EndProcedure

Procedure AddImageToTreeRow(ImageID.I, Row.I)
  Shared ButtonCell.I()
  Protected ItemText.S
  Protected TableColumn.I

  TableColumn = CocoaMessage(0, CocoaMessage(0, GadgetID(0), "tableColumns"),
    "objectAtIndex:", 0)
  ButtonCell(Row) = CocoaMessage(0, 0, "NSButtonCell new")
  CocoaMessage(0, ButtonCell(Row), "setButtonType:", #NSSwitchButton)
  CocoaMessage(0, ButtonCell(Row), "setImage:", ImageID(ImageID))
  ItemText = GetGadgetItemText(0, Row)
  CocoaMessage(0, ButtonCell(Row), "setTitle:$", @ItemText)
EndProcedure

CreateImage(0, 16, 16)

If StartDrawing(ImageOutput(0))
  Box(0, 0, OutputWidth(), OutputHeight(), $CC3299)
  StopDrawing()
EndIf

CreateImage(1, 16, 16)

If StartDrawing(ImageOutput(1))
  Box(0, 0, OutputWidth(), OutputHeight(), $7A96E9)
  StopDrawing()
EndIf

LoadFont(0, "Verdana", 12)

OpenWindow(0, 270, 100, 140, 100, "Tree")
TreeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
SetGadgetFont(0, FontID(0))

AddGadgetItem(0, -1, "Item", ImageID(0))
AddGadgetItem(0, -1, "Subitem", ImageID(1), 1)
SetGadgetItemState(0, 0, #PB_Tree_Expanded)

; ----- Setup callback for TreeGadget
SetGadgetCallback(0, @CellDisplayCallback(),
  "outlineView:dataCellForTableColumn:item:")

; ----- Add images to specified rows
ReDim ButtonCell(CountGadgetItems(0) - 1)
AddImageToTreeRow(0, 0)
AddImageToTreeRow(1, 1)

; ----- Don't indent indentation marker along with cell contents
CocoaMessage(0, GadgetID(0), "setIndentationMarkerFollowsCell:", #NO)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

; ----- Release NSButtonCell objects
For i = 0 To CountGadgetItems(0) - 1
  If ButtonCell(i) <> 0
    CocoaMessage(0, ButtonCell(i), "release")
  EndIf
Next i
Christian
Enthusiast
Enthusiast
Posts: 154
Joined: Mon Dec 08, 2003 7:50 pm
Location: Germany

Re: [Bug?] SetGadgetFont() on TreeGadget() with icons

Post by Christian »

Thanks for your sample code, Shardik. Unfortunately, it still does not align in fully correctly under macOS Sierra 10.12. It is now shifted a little bit to the top of the row instead of being shifted to the bottom. Apart from that I don't see where the exact position of the text is set in your code. Maybe you can point it out? I'm not so familiar with macOS programming and the Objective C classes and methods. :oops:
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: [Bug?] SetGadgetFont() on TreeGadget() with icons

Post by Shardik »

The easiest workaround without needing any API function should be to move the icon vertically down until it matches your preferred text position by increasing the height of your icon. I have taken your example and added the new constant #yOffset. Simply adjust #yOffset until the text is displayed in your preferred vertical position in relation to your icon:

Code: Select all

EnableExplicit

#yOffset = 3

Define WindowEvent.I

LoadFont(0, "Verdana", 12)

If OpenWindow(0, 270, 100, 140, 100, "Tree")
  TreeGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
  SetGadgetFont(0, FontID(0))
 
  CreateImage(0, 16, 16 + #yOffset, 32, $FFFFFF)

  If StartDrawing(ImageOutput(0))
    Box(0, #yOffset, OutputWidth(), OutputHeight() - #yOffset, $CC3299)
    StopDrawing()
  EndIf

  CreateImage(1, 16, 16 + #yOffset, 32, $FFFFFF)

  If StartDrawing(ImageOutput(1))
    Box(0, #yOffset, OutputWidth(), OutputHeight() - #yOffset, $7A96E9)
    StopDrawing()
  EndIf
 
  AddGadgetItem(0, -1, "Item", ImageID(0))
  AddGadgetItem(0, -1, "Subitem", ImageID(1), 1)
  SetGadgetItemState(0, 0, #PB_Tree_Expanded)

  Repeat
    WindowEvent = WaitWindowEvent()

    Select WindowEvent
    EndSelect
  Until WindowEvent = #PB_Event_CloseWindow
EndIf
Christian
Enthusiast
Enthusiast
Posts: 154
Joined: Mon Dec 08, 2003 7:50 pm
Location: Germany

Re: [Bug?] SetGadgetFont() on TreeGadget() with icons

Post by Christian »

Thanks, Shardik. I think these suggestion will help, until the bug is fixed. :)

Best regards,
Christian
Post Reply