TreeGadget icons

Mac OSX specific forum
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