]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/lib/calendar.py
Fix "warning: operation on 'y' may be undefined".
[wxWidgets.git] / wxPython / wx / lib / calendar.py
index f5e708763a3a3344014b5af2ccdf13d66180c5c2..4c8c3b4a4890f3f8b370e2847d45c256ce7f59ac 100644 (file)
 #       integer argument expected, got float
 #   newobj = _core.new_Rect(*args, **kwargs)
 # 
 #       integer argument expected, got float
 #   newobj = _core.new_Rect(*args, **kwargs)
 # 
+# 12/17/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o A few style-guide nips and tucks
+# o Renamed wxCalendar to Calendar
+# o Couple of bugfixes
+#
+# 06/02/2004 - Joerg "Adi" Sieker adi@sieker.info
+#
+# o Changed color handling, use dictionary instead of members.
+#   This causes all color changes to be ignored if they manipluate the members directly.
+#   SetWeekColor and other method color methods were adapted to use the new dictionary.
+# o Added COLOR_* constants
+# o Added SetColor method for Calendar class
+# o Added 3D look of week header
+# o Added colors for 3D look of header
+# o Fixed width calculation.
+#   Because of rounding difference the total width and height of the
+#   calendar could be up to 6 pixels to small. The last column and row
+#   are now wider/taller by the missing amount.
+# o Added SetTextAlign method to wxCalendar. This exposes logic
+#   which was already there.
+# o Fixed CalDraw.SetMarg which set set_x_st and set_y_st which don't get used anywhere.
+#   Instead set set_x_mrg and set_y_mrg
+# o Changed default X and Y Margin to 0.
+# o Added wxCalendar.SetMargin.
+#
+# 17/03/2004 - Joerg "Adi" Sieker adi@sieker.info
+# o Added keyboard navigation to the control.
+#   Use the cursor keys to navigate through the ages. :)
+#   The Home key function as go to today
+# o select day is now a filled rect instead of just an outline
+#
+# 15/04/2005 - Joe "shmengie" Brown joebrown@podiatryfl.com
+# o Adjusted spin control size/placement (On Windows ctrls were overlapping).
+# o Set Ok/Cancel buttons to wx.ID_OK & wx.ID_CANCEL to provide default dialog
+#   behaviour.
+# o If no date has been clicked clicked, OnOk set the result to calend's date,
+#   important if keyboard only navigation is used.
+
 
 
-import  wx
+import wx
 
 from CDate import *
 
 
 from CDate import *
 
@@ -42,6 +81,21 @@ CalDays = [6, 0, 1, 2, 3, 4, 5]
 AbrWeekday = {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"}
 _MIDSIZE = 180
 
 AbrWeekday = {6:"Sun", 0:"Mon", 1:"Tue", 2:"Wed", 3:"Thu", 4:"Fri", 5:"Sat"}
 _MIDSIZE = 180
 
+COLOR_GRID_LINES = "grid_lines"
+COLOR_BACKGROUND = "background"
+COLOR_SELECTION_FONT = "selection_font"
+COLOR_SELECTION_BACKGROUND = "selection_background"
+COLOR_BORDER = "border"
+COLOR_HEADER_BACKGROUND = "header_background"
+COLOR_HEADER_FONT = "header_font"
+COLOR_WEEKEND_BACKGROUND = "weekend_background"
+COLOR_WEEKEND_FONT = "weekend_font"
+COLOR_FONT = "font"
+COLOR_3D_LIGHT = "3d_light"
+COLOR_3D_DARK = "3d_dark"
+COLOR_HIGHLIGHT_FONT = "highlight_font"
+COLOR_HIGHLIGHT_BACKGROUND = "highlight_background"
+
 BusCalDays = [0, 1, 2, 3, 4, 5, 6]
 
 # Calendar click event - added 12/1/03 by jmg (see above)
 BusCalDays = [0, 1, 2, 3, 4, 5, 6]
 
 # Calendar click event - added 12/1/03 by jmg (see above)
@@ -56,6 +110,30 @@ def GetMonthList():
             monthlist.append(name)
     return monthlist
 
             monthlist.append(name)
     return monthlist
 
+def MakeColor(in_color):
+    try:
+        color = wxNamedColour(in_color)
+    except:
+        color = in_color
+    return color
+
+def DefaultColors():
+    colors = {}
+    colors[COLOR_GRID_LINES] = 'BLACK'
+    colors[COLOR_BACKGROUND] = 'WHITE'
+    colors[COLOR_SELECTION_FONT]  =  wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
+    colors[COLOR_SELECTION_BACKGROUND] =wx.Colour(255,255,225)
+    colors[COLOR_BORDER] = 'BLACK'
+    colors[COLOR_HEADER_BACKGROUND] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE)
+    colors[COLOR_HEADER_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
+    colors[COLOR_WEEKEND_BACKGROUND] = 'LIGHT GREY'
+    colors[COLOR_WEEKEND_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
+    colors[COLOR_FONT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOWTEXT)
+    colors[COLOR_3D_LIGHT] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT)
+    colors[COLOR_3D_DARK] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_BTNSHADOW)
+    colors[COLOR_HIGHLIGHT_FONT]  =  wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)
+    colors[COLOR_HIGHLIGHT_BACKGROUND] = wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT)
+    return colors
 # calendar drawing routing
 
 class CalDraw:
 # calendar drawing routing
 
 class CalDraw:
@@ -67,12 +145,15 @@ class CalDraw:
         except:
             self.scale = 1
 
         except:
             self.scale = 1
 
+        self.gridx = []
+        self.gridy = []
+
         self.DefParms()
 
     def DefParms(self):
         self.DefParms()
 
     def DefParms(self):
-        self.num_auto = True       # auto scale of the cal number day size
-        self.num_size = 12          # default size of calendar if no auto size
-        self.max_num_size = 12     # maximum size for calendar number
+        self.num_auto = True    # auto scale of the cal number day size
+        self.num_size = 12      # default size of calendar if no auto size
+        self.max_num_size = 12  # maximum size for calendar number
 
         self.num_align_horz = wx.ALIGN_CENTRE    # alignment of numbers
         self.num_align_vert = wx.ALIGN_CENTRE
 
         self.num_align_horz = wx.ALIGN_CENTRE    # alignment of numbers
         self.num_align_vert = wx.ALIGN_CENTRE
@@ -83,16 +164,7 @@ class CalDraw:
         self.week_size = 10
         self.max_week_size = 12
 
         self.week_size = 10
         self.max_week_size = 12
 
-        self.grid_color = 'BLACK'       # grid and selection colors
-        self.back_color = 'WHITE'
-        self.sel_color = 'RED'
-
-        self.high_color = 'LIGHT BLUE'
-        self.border_color = 'BLACK'
-        self.week_color = 'LIGHT GREY'
-
-        self.week_font_color = 'BLACK'      # font colors
-        self.day_font_color = 'BLACK'
+        self.colors = DefaultColors()
 
         self.font = wx.SWISS
         self.bold = wx.NORMAL
 
         self.font = wx.SWISS
         self.bold = wx.NORMAL
@@ -106,9 +178,12 @@ class CalDraw:
         self.show_weekend = False
         self.cal_type = "NORMAL"
 
         self.show_weekend = False
         self.cal_type = "NORMAL"
 
-    def SetWeekColor(self, font_color, week_color):     # set font and background color for week title
-        self.week_font_color = font_color
-        self.week_color = week_color
+    def SetWeekColor(self, font_color, week_color):
+        # set font and background color for week title
+        self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
+        self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
+        self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
+        self.colors[COLOR_3D_DARK] = MakeColor(week_color)
 
     def SetSize(self, size):
         self.set_sizew = size[0]
 
     def SetSize(self, size):
         self.set_sizew = size[0]
@@ -120,79 +195,84 @@ class CalDraw:
         self.set_cy_st = 0      # start position
         self.set_cx_st = 0
 
         self.set_cy_st = 0      # start position
         self.set_cx_st = 0
 
-        self.set_y_mrg = 15      # start of vertical draw default
-        self.set_x_mrg = 10
-        self.set_y_end = 10
-
+        self.set_y_mrg = 1      # start of vertical draw default
+        self.set_x_mrg = 1
+        self.set_y_end = 1
     def SetPos(self, xpos, ypos):
         self.set_cx_st = xpos
         self.set_cy_st = ypos
 
     def SetMarg(self, xmarg, ymarg):
     def SetPos(self, xpos, ypos):
         self.set_cx_st = xpos
         self.set_cy_st = ypos
 
     def SetMarg(self, xmarg, ymarg):
-        self.set_x_st = xmarg
-        self.set_y_st = ymarg
+        self.set_x_mrg = xmarg
+        self.set_y_mrg = ymarg
         self.set_y_end = ymarg
 
     def InitScale(self):        # scale position values
         self.set_y_end = ymarg
 
     def InitScale(self):        # scale position values
-        self.sizew = self.set_sizew * self.pwidth
-        self.sizeh = self.set_sizeh * self.pheight
+        self.sizew = int(self.set_sizew * self.pwidth)
+        self.sizeh = int(self.set_sizeh * self.pheight)
 
 
-        self.cx_st = self.set_cx_st * self.pwidth       # draw start position
-        self.cy_st = self.set_cy_st * self.pheight
+        self.cx_st = int(self.set_cx_st * self.pwidth)       # draw start position
+        self.cy_st = int(self.set_cy_st * self.pheight)
 
 
-        self.x_mrg = self.set_x_mrg * self.pwidth         # calendar draw margins
-        self.y_mrg = self.set_y_mrg * self.pheight
-        self.y_end = self.set_y_end * self.pheight
+        self.x_mrg = int(self.set_x_mrg * self.pwidth)         # calendar draw margins
+        self.y_mrg = int(self.set_y_mrg * self.pheight)
+        self.y_end = int(self.set_y_end * self.pheight)
 
     def DrawCal(self, DC, sel_lst=[]):
 
     def DrawCal(self, DC, sel_lst=[]):
-        self.DC = DC
         self.InitScale()
 
         self.InitScale()
 
-        self.DrawBorder()
+        self.DrawBorder(DC)
 
         if self.hide_title is False:
 
         if self.hide_title is False:
-            self.DrawMonth()
+            self.DrawMonth(DC)
 
         self.Center()
 
 
         self.Center()
 
-        self.DrawGrid()
+        self.DrawGrid(DC)
         self.GetRect()
         self.GetRect()
-
         if self.show_weekend is True:       # highlight weekend dates
             self.SetWeekEnd()
 
         self.AddSelect(sel_lst)     # overrides the weekend highlight
 
         if self.show_weekend is True:       # highlight weekend dates
             self.SetWeekEnd()
 
         self.AddSelect(sel_lst)     # overrides the weekend highlight
 
-        self.DrawSel()      # highlighted days
-        self.DrawWeek()
-        self.DrawNum()
+        self.DrawSel(DC)      # highlighted days
+        self.DrawWeek(DC)
+        self.DrawNum(DC)
 
     def AddSelect(self, list, cfont=None, cbackgrd = None):
         if cfont is None:
 
     def AddSelect(self, list, cfont=None, cbackgrd = None):
         if cfont is None:
-            cfont = self.sel_color      # font digit color
-
+            cfont = self.colors[COLOR_SELECTION_FONT]      # font digit color
         if cbackgrd is None:
         if cbackgrd is None:
-            cbackgrd = self.high_color     # select background color
+            cbackgrd = self.colors[COLOR_SELECTION_BACKGROUND]     # select background color
 
         for val in list:
             self.cal_sel[val] = (cfont, cbackgrd)
 
     # draw border around the outside of the main display rectangle
 
         for val in list:
             self.cal_sel[val] = (cfont, cbackgrd)
 
     # draw border around the outside of the main display rectangle
-    def DrawBorder(self):   
-        brush = wx.Brush(wx.NamedColour(self.back_color), wx.SOLID)
-        self.DC.SetBrush(brush)
-        self.DC.SetPen(wx.Pen(wx.NamedColour(self.border_color), 1))
+    def DrawBorder(self, DC, transparent = False):   
+        if self.outer_border is True:
+            if transparent == False:
+                brush = wx.Brush(MakeColor(self.colors[COLOR_BACKGROUND]), wx.SOLID)
+            else:
+                brush = wx.TRANSPARENT_BRUSH
+            DC.SetBrush(brush)
+            DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BORDER])))
+            # full display window area
+            rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)  
+            DC.DrawRectangleRect(rect)
 
 
+    def DrawFocusIndicator(self, DC):
         if self.outer_border is True:
         if self.outer_border is True:
+            DC.SetBrush(wx.TRANSPARENT_BRUSH)
+            DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_HIGHLIGHT_BACKGROUND]), style=wx.DOT))
             # full display window area
             rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)  
             # full display window area
             rect = wx.Rect(self.cx_st, self.cy_st, self.sizew, self.sizeh)  
-            self.DC.DrawRectangleRect(rect)
+            DC.DrawRectangleRect(rect)
 
     def DrawNumVal(self):
         self.DrawNum()
 
 
     def DrawNumVal(self):
         self.DrawNum()
 
-# calculate the calendar days and offset position
-
+    # calculate the calendar days and offset position
     def SetCal(self, year, month):
         self.InitValues()       # reset initial values
 
     def SetCal(self, year, month):
         self.InitValues()       # reset initial values
 
@@ -211,47 +291,71 @@ class CalDraw:
 
         self.st_pos = start_pos
 
 
         self.st_pos = start_pos
 
-        self.cal = []
+        self.cal_days = []
         for i in range(start_pos):
         for i in range(start_pos):
-            self.cal.append('')
+            self.cal_days.append('')
 
         i = 1
         while i <= dim:
 
         i = 1
         while i <= dim:
-            self.cal.append(str(i))
+            self.cal_days.append(str(i))
             i = i + 1
 
             i = i + 1
 
+        self.end_pos = start_pos + dim - 1
+
         return start_pos
 
         return start_pos
 
-    def SetWeekEnd(self, font_color='BLACK', backgrd = 'LIGHT GREY'):
+    def SetWeekEnd(self, font_color=None, backgrd = None):
+        if font_color != None:
+            self.SetColor(COLOR_WEEKEND_FONT, MakeColor(font_color))
+        if backgrd != None:
+            self.SetColor(COLOR_WEEKEND_BACKGROUND, MakeColor(backgrd))
+
         date = 6 - int(self.dow)     # start day of first saturday
 
         while date <= self.dim:
         date = 6 - int(self.dow)     # start day of first saturday
 
         while date <= self.dim:
-            self.cal_sel[date] = (font_color, backgrd)  # Saturday
+            self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND))  # Saturday
             date = date + 1
 
             if date <= self.dim:
             date = date + 1
 
             if date <= self.dim:
-                self.cal_sel[date] = (font_color, backgrd)      # Sunday
+                self.cal_sel[date] = (self.GetColor(COLOR_WEEKEND_FONT), self.GetColor(COLOR_WEEKEND_BACKGROUND))      # Sunday
                 date = date + 6
             else:
                 date = date + 7
 
                 date = date + 6
             else:
                 date = date + 7
 
-    def GetRect(self):      # get the display rectange list of the day grid
+    # get the display rectange list of the day grid
+    def GetRect(self):      
         cnt = 0
         cnt = 0
+        h = 0
+        w = 0
         for y in self.gridy[1:-1]:
         for y in self.gridy[1:-1]:
+            if y == self.gridy[-2]:
+                h = h + self.restH
+
             for x in self.gridx[:-1]:
             for x in self.gridx[:-1]:
-                rect = wx.Rect(x, y, self.dl_w, self.dl_h)  # create rect region
+                assert type(y) == int
+                assert type(x) == int
+
+                w = self.cellW
+                h = self.cellH
+
+                if x == self.gridx[-2]:
+                    w = w + self.restW
+
+                rect = wx.Rect(x, y, w+1, h+1)  # create rect region
+
                 self.rg[cnt] = rect
                 cnt = cnt + 1
 
         return self.rg
 
     def GetCal(self):
                 self.rg[cnt] = rect
                 cnt = cnt + 1
 
         return self.rg
 
     def GetCal(self):
-        return self.cal
+        return self.cal_days
 
     def GetOffset(self):
         return self.st_pos
 
 
     def GetOffset(self):
         return self.st_pos
 
-    def DrawMonth(self):        # month and year title
+    # month and year title
+    def DrawMonth(self, DC):
         month = Month[self.month]
 
         sizef = 11
         month = Month[self.month]
 
         sizef = 11
@@ -259,26 +363,27 @@ class CalDraw:
             sizef = 10
 
         f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
             sizef = 10
 
         f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
-        self.DC.SetFont(f)
+        DC.SetFont(f)
 
 
-        tw,th = self.DC.GetTextExtent(month)
+        tw,th = DC.GetTextExtent(month)
         adjust = self.cx_st + (self.sizew-tw)/2
         adjust = self.cx_st + (self.sizew-tw)/2
-        self.DC.DrawText(month, (adjust, self.cy_st + th))
+        DC.DrawText(month, adjust, self.cy_st + th)
 
         year = str(self.year)
 
         year = str(self.year)
-        tw,th = self.DC.GetTextExtent(year)
+        tw,th = DC.GetTextExtent(year)
         adjust =  self.sizew - tw - self.x_mrg
 
         self.title_offset = th * 2
 
         f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
         adjust =  self.sizew - tw - self.x_mrg
 
         self.title_offset = th * 2
 
         f = wx.Font(sizef, self.font, wx.NORMAL, self.bold)
-        self.DC.SetFont(f)
-        self.DC.DrawText(year, (self.cx_st + adjust, self.cy_st + th))
+        DC.SetFont(f)
+        DC.DrawText(year, self.cx_st + adjust, self.cy_st + th)
 
 
-    def DrawWeek(self):     # draw the week days
-        width = self.gridx[1]-self.gridx[0]
-        height = self.gridy[1] - self.gridy[0]
-        rect_w = self.gridx[7]-self.gridx[0]
+    def DrawWeek(self, DC):     # draw the week days
+        # increase by 1 to include all gridlines
+        width  = self.gridx[1] - self.gridx[0] + 1
+        height = self.gridy[1] - self.gridy[0] + 1
+        rect_w = self.gridx[-1] - self.gridx[0]
 
         f = wx.Font(10, self.font, wx.NORMAL, self.bold)      # initial font setting
 
 
         f = wx.Font(10, self.font, wx.NORMAL, self.bold)      # initial font setting
 
@@ -287,8 +392,8 @@ class CalDraw:
             test_day = ' Sun '
             while test_size > 2:
                 f.SetPointSize(test_size)
             test_day = ' Sun '
             while test_size > 2:
                 f.SetPointSize(test_size)
-                self.DC.SetFont(f)
-                tw,th = self.DC.GetTextExtent(test_day)
+                DC.SetFont(f)
+                tw,th = DC.GetTextExtent(test_day)
 
                 if tw < width and th < height:
                     break
 
                 if tw < width and th < height:
                     break
@@ -296,16 +401,15 @@ class CalDraw:
                 test_size = test_size - 1
         else:
             f.SetPointSize(self.week_size)   # set fixed size
                 test_size = test_size - 1
         else:
             f.SetPointSize(self.week_size)   # set fixed size
-            self.DC.SetFont(f)
+            DC.SetFont(f)
 
 
-        self.DC.SetTextForeground(wx.NamedColour(self.week_font_color))
+        DC.SetTextForeground(MakeColor(self.colors[COLOR_HEADER_FONT]))
 
         cnt_x = 0
         cnt_y = 0
 
 
         cnt_x = 0
         cnt_y = 0
 
-        brush = wx.Brush(wx.NamedColour(self.week_color), wx.SOLID)
-        self.DC.SetBrush(brush)
-        self.DC.DrawRectangle((self.gridx[0], self.gridy[0]), (rect_w+1, height))
+        brush = wx.Brush(MakeColor(self.colors[COLOR_HEADER_BACKGROUND]), wx.SOLID)
+        DC.SetBrush(brush)
 
         if self.cal_type == "NORMAL":
             cal_days = CalDays
 
         if self.cal_type == "NORMAL":
             cal_days = CalDays
@@ -313,153 +417,248 @@ class CalDraw:
             cal_days = BusCalDays
 
         for val in cal_days:
             cal_days = BusCalDays
 
         for val in cal_days:
+            if val == cal_days[-1]:
+                width = width + self.restW
+
             day = AbrWeekday[val]
 
             if self.sizew < 200:
                 day = day[0]
 
             day = AbrWeekday[val]
 
             if self.sizew < 200:
                 day = day[0]
 
-            dw,dh = self.DC.GetTextExtent(day)
+            dw,dh = DC.GetTextExtent(day)
+
             diffx = (width-dw)/2
             diffy = (height-dh)/2
 
             x = self.gridx[cnt_x]
             y = self.gridy[cnt_y]
             diffx = (width-dw)/2
             diffy = (height-dh)/2
 
             x = self.gridx[cnt_x]
             y = self.gridy[cnt_y]
-            self.DC.DrawRectangle((self.gridx[cnt_x], self.gridy[0]), (width+1, height))
-            self.DC.DrawText(day, (x+diffx, y+diffy))
+            pointXY = (x, y)
+            pointWH = (width, height)
+            if self.hide_grid == False:
+                pen = wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), 1, wx.SOLID)
+            else:
+                pen = wx.Pen(MakeColor(self.GetColor(COLOR_BACKGROUND)), 1, wx.SOLID)
+            DC.SetPen(pen)
+            DC.DrawRectanglePointSize( pointXY, pointWH)
+            
+            old_pen = DC.GetPen()
+
+            pen = wx.Pen(MakeColor(self.colors[COLOR_3D_LIGHT]), 1, wx.SOLID)
+            DC.SetPen(pen)
+            # draw the horizontal hilight
+            startPoint = wx.Point(x + 1 , y + 1)
+            endPoint   = wx.Point(x + width - 1, y + 1)
+            DC.DrawLinePoint(startPoint, endPoint )
+
+            # draw the vertical hilight
+            startPoint = wx.Point(x + 1 , y + 1)
+            endPoint   = wx.Point(x + 1, y + height - 2)
+            DC.DrawLinePoint(startPoint, endPoint )
+
+            pen = wx.Pen(MakeColor(self.colors[COLOR_3D_DARK]), 1, wx.SOLID)
+            DC.SetPen(pen)
+            
+            # draw the horizontal lowlight
+            startPoint = wx.Point(x + 1, y + height - 2)
+            endPoint   = wx.Point(x + width - 1, y + height - 2)
+            DC.DrawLinePoint(startPoint, endPoint )
+            
+            # draw the vertical lowlight
+            startPoint = wx.Point(x + width - 2 , y + 2)
+            endPoint   = wx.Point(x + width - 2, y + height - 2)
+            DC.DrawLinePoint(startPoint, endPoint )
+
+            pen = wx.Pen(MakeColor(self.colors[COLOR_FONT]), 1, wx.SOLID)
+            
+            DC.SetPen(pen)
+                
+            point = (x+diffx, y+diffy)
+            DC.DrawTextPoint(day, point)
             cnt_x = cnt_x + 1
 
             cnt_x = cnt_x + 1
 
-    def DrawNum(self):      # draw the day numbers
-        f = wx.Font(10, self.font, wx.NORMAL, self.bold)      # initial font setting
-
+    def _CalcFontSize(self, DC, f):
         if self.num_auto == True:
             test_size = self.max_num_size      # max size
             test_day = ' 99 '
 
             while test_size > 2:
                 f.SetPointSize(test_size)
         if self.num_auto == True:
             test_size = self.max_num_size      # max size
             test_day = ' 99 '
 
             while test_size > 2:
                 f.SetPointSize(test_size)
-                self.DC.SetFont(f)
-                tw,th = self.DC.GetTextExtent(test_day)
+                DC.SetFont(f)
+                tw,th = DC.GetTextExtent(test_day)
 
 
-                if tw < self.dl_w and th < self.dl_h:
+                if tw < self.cellW and th < self.cellH:
                     sizef = test_size
                     break
                 test_size = test_size - 1
         else:
             f.SetPointSize(self.num_size)   # set fixed size
                     sizef = test_size
                     break
                 test_size = test_size - 1
         else:
             f.SetPointSize(self.num_size)   # set fixed size
-            self.DC.SetFont(f)
+            DC.SetFont(f)
+
+    # draw the day numbers
+    def DrawNum(self, DC):      
+        f = wx.Font(10, self.font, wx.NORMAL, self.bold)      # initial font setting
+        self._CalcFontSize(DC, f)
 
         cnt_x = 0
         cnt_y = 1
 
         cnt_x = 0
         cnt_y = 1
-        for val in self.cal:
+        for val in self.cal_days:
             x = self.gridx[cnt_x]
             y = self.gridy[cnt_y]
 
             x = self.gridx[cnt_x]
             y = self.gridy[cnt_y]
 
-            try:
-                num_val = int(val)
-                num_color = self.cal_sel[num_val][0]
-            except:
-                num_color = self.day_font_color
+            self._DrawDayText(x, y, val, f, DC)
 
 
-            self.DC.SetTextForeground(wx.NamedColour(num_color))
-            self.DC.SetFont(f)
+            if cnt_x < 6:
+                cnt_x = cnt_x + 1
+            else:
+                cnt_x = 0
+                cnt_y = cnt_y + 1
 
 
-            tw,th = self.DC.GetTextExtent(val)
+    def _DrawDayText(self, x, y, text, font, DC):
 
 
-            if self.num_align_horz == wx.ALIGN_CENTRE:
-                adj_h = (self.dl_w - tw)/2
-            elif self.num_align_horz == wx.ALIGN_RIGHT:
-                adj_h = self.dl_w - tw
-            else:
-                adj_h = 0   # left alignment
+        try:
+            num_val = int(text)
+            num_color = self.cal_sel[num_val][0]
+        except:
+            num_color = self.colors[COLOR_FONT]
+            
+        DC.SetTextForeground(MakeColor(num_color))
+        DC.SetFont(font)
+
+        tw,th = DC.GetTextExtent(text)
+        
+        if self.num_align_horz == wx.ALIGN_CENTRE:
+            adj_h = (self.cellW - tw)/2
+        elif self.num_align_horz == wx.ALIGN_RIGHT:
+            adj_h = self.cellW - tw
+        else:
+            adj_h = 0   # left alignment
 
 
-            adj_h = adj_h + self.num_indent_horz
+        adj_h = adj_h + self.num_indent_horz
 
 
-            if self.num_align_vert == wx.ALIGN_CENTRE:
-                adj_v = (self.dl_h - th)/2
-            elif self.num_align_horz == wx.ALIGN_RIGHT:
-                adj_v = self.dl_h - th
-            else:
-                adj_v = 0   # left alignment
+        if self.num_align_vert == wx.ALIGN_CENTRE:
+            adj_v = (self.cellH - th)/2
+        elif self.num_align_vert == wx.ALIGN_BOTTOM:
+            adj_v = self.cellH - th
+        else:
+            adj_v = 0   # left alignment
 
 
-            adj_v = adj_v + self.num_indent_vert
+        adj_v = adj_v + self.num_indent_vert
 
 
-            self.DC.DrawText(val, (x+adj_h, y+adj_v))
+        DC.DrawTextPoint(text, (x+adj_h, y+adj_v))
+        
+    def DrawDayText(self, DC, key):
+        f = wx.Font(10, self.font, wx.NORMAL, self.bold)      # initial font setting
+        self._CalcFontSize(DC, f)
 
 
-            if cnt_x < 6:
-                cnt_x = cnt_x + 1
-            else:
-                cnt_x = 0
-                cnt_y = cnt_y + 1
+        if key > self.end_pos: 
+            key = self.end_pos
+
+        val = self.cal_days[key]
+        cnt_x = key % 7
+        cnt_y = int(key / 7)+1
+        x = self.gridx[cnt_x]
+        y = self.gridy[cnt_y]
+        self._DrawDayText(x, y, val, f, DC)
+
+
+    # calculate the dimensions in the center of the drawing area
+    def Center(self):       
+        borderW = self.x_mrg * 2
+        borderH = self.y_mrg + self.y_end + self.title_offset
+
+        self.cellW = int((self.sizew - borderW)/7)
+        self.cellH = int((self.sizeh - borderH)/7)
 
 
-    def Center(self):       # calculate the dimensions in the center of the drawing area
-        bdw = self.x_mrg * 2
-        bdh = self.y_mrg + self.y_end + self.title_offset
+        self.restW = ((self.sizew - borderW)%7 ) - 1
 
 
-        self.dl_w = int((self.sizew-bdw)/7)
-        self.dl_h = int((self.sizeh-bdh)/7)
+        # week title adjustment
+        self.weekHdrCellH = int(self.cellH * self.cal_week_scale)
+        # recalculate the cell height exkl. the week header and
+        # subtracting the size
+        self.cellH = int((self.sizeh - borderH - self.weekHdrCellH)/6)
 
 
-        self.dl_th = int(self.dl_h*self.cal_week_scale)     # week title adjustment
-        self.cwidth = self.dl_w * 7
-        self.cheight = self.dl_h * 6 + self.dl_th
+        self.restH = ((self.sizeh - borderH - self.weekHdrCellH)%6 ) - 1
+        self.calW = self.cellW * 7
+        self.calH = self.cellH * 6 + self.weekHdrCellH
+
+    # highlighted selected days
+    def DrawSel(self, DC):
 
 
-    def DrawSel(self):         # highlighted selected days
         for key in self.cal_sel.keys():
             sel_color = self.cal_sel[key][1]
         for key in self.cal_sel.keys():
             sel_color = self.cal_sel[key][1]
-            brush = wx.Brush(wx.NamedColour(sel_color), wx.SOLID)
-            self.DC.SetBrush(brush)
+            brush = wx.Brush(MakeColor(sel_color), wx.SOLID)
+            DC.SetBrush(brush)
 
             if self.hide_grid is False:
 
             if self.hide_grid is False:
-                self.DC.SetPen(wx.Pen(wx.NamedColour(self.grid_color), 0))
+                DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0))
             else:
             else:
-                self.DC.SetPen(wx.Pen(wx.NamedColour(self.back_color), 0))
-
+                DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BACKGROUND]), 0))
+            
             nkey = key + self.st_pos -1
             rect = self.rg[nkey]
             nkey = key + self.st_pos -1
             rect = self.rg[nkey]
-            self.DC.DrawRectangle((rect.x, rect.y), (rect.width+1, rect.height+1))
 
 
-    def DrawGrid(self):         # calculate and draw the grid lines
-        self.DC.SetPen(wx.Pen(wx.NamedColour(self.grid_color), 0))
+            DC.DrawRectangleRect(rect)
+
+    # calculate and draw the grid lines
+    def DrawGrid(self, DC):
+        DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0))
 
         self.gridx = []
         self.gridy = []
 
         self.x_st = self.cx_st + self.x_mrg
 
         self.gridx = []
         self.gridy = []
 
         self.x_st = self.cx_st + self.x_mrg
-        self.y_st = self.cy_st + self.y_mrg + self.title_offset     # start postion of draw
-
+        # start postion of draw
+        self.y_st = self.cy_st + self.y_mrg + self.title_offset
+        
         x1 = self.x_st
         y1 = self.y_st
         x1 = self.x_st
         y1 = self.y_st
-        y2 = y1 + self.cheight
+        y2 = y1 + self.calH + self.restH
 
         for i in range(8):
 
         for i in range(8):
+            if i == 7:
+                x1 = x1 + self.restW
+
             if self.hide_grid is False:
             if self.hide_grid is False:
-                self.DC.DrawLine((x1, y1), (x1, y2))
+                DC.DrawLinePoint((x1, y1), (x1, y2))
+
             self.gridx.append(x1)
             self.gridx.append(x1)
-            x1 = x1 + self.dl_w
+
+            x1 = x1 + self.cellW
 
         x1 = self.x_st
         y1 = self.y_st
 
         x1 = self.x_st
         y1 = self.y_st
-        x2 = x1 + self.cwidth
+        x2 = x1 + self.calW + self.restW
 
         for i in range(8):
 
         for i in range(8):
+            if i == 7:
+                y1 = y1 + self.restH
+
             if self.hide_grid is False:
             if self.hide_grid is False:
-                self.DC.DrawLine((x1, y1), (x2, y1))
+                DC.DrawLinePoint((x1, y1), (x2, y1))
 
             self.gridy.append(y1)
 
             if i == 0:
 
             self.gridy.append(y1)
 
             if i == 0:
-                y1 = y1 + self.dl_th
+                y1 = y1 + self.weekHdrCellH
             else:
             else:
-                y1 = y1 + self.dl_h
+                y1 = y1 + self.cellH
+    
+    def GetColor(self, name):
+        return MakeColor(self.colors[name])
 
 
+    def SetColor(self, name, value):
+        self.colors[name] = MakeColor(value)
 
 class PrtCalDraw(CalDraw):
     def InitValues(self):
         self.rg = {}
         self.cal_sel = {}
 
 class PrtCalDraw(CalDraw):
     def InitValues(self):
         self.rg = {}
         self.cal_sel = {}
-        self.set_cx_st = 1.0        # start draw border location
+        # start draw border location
+        self.set_cx_st = 1.0        
         self.set_cy_st = 1.0
 
         self.set_cy_st = 1.0
 
-        self.set_y_mrg = 0.2      # draw offset position
+        # draw offset position
+        self.set_y_mrg = 0.2      
         self.set_x_mrg = 0.2
         self.set_y_end = 0.2
 
         self.set_x_mrg = 0.2
         self.set_y_end = 0.2
 
@@ -471,43 +670,60 @@ class PrtCalDraw(CalDraw):
     def SetPreview(self, preview):
         self.preview = preview
 
     def SetPreview(self, preview):
         self.preview = preview
 
-class wxCalendar(wx.Window):
-    def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize):
-        wx.Window.__init__(self, parent, id, pos, size)
+class Calendar( wx.PyControl ):
+    def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.Size(200,200),
+                   style= 0, validator=wx.DefaultValidator,
+                   name= "calendar"):
+        wx.PyControl.__init__(self, parent, id, pos, size, style | wx.WANTS_CHARS, validator, name)
 
 
-    # set the calendar control attributes
+        self.hasFocus = False
+        # set the calendar control attributes
 
 
-        self.grid_color = 'BLACK'
-        self.back_color = 'WHITE'
         self.hide_grid = False
         self.hide_grid = False
-        self.sel_color = 'RED'
         self.hide_title = False
         self.show_weekend = False
         self.cal_type = "NORMAL"
         self.hide_title = False
         self.show_weekend = False
         self.cal_type = "NORMAL"
-
-        self.week_color = 'LIGHT GREY'
-        self.week_font_color = 'BLACK'      # font colors
+        self.outer_border = True
+        self.num_align_horz = wx.ALIGN_CENTRE
+        self.num_align_vert = wx.ALIGN_CENTRE
+        self.colors = DefaultColors()
+        self.set_x_mrg = 1
+        self.set_y_mrg = 1
+        self.set_y_end = 1
 
         self.select_list = []
 
 
         self.select_list = []
 
-        self.SetBackgroundColour(self.back_color)
+        self.SetBackgroundColour(MakeColor(self.colors[COLOR_BACKGROUND]))
         self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftEvent)
         self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDEvent)
         self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightEvent)
         self.Bind(wx.EVT_RIGHT_DCLICK, self.OnRightDEvent)
         self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftEvent)
         self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDEvent)
         self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightEvent)
         self.Bind(wx.EVT_RIGHT_DCLICK, self.OnRightDEvent)
+        self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
+        self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
+        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
 
         self.sel_key = None      #  last used by
         self.sel_lst = []        # highlighted selected days
 
 
         self.sel_key = None      #  last used by
         self.sel_lst = []        # highlighted selected days
 
-        self.SetNow()       # default calendar for current month
+        # default calendar for current month
+        self.SetNow()       
 
         self.size = None
         self.set_day = None
 
         self.Bind(wx.EVT_PAINT, self.OnPaint)
         self.Bind(wx.EVT_SIZE, self.OnSize)
 
         self.size = None
         self.set_day = None
 
         self.Bind(wx.EVT_PAINT, self.OnPaint)
         self.Bind(wx.EVT_SIZE, self.OnSize)
+        
+    def AcceptsFocus(self):
+        return self.IsShown() and self.IsEnabled()
+    
+    def GetColor(self, name):
+        return MakeColor(self.colors[name])
+
+    def SetColor(self, name, value):
+        self.colors[name] = MakeColor(value)
 
 
-# control some of the main calendar attributes
+    # control some of the main calendar attributes
 
     def HideTitle(self):
         self.hide_title = True
 
     def HideTitle(self):
         self.hide_title = True
@@ -515,14 +731,14 @@ class wxCalendar(wx.Window):
     def HideGrid(self):
         self.hide_grid = True
 
     def HideGrid(self):
         self.hide_grid = True
 
-# determine the calendar rectangle click area and draw a selection
+    # determine the calendar rectangle click area and draw a selection
 
     def ProcessClick(self, event):
         self.x, self.y = event.GetX(), event.GetY()
         key = self.GetDayHit(self.x, self.y)
         self.SelectDay(key)
 
 
     def ProcessClick(self, event):
         self.x, self.y = event.GetX(), event.GetY()
         key = self.GetDayHit(self.x, self.y)
         self.SelectDay(key)
 
-# tab mouse click events and process
+    # tab mouse click events and process
 
     def OnLeftEvent(self, event):
         self.click = 'LEFT'
 
     def OnLeftEvent(self, event):
         self.click = 'LEFT'
@@ -542,27 +758,85 @@ class wxCalendar(wx.Window):
         self.click = 'DRIGHT'
         self.ProcessClick(event)
 
         self.click = 'DRIGHT'
         self.ProcessClick(event)
 
+    def OnSetFocus(self, event):
+        self.hasFocus = True
+        self.DrawFocusIndicator(True)
+
+    def OnKillFocus(self, event):
+        self.hasFocus = False
+        self.DrawFocusIndicator(False)
+
+    def OnKeyDown(self, event):
+        if not self.hasFocus:
+            event.Skip()
+            return
+        
+        key_code = event.KeyCode()
+        
+        if key_code == wx.WXK_TAB:
+            forward = not event.ShiftDown()
+            ne = wx.NavigationKeyEvent()
+            ne.SetDirection(forward)
+            ne.SetCurrentFocus(self)
+            ne.SetEventObject(self)
+            self.GetParent().GetEventHandler().ProcessEvent(ne)
+            event.Skip()
+            return
+
+        delta = None
+
+        if key_code == wx.WXK_UP:
+            delta = -7 
+        elif key_code == wx.WXK_DOWN:
+            delta = 7 
+        elif key_code == wx.WXK_LEFT:
+            delta = -1 
+        elif key_code == wx.WXK_RIGHT:
+            delta = 1 
+        elif key_code == wx.WXK_HOME:
+            curDate = wx.DateTimeFromDMY(int(self.cal_days[self.sel_key]),self.month - 1,self.year)
+            newDate = wx.DateTime_Now()
+            ts = newDate - curDate
+            delta =  ts.GetDays()
+
+        if delta <> None:
+            curDate = wx.DateTimeFromDMY(int(self.cal_days[self.sel_key]),self.month - 1,self.year)
+            timeSpan = wx.TimeSpan_Days(delta)
+            newDate = curDate + timeSpan
+
+            if curDate.GetMonth() == newDate.GetMonth():
+                self.set_day = newDate.GetDay()
+                key = self.sel_key + delta
+                self.SelectDay(key)
+            else:
+                self.month = newDate.GetMonth() + 1
+                self.year = newDate.GetYear()
+                self.set_day = newDate.GetDay()
+                self.sel_key = None
+                self.DoDrawing(wx.ClientDC(self))
+
+        event.Skip()
+
     def SetSize(self, set_size):
         self.size = set_size
 
     def SetSelDay(self, sel):
     def SetSize(self, set_size):
         self.size = set_size
 
     def SetSelDay(self, sel):
-        self.sel_lst = sel  # list of highlighted days
-
-# get the current date
+        # list of highlighted days
+        self.sel_lst = sel  
 
 
+    # get the current date
     def SetNow(self):
         dt = now()
         self.month = dt.month
         self.year = dt.year
         self.day = dt.day
 
     def SetNow(self):
         dt = now()
         self.month = dt.month
         self.year = dt.year
         self.day = dt.day
 
-# set the current day
-
+    # set the current day
     def SetCurrentDay(self):
         self.SetNow()
         self.set_day = self.day
 
     def SetCurrentDay(self):
         self.SetNow()
         self.set_day = self.day
 
-# get the date, day, month, year set in calendar
+    # get the date, day, month, year set in calendar
 
     def GetDate(self):
         return self.day, self.month, self.year
 
     def GetDate(self):
         return self.day, self.month, self.year
@@ -576,7 +850,7 @@ class wxCalendar(wx.Window):
     def GetYear(self):
         return self.year
 
     def GetYear(self):
         return self.year
 
-# set the day, month, and year
+    # set the day, month, and year
 
     def SetDayValue(self, day):
         self.set_day = day
 
     def SetDayValue(self, day):
         self.set_day = day
@@ -591,7 +865,7 @@ class wxCalendar(wx.Window):
     def SetYear(self, year):
         self.year = year
 
     def SetYear(self, year):
         self.year = year
 
-# increment year and month
+    # increment year and month
 
     def IncYear(self):
         self.year = self.year + 1
 
     def IncYear(self):
         self.year = self.year + 1
@@ -615,13 +889,14 @@ class wxCalendar(wx.Window):
             self.year = self.year - 1
         self.set_day = None
 
             self.year = self.year - 1
         self.set_day = None
 
-# test to see if the selection has a date and create event
+    # test to see if the selection has a date and create event
 
     def TestDay(self, key):
         try:
 
     def TestDay(self, key):
         try:
-            self.day = int(self.cal[key])
+            self.day = int(self.cal_days[key])
         except:
             return None
         except:
             return None
+        
         if self.day == "":
             return None
         else:
         if self.day == "":
             return None
         else:
@@ -635,7 +910,7 @@ class wxCalendar(wx.Window):
             self.set_day = self.day
             return key
 
             self.set_day = self.day
             return key
 
-# find the clicked area rectangle
+    # find the clicked area rectangle
 
     def GetDayHit(self, mx, my):
         for key in self.rg.keys():
 
     def GetDayHit(self, mx, my):
         for key in self.rg.keys():
@@ -647,18 +922,26 @@ class wxCalendar(wx.Window):
 
         return None
 
 
         return None
 
-# calendar drawing
+    # calendar drawing
 
 
-    def SetWeekColor(self, font_color, week_color):     # set font and background color for week title
-        self.week_font_color = font_color
-        self.week_color = week_color
+    def SetWeekColor(self, font_color, week_color):     
+        # set font and background color for week title
+        self.colors[COLOR_HEADER_FONT] = MakeColor(font_color)
+        self.colors[COLOR_HEADER_BACKGROUND] = MakeColor(week_color)
+        self.colors[COLOR_3D_LIGHT] = MakeColor(week_color)
+        self.colors[COLOR_3D_DARK] = MakeColor(week_color)
 
 
+    def SetTextAlign(self, vert, horz):
+        self.num_align_horz = horz
+        self.num_align_vert = vert
+        
     def AddSelect(self, list, font_color, back_color):
         list_val = [list, font_color, back_color]
         self.select_list.append(list_val)
 
     def ShowWeekEnd(self):
     def AddSelect(self, list, font_color, back_color):
         list_val = [list, font_color, back_color]
         self.select_list.append(list_val)
 
     def ShowWeekEnd(self):
-        self.show_weekend = True    # highlight weekend
+        # highlight weekend
+        self.show_weekend = True
 
     def SetBusType(self):
         self.cal_type = "BUS"
 
     def SetBusType(self):
         self.cal_type = "BUS"
@@ -672,39 +955,46 @@ class wxCalendar(wx.Window):
         self.DoDrawing(DC)
 
     def DoDrawing(self, DC):
         self.DoDrawing(DC)
 
     def DoDrawing(self, DC):
-        DC = wx.PaintDC(self)
+        #DC = wx.PaintDC(self)
         DC.BeginDrawing()
 
         DC.BeginDrawing()
 
-        self.cal = cal = CalDraw(self)
+        try:
+            cal = self.caldraw
+        except:
+            self.caldraw = CalDraw(self)
+            cal = self.caldraw
 
 
-        cal.grid_color = self.grid_color
-        cal.back_color = self.back_color
         cal.hide_grid = self.hide_grid
         cal.hide_grid = self.hide_grid
-        cal.grid_color = self.grid_color
         cal.hide_title = self.hide_title
         cal.show_weekend = self.show_weekend
         cal.cal_type = self.cal_type
         cal.hide_title = self.hide_title
         cal.show_weekend = self.show_weekend
         cal.cal_type = self.cal_type
+        cal.outer_border = self.outer_border
+        cal.num_align_horz = self.num_align_horz
+        cal.num_align_vert = self.num_align_vert
+        cal.colors = self.colors
 
         if self.size is None:
             size = self.GetClientSize()
         else:
             size = self.size
 
 
         if self.size is None:
             size = self.GetClientSize()
         else:
             size = self.size
 
-# drawing attributes
-
-        cal.week_font_color = self.week_font_color
-        cal.week_color = self.week_color
+        # drawing attributes
 
         cal.SetSize(size)
         cal.SetCal(self.year, self.month)
 
 
         cal.SetSize(size)
         cal.SetCal(self.year, self.month)
 
+        # these have to set after SetCal as SetCal would overwrite them again.
+        cal.set_x_mrg = self.set_x_mrg 
+        cal.set_y_mrg = self.set_y_mrg 
+        cal.set_y_end = self.set_y_end 
+
         for val in self.select_list:
             cal.AddSelect(val[0], val[1], val[2])
 
         cal.DrawCal(DC, self.sel_lst)
 
         self.rg = cal.GetRect()
         for val in self.select_list:
             cal.AddSelect(val[0], val[1], val[2])
 
         cal.DrawCal(DC, self.sel_lst)
 
         self.rg = cal.GetRect()
-        self.cal = cal.GetCal()
+        self.cal_days = cal.GetCal()
         self.st_pos = cal.GetOffset()
         self.ymax = DC.MaxY()
 
         self.st_pos = cal.GetOffset()
         self.ymax = DC.MaxY()
 
@@ -713,9 +1003,37 @@ class wxCalendar(wx.Window):
 
         DC.EndDrawing()
 
 
         DC.EndDrawing()
 
-# draw the selection rectangle
+    # draw the selection rectangle
+    def DrawFocusIndicator(self, draw):
+        DC = wx.ClientDC(self)
+        try:
+            if draw == True:
+                self.caldraw.DrawFocusIndicator(DC)
+            else:
+                self.caldraw.DrawBorder(DC,True)
+        except:
+            pass
+
+    def DrawRect(self, key, bgcolor = 'WHITE', fgcolor= 'PINK',width = 0):
+        if key == None:
+            return
+
+        DC = wx.ClientDC(self)
+        DC.BeginDrawing()
+
+        brush = wx.Brush(MakeColor(bgcolor))
+        DC.SetBrush(brush)
+
+        DC.SetPen(wx.TRANSPARENT_PEN)
+
+        rect = self.rg[key]
+        DC.DrawRectangle(rect.x+1, rect.y+1, rect.width-2, rect.height-2)
+
+        self.caldraw.DrawDayText(DC,key)
 
 
-    def DrawRect(self, key, color = 'BLACK', width = 0):
+        DC.EndDrawing()
+        
+    def DrawRectOrg(self, key, fgcolor = 'BLACK', width = 0):
         if key == None:
             return
 
         if key == None:
             return
 
@@ -724,39 +1042,76 @@ class wxCalendar(wx.Window):
 
         brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT)
         DC.SetBrush(brush)
 
         brush = wx.Brush(wx.Colour(0, 0xFF, 0x80), wx.TRANSPARENT)
         DC.SetBrush(brush)
-        DC.SetPen(wx.Pen(wx.NamedColour(color), width))
+
+        try:
+            DC.SetPen(wx.Pen(MakeColor(fgcolor), width))
+        except:
+            DC.SetPen(wx.Pen(MakeColor(self.GetColor(COLOR_GRID_LINES)), width))
 
         rect = self.rg[key]
 
         rect = self.rg[key]
-        DC.DrawRectangle((rect.x, rect.y), (rect.width+1, rect.height+1))
+        DC.DrawRectangleRect(rect)
 
         DC.EndDrawing()
 
 
         DC.EndDrawing()
 
-# set the day selection
+    # set the day selection
 
     def SetDay(self, day):
 
     def SetDay(self, day):
-        day = day + self.st_pos - 1
-        self.SelectDay(day)
+        d = day + self.st_pos - 1
+        self.SelectDay(d)
 
 
+    def IsDayInWeekend(self, key):
+        try:
+            t = Date(self.year, self.month, 1)
+            
+            day = self.cal_days[key]
+            day = int(day) + t.day_of_week
+            
+            if day % 7 == 6 or day % 7 == 0:
+                return True
+        except:
+            return False
+        
     def SelectDay(self, key):
         sel_size = 1
     def SelectDay(self, key):
         sel_size = 1
-        self.DrawRect(self.sel_key, self.back_color, sel_size)     # clear large selection
-
-        if self.hide_grid is False:
-            self.DrawRect(self.sel_key, self.grid_color)
+        # clear large selection
+        
+        if self.sel_key != None:
+            (cfont, bgcolor) = self.__GetColorsForDay(self.sel_key)
+            self.DrawRect(self.sel_key, bgcolor,cfont, sel_size)
 
 
-        self.DrawRect(key, self.sel_color, sel_size)
-        self.sel_key = key      # store last used by
-        self.select_day = None
+        self.DrawRect(key, self.GetColor(COLOR_HIGHLIGHT_BACKGROUND), self.GetColor(COLOR_HIGHLIGHT_FONT), sel_size)
+        # store last used by
+        self.sel_key = key
 
     def ClearDsp(self):
         self.Clear()
 
     def ClearDsp(self):
         self.Clear()
+    def SetMargin(self, xmarg, ymarg):
+        self.set_x_mrg = xmarg
+        self.set_y_mrg = ymarg
+        self.set_y_end = ymarg
+    def __GetColorsForDay(self, key):
+        cfont   = self.GetColor(COLOR_FONT)
+        bgcolor = self.GetColor(COLOR_BACKGROUND)
+
+        if self.IsDayInWeekend(key) is True and self.show_weekend is True:
+            cfont   = self.GetColor(COLOR_WEEKEND_FONT)
+            bgcolor = self.GetColor(COLOR_WEEKEND_BACKGROUND)
+
+        try:
+            dayIdx = int(self.cal_days[key])
+            (cfont, bgcolor) = self.caldraw.cal_sel[dayIdx]
+        except:
+            pass
+
+        return (cfont, bgcolor)
 
 class CalenDlg(wx.Dialog):
     def __init__(self, parent, month=None, day = None, year=None):
         wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360))
 
 class CalenDlg(wx.Dialog):
     def __init__(self, parent, month=None, day = None, year=None):
         wx.Dialog.__init__(self, parent, -1, "Event Calendar", wx.DefaultPosition, (280, 360))
-
-    # set the calendar and attributes
-        self.calend = wxCalendar(self, -1, (20, 60), (240, 200))
+        self.result = None
+        
+        # set the calendar and attributes
+        self.calend = Calendar(self, -1, (20, 60), (240, 200))
 
         if month == None:
             self.calend.SetCurrentDay()
 
         if month == None:
             self.calend.SetCurrentDay()
@@ -770,26 +1125,26 @@ class CalenDlg(wx.Dialog):
         self.calend.HideTitle()
         self.ResetDisplay()
 
         self.calend.HideTitle()
         self.ResetDisplay()
 
-    # get month list from DateTime
+        # get month list from DateTime
         monthlist = GetMonthList()
 
         monthlist = GetMonthList()
 
-    # select the month
+        # select the month
         self.date = wx.ComboBox(self, -1, Month[start_month], (20, 20), (90, -1), 
                                 monthlist, wx.CB_DROPDOWN)
         self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)
 
         self.date = wx.ComboBox(self, -1, Month[start_month], (20, 20), (90, -1), 
                                 monthlist, wx.CB_DROPDOWN)
         self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, self.date)
 
-    # alternate spin button to control the month
+        # alternate spin button to control the month
         h = self.date.GetSize().height
         h = self.date.GetSize().height
-        self.m_spin = wx.SpinButton(self, -1, (130, 20), (h*2, h), wx.SP_VERTICAL)
+        self.m_spin = wx.SpinButton(self, -1, (115, 20), (h*1.5, h), wx.SP_VERTICAL)
         self.m_spin.SetRange(1, 12)
         self.m_spin.SetValue(start_month)
         self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin)
 
         self.m_spin.SetRange(1, 12)
         self.m_spin.SetValue(start_month)
         self.Bind(wx.EVT_SPIN, self.OnMonthSpin, self.m_spin)
 
-    # spin button to control the year
+        # spin button to control the year
         self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1))
         h = self.dtext.GetSize().height
 
         self.dtext = wx.TextCtrl(self, -1, str(start_year), (160, 20), (60, -1))
         h = self.dtext.GetSize().height
 
-        self.y_spin = wx.SpinButton(self, -1, (220, 20), (h*2, h), wx.SP_VERTICAL)
+        self.y_spin = wx.SpinButton(self, -1, (225, 20), (h*1.5, h), wx.SP_VERTICAL)
         self.y_spin.SetRange(1980, 2010)
         self.y_spin.SetValue(start_year)
 
         self.y_spin.SetRange(1980, 2010)
         self.y_spin.SetValue(start_year)
 
@@ -800,27 +1155,29 @@ class CalenDlg(wx.Dialog):
         y_pos = 280
         but_size = (60, 25)
 
         y_pos = 280
         but_size = (60, 25)
 
-        btn = wx.Button(self, -1, ' Ok ', (x_pos, y_pos), but_size)
+        btn = wx.Button(self, wx.ID_OK, ' Ok ', (x_pos, y_pos), but_size)
         self.Bind(wx.EVT_BUTTON, self.OnOk, btn)
 
         self.Bind(wx.EVT_BUTTON, self.OnOk, btn)
 
-        btn = wx.Button(self, mID, ' Close ', (x_pos + 120, y_pos), but_size)
+        btn = wx.Button(self, wx.ID_CANCEL, ' Close ', (x_pos + 120, y_pos), but_size)
         self.Bind(wx.EVT_BUTTON, self.OnCancel, btn)
 
         self.Bind(wx.EVT_BUTTON, self.OnCancel, btn)
 
-    def OnOk(self, event):
+    def OnOk(self, evt):
+        self.result = ['None', str(self.calend.day), Month[self.calend.month], str(self.calend.year)]
         self.EndModal(wx.ID_OK)
 
     def OnCancel(self, event):
         self.EndModal(wx.ID_CANCEL)
 
         self.EndModal(wx.ID_OK)
 
     def OnCancel(self, event):
         self.EndModal(wx.ID_CANCEL)
 
-# log the mouse clicks
+    # log the mouse clicks
     def MouseClick(self, evt):
         self.month = evt.month
     def MouseClick(self, evt):
         self.month = evt.month
-        self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)]  # result click type and date
+        # result click type and date
+        self.result = [evt.click, str(evt.day), Month[evt.month], str(evt.year)]  
 
         if evt.click == 'DLEFT':
             self.EndModal(wx.ID_OK)
 
 
         if evt.click == 'DLEFT':
             self.EndModal(wx.ID_OK)
 
-# month and year spin selection routines
+    # month and year spin selection routines
     def OnMonthSpin(self, event):
         month = event.GetPosition()
         self.date.SetValue(Month[month])
     def OnMonthSpin(self, event):
         month = event.GetPosition()
         self.date.SetValue(Month[month])
@@ -841,7 +1198,7 @@ class CalenDlg(wx.Dialog):
         self.calend.SetMonth(monthval+1)
         self.ResetDisplay()
 
         self.calend.SetMonth(monthval+1)
         self.ResetDisplay()
 
-# set the calendar for highlighted days
+    # set the calendar for highlighted days
 
     def ResetDisplay(self):
         month = self.calend.GetMonth()
 
     def ResetDisplay(self):
         month = self.calend.GetMonth()