10.11. Calendar

The Calendar widget is an effective way to display and retrieve monthly date related information. It is a very simple widget to create and work with.

Creating a Calendar widget is a simple as:

Gtk::Calendar.new

There might be times where you need to change a lot of information within this widget and the following methods allow you to make multiple changes to a Calendar widget without the user seeing multiple on-screen updates.

Gtk::Calendar#freeze

Gtk::Calendar#thaw

They work just like the freeze/thaw methods of every other widget.

The Calendar widget has a few options that allow you to change the way the widget both looks and operates by using the method

Gtk::Calendar#display_options( flags )

The flags argument can be formed by combining either of the following five options using the logical bitwise OR (|) operation:

Gtk::Calendar::SHOW_HEADING

this option specifies that the month and year should be shown when drawing the calendar.

Gtk::Calendar::SHOW_DAY_NAMES

this option specifies that the three letter descriptions should be displayed for each day (eg Mon,Tue, etc.).

Gtk::Calendar::NO_MONTH_CHANGE

this option states that the user should not and can not change the currently displayed month. This can be good if you only need to display a particular month such as if you are displaying 12 calendar widgets for every month in a particular year.

Gtk::Calendar::SHOW_WEEK_NUMBERS

this option specifies that the number for each week should be displayed down the left side of the calendar. (eg. Jan 1 = Week 1,Dec 31 = Week 52).

Gtk::Calendar::WEEK_START_MONDAY

this option states that the calander week will start on Monday instead of Sunday which is the default. This only affects the order in which days are displayed from left to right.

The following methods are used to set the the currently displayed date:

Gtk::Calendar#select_month( month, year )

Gtk::Calendar#select_day( day )

The return value from Gtk::Calendar#select_month is a boolean value indicating whether the selection was successful.

With Gtk::Calendar#select_day the specified day number is selected within the current month, if that is possible. A day value of 0 will deselect any current selection.

In addition to having a day selected, any number of days in the month may be "marked". A marked day is highlighted within the calendar display. The following methods are provided to manipulate marked days:

Gtk::Calendar#mark_day( day )

Gtk::Calendar#unmark_day( day )

Gtk::Calendar#clear_marks

The currently marked days are stored within an array within the Calendar structure. This array is 31 elements long so to test whether a particular day is currently marked, you need to access the corresponding element of the array. For example:

calendar = Gtk::Calendar#new

    ...

    # Is day 7 marked?
    if calendar[7]
       # day is marked

Note that marks are persistent across month and year changes.

The final Calendar widget method is used to retrieve the currently selected date, month and/or year.

Gtk::Calendar#date( year, month, day )

This method requires you to pass variables, into which the result will be placed. Passing nill as a value will result in the corresponding value not being returned.

The Calendar widget can generate a number of signals indicating date selection and change. The names of these signals are self explanatory, and are:

 month_changed
day_selected
day_selected_double_click
prev_month
next_month
prev_year
next_year

That just leaves us with the need to put all of this together into some basic example code.

CODE
CODE
CODE
CODE
CODE
CODE

Prev Next