Page 1 of 1

Editable Treeview

Posted: Sat Mar 27, 2004 7:01 am
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

alternate version without any result...

Posted: Sat Mar 27, 2004 7:20 am
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 


Posted: Sat Mar 27, 2004 2:04 pm
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

Posted: Sat Mar 27, 2004 3:55 pm
by eddy
Thx Freaks...
I understand better now.

Posted: Sun Mar 28, 2004 6:57 pm
by eddy
[Bug Fixed]

I added this code :
*tvinfo\item\pszText<>0
to prevent PeekS() crash...