Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The buggy state of wx.lib.calendar #1230

Closed
ricpol opened this issue May 22, 2019 · 7 comments · Fixed by #1234

Comments

@ricpol
Copy link
Contributor

@ricpol ricpol commented May 22, 2019

wx.lib.calendar feels a little rusty overall... The main visible issue being that for most months, day numbers ends up in the wrong (day name) column: this is because the underlaying wx.lib.CDate.julianDay still uses the "old" division operator which in Python 3 results in rounding errors.
But the real question here is why we still have to rely on this old messy way of going through a manual calculation of the Julian day, only to know the weekday name: just ask datetime.datetime.weekday...
We should probably get rid of wx.lib.CDate once and for all, and just rely on datetime.date for wx.lib.Calendar.

Another, unrelated, issue is that key navigation across month boundaries fails because the event callback directly calls Calendar.DoDrawing, which results in attempting to create a wx.PaintDC outside a EVT_PAINT handler...

And then there are some minor annoyances here and there, like the "year" spin control in CalenDlg being capped at 2010 (it must have seemed a very distant future, back then!) and so on.

Is anyone already working on these things? Or maybe I could try to give a first stab at it myself...

@Metallicow

This comment has been minimized.

Copy link
Contributor

@Metallicow Metallicow commented May 22, 2019

I suggest you write a pure python implementation of a calendar and call it a day. Just my thoughts....

@Metallicow

This comment has been minimized.

Copy link
Contributor

@Metallicow Metallicow commented May 22, 2019

@ricpol btw. I wrote a blank frameework, but I'm not working on it...

@ricpol

This comment has been minimized.

Copy link
Contributor Author

@ricpol ricpol commented May 22, 2019

@Metallicow, not sure what you mean... wxPython already has a pure python implementation of a calendar, namely wx.lib.calendar. It just needs a little update.

@RobinD42

This comment has been minimized.

Copy link
Member

@RobinD42 RobinD42 commented May 22, 2019

Or maybe I could try to give a first stab at it myself...

Please do, and submit it here as a PR.

@Metallicow

This comment has been minimized.

Copy link
Contributor

@Metallicow Metallicow commented May 22, 2019

@ricpol Essentially I duplicated the API docs for the CalendarCtrl, since there is no pure python version.
The reasoning is about the same as when @RobinD42 ported DynamicSash widget to pure python from gizmos or @infinity77 ported AUI in order to fix bugs.

...Or just to be able to roll your own custom drawn calendar the way you like...

Here is the base class based of the docs.
basically just pug in the def's as you go and try and reimplement basic functions, etc...

class PyCalendarCtrl(wx.Panel):
    """
    TOWRITE Pure Python Reimplimentation of `wx.adv.CalendarCtrl`_

    Subclass of `wx.Panel <http://wxpython.org/Phoenix/docs/html/wx.Panel.html>`_

    =================================
    |phoenix_title|  **CalendarCtrl**
    =================================

    The calendar control allows the user to pick a date.

    The user can move the current selection using the keyboard and select the date
    (generating ``EVT_CALENDAR`` event) by pressing ``<Return>`` or double clicking it.

    Generic calendar has advanced possibilities for the customization of its display,
    described below. If you want to use these possibilities on every platform, use
    GenericCalendarCtrl instead of :ref:`adv.CalendarCtrl`.

    All global settings (such as colours and fonts used) can, of course, be changed.
    But also, the display style for each day in the month can be set independently
    using :ref:`adv.CalendarDateAttr`  class.

    An item without custom attributes is drawn with the default colours and font and
    without border, but setting custom attributes with :meth:`~adv.CalendarCtrl.SetAttr`
    allows to modify its appearance. Just create a custom attribute object and set it
    for the day you want to be displayed specially (note that the control will take
    ownership of the pointer, i.e. it will delete it itself). A day may be marked
    as being a holiday, even if it is not recognized as one by :ref:`DateTime`  using
    the :meth:`CalendarDateAttr.SetHoliday`   method.

    As the attributes are specified for each day, they may change when the month
    is changed, so you will often want to update them in ``EVT_CALENDAR_PAGE_CHANGED``
    event handler.

    |styles| Window Styles
    ================================

    This class supports the following styles:

    - ``CAL_SUNDAY_FIRST``: Show Sunday as the first day in the week (not in wxGTK)
    - ``CAL_MONDAY_FIRST``: Show Monday as the first day in the week (not in wxGTK)
    - ``CAL_SHOW_HOLIDAYS``: Highlight holidays in the calendar (only generic)
    - ``CAL_NO_YEAR_CHANGE``: Disable the year changing (deprecated, only generic)
    - ``CAL_NO_MONTH_CHANGE``: Disable the month (and, implicitly, the year) changing
    - ``CAL_SHOW_SURROUNDING_WEEKS``: Show the neighbouring weeks in the previous and next months (only generic, always on for the native controls)
    - ``CAL_SEQUENTIAL_MONTH_SELECTION``: Use alternative, more compact, style for the month and year selection controls. (only generic)
    - ``CAL_SHOW_WEEK_NUMBERS``: Show week numbers on the left side of the calendar. (not in generic)

    |events| Events Emitted by this Class
    =====================================

    Handlers bound for the following event types will receive a :ref:`adv.CalendarEvent` parameter.

    - EVT_CALENDAR: A day was double clicked in the calendar.
    - EVT_CALENDAR_SEL_CHANGED: The selected date changed.
    - EVT_CALENDAR_PAGE_CHANGED: The selected month (and/or year) changed.
    - EVT_CALENDAR_WEEKDAY_CLICKED: User clicked on the week day header (only generic).
    - EVT_CALENDAR_WEEK_CLICKED: User clicked on the week of the year number (only generic).

    |method_summary| Methods Summary
    ================================

    ========================================== ================================================================================
    :meth:`~CalendarCtrl.__init__`             Default constructor.
    :meth:`~CalendarCtrl.Create`               Creates the control.
    :meth:`~CalendarCtrl.EnableHolidayDisplay` This function should be used instead of changing  ``CAL_SHOW_HOLIDAYS``   style bit directly.
    :meth:`~CalendarCtrl.EnableMonthChange`    This function should be used instead of changing  ``CAL_NO_MONTH_CHANGE``   style bit.
    :meth:`~CalendarCtrl.GetAttr`              Returns the attribute for the given date (should be in the range 1...31).
    :meth:`~CalendarCtrl.GetDate`              Gets the currently selected date.
    :meth:`~CalendarCtrl.GetDateRange`         Returns the limits currently being used.
    :meth:`~CalendarCtrl.GetHeaderColourBg`    Gets the background colour of the header part of the calendar window.
    :meth:`~CalendarCtrl.GetHeaderColourFg`    Gets the foreground colour of the header part of the calendar window.
    :meth:`~CalendarCtrl.GetHighlightColourBg` Gets the background highlight colour.
    :meth:`~CalendarCtrl.GetHighlightColourFg` Gets the foreground highlight colour.
    :meth:`~CalendarCtrl.GetHolidayColourBg`   Return the background colour currently used for holiday highlighting.
    :meth:`~CalendarCtrl.GetHolidayColourFg`   Return the foreground colour currently used for holiday highlighting.
    :meth:`~CalendarCtrl.HitTest`              Returns one of CalendarHitTestResult constants and fills either `date` or `wd` pointer with the corresponding value depending on the hit test code.
    :meth:`~CalendarCtrl.Mark`                 Mark or unmark the day.
    :meth:`~CalendarCtrl.ResetAttr`            Clears any attributes associated with the given day (in the range 1...31).
    :meth:`~CalendarCtrl.SetAttr`              Associates the attribute with the specified date (in the range 1...31).
    :meth:`~CalendarCtrl.SetDate`              Sets the current date.
    :meth:`~CalendarCtrl.SetDateRange`         Restrict the dates that can be selected in the control to the specified range.
    :meth:`~CalendarCtrl.SetHeaderColours`     Set the colours used for painting the weekdays at the top of the control.
    :meth:`~CalendarCtrl.SetHighlightColours`  Set the colours to be used for highlighting the currently selected date.
    :meth:`~CalendarCtrl.SetHoliday`           Marks the specified day as being a holiday in the current month.
    :meth:`~CalendarCtrl.SetHolidayColours`    Sets the colours to be used for the holidays highlighting.
    ========================================== ================================================================================

    |property_summary| Properties Summary
    =====================================

    ======================================= =============================================================================
    :attr:`~CalendarCtrl.Date`              See :meth:`~CalendarCtrl.GetDate` and :meth:`~CalendarCtrl.SetDate`
    :attr:`~CalendarCtrl.DateRange`         See :meth:`~CalendarCtrl.GetDateRange` and :meth:`~CalendarCtrl.SetDateRange`
    :attr:`~CalendarCtrl.HeaderColourBg`    See :meth:`~CalendarCtrl.GetHeaderColourBg`
    :attr:`~CalendarCtrl.HeaderColourFg`    See :meth:`~CalendarCtrl.GetHeaderColourFg`
    :attr:`~CalendarCtrl.HighlightColourBg` See :meth:`~CalendarCtrl.GetHighlightColourBg`
    :attr:`~CalendarCtrl.HighlightColourFg` See :meth:`~CalendarCtrl.GetHighlightColourFg`
    :attr:`~CalendarCtrl.HolidayColourBg`   See :meth:`~CalendarCtrl.GetHolidayColourBg`
    :attr:`~CalendarCtrl.HolidayColourFg`   See :meth:`~CalendarCtrl.GetHolidayColourFg`
    ======================================= =============================================================================

    """
    def __init__(self, parent, id=wx.ID_ANY, date=wx.DateTime.Now(),
                 pos=wx.DefaultPosition, size=wx.DefaultSize,
                 style=CAL_SHOW_HOLIDAYS, name='panel'):
        """
        Default class constructor.

        :param `parent`: Pointer to a parent window. Must not be ``None``.
        :type `parent`: `wx.Window`
        :param `id`: Window identifier. ``wx.ID_ANY`` indicates a default value.
        :type `id`: int
        :param `pos`: Window position. The value ``wx.DefaultPosition`` indicates a default position, chosen by either the windowing system or wxWidgets, depending on platform.
        :type `pos`: `wx.Point`
        :param `size`: Window size. The value ``wx.DefaultSize`` indicates a default size, chosen by either the windowing system or wxWidgets, depending on platform.
        :type `size`: `wx.Size`
        :param `style`: Window style.
        :type `style`: long
        :param `name`: Window name.
        :type `name`: str
        """
        wx.Panel.__init__(self, parent, id, pos, size, style, name)

        # today = calendar.datetime.date.today()
        # self.Date = today
        self.Date = date
        # .. attribute:: Date
        #    See :meth:`~CalendarCtrl.GetDate` and :meth:`~CalendarCtrl.SetDate`
        self.DateRange = (wx.DefaultDateTime, wx.DefaultDateTime)  # bool,
        # .. attribute:: DateRange
        #    See :meth:`~CalendarCtrl.GetDateRange` and :meth:`~CalendarCtrl.SetDateRange`
        self.HeaderColourBg = wx.NullColour
        # .. attribute:: HeaderColourBg
        #    See :meth:`~CalendarCtrl.GetHeaderColourBg`
        self.HeaderColourFg = wx.NullColour
        # .. attribute:: HeaderColourFg
        #    See :meth:`~CalendarCtrl.GetHeaderColourFg`
        self.HighlightColourBg = wx.NullColour
        # .. attribute:: HighlightColourBg
        #    See :meth:`~CalendarCtrl.GetHighlightColourBg`
        self.HighlightColourFg = wx.NullColour
        # .. attribute:: HighlightColourFg
        #    See :meth:`~CalendarCtrl.GetHighlightColourFg`
        self.HolidayColourBg = wx.NullColour
        # .. attribute:: HolidayColourBg
        #    See :meth:`~CalendarCtrl.GetHolidayColourBg`
        self.HolidayColourFg = wx.NullColour
        # .. attribute:: HolidayColourFg
        #    See :meth:`~CalendarCtrl.GetHolidayColourFg`
@ricpol

This comment has been minimized.

Copy link
Contributor Author

@ricpol ricpol commented May 22, 2019

@RobinD42

Please do, and submit it here as a PR.

Will do

@Metallicow
I see your point but that's a task for another day... right now the goal shoud be to fix the bugs in wx.lib.calendar...

@Metallicow

This comment has been minimized.

Copy link
Contributor

@Metallicow Metallicow commented May 22, 2019

@ricpol Yes, it is a task. Not a priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.