Page 1 of 1

ListIconGadget

Posted: Thu Jul 10, 2014 7:46 pm
by spacebuddy
Is there anyway to make the ListIconGadget editable so I can type stuff in.

Re: ListIconGadget

Posted: Fri Jul 11, 2014 6:03 am
by wilbert
There's no way without knowing exactly how PB does things internally.

Re: ListIconGadget

Posted: Wed Jul 16, 2014 2:32 pm
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

Re: ListIconGadget

Posted: Wed Jul 16, 2014 3:44 pm
by spacebuddy
Thanks, that works :D

Re: ListIconGadget

Posted: Wed Jul 16, 2014 7:14 pm
by Danilo
Very nice, thanks Shardik!

Re: ListIconGadget

Posted: Fri Nov 14, 2014 2:02 pm
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?

Re: ListIconGadget

Posted: Fri Nov 14, 2014 7:08 pm
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).

Re: ListIconGadget

Posted: Fri Nov 14, 2014 7:17 pm
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.

Re: ListIconGadget

Posted: Sun Nov 16, 2014 9:09 pm
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...