It is currently Sun Dec 08, 2019 7:10 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: How to detect double click on TextGadget()?
PostPosted: Sat Oct 05, 2019 1:35 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 472
I want to edit the text of a TextGadget().
To do that I need to detect a double click on it so I can swop the gadget with a string gadget.

Any ideas how to do that?


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sat Oct 05, 2019 2:42 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 630
Location: Linz, Austria
On Windows you can add #SS_NOTIFY to the TextGadget... Maybe there is something similar for OSX?!


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sat Oct 05, 2019 6:40 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Got it running after a while.

Update v0.1
- Added free subclass
- Optimize first mouse click

Code:
;-TOP

;Define AppDelegate.I = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")

; ----

ImportC ""
  objc_disposeClassPair(*Class)
EndImport
 
Structure udtGadgetData
  ; Header
  Window.i
  Gadget.i
  *OldClass
  *NewClass
  ; Data
  last_time.i
EndStructure

Global NewMap ClassGadgetMap.udtGadgetData()

ProcedureC ClassGadgetCallback(Object.i, Selector.i, Event.i)
  Protected time, diff_time
  time = ElapsedMilliseconds()
  If FindMapElement(ClassGadgetMap(), Hex(Object))
    With ClassGadgetMap()
      diff_time = time - \last_time
      \last_time = time
      If diff_time < DoubleClickTime()
        PostEvent(#PB_Event_Gadget, \Window, \Gadget, #PB_EventType_LeftDoubleClick)
      Else
        PostEvent(#PB_Event_Gadget, \Window, \Gadget, #PB_EventType_LeftClick)
      EndIf
    EndWith
  EndIf
EndProcedure

Procedure SubclassGadget(Window.i, Gadget.i, NewClassName.s)
  Protected GadgetID = GadgetID(Gadget)
  Protected GadgetClass = CocoaMessage(0, GadgetID, "class")
  Protected NewGadgetClass
 
  If Not FindMapElement(ClassGadgetMap(), Hex(GadgetID))
    AddMapElement(ClassGadgetMap(), Hex(GadgetID))
    NewClassName + Str(Gadget)
   
    NewGadgetClass = objc_allocateClassPair_(GadgetClass, NewClassName, 0)
    objc_registerClassPair_(NewGadgetClass)
    object_setClass_(GadgetID, NewGadgetClass)
   
    ;class_addMethod_(NewGadgetClass, sel_registerName_("rightMouseDown:"), @ClassGadgetCallback(), "v@:@")
    class_addMethod_(NewGadgetClass, sel_registerName_("mouseDown:"), @ClassGadgetCallback(), "v@:@")
   
    With ClassGadgetMap()
      \Window = Window
      \Gadget = Gadget
      \OldClass = GadgetClass
      \NewClass = NewGadgetClass
      \last_time = ElapsedMilliseconds() - DoubleClickTime()
    EndWith
   
  EndIf
EndProcedure

Procedure FreeSubclassGadget(Gadget)
  Protected GadgetID = GadgetID(Gadget)
  If FindMapElement(ClassGadgetMap(), Hex(GadgetID))
    With ClassGadgetMap()
      object_setClass_(GadgetID, \OldClass)
      objc_disposeClassPair(\NewClass)
    EndWith
    DeleteMapElement(ClassGadgetMap())
  EndIf
EndProcedure

; ---.

If OpenWindow(0, 40, 40, 200, 120, "Test")
  TextGadget(0, 10, 10, 180, 30, "TextGadget 1")
  TextGadget(1, 10, 50, 180, 30, "TextGadget 2")
 
  SubclassGadget(0, 0, "SubclassedTextGadget")
  SubclassGadget(0, 1, "SubclassedTextGadget")
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_LeftClick
                Debug "1. LeftClick"
              Case #PB_EventType_LeftDoubleClick
                Debug "1. LeftDoubleClick"
                ;FreeSubclassGadget(1)
            EndSelect
          Case 1
            Select EventType()
              Case #PB_EventType_LeftClick
                Debug "2. LeftClick"
              Case #PB_EventType_LeftDoubleClick
                Debug "2. LeftDoubleClick"
            EndSelect
           
        EndSelect
       
    EndSelect
   
  ForEver
 
EndIf

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sat Oct 05, 2019 7:45 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3542
Location: Netherlands
If you don't mind targeting MacOS 10.10+ you can use NSClickGestureRecognizer

Code:
; MacOS 10.10+

ProcedureC DoubleClickCallback(Obj, Sel, GestureRecognizer)
  Debug "DoubleClick at GadgetID: " + Str(CocoaMessage(0, GestureRecognizer, "view"))
EndProcedure

AppDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
DelegateClass = CocoaMessage(0, AppDelegate, "class")
class_addMethod_(DelegateClass, sel_registerName_("doubleClick:"), @DoubleClickCallback(),"v@:@")

DoubleClick = CocoaMessage(0, 0, "NSClickGestureRecognizer alloc")
DoubleClick = CocoaMessage(0, DoubleClick, "initWithTarget:", AppDelegate, "action:", sel_registerName_("doubleClick:"))
CocoaMessage(0, DoubleClick, "setNumberOfClicksRequired:", 2)

OpenWindow(0, 0, 0, 322, 50, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(0, 8, 8, 306, 20, "DoubleClick me", #PB_Text_Border)
CocoaMessage(0, GadgetID(0), "addGestureRecognizer:", DoubleClick)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sat Oct 05, 2019 7:51 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 472
Wow! Thanks both

if someone has a more easy solution as my pleas let me know.

Code:
Global Window_0, textGadget, strGadget

ProcedureC DoubleClickCallback(Obj, Sel, GestureRecognizer)
  Debug "DoubleClick at GadgetID: " + Str(CocoaMessage(0, GestureRecognizer, "view"))
  HideGadget(textGadget, #True)
  HideGadget(strGadget, #False)
  SetActiveGadget(strGadget)
EndProcedure


Global AppDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
DelegateClass = CocoaMessage(0, AppDelegate, "class")
class_addMethod_(DelegateClass, sel_registerName_("doubleClick:"), @DoubleClickCallback(),"v@:@")



Procedure OpenWindow_0(x = 0, y = 0, width = 450, height = 160)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  textGadget = TextGadget(#PB_Any, 8, 8, 306, 20, "DoubleClick me", #PB_Text_Border)
  DoubleClick = CocoaMessage(0, 0, "NSClickGestureRecognizer alloc")
  DoubleClick = CocoaMessage(0, DoubleClick, "initWithTarget:", AppDelegate, "action:", sel_registerName_("doubleClick:"))
  CocoaMessage(0, DoubleClick, "setNumberOfClicksRequired:", 2)
  CocoaMessage(0, GadgetID(textGadget), "addGestureRecognizer:", DoubleClick)
 
 
  strGadget = StringGadget(#PB_Any, 8, 8, 306, 20,  "DoubleClick me")
  CocoaMessage(0, GadgetID(strGadget), "setDelegate:", AppDelegate)
  CocoaMessage(0, GadgetID(strGadget), "setAction:", sel_registerName_("performClick:") )
  CocoaMessage(0, GadgetID(strGadget), "setTarget:", GadgetID(textGadget))
  HideGadget(strGadget, #True)
 
EndProcedure


Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
     
    Case #PB_Event_Menu
      Select EventMenu()
        Case #PB_Menu_Quit
          ProcedureReturn #False
      EndSelect
     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
        Case textGadget
          If EventType() = 0
            HideGadget(textGadget, #False)
            SetGadgetText(textGadget, GetGadgetText(strGadget) )
            HideGadget(strGadget, #True)
          EndIf
         
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False



Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sun Oct 06, 2019 6:17 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3542
Location: Netherlands
Wolfram wrote:
if someone has a more easy solution as my pleas let me know.

I don't know if it is easier but you could use only a StringGadget without a TextGadget and switch its state between editable or not.
Code:
Procedure ChangeState(StringGadget, Editable)
  Protected StringGadgetID = GadgetID(StringGadget)
  CocoaMessage(0, StringGadgetID, "setEditable:", Editable)
  CocoaMessage(0, StringGadgetID, "setBezeled:", Editable)
  CocoaMessage(0, StringGadgetID, "setDrawsBackground:", Editable)
EndProcedure

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sun Oct 06, 2019 11:18 am 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 472
Yes, these looks more economic.
But I still need a second gadget to exit the edit mode by pressing ReturnKey.

Code:
  CocoaMessage(0, GadgetID(strGadget), "setAction:", sel_registerName_("performClick:") )
  CocoaMessage(0, GadgetID(strGadget), "setTarget:", GadgetID(textGadget))


Is there another way to call a procedure by pressing Return on the StringGadget()?


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sun Oct 06, 2019 1:59 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Add method 'keyDown:' to gadget not work, but without CocoaMessages...
Code:
;-TOP
Enumeration CustomEventType #PB_EventType_FirstCustomValue
  #My_EventType_Return
EndEnumeration
 
Procedure EventReturnKey()
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #My_EventType_Return)
EndProcedure


If OpenWindow(0, 0, 0, 400, 300, "StringGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  StringGadget(0, 10, 10, 380, 30, "StringGadget...")
  EditorGadget(1, 10, 50, 380, 240)
 
  CreateMenu(0, WindowID(0))
  BindMenuEvent(0, 1000, @EventReturnKey())
 
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Break
       
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_Focus
                AddKeyboardShortcut(0, #PB_Shortcut_Return, 1000)
              Case #PB_EventType_LostFocus
                RemoveKeyboardShortcut(0, #PB_Shortcut_Return)
              Case #My_EventType_Return
                Debug "Return"
                SetActiveGadget(1)
            EndSelect
               
        EndSelect
       
    EndSelect
  ForEver
 
EndIf

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Sun Oct 06, 2019 3:01 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 472
So this look to me good.

Code:
Global Window_0, strGadget

Enumeration CustomEventType #PB_EventType_FirstCustomValue
  #My_EventType_Return
EndEnumeration


Procedure ChangeState(StringGadget, Editable)
  Protected StringGadgetID = GadgetID(StringGadget)
  CocoaMessage(0, StringGadgetID, "setEditable:", Editable)
  CocoaMessage(0, StringGadgetID, "setBezeled:", Editable)
  CocoaMessage(0, StringGadgetID, "setDrawsBackground:", Editable)
  SetActiveGadget(StringGadget)
EndProcedure


Procedure EventReturnKey()
  Debug "RetunKey"
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #My_EventType_Return)
EndProcedure


ProcedureC DoubleClickCallback(Obj, Sel, GestureRecognizer)
  Debug "DoubleClick at GadgetID: " + Str(CocoaMessage(0, GestureRecognizer, "view"))

  ChangeState(strGadget, #True)
 
  BindMenuEvent(0, 1000, @EventReturnKey())
  AddKeyboardShortcut(Window_0, #PB_Shortcut_Return, 1000)
 
EndProcedure


Global AppDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
DelegateClass = CocoaMessage(0, AppDelegate, "class")
class_addMethod_(DelegateClass, sel_registerName_("doubleClick:"), @DoubleClickCallback(),"v@:@")



Procedure OpenWindow_0(x = 0, y = 0, width = 450, height = 160)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  strGadget = StringGadget(#PB_Any, 8, 8, 306, 20,  "DoubleClick me")
  CocoaMessage(0, GadgetID(strGadget), "setDelegate:", AppDelegate)
 
  ChangeState(strGadget, #False)
 
  DoubleClick = CocoaMessage(0, 0, "NSClickGestureRecognizer alloc")
  DoubleClick = CocoaMessage(0, DoubleClick, "initWithTarget:", AppDelegate, "action:", sel_registerName_("doubleClick:"))
  CocoaMessage(0, DoubleClick, "setNumberOfClicksRequired:", 2)
  CocoaMessage(0, GadgetID(strGadget), "addGestureRecognizer:", DoubleClick)
 
  CreateMenu(0, WindowID(Window_0))
 
EndProcedure


Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
     
    Case #PB_Event_Menu
      Select EventMenu()
        Case #PB_Menu_Quit
          ProcedureReturn #False
      EndSelect
     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
        Case strGadget
          If EventType() = #My_EventType_Return
            UnbindMenuEvent(0, 1000, @EventReturnKey())
            RemoveKeyboardShortcut(Window_0, #PB_Shortcut_Return)
            ChangeState(strGadget, #False)     
          EndIf
         
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Mon Oct 07, 2019 2:29 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 472
Which object is given on this callback?
How can I select between different gadgets?

Code:
ProcedureC DoubleClickCallback(Obj, Sel, GestureRecognizer)

  Select Obj
    Case GadgetID(strGadget)
      ChangeState(strGadget, #True)
     
    Case GadgetID(strGadget2)
      ChangeState(strGadget2, #True)
     
  EndSelect

  BindMenuEvent(0, 1000, @EventReturnKey())
  AddKeyboardShortcut(Window_0, #PB_Shortcut_Return, 1000)
 
EndProcedure


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Mon Oct 07, 2019 5:13 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3542
Location: Netherlands
Wolfram wrote:
How can I select between different gadgets?

I just found out a gesture recognizer can only be added to one view.
That means you would have to create a new one for every gadget :(

So maybe replacing mouseDown: similar to what mk-soft did is a better option.
Code:
Prototype Proto_MouseDown(Object, Selector, Event)
DeclareC MouseDown(Object, Selector, Event)

Global MouseDown_.Proto_MouseDown = class_replaceMethod_(objc_getClass_("NSTextField"),
                                                         sel_registerName_("mouseDown:"),
                                                         @MouseDown(), "v@:@")

ProcedureC MouseDown(Object, Selector, Event)
  Protected Gadget, ClickCount = CocoaMessage(0, Event, "clickCount")
  If ClickCount = 2
    Gadget = CocoaMessage(0, Object, "tag")
    Debug "DoubleClick on gadget:" + Str(Gadget)
  EndIf
  If MouseDown_
    MouseDown_(Object, Selector, Event)
  EndIf
EndProcedure


OpenWindow(0, 270, 100, 200, 170, "DoubleClick", #PB_Window_MinimizeGadget)
TextGadget(1, 10, 10, 100, 20, "Gadget 1")
TextGadget(2, 10, 40, 100, 20, "Gadget 2")
StringGadget(3, 10, 70, 100, 20, "Gadget 3")

Repeat
  Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Mon Oct 07, 2019 7:53 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 30, 2013 4:39 pm
Posts: 472
Hi Wilbert,

what do you think about this solution?

Code:
Global Window_0, strGadget, strGadget2

Enumeration CustomEventType #PB_EventType_FirstCustomValue
  #My_EventType_Return
EndEnumeration


Procedure ChangeState(StringGadget, Editable)
  Protected StringGadgetID = GadgetID(StringGadget)
  CocoaMessage(0, StringGadgetID, "setEditable:", Editable)
  CocoaMessage(0, StringGadgetID, "setBezeled:", Editable)
  CocoaMessage(0, StringGadgetID, "setDrawsBackground:", Editable)
  SetActiveGadget(StringGadget)
EndProcedure


Procedure EventReturnKey()
  Debug "RetunKey"
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #My_EventType_Return)
EndProcedure


Procedure getGadget()
  windowID = WindowID(Window_0)
  NSPoint.NSPoint
 
  CocoaMessage(@NSPoint, windowID, "mouseLocationOutsideOfEventStream")
  object = CocoaMessage(0, CocoaMessage(0, windowID, "contentView"), "hitTest:@", @NSPoint)
 
  ProcedureReturn CocoaMessage(0, object, "tag")
EndProcedure


Procedure exitEdit(gadget)
  ChangeState(gadget, #False)
  UnbindMenuEvent(0, 1000, @EventReturnKey())
  RemoveKeyboardShortcut(Window_0, #PB_Shortcut_Return)
 
EndProcedure


ProcedureC DoubleClickCallback(Obj, Sel, GestureRecognizer)
  Static gadget = -1
 
  If gadget <> -1
    exitEdit(gadget)
  EndIf
 
  gadget = getGadget()
 
  If gadget <> -1
    SetActiveGadget(gadget)
    ChangeState(gadget, #True)
    BindMenuEvent(0, 1000, @EventReturnKey())
    AddKeyboardShortcut(Window_0, #PB_Shortcut_Return, 1000)
   
  EndIf

EndProcedure

Procedure initDoubleClickGesture()
  DoubleClick = CocoaMessage(0, 0, "NSClickGestureRecognizer alloc")
  DoubleClick = CocoaMessage(0, DoubleClick, "initWithTarget:", AppDelegate, "action:", sel_registerName_("doubleClick:"))
  CocoaMessage(0, DoubleClick, "setNumberOfClicksRequired:", 2)
  ProcedureReturn DoubleClick
EndProcedure


Global AppDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
DelegateClass = CocoaMessage(0, AppDelegate, "class")
class_addMethod_(DelegateClass, sel_registerName_("doubleClick:"), @DoubleClickCallback(),"v@:@")



Procedure OpenWindow_0(x = 0, y = 0, width = 450, height = 160)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  strGadget = StringGadget(#PB_Any, 8, 8, 306, 20,  "DoubleClick me")
  strGadget2 = StringGadget(#PB_Any, 8, 50, 306, 20,  "DoubleClick me2")
  CocoaMessage(0, GadgetID(strGadget), "setDelegate:", AppDelegate)
  CocoaMessage(0, GadgetID(strGadget2), "setDelegate:", AppDelegate)
 
  ChangeState(strGadget, #False)
  ChangeState(strGadget2, #False)
 

  CocoaMessage(0, GadgetID(strGadget), "addGestureRecognizer:", initDoubleClickGesture() )

  CocoaMessage(0, GadgetID(strGadget2), "addGestureRecognizer:", initDoubleClickGesture() )
 
  CreateMenu(0, WindowID(Window_0))
 
EndProcedure


Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
     
    Case #PB_Event_Menu
      Select EventMenu()
        Case #PB_Menu_Quit
          ProcedureReturn #False
      EndSelect
     
    Case #PB_Event_Gadget
      Select EventGadget()
         
        Case strGadget
          If EventType() = #My_EventType_Return
            exitEdit(strGadget)     
          EndIf
        Case strGadget2
          If EventType() = #My_EventType_Return
            exitEdit(strGadget2)   
          EndIf
         
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0()

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Mon Oct 07, 2019 8:22 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
@wilbert
How do you know something like this that Purebasic added the method (or property) "tag" to get the gadget number :?: :?: :?:
Code:
Gadget = CocoaMessage(0, Object, "tag")

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Mon Oct 07, 2019 8:27 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3542
Location: Netherlands
Wolfram wrote:
what do you think about this solution?

Inside the DoubleClickCallback procedure, it's easier to get the gadget like this
Code:
gadget = CocoaMessage(0, CocoaMessage(0, GestureRecognizer, "view"), "tag")
You don't need things like hitTest.


mk-soft wrote:
How do you know something like this that Purebasic added the method (or property) "tag" to get the gadget number

Inside the SDK, there's a file named Gadget.h which mentions this
Quote:
the NSView tag method is used to store our GadgetID

_________________
macOS 10.15 Catalina, PB 5.71 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: How to detect double click on TextGadget()?
PostPosted: Mon Oct 07, 2019 8:56 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2053
Location: Germany
Ok, Thanks wildert...

Now with your 'mouseDown' code

Bugfix - missing EnableExplicit
- Update: Move SetActiveGadget to enterEdit

Code:
;-TOP

EnableExplicit

; ***************************************
;- Wilberts mouseDown Method (and mk-soft)

Prototype Proto_MouseDown(Object, Selector, Event)
DeclareC MyMouseDown(Object, Selector, Event)

Global DefaultMouseDown.Proto_MouseDown = class_replaceMethod_(objc_getClass_("NSTextField"),
                                                         sel_registerName_("mouseDown:"),
                                                         @MyMouseDown(), "v@:@")

ProcedureC MyMouseDown(Object, Selector, Event)
  Protected Gadget = CocoaMessage(0, Object, "tag")
  Protected ClickCount = CocoaMessage(0, Event, "clickCount")
  If ClickCount = 1
    PostEvent(#PB_Event_Gadget, GetActiveWindow(), Gadget, #PB_EventType_LeftClick)
  ElseIf ClickCount = 2
    PostEvent(#PB_Event_Gadget, GetActiveWindow(), Gadget, #PB_EventType_LeftDoubleClick)
  EndIf
  If DefaultMouseDown
    DefaultMouseDown(Object, Selector, Event)
  EndIf
EndProcedure

; ***************************************


Global Window_0, strGadget, strGadget2, strGadget3

Enumeration CustomEventType #PB_EventType_FirstCustomValue
  #My_EventType_Return
EndEnumeration


Procedure ChangeState(StringGadget, Editable)
  Protected StringGadgetID = GadgetID(StringGadget)
  CocoaMessage(0, StringGadgetID, "setEditable:", Editable)
  CocoaMessage(0, StringGadgetID, "setBezeled:", Editable)
  CocoaMessage(0, StringGadgetID, "setDrawsBackground:", Editable)
EndProcedure


Procedure EventReturnKey()
  PostEvent(#PB_Event_Gadget, GetActiveWindow(), GetActiveGadget(), #My_EventType_Return)
EndProcedure


Procedure enterEdit(gadget)
  ChangeState(gadget, #True)
  AddKeyboardShortcut(Window_0, #PB_Shortcut_Return, 1000)
  SetActiveGadget(gadget)
EndProcedure

Procedure exitEdit(gadget)
  ChangeState(gadget, #False)
  RemoveKeyboardShortcut(Window_0, #PB_Shortcut_Return)
EndProcedure


Procedure OpenWindow_0(x = 0, y = 0, width = 450, height = 160)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  strGadget = StringGadget(#PB_Any, 8, 8, 306, 20,  "DoubleClick me")
  strGadget2 = StringGadget(#PB_Any, 8, 50, 306, 20,  "DoubleClick me2")
  strGadget3 = EditorGadget(#PB_Any, 8, 92, 306, 60)
 
  ChangeState(strGadget, #False)
  ChangeState(strGadget2, #False)
 
  CreateMenu(0, WindowID(Window_0))
  BindMenuEvent(0, 1000, @EventReturnKey())
 
EndProcedure


Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False
     
    Case #PB_Event_Menu
      Select EventMenu()
        Case #PB_Menu_Quit
          ProcedureReturn #False
      EndSelect
     
    Case #PB_Event_Gadget
      Select EventGadget()
         
        Case strGadget
          Select EventType()
            Case #PB_EventType_LeftDoubleClick
              enterEdit(strGadget)
             
            Case #My_EventType_Return, #PB_EventType_LostFocus
              exitEdit(strGadget)
             
          EndSelect
         
        Case strGadget2
          Select EventType()
            Case #PB_EventType_LeftDoubleClick
              enterEdit(strGadget2)
           
            Case #My_EventType_Return, #PB_EventType_LostFocus
              exitEdit(strGadget2)
             
          EndSelect
         
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure

OpenWindow_0()

Define event

Repeat
  event = WaitWindowEvent()
Until Window_0_Events(event) = #False

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye