Page 2 of 2

Re: editing a tree gadget

Posted: Sat Sep 27, 2014 8:51 pm
by TI-994A
RASHAD wrote:Your last post need to be recoded again (lot of bugs)…
So, no bugs? :wink:
RASHAD wrote:API again edit in place…
Habibi, the tree gadget in your updated API example now has two editing routines; the one that is enabled when you set the TVS_EDITLABELS style to it, and your own.

When the gadget is double-clicked, your routine is activated, which manually sends the TVM_EDITLABEL message, starts recording the keystrokes into a string, then sets the tree item with that string once the ENTER key is pressed. It appears to work, although it does not filter out non alpha-numeric input like backspaces and control characters.

When the gadget is slow clicked twice (not double-clicked), or is right clicked then left clicked, the system editing routine is activated instead. However, since there's no procedure to process the input, it is simply discarded.

If the tree gadget has the TVS_EDITLABELS style set, all the editing work will be handled by the system, but requires validation before it updates the tree item:

Code: Select all

Procedure tgProc(hWnd, uMsg, wParam, lParam)
  Shared sysProc
  If uMsg = #WM_NOTIFY
    *lParam.NMHDR = lParam
    If *lParam\code = #TVN_ENDLABELEDIT
      ProcedureReturn #True
    EndIf
  EndIf
  ProcedureReturn CallWindowProc_(sysProc, hWnd, uMsg, wParam, lParam)
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
window = OpenWindow(#PB_Any, 0, 0, 220, 220, "TreeGadget", wFlags)
tree = TreeGadget(#PB_Any, 10, 10, 200, 200)
SetWindowLongPtr_(GadgetID(tree), #GWL_STYLE, 
                  GetWindowLong_(GadgetID(tree), #GWL_STYLE) | #TVS_EDITLABELS)
sysProc = SetWindowLong_(WindowID(window), #GWL_WNDPROC, @tgProc())
For populate = 0 To 10
  AddGadgetItem (tree, -1, "main node " + Str(populate))
  AddGadgetItem (tree, -1, "first sub-level "  + Str(populate), 0, 1)
  AddGadgetItem (tree, -1, "second sub-level "  + Str(populate), 0, 2)
Next
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Just some observations; not complaints. :lol:

Re: editing a tree gadget

Posted: Sat Sep 27, 2014 10:50 pm
by RASHAD
Hi TI-994A
-Right then Left to activate the editing (BAD) :)
-Keyboard not supported (Bad again) :D
-In edit mode the main Window looses focus (kh kh kh) :mrgreen:
And more

Now pick one
- With callback

Code: Select all

Procedure WCallback(hWnd,uMsg,wParam,lParam)
  Result = #PB_ProcessPureBasicEvents 
  Select  uMsg
      Case  #WM_NOTIFY
        *wn.NMHDR = lParam    
            Select *wn\code           
              Case #TVN_ENDLABELEDIT
                *tvi.NMTVDISPINFO = lParam
                If *tvi\item\psztext <> 0     
                  If PeekS(*tvi\item\psztext) = ""
                    Result = 0
                  Else
                    Result = 1
                  EndIf             
                EndIf
            EndSelect
  EndSelect
  ProcedureReturn Result
EndProcedure

If OpenWindow(0, 0, 0, 400, 320, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TreeGadget(0, 10, 10, 200, 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
 SetWindowLongPtr_(GadgetID(0),#GWL_STYLE, GetWindowLongPtr_(GadgetID(0),#GWL_STYLE) | #TVS_EDITLABELS)
    
 SetWindowCallback(@WCallback())

Repeat
  Select WaitWindowEvent()
     
          Case #PB_Event_CloseWindow
                  Quit = 1 
      
          Case #WM_KEYDOWN
                      If EventwParam() = 13 And EditFlag = 0
                            EditFlag = 1
                            Act = GetGadgetState(0)
                            SendMessage_(GadgetID(0),#TVM_EDITLABEL,0,GadgetItemID(0, Act))
                      ElseIf (EventwParam() = 13 And EditFlag = 1) Or EventwParam() = 27
                            EditFlag = 0
                      EndIf
                       
          Case #PB_Event_Gadget
            Select EventGadget()
              Case 0
                  Select EventType()                              
                      Case #PB_EventType_LeftDoubleClick
                               Act = GetGadgetState(0)
                               EditFlag = 1
                               SendMessage_(GadgetID(0), #TVM_EDITLABEL,0, GadgetItemID(0, Act)) 
              EndSelect
      EndSelect
  EndSelect
Until Quit = 1
EndIf
-Without Callback

Code: Select all

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 EventwParam() = 13 And EditFlag = 0
                  EditFlag = 1
                  Act = GetGadgetState(0)
                  SendMessage_(GadgetID(0),#TVM_EDITLABEL,0, GadgetItemID(0, Act))
            ElseIf EventwParam() = 13 And EditFlag = 1
                  EditFlag = 0
                  SetGadgetItemText(GetActiveGadget(),Act,Text$)
                  Text$=""
            ElseIf EventwParam() = 27
                  EditFlag = 0
                  Text$ = ""
            EndIf           
     
    Case #WM_CHAR
            If EditFlag = 1
                 If EventwParam() > 31 And EventwParam() < 126
                    Text$ = Text$+ Chr(EventwParam())
                 EndIf
            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
I win :lol: (I am not sure about that :mrgreen: )

Re: editing a tree gadget

Posted: Sun Sep 28, 2014 12:12 pm
by TI-994A
RASHAD wrote:-Right then Left to activate the editing (BAD) :)
-Keyboard not supported (Bad again) :D
-In edit mode the main Window looses focus (kh kh kh) :mrgreen:
RASHAD my friend, I believe that you have your terminologies mixed up. Intended behaviours are not bugs just because they're not good. However, while some may not be bugs technically, they could border on it. A good example of this is the use a double-click to activate the edit feature of the tree gadget when the same is being utilised to expand/collapse the tree view.

On the other hand, some good examples of bugs would be:
- a string gadget getting its display clipped.
- a string gadget that gets displaced during editing.
- a string gadget displaying unreadable characters.
- a tree gadget's edit feature that can be activated but not saved.
RASHAD wrote:And more
Yes, but from your two latest examples.

In the callback version, if a tree item is selected for editing by clicking on it twice (not a double-click, but two single clicks), the <Enter> key would not capture the edit unless pressed twice.

In the non-callback version, the same bug exists, but the second press of the <Enter> key would discard any changes and save a blank string instead.
RASHAD wrote:I win :lol:
You're clearly the superior programmer here, hands down. So, the issue has never been about winning, but learning. And I sincerely hope to continue to learn from your great examples. :)

Re: editing a tree gadget

Posted: Sun Sep 28, 2014 2:56 pm
by RASHAD
Hi TI
When I see that the main window which should be activated loosing focus
I will not say it is a bad design but some BUG there
You changed some parameters then you get some bugs (like narrowing the width of the Tree)
I do not post for a global solutions
Just applicable ideas my friend and the requester do the rest
You did the bugs by your own hands
Anything else I am at your service :mrgreen:

Code: Select all

Global oldproc,Act,r.RECT,EditFlag

Procedure WindowCallback(hWnd, uMsg, wParam, lParam)
  Protected topitem, currentitem
    Result = CallWindowProc_(oldproc, hWnd, uMsg, wParam, lParam)
    Select uMsg
      Case #WM_VSCROLL,#WM_HSCROLL
            If EditFlag = 1 
               r\left = GadgetItemID(1, Act)
               SendMessage_(GadgetID(1),#TVM_GETITEMRECT,1,r)                                                     
               ResizeGadget(0,GadgetX(1,#PB_Gadget_ContainerCoordinate)+ r\left+2,GadgetY(1,#PB_Gadget_ContainerCoordinate)+r\top+1,100,18)                     
               SetGadgetText(0,GetGadgetItemText(1,Act))
               SetActiveGadget(0)
            EndIf

    EndSelect
  ProcedureReturn Result
EndProcedure

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, 1|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
    
oldproc=SetWindowLongPtr_(GadgetID(1), #GWL_WNDPROC, @WindowCallback())
    
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)
    
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
                     EditFlag = 0
                     SetGadgetItemText(1,Act,GetGadgetText(0))
                     SetGadgetText(0,"")
                     ResizeGadget(0,0,0,0,0)
                     SetActiveGadget(1)
                 EndIf
                 
          Case 30
                  If GetActiveGadget() = 0
                      EditFlag = 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
                             EditFlag = 0
                             ResizeGadget(0,0,0,0,0)
                              
                      Case #PB_EventType_LeftDoubleClick
                             EditFlag = 1
                             Act = GetGadgetState(1)
                             r\left = GadgetItemID(1, Act)
                             SendMessage_(GadgetID(1),#TVM_GETITEMRECT,1,r)                           
                             ResizeGadget(0,GadgetX(1,#PB_Gadget_ContainerCoordinate) + r\left+2,GadgetY(1,#PB_Gadget_ContainerCoordinate)+ r\top+1,100,18)
                             SetGadgetText(0,GetGadgetItemText(1,Act))
                             SetActiveGadget(0)
          
              EndSelect
      EndSelect
  EndSelect
Until Quit = 1
EndIf

Re: editing a tree gadget

Posted: Sun Sep 28, 2014 3:31 pm
by TI-994A
RASHAD wrote:…When I see that the main window which should be activated loosing focus
I will not say it is a bad design but some BUG there
You changed some parameters then you get some bugs (like narrowing the width of the Tree)…
Habibi, it cannot be a bug when there's a command to explicitly disable the window. Intended behaviour is not a bug.

On the other hand, your latest post is still buggy. The string gadget will be displayed only on the first double-click, and does not appear on subsequent attempts. That's clearly not intended behaviour, and so it's a bug.

Furthermore, it is absolutely reasonable to narrow the width of the tree gadget to test the boundaries of a solution because the tree list itself would eventually increase in width, resulting in the same error. Rather than adding another few sub-levels, it's faster and easier to simply narrow its width for such testing.
RASHAD wrote:You did the bugs by your own hands
In this matter, I must insist that all due credit is yours. :lol:

Re: editing a tree gadget

Posted: Sun Sep 28, 2014 3:56 pm
by RASHAD
About the dblclick that is not true
Now go and give us some good exam.(Free of bugs please :mrgreen: )

Re: editing a tree gadget

Posted: Sun Sep 28, 2014 5:08 pm
by TI-994A
RASHAD wrote:About the dblclick that is not true...
That it's not a good idea, perhaps. But it still doesn't work.

EDIT: Just tested your code again, and it doesn't work on PureBasic v5.22 LTS (x64) or v5.30 (x64). But apparently it does on v.5.22 LTS (x86) and v5.21 (x86). Now that's a serious bug!
RASHAD wrote:Now go and give us some good exam.(Free of bugs please :mrgreen: )
Assuming that exam is your short form for example, I believe I have; bug-free.

Your turn. :lol: