⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendar.py

📁 Wxpython Implemented on Windows CE, Source code
💻 PY
📖 第 1 页 / 共 3 页
字号:

        DC.SetTextForeground(MakeColor(self.colors[COLOR_HEADER_FONT]))

        cnt_x = 0
        cnt_y = 0

        brush = wx.Brush(MakeColor(self.colors[COLOR_HEADER_BACKGROUND]), wx.SOLID)
        DC.SetBrush(brush)

        if self.cal_type == "NORMAL":
            cal_days = CalDays
        else:
            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]

            dw,dh = DC.GetTextExtent(day)

            diffx = (width-dw)/2
            diffy = (height-dh)/2

            x = self.gridx[cnt_x]
            y = self.gridy[cnt_y]
            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

    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)
                DC.SetFont(f)
                tw,th = DC.GetTextExtent(test_day)

                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
            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
        for val in self.cal_days:
            x = self.gridx[cnt_x]
            y = self.gridy[cnt_y]

            self._DrawDayText(x, y, val, f, DC)

            if cnt_x < 6:
                cnt_x = cnt_x + 1
            else:
                cnt_x = 0
                cnt_y = cnt_y + 1

    def _DrawDayText(self, x, y, text, font, DC):

        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

        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

        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 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)

        self.restW = ((self.sizew - borderW)%7 ) - 1

        # 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.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):

        for key in self.cal_sel.keys():
            sel_color = self.cal_sel[key][1]
            brush = wx.Brush(MakeColor(sel_color), wx.SOLID)
            DC.SetBrush(brush)

            if self.hide_grid is False:
                DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_GRID_LINES]), 0))
            else:
                DC.SetPen(wx.Pen(MakeColor(self.colors[COLOR_BACKGROUND]), 0))
            
            nkey = key + self.st_pos -1
            rect = self.rg[nkey]

            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
        # start postion of draw
        self.y_st = self.cy_st + self.y_mrg + self.title_offset
        
        x1 = self.x_st
        y1 = self.y_st
        y2 = y1 + self.calH + self.restH

        for i in range(8):
            if i == 7:
                x1 = x1 + self.restW

            if self.hide_grid is False:
                DC.DrawLinePoint((x1, y1), (x1, y2))

            self.gridx.append(x1)

            x1 = x1 + self.cellW

        x1 = self.x_st
        y1 = self.y_st
        x2 = x1 + self.calW + self.restW

        for i in range(8):
            if i == 7:
                y1 = y1 + self.restH

            if self.hide_grid is False:
                DC.DrawLinePoint((x1, y1), (x2, y1))

            self.gridy.append(y1)

            if i == 0:
                y1 = y1 + self.weekHdrCellH
            else:
                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 = {}
        # start draw border location
        self.set_cx_st = 1.0        
        self.set_cy_st = 1.0

        # draw offset position
        self.set_y_mrg = 0.2      
        self.set_x_mrg = 0.2
        self.set_y_end = 0.2

    # calculate the dimensions in the center of the drawing area
    def SetPSize(self, pwidth, pheight):    
        self.pwidth = int(pwidth)/self.scale
        self.pheight = int(pheight)/self.scale

    def SetPreview(self, preview):
        self.preview = preview

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)

        self.hasFocus = False
        # set the calendar control attributes

        self.hide_grid = False
        self.hide_title = False
        self.show_weekend = False
        self.cal_type = "NORMAL"
        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.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_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

        # 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)
        
    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

    def HideTitle(self):
        self.hide_title = True

    def HideGrid(self):
        self.hide_grid = True

    # 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)

    # tab mouse click events and process

    def OnLeftEvent(self, event):
        self.click = 'LEFT'
        self.shiftkey = event.ShiftDown()
        self.ctrlkey = event.ControlDown()
        self.ProcessClick(event)

    def OnLeftDEvent(self, event):
        self.click = 'DLEFT'
        self.ProcessClick(event)

    def OnRightEvent(self, event):
        self.click = 'RIGHT'
        self.ProcessClick(event)

    def OnRightDEvent(self, 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()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -