def AdjustFontForHeight(self, font, height):
     pointSize = 18
     while True:
         font.SetPointSize(pointSize)
         _, th = self.context.GetTextExtent(
             ' ' + wxTimeFormat.FormatTime(wx.DateTimeFromHMS(23, 59, 59)))
         if th <= height:
             return
         pointSize -= 1
         if pointSize == 1:
             return  # Hum
 def AdjustFontForWidth(self, font, width):
     pointSize = 18
     while True:
         font.SetPointSize(pointSize)
         self.context.SetFont(font)
         tw, _ = self.context.GetTextExtent(
             ' ' + wxTimeFormat.FormatTime(wx.DateTimeFromHMS(23, 59, 59)))
         if tw <= width:
             return
         pointSize -= 1
         if pointSize == 1:
             return  # Hum
    def DrawHours(self, x, y, w, h, direction, includeText=True):
        if direction == wxSCHEDULER_VERTICAL:
            brush = self.context.CreateLinearGradientBrush(
                x, y, x + w, y + h, SCHEDULER_BACKGROUND_BRUSH(),
                DAY_BACKGROUND_BRUSH())
            self.context.SetBrush(brush)
            self.context.DrawRectangle(x, y, LEFT_COLUMN_SIZE, h)

        font = wx.NORMAL_FONT
        fsize = font.GetPointSize()
        fweight = font.GetWeight()

        try:
            font.SetWeight(wx.FONTWEIGHT_NORMAL)
            self.context.SetFont(font, wx.BLACK)

            self.context.SetPen(FOREGROUND_PEN)

            if direction == wxSCHEDULER_VERTICAL:
                hourH = 1.0 * h / len(self.displayedHours)
                self.AdjustFontForHeight(font, hourH)
                hourW, _ = self.context.GetTextExtent(
                    ' ' +
                    wxTimeFormat.FormatTime(wx.DateTimeFromHMS(23, 59, 59)))
            else:
                hourW = 1.0 * w / len(self.displayedHours)
                self.AdjustFontForWidth(font, int(hourW * 2 * 0.9))
                _, hourH = self.context.GetTextExtent(
                    ' ' +
                    wxTimeFormat.FormatTime(wx.DateTimeFromHMS(23, 59, 59)))

            if not includeText:
                hourH = 0

            for i, hour in enumerate(self.displayedHours):
                if hour.GetMinute() == 0:
                    if direction == wxSCHEDULER_VERTICAL:
                        self.context.DrawLines([
                            (x + LEFT_COLUMN_SIZE - hourW / 2, y + i * hourH),
                            (x + w, y + i * hourH)
                        ])
                        if includeText:
                            self.context.DrawText(
                                ' ' + wxTimeFormat.FormatTime(hour),
                                x + LEFT_COLUMN_SIZE - hourW - 10,
                                y + i * hourH)
                    else:
                        self.context.DrawLines([(x + i * hourW,
                                                 y + hourH * 1.25),
                                                (x + i * hourW, y + h + 10)])
                        if includeText:
                            self.context.DrawText(
                                wxTimeFormat.FormatTime(hour),
                                x + i * hourW + 5, y + hourH * .25)
                else:
                    if direction == wxSCHEDULER_VERTICAL:
                        self.context.DrawLines([(x + LEFT_COLUMN_SIZE,
                                                 y + i * hourH),
                                                (x + w, y + i * hourH)])
                    else:
                        self.context.DrawLines([(x + i * hourW,
                                                 y + hourH * 1.4),
                                                (x + i * hourW, y + h)])

            if direction == wxSCHEDULER_VERTICAL:
                self.context.DrawLines([(x + LEFT_COLUMN_SIZE - 1, y),
                                        (x + LEFT_COLUMN_SIZE - 1, y + h)])
                return LEFT_COLUMN_SIZE, max(h, DAY_SIZE_MIN.height)
            else:
                self.context.DrawLines([(x, y + hourH * 1.5 - 1),
                                        (x + w, y + hourH * 1.5 - 1)])
                return max(w, DAY_SIZE_MIN.width), hourH * 1.5
        finally:
            font.SetPointSize(fsize)
            font.SetWeight(fweight)
    def DrawHours(self, x, y, w, h, direction, includeText=True):
        if direction == wxSCHEDULER_VERTICAL:
            self.context.SetBrush(wx.Brush(SCHEDULER_BACKGROUND_BRUSH()))
            self.context.DrawRectangle(x, y, LEFT_COLUMN_SIZE, h)

        font = self.context.GetFont()
        fWeight = font.GetWeight()
        fSize = font.GetPointSize()
        try:
            font.SetWeight(wx.FONTWEIGHT_NORMAL)
            self.context.SetFont(font)
            self.context.SetTextForeground(wx.BLACK)

            if direction == wxSCHEDULER_VERTICAL:
                hourH = 1.0 * h / len(self.displayedHours)
                self.AdjustFontForHeight(font, hourH)
                hourW, _ = self.context.GetTextExtent(
                    ' ' +
                    wxTimeFormat.FormatTime(wx.DateTimeFromHMS(23, 59, 59)))
            else:
                hourW = 1.0 * w / len(self.displayedHours)
                self.AdjustFontForWidth(font, int(hourW * 2 * 0.9))
                _, hourH = self.context.GetTextExtent(
                    ' ' +
                    wxTimeFormat.FormatTime(wx.DateTimeFromHMS(23, 59, 59)))

            if not includeText:
                hourH = 0

            for i, hour in enumerate(self.displayedHours):
                if hour.GetMinute() == 0:
                    if direction == wxSCHEDULER_VERTICAL:
                        self.context.DrawLine(x + LEFT_COLUMN_SIZE - hourW / 2,
                                              y + i * hourH, x + w,
                                              y + i * hourH)
                        if includeText:
                            self.context.DrawText(
                                wxTimeFormat.FormatTime(hour),
                                x + LEFT_COLUMN_SIZE - hourW - 5,
                                y + i * hourH)
                    else:
                        self.context.DrawLine(x + i * hourW, y + hourH * 1.25,
                                              x + i * hourW, y + h)
                        if includeText:
                            self.context.DrawText(
                                wxTimeFormat.FormatTime(hour),
                                x + i * hourW + 5, y + hourH * .25)
                else:
                    if direction == wxSCHEDULER_VERTICAL:
                        self.context.DrawLine(x + LEFT_COLUMN_SIZE,
                                              y + i * hourH, x + w,
                                              y + i * hourH)
                    else:
                        self.context.DrawLine(x + i * hourW, y + hourH * 1.4,
                                              x + i * hourW, y + h)

            if direction == wxSCHEDULER_VERTICAL:
                self.context.DrawLine(x + LEFT_COLUMN_SIZE - 1, y,
                                      x + LEFT_COLUMN_SIZE - 1, y + h)
                return LEFT_COLUMN_SIZE, max(h, DAY_SIZE_MIN.height)
            else:
                self.context.DrawLine(x, y + hourH * 1.5 - 1, x + w,
                                      y + hourH * 1.5 - 1)
                return max(w, DAY_SIZE_MIN.width), hourH * 1.5
        finally:
            font.SetWeight(fWeight)
            font.SetPointSize(fSize)
    def DrawSchedulesCompact(self, day, schedules, x, y, width, height,
                             highlightColor):
        if day is None:
            brush = self.context.CreateLinearGradientBrush(
                x, y, x + width, y + height, wx.BLACK,
                SCHEDULER_BACKGROUND_BRUSH())
        else:
            brush = self.context.CreateLinearGradientBrush(
                x, y, x + width, y + height, wx.LIGHT_GREY,
                DAY_BACKGROUND_BRUSH())

        self.context.SetBrush(brush)
        self.context.DrawRectangle(x, y, width, height)

        font = wx.NORMAL_FONT
        fsize = font.GetPointSize()
        fweight = font.GetWeight()

        try:
            font.SetPointSize(10)
            font.SetWeight(wx.FONTWEIGHT_NORMAL)

            results = []

            if day is not None:
                if day.IsSameDate(wx.DateTime.Now()):
                    color = highlightColor
                else:
                    color = None
                headerW, headerH = self.DrawSimpleDayHeader(day,
                                                            x,
                                                            y,
                                                            width,
                                                            height,
                                                            highlight=color)
                y += headerH
                height -= headerH

                x += SCHEDULE_OUTSIDE_MARGIN
                width -= 2 * SCHEDULE_OUTSIDE_MARGIN

                y += SCHEDULE_OUTSIDE_MARGIN
                height -= 2 * SCHEDULE_OUTSIDE_MARGIN

                self.context.SetPen(FOREGROUND_PEN)

                totalHeight = 0

                for schedule in schedules:
                    if schedule.start.Format('%H%M') != '0000':
                        description = '%s %s' % (wxTimeFormat.FormatTime(
                            schedule.start,
                            includeMinutes=True), schedule.description)
                    else:
                        description = schedule.description
                    description = self._shrinkText(
                        self.context, description,
                        width - 2 * SCHEDULE_INSIDE_MARGIN, headerH)[0]

                    textW, textH = self.context.GetTextExtent(description)
                    if totalHeight + textH > height:
                        break

                    brush = self.context.CreateLinearGradientBrush(
                        x, y, x + width, y + height, schedule.color,
                        DAY_BACKGROUND_BRUSH())
                    self.context.SetBrush(brush)
                    self.context.DrawRoundedRectangle(x, y, width, textH * 1.2,
                                                      1.0 * textH / 2)
                    results.append((schedule, wx.Point(x, y),
                                    wx.Point(x + width, y + textH * 1.2)))

                    self.context.SetFont(schedule.font, schedule.foreground)
                    self.context.DrawText(description,
                                          x + SCHEDULE_INSIDE_MARGIN,
                                          y + textH * 0.1)

                    y += textH * 1.2
                    totalHeight += textH * 1.2

            return results
        finally:
            font.SetPointSize(fsize)
            font.SetWeight(fweight)
    def DrawSchedulesCompact(self, day, schedules, x, y, width, height,
                             highlightColor):
        if day is None:
            self.context.SetBrush(wx.LIGHT_GREY_BRUSH)
        else:
            self.context.SetBrush(wx.Brush(DAY_BACKGROUND_BRUSH()))

        self.context.DrawRectangle(x, y, width, height)

        results = []

        if day is not None:
            if day.IsSameDate(wx.DateTime.Now()):
                color = highlightColor
            else:
                color = None
            headerW, headerH = self.DrawSimpleDayHeader(day,
                                                        x,
                                                        y,
                                                        width,
                                                        height,
                                                        highlight=color)
            y += headerH
            height -= headerH

            x += SCHEDULE_OUTSIDE_MARGIN
            width -= 2 * SCHEDULE_OUTSIDE_MARGIN

            y += SCHEDULE_OUTSIDE_MARGIN
            height -= 2 * SCHEDULE_OUTSIDE_MARGIN

            self.context.SetPen(FOREGROUND_PEN)

            totalHeight = 0

            for schedule in schedules:
                if schedule.start.Format('%H%M') != '0000':
                    description = '%s %s' % (wxTimeFormat.FormatTime(
                        schedule.start,
                        includeMinutes=True), schedule.description)
                else:
                    description = schedule.description
                description = self._shrinkText(
                    self.context, description,
                    width - 2 * SCHEDULE_INSIDE_MARGIN, headerH)[0]

                textW, textH = self.context.GetTextExtent(description)
                if totalHeight + textH > height:
                    break

                self.context.SetBrush(wx.Brush(schedule.color))
                self.context.DrawRectangle(x, y, width, textH * 1.2)
                results.append((schedule, wx.Point(x, y),
                                wx.Point(x + width, y + textH * 1.2)))

                self.context.SetTextForeground(schedule.foreground)
                self.context.DrawText(description, x + SCHEDULE_INSIDE_MARGIN,
                                      y + textH * 0.1)

                y += textH * 1.2
                totalHeight += textH * 1.2

        return results