class SuperScrollPanel(ScrollPanel): def __init__(self, panel): ScrollPanel.__init__(self) self.setHeight("100%") self.setStyleName("SuperScrollPanelOuter") self.inner = SimplePanel(Height="100%") self.add(self.inner) self.inner.setStyleName("SuperScrollPanelInner") self.inner.add(panel)
class Calendar(FocusPanel): monthsOfYear = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] daysOfWeek = ['S', 'M', 'T', 'W', 'T', 'F', 'S'] today = 'Today' tomorrow = 'Tomorrow' yesterday = 'Yesterday' cancel = 'Cancel' def __init__(self, **kwargs): FocusPanel.__init__(self, **kwargs) yr, mth, day = time.strftime("%Y-%m-%d").split("-") self.todayYear = int(yr) self.todayMonth = int(mth) # change to offset 0 as per javascript self.todayDay = int(day) self.currentMonth = self.todayMonth self.currentYear = self.todayYear self.currentDay = self.todayDay self.selectedDateListeners = [] self.defaultGrid = None # used later return def setDate(self,_date): """ _date - object of datetime.date class """ self.currentMonth = _date.month self.currentYear = _date.year self.currentDay = _date.day def getMonthsOfYear(self): return self.monthsOfYear def getDaysOfWeek(self): return self.daysOfWeek def addSelectedDateListener(self, listener): self.selectedDateListeners.append(listener) def removeSelectedDateListener(self, listener): self.selectedDateListeners.remove(listener) def isLeapYear(self, year): if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): return True else: return False def getDaysInMonth(self, mth, year): days = 0 if mth in [1, 3, 5, 7, 8, 10, 12]: days=31 elif mth in [4, 6, 9, 11]: days = 30 elif (mth==2 and self.isLeapYear(year)): days = 29 else: days = 28 return days def setPosition(self, left, top): element = self.getElement() DOM.setStyleAttribute(element, "left", "%dpx" % left) DOM.setStyleAttribute(element, "top", "%dpx" % top) def show(self, left, top): if left < 0: left = 0 if top < 0: top = 0 self.setPosition(left, top) self.drawCurrent() self.setVisible(True) def drawCurrent(self): yr, mth, day = self.currentYear, self.currentMonth, self.currentDay self.draw(int(mth), int(yr)) def draw(self, month , year): tod = time.localtime() mm = tod.tm_mon yy = tod.tm_year # has today changed and thus changed month? cater to rare case # where widget in created on last day of month and # page left till next day hasChangeMonth = False if yy <> self.todayYear or mm <> self.todayMonth: hasChangeMonth = True self.todayYear = yy self.todayMonth = mm # check to see if we have drawn the full widget before if self.defaultGrid is None: self.drawFull(month, year) else: # ok means we are re-drawing, but first check if it is the same # as the defaultGrid, if yes, just use it if not hasChangeMonth and month == self.todayMonth and \ year == self.todayYear: self.middlePanel.setWidget(self.defaultGrid) self.currentMonth = self.todayMonth self.currentYear = self.todayYear else: # we have to redraw the grid -- bah g = self.drawGrid(month, year) if hasChangeMonth: # reset the default grid as we have changed months self.defaultGrid = grid # # what about the title panel? # txt = "<b>" txt += self.getMonthsOfYear()[month-1] + " " + str(year) txt += "</b>" self.titlePanel.setWidget(HTML(txt)) self.setVisible(True) return def drawFull(self, month, year): # should be called only once when we draw the calendar for # the first time self.vp = VerticalPanel() self.vp.setSpacing(2) self.vp.addStyleName("calendarbox calendar-module calendar") self.setWidget(self.vp) self.setVisible(False) # mth = int(month) yr = int(year) tp = HorizontalPanel() tp.addStyleName("calendar-top-panel") tp.setSpacing(5) h1 = Hyperlink('<<') h1.addClickListener(getattr(self, 'onPreviousYear')) h2 = Hyperlink('<') h2.addClickListener(getattr(self, 'onPreviousMonth')) h4 = Hyperlink('>') h4.addClickListener(getattr(self, 'onNextMonth')) h5 = Hyperlink('>>') h5.addClickListener(getattr(self, 'onNextYear')) tp.add(h1) tp.add(h2) # titlePanel can be changed, whenever we draw, so keep the reference txt = "<b>" txt += self.getMonthsOfYear()[mth-1] + " " + str(yr) txt += "</b>" self.titlePanel = SimplePanel() self.titlePanel.setWidget(HTML(txt)) self.titlePanel.setStyleName("calendar-center") tp.add(self.titlePanel) tp.add(h4) tp.add(h5) tvp = VerticalPanel() tvp.setSpacing(10) tvp.add(tp) self.vp.add(tvp) # done with top panel self.middlePanel = SimplePanel() grid = self.drawGrid(mth, yr) self.middlePanel.setWidget(grid) self.vp.add(self.middlePanel) self.defaultGrid = grid self._gridShortcutsLinks() self._gridCancelLink() # # add code to test another way of doing the layout # self.setVisible(True) return def _gridShortcutsLinks(self): # # some links & handlers # bh1 = Hyperlink(self.yesterday) bh1.addClickListener(getattr(self, 'onYesterday')) bh2 = Hyperlink(self.today) bh2.addClickListener(getattr(self, 'onToday')) bh3 = Hyperlink(self.tomorrow) bh3.addClickListener(getattr(self, 'onTomorrow')) b = HorizontalPanel() b.add(bh1) b.add(bh2) b.add(bh3) b.addStyleName("calendar-shortcuts") self.vp.add(b) def _gridCancelLink(self): bh4 = Hyperlink(self.cancel) bh4.addClickListener(getattr(self, 'onCancel')) b2 = SimplePanel() b2.add(bh4) b2.addStyleName("calendar-cancel") self.vp.add(b2) def drawGrid(self, month, year): # draw the grid in the middle of the calendar daysInMonth = self.getDaysInMonth(month, year) # first day of the month & year secs = time.mktime((year, month, 1, 0, 0, 0, 0, 0, -1)) struct = time.localtime(secs) # 0 - sunday for our needs instead 0 = monday in tm_wday startPos = (struct.tm_wday + 1) % 7 slots = startPos + daysInMonth - 1 rows = int(slots/7) + 1 grid = Grid(rows+1, 7) # extra row for the days in the week grid.setWidth("100%") grid.addTableListener(self) self.middlePanel.setWidget(grid) # # put some content into the grid cells # for i in range(7): grid.setText(0, i, self.getDaysOfWeek()[i]) grid.cellFormatter.addStyleName(0, i, "calendar-header") # # draw cells which are empty first # day =0 pos = 0 while pos < startPos: grid.setText(1, pos , " ") grid.cellFormatter.setStyleAttr(1, pos, "background", "#f3f3f3") grid.cellFormatter.addStyleName(1, pos, "calendar-blank-cell") pos += 1 # now for days of the month row = 1 day = 1 col = startPos while day <= daysInMonth: if pos % 7 == 0 and day <> 1: row += 1 col = pos % 7 grid.setText(row, col, str(day)) if self.currentYear == self.todayYear and \ self.currentMonth == self.todayMonth and day == self.todayDay: grid.cellFormatter.addStyleName(row, col, "calendar-cell-today") else: grid.cellFormatter.addStyleName(row, col, "calendar-day-cell") day += 1 pos += 1 # # now blank lines on the last row # col += 1 while col < 7: grid.setText(row, col, " ") grid.cellFormatter.setStyleAttr(row, col, "background", "#f3f3f3") grid.cellFormatter.addStyleName(row, col, "calendar-blank-cell") col += 1 return grid def onCellClicked(self, grid, row, col): if row == 0: return text = grid.getText(row, col).strip() if text == "": return try: selectedDay = int(text) except ValueError, e: return # well if anyone is listening to the listener, fire that event for listener in self.selectedDateListeners: if hasattr(listener, "onDateSelected"): listener.onDateSelected(self.currentYear, self.currentMonth, selectedDay) else: listener(self.currentYear, self.currentMonth, selectedDay) self.setVisible(False)
class Calendar(FocusPanel, DateSelectedHandler): monthsOfYear = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] daysOfWeek = ['S', 'M', 'T', 'W', 'T', 'F', 'S'] today = 'Today' tomorrow = 'Tomorrow' yesterday = 'Yesterday' cancel = 'Cancel' def __init__(self, **kwargs): FocusPanel.__init__(self, **kwargs) DateSelectedHandler.__init__(self) yr, mth, day = time.strftime("%Y-%m-%d").split("-") self.todayYear = int(yr) self.todayMonth = int(mth) # change to offset 0 as per javascript self.todayDay = int(day) self.currentMonth = self.todayMonth self.currentYear = self.todayYear self.currentDay = self.todayDay self.defaultGrid = None # used later return def setDate(self, _date): """ _date - object of datetime.date class """ self.currentMonth = _date.month self.currentYear = _date.year self.currentDay = _date.day def getMonthsOfYear(self): return self.monthsOfYear def getDaysOfWeek(self): return self.daysOfWeek def isLeapYear(self, year): if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): return True else: return False def getDaysInMonth(self, mth, year): days = 0 if mth in [1, 3, 5, 7, 8, 10, 12]: days = 31 elif mth in [4, 6, 9, 11]: days = 30 elif (mth == 2 and self.isLeapYear(year)): days = 29 else: days = 28 return days def setPosition(self, left, top): element = self.getElement() DOM.setStyleAttribute(element, "left", "%dpx" % left) DOM.setStyleAttribute(element, "top", "%dpx" % top) def show(self, left, top): if left < 0: left = 0 if top < 0: top = 0 self.setPosition(left, top) self.drawCurrent() self.setVisible(True) def drawCurrent(self): yr, mth, day = self.currentYear, self.currentMonth, self.currentDay self.draw(int(mth), int(yr)) def draw(self, month , year): tod = time.localtime() mm = tod.tm_mon yy = tod.tm_year # has today changed and thus changed month? cater to rare case # where widget in created on last day of month and # page left till next day hasChangeMonth = False if yy <> self.todayYear or mm <> self.todayMonth: hasChangeMonth = True self.todayYear = yy self.todayMonth = mm # check to see if we have drawn the full widget before if self.defaultGrid is None: self.drawFull(month, year) else: # ok means we are re-drawing, but first check if it is the same # as the defaultGrid, if yes, just use it if not hasChangeMonth and month == self.todayMonth and \ year == self.todayYear: self.middlePanel.setWidget(self.defaultGrid) self.currentMonth = self.todayMonth self.currentYear = self.todayYear else: # we have to redraw the grid -- bah g = self.drawGrid(month, year) if hasChangeMonth: # reset the default grid as we have changed months self.defaultGrid = grid # # what about the title panel? # txt = "<b>" txt += self.getMonthsOfYear()[month - 1] + " " + str(year) txt += "</b>" self.titlePanel.setWidget(HTML(txt)) self.setVisible(True) return def drawFull(self, month, year): # should be called only once when we draw the calendar for # the first time self.vp = VerticalPanel() self.vp.setSpacing(2) self.vp.addStyleName("calendarbox calendar-module calendar") self.setWidget(self.vp) self.setVisible(False) # mth = int(month) yr = int(year) tp = HorizontalPanel() tp.addStyleName("calendar-top-panel") tp.setSpacing(5) h1 = Hyperlink('<<') h1.addClickListener(getattr(self, 'onPreviousYear')) h2 = Hyperlink('<') h2.addClickListener(getattr(self, 'onPreviousMonth')) h4 = Hyperlink('>') h4.addClickListener(getattr(self, 'onNextMonth')) h5 = Hyperlink('>>') h5.addClickListener(getattr(self, 'onNextYear')) tp.add(h1) tp.add(h2) # titlePanel can be changed, whenever we draw, so keep the reference txt = "<b>" txt += self.getMonthsOfYear()[mth - 1] + " " + str(yr) txt += "</b>" self.titlePanel = SimplePanel() self.titlePanel.setWidget(HTML(txt)) self.titlePanel.setStyleName("calendar-center") tp.add(self.titlePanel) tp.add(h4) tp.add(h5) tvp = VerticalPanel() tvp.setSpacing(10) tvp.add(tp) self.vp.add(tvp) # done with top panel self.middlePanel = SimplePanel() grid = self.drawGrid(mth, yr) self.middlePanel.setWidget(grid) self.vp.add(self.middlePanel) self.defaultGrid = grid self._gridShortcutsLinks() self._gridCancelLink() # # add code to test another way of doing the layout # self.setVisible(True) return def _gridShortcutsLinks(self): # # some links & handlers # bh1 = Hyperlink(self.yesterday) bh1.addClickListener(getattr(self, 'onYesterday')) bh2 = Hyperlink(self.today) bh2.addClickListener(getattr(self, 'onToday')) bh3 = Hyperlink(self.tomorrow) bh3.addClickListener(getattr(self, 'onTomorrow')) b = HorizontalPanel() b.add(bh1) b.add(bh2) b.add(bh3) b.addStyleName("calendar-shortcuts") self.vp.add(b) def _gridCancelLink(self): bh4 = Hyperlink(self.cancel) bh4.addClickListener(getattr(self, 'onCancel')) b2 = SimplePanel() b2.add(bh4) b2.addStyleName("calendar-cancel") self.vp.add(b2) def drawGrid(self, month, year): # draw the grid in the middle of the calendar daysInMonth = self.getDaysInMonth(month, year) # first day of the month & year secs = time.mktime((year, month, 1, 0, 0, 0, 0, 0, -1)) struct = time.localtime(secs) # 0 - sunday for our needs instead 0 = monday in tm_wday startPos = (struct.tm_wday + 1) % 7 slots = startPos + daysInMonth - 1 rows = int(slots / 7) + 1 grid = Grid(rows + 1, 7) # extra row for the days in the week grid.setWidth("100%") grid.addTableListener(self) self.middlePanel.setWidget(grid) # # put some content into the grid cells # for i in range(7): grid.setText(0, i, self.getDaysOfWeek()[i]) grid.cellFormatter.addStyleName(0, i, "calendar-header") # # draw cells which are empty first # day = 0 pos = 0 while pos < startPos: grid.setText(1, pos , " ") grid.cellFormatter.setStyleAttr(1, pos, "background", "#f3f3f3") grid.cellFormatter.addStyleName(1, pos, "calendar-blank-cell") pos += 1 # now for days of the month row = 1 day = 1 col = startPos while day <= daysInMonth: if pos % 7 == 0 and day <> 1: row += 1 col = pos % 7 grid.setText(row, col, str(day)) if self.currentYear == self.todayYear and \ self.currentMonth == self.todayMonth and day == self.todayDay: grid.cellFormatter.addStyleName(row, col, "calendar-cell-today") else: grid.cellFormatter.addStyleName(row, col, "calendar-day-cell") day += 1 pos += 1 # # now blank lines on the last row # col += 1 while col < 7: grid.setText(row, col, " ") grid.cellFormatter.setStyleAttr(row, col, "background", "#f3f3f3") grid.cellFormatter.addStyleName(row, col, "calendar-blank-cell") col += 1 return grid def onCellClicked(self, grid, row, col): if row == 0: return text = grid.getText(row, col).strip() if text == "": return try: selectedDay = int(text) except ValueError, e: return self.fireDateSelectedEvent(date( self.currentYear, self.currentMonth, selectedDay, )) self.setVisible(False)
class Calendar(FocusPanel): monthsOfYear = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] daysOfWeek = ['M', 'T', 'W', 'T', 'F', 'S', 'S'] daysOfWeek3 = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] today = 'Today' tomorrow = 'Tomorrow' yesterday = 'Yesterday' cancel = 'Cancel' def __init__(self, **kwargs): self.backyear = kwargs.pop('BackyearSymbol', '<<') self.backmonth = kwargs.pop('BackmonthSymbol', '<') self.fwdyear = kwargs.pop('FwdyearSymbol', '>>') self.fwdmonth = kwargs.pop('FwdmonthSymbol', '>') self.addbuttons = kwargs.pop('AddButtons', True) self.mindate = kwargs.pop('MinDate', None) # (yr, mnth, day) self.maxdate = kwargs.pop('MaxDate', None) # (yr, mnth, day) self.weekdaylength = kwargs.pop('WeekdayLength', 1) self.dayoffset = kwargs.pop('DayOffset', 1) # 1 returns Mon, 0 for Sun self.daybuttons = kwargs.pop('DayButtons', False) if kwargs.pop('ArrowButtons', False): self.bkls = Button else: self.bkls = Hyperlink FocusPanel.__init__(self, **kwargs) self.setDate() self.todayYear = self.currentYear self.todayMonth = self.currentMonth self.todayDay = self.currentDay self.selectedDateListeners = [] self.defaultGrid = None # used later def setDate(self, _date=None, m=None, d=None): """ _date - object of datetime.date class """ if m is None and d is None: if _date is None: y, m, d = time.strftime("%Y-%m-%d").split("-") else: m = _date.month y = _date.year d = _date.day else: y = _date self.currentYear = int(y) self.currentMonth = int(m) self.currentDay = int(d) def getMonthsOfYear(self): return self.monthsOfYear def getDaysOfWeek(self): if self.weekdaylength == 1: dow = self.daysOfWeek else: dow = self.daysOfWeek3 return dow[-self.dayoffset:] + \ dow[:-self.dayoffset] def addSelectedDateListener(self, listener): self.selectedDateListeners.append(listener) def removeSelectedDateListener(self, listener): self.selectedDateListeners.remove(listener) def isLeapYear(self, year): if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): return True else: return False def _indaterange(self, year, mnth, day=None): if self.mindate is not None: if year < self.mindate[0]: return False if year == self.mindate[0]: if mnth < self.mindate[1]: return False if day is not None: if mnth == self.mindate[1] and day < self.mindate[2]: return False if self.maxdate is not None: if year > self.maxdate[0]: return False if year == self.maxdate[0]: if mnth > self.maxdate[1]: return False if day is not None: if mnth == self.maxdate[1] and day > self.maxdate[2]: return False return True def getDaysInMonth(self, mth, year): days = 0 if mth in [1, 3, 5, 7, 8, 10, 12]: days=31 elif mth in [4, 6, 9, 11]: days = 30 elif (mth==2 and self.isLeapYear(year)): days = 29 else: days = 28 return days def setPosition(self, left, top): element = self.getElement() DOM.setStyleAttribute(element, "left", "%dpx" % left) DOM.setStyleAttribute(element, "top", "%dpx" % top) def show(self, left, top): if left < 0: left = 0 if top < 0: top = 0 self.setPosition(left, top) self.drawCurrent() self.setVisible(True) def drawCurrent(self): self.drawDate() def draw(self, month , year): tod = time.localtime() mm = tod.tm_mon yy = tod.tm_year # has today changed and thus changed month? cater to rare case # where widget in created on last day of month and # page left till next day hasChangeMonth = False if yy != self.todayYear or mm != self.todayMonth: hasChangeMonth = True self.todayYear = yy self.todayMonth = mm # check to see if we have drawn the full widget before if self.defaultGrid is None: self.drawFull(month, year) else: # ok means we are re-drawing, but first check if it is the same # as the defaultGrid, if yes, just use it if not hasChangeMonth and month == self.todayMonth and \ year == self.todayYear: self.middlePanel.setWidget(self.defaultGrid) self.currentMonth = self.todayMonth self.currentYear = self.todayYear else: # we have to redraw the grid -- bah g = self.drawGrid(month, year) if hasChangeMonth: # reset the default grid as we have changed months self.defaultGrid = grid # # what about the title panel? # txt = "<b>" txt += self.getMonthsOfYear()[month-1] + " " + str(year) txt += "</b>" self.titlePanel.setWidget(HTML(txt)) self.setVisible(True) def drawFull(self, month, year): # should be called only once when we draw the calendar for # the first time self.vp = VerticalPanel() self.vp.setSpacing(0) self.vp.setPadding(0) self.vp.addStyleName("calendarbox calendar-module calendar") self.setWidget(self.vp) self.setVisible(False) # mth = int(month) yr = int(year) tp = HorizontalPanel(Width="100%") tp.addStyleName("calendar-top-panel") tp.setSpacing(0) tp.setPadding(0) self.h1 = None self.h2 = None self.h4 = None self.h5 = None if self.backyear: self.h1 = self.bkls(self.backyear, StyleName="calendar-arrows") self.h1.addClickListener(getattr(self, 'onPreviousYear')) tp.add(self.h1) tp.setCellHorizontalAlignment(self.h1, "left") if self.backmonth: self.h2 = self.bkls(self.backmonth, StyleName="calendar-arrows") self.h2.addClickListener(getattr(self, 'onPreviousMonth')) tp.add(self.h2) tp.setCellHorizontalAlignment(self.h2, "left") # titlePanel can be changed, whenever we draw, so keep the reference txt = "<b>" txt += self.getMonthsOfYear()[mth-1] + " " + str(yr) txt += "</b>" self.titlePanel = SimplePanel() self.titlePanel.setWidget(HTML(txt)) self.titlePanel.setStyleName("calendar-center") tp.add(self.titlePanel) tp.setCellHorizontalAlignment(self.titlePanel, "center") tp.setCellWidth(self.titlePanel, "100%") if self.fwdmonth: self.h4 = self.bkls(self.fwdmonth, StyleName="calendar-arrows") self.h4.addClickListener(getattr(self, 'onNextMonth')) tp.add(self.h4) tp.setCellHorizontalAlignment(self.h4, "right") tp.setCellWidth(self.h4, "100%") self.h4.setWidth("100%") if self.fwdyear: self.h5 = self.bkls(self.fwdyear, StyleName="calendar-arrows") self.h5.addClickListener(getattr(self, 'onNextYear')) tp.add(self.h5) tp.setCellHorizontalAlignment(self.h5, "right") tvp = VerticalPanel(Width="100%") tvp.setSpacing(2) tvp.add(tp) self.vp.add(tvp) # done with top panel self.middlePanel = SimplePanel() grid = self.drawGrid(mth, yr) self.middlePanel.setWidget(grid) self.vp.add(self.middlePanel) self.defaultGrid = grid if self.addbuttons: # # some links & handlers # bh1 = Hyperlink(self.yesterday) bh1.addClickListener(getattr(self, 'onYesterday')) bh2 = Hyperlink(self.today) bh2.addClickListener(getattr(self, 'onToday')) bh3 = Hyperlink(self.tomorrow) bh3.addClickListener(getattr(self, 'onTomorrow')) bh4 = Hyperlink(self.cancel) bh4.addClickListener(getattr(self, 'onCancel')) # # add code to test another way of doing the layout # b = HorizontalPanel() b.add(bh1) b.add(bh2) b.add(bh3) b.addStyleName("calendar-shortcuts") self.vp.add(b) b2 = SimplePanel() b2.add(bh4) b2.addStyleName("calendar-cancel") self.vp.add(b2) self.checkLinks(mth, yr) self.setVisible(True) def checkLinks(self, month=None, year=None): if month is None: month = self.currentMonth if year is None: year = self.currentYear if self.backyear: ok = self._indaterange(year-1, month) self.h1.setEnabled(ok) if self.backmonth: py, pm = self._previousMonth(year, month) ok = self._indaterange(py, pm) print "prevmonth", month, year, py, pm, ok self.h2.setEnabled(ok) if self.fwdmonth: ny, nm = self._nextMonth(year, month) ok = self._indaterange(ny, nm) print "nextmonth", month, year, ny, nm, ok self.h4.setEnabled(ok) if self.fwdyear: ok = self._indaterange(year+1, month) self.h5.setEnabled(ok) def drawGrid(self, month, year): # draw the grid in the middle of the calendar self.checkLinks(month, year) daysInMonth = self.getDaysInMonth(month, year) # first day of the month & year secs = time.mktime((year, month, 1, 0, 0, 0, 0, 0, -1)) struct = time.localtime(secs) startPos = (struct.tm_wday + self.dayoffset) % 7 slots = startPos + daysInMonth - 1 rows = int(slots/7) + 1 grid = Grid(rows+1, 7, # extra row for the days in the week StyleName="calendar-grid") grid.setWidth("100%") grid.addTableListener(self) self.middlePanel.setWidget(grid) cf = grid.getCellFormatter() # # put some content into the grid cells # for i in range(7): grid.setText(0, i, self.getDaysOfWeek()[i]) cf.addStyleName(0, i, "calendar-header") # # draw cells which are empty first # day = 0 pos = 0 while pos < startPos: self._setCell(grid, 1, pos, BLANKCELL, "calendar-blank-cell") pos += 1 # now for days of the month row = 1 day = 1 col = startPos while day <= daysInMonth: if pos % 7 == 0 and day != 1: row += 1 col = pos % 7 if not self._indaterange(self.currentYear, self.currentMonth, day): self._setCell(grid, row, col, BLANKCELL, "calendar-blank-cell") day += 1 pos += 1 continue if self.currentYear == self.todayYear and \ self.currentMonth == self.todayMonth and day == self.todayDay: style = "calendar-cell-today" else: style = "calendar-day-cell" self._setCell(grid, row, col, str(day), style) day += 1 pos += 1 # # now blank lines on the last row # col += 1 while col < 7: self._setCell(grid, row, col, BLANKCELL, "calendar-blank-cell") col += 1 return grid def _setCell(self, grid, row, col, txt, style): cf = grid.getCellFormatter() if self.daybuttons: if txt != BLANKCELL: listener = lambda x, col=col, row=row, grid=grid\ : self.onCellClicked(grid, row, col) else: listener = None b = Button(txt, listener=listener, StyleName="%s-button" % style) grid.setWidget(row, col, b) cf.setWidth(row, col, "100%") else: grid.setHTML(row, col, txt) cf.setStyleName(row, col, style) def onCellClicked(self, grid, row, col): if row == 0: return text = grid.getText(row, col).strip() if text == BLANKCELL: return try: selectedDay = int(text) except ValueError, e: return # fire event to all listeners for listener in self.selectedDateListeners: if hasattr(listener, "onDateSelected"): listener = listener.onDateSelected listener(self.currentYear, self.currentMonth, selectedDay) self.setVisible(False)
def __init__(self, autoHide=None, modal=True, centered=False, **kwargs): # Init section self.dragging = False self.dragStartX = 0 self.dragStartY = 0 self.child = None self.panel = FlexTable( Height="100%", BorderWidth="0", CellPadding="0", CellSpacing="0", ) cf = self.panel.getCellFormatter() rf = self.panel.getRowFormatter() # Arguments section self.modal = modal self.caption = HTML() self.caption.setStyleName("Caption") self.caption.addMouseListener(self) # Make the DialogBox a 3x3 table, like GWT does, with # empty elements with specific style names. These can be # used with CSS to, for example, create border around the # dialog box. self.generate_gwt15 = kwargs.pop('gwt15', False) and True if not self.generate_gwt15: cf.setHeight(1, 0, "100%") cf.setWidth(1, 0, "100%") cf.setAlignment( 1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE, ) self.panel.setWidget(0, 0, self.caption) else: row_labels = ['Top', 'Middle', 'Bottom'] col_labels = ['Left', 'Center', 'Right'] for r in range(3): rf.setStyleName(r, 'dialog%s' % row_labels[r]) for c in range(3): cf.setStyleName(r, c, 'dialog%s%s' % (row_labels[r], col_labels[c])) sp = SimplePanel() sp.setStyleName('dialog%s%sInner' % (row_labels[r], col_labels[c])) self.panel.setWidget(r, c, sp) cf.setAlignment( 1, 1, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE, ) self.dialog_content = SimplePanel() self.dialog_content.setStyleName('dialogContent') self.panel.getWidget(0, 1).add(self.caption) self.panel.getWidget(1, 1).add(self.dialog_content) # Finalize kwargs['StyleName'] = kwargs.get('StyleName', "gwt-DialogBox") PopupPanel.__init__(self, autoHide, modal, **kwargs) PopupPanel.setWidget(self, self.panel) self.centered = centered
class DialogBox(PopupPanel): _props = [ ("caption", "Caption", "HTML", None), ] def __init__(self, autoHide=None, modal=True, centered=False, **kwargs): # Init section self.dragging = False self.dragStartX = 0 self.dragStartY = 0 self.child = None self.panel = FlexTable( Height="100%", BorderWidth="0", CellPadding="0", CellSpacing="0", ) cf = self.panel.getCellFormatter() rf = self.panel.getRowFormatter() # Arguments section self.modal = modal self.caption = HTML() self.caption.setStyleName("Caption") self.caption.addMouseListener(self) # Make the DialogBox a 3x3 table, like GWT does, with # empty elements with specific style names. These can be # used with CSS to, for example, create border around the # dialog box. self.generate_gwt15 = kwargs.pop('gwt15', False) and True if not self.generate_gwt15: cf.setHeight(1, 0, "100%") cf.setWidth(1, 0, "100%") cf.setAlignment( 1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE, ) self.panel.setWidget(0, 0, self.caption) else: row_labels = ['Top', 'Middle', 'Bottom'] col_labels = ['Left', 'Center', 'Right'] for r in range(3): rf.setStyleName(r, 'dialog%s' % row_labels[r]) for c in range(3): cf.setStyleName(r, c, 'dialog%s%s' % (row_labels[r], col_labels[c])) sp = SimplePanel() sp.setStyleName('dialog%s%sInner' % (row_labels[r], col_labels[c])) self.panel.setWidget(r, c, sp) cf.setAlignment( 1, 1, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE, ) self.dialog_content = SimplePanel() self.dialog_content.setStyleName('dialogContent') self.panel.getWidget(0, 1).add(self.caption) self.panel.getWidget(1, 1).add(self.dialog_content) # Finalize kwargs['StyleName'] = kwargs.get('StyleName', "gwt-DialogBox") PopupPanel.__init__(self, autoHide, modal, **kwargs) PopupPanel.setWidget(self, self.panel) self.centered = centered def onWindowResized(self, width, height): super(DialogBox, self).onWindowResized(width, height) if self.centered: self.centerBox() def show(self): super(DialogBox, self).show() if self.centered: self.centerBox() @classmethod def _getProps(self): return PopupPanel._getProps() + self._props def onEventPreview(self, event): # preventDefault on mousedown events, outside of the # dialog, to stop text-selection on dragging type = DOM.eventGetType(event) if type == 'mousedown': target = DOM.eventGetTarget(event) elem = self.caption.getElement() event_targets_popup = target and DOM.isOrHasChild(elem, target) if event_targets_popup: DOM.eventPreventDefault(event) return PopupPanel.onEventPreview(self, event) def getHTML(self): return self.caption.getHTML() def getText(self): return self.caption.getText() def setHTML(self, html): self.caption.setHTML(html) def setText(self, text): self.caption.setText(text) def onMouseDown(self, sender, x, y): self.dragging = True GlassWidget.show(self.caption) self.dragStartX = x self.dragStartY = y def onMouseEnter(self, sender): pass def onMouseLeave(self, sender): pass def onMouseMove(self, sender, x, y): if not self.dragging: return absX = x + self.getAbsoluteLeft() absY = y + self.getAbsoluteTop() self.setPopupPosition(absX - self.dragStartX, absY - self.dragStartY) def onMouseUp(self, sender, x, y): self.endDragging() def onMouseGlassEnter(self, sender): pass def onMouseGlassLeave(self, sender): self.endDragging() def endDragging(self): if not self.dragging: return self.dragging = False GlassWidget.hide() def remove(self, widget): if self.child != widget: return False self.panel.remove(widget) self.child = None return True def doAttachChildren(self): PopupPanel.doAttachChildren(self) self.caption.onAttach() def doDetachChildren(self): PopupPanel.doDetachChildren(self) self.caption.onDetach() def setWidget(self, widget): if self.child is not None: if not self.generate_gwt15: self.panel.remove(self.child) else: self.dialog_content.remove(self.child) if widget is not None: if not self.generate_gwt15: self.panel.setWidget(1, 0, widget) else: self.dialog_content.setWidget(widget) self.child = widget