ListIconGadget

Mac OSX specific forum
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

ListIconGadget

Post by spacebuddy »

Is there anyway to make the ListIconGadget editable so I can type stuff in.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: ListIconGadget

Post by wilbert »

There's no way without knowing exactly how PB does things internally.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: ListIconGadget

Post by Shardik »

spacebuddy wrote:Is there anyway to make the ListIconGadget editable so I can type stuff in.
A left click onto any cell (except header cells) makes it editable. Modify the cell's content and end this action with <Enter> or <Tab> or a click onto another cell. The modified text will be copied from the edit field into the ListIcon cell.

I have tested this code example successfully on MacOS X 10.6.8 (Snow Leopard) and MacOS X 10.9.4 (Mavericks) in both ASCII and Unicode mode with PB 5.22 x86 and x64 and PB 5.30 x86 Beta 7.

Code: Select all

EnableExplicit

ImportC ""
  sel_registerName(MethodName.S)
  class_addMethod(Class.I, Selector.I, Implementation.I, Types.S)
EndImport

Procedure.S ConvertToUTF8(String.S)
  Protected UTF8String.S = Space(StringByteLength(String))
  PokeS(@UTF8String, String, -1, #PB_UTF8)
  ProcedureReturn UTF8String
EndProcedure

ProcedureC EditingFinishedCallback(Object.I, Selector.I, Notification.I)
  Protected EditedCell.I
  Protected EditedColumn.I = CocoaMessage(0, GadgetID(0), "editedColumn")
  Protected EditedRow.I = CocoaMessage(0, GadgetID(0), "editedRow")
  Protected EditedText.S

  EditedCell = CocoaMessage(0, Notification, "object")
  EditedText = PeekS(CocoaMessage(0, CocoaMessage(0, EditedCell, "stringValue"),
    "UTF8String"), -1, #PB_UTF8)
  SetGadgetItemText(0, EditedRow, EditedText, EditedColumn)
EndProcedure

Define CursorLocation.NSPoint
Define AppDelegate.I = CocoaMessage(0, CocoaMessage(0, 0,
  "NSApplication sharedApplication"), "delegate")
Define DelegateClass.I = CocoaMessage(0, AppDelegate, "class")
Define NotificationCenter.I = CocoaMessage(0, 0,
  "NSNotificationCenter defaultCenter")
Define SelectedColumn.I
Define Selector.I = sel_registerName(ConvertToUTF8("textDidEndEditing:"))

OpenWindow(0, 200, 100, 430, 95, "Editable ListIconGadget demo")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, "Name", 110)
AddGadgetColumn(0, 1, "Address", 292)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ + "12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(0, -1, "Didi Foundit" + #LF$ + "321 Logo Drive, Mouse House, Downtown")
CocoaMessage(0, GadgetID(0), "setSelectionHighlightStyle:", -1)
class_addMethod(DelegateClass, Selector, @EditingFinishedCallback(), "v@:@")
CocoaMessage(0, NotificationCenter,
  "addObserver:", AppDelegate,
  "selector:", Selector,
  "name:$", @"NSControlTextDidEndEditingNotification",
  "object:", GadgetID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
        CursorLocation\x = WindowMouseX(0)
        CursorLocation\y = WindowHeight(0) - WindowMouseY(0)
        CocoaMessage(@CursorLocation, GadgetID(0),
          "convertPoint:@", @CursorLocation, "fromView:", 0)
        SelectedColumn = CocoaMessage(0, GadgetID(0),
          "columnAtPoint:@", @CursorLocation)
        CocoaMessage(0, GadgetID(0),
          "editColumn:", SelectedColumn,
          "row:", GetGadgetState(0),
          "withEvent:", 0,
          "select:", #YES)
      EndIf
  EndSelect
ForEver
spacebuddy
Enthusiast
Enthusiast
Posts: 347
Joined: Thu Jul 02, 2009 5:42 am

Re: ListIconGadget

Post by spacebuddy »

Thanks, that works :D
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: ListIconGadget

Post by Danilo »

Very nice, thanks Shardik!
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: ListIconGadget

Post by Lebostein »

It is possible to use pseudotypes

Code: Select all

ImportC ""
  sel_registerName(MethodName.p-utf8)
  class_addMethod(Class.I, Selector.I, Implementation.I, Types.p-utf8)
EndImport
instead of

Code: Select all

ImportC ""
  sel_registerName(MethodName.S)
  class_addMethod(Class.I, Selector.I, Implementation.I, Types.S)
EndImport
to remove the ConvertToUTF8() function?
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: ListIconGadget

Post by Shardik »

Yes, now the procedure ConvertToUTF8() isn't necessary anymore because Fred has fixed a bug in P-UTF8 in PB 5.24 and PB 5.31 for MacOS. In older PB versions ConvertToUTF8() is still necessary so that I won't change my code example from above as it is working fine in both older and current PB versions (x86 and x64 PB compiler and in both ASCII and Unicode mode).
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: ListIconGadget

Post by WilliamL »

Hey, that's pretty cool!

Can it be made to work with a ListView gadget?

Hmm, I'm thinking of the possibilities. :)

[later]
I just tried it and it seems to work fine using ListView.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: ListIconGadget

Post by Shardik »

William,

the ListViewGadget() and the ListIconGadget() in PB for MacOS X are utilising the same underlying Cocoa framework control: an NSTableView. So in essence the ListViewGadget is a ListIconGadget with only one column and editing a cell therefore should work the same in both gadgets.

If you need to know the underlying Cocoa control of a PB gadget you may take a look into this table...
Post Reply