realbasic-nug
[Top] [All Lists]

Re: needed for Zymail: a constrained date formatter

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: needed for Zymail: a constrained date formatter
From: Jonathan Johnson <jon at alacatialabs dot com>
Date: Fri, 30 Nov 2007 22:52:40 -0600
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
On Nov 30, 2007, at 10:48 PM, Joe Strout wrote:

> On Nov 30, 2007, at 9:31 PM, Thom McGrath wrote:
>
>> So like Mail.app's date columns?
>
> Yes, exactly.

How about this:

Sub FormatConstrained(extends d as Date, g as Graphics, width as  
Integer, byref outLeftPart as String, byref outRightPart as String)
   outLeftPart = ""
   outRightPart = d.ShortTime
   dim rightPartWidth as Double = g.StringWidth(outRightPart)
   dim leftPartWidth as Double
   const partSpacing = 8
   dim maxLeftPartWidth as Double = width - partSpacing - rightPartWidth

   // Substitute strings for well known dates: Today, Yesterday, and  
Tomorrow
   dim testDay as new Date
   if DatesMatch(testDay, d) then
     outLeftPart = "Today"
   else
     testDay.Day = testDay.Day - 1
     if DatesMatch(testDay, d) then
       outLeftPart = "Yesterday"
     else
       testDay.Day = testDay.Day + 2
       if DatesMatch(testDay, d) then
         outLeftPart = "Tomorrow"
       end if
     end if
   end if

   if outLeftPart = "" then
     // Try all of the formats, starting with the longest. Go smaller  
until we fit.
     outLeftPart = d.LongDate
     leftPartWidth = g.StringWidth(outLeftPart)
     if leftPartWidth < maxLeftPartWidth then return

     outLeftPart = d.AbbreviatedDate
     leftPartWidth = g.StringWidth(outLeftPart)
     if leftPartWidth < maxLeftPartWidth then return

     outLeftPart = d.ShortDate
     leftPartWidth = g.StringWidth(outLeftPart)
     if leftPartWidth < maxLeftPartWidth then return
   end if

   // If we've gotten here, we're in one of two situations:
   //  1) we are using a specific string such as Today
   //  If this is the case, we need to simply check to see if it fits
   leftPartWidth = g.StringWidth(outLeftPart)
   if leftPartWidth < maxLeftPartWidth then return

   //  2) we are not able to fit both the date and time together.
   //  In this situation, we just ignore the right part.
   outRightPart = ""
End Sub

With the helper function:
Private Function DatesMatch(d1 as Date, d2 as Date) As Boolean
   return d1.DayOfYear = d2.DayOfYear and d1.Year = d2.Year
End Function

And usage:

Sub Paint(g As Graphics)
   dim d as new Date
   dim left, right as string
   d.FormatConstrained(g, g.Width, left, right)
   g.DrawString left, 0, g.TextAscent
   g.DrawString right, g.Width - g.StringWidth(right), g.TextAscent
End Sub


HTH,
Jon


-- 
Jonathan Johnson
President
Alacatia Labs, Inc.
http://www.alacatialabs.com/


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>