<?xml version="1.0" encoding="UTF-8"?>

  <display id="win" anchor="ne"
           window-flags="sticky, below">

  <meta author="Dave Minogue, Joe Sapp, Robert Pastierovic"
        version="0.62"
        name="Calendar"
        category="Date/Time"
        description="A navigable calendar showing the entire month."
        preview="cal.png"/>

  # Retrieve the time and calendar controls
  <control id="time" interface="ITime:9y703dqtfnv4w373caserz68r"/>
  <control id="cal" interface="ICalendar:cfw9o85ptery8egvoff6ijswc"/>
  
   <frame id="monthframe" width="200">
    <group id="displaygroup" width="100%" 
           on-scroll="scroll_month(self.event.direction)">
     <image id="bg_image" image-width="100%" uri=""/>
     <image id="monthimage" width="100%" x="0" y="0" anchor="nw"/>
     
     <group id="month_area" relative-to="monthimage,y" anchor="n" x="50%" y="5">
       <label id="prev_month" font="Sans Bold 8" color="#90909020" value="&amp;lt;&amp;lt;  " on-click="changeMonth(-11)" on-enter="self.color='#000000'" on-leave="self.color='#90909020'"/>
       <label id="month" font="Sans Bold 10" color="#a0a0a0" relative-to="prev_month,x"/>
       <label id="next_month" font="Sans Bold 8" color="#90909020" relative-to="month,x" value="  &amp;gt;&amp;gt;" on-click="changeMonth(1)" on-enter="self.color='#000000'" on-leave="self.color='#90909020'"/>
     </group>
     
     <array id="week_array" length="7" relative-to="month_area,y" y="0.1cm" anchor="n">
       <label id="weekday" font="Sans Bold 7" color="#a0a0a0" value=""/>
     </array>
     
     <array id="day_array" length="32" relative-to="week_array,y" y="0.1cm" anchor="n">
       <group id="day_bg">
       <label id="day" font="Sans Bold 7" color="#a0a0a0" value=""/>
       </group>
     </array>
     
     <label id="event_info" anchor="n" font="Sans Bold 7" relative-to="day_array,y" y="0.1cm" value=""/>
     <image id="empty" height="5" width="100%" x="0" y="0" relative-to="event_info,y" uri=""/>
    </group>
   </frame>
   
  <prefs callback="prefs_cb">
    <page label="Image">
      <title label="Month Image"/>
        <boolean label="Show Month Image" bind="month_image_visible"
                 help="Show the image at the top?"/>
        <boolean id="image_of_month_bool" label="Default image of the Month" bind="image_of_month"
                 help="Show default image for each month?"/>
        <boolean id="rand_image_bool" label="Random Image" bind="rand_image"
                 help="Show a random image? (uses internet connection)"/>
        <uri id="cal_image_uri" label="Month image:" bind="month_image"/>
        
      <title label="Background"/>
        <boolean label="Show Background Image" bind="background_image_visible"
                 help="Show the background image?"/>
        <uri id="bg_image_uri" label="Background Image:" bind="bg_image"/>
        <color id="bg_colour_color" label="Background Colour" bind="bg_colour"/>
    </page>
    
    <page label="Text">
      <title label="Fonts"/>
        <font label="Month Name:" bind="month_font"/>
        <color label="Month Name:" bind="month_font_colour"/>
        <font label="Week Day:" bind="dow_font"/>
        <color label="Week Day:" bind="dow_font_colour"/>
        <font label="Date:" bind="date_font"/>
        <color label="Date:" bind="date_font_colour"/>
    </page>

    <page label="Appearance">
      <title label="Frame"/>
        <boolean label="Show Frame" bind="monthframe_visible" help="Show the frame?"/>
        <color id="frame_colour_enabled" label="Frame Colour:" bind="frame_colour"/>
      <title label="Other Options"/>
        <enum label="Month and Year separator:" bind="my_sep">
          <item label="None"              value=" "/>
          <item label="Hyphen (-)"        value=" - "/>
          <item label="Colon (:)"         value=" : "/>
          <item label="Double Colon (::)" value=" :: "/>
          <item label="Tilde (~)"         value=" ~ "/>
          <item label="Slash (/)"         value=" / "/>
          <item label="Backslash (\)"     value=" \ "/>
        </enum>
        <integer label="Day of week characters:" bind="dow_chars" 
            min="1" max="3" 
            help="Number of characters the days of the week should be"/>
    </page>

    <page label="Events">
      <title label="Today"/>
        <color id="ev_today_pref" label="Today's Color:" bind="today_col"/>
      <title label="Events"/>
        <boolean label="Show Events" bind="evs"/>
        <enum label="Event Set:" id="EventsSetting" bind="EventsSet">
           <!-- Put EventsSets in here! -->
        </enum>
        <color id="ev_color_pref" label="Event Color:" bind="event_col"/>
        <color id="ev_text_pref" label="Event Text:" bind="event_text_colour"/>
    </page>

    <page label="Other">
      <title label="Languages"/>
        <enum label="Language:" id="language" bind="lang">
           <!-- Put languages in here! -->
        </enum>
      <title label="Other Options"/>
        <enum label="First Weekday:" bind="_firstweekday">
          <item label="Sunday" value="6"/>
          <item label="Monday" value="0"/>
        </enum>
    </page>

  </prefs>

  <!-- This loads the language data and functions -->
  <script uri="lang.script"/>
  <!-- This loads events data -->
  <script uri="events.script"/>
  <!-- This loads calendar functions -->
  <script uri="calendar_mod.script"/>

 <script>
    #<![CDATA[
    _y,_m,_d            = time.date
    today_col           = "#ccffffff"
    event_col           = "#e0e0e0ff"
    event_text_colour   = "#808080ff"
    evs                 = True
    bg_colour           = "#ffffffff"
    frame_colour        = "#000000ff"
    Dsp.monthframe.color = "#000000ff"
    bg_image            = ""
    rand_image          = False
    image_of_month      = False
    monthframe_visible  = True
    month_image_visible = True
    background_image_visible = False
    month_image         = "months/0.png"
    bg_image            = "../../Additions/bg/glass.svg"
    dow_font            = "Sans Bold 7"
    dow_font_colour     = "#a0a0a0"
    date_font           = "Sans Bold 7"
    date_font_colour    = "#a0a0a0"
    month_font          = "Sans Bold 10"
    month_font_colour   = "#a0a0a0"
    dow_chars           = 3
    my_sep              = " - "

    #This defines behaviour of preferences keys
    def prefs_cb(key,value):
      if (key == "_firstweekday" or key == "my_sep" or key == "dow_chars" or \
            key == "today_col" or key == "ev_col" or key=="EventsSet" or key=="lang" or \
            key=="event_col"):
        draw_cal()

      if (key == "frame_colour"):
          Dsp.monthframe.color          = value

      if (key == "monthframe_visible"):
          Prefs.frame_colour_enabled.enabled = value
          draw_cal()

      # displaying of month image
      if (key == "month_image_visible"):
          Dsp.monthimage.visible        = value
          Dsp.bg_image.image_height     = Unit(100,PERCENT)

      # enable/disable other prefs buttons accordingly
      Prefs.cal_image_uri.enabled = Prefs.image_of_month_bool.enabled = Prefs.rand_image_bool.enabled = month_image_visible
      
      # uri for own image from file
      if (key == "month_image"):
        Dsp.monthimage.uri          = month_image
      
      # default month image for each month
      if (key == "image_of_month"):
        if image_of_month:
          Dsp.monthimage.uri            = "months/" + str(_m) + ".png"
        else:
          Dsp.monthimage.uri            = month_image

      # random image
      if (key == "rand_image"):
        if (value):
          Dsp.monthimage.uri          = "http://www.firefoxed.com/c/img.php"
        else:
          Dsp.monthimage.uri          = month_image

      # behavior of other prefs accordingly
      if month_image_visible:
        if image_of_month:
          Prefs.cal_image_uri.enabled = Prefs.rand_image_bool.enabled = not image_of_month
        elif rand_image:
          Prefs.cal_image_uri.enabled = Prefs.image_of_month_bool.enabled = not rand_image

      # background image
      if (key == "background_image_visible"):
        Dsp.bg_image.uri              = bg_image
        Dsp.bg_image.visible          = value
        Dsp.bg_image.image_height     = Unit(100,PERCENT)
        Prefs.bg_image_uri.enabled    = value
        Prefs.bg_colour_color.enabled = not value
        if (value):
          Dsp.displaygroup.bg_color     = "#00000000"
        else:
          Dsp.displaygroup.bg_color     = bg_colour

      if (key == "bg_image"):
        if background_image_visible:
          Dsp.bg_image.uri              = bg_image
          Dsp.bg_image.visible          = True
          Dsp.bg_image.image_height     = Unit(100,PERCENT)
          Dsp.displaygroup.bg_color     = "#00000000"
        else:
          Dsp.bg_image.visible      = False
          Dsp.displaygroup.bg_color = bg_colour

      if (key == "bg_colour"):
        if not background_image_visible:
          Dsp.displaygroup.bg_color   = value
       
      if (key == "event_text_colour"):
        Dsp.event_info.color          = value
        
      if (key == "dow_font"):
        for j in range(Dsp.week_array.length):
          Dsp.weekday[j].font = value
          
      if (key == "dow_font_colour"):
        for j in range(Dsp.week_array.length):
          Dsp.weekday[j].color = value 
          
      if (key == "month_font"):
        Dsp.month.font = value
        
      if (key == "month_font_colour"):
        Dsp.month.color = value
        
      if (key == "date_font"):
        for j in range(Dsp.day_array.length-1):
          Dsp.day[j].font = value
          
      if (key == "date_font_colour"):
        for j in range(Dsp.day_array.length-1):
          Dsp.day[j].color = value
        draw_cal()
      
      # events keys
      if (key == "evs"):
        Prefs.ev_color_pref.enabled = Prefs.ev_text_pref.enabled = Prefs.EventsSetting.enabled = value
        draw_cal()

    # update display with new date
    def do_date(value):
        y, m, d = value
        update_display(d, cal.day, m, y)


    def update_display(dt, dy, m, y):
        dt = int(dt)
        m  = int(m)
        draw_cal()

    # draw calendar
    def draw_cal(yr=0, mon=0):
        y,m,d = time.date
        this_month = True

        if (yr != 0 or mon != 0) and (yr != y or mon != m):
            this_month = False

        if yr != 0 and mon != 0:
            y = yr
            m = mon
            Dsp.month.value = month_i18n(m)
        elif yr == 0 and mon != 0:
            m = mon
        elif yr != 0 and mon == 0:
            y = yr

        Dsp.month.value = ''.join( [month_i18n(m), my_sep, str(y)] )
        day1, ndays     = monthrange(y, m)
        month_matrix    = monthcalendar(y, m)
        offset          = 0
        
        # draw background image or bg colour
        if background_image_visible:
          Dsp.bg_image.uri          = bg_image
          Dsp.bg_image.visible      = True
          Dsp.displaygroup.bg_color = "#00000000"
        else:
          Dsp.bg_image.visible      = False
          Dsp.displaygroup.bg_color = bg_colour

        # draw frame
        if monthframe_visible:
          Dsp.monthframe.color = frame_colour
        else:
          Dsp.monthframe.color = frame_colour[:7] + "00"
        
        Dsp.event_info.color    = event_text_colour

        # draw month image
        if image_of_month:
          Dsp.monthimage.uri          = "months/" + str(_m) + ".png"
        elif rand_image:
          Dsp.monthimage.uri          = "http://www.firefoxed.com/c/img.php"
        else:
          Dsp.monthimage.uri          = month_image
        Dsp.monthimage.visible  = month_image_visible
        
        if _firstweekday == `MONDAY`:
          for j in range(Dsp.week_array.length):
            Dsp.weekday[j].value = day_abbr_i18n(j)[0:dow_chars]
            Dsp.weekday[j].x = Unit( (j) * 0.6, CM )
        else:
          for j in range(Dsp.week_array.length):
            Dsp.weekday[j].value = day_abbr_i18n(j-1)[0:dow_chars]
            Dsp.weekday[j].x = Unit( (j) * 0.6, CM )
            
          Dsp.weekday[0].value = day_abbr_i18n(6)[0:dow_chars]
          Dsp.weekday[0].x = Unit( 0, CM )
          offset=1

        # draw events
        if evs:
          for j in range(Dsp.day_array.length-1):
            if day1+offset > 6:
             day1=-1
            if j < ndays:
              Dsp.day_bg[j].x = Unit( ((j+day1+offset)%7) * 0.6, CM )
              Dsp.day_bg[j].y = Unit( ((j+day1+offset)/7) * 0.4, CM )
              Dsp.day[j].value=j+1
              
              if (EventsList[EventsSetList2.index(EventsSet)][m-1][j+1] != ""):
                Dsp.day_bg[j].bg_color = event_col
                Dsp.day_bg[j].on_enter = ''.join( ["Dsp.event_info.value='", EventsList[EventsSetList2.index(EventsSet)][m-1][j+1], "'"] )
                Dsp.day_bg[j].on_leave = "Dsp.event_info.value=''"
              else:
                Dsp.day_bg[j].bg_color = "#00000000"
                # Reset on_enter
                Dsp.day_bg[j].on_enter = ""
                
              if((j+1) == d) and this_month:
                Dsp.day_bg[j].bg_color = today_col
                
            else:
              Dsp.day[j].value = ""  
              Dsp.day_bg[j].bg_color = "#00000000"
              # Reset on_enter
              Dsp.day_bg[j].on_enter = ""
        if not evs:
          for j in range(Dsp.day_array.length-1):
            if day1+offset > 6:
             day1=-1
            if j < ndays:
              Dsp.day_bg[j].x = Unit( ((j+day1+offset)%7) * 0.6, CM )
              Dsp.day_bg[j].y = Unit( ((j+day1+offset)/7) * 0.4, CM )
              Dsp.day[j].value=j+1
              Dsp.day_bg[j].bg_color = "#00000000"
              # Reset on_enter
              Dsp.day_bg[j].on_enter = ""
              if((j+1) == d) and this_month:
                Dsp.day_bg[j].bg_color = today_col
            else:
              Dsp.day[j].value = ""  
              Dsp.day_bg[j].bg_color = "#00000000"
              # Reset on_enter
              Dsp.day_bg[j].on_enter = ""

        Dsp.day_bg[31].y = Unit( 5*0.4, CM )
        Dsp.day_bg[31].x = Unit( 6*0.6, CM )


    # prev/next month
    def changeMonth(key):
      y,m,d = time.date
      global _m,_y
      if key > 0:
        if _m != 12:
          _m += 1
        else:
          _y += 1
          _m = 1
      elif key < 0:
        if _m != 1:
          _m -= 1
        else:
          _m = 12
          _y -= 1
      draw_cal(_y, _m)
      
    # change month with mouse scroll
    def scroll_month(d):
      if d == 0:
        # scroll up
        changeMonth(-11)
      else:
        # scroll down
        changeMonth(1)
    

    # watch date and time
    cal.time = time
    time.bind("date", do_date)
    do_date(time.date)

  ]]>
 </script>
</display>
