RMB inside TreeGadget

Share your advanced PureBasic knowledge/code with the community.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

RMB inside TreeGadget

Post by Psychophanta »

Code updated For 5.20+

I finally got to select TreeView items with the right mouse button (RMB).

I include Window Callback (might be not necessary) and a PopUpMenu to test it.

Code: Select all

Global RMB.l,hWnd.l
Procedure.l MyWindowCallBack(WindowID.l,Message.l,wParam.l,lParam.l)
  If WindowID=hWnd And Message=#WM_CONTEXTMENU  ;detect RMB click inside main window
    RMB=lParam
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If CreatePopupMenu(0)
  MenuItem(1, "opt1...")
  MenuBar()
  MenuItem(2, "opt2...")
EndIf

hWnd=OpenWindow(0,200,200,150,420,"Title",#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget)



TreeGadget(1,0,0,150,420)
For t=1 To 30
  AddGadgetItem(1,-1,"item"+Hex(t))
Next

SetWindowCallback(@MyWindowCallBack())

Repeat
  EventID.l=WaitWindowEvent()
  Select EventID
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1;
          If RMB
            var.TV_HITTESTINFO\pt\y=RMB>>16-WindowY(0)-$18
            SendMessage_(GadgetID(1),#TVM_HITTEST,0,var)
            SendMessage_(GadgetID(1),#TVM_SELECTITEM,#TVGN_CARET,var\hItem)
            DisplayPopupMenu(0,hWnd)
            RMB=0
          EndIf
      EndSelect
  EndSelect 
Until EventID=#PB_Event_CloseWindow



Nobody can hate WinAPI incoherences like me :evil:

AL
Hi-Toro
Enthusiast
Enthusiast
Posts: 270
Joined: Sat Apr 26, 2003 3:23 pm

Cool

Post by Hi-Toro »

Nice one!
James Boyd
http://www.hi-toro.com/
Death to the Pixies!
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

The simplest way, i think.

Without Callback.

Code: Select all

; English forum: http://jconserv.net/purebasic/viewtopic.php?t=7316
; Author: Psychophanta
; Date: 25. August 2003

If CreatePopupMenu(0) 
  MenuItem(1,"LeftClick") 
  MenuItem(2,"select this item") 
EndIf 

If CreatePopupMenu(1) 
  MenuItem(1,"RightClick") 
  MenuItem(2,"select this item") 
EndIf 

If CreatePopupMenu(2) 
  MenuItem(1,"LeftDoubleClick") 
  MenuItem(2,"select this item") 
EndIf 

If CreatePopupMenu(3) 
  MenuItem(1,"RightDoubleClick") 
  MenuItem(2,"select this item") 
EndIf 

If CreatePopupMenu(4) 
  MenuItem(1,"RightClick Outside") 
EndIf 

If CreatePopupMenu(5) 
  MenuItem(1,"LeftClick Outside") 
EndIf 

hWnd.l=OpenWindow(0,200,200,150,420,#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget,"Title") 

CreateGadgetList(hWnd) 

TreeGadget(1,0,0,150,420) 
For t=1 To 15 
  AddGadgetItem(1,-1,"item"+Hex(t)) 
Next 


Repeat 
  EventID.l=WaitWindowEvent() 
  Select EventID 
  Case #PB_Event_Gadget 
    Select EventGadgetID() 
    Case 1 
      MB.l=EventType() ;<--- for checking if was pushed RMB or LMB, ... 
      GetCursorPos_(@var.TV_HITTESTINFO\pt) ;get mousepointer position screen coordenates 
      ScreenToClient_(GadgetID(1),@var.TV_HITTESTINFO\pt) ;convert that coordenates to the Gadget 1 (TreeView Gadget) referenced by 
      SendMessage_(GadgetID(1),#TVM_HITTEST,0,var) ;this is to know what is the item i am pointing. 
      If var.TV_HITTESTINFO\flags&#TVHT_ONITEM;<-If clicked inside an item bitmap or item label
        Select MB 
        ;--------------- 
        ;Case #PB_EventType_LeftClick 
        ;  DisplayPopupMenu(0,hWnd) 
        Case #PB_EventType_RightClick 
          SendMessage_(GadgetID(1),#TVM_SELECTITEM,#TVGN_CARET,var\hItem);<-- and this selects the pointed item
          DisplayPopupMenu(1,hWnd) 
        Case #PB_EventType_LeftDoubleClick ;<---this doesn't work if single LeftClick is checked (logical, because this is captured at once) 
          DisplayPopupMenu(2,hWnd) 
        ;Case #PB_EventType_RightDoubleClick  ;<-- this never work !?! 
        ;  DisplayPopupMenu(3,hWnd) 
        ;--------------------- 
        EndSelect
        itemsel=GetGadgetState(1) ;save selected item
      Else ;<-If clicked in TreeWiew gadget outside an item bitmap or item label:
        Select MB
        Case #PB_EventType_RightClick
          DisplayPopupMenu(4,hWnd)
        Case #PB_EventType_LeftClick
          DisplayPopupMenu(5,hWnd)
        EndSelect
      EndIf
    EndSelect 
  Case #PB_Event_Menu 
    Select EventMenuID() 
    Case 2 
      SetGadgetItemText(1,itemsel,"selected",0) 
    EndSelect 
  EndSelect 
Until EventID=#PB_Event_CloseWindow 
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Sample code to detect Right Mouse Button clicks inside TreeGadgets.

Now is updated (just in previous post to this one) and there is shown how to differ between click onto some item, or outside items.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

An oldie post but a goodie post!

Thanks! :)
@}--`--,-- A rose by any other name ..
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Dare2 wrote:An oldie post but a goodie post!
Thank you. Oldie, and it works under 3.94b2 without source modification. :)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply