Treegadget editable (readable version & more functions)

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

Treegadget editable (readable version & more functions)

Post by eddy »

- I tried to make a PB event-like system for notify callback
- notify functions for treegadget

Code: Select all

; ====================================
; some functions to make this code readable
; ====================================

;like eventGadgetID()
;     Procedure NEventGadgetID()
;       *hdr.NMHDR=EventlParam()
;       If *hdr
;         ProcedureReturn *hdr\idFrom
;         EndIf
;     EndProcedure

; -------------------------------

;like eventType()
;     Procedure NEventType()
;        *hdr.NMHDR=EventlParam()
;        ProcedureReturn *hdr\code
;     EndProcedure

; ====================================
; some TreeGadget-specific functions to make this code readable
; ====================================

#TREE_LABEL_EDITABLE=0
#TREE_LABEL_UNEDITABLE=1
#TREE_LABEL_SAVED=1
#TREE_LABEL_UNSAVED=0

; -------------------------------         

;make tree editable
Procedure TreeEditable(TreeID.l)             
  SetWindowLong_(GadgetID(TreeID),#GWL_STYLE,GetWindowLong_(GadgetID(TreeID),#GWL_STYLE) | #TVS_EDITLABELS)
EndProcedure

; -------------------------------

;set label text before modification
Procedure N_SetLabelText(txt.s)
  ctl=SendMessage_(GadgetID(GadgetID),#TVM_GETEDITCONTROL,0,0)
  SendMessage_(ctl,#WM_SETTEXT,0,@txt)
EndProcedure

; -------------------------------         

;get modified label
Procedure.s N_GetLabelText()
  *tvinfo.NMTVDISPINFO=EventlParam()
  *txt=*tvinfo\item\psztext
  If *txt=0
    newtxt.s=""
  Else
    newtxt.s=PeekS(*txt)
  EndIf
  ProcedureReturn newtxt
EndProcedure

; -------------------------------         

;save modified label
Procedure.s N_SaveLabelText(newtxt.s)
  *tvinfo.NMTVDISPINFO=EventlParam()
  *txt=*tvinfo\item\psztext
  CopyMemoryString(newtxt,@*txt)
EndProcedure

; -------------------------------

;if user cancels the label edition
Procedure N_LabelEditCanceled()
  *tvinfo.NMTVDISPINFO=EventlParam()
  *txt=*tvinfo\item\psztext
  If *txt=0
    ProcedureReturn #True
  EndIf
EndProcedure

; =======================
; code example
; =======================

Enumeration
  #WINDOW
  #TREE
EndEnumeration

; -------------------

Procedure WindowCallBack(Window,Message,wParam,lParam)
  ReturnValue = #PB_ProcessPureBasicEvents
  
  If Message=#WM_NOTIFY   
    ;          *hdr.NMHDR=EventlParam()
    ;           GadgetID=*hdr\idFrom
    If EventGadget()=#TREE
      If EventType()=#TVN_BEGINLABELEDIT
        ;start modification
        index=GetGadgetState(GadgetID)
        Select index
          Case 1 
            ReturnValue=#TREE_LABEL_UNEDITABLE
          Case 3
            txt.s=GetGadgetText(GadgetID)
            txt=Left(txt,Len(txt)-Len(" : postfix text"))
            N_SetLabelText(txt)
            ReturnValue=#TREE_LABEL_EDITABLE
          Default
            ReturnValue=#TREE_LABEL_EDITABLE
        EndSelect
        
      ElseIf EventType()=#TVN_ENDLABELEDIT
        ;save modification
        If N_LabelEditCanceled()
          ReturnValue=#TREE_LABEL_UNSAVED               
        Else
          ReturnValue=#TREE_LABEL_SAVED     
          index=GetGadgetState(GadgetID)
          Select index
            Case 2
              If N_GetLabelText()=""
                N_SaveLabelText("You tried to save an empty string !")   
              EndIf
            Case 3
              N_SaveLabelText(N_GetLabelText()+" : postfix text")
          EndSelect 
        EndIf
        
      EndIf
    EndIf
  EndIf
  
  ProcedureReturn ReturnValue
EndProcedure

; -------------------

If OpenWindow(#WINDOW ,300,300,200,150,"Tree Edit",#PB_Window_SystemMenu) 
  TreeGadget(#TREE, 10,10,180,120)           
  TreeEditable(#TREE)
  AddGadgetItem (#TREE, 0, "Label ") 
  AddGadgetItem (#TREE, 1, "Label (uneditable)") 
  AddGadgetItem (#TREE, 2, "Label (can't save empty string)") 
  AddGadgetItem (#TREE, 3, "Label : postfix text")
  SetWindowCallback(@WindowCallBack())
  
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf  
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool