This Question is Answered

15 "helpful" answers available (3 pts)
1 Replies Last post: Sep 1, 2008 6:27 PM by carl

About datefield's context menu

Sep 1, 2008 5:22 AM

Click to view taor's profile Level 2 taor 29 posts since
Jul 9, 2008
how to let datefield not popup default context-menu when right Click, but let it popup when right Dblclick?
Click to view carl's profile Curl carl 74 posts since
Oct 17, 2007
1. Re: About datefield's context menu Sep 1, 2008 6:27 PM
A solution might involve altering the contents of the DateField and consuming the offending ContextMenuEvent, like so:

|| Find a TextFieldUI by searching through a graphical hierarchy.
{define-proc {seek g:Graphic}:#Graphic
    {type-switch g
     case tf:TextFieldUI do
        {return tf}
     case b:Box do
        {for one-graphic in b.graphical-children do
            {if-non-null target = {seek one-graphic} then
                {return target}
            }
        }
    }
    {return null}
}
 
{let consume-context?:bool = true}
 
|| An event handler to consume unwanted context menus.
{def context-consumer =
    {on e:ContextMenuEvent do
        {if consume-context? then
            {e.consume}
        }
    }
}
 
{DateField
    {on e:AttachEvent at df:DateField do
        || To be safe, wait a moment until all attachment is done.
        {after 0s do
            {if-non-null tfui = {seek df} then
                {if not {tfui.event-handler-present? context-consumer} then
                    {tfui.add-event-handler context-consumer}
                }
            }
        }
    },
    {on e:PointerEnvelopeEvent at df:DateField do
        {type-switch e.contents
         case pp:PointerButtonEvent do
            {if pp.click-count == 2 then
                set consume-context? = false
                || Turn consuming back on after this pointer event is over.
                {after 0s do
                    set consume-context? = true
                }
            }
        }
    }
}


Note that if the TextFieldUI being used by the DateField changed, you'd have to add the event handler to the new UI. So to be totally safe, you'd want to put code like this into a subclass of some DateFieldUI and notice any TextFieldUI changes (you could also simplify the above in that case, since you wouldn't have to go searching for various Graphics, etc.). For the regular Curl-provided DateFieldUIs, at present, I believe that the TextFieldUI would only change if you changed the show-spin-buttons? option, so you could just use the above in places where that option does not change on the fly.