TreeGadget icons

Mac OSX specific forum
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

TreeGadget icons

Post by wombats »

Is it possible to change the TreeGadget so the icons are indented as in the one on the right in the image below? The left is how PureBasic positions them, which looks a bit odd to me.

Image

Thanks.
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: TreeGadget icons

Post by nospam »

What happens when you run the sample code in the help text?

Code: Select all

  If OpenWindow(0, 0, 0, 355, 180, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TreeGadget(0, 10, 10, 160, 160)                                         ; TreeGadget standard
    TreeGadget(1, 180, 10, 160, 160, #PB_Tree_CheckBoxes | #PB_Tree_NoLines)  ; TreeGadget with Checkboxes + NoLines
    For ID = 0 To 1
      For a = 0 To 10
        AddGadgetItem (ID, -1, "Normal Item "+Str(a), 0, 0) ; if you want to add an image, use
        AddGadgetItem (ID, -1, "Node "+Str(a), 0, 0)        ; ImageID(x) as 4th parameter
        AddGadgetItem(ID, -1, "Sub-Item 1", 0, 1)    ; These are on the 1st sublevel
        AddGadgetItem(ID, -1, "Sub-Item 2", 0, 1)
        AddGadgetItem(ID, -1, "Sub-Item 3", 0, 1)
        AddGadgetItem(ID, -1, "Sub-Item 4", 0, 1)
        AddGadgetItem (ID, -1, "File "+Str(a), 0, 0) ; sublevel 0 again
      Next
    Next
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: TreeGadget icons

Post by wombats »

It has no icons, so it looks fine:

Image
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: TreeGadget icons

Post by nospam »

wombats wrote:...it looks fine:
No, it doesn't look fine. The checkboxes should indent with the gadget's levels. If the same type of control looks different in other applications then perhaps you need to report a bug. If the same type of control exhibits the same behaviour in other applications then it's probably the Mac way of drawing the gadget, though I'm dubious on that latter point.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: TreeGadget icons

Post by Shardik »

wombats wrote:Is it possible to change the TreeGadget so the icons are indented as in the one on the right in the image below?
With my workaround below you can obtain this result:

Image

My workaround works as follows:

1. A callback is defined in the procedure SetGadgetCallback(). In this procedure the callback CellDisplayCallback() is setup for the TreeGadget as a delegate for the method "outlineView:dataCellForTableColumn:item:". This method is called for every display of a tree node and is now routed to CellDisplayCallback().

2. For each node that should display an image, the procedure AddImageToRow() has to be called. In this procedure a NSButtonCell object is allocated for the specified row/node. It contains the image and the text for the node. Each NSButtonCell object is stored in the array ButtonCell().

3. Every time when a tree node has to be displayed, the callback CellDisplayCallback() is called. For each tree node containing an image, the ButtonCell object from the array ButtonCell() is returned containing the image and the text for the node to be displayed. An array element for a node without an image contains 0 and so 0 is returned by the callback, so in this case no change is taking place.

Code: Select all

EnableExplicit

#NSSwitchButton = 3

ImportC ""
  sel_registerName(MethodName.P-ASCII)
  class_addMethod(Class.I, Selector.I, Implementation.I, Types.P-ASCII)
EndImport

Dim ButtonCell.I(0)
Define i.I

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

Procedure LoadIcons(IconNameList.S)
  Protected i.I
  Protected *ImageBuffer
  Protected ImageSize.I

  If OpenPack(0, #PB_Compiler_Home + "Themes/SilkTheme.zip")
    *ImageBuffer = AllocateMemory(1024)
    
    If ExaminePack(0)
      For i = 0 To CountString(IconNameList, "+")
        ImageSize = UncompressPackMemory(0, *ImageBuffer, MemorySize(*ImageBuffer),
          StringField(IconNameList, i + 1, "+"))
        
        If ImageSize > 0
          CatchImage(i, *ImageBuffer, ImageSize)
        EndIf
      Next i
    EndIf
    
    FreeMemory(*ImageBuffer)
  EndIf
EndProcedure

UsePNGImageDecoder()
UseZipPacker()

LoadIcons("folder.png+page_white.png")

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

AddGadgetItem (0, -1, "Parent")

For i = 1 To 3
  AddGadgetItem (0, -1, "Child" + Str(i), 0, 1)
Next i

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)
AddImageToTreeRow(1, 2)
AddImageToTreeRow(1, 3)

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
wombats
Enthusiast
Enthusiast
Posts: 716
Joined: Thu Dec 29, 2011 5:03 pm

Re: TreeGadget icons

Post by wombats »

Thanks for coming up with this! I hope it finds its way into PureBasic officially.
Post Reply