Editable Treeview

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Editable Treeview

Post by eddy »

Code updated for 5.20+

This code works because I've added result=1 :)

Why does this notification (#TVN_ENDLABELEDIT) need this result result ?

Code: Select all

#Window = 0 
#Gadget_TV = 0 

Procedure CB(window, message, wParam, lParam) 
  result = #PB_ProcessPureBasicEvents   
  If window = WindowID(#Window)
    If message = #WM_NOTIFY
      ; =======================
      ; Treeview Notifications
      ; =======================
      *tvinfo.NMTVDISPINFO = lParam
      If *tvinfo\hdr\hwndfrom = GadgetID(#Gadget_TV)
        If *tvinfo\hdr\code = #TVN_BEGINLABELEDIT
          ; ==================================
          ; something to do before editing ...
          ; ==================================
        EndIf 
        If *tvinfo\hdr\code = #TVN_ENDLABELEDIT And *tvinfo\item\pszText <> 0
          ; =======================
          ; get results...it works!
          ; =======================
          Debug "Selected Item: " + Str(GetGadgetState(#Gadget_TV))
          Debug "The new label is: " + PeekS(*tvinfo\item\pszText)
          ProcedureReturn
        EndIf 
      EndIf             
    EndIf 
  EndIf 
  ProcedureReturn result
EndProcedure 

If OpenWindow(#Window, 0, 0, 300, 200, "Editable Treeview (edit on 2nd click)",
              #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  tv = TreeGadget(#Gadget_TV, 10, 10, 280, 180) 
  AddGadgetItem(#Gadget_TV, -1, "item 1") 
  AddGadgetItem(#Gadget_TV, -1, "item 2") 
  AddGadgetItem(#Gadget_TV, -1, "sub item 2", 0, 1) 
  AddGadgetItem(#Gadget_TV, -1, "sub item 2", 0, 1) 

  ;new editable style
  SetWindowLong_(tv, #GWL_STYLE, GetWindowLong_(tv, #GWL_STYLE) | #TVS_EDITLABELS)

  ;new callback
  SetWindowCallback(@CB()) 

  While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
EndIf
Last edited by eddy on Sun Mar 28, 2004 6:54 pm, edited 2 times in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

alternate version without any result...

Post by eddy »

This second version does not need any result
but it uses some PB commands to update the label. :idea:

Code: Select all

#Window = 0 
#Gadget_TV = 0 

Procedure CB(Window, Message, wParam, lParam) 
    Result = #PB_ProcessPureBasicEvents   
    
    If Window =WindowID(#Window)
    
      If Message=#WM_NOTIFY
         ; =======================
         ; Treeview Notifications
         ; =======================
      
         *tvinfo.NMTVDISPINFO=lParam
         If *tvinfo\hdr\hwndfrom=GadgetID(#Gadget_TV)
            If *tvinfo\hdr\code=#TVN_BEGINLABELEDIT
               ;
               ;something to do before editiong ...
               ;
            EndIf 
            
            If *tvinfo\hdr\code=#TVN_ENDLABELEDIT and *tvinfo\item\pszText<>0
               ;
               ;get result ...its work !!
               ;
               newLabel=PeekS(*tvinfo\item\pszText)
               selectedItem=GetGadgetState(#Gadget_TV)
               SetGadgetItemText(#Gadget_TV,selectedItem,newLabel,0)
            EndIf 
         EndIf             
      EndIf 
    EndIf 
    
    ProcedureReturn Result
EndProcedure 

If OpenWindow(#Window, 111,111, 300, 200, #PB_Window_TitleBar , "Editable Treeview") 
   If CreateGadgetList(WindowID()) 
      tv=TreeGadget(#Gadget_TV, 10, 10, 270, 180) 
      AddGadgetItem(#Gadget_TV, -1, "item") 
      AddGadgetItem(#Gadget_TV, -1, "item")

      ;new style
      SetWindowLong_(tv,#GWL_STYLE,GetWindowLong_(tv,#GWL_STYLE) | #TVS_EDITLABELS)                  
      
      ;new callback
      SetWindowCallback(@CB()) 
   EndIf            
   
   Repeat 
   Until WaitWindowEvent()=#PB_Event_CloseWindow 
EndIf 

Last edited by eddy on Sun Mar 28, 2004 6:55 pm, edited 1 time in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
freak
PureBasic Team
PureBasic Team
Posts: 5950
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

The result has a meaning, see the documentation:
Return Value

If the pszText member is non-NULL, return TRUE to set the item's label to the edited text. Return FALSE to reject the edited text and revert to the original label.
So, if you return nothing, the default procedure will return 0, which
then rejects the edited text.

You can also stop the user from editing a specific item at all, by
returning 1 from the #TVN_BEGINLABELEDIT message.

You can see here for another example in PB:
viewtopic.php?p=7628#7628

Timo
quidquid Latine dictum sit altum videtur
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

Thx Freaks...
I understand better now.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

[Bug Fixed]

I added this code :
*tvinfo\item\pszText<>0
to prevent PeekS() crash...
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
Post Reply