PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: JR Heathcote on May 29, 2006, 10:47:38 PM

Title: TreeView Woes
Post by: JR Heathcote on May 29, 2006, 10:47:38 PM
To All,

I have a treeview loaded with items and I need to return the selection of the item I pick.  Only problem is where do I put the code to respond to the user selection?  Tried putting this in the FORM_CUSTOM event, using something like:



 LOCAL pNMHDR AS NMHDR PTR
 
 LOCAL nLine  AS LONG
 
 LOCAL hItem  AS DWORD
 
 LOCAL sText  AS STRING
 '-----------------------------------------------------------------------
 
 SELECT CASE wMsg
   CASE %WM_NOTIFY
     pNMHDR = lParam
     SELECT CASE @pNMHDR.hWndFrom
       CASE IDC_DEMO_TV
         SELECT CASE @pNMHDR.Code
           CASE %NM_CLICK
             hItem = SendMessage(HWND_DEMO_TV, %TVM_GETNEXTITEM, %TVGN_CARET, %Null)
             sText = edit_TVGetText(HWND_DEMO_TV, hItem)




But IDC_DEMO_TV is never pointed to by @pNMHDR.hWndFrom.

So what am I doing wrong?

TIA
Title: TreeView Woes
Post by: TechSupport on May 29, 2006, 11:07:25 PM
At first glance, it looks like you are comparing the Treeview's identifer to the windows handle.

Maybe you should try something like the following:

     SELECT CASE @pNMHDR.hWndFrom
       CASE HWND_DEMO_TV
Title: TreeView Woes
Post by: JR Heathcote on May 30, 2006, 09:42:46 AM
Paul,

Thanks, your suggestion worked.  I got this mess working, sort of.

Only now it requires that I click in the TreeView control twice in order to invoke the item.  It's like I have to click once in the TreeView to activate it, then again on the item I wish to select.  Not what I want.

Here's the code I'm using with some code comments to explain what I'm doing and why:


'---------------------------------------------------------------------------
FUNCTION DEMO_WM_NOTIFY ( _
                       hWndForm     AS DWORD,     _  ' handle of Form
                       idCtrl       AS DWORD,     _  ' control ID
                       BYVAL pNMHDR AS NMHDR PTR  _  ' pointer to NMHDR structure
                       ) AS LONG

 Local x     As Long
 Local nLine As Long
 
 Local hItem As Dword
 
 Local sText As String
 '-----------------------------------------------------------------------
 
 Select Case @pNMHDR.hWndFrom
   Case HWND_DEMO_TV
     Select Case @pNMHDR.Code
       'The purpose of %NM_CLICK is to set the caret in the RichEdit
       'control to a line containing a user specifed tag.  This affords
       'the user the opportunity to identify critical sections in the text
       'file.  The user can then click on the tag and immediately go to a
       'specific line in the file.  THIS WOULD BE GREAT IF THIS CODE
       'WORKED THAT WAY!!!
       Case %NM_CLICK
         'The following code works, but requires the user clicks twice
         '(not double click) on the desired TreeView item in order to
         'invoke it.  Would like to be able to do this with one click.
         hItem = SendMessage(HWND_DEMO_TV, %TVM_GETNEXTITEM, %TVGN_CARET, %Null)
         sText = edit_TVGetText(HWND_DEMO_TV, hItem)
         nLine = Val(sText) - 1
         
         x = 0
         
         'Another annoyance, when the treeview item is finally invoked
         'focus is not set to the RichEdit control despite the two
         'SetFocus calls.
         SetFocus(HWND_DEMO_TXT)
         
         SendMessage(HWND_DEMO_TXT, %EM_LINESCROLL, x, nLine)
                                 
         Call edit_LineOffsetToCaret(HWND_DEMO_TXT, nLine, 0)                    

         SetFocus(HWND_DEMO_TXT)
         
       Case Else
       
     End Select
   
 End Select

END FUNCTION


BTW, I moved this code from DEMO_CUSTOM to the above named event and everythings seems to work the same.  When handling %WM_NOTIFY messages that are returned to the parent dialog is this the preferred way to do it rather than using the _CUSTOM events?

JR
Title: TreeView Woes
Post by: TechSupport on May 30, 2006, 04:56:59 PM
I may not fully understand your problem but the following code should help. I am responding to the %TVN_SELCHANGED message rather than the NM_CLICK because the selection is not set to the clicked node at the time that the NM_CLICK fires. TVN_SELCHANGED is more appropriate.

Also, you stumbled on a problem that I came across while writing FireFly. That is, the focus issue during the execution of the notification  message. The easy solution is to POST a message back to the custom procedure. I called the user defined message MSG_POSITIONTEXT. You could call it whatever you wish.

Also, you could try storing your line numbers in the lParam of the hItem when you create your node. You could then retrive the line number directly instead of having to get the text string first and then convert it to a number.


%MSG_POSITIONTEXT = %WM_USER + 1000


'------------------------------------------------------------------------------------------------------------------------
Function DEMO_CUSTOM ( _
                    hWndForm      As Dword, _  ' handle of Form
                    wMsg          As Long,  _  ' type of message
                    wParam        As Dword, _  ' first message parameter
                    lParam        As Long   _  ' second message parameter
                    ) As Long

 Local pNMTV As NM_TREEVIEW Ptr
 Local sText As String
 Local nLine As Long
 
 Select Case wMsg

    Case %MSG_POSITIONTEXT                              
       ' The nLine arrives in the wParam of the message
       SetFocus HWND_DEMO_TXT
       SendMessage HWND_DEMO_TXT, %EM_LINESCROLL, 0, wParam
       'edit_LineOffsetToCaret HWND_DEMO_TXT, wParam, 0
                                 
                                 
    Case %WM_NOTIFY
        pNMTV = lParam
       
        Select Case @pNMTV.hdr.idfrom
       
             Case IDC_DEMO_TV      ' notify message from the treeview control
                 
                 Select Case @pNMTV.hdr.code
                       
                       Case %TVN_SELCHANGED
                          ' Retrieve the text of the selected item.
                          sText = FF_TreeView_GetText( @pNMTV.hdr.hWndFrom, @pNMTV.ItemNew.hItem )
                          nLine = Val(sText) - 1
                          PostMessage hWndForm, %MSG_POSITIONTEXT, nLine, 0
                         
                       Case %NM_CLICK    'left click        
                         
                       Case %NM_DBLCLK   'left double-click
                             
                       Case %NM_RCLICK   ' right click

                       Case %NM_RDBLCLK   'right double-click

                 End Select
                 
        End Select                    

 End Select

End Function
Title: TreeView Woes
Post by: JR Heathcote on May 30, 2006, 06:31:27 PM
Thanks will give this a try

Quote
...you stumbled on a problem that I came across while writing FireFly.

Stumbling is something I am very good at doing since I seem to accomplish so much of it at any given time.

JR