Double Click & Single Click

Just starting out? Need help? Post your questions and find answers here.
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Double Click & Single Click

Post by AMpos »

BarryG wrote:It's because you've got "Break" in there for no valid reason. Remove it and it works. (See how posting code makes it easier for us to help?).
It is not working.

The BREAK is there because after DoubleClick at Window B, I have to close it and exit his loop to continue working on Window A.

If BREAK is removed, Window B is closed but the program is still inside first loop until I click CLOSE in Window A.
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Double Click & Single Click

Post by AMpos »

MY program does this (pseudocode):

Code: Select all

MAIN LOOP
   if input at gadget(input)
      code$=getgadgettext(input)
      if code$="?"
         code$=SHowList()
      endif
      add code$ to main list
   endif
   if LeftClick inside IconList
      code$=Code at line clicked
   endif

FOREVER

procedure showlist()
   openwindow B
   show a list of codes
   wait till double click on one item
   procedurereturn code of item double selected
endprocedure
The user is adding codes to a main list. He can click on one of the list to change something. If enter "?" as a code (because he didnt know the code of the item he wants to add), a list of available codes will be shown.

(The codes are Articles Bar Codes, and the "?" is to search articles by name.)
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Double Click & Single Click

Post by Marc56us »

Tip, avoid using numeric identifiers for gadgets, it's the best way to get confused afterwards.
Do something like this:

Code: Select all

EnableExplicit

Enumeration 
    #Win_0
    #Win_1
    #Lst_0
    #Lst_1
EndEnumeration

OpenWindow(#Win_0,000,000,400,400,"Window A")
ListIconGadget(#Lst_0,10,10,380,380,"TEST",100)

OpenWindow(#Win_0,40,40,320,320,"Window B")
ListIconGadget(#Lst_1,10,10,300,300,"test 2",100)

DisableWindow(#Win_0, #False)

Repeat
    Select WaitWindowEvent()
:wink:
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Double Click & Single Click

Post by AMpos »

Marc56us wrote:Tip, avoid using numeric identifiers for gadgets, it's the best way to get confused afterwards.
Do something like this:

Code: Select all

...
:wink:
Of course I am doing it on my program!

Even so, it will not work.
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: Double Click & Single Click

Post by BarryG »

I took a deeper look and it's because the closing of window B is so fast that window A gets the focus again and thus physically gets the second click on it. So you need to stop the second click from physically touching it; either by not positioning window B over the top of window A, or delaying the second click. This works:

Code: Select all

OpenWindow(0,000,000,400,400,"Window A")
ListIconGadget(0,10,10,380,380,"TEST",100)
OpenWindow(1,40,40,320,320,"Window B")
ListIconGadget(1,10,10,300,300,"test 2",100)

DisableWindow(0,1)

Repeat
  event=WaitWindowEvent()
  If event=#PB_Event_Gadget
    Select EventWindow()
      Case 0
        If EventGadget()=0 And EventType()=#PB_EventType_LeftClick
          Debug "Click Once at Window A"
        EndIf
      Case 1
        If EventGadget()=1 And EventType()=#PB_EventType_LeftDoubleClick
          Debug "Click Twice at Window B"
          CloseWindow(1)
          DisableWindow(0,0)
          SetActiveWindow(0)
          Delay(DoubleClickTime()/2) ; Let the double-click event finish.
          While WindowEvent() : Wend ; And clear the events for the clicks.
        EndIf
    EndSelect
  EndIf
Until event=#PB_Event_CloseWindow
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Double Click & Single Click

Post by AMpos »

Now it is working.

Notice I know it was this "so fast thing", but just adding a "delay (x)" didn't worked (that was driving me crazy), it is the combination of "delay+ClearEventLoop" that did the trick.

Thank you dude, and all the others.
BarryG
Addict
Addict
Posts: 3318
Joined: Thu Apr 18, 2019 8:17 am

Re: Double Click & Single Click

Post by BarryG »

Excellent. Glad I could help.
User avatar
Demivec
Addict
Addict
Posts: 4089
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Double Click & Single Click

Post by Demivec »

@AMpos: Here is another variation. I personally would do this because I don't want to throw away events without knowing what they are most of the time.

It implements an ignore timer just for clicks in window A which is tested for for only window A clicks.

Code: Select all

OpenWindow(0,000,000,400,400,"Window A")
ListIconGadget(0,10,10,380,380,"TEST",100)
OpenWindow(1,40,40,320,320,"Window B")
ListIconGadget(1,10,10,300,300,"test 2",100)

DisableWindow(0,1)
Define DisableWindowAClicksTime = 0, DisableWindowAClicks = #False

Repeat
  event=WaitWindowEvent()
  If event=#PB_Event_Gadget
    Select EventWindow()
      Case 0
        If EventGadget()=0 And EventType()=#PB_EventType_LeftClick
          If DisableWindowAClicks = #True
            ;Debug "" + DisableWindowAClicks + " : " + Str(DisableWindowAClicksTime - ElapsedMilliseconds())
            If DisableWindowAClicksTime < ElapsedMilliseconds()
              DisableWindowAClicksTime = 0
              DisableWindowAClicks = #False
            EndIf
          EndIf
          
          If Not DisableWindowAClicks
            Debug "Click Once at Window A"
          EndIf
        EndIf
      Case 1
        If EventGadget()=1 And EventType()=#PB_EventType_LeftDoubleClick
          Debug "Click Twice at Window B"
          DisableWindowAClicksTime = ElapsedMilliseconds() + DoubleClickTime()/2 
          DisableWindowAClicks = #True
          CloseWindow(1)
          DisableWindow(0,0)
          SetActiveWindow(0)
          ;           Delay(2) ; Let the double-click event finish.
          ;           While WindowEvent() : Wend ; And clear the events for the clicks.
        EndIf
    EndSelect
  EndIf
Until event=#PB_Event_CloseWindow
AMpos
Enthusiast
Enthusiast
Posts: 128
Joined: Fri Jun 05, 2020 12:47 am

Re: Double Click & Single Click

Post by AMpos »

Thanks, will check
breeze4me
Enthusiast
Enthusiast
Posts: 523
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: Double Click & Single Click

Post by breeze4me »

BarryG's code and Demivec's code do not work properly.
If the left button-up is delayed after double-clicking in Windows B, a click event occurs in Windows A.

Therefore, it is better to use a custom event on Windows.

Edit:
BTW, it seems to be a bug.
1. Left button-down outside a listicon gadget.
2. Move the mouse pointer into the gadget.
3. Left button-up inside the gadget, then a click event occurs.

Code: Select all

Enumeration #PB_EventType_FirstCustomValue
  #MyApp_ListIcon_EventType_LeftClick
  ;#MyApp_ListIcon_EventType_LeftDoubleClick
EndEnumeration

Procedure WndProc(hwnd, uMsg, wParam, lParam)
  
  Select uMsg
    Case #WM_NOTIFY
      *lParam.NMHDR = lParam
      If *lParam\hwndFrom = GadgetID(0)
        If *lParam\code = #NM_CLICK
          PostEvent(#PB_Event_Gadget, 0, 0, #MyApp_ListIcon_EventType_LeftClick)
        ;ElseIf *lParam\code = #NM_DBLCLK
        ;  PostEvent(#PB_Event_Gadget, 0, 0, #MyApp_ListIcon_EventType_LeftDoubleClick)
        EndIf
      EndIf
      
  EndSelect
  
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

OpenWindow(0,000,000,400,400,"Window A")
ListIconGadget(0,10,10,380,380,"TEST",100)

OpenWindow(1,40,40,320,320,"Window B")
ListIconGadget(1,10,10,300,300,"test 2",100)

SetWindowCallback(@WndProc(), 0)

DisableWindow(0,1)

Repeat
  
  event=WaitWindowEvent()
  
  If event=#PB_Event_Gadget
    
    Select EventWindow()
      Case 0
        
        If EventGadget() = 0 And EventType() = #MyApp_ListIcon_EventType_LeftClick
          Debug "Click Once at Window A"
        EndIf
        
      Case 1
        
        If EventGadget()=1 And EventType() = #PB_EventType_LeftDoubleClick
          Debug "Click Twice at Window B"
          
          CloseWindow(1)
          DisableWindow(0,0)
          
          SetActiveWindow(0)
          SetActiveGadget(0)
        EndIf
        
    EndSelect
    
  EndIf
Until event=#PB_Event_CloseWindow
Post Reply