GTK+ 2.0 教學-日曆 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參數可以將下面的五種選項中的一個或者多個用邏輯位元OR(|)運算子組合起來:

GTK_CALENDAR_SHOW_HEADING
這個選項指定在繪製日曆元件時,應該顯示月份和年份。

GTK_CALENDAR_SHOW_DAY_NAMES
這個選項指定用三個字母的縮寫顯示每一天是星期幾(比如Mon、Tue等)。

GTK_CALENDAR_NO_MONTH_CHANGE
這個選項指定用戶不應該也不能夠改變顯示的月份。如果只想顯示某個特定的月份,則可以使用這個選項。比如,在視窗上同時為一年的12個月分別設置一個日曆元件時。

GTK_CALENDAR_SHOW_WEEK_NUMBERS
這個選項指定應該在日曆的左邊顯示每一週在全年的周序號(例如;1月1日是第1周,12月31日是第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

下面是一個日曆元件的範例,運用了上面介紹的各項特性。

/*
 * 版權 (C) 1998 Cesar Miquel, Shawn T. Amundson, Mattias Gr�nlund
 * 版權 (C) 2000 Tony Gale
 *
 * 本程式是自由軟體。你可以在自由軟體基金發佈的 GNU GPL 的條款下重新分發
 * 或修改它。GPL 可以使用版本 2 或(由你選擇)任何隨後的版本。
 *
 * 本程式分發的目的是它可能對其他人有用,但不提供任何的擔保,包括隱含的
 * 和適合特定用途的保證。請查閱GNU通用公共許可證獲得詳細的信息。
 * 你應該已經隨該軟體一起收到一份GNU通用公共許可。如果還沒有,請寫信給
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.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];
  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 日曆元件
 */
static void calendar_date_to_string( CalendarData *data,
                                     char         *buffer,
                                     gint          buff_len )
{
  GDate date;
  guint year, month, day;

  gtk_calendar_get_date (GTK_CALENDAR (data->window),
			 &year, &month, &day);
  g_date_set_dmy (&date, day, month + 1, year);
  g_date_strftime (buffer, buff_len - 1, "%x", &date);

}

static 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);
}

static 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);
}

static 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);
}

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

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

  gtk_calendar_get_date (GTK_CALENDAR (data->window),
			 NULL, NULL, &day);

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

static 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);
}

static 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);
}

static 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);
}

static 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);
}

static 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);
}

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

}

static void calendar_font_selection_ok( GtkWidget    *button,
                                        CalendarData *calendar )
{
  GtkRcStyle *style;
  char *font_name;

  if (calendar->window)
    {
      font_name = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (calendar->font_dialog));
      if (font_name) 
	{
	  style = gtk_rc_style_new ();
	  pango_font_description_free (style->font_desc);
	  style->font_desc = pango_font_description_from_string (font_name);
	  gtk_widget_modify_style (calendar->window, style);
	  g_free (font_name);
	}
    }

  gtk_widget_destroy (calendar->font_dialog);
}

static 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 (window, "destroy",
		      G_CALLBACK (gtk_widget_destroyed),
		      &calendar->font_dialog);

    g_signal_connect (GTK_FONT_SELECTION_DIALOG (window)->ok_button,
		      "clicked", G_CALLBACK (calendar_font_selection_ok),
		      calendar);
    g_signal_connect_swapped (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);

}

static void create_calendar( void )
{
  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_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 (window, "destroy",
		    G_CALLBACK (gtk_main_quit),
		    NULL);
  g_signal_connect (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 (calendar, "month_changed", 
		    G_CALLBACK (calendar_month_changed),
		    &calendar_data);
  g_signal_connect (calendar, "day_selected", 
		    G_CALLBACK (calendar_day_selected),
		    &calendar_data);
  g_signal_connect (calendar, "day_selected_double_click", 
		    G_CALLBACK (calendar_day_selected_double_click),
		    &calendar_data);
  g_signal_connect (calendar, "prev_month", 
		    G_CALLBACK (calendar_prev_month),
		    &calendar_data);
  g_signal_connect (calendar, "next_month", 
		    G_CALLBACK (calendar_next_month),
		    &calendar_data);
  g_signal_connect (calendar, "prev_year", 
		    G_CALLBACK (calendar_prev_year),
		    &calendar_data);
  g_signal_connect (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 (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 (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 (button, "clicked", 
		    G_CALLBACK (gtk_main_quit), 
		    NULL);
  gtk_container_add (GTK_CONTAINER (bbox), button);
  gtk_widget_set_can_default (button, TRUE);
  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;
}
« 單元首頁
 »

感謝你看到這裡,很快就可以離開了,但最好的獎勵行動就是按一下幫我分享或留言,感恩喔~

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *