This Question is Answered

15 "helpful" answers available (3 pts)
5 Replies Last post: Jul 2, 2008 12:33 AM by tdeng

I need a DateField looks like a TextField.

Jun 12, 2008 1:55 AM

Click to view tdeng's profile Level 3 tdeng 35 posts since
Oct 17, 2007
I want to hide the calendar dropdown button on the right side of DateField, so the DateField will look like a TextField. How could this be done?
Click to view Kamal's profile Curl Kamal 149 posts since
Oct 17, 2007
1. Re: I need a DateField looks like a TextField. Jun 12, 2008 5:29 AM
For this you will need to provide your own UI for the DateField. A simple way of doing this is to sub class an existing DateFieldUI. In the example below I am sub classing the StandardDateFieldUI.


{curl 6.0 applet}
{curl-file-attributes character-encoding = "windows-latin-1"}
 
{define-class public open CustomDateFieldUI 
  {inherits StandardDateFieldUI}
 
  field private calendar-button:#Graphic 
 
   {constructor public {default
                          control:#DateField = null,
                          ...
                      }
     {construct-super.StandardDateFieldUI control = control, ...}
   }
 
  {method protected open {create-main-panel}:Graphic
    let constant g:Grid = 
        {Grid 
            margin = 2pixel,
            control-appearance-changeable? = true
        }
    
    {g.add
        {self.create-date-editor},
        left = {g.left},
        top = {g.top},
        right = {g.right},
        bottom = {g.bottom},
        vorigin = {g.vorigin-fiducial}
    }
    
    {return g}
  }
  
}
 
{DateField}
 
{DateField  value = {DateTime}, ui-object = {CustomDateFieldUI}}
 
{DateField  ui-object = {CustomDateFieldUI}}
 
Click to view tdeng's profile Level 3 tdeng 35 posts since
Oct 17, 2007
2. Re: I need a DateField looks like a TextField. Jun 12, 2008 7:35 AM
in response to: Kamal
Hi Kamal!
Thank you for your quick answer.

I wonder whether this customization could be used together with styled controls in version 6. I find it works with version 7 (uses styled controls by default).
Click to view Kamal's profile Curl Kamal 149 posts since
Oct 17, 2007
3. Re: I need a DateField looks like a TextField. Jun 12, 2008 7:49 AM
in response to: tdeng
It will work in 7.0 as StandardDateFieldUI is in 7.0 too and in my example I am passing the CustomDateFiledUI object to the DateField control (and hence this DateField will always use the ui object that I passed it). Notice however that the DateField you will get will look like a StandardDateFieldUI except the drop down button. If you want to look like the skinned DateFieldUI then you will have to subclass the skinned DateFieldUI.
Click to view carl's profile Curl carl 83 posts since
Oct 17, 2007
4. Re: I need a DateField looks like a TextField. Jun 12, 2008 10:46 PM
in response to: Kamal
Actually, for styled controls, you have a number of options. Two reasonably easy ones are demoed below. Depending on your other requirements, you might find one or the other more useful.

1) Collapse the button using style rules

The Graphic in question has the StyleRule "DateField/calendar-button", which you can use to set its width using a StyleManager/StyleSheet, like so:

{value
    || In version 6.0, this line would be {StyleSheet.from-url <foo>}:
    def style = {get-default-style-sheet}
    {style.rules.append
        {StyleRule
            "DateField/calendar-button",
            width = 0px
        }
    }
    def manager = {BasicStyleManager style}
    {DateField
        style-manager = manager
    }
}


Note that the style manager here would best be set farther up in the graphical hierarchy if you have other changes or are using multiple DateFields like this.

2) Swap in a blank Graphic for the CommandButton

You could override the DateFieldSkin instead of the UI object. Which class you change depends on your planned usages, but the override of the ControlSkin is somewhat more correct. The idea is that skinnable controls do not guarantee what kind of object is delivered for any given content tag, so you can swap in a blank instead of the dropdown button.

(You might use LookAndFeel registrations in an actual application rather than setting the ui-object directly as in the example below. You might also want to platform-switch the control-skin to account for differences on the mac, but here that was omitted for brevity.)

Changing the ControlSkin:

{define-class public open PlainSkinnedDateFieldSkin
  {inherits SkinnedDateFieldSkin}
 
  {method protected open {get-tagged-content content-tag:String}:#Graphic
    || Substitute a blank for the calendar button.
    {if content-tag == "calendar-button" then
        {return {Fill width = 0m, height = 0m}}
    }
    {return {super.get-tagged-content content-tag}}
  }
 
}
 
{DateField
    ui-object =
        {PlainSkinnableDateFieldUI
            control-skin = {PlainSkinnedDateFieldSkin},
            control-feel = {SkinnedDateFieldFeel}
        }
}


(On the UI, you'd override the create-dropdown-button method to return a Fill and not call super.)
Click to view tdeng's profile Level 3 tdeng 35 posts since
Oct 17, 2007
5. Re: I need a DateField looks like a TextField. Jul 2, 2008 12:33 AM
in response to: carl
I didn't notice your reply :(

Your two approaches seem to be useful for my future plan. I'll consider these when I begin to switch to using skinnable controls in my project.
Thanks a lot!