Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drag & Drop of a Treeview item
#1
Hi again,

Please, any advice on how can I drop an item to other destination than the root?

Regards.

Function dialog_treeview_xml2
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

;create xml macro if does not exist

if !qmitem("TreeView_Test_XML" 1)
,_s=
;<x>
,;<x t="a">
,,;<x t="a1" />
,,;<x t="a2" />
,,;<x t="a3" />
,;</x>
,;<x t="b" />
;</x>

,newitem("TreeView_Test_XML" _s)

str controls = "5"
str e5
if(!ShowDialog("dialog_treeview_xml2" &dialog_treeview_xml2 &controls)) ret

;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 223 135 "Dialog"
;3 SysTreeView32 0x54010027 0x0 0 0 96 116 ""
;5 Edit 0x54030080 0x200 102 6 118 14 ""
;4 Button 0x54032000 0x0 102 24 118 14 "Add child of selected"
;6 Button 0x54032000 0x0 154 44 66 14 "Delete selected"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2030300 "" "" ""

ret
;messages
sel message
,case WM_INITDIALOG
,_s.getmacro("TreeView_Test_XML")
,if(_s.len) XmlToTreeView _s id(3 hDlg)
,
,case WM_DESTROY
,case WM_COMMAND goto messages2
,case WM_NOTIFY goto messages3
ret
;messages2
sel wParam
,case IDOK
,;save tree view control data to xml macro
,XmlFromTreeView _s id(3 hDlg)

,_s.setmacro("TreeView_Test_XML")
,
,case 4 ;;add child item of selected item
,_s.getwintext(id(5 hDlg))
,TvAdd id(3 hDlg) SendMessage(id(3 hDlg) TVM_GETNEXTITEM TVGN_CARET 0) _s
,InvalidateRect id(3 hDlg) 0 1 ;;redraw
,
,case 6 ;;delete selected item
,_i=SendMessage(id(3 hDlg) TVM_GETNEXTITEM TVGN_CARET 0)
,if(_i) SendMessage(id(3 hDlg) TVM_DELETEITEM 0 _i) ;;else deletes all
,
,case IDCANCEL
ret 1
;messages3
int i
NMHDR* nh=+lParam
sel nh.idFrom
,case 3
,NMTREEVIEW* nt=+nh
,if(nh.code=TVN_SELCHANGEDW or nh.code=TVN_SELCHANGED)
,,i=nt.itemNew.lParam ;;was set by TvAdd
,else if(nh.code=TVN_BEGINDRAGW or nh.code=TVN_BEGINDRAG)
,,type TVDRAG49 htv hidrag
,,TVDRAG49 td.htv=nh.hwndFrom; td.hidrag=nt.itemNew.hItem
,,Drag(hDlg &drag_test_proc2 &td)
,,SendMessage td.htv TVM_SELECTITEM TVGN_DROPHILITE 0

ret

Function drag_test_proc2
Code:
Copy      Help
;/
function button TVDRAG49&td

TVHITTESTINFO ht
xm ht.pt; ScreenToClient(td.htv &ht.pt)
int hidrop=SendMessage(td.htv TVM_HITTEST 0 &ht)

int candrop=hidrop or ht.flags&TVHT_NOWHERE

if(button)
,SendMessage td.htv TVM_SELECTITEM TVGN_DROPHILITE 0
,if(!candrop) ret
,ret TvMoveItem(td.htv td.hidrag hidrop GetMod=2)
else
,SendMessage td.htv TVM_SELECTITEM TVGN_DROPHILITE hidrop
,if(!candrop) ret 3
,ret iif(GetMod=2 2 1)

;instead of TVM_SELECTITEM could use TVM_SETINSERTMARK, but then need more calculations
#2
Up. Any idea, please?

Thanks
#3
In TvMoveItem need to set is.hiParent.

Function TvMoveItem2
Code:
Copy      Help
;/
function# htv hi hito copy

;Moves or copies treeview control item hi to the place of hito.
;If successful, returns new item handle (on move too, because the item must be deleted and new item created).
;Does not copy Vista-specific properties. If you use them, set them again.
;If hito is 0, moves/copies to the end.


if(hi=hito) ret hi

;get all properties of hi, except Vista properties
TVINSERTSTRUCTW is
TVITEMW& t=is.item
t.hItem=hi; t.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_PARAM|TVIF_STATE|TVIF_TEXT
t.stateMask=TVIS_BOLD|TVIS_CUT|TVIS_DROPHILITED|TVIS_EXPANDED|TVIS_EXPANDEDONCE|TVIS_EXPANDPARTIAL|TVIS_SELECTED|TVIS_OVERLAYMASK|TVIS_STATEIMAGEMASK|TVIS_USERMASK
BSTR b; t.pszText=b.alloc(300); t.cchTextMax=300
if(!SendMessage(htv TVM_GETITEMW 0 &t)) ret
int selected=t.state&TVIS_SELECTED

;insert new item with these properties
if(copy) t.mask&=~TVIF_STATE
else
,int hparent(GetParent(htv)) wp(SubclassWindow(hparent &DefWindowProcW)) ;;subclass parent window to prevent receiving select and delete messages

if(hito)
,is.hParent=SendMessage(htv TVM_GETNEXTITEM TVGN_PARENT hito)
,is.hInsertAfter=SendMessage(htv TVM_GETNEXTITEM TVGN_PREVIOUS hito)
,if(!is.hInsertAfter) is.hInsertAfter=TVI_FIRST
else is.hInsertAfter=TVI_LAST

int hinew=SendMessage(htv TVM_INSERTITEMW 0 &is)

;delete hi
if(hinew and !copy)
,if(selected) SendMessage htv TVM_SELECTITEM TVGN_CARET hinew
,SendMessage(htv TVM_DELETEITEM 0 hi)

;unsubclass
if(wp) SubclassWindow(hparent wp)

ret hinew

Still not perfect.
#4
Wow!!! Looks wonderful!

Thanks for sharing.


Forum Jump:


Users browsing this thread: 1 Guest(s)