ListIconGadget with Icon and Text example
Posted: Mon Jun 23, 2025 9:39 pm
Here is an example of a ListIconGadget under macOS with icons with variable size.
Unfortunately, the constrain does not work as it should or as I expect it to. If anyone has a suggestion for improvement, please post it.
Unfortunately, the constrain does not work as it should or as I expect it to. If anyone has a suggestion for improvement, please post it.
Code: Select all
Global mainWindow, NSTableView
CreateImage(0, 32, 32, 32, $FF0000)
CreateImage(1, 32, 32, 32, $00FF00)
CreateImage(2, 32, 32, 32, $0000FF)
ProcedureC.i iconTextTableCellView(obj.i, sel.i, tableView.i, columnObject.i, row.i)
#NSUserInterfaceLayoutOrientationVertical = 1
#NSLayoutAttributeCenterX = 9
#NSTextAlignmentCenter = 2
frameRect.NSRect
frameRect\origin\x = 0
frameRect\origin\y = 0
frameRect\size\width = 100
frameRect\size\height = 60
view = CocoaMessage(0, CocoaMessage(0, 0, "NSTableCellView alloc"), "initWithFrame:@", @frameRect)
If view
*utf8 = UTF8("IconTextCell")
CocoaMessage(0, view, "setIdentifier:", CocoaMessage(0, 0, "NSString stringWithUTF8String:", *utf8))
frameRect\size\width = 32
frameRect\size\height = 32
iconView = CocoaMessage(0, CocoaMessage(0, 0, "NSImageView alloc"), "initWithFrame:@", @frameRect)
CocoaMessage(0, iconView, "setImage:", ImageID(row))
CocoaMessage(0, iconView, "setTranslatesAutoresizingMaskIntoConstraints:", #YES)
frameRect\size\width = 80
frameRect\size\height = 20
textFieldLabel = CocoaMessage(0, CocoaMessage(0, 0, "NSTextField alloc"), "initWithFrame:@", @frameRect)
cellContent.s = GetGadgetItemText(0, row, column)
CocoaMessage(0, textFieldLabel, "setStringValue:$", @cellContent)
CocoaMessage(0, textFieldLabel, "setAlignment:", #NSTextAlignmentCenter)
CocoaMessage(0, textFieldLabel, "setBordered:", #NO)
CocoaMessage(0, textFieldLabel, "setBezeled:", #NO)
CocoaMessage(0, textFieldLabel, "setEditable:", #YES)
CocoaMessage(0, textFieldLabel, "setSelectable:", #YES)
CocoaMessage(0, textFieldLabel, "setDrawsBackground:", #NO)
fontSize.CGFloat = 12.0
CocoaMessage(0, textFieldLabel, "setFont:", CocoaMessage(0, 0, "NSFont systemFontOfSize:@", @fontSize) )
frameRect\origin\y = -10
frameRect\size\width = 100
frameRect\size\height = 70
stackView = CocoaMessage(0, CocoaMessage(0, 0, "NSStackView alloc"), "initWithFrame:@", @frameRect)
CocoaMessage(0, stackView, "setOrientation:", #NSUserInterfaceLayoutOrientationVertical)
CocoaMessage(0, stackView, "setAlignment:", #NSLayoutAttributeCenterX)
spacing.CGFloat = 4.0
CocoaMessage(0, stackView, "setSpacing:@", @spacing)
CocoaMessage(0, stackView, "addArrangedSubview:", iconView)
CocoaMessage(0, stackView, "addArrangedSubview:", textFieldLabel)
CocoaMessage(0, stackView, "setTranslatesAutoresizingMaskIntoConstraints:", #YES)
CocoaMessage(0, view, "addSubview:", stackView)
Define topAnchor = CocoaMessage(0, stackView, "topAnchor")
Define bottomAnchor = CocoaMessage(0, stackView, "bottomAnchor")
Define leadingAnchor = CocoaMessage(0, stackView, "leadingAnchor")
Define trailingAnchor = CocoaMessage(0, stackView, "trailingAnchor")
Define selfTopAnchor = CocoaMessage(0, view, "topAnchor")
Define selfBottomAnchor = CocoaMessage(0, view, "bottomAnchor")
Define selfLeadingAnchor = CocoaMessage(0, view, "leadingAnchor")
Define selfTrailingAnchor= CocoaMessage(0, view, "trailingAnchor")
tValue.CGFloat = 4.0
bValue.CGFloat = -4.0
lValue.CGFloat = 4.0
rValue.CGFloat = -4.0
Define cTop = CocoaMessage(0, topAnchor, "constraintEqualToAnchor:", selfTopAnchor, "constant:@", @tValue)
Define cBottom = CocoaMessage(0, bottomAnchor, "constraintEqualToAnchor:", selfBottomAnchor, "constant:@", @bValue)
Define cLeading = CocoaMessage(0, leadingAnchor, "constraintEqualToAnchor:", selfLeadingAnchor, "constant:@", @lValue)
Define cTrailing = CocoaMessage(0, trailingAnchor, "constraintEqualToAnchor:", selfTrailingAnchor, "constant:@", @rValue)
constraintsArray = CocoaMessage(0, 0, "NSMutableArray array")
CocoaMessage(0, constraintsArray, "addObject:", cTop)
CocoaMessage(0, constraintsArray, "addObject:", cBottom)
CocoaMessage(0, constraintsArray, "addObject:", cLeading)
CocoaMessage(0, constraintsArray, "addObject:", cTrailing)
CocoaMessage(0, 0, "NSLayoutConstraint activateConstraints:", constraintsArray)
EndIf
ProcedureReturn view
EndProcedure
class.i = objc_allocateClassPair_(objc_getClass_("NSObject"), "MyClass", 0)
selector.i = sel_registerName_("tableView:viewForTableColumn:row:")
class_addMethod_(class, selector, @iconTextTableCellView(), "@@:@@@")
objc_registerClassPair_(class)
width = 450 : height = 560
mainWindow = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListIconGadget(0, 10, 10, 110, height - 20, "objects", 110)
AddGadgetItem(0, -1, "ohne Titel 1")
AddGadgetItem(0, -1, "ohne Titel 2")
AddGadgetItem(0, -1, "ohne Titel 3")
NSTableView = GadgetID(0)
columnWidth.CGFloat = 70
CocoaMessage(0, NSTableView, "setRowHeight:@", @columnWidth)
column = CocoaMessage(0, CocoaMessage(0, GadgetID(GadgetID), "tableColumns"), "objectAtIndex:", 0)
headerCell = CocoaMessage(0, column, "headerCell")
CocoaMessage(0, headerCell, "setAlignment:", #NSTextAlignmentCenter)
myDelegate = CocoaMessage(0, 0, "MyClass new")
CocoaMessage(0, NSTableView, "setDelegate:", myDelegate)
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Menu
Select EventMenu()
Case #PB_Menu_Quit
Break
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case 0
If EventType() = #PB_EventType_LeftDoubleClick
Debug GetGadgetItemText(0, GetGadgetState(0) )
EndIf
EndSelect
EndSelect
ForEver