日历 Calendar

日历(Calendar)控件是显示和获取每月日期等信息的高效方法。它是一个很容易创建和使用的控件。

创建日历控件的方法和其它控件的类似:

GtkWidget *gtk_calendar_new( void );

有时候,需要同时对控件的外观和内容做很多的修改。这时候可能会引起控件的多次更新,导致屏幕闪烁。可以在修改之前使用一个函数将控件“冻结”,然后在修改完成之后再用一个函数将控件“解冻”。这样,控件在整个过程中只做一次更新。

void gtk_calendar_freeze( GtkCalendar *Calendar );

void gtk_calendar_thaw( GtkCalendar *Calendar );

这两个函数和其它控件的冻结/解冻(freeze/thaw)函数作用完全一样。

日历控件有几个选项,可以用来改变控件的外观和操作方式。使用下面的函数可以改变这些选项:

void gtk_calendar_display_options( GtkCalendar               *calendar,
                                   GtkCalendarDisplayOptions  flags );

函数中的flags参数可以将下面的五种选项中的一个或者多个用逻辑位或(|)操作符组合起来:

GTK_CALENDAR_SHOW_HEADING
这个选项指定在绘制日历控件时,应该显示月份和年份。

GTK_CALENDAR_SHOW_DAY_NAMES
这个选项指定用三个字母的缩写显示每一天是星期几(比如Mon、Tue等)。

GTK_CALENDAR_NO_MONTH_CHANGE
这个选项指定用户不应该也不能够改变显示的月份。如果只想显示某个特定的月份,则可以使用这个选项。比如,在窗口上同时为一年的12个月分别设置一个日历控件时。

GTK_CALENDAR_SHOW_WEEK_NUMBERS
这个选项指定应该在日历的左边显示每一周在全年的周序号(例如;11日是第1周,1231日是第52周)。

GTK_CALENDAR_WEEK_START_MONDAY
这个选项指定在日历控件中每一周是从星期一开始而不是从星期天开始。缺省设置是从星期天开始。此选项只影响日期在控件中从左到右的排列顺序。

下面的函数用于设置当前要显示的日期:

gint gtk_calendar_select_month( GtkCalendar *calendar, 
                                guint        month,
                                guint        year );

void gtk_calendar_select_day( GtkCalendar *calendar,
                              guint        day ); 

gtk_calendar_select_month()的返回值是一个布尔值,指示设置是否成功。

使用gtk_calendar_select_day()函数,如果指定的日期是合法的,会在日历控件中选中该日期。如果day参数的值是0,将清除当前的选择。

除了可以选中一个日期以外,在一个月中可以有任意个日期被“标记”。被“标记”的日期会在日历控件中高亮显示。下面的函数用于标记日期和取消标记:

gint gtk_calendar_mark_day( GtkCalendar *calendar,
                            guint        day);

gint gtk_calendar_unmark_day( GtkCalendar *calendar,
                              guint        day);

void gtk_calendar_clear_marks( GtkCalendar *calendar); 

当前标记的日期存储在一个GtkCalendar结构的数组中。数组的长度是31,这样,要想知道某个特定的日期是否被标记,可以访问数值中相应的元素(注意,在C语言中,数值是从0开始编号的)。例如:

    GtkCalendar *calendar;
    calendar = gtk_calendar_new ();

    ...

    /* 当月7日被标记了吗? */
    if (calendar->marked_date[7-1])
       /* 若执行此处的代码,表明7日已经被标记 */ 

注意,在月份和年份变化时,被标记的日期是不会变化的。

日历控件的最后一个函数用于取得当前选中的日/月/年值:

void gtk_calendar_get_date( GtkCalendar *calendar, 
                            guint       *year,
                            guint       *month,
                            guint       *day ); 

使用这个函数时,需要先声明几个guint类型的变量,再把变量地址传递给函数。所需要的返回值就存放在这几个变量中。如果将某一个参数设置为NULL,则不返回该值。

日历控件能够引发许多信号,用于指示日期被选中以及选择发生的变化。信号的意义很容易理解。信号名称如下:


month_changed

day_selected

day_selected_double_click

prev_month

next_month

prev_year

next_year

示例

效果

[… 待补充]

源码

[… 待验证]

下面是一个日历控件的示例,运用了上面介绍的各项特性。

/* GTK - GIMP工具包
 * 版权 (C) 1995-1997 Peter Mattis, Spencer Kimball 和 Josh MacDonald 所有
 *
 * 本程序是自由软件。你可以在自由软件基金发布的 GNU GPL 的条款下重新分发
 * 或修改它。GPL 可以使用版本 2 或(由你选择)任何随后的版本。
 *
 * 本程序分发的目的是它可能对其他人有用,但不提供任何的担保,包括隐含的
 * 和适合特定用途的保证。请查阅GNU通用公共许可证获得详细的信息。
 *
 * 你应该已经随该软件一起收到一份GNU通用公共许可。如果还没有,请写信给
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#define DEF_PAD 10
#define DEF_PAD_SMALL 5

#define TM_YEAR_BASE 1900

typedef struct _CalendarData {
    GtkWidget *flag_checkboxes[5];
    gboolean  settings[5];
    gchar     *font;
    GtkWidget *font_dialog;
    GtkWidget *window;
    GtkWidget *prev2_sig;
    GtkWidget *prev_sig;
    GtkWidget *last_sig;
    GtkWidget *month;
} CalendarData;

enum {
    calendar_show_header,
    calendar_show_days,
    calendar_month_change, 
    calendar_show_week,
    calendar_monday_first
};

/*
 * GtkCalendar 日历控件
 */
void calendar_date_to_string( CalendarData *data,
                  char         *buffer,
                  gint          buff_len )
{
    struct tm tm;
    time_t time;

    memset (&tm, 0, sizeof (tm));
    gtk_calendar_get_date (GTK_CALENDAR (data->window),
                                &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
    tm.tm_year -= TM_YEAR_BASE;
    time = mktime (&tm);
    strftime (buffer, buff_len-1, "%x", gmtime (&time));
}

void calendar_set_signal_strings( char         *sig_str,
                  CalendarData *data)
{
    const gchar *prev_sig;

    prev_sig = gtk_label_get_text (GTK_LABEL (data->prev_sig));
    gtk_label_set_text (GTK_LABEL (data->prev2_sig), prev_sig);

    prev_sig = gtk_label_get_text (GTK_LABEL (data->last_sig));
    gtk_label_set_text (GTK_LABEL (data->prev_sig), prev_sig);
    gtk_label_set_text (GTK_LABEL (data->last_sig), sig_str);
}

void calendar_month_changed( GtkWidget    *widget,
                             CalendarData *data )
{
    char buffer[256] = "month_changed: ";

    calendar_date_to_string (data, buffer+15, 256-15);
    calendar_set_signal_strings (buffer, data);
}

void calendar_day_selected( GtkWidget    *widget,
                            CalendarData *data )
{
    char buffer[256] = "day_selected: ";

    calendar_date_to_string (data, buffer+14, 256-14);
    calendar_set_signal_strings (buffer, data);
}

void calendar_day_selected_double_click( GtkWidget    *widget,
                                         CalendarData *data )
{
    struct tm tm;
    char buffer[256] = "day_selected_double_click: ";

    calendar_date_to_string (data, buffer+27, 256-27);
    calendar_set_signal_strings (buffer, data);

    memset (&tm, 0, sizeof (tm));
    gtk_calendar_get_date (GTK_CALENDAR (data->window),
                                &tm.tm_year, &tm.tm_mon, &tm.tm_mday);
    tm.tm_year -= TM_YEAR_BASE;

    if (GTK_CALENDAR (data->window)->marked_date[tm.tm_mday-1] == 0) 
    {
        gtk_calendar_mark_day (GTK_CALENDAR (data->window), tm.tm_mday);
    } 
    else 
    { 
        gtk_calendar_unmark_day (GTK_CALENDAR (data->window), tm.tm_mday);
    }
}

void calendar_prev_month( GtkWidget    *widget,
                            CalendarData *data )
{
    char buffer[256] = "prev_month: ";

    calendar_date_to_string (data, buffer+12, 256-12);
    calendar_set_signal_strings (buffer, data);
}

void calendar_next_month( GtkWidget    *widget,
                            CalendarData *data )
{
    char buffer[256] = "next_month: ";

    calendar_date_to_string (data, buffer+12, 256-12);
    calendar_set_signal_strings (buffer, data);
}

void calendar_prev_year( GtkWidget    *widget,
                            CalendarData *data )
{
    char buffer[256] = "prev_year: ";

    calendar_date_to_string (data, buffer+11, 256-11);
    calendar_set_signal_strings (buffer, data);
}

void calendar_next_year( GtkWidget    *widget,
                            CalendarData *data )
{
    char buffer[256] = "next_year: ";

    calendar_date_to_string (data, buffer+11, 256-11);
    calendar_set_signal_strings (buffer, data);
}


void calendar_set_flags( CalendarData *calendar )
{
    gint i;
    gint options = 0;
    for (i = 0; i < 5; i++) 
    {
        if (calendar->settings[i])
        {
            options=options + (1<<i);
        }
    }
    if (calendar->window)
    {
        gtk_calendar_display_options (GTK_CALENDAR (calendar->window), options);
    }
}

void calendar_toggle_flag( GtkWidget    *toggle,
                           CalendarData *calendar )
{
    gint i;
    gint j;
    j = 0;

    for (i = 0; i < 5; i++)
    {
        if (calendar->flag_checkboxes[i] == toggle)
        {
            j = i;
        }
    }

    calendar->settings[j] = !calendar->settings[j];
    calendar_set_flags (calendar);
}

void calendar_font_selection_ok( GtkWidget    *button,
                                 CalendarData *calendar )
{
    GtkStyle *style;
    PangoFontDescription *font_desc;

    calendar->font = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (calendar->font_dialog));
    if (calendar->window)
    {
        font_desc = pango_font_description_from_string (calendar->font);
        if (font_desc) 
        {
            style = gtk_style_copy (gtk_widget_get_style (calendar->window));
            style->font_desc = font_desc;
            gtk_widget_set_style (calendar->window, style);
        }
    }
}

void calendar_select_font( GtkWidget    *button,
                           CalendarData *calendar )
{
    GtkWidget *window;

    if (!calendar->font_dialog)
    {
        window = gtk_font_selection_dialog_new ("Font Selection Dialog");
        g_return_if_fail (GTK_IS_FONT_SELECTION_DIALOG (window));
        calendar->font_dialog = window;

        gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);

        g_signal_connect (G_OBJECT (window), "destroy",
                                G_CALLBACK (gtk_widget_destroyed),
                                &calendar->font_dialog);

        g_signal_connect (G_OBJECT (GTK_FONT_SELECTION_DIALOG (window)->ok_button),
                                "clicked", G_CALLBACK (calendar_font_selection_ok),
                                calendar);
        g_signal_connect_swapped (G_OBJECT (GTK_FONT_SELECTION_DIALOG (window)->cancel_button),
                                "clicked",
                                G_CALLBACK (gtk_widget_destroy),
                                calendar->font_dialog);
    }

    window=calendar->font_dialog;
    if (!GTK_WIDGET_VISIBLE (window))
    {
        gtk_widget_show (window);
    }
    else
    {
        gtk_widget_destroy (window);
    }

}

void create_calendar()
{
    GtkWidget *window;
    GtkWidget *vbox, *vbox2, *vbox3;
    GtkWidget *hbox;
    GtkWidget *hbbox;
    GtkWidget *calendar;
    GtkWidget *toggle;
    GtkWidget *button;
    GtkWidget *frame;
    GtkWidget *separator;
    GtkWidget *label;
    GtkWidget *bbox;
    static CalendarData calendar_data;
    gint i;

    struct {
        char *label;
    } flags[] =
    {
        { "Show Heading" },
        { "Show Day Names" },
        { "No Month Change" },
        { "Show Week Numbers" },
        { "Week Start Monday" }
    };

    calendar_data.window = NULL;
    calendar_data.font = NULL;
    calendar_data.font_dialog = NULL;

    for (i = 0; i < 5; i++)
    {
        calendar_data.settings[i] = 0;
    }

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "GtkCalendar Example");
    gtk_container_set_border_width (GTK_CONTAINER (window), 5);
    g_signal_connect (G_OBJECT (window), "destroy",
                        G_CALLBACK (gtk_main_quit),
                        NULL);
    g_signal_connect (G_OBJECT (window), "delete-event",
                        G_CALLBACK (gtk_false),
                        NULL);

    gtk_window_set_resizable (GTK_WINDOW (window), FALSE);

    vbox = gtk_vbox_new (FALSE, DEF_PAD);
    gtk_container_add (GTK_CONTAINER (window), vbox);

    /*
    * 顶级窗口,其中包含日历控件,设置日历各参数的复选按钮和设置字体的按钮
    */
    hbox = gtk_hbox_new (FALSE, DEF_PAD);
    gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, DEF_PAD);
    hbbox = gtk_hbutton_box_new ();
    gtk_box_pack_start (GTK_BOX (hbox), hbbox, FALSE, FALSE, DEF_PAD);
    gtk_button_box_set_layout (GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_SPREAD);
    gtk_box_set_spacing (GTK_BOX (hbbox), 5);

    /* 日历控件 */
    frame = gtk_frame_new ("Calendar");
    gtk_box_pack_start (GTK_BOX (hbbox), frame, FALSE, TRUE, DEF_PAD);
    calendar=gtk_calendar_new ();
    calendar_data.window = calendar;
    calendar_set_flags (&calendar_data);
    gtk_calendar_mark_day (GTK_CALENDAR (calendar), 19);
    gtk_container_add( GTK_CONTAINER (frame), calendar);
    g_signal_connect (G_OBJECT (calendar), "month_changed",
                            G_CALLBACK (calendar_month_changed),
                            &calendar_data);
    g_signal_connect (G_OBJECT (calendar), "day_selected",
                            G_CALLBACK (calendar_day_selected),
                            &calendar_data);
    g_signal_connect (G_OBJECT (calendar), "day_selected_double_click", 
                            G_CALLBACK (calendar_day_selected_double_click),
                            &calendar_data);
    g_signal_connect (G_OBJECT (calendar), "prev_month",
                            G_CALLBACK (calendar_prev_month),
                            &calendar_data);
    g_signal_connect (G_OBJECT (calendar), "next_month",
                            G_CALLBACK (calendar_next_month),
                            &calendar_data);
    g_signal_connect (G_OBJECT (calendar), "prev_year",
                            G_CALLBACK (calendar_prev_year),
                            &calendar_data);
    g_signal_connect (G_OBJECT (calendar), "next_year",
                            G_CALLBACK (calendar_next_year),
                            &calendar_data);

    separator = gtk_vseparator_new ();
    gtk_box_pack_start (GTK_BOX (hbox), separator, FALSE, TRUE, 0);

    vbox2 = gtk_vbox_new (FALSE, DEF_PAD);
    gtk_box_pack_start (GTK_BOX (hbox), vbox2, FALSE, FALSE, DEF_PAD);

    /* 创建一个框架,放入设置各种参数的复选按钮 */ 
    frame = gtk_frame_new ("Flags");
    gtk_box_pack_start (GTK_BOX (vbox2), frame, TRUE, TRUE, DEF_PAD);
    vbox3 = gtk_vbox_new (TRUE, DEF_PAD_SMALL);
    gtk_container_add (GTK_CONTAINER (frame), vbox3);

    for (i = 0; i < 5; i++)
    {
        toggle = gtk_check_button_new_with_label (flags[i].label);
        g_signal_connect (G_OBJECT (toggle),
                            "toggled",
                            G_CALLBACK (calendar_toggle_flag),
                            &calendar_data);
        gtk_box_pack_start (GTK_BOX (vbox3), toggle, TRUE, TRUE, 0);
        calendar_data.flag_checkboxes[i] = toggle;
    }
    /* 创建一个按钮,用于设置字体 */ 
    button = gtk_button_new_with_label ("Font...");
    g_signal_connect (G_OBJECT (button),
                            "clicked",
                            G_CALLBACK (calendar_select_font),
                            &calendar_data);
    gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);

    /*
    *  创建“信号-事件”部分
    */
    frame = gtk_frame_new ("Signal events");
    gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, DEF_PAD);

    vbox2 = gtk_vbox_new (TRUE, DEF_PAD_SMALL);
    gtk_container_add (GTK_CONTAINER (frame), vbox2);

    hbox = gtk_hbox_new (FALSE, 3);
    gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
    label = gtk_label_new ("Signal:");
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
    calendar_data.last_sig = gtk_label_new ("");
    gtk_box_pack_start (GTK_BOX (hbox), calendar_data.last_sig, FALSE, TRUE, 0);

    hbox = gtk_hbox_new (FALSE, 3);
    gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
    label = gtk_label_new ("Previous signal:");
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
    calendar_data.prev_sig = gtk_label_new ("");
    gtk_box_pack_start (GTK_BOX (hbox), calendar_data.prev_sig, FALSE, TRUE, 0);

    hbox = gtk_hbox_new (FALSE, 3);
    gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
    label = gtk_label_new ("Second previous signal:");
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
    calendar_data.prev2_sig = gtk_label_new ("");
    gtk_box_pack_start (GTK_BOX (hbox), calendar_data.prev2_sig, FALSE, TRUE, 0);

    bbox = gtk_hbutton_box_new ();
    gtk_box_pack_start (GTK_BOX (vbox), bbox, FALSE, FALSE, 0);
    gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);

    button = gtk_button_new_with_label ("Close");
    g_signal_connect (G_OBJECT (button), "clicked",
                        G_CALLBACK (gtk_main_quit),
                        NULL);
    gtk_container_add (GTK_CONTAINER (bbox), button);
    GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
    gtk_widget_grab_default (button);

    gtk_widget_show_all (window);
}

int main(int   argc,
         char *argv[] )
{
    gtk_init (&argc, &argv);

    create_calendar ();

    gtk_main ();

    return 0;
}