Page 1 of 2

editing a tree gadget

Posted: Thu Sep 25, 2014 1:27 pm
by sirrab
Hi All,

Can anyone tell me if it is possible to edit tree gadget items? IE have a button to press to edit the selected tree gadget item :)

Thanks for reading :)

Craig

Re: editing a tree gadget

Posted: Thu Sep 25, 2014 3:28 pm
by Shardik
For Windows you may take a look into this almost 12 year old tutorial from freak (updated for PB 5.20)... :wink:

Re: editing a tree gadget

Posted: Thu Sep 25, 2014 5:43 pm
by TI-994A
sirrab wrote:Can anyone tell me if it is possible to edit tree gadget items? IE have a button to press to edit the selected tree gadget item...
Hi Craig. As Shardik had pointed out, freak's method is ultimate. However, if you might prefer a non-API, cross-platform solution, try this one:

Code: Select all

;========================================================
; A quick & dirty method for editable TreeGadget() items
;      
; a cross-platform solution by TI-994A
;
; 25th September 2014 - free to use, improve, share...
;========================================================

Enumeration 
  #mainWindow   
  #tree1
  #text1
EndEnumeration

;***************************************************
;a fully contained, ready-to-use TreeGadget() editor
;***************************************************
Procedure ediTree(parentWindow, treeGadgetNo, treeText.s)
  Enumeration #PB_Compiler_EnumerationValue
    #treeEditor
    #teString
  EndEnumeration
  
  DisableWindow(parentWindow, #True)
  OpenWindow(#treeEditor, DesktopMouseX(), DesktopMouseY(), 100, 20, "", 
             #PB_Window_BorderLess, WindowID(parentWindow))
  AddKeyboardShortcut(#treeEditor, #PB_Shortcut_Return, 1)
  AddKeyboardShortcut(#treeEditor, #PB_Shortcut_Escape, 2)
  StringGadget(#teString, 0, 0, 100, 20, treeText)
  SetGadgetColor(#teString, #PB_Gadget_BackColor, RGB(255, 255, 0))
  SetGadgetColor(#teString, #PB_Gadget_FrontColor, RGB(100, 0, 0))
  SetActiveGadget(#teString)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            If Trim(GetGadgetText(#teString)) <> ""
              SetGadgetText(treeGadgetNo, Trim(GetGadgetText(#teString)))
            EndIf
            escaped = 1
          Case 2
            escaped = 1
        EndSelect
    EndSelect  
  Until escaped
  DisableWindow(parentWindow, #False)
  CloseWindow(#treeEditor)
EndProcedure

;*********
;demo code
;*********
wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(#mainWindow, 0, 0, 180, 230, "TreeGadget", wFlags)
  TextGadget(#text1, 10, 10, 160, 40, "Double-click tree item to edit," + 
                                      #CRLF$ + "ENTER to set, ESC to cancel.")
  TreeGadget(#tree1, 10, 45, 160, 180)
  For populate = 0 To 9
    Read.s dataStr$
    AddGadgetItem (#tree1, -1, dataStr$)
  Next
  Repeat
    Select WaitWindowEvent() 
      Case #PB_Event_CloseWindow
        appQuit = 1
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #tree1
            Select EventType()
              Case #PB_EventType_LeftDoubleClick
              ;****************************************************
              ; simply call this as a function for any TreeGadget()
              ;****************************************************
                ediTree(#mainWindow, #tree1, GetGadgetText(#tree1))
              ;****************************************************
            EndSelect        
        EndSelect
    EndSelect
  Until appQuit = 1 
EndIf

DataSection
  Data.s "Albert Einstein", "Isaac Newton", "Charles Darwin", "Guglielmo Marconi", "Benjamin Franklin"
  Data.s "Galileo Galilei", "Leonardo da Vinci", "René Descartes", "Thomas Edison", "Alexander Graham Bell"
EndDataSection
Perhaps not the best implementation, but it's simple, and it works. :D

Re: editing a tree gadget

Posted: Thu Sep 25, 2014 6:19 pm
by RASHAD
Hi
- Ctrl - E to edit the item
- Esc to cancel Edit
- Return to finalize edit ( StringGadget)

Color not supported in MAC

Code: Select all

  If OpenWindow(0, 0, 0, 350, 300, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0,10,10,150,20,"")
    TreeGadget(1, 10, 40, 160, 250)                                         ; TreeGadget standard
    TreeGadget(2, 180, 40, 160, 250, #PB_Tree_CheckBoxes | #PB_Tree_NoLines)  ; TreeGadget with Checkboxes + NoLines
    For ID = 1 To 2
      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
    AddKeyboardShortcut(0,#PB_Shortcut_Control|#PB_Shortcut_E,10)
    AddKeyboardShortcut(0,#PB_Shortcut_Return,20)
    AddKeyboardShortcut(0,#PB_Shortcut_Escape,30)

  Repeat
    Select WaitWindowEvent()
       Case #PB_Event_CloseWindow
              Quit = 1
              
       Case #PB_Event_Menu
             Select EventMenu()
                  Case 10
                          If GetActiveGadget() = 1
                              Act = GetGadgetState(1)
                              SetGadgetItemColor(1,Act,#PB_Gadget_BackColor,$FF9933)
                              SetGadgetItemColor(1,Act,#PB_Gadget_FrontColor ,#White)
                              SetActiveGadget(0)
                          EndIf
                          
                  Case 20
                         If GetActiveGadget() = 0
                             SetGadgetItemColor(1,Act,#PB_Gadget_BackColor,#White)
                             SetGadgetItemColor(1,Act,#PB_Gadget_FrontColor ,#Black)
                             SetGadgetItemText(1,Act,GetGadgetText(0))
                             SetGadgetText(0,"")
                             SetActiveGadget(1)
                         EndIf
                         
                  Case 30
                          If GetActiveGadget() = 0
                              SetGadgetText(0,"")
                              SetActiveGadget(1)
                          EndIf

             EndSelect
             
       Case #PB_Event_Gadget
              Select EventGadget()
                       Case 0
              EndSelect
               
    EndSelect
  Until Quit = 1
  EndIf


Re: editing a tree gadget

Posted: Thu Sep 25, 2014 7:16 pm
by TI-994A
I've streamlined my earlier solution to require only two parameters instead of three, and to do away with the enumeration block. To implement it in any project that requires editing capabilities for the TreeGadget(), simply add this procedure, and then call it with the tree gadget number, and its parent window number.

just add this procedure to your project:

Code: Select all

;=============================================================
; fully self-contained and ready to use - no changes required.
; just drop into your project and call ediTree(win#, tree#)
;=============================================================
Procedure ediTree(parentWindow, treeGadgetNo)
  DisableWindow(parentWindow, #True)
  treeText.s = GetGadgetText(treeGadgetNo)
  
  treeEditor = OpenWindow(#PB_Any, DesktopMouseX(), DesktopMouseY(), 100, 20, "", 
                          #PB_Window_BorderLess, WindowID(parentWindow))
  AddKeyboardShortcut(treeEditor, #PB_Shortcut_Return, 1)
  AddKeyboardShortcut(treeEditor, #PB_Shortcut_Escape, 2)
  teString = StringGadget(#PB_Any, 0, 0, 100, 20, treeText)
  SetGadgetColor(teString, #PB_Gadget_BackColor, RGB(255, 255, 0))
  SetGadgetColor(teString, #PB_Gadget_FrontColor, RGB(100, 0, 0))
  SetActiveGadget(teString)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            If Trim(GetGadgetText(teString)) <> ""
              SetGadgetText(treeGadgetNo, Trim(GetGadgetText(teString)))
            EndIf
            escaped = 1
          Case 2
            escaped = 1
        EndSelect
    EndSelect  
  Until escaped
  
  DisableWindow(parentWindow, #False)
  CloseWindow(treeEditor)
  
EndProcedure
and place the call inside the PB_EventType_LeftDoubleClick event of the tree gadget:

Code: Select all

ediTree(windowNumber, treeGadgetNumber) 
That's all! :)

Re: editing a tree gadget

Posted: Thu Sep 25, 2014 9:58 pm
by davido
@TI-994A , Very nice solution. :D

Thank you for sharing.

Re: editing a tree gadget

Posted: Thu Sep 25, 2014 10:46 pm
by Shardik
Thank you, TI-994A and Rashad for your nice examples!
RASHAD wrote:Color not supported in MAC
The color constants #White and #Black in your code are only defined in the Windows version. But a simple

Code: Select all

  CompilerIf Defined(White, #PB_Constant) = #False
    #Black = 0
    #White = $FFFFFF
  CompilerEndIf
at the top of your code will allow your example to be also executed on Linux and MacOS X (tested on MacOS X 10.9.5 "Mavericks" x86, Ubuntu 12.04 x64 with Unity and Windows 7 SP1 x64 )... :wink:

TI-994A wrote:However, if you might prefer a non-API, cross-platform solution, try this one:
Unfortunately you have to really test a seemingly cross-platform solution on all 3 operating systems to really be sure because even without any API functions there seem to exist subtle differences between the platforms. In both your examples you have to change

Code: Select all

SetGadgetText(treeGadgetNo, Trim(GetGadgetText(#teString)))]
to

Code: Select all

SetGadgetItemText(treeGadgetNo, GetGadgetState(treeGadgetNo), Trim(GetGadgetText(#teString)))
in order to finally display the edited node text in the TreeGadget on MacOS X 10.9.5 "Mavericks" (no problem with your original line on Ubuntu 12.04 x64 with Unity and Windows 7 x64 SP1, but the modified line works correctly on all 3 platforms)... :wink:

Re: editing a tree gadget

Posted: Fri Sep 26, 2014 12:54 am
by RASHAD
Another solution with exact location for the item to edit

Code: Select all

  Global Hlight
  Hlight = $FF9933
      
  If OpenWindow(0, 0, 0, 350, 300, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    StringGadget(0,0,0,0,0,"")
    TreeGadget(1, 10, 10, 160, 250)                                         ; TreeGadget standard
    TreeGadget(2, 180, 10, 160, 250, #PB_Tree_CheckBoxes | #PB_Tree_NoLines)  ; TreeGadget with Checkboxes + NoLines
    For ID = 1 To 2
      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
    AddKeyboardShortcut(0,#PB_Shortcut_Return,20)
    AddKeyboardShortcut(0,#PB_Shortcut_Escape,30)

  Repeat                
     Select WaitWindowEvent()
       Case #PB_Event_CloseWindow
              Quit = 1              
              
       Case #PB_Event_Menu
             Select EventMenu()
                         
                  Case 20
                         If GetActiveGadget() = 0
                             SetGadgetItemText(1,Act,GetGadgetText(0))
                             SetGadgetText(0,"")
                             ResizeGadget(0,0,0,0,0)
                             SetActiveGadget(1)
                         EndIf
                         
                  Case 30
                          If GetActiveGadget() = 0
                              SetGadgetText(0,"")
                              ResizeGadget(0,0,0,0,0)
                              SetActiveGadget(1)
                          EndIf

             EndSelect
             
       Case #PB_Event_Gadget
              Select EventGadget()
                       Case 1
                              Select EventType()
                                    Case #PB_EventType_Change,#PB_EventType_LeftClick
                                             ResizeGadget(0,0,0,0,0)
                                             
                                    Case #PB_EventType_LeftDoubleClick
                                            Act = GetGadgetState(1)
                                            StartDrawing(WindowOutput(0))
                                                For y = 0 To 250
                                                     If Point(20,y) = Hlight Or Point(60,y) = Hlight
                                                         For x = 10 To 160
                                                              If Point(x,y) = Hlight
                                                                  Break
                                                              EndIf
                                                         Next
                                                         Break 
                                                      EndIf                                                        
                                                Next
                                                Width =  TextWidth(GetGadgetItemText(1,Act))
                                            StopDrawing()
                                            ResizeGadget(0,x,y,Width,18)
                                            SetGadgetText(0,GetGadgetItemText(1,Act))
                                            SetActiveGadget(0)
                              EndSelect                                    
              EndSelect             
               
    EndSelect
  Until Quit = 1
  EndIf


Re: editing a tree gadget

Posted: Fri Sep 26, 2014 9:12 am
by TI-994A
@davido: Thank you. :D

@Shardik: Thank you, and also for pointing out the compatibility issues with Mac OSX. I must admit that it was only tested on Windows; like the listing says, quick & dirty. :wink:

Nevertheless, it might be worth putting a note to the documentation team, highlighting this incompatibility with the SetGadgetText() function when used with the TreeGadget() on Mac OSX.

@Rashad: Good approach habibi, although scanning for the highlight colour would not work when custom colour preferences or themes are applied. Also, the string gadget gets displaced if the list is scrolled during editing, and it gets clipped if the tree gadget width is smaller than the tree. 8)

I've taken a page from that approach, and modified my example to retrieve the currently applied highlight colour using the Windows GetSysColor() API function. To maintain cross-compatibility, I've added a compiler directive to implement the original method on Mac OSX and Linux.

Code: Select all

;========================================================
; A quick & dirty method for editable TreeGadget() items
;      
; a cross-platform (?) solution by TI-994A - version 1.3
;
; 26th September 2014 - free to use, improve, share...
;========================================================

Enumeration 
  #mainWindow   
  #tree1
  #text1
EndEnumeration

;=============================================================
; fully self-contained and ready to use - no changes required.
; just drop into your project and call ediTree(win#, tree#)
;=============================================================
Procedure ediTree(parentWindowNo, treeGadgetNo)
  DisableWindow(parentWindowNo, #True)
  treeText.s = GetGadgetText(treeGadgetNo)
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      StartDrawing(WindowOutput(parentWindowNo))
        teStrWidth = TextWidth(GetGadgetItemText(treeGadgetNo, GetGadgetState(treeGadgetNo)))
        teStrHeight = TextHeight(GetGadgetItemText(treeGadgetNo, GetGadgetState(treeGadgetNo)))
        If teStrWidth < 100
          teStrWidth = 100
        EndIf
        xScanStart = WindowMouseX(parentWindowNo) - teStrWidth
        If xScanStart < 0
          xScanStart = 0
        EndIf
        yScanStart = WindowMouseY(parentWindowNo) - teStrHeight
        If yScanStart < 0
          yScanStart = 0
        EndIf
        For selectedX = xScanStart To (xScanStart + teStrWidth)
          For selectedY = yScanStart To (yScanStart + teStrHeight)
            If Point(selectedX, selectedY) = GetSysColor_(#COLOR_HIGHLIGHT)
              Break 2
            EndIf
          Next
        Next
        selectedX + WindowX(parentWindowNo, #PB_Window_InnerCoordinate)
        selectedY + WindowY(parentWindowNo, #PB_Window_InnerCoordinate)
      StopDrawing()  
    CompilerDefault
      teStrWidth = 100
      teStrHeight = 20
      selectedX = DesktopMouseX()
      selectedY = DesktopMouseY()
  CompilerEndSelect
  
  treeEditor = OpenWindow(#PB_Any, selectedX, selectedY, teStrWidth, teStrHeight, "", 
                          #PB_Window_BorderLess, WindowID(parentWindowNo))
  AddKeyboardShortcut(treeEditor, #PB_Shortcut_Return, 1)
  AddKeyboardShortcut(treeEditor, #PB_Shortcut_Escape, 2)
  teString = StringGadget(#PB_Any, 0, 0, teStrWidth, teStrHeight, treeText, #PB_String_BorderLess)
  SetGadgetColor(teString, #PB_Gadget_BackColor, RGB(255, 255, 0))
  SetGadgetColor(teString, #PB_Gadget_FrontColor, RGB(100, 0, 0))
  SetActiveGadget(teString)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            If Trim(GetGadgetText(teString)) <> ""
              SetGadgetItemText(treeGadgetNo, GetGadgetState(treeGadgetNo), Trim(GetGadgetText(teString)))
            EndIf
            escaped = 1
          Case 2
            escaped = 1
        EndSelect
    EndSelect  
  Until escaped
  
  DisableWindow(parentWindowNo, #False)
  CloseWindow(treeEditor)
  
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(#mainWindow, 0, 0, 220, 250, "TreeGadget", wFlags)
  TextGadget(#text1, 10, 10, 200, 30, "Double-click the tree item to edit," + 
                                      #CRLF$ + "ENTER to set, ESC to cancel.")
  TreeGadget(#tree1, 10, 45, 200, 200, #PB_Tree_CheckBoxes)
  For populate = 0 To 9
    Read.s dataStr$
    AddGadgetItem (#tree1, -1, dataStr$)
    Read.s dataStr$
    AddGadgetItem (#tree1, -1, dataStr$, 0, 1)
    AddGadgetItem (#tree1, -1, "one more...", 0, 2)  
  Next
  
  Repeat    
    Select WaitWindowEvent() 
      Case #PB_Event_CloseWindow
        appQuit = 1
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #tree1
            Select EventType()
              Case #PB_EventType_LeftDoubleClick
                ;*****************************************************
                ; simply call this as a function for any TreeGadget()
                ;*****************************************************
                ediTree(#mainWindow, #tree1)
                ;*****************************************************
            EndSelect        
        EndSelect
    EndSelect
  Until appQuit = 1 
EndIf

DataSection
  Data.s "Albert Einstein", "e = mc2", "Isaac Newton", "Apple on the head?", "Charles Darwin", "Theory of evolution."
  Data.s "Guglielmo Marconi", "This is the BBC.", "Benjamin Franklin", "Flying kites in the rain?", "Galileo Galilei"
  Data.s "Starlight, star bright...", "Leonardo da Vinci", "Mona Lisa.", "René Descartes", "I think, therefore I am?"
  Data.s "Thomas Edison", "1% perspiration, 99% rip-off!", "Alexander Graham Bell", "Hello? Can you hear me?"
EndDataSection

Re: editing a tree gadget

Posted: Fri Sep 26, 2014 9:34 am
by sirrab
Thanks everyone !!

Gad to see its not just me learning something :D

Craig

Re: editing a tree gadget

Posted: Fri Sep 26, 2014 11:05 am
by RASHAD
Hi TI-994A
I tried not to use Windows API
With API it will be very easy and accurate

Code: Select all

If OpenWindow(0, 0, 0, 400, 320, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0,0,0,0,0,"")
  SetGadgetColor(0,#PB_Gadget_BackColor,$82FEFD)
  SetGadgetColor(0,#PB_Gadget_FrontColor,$FD241F)
  SendMessage_(GadgetID(0), #EM_SETMARGINS, #EC_LEFTMARGIN, 2|0 << 16)
  TreeGadget(1, 10, 10, 200, 300,#PB_Tree_CheckBoxes)
    For a = 0 To 10
      AddGadgetItem (1, -1, "Normal Item "+Str(a), 0, 0)
      AddGadgetItem (1, -1, "Node "+Str(a), 0, 0)
      AddGadgetItem(1, -1, "Sub-Item 1", 0, 1)
      AddGadgetItem(1, -1, "Sub-Item 2", 0, 1)
      AddGadgetItem(1, -1, "Sub-Item 3", 0, 1)
      AddGadgetItem(1, -1, "Sub-Item 4", 0, 1)
      AddGadgetItem (1, -1, "File "+Str(a), 0, 0)
    Next
    
SetWindowLongPtr_(GadgetID(1), #GWL_STYLE, GetWindowLongPtr_(GadgetID(1), #GWL_STYLE) | #WS_CLIPSIBLINGS)
SetWindowPos_(GadgetID(1), #HWND_BOTTOM, -1, -1, -1, -1, #SWP_NOSIZE | #SWP_NOMOVE)
    
  Padx = GadgetX(1)+3
  Pady = GadgetY(1)+1
    
 AddKeyboardShortcut(0,#PB_Shortcut_Return,20)
 AddKeyboardShortcut(0,#PB_Shortcut_Escape,30)
      
r.RECT
Repeat
  Select WaitWindowEvent()
     
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #PB_Event_Menu
     Select EventMenu()
                 
          Case 20
                 If GetActiveGadget() = 0
                     SetGadgetItemText(1,Act,GetGadgetText(0))
                     SetGadgetText(0,"")
                     ResizeGadget(0,0,0,0,0)
                     SetActiveGadget(1)
                 EndIf
                 
          Case 30
                  If GetActiveGadget() = 0
                      SetGadgetText(0,"")
                      ResizeGadget(0,0,0,0,0)
                      SetActiveGadget(1)
                  EndIf
      EndSelect         

                       
          Case #PB_Event_Gadget
            Select EventGadget()
              Case 1
                  Select EventType()
                      Case #PB_EventType_Change,#PB_EventType_LeftClick
                              ResizeGadget(0,0,0,0,0)
                              
                      Case #PB_EventType_LeftDoubleClick
                           Act = GetGadgetState(1)
                           r\left = GadgetItemID(1, Act)
                           SendMessage_(GadgetID(1),#TVM_GETITEMRECT,1,r)                           
                           ResizeGadget(0,r\left+Padx,r\top+Pady,100,18)
                           SetGadgetText(0,GetGadgetItemText(1,Act))
                           SetActiveGadget(0)
          
              EndSelect
      EndSelect
  EndSelect
Until Quit = 1
EndIf
Edit : Modified

Re: editing a tree gadget

Posted: Sat Sep 27, 2014 6:18 am
by TI-994A
Hi RASHAD. Really nice! But there appears to be something wrong with your example; the first double-click displays the string gadget for editing, but not on consequent clicks. Also, the displacement issues are still there; the string gadget doesn't scroll with the tree, and it gets clipped by the scroll bar if the tree gadget width is smaller than the tree width.

I've implemented this approach in my example. Please note that the edit activation sequence has been changed from a double left-click to a single right-click followed by a single left-click, so that it would not interfere with the tree expanding/collapsing action which also uses the double left-click sequence.

Code: Select all

;========================================================
; A quick & dirty method for editable TreeGadget() items
;      
; a cross-platform (?) solution by TI-994A - version 1.4
;
; 27th September 2014 - free to use, improve, share...
;========================================================

Enumeration 
  #mainWindow   
  #tree1
  #text1
EndEnumeration

;=============================================================
; fully self-contained and ready to use - no changes required.
; just drop into your project and call ediTree(win#, tree#)
;=============================================================
Procedure ediTree(parentWindowNo, treeGadgetNo)
  
  DisableWindow(parentWindowNo, #True)
  treeText.s = GetGadgetText(treeGadgetNo)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows
      teTextRect.RECT\left = GadgetItemID(treeGadgetNo, GetGadgetState(treeGadgetNo))
      SendMessage_(GadgetID(treeGadgetNo), #TVM_GETITEMRECT, #True, teTextRect)
      With teTextRect
        teStrWidth = (\right - \left)
        teStrHeight = (\bottom - \top)
        selectedX = \left + GadgetX(treeGadgetNo, #PB_Gadget_ScreenCoordinate) + 2
        selectedY = \top + GadgetY(treeGadgetNo, #PB_Gadget_ScreenCoordinate) + 2        
      EndWith
    CompilerDefault
      teStrWidth = 100
      teStrHeight = 20
      selectedX = DesktopMouseX()
      selectedY = DesktopMouseY()
  CompilerEndSelect
  
  treeEditor = OpenWindow(#PB_Any, selectedX, selectedY, teStrWidth, teStrHeight, "",
                          #PB_Window_BorderLess, WindowID(parentWindowNo))
  AddKeyboardShortcut(treeEditor, #PB_Shortcut_Return, 1)
  AddKeyboardShortcut(treeEditor, #PB_Shortcut_Escape, 2)
  teString = StringGadget(#PB_Any, 2, 1, teStrWidth, teStrHeight, treeText, #PB_String_BorderLess)
  SetWindowColor(treeEditor, RGB(255, 255, 0))
  SetGadgetColor(teString, #PB_Gadget_BackColor, RGB(255, 255, 0))
  SetGadgetColor(teString, #PB_Gadget_FrontColor, RGB(100, 0, 0))
  SetActiveGadget(teString)
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1
            If Trim(GetGadgetText(teString)) <> ""
              SetGadgetItemText(treeGadgetNo, GetGadgetState(treeGadgetNo), Trim(GetGadgetText(teString)))
            EndIf
            escaped = 1
          Case 2
            escaped = 1
        EndSelect
    EndSelect  
  Until escaped
  
  DisableWindow(parentWindowNo, #False)
  CloseWindow(treeEditor)
  
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
If OpenWindow(#mainWindow, 0, 0, 220, 250, "TreeGadget", wFlags)
  TextGadget(#text1, 10, 10, 200, 30, "Double-click the tree item to edit," + 
                                      #CRLF$ + "ENTER to set, ESC to cancel.")
  TreeGadget(#tree1, 10, 45, 200, 200)   ;, #PB_Tree_CheckBoxes)
  For populate = 0 To 9
    Read.s dataStr$
    AddGadgetItem (#tree1, -1, dataStr$)
    Read.s dataStr$
    AddGadgetItem (#tree1, -1, dataStr$, 0, 1)
    AddGadgetItem (#tree1, -1, "one more...", 0, 2)  
  Next
  
  Repeat    
    Select WaitWindowEvent() 
      Case #PB_Event_CloseWindow
        appQuit = 1
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #tree1
            Select EventType()
              Case #PB_EventType_RightClick
                treeEditFlag = 1
              Case #PB_EventType_LeftClick
                If treeEditFlag
                  ;*****************************************************
                  ; simply call this as a function for any TreeGadget()
                  ;*****************************************************
                  ediTree(#mainWindow, #tree1)
                  ;*****************************************************
                  treeEditFlag = 0
                EndIf
            EndSelect        
        EndSelect
    EndSelect
  Until appQuit = 1 
EndIf

DataSection
  Data.s "Albert Einstein", "e = mc2", "Isaac Newton", "Apple on the head?", "Charles Darwin", "Theory of evolution."
  Data.s "Guglielmo Marconi", "This is the BBC.", "Benjamin Franklin", "Flying kites in the rain?", "Galileo Galilei"
  Data.s "Starlight, star bright...", "Leonardo da Vinci", "Mona Lisa.", "René Descartes", "I think, therefore I am?"
  Data.s "Thomas Edison", "1% perspiration, 99% rip-off!", "Alexander Graham Bell", "Hello? Can you hear me?"
EndDataSection

Re: editing a tree gadget

Posted: Sat Sep 27, 2014 9:44 am
by RASHAD
Hi TI-994A
My last post just updated to solve some of your (special) remarks
Your last post need to be recoded again (lot of bugs) :)
Tested with PB 5.30 x86 Win 8.1 x64

Re: editing a tree gadget

Posted: Sat Sep 27, 2014 11:05 am
by TI-994A
RASHAD wrote:My last post just updated to solve some of your (special) remarks
Your last post need to be recoded again (lot of bugs) :)
Tested with PB 5.30 x86 Win 8.1 x64
Hello again. You've solved the clipping issue with the string gadget, but it still does not scroll with the tree. Also, the first double-click to activate the editing works, but not consequent ones; the string gadget simply doesn't appear.

I've just tested my code with PureBasic v5.30 on Windows 8.1, and it works exactly as it should. What are the issues that you seem to be facing? Perhaps you didn't realise that I changed the editing activation sequence.

Re: editing a tree gadget

Posted: Sat Sep 27, 2014 1:43 pm
by RASHAD
Well ,API again edit in place
TI ,if you want to complain just headed to $MS :mrgreen:

Code: Select all

Global EditFlag,Act

If OpenWindow(0, 0, 0, 400, 320, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TreeGadget(0, 10, 10, 185, 300,#PB_Tree_CheckBoxes)
    For a = 0 To 10
      AddGadgetItem (0, -1, "Normal Item "+Str(a), 0, 0)
      AddGadgetItem (0, -1, "Node "+Str(a), 0, 0)
      AddGadgetItem(0, -1, "Sub-Item 1", 0, 1)
      AddGadgetItem(0, -1, "Sub-Item 2", 0, 1)
      AddGadgetItem(0, -1, "Sub-Item 3", 0, 1)
      AddGadgetItem(0, -1, "Sub-Item 4", 0, 1)
      AddGadgetItem (0, -1, "File "+Str(a), 0, 0)
    Next
    
    TreeGadget(1, 205, 10,185, 300,#PB_Tree_CheckBoxes)
    For a = 0 To 10
      AddGadgetItem (1, -1, "Normal Item "+Str(a), 0, 0)
      AddGadgetItem (1, -1, "Node "+Str(a), 0, 0)
      AddGadgetItem(1, -1, "Sub-Item 1", 0, 1)
      AddGadgetItem(1, -1, "Sub-Item 2", 0, 1)
      AddGadgetItem(1, -1, "Sub-Item 3", 0, 1)
      AddGadgetItem(1, -1, "Sub-Item 4", 0, 1)
      AddGadgetItem (1, -1, "File "+Str(a), 0, 0)
    Next
    
 SetWindowLongPtr_(GadgetID(0),#GWL_STYLE, GetWindowLongPtr_(GadgetID(0),#GWL_STYLE) | #TVS_EDITLABELS)
    
Repeat
  Select WaitWindowEvent()
     
    Case #PB_Event_CloseWindow
      Quit = 1
      
    Case #WM_KEYDOWN
            If EditFlag = 1 And EventwParam() = 13
                SetGadgetItemText(0,Act,Text$)
                EditFlag = 0 
            EndIf    
      
    Case #WM_CHAR
            If EditFlag = 1
                 Text$ = Text$+ Chr(EventwParam())
            EndIf
                       
          Case #PB_Event_Gadget
            Select EventGadget()
              Case 0
                  Select EventType()                                                      
                      Case #PB_EventType_LeftDoubleClick
                           Act = GetGadgetState(0)
                           EditFlag = 1
                           Text$ = ""
                           SendMessage_(GadgetID(0),#TVM_EDITLABEL,0, GadgetItemID(0, Act)) 
              EndSelect
      EndSelect
  EndSelect
Until Quit = 1
EndIf