New section on GtkCalendar Update example code

Wed Feb 23 10:54:14 GMT 2000  Tony Gale <gale@gtk.org>

        * docs/gtk_tut.sgml: New section on GtkCalendar
        * examples/calendar: Update example code
This commit is contained in:
GMT 2000 Tony Gale
2000-02-23 10:59:21 +00:00
committed by Tony Gale
parent 811cd0c124
commit 2444c80ff7
11 changed files with 1405 additions and 124 deletions

View File

@ -1,3 +1,8 @@
Wed Feb 23 10:54:14 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml: New section on GtkCalendar
* examples/calendar: Update example code
Tue Feb 22 13:54:12 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: FAQ Update

View File

@ -1,3 +1,8 @@
Wed Feb 23 10:54:14 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml: New section on GtkCalendar
* examples/calendar: Update example code
Tue Feb 22 13:54:12 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: FAQ Update

View File

@ -1,3 +1,8 @@
Wed Feb 23 10:54:14 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml: New section on GtkCalendar
* examples/calendar: Update example code
Tue Feb 22 13:54:12 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: FAQ Update

View File

@ -1,3 +1,8 @@
Wed Feb 23 10:54:14 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml: New section on GtkCalendar
* examples/calendar: Update example code
Tue Feb 22 13:54:12 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: FAQ Update

View File

@ -1,3 +1,8 @@
Wed Feb 23 10:54:14 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml: New section on GtkCalendar
* examples/calendar: Update example code
Tue Feb 22 13:54:12 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: FAQ Update

View File

@ -1,3 +1,8 @@
Wed Feb 23 10:54:14 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml: New section on GtkCalendar
* examples/calendar: Update example code
Tue Feb 22 13:54:12 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: FAQ Update

View File

@ -1,3 +1,8 @@
Wed Feb 23 10:54:14 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtk_tut.sgml: New section on GtkCalendar
* examples/calendar: Update example code
Tue Feb 22 13:54:12 GMT 2000 Tony Gale <gale@gtk.org>
* docs/gtkfaq.sgml: FAQ Update

View File

@ -11,7 +11,7 @@ Tony Gale <tt><htmlurl url="mailto:gale@gtk.org"
name="&lt;gale@gtk.org&gt;"></tt>,
Ian Main <tt><htmlurl url="mailto:imain@gtk.org"
name="&lt;imain@gtk.org&gt;"></tt>
<date>November 13th, 1999
<date>February 23rd, 2000
<abstract>
This is a tutorial on how to use GTK (the GIMP Toolkit) through its C
interface.
@ -33,7 +33,7 @@ open software, free software, or even commercial non-free software
using GTK without having to spend anything for licenses or royalties.
It's called the GIMP toolkit because it was originally written for
developing the General Image Manipulation Program (GIMP), but GTK has
developing the GNU Image Manipulation Program (GIMP), but GTK has
now been used in a large number of software projects, including the
GNU Network Object Model Environment (GNOME) project. GTK is built on
top of GDK (GIMP Drawing Kit) which is basically a wrapper around the
@ -839,15 +839,31 @@ So, by passing in the widget you wish to remove the handler from, and
the tag returned by one of the signal_connect functions, you can
disconnect a signal handler.
Another function to remove all the signal handers from an object is:
You can also temporarily disable signal handlers with the
gtk_signal_handler_block() and gtk_signal_handler_unblock() family of
functions.
<tscreen><verb>
void gtk_signal_handlers_destroy( GtkObject *object );
</verb></tscreen>
void gtk_signal_handler_block( GtkObject *object,
guint handler_id );
This call is fairly self explanatory. It simply removes all the
current signal handlers from the object passed in as the first
argument.
void gtk_signal_handler_block_by_func( GtkObject *object,
GtkSignalFunc func,
gpointer data );
void gtk_signal_handler_block_by_data( GtkObject *object,
gpointer data );
void gtk_signal_handler_unblock( GtkObject *object,
guint handler_id );
void gtk_signal_handler_unblock_by_func( GtkObject *object,
GtkSignalFunc func,
gpointer data );
void gtk_signal_handler_unblock_by_data( GtkObject *object,
gpointer data);
</verb></tscreen>
<!-- ----------------------------------------------------------------- -->
<sect1>An Upgraded Hello World
@ -4842,6 +4858,7 @@ The following code is an example of using an Entry widget.
<tscreen><verb>
/* example-start entry entry.c */
#include <stdio.h>
#include <gtk/gtk.h>
void enter_callback( GtkWidget *widget,
@ -5170,6 +5187,7 @@ It's example time again.
<tscreen><verb>
/* example-start spinbutton spinbutton.c */
#include <stdio.h>
#include <gtk/gtk.h>
static GtkWidget *spinner1;
@ -5565,6 +5583,578 @@ gtk_combo_set_item_string(GtkCombo *combo, GtkItem *item, const gchar
appropriate Item. Frankly, I have no idea how to do that.
-->
<!-- ----------------------------------------------------------------- -->
<sect1> Calendar
<p>
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 GtkCalendar widget is a simple as:
<tscreen><verb>
GtkWidget *gtk_calendar_new();
</verb></tscreen>
There might be times where you need to change a lot of information
within this widget and the following functions allow you to make
multiple change to a Calendar widget without the user seeing multiple
on-screen updates.
<tscreen><verb>
void gtk_calendar_freeze( GtkCalendar *Calendar );
void gtk_calendar_thaw ( GtkCalendar *Calendar );
</verb></tscreen>
They work just like the freeze/thaw functions 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 function
<tscreen><verb>
void gtk_calendar_display_options( GtkCalendar *calendar,
GtkCalendarDisplayOptions flags );
</verb></tscreen>
The <tt/flags/ argument can be formed by combining either of the
following five options using the logical bitwise OR (|) operation:
<itemize>
<item> GTK_CALENDAR_SHOW_HEADING - this option specifies that
the month and year should be shown when drawing the calendar.
<item> GTK_CALENDAR_SHOW_DAY_NAMES - this option specifies that the
three letter descriptions should be displayed for each day (eg
MON,TUE...).
<item> 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.
<item> 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).
<item> 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.
</itemize>
The following functions are used to set the the currently displayed
date:
<tscreen><verb>
gint gtk_calendar_select_month( GtkCalendar *calendar,
guint month,
guint year );
void gtk_calendar_select_day( GtkCalendar *calendar,
guint day );
</verb></tscreen>
The return value from <tt/gtk_calendar_select_month()/ is a boolean
value indicating whether the selection was successful.
With <tt/gtk_calendar_select_day()/ the specified day number is
selected within the current month, if that is possible. A
<tt/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 functions are provided to manipulate marked
days:
<tscreen><verb>
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);
</verb></tscreen>
The currently marked days are stored within an array within the
GtkCalendar 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 (don't forget in C that array
elements are numbered 0 to n-1). For example:
<tscreen><verb>
GtkCalendar *calendar;
calendar = gtk_calendar_new();
...
/* Is day 7 marked? */
if (calendar->marked_date[7-1])
/* day is marked */
</verb></tscreen>
Note that marks are persistent across month and year changes.
The final Calendar widget function is used to retrieve the currently
selected date, month and/or year.
<tscreen><verb>
void gtk_calendar_get_date( GtkCalendar *calendar,
guint *year,
guint *month,
guint *day );
</verb></tscreen>
This function requires you to pass the addresses of <tt/guint/
variables, into which the result will be placed. Passing <tt/NULL/ 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:
<itemize>
<item> <tt/month_changed/
<item> <tt/day_selected/
<item> <tt/day_selected_double_click/
<item> <tt/prev_month/
<item> <tt/next_month/
<item> <tt/prev_year/
<item> <tt/next_year/
</itemize>
That just leaves us with the need to put all of this together into
example code.
<tscreen><verb>
/* example-start calendar calendar.c */
/*
* Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson, Mattias Gr<47>nlund
* Copyright (C) 2000 Tony Gale
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 (&amp;tm, 0, sizeof (tm));
gtk_calendar_get_date (GTK_CALENDAR(data->window),
&amp;tm.tm_year, &amp;tm.tm_mon, &amp;tm.tm_mday);
tm.tm_year -= TM_YEAR_BASE;
time = mktime(&amp;tm);
strftime (buffer, buff_len-1, "%x", gmtime(&amp;time));
}
void calendar_set_signal_strings( char *sig_str,
CalendarData *data)
{
gchar *prev_sig;
gtk_label_get (GTK_LABEL (data->prev_sig), &amp;prev_sig);
gtk_label_set (GTK_LABEL (data->prev2_sig), prev_sig);
gtk_label_get (GTK_LABEL (data->last_sig), &amp;prev_sig);
gtk_label_set (GTK_LABEL (data->prev_sig), prev_sig);
gtk_label_set (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 (&amp;tm, 0, sizeof (tm));
gtk_calendar_get_date (GTK_CALENDAR(data->window),
&amp;tm.tm_year, &amp;tm.tm_mon, &amp;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;
GdkFont *font;
calendar->font = gtk_font_selection_dialog_get_font_name(
GTK_FONT_SELECTION_DIALOG (calendar->font_dialog));
if (calendar->window)
{
font = gtk_font_selection_dialog_get_font(GTK_FONT_SELECTION_DIALOG(calendar->font_dialog));
if (font)
{
style = gtk_style_copy (gtk_widget_get_style (calendar->window));
gdk_font_unref (style->font);
style->font = font;
gdk_font_ref (style->font);
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_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_widget_destroyed),
&amp;calendar->font_dialog);
gtk_signal_connect (GTK_OBJECT (GTK_FONT_SELECTION_DIALOG (window)->ok_button),
"clicked", GTK_SIGNAL_FUNC(calendar_font_selection_ok),
calendar);
gtk_signal_connect_object (GTK_OBJECT (GTK_FONT_SELECTION_DIALOG (window)->cancel_button),
"clicked",
GTK_SIGNAL_FUNC (gtk_widget_destroy),
GTK_OBJECT (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_border_width (GTK_CONTAINER (window), 5);
gtk_signal_connect(GTK_OBJECT(window), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit),
NULL);
gtk_signal_connect(GTK_OBJECT(window), "delete-event",
GTK_SIGNAL_FUNC(gtk_false),
NULL);
gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, TRUE);
vbox = gtk_vbox_new(FALSE, DEF_PAD);
gtk_container_add (GTK_CONTAINER (window), vbox);
/*
* The top part of the window, Calendar, flags and fontsel.
*/
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_button_box_set_spacing(GTK_BUTTON_BOX(hbbox), 5);
/* Calendar widget */
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(&amp;calendar_data);
gtk_calendar_mark_day ( GTK_CALENDAR(calendar), 19);
gtk_container_add( GTK_CONTAINER( frame), calendar);
gtk_signal_connect (GTK_OBJECT (calendar), "month_changed",
GTK_SIGNAL_FUNC (calendar_month_changed),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "day_selected",
GTK_SIGNAL_FUNC (calendar_day_selected),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "day_selected_double_click",
GTK_SIGNAL_FUNC (calendar_day_selected_double_click),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "prev_month",
GTK_SIGNAL_FUNC (calendar_prev_month),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "next_month",
GTK_SIGNAL_FUNC (calendar_next_month),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "prev_year",
GTK_SIGNAL_FUNC (calendar_prev_year),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "next_year",
GTK_SIGNAL_FUNC (calendar_next_year),
&amp;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);
/* Build the Right frame with the flags in */
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);
gtk_signal_connect (GTK_OBJECT (toggle),
"toggled",
GTK_SIGNAL_FUNC(calendar_toggle_flag),
&amp;calendar_data);
gtk_box_pack_start (GTK_BOX (vbox3), toggle, TRUE, TRUE, 0);
calendar_data.flag_checkboxes[i]=toggle;
}
/* Build the right font-button */
button = gtk_button_new_with_label("Font...");
gtk_signal_connect (GTK_OBJECT (button),
"clicked",
GTK_SIGNAL_FUNC(calendar_select_font),
&amp;calendar_data);
gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
/*
* Build the Signal-event part.
*/
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");
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (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_set_locale ();
gtk_init (&amp;argc, &amp;argv);
create_calendar();
gtk_main();
return(0);
}
/* example-end */
</verb></tscreen>
<!-- ----------------------------------------------------------------- -->
<sect1> Color Selection
<p>
@ -6505,6 +7095,7 @@ window.
<tscreen><verb>
/* example-start paned paned.c */
#include <stdio.h>
#include <gtk/gtk.h>
/* Create the list of "messages" */
@ -6751,6 +7342,7 @@ new to you.
<tscreen><verb>
/* example-start scrolledwin scrolledwin.c */
#include <stdio.h>
#include <gtk/gtk.h>
void destroy( GtkWidget *widget,
@ -7630,6 +8222,7 @@ backward manner, and exit the program.
<tscreen><verb>
/* example-start notebook notebook.c */
#include <stdio.h>
#include <gtk/gtk.h>
/* This function rotates the position of the tabs */
@ -9550,6 +10143,7 @@ That should about do it. Let's take a look at an example to help clarify.
<tscreen><verb>
/* example-start menu menu.c */
#include <stdio.h>
#include <gtk/gtk.h>
static gint button_press (GtkWidget *, GdkEvent *);
@ -10258,9 +10852,6 @@ When you do come to understand all the functions of a new undocumented
widget, please consider writing a tutorial on it so others may benefit
from your time.
<!-- ----------------------------------------------------------------- -->
<sect1> Calendar
<p>
<!-- ----------------------------------------------------------------- -->
<sect1> CTree
<p>
@ -16709,6 +17300,7 @@ gtk_dial_adjustment_value_changed (GtkAdjustment *adjustment,
<sect2> dial_test.c
<p>
<tscreen><verb>
#include <stdio.h>
#include <gtk/gtk.h>
#include "gtkdial.h"

View File

@ -11,7 +11,7 @@ Tony Gale <tt><htmlurl url="mailto:gale@gtk.org"
name="&lt;gale@gtk.org&gt;"></tt>,
Ian Main <tt><htmlurl url="mailto:imain@gtk.org"
name="&lt;imain@gtk.org&gt;"></tt>
<date>November 13th, 1999
<date>February 23rd, 2000
<abstract>
This is a tutorial on how to use GTK (the GIMP Toolkit) through its C
interface.
@ -33,7 +33,7 @@ open software, free software, or even commercial non-free software
using GTK without having to spend anything for licenses or royalties.
It's called the GIMP toolkit because it was originally written for
developing the General Image Manipulation Program (GIMP), but GTK has
developing the GNU Image Manipulation Program (GIMP), but GTK has
now been used in a large number of software projects, including the
GNU Network Object Model Environment (GNOME) project. GTK is built on
top of GDK (GIMP Drawing Kit) which is basically a wrapper around the
@ -839,15 +839,31 @@ So, by passing in the widget you wish to remove the handler from, and
the tag returned by one of the signal_connect functions, you can
disconnect a signal handler.
Another function to remove all the signal handers from an object is:
You can also temporarily disable signal handlers with the
gtk_signal_handler_block() and gtk_signal_handler_unblock() family of
functions.
<tscreen><verb>
void gtk_signal_handlers_destroy( GtkObject *object );
</verb></tscreen>
void gtk_signal_handler_block( GtkObject *object,
guint handler_id );
This call is fairly self explanatory. It simply removes all the
current signal handlers from the object passed in as the first
argument.
void gtk_signal_handler_block_by_func( GtkObject *object,
GtkSignalFunc func,
gpointer data );
void gtk_signal_handler_block_by_data( GtkObject *object,
gpointer data );
void gtk_signal_handler_unblock( GtkObject *object,
guint handler_id );
void gtk_signal_handler_unblock_by_func( GtkObject *object,
GtkSignalFunc func,
gpointer data );
void gtk_signal_handler_unblock_by_data( GtkObject *object,
gpointer data);
</verb></tscreen>
<!-- ----------------------------------------------------------------- -->
<sect1>An Upgraded Hello World
@ -4842,6 +4858,7 @@ The following code is an example of using an Entry widget.
<tscreen><verb>
/* example-start entry entry.c */
#include <stdio.h>
#include <gtk/gtk.h>
void enter_callback( GtkWidget *widget,
@ -5170,6 +5187,7 @@ It's example time again.
<tscreen><verb>
/* example-start spinbutton spinbutton.c */
#include <stdio.h>
#include <gtk/gtk.h>
static GtkWidget *spinner1;
@ -5565,6 +5583,578 @@ gtk_combo_set_item_string(GtkCombo *combo, GtkItem *item, const gchar
appropriate Item. Frankly, I have no idea how to do that.
-->
<!-- ----------------------------------------------------------------- -->
<sect1> Calendar
<p>
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 GtkCalendar widget is a simple as:
<tscreen><verb>
GtkWidget *gtk_calendar_new();
</verb></tscreen>
There might be times where you need to change a lot of information
within this widget and the following functions allow you to make
multiple change to a Calendar widget without the user seeing multiple
on-screen updates.
<tscreen><verb>
void gtk_calendar_freeze( GtkCalendar *Calendar );
void gtk_calendar_thaw ( GtkCalendar *Calendar );
</verb></tscreen>
They work just like the freeze/thaw functions 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 function
<tscreen><verb>
void gtk_calendar_display_options( GtkCalendar *calendar,
GtkCalendarDisplayOptions flags );
</verb></tscreen>
The <tt/flags/ argument can be formed by combining either of the
following five options using the logical bitwise OR (|) operation:
<itemize>
<item> GTK_CALENDAR_SHOW_HEADING - this option specifies that
the month and year should be shown when drawing the calendar.
<item> GTK_CALENDAR_SHOW_DAY_NAMES - this option specifies that the
three letter descriptions should be displayed for each day (eg
MON,TUE...).
<item> 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.
<item> 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).
<item> 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.
</itemize>
The following functions are used to set the the currently displayed
date:
<tscreen><verb>
gint gtk_calendar_select_month( GtkCalendar *calendar,
guint month,
guint year );
void gtk_calendar_select_day( GtkCalendar *calendar,
guint day );
</verb></tscreen>
The return value from <tt/gtk_calendar_select_month()/ is a boolean
value indicating whether the selection was successful.
With <tt/gtk_calendar_select_day()/ the specified day number is
selected within the current month, if that is possible. A
<tt/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 functions are provided to manipulate marked
days:
<tscreen><verb>
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);
</verb></tscreen>
The currently marked days are stored within an array within the
GtkCalendar 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 (don't forget in C that array
elements are numbered 0 to n-1). For example:
<tscreen><verb>
GtkCalendar *calendar;
calendar = gtk_calendar_new();
...
/* Is day 7 marked? */
if (calendar->marked_date[7-1])
/* day is marked */
</verb></tscreen>
Note that marks are persistent across month and year changes.
The final Calendar widget function is used to retrieve the currently
selected date, month and/or year.
<tscreen><verb>
void gtk_calendar_get_date( GtkCalendar *calendar,
guint *year,
guint *month,
guint *day );
</verb></tscreen>
This function requires you to pass the addresses of <tt/guint/
variables, into which the result will be placed. Passing <tt/NULL/ 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:
<itemize>
<item> <tt/month_changed/
<item> <tt/day_selected/
<item> <tt/day_selected_double_click/
<item> <tt/prev_month/
<item> <tt/next_month/
<item> <tt/prev_year/
<item> <tt/next_year/
</itemize>
That just leaves us with the need to put all of this together into
example code.
<tscreen><verb>
/* example-start calendar calendar.c */
/*
* Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson, Mattias Gr<47>nlund
* Copyright (C) 2000 Tony Gale
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 (&amp;tm, 0, sizeof (tm));
gtk_calendar_get_date (GTK_CALENDAR(data->window),
&amp;tm.tm_year, &amp;tm.tm_mon, &amp;tm.tm_mday);
tm.tm_year -= TM_YEAR_BASE;
time = mktime(&amp;tm);
strftime (buffer, buff_len-1, "%x", gmtime(&amp;time));
}
void calendar_set_signal_strings( char *sig_str,
CalendarData *data)
{
gchar *prev_sig;
gtk_label_get (GTK_LABEL (data->prev_sig), &amp;prev_sig);
gtk_label_set (GTK_LABEL (data->prev2_sig), prev_sig);
gtk_label_get (GTK_LABEL (data->last_sig), &amp;prev_sig);
gtk_label_set (GTK_LABEL (data->prev_sig), prev_sig);
gtk_label_set (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 (&amp;tm, 0, sizeof (tm));
gtk_calendar_get_date (GTK_CALENDAR(data->window),
&amp;tm.tm_year, &amp;tm.tm_mon, &amp;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;
GdkFont *font;
calendar->font = gtk_font_selection_dialog_get_font_name(
GTK_FONT_SELECTION_DIALOG (calendar->font_dialog));
if (calendar->window)
{
font = gtk_font_selection_dialog_get_font(GTK_FONT_SELECTION_DIALOG(calendar->font_dialog));
if (font)
{
style = gtk_style_copy (gtk_widget_get_style (calendar->window));
gdk_font_unref (style->font);
style->font = font;
gdk_font_ref (style->font);
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_position (GTK_WINDOW (window), GTK_WIN_POS_MOUSE);
gtk_signal_connect (GTK_OBJECT (window), "destroy",
GTK_SIGNAL_FUNC (gtk_widget_destroyed),
&amp;calendar->font_dialog);
gtk_signal_connect (GTK_OBJECT (GTK_FONT_SELECTION_DIALOG (window)->ok_button),
"clicked", GTK_SIGNAL_FUNC(calendar_font_selection_ok),
calendar);
gtk_signal_connect_object (GTK_OBJECT (GTK_FONT_SELECTION_DIALOG (window)->cancel_button),
"clicked",
GTK_SIGNAL_FUNC (gtk_widget_destroy),
GTK_OBJECT (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_border_width (GTK_CONTAINER (window), 5);
gtk_signal_connect(GTK_OBJECT(window), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit),
NULL);
gtk_signal_connect(GTK_OBJECT(window), "delete-event",
GTK_SIGNAL_FUNC(gtk_false),
NULL);
gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, TRUE);
vbox = gtk_vbox_new(FALSE, DEF_PAD);
gtk_container_add (GTK_CONTAINER (window), vbox);
/*
* The top part of the window, Calendar, flags and fontsel.
*/
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_button_box_set_spacing(GTK_BUTTON_BOX(hbbox), 5);
/* Calendar widget */
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(&amp;calendar_data);
gtk_calendar_mark_day ( GTK_CALENDAR(calendar), 19);
gtk_container_add( GTK_CONTAINER( frame), calendar);
gtk_signal_connect (GTK_OBJECT (calendar), "month_changed",
GTK_SIGNAL_FUNC (calendar_month_changed),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "day_selected",
GTK_SIGNAL_FUNC (calendar_day_selected),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "day_selected_double_click",
GTK_SIGNAL_FUNC (calendar_day_selected_double_click),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "prev_month",
GTK_SIGNAL_FUNC (calendar_prev_month),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "next_month",
GTK_SIGNAL_FUNC (calendar_next_month),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "prev_year",
GTK_SIGNAL_FUNC (calendar_prev_year),
&amp;calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "next_year",
GTK_SIGNAL_FUNC (calendar_next_year),
&amp;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);
/* Build the Right frame with the flags in */
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);
gtk_signal_connect (GTK_OBJECT (toggle),
"toggled",
GTK_SIGNAL_FUNC(calendar_toggle_flag),
&amp;calendar_data);
gtk_box_pack_start (GTK_BOX (vbox3), toggle, TRUE, TRUE, 0);
calendar_data.flag_checkboxes[i]=toggle;
}
/* Build the right font-button */
button = gtk_button_new_with_label("Font...");
gtk_signal_connect (GTK_OBJECT (button),
"clicked",
GTK_SIGNAL_FUNC(calendar_select_font),
&amp;calendar_data);
gtk_box_pack_start (GTK_BOX (vbox2), button, FALSE, FALSE, 0);
/*
* Build the Signal-event part.
*/
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");
gtk_signal_connect (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC (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_set_locale ();
gtk_init (&amp;argc, &amp;argv);
create_calendar();
gtk_main();
return(0);
}
/* example-end */
</verb></tscreen>
<!-- ----------------------------------------------------------------- -->
<sect1> Color Selection
<p>
@ -6505,6 +7095,7 @@ window.
<tscreen><verb>
/* example-start paned paned.c */
#include <stdio.h>
#include <gtk/gtk.h>
/* Create the list of "messages" */
@ -6751,6 +7342,7 @@ new to you.
<tscreen><verb>
/* example-start scrolledwin scrolledwin.c */
#include <stdio.h>
#include <gtk/gtk.h>
void destroy( GtkWidget *widget,
@ -7630,6 +8222,7 @@ backward manner, and exit the program.
<tscreen><verb>
/* example-start notebook notebook.c */
#include <stdio.h>
#include <gtk/gtk.h>
/* This function rotates the position of the tabs */
@ -9550,6 +10143,7 @@ That should about do it. Let's take a look at an example to help clarify.
<tscreen><verb>
/* example-start menu menu.c */
#include <stdio.h>
#include <gtk/gtk.h>
static gint button_press (GtkWidget *, GdkEvent *);
@ -10258,9 +10852,6 @@ When you do come to understand all the functions of a new undocumented
widget, please consider writing a tutorial on it so others may benefit
from your time.
<!-- ----------------------------------------------------------------- -->
<sect1> Calendar
<p>
<!-- ----------------------------------------------------------------- -->
<sect1> CTree
<p>
@ -16709,6 +17300,7 @@ gtk_dial_adjustment_value_changed (GtkAdjustment *adjustment,
<sect2> dial_test.c
<p>
<tscreen><verb>
#include <stdio.h>
#include <gtk/gtk.h>
#include "gtkdial.h"

View File

@ -1,8 +1,8 @@
CC = gcc
gcalendar: gcalendar.c
$(CC) `gtk-config --cflags` gcalendar.c -o gcalendar `gtk-config --libs`
calendar: calendar.c
$(CC) `gtk-config --cflags` calendar.c -o calendar `gtk-config --libs`
clean:
rm -f gcalendar
rm -f calendar

View File

@ -1,5 +1,7 @@
/* G Calendar
/* example-start calendar calendar.c */
/*
* Copyright (C) 1998 Cesar Miquel, Shawn T. Amundson, Mattias Grönlund
* Copyright (C) 2000 Tony Gale
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -26,84 +28,18 @@
#define TM_YEAR_BASE 1900
typedef struct _CalendarData {
GtkWidget *flag_checkboxes[5];
gboolean settings[5];
gchar *font;
GtkWidget *font_dialog;
GtkWidget *window;
GtkWidget *selected;
GtkWidget *selected_double_click;
GtkWidget *prev2_sig;
GtkWidget *prev_sig;
GtkWidget *last_sig;
GtkWidget *month;
} CalendarData;
void create_calendar();
gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data);
void destroy (GtkWidget *widget, gpointer data);
void day_selected_double_click (GtkWidget *widget, gpointer data);
int main(int argc, char *argv[]);
void calendar_month_changed (GtkWidget *widget, CalendarData *data);
void calendar_day_selected (GtkWidget *widget, CalendarData *data);
void calendar_day_selected_double_click (GtkWidget *widget, CalendarData *data);
void calendar_set_flags(CalendarData *calendar);
void calendar_toggle_flag(GtkWidget * toggle, CalendarData *calendar);
void calendar_font_selection_ok(GtkWidget * button, CalendarData *calendar);
void calendar_font_selection_destroy(GtkWidget * button, GtkWidget widget);
void calendar_select_font(GtkWidget * button, CalendarData *calendar);
void calendar_create_window_destroy(GtkWidget * ignore, CalendarData *calendar);
void calendar_create_window(GtkWidget * ignored, CalendarData * calendar);
void calendar_window_destroy(GtkWidget * ignore, CalendarData *calendar);
void create_calendar();
/*
* GtkCalendar
*/
void
calendar_month_changed (GtkWidget *widget, CalendarData *data)
{
char buffer[256];
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, 255, "%x", gmtime(&time));
gtk_label_set (GTK_LABEL (data->month), buffer);
}
void
calendar_day_selected (GtkWidget *widget, CalendarData *data)
{
char buffer[256];
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, 255, "%x", gmtime(&time));
gtk_label_set (GTK_LABEL (data->selected), buffer);
}
void
calendar_day_selected_double_click (GtkWidget *widget, CalendarData *data)
{
char buffer[256];
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, 255, "%x", gmtime(&time));
gtk_label_set (GTK_LABEL (data->selected_double_click), buffer);
}
enum {
calendar_show_header,
calendar_show_days,
@ -112,8 +48,115 @@ enum {
calendar_monday_first
};
void
calendar_set_flags(CalendarData *calendar)
/*
* 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)
{
gchar *prev_sig;
gtk_label_get (GTK_LABEL (data->prev_sig), &prev_sig);
gtk_label_set (GTK_LABEL (data->prev2_sig), prev_sig);
gtk_label_get (GTK_LABEL (data->last_sig), &prev_sig);
gtk_label_set (GTK_LABEL (data->prev_sig), prev_sig);
gtk_label_set (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;
@ -126,8 +169,8 @@ calendar_set_flags(CalendarData *calendar)
gtk_calendar_display_options (GTK_CALENDAR (calendar->window), options);
}
void
calendar_toggle_flag(GtkWidget * toggle, CalendarData *calendar)
void calendar_toggle_flag( GtkWidget *toggle,
CalendarData *calendar )
{
gint i;
gint j;
@ -140,13 +183,15 @@ calendar_toggle_flag(GtkWidget * toggle, CalendarData *calendar)
calendar_set_flags(calendar);
}
void
calendar_font_selection_ok(GtkWidget * button, CalendarData *calendar)
void calendar_font_selection_ok( GtkWidget *button,
CalendarData *calendar )
{
GtkStyle *style;
GdkFont *font;
calendar->font = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (calendar->font_dialog));
calendar->font = gtk_font_selection_dialog_get_font_name(
GTK_FONT_SELECTION_DIALOG (calendar->font_dialog));
if (calendar->window)
{
font = gtk_font_selection_dialog_get_font(GTK_FONT_SELECTION_DIALOG(calendar->font_dialog));
@ -161,8 +206,8 @@ calendar_font_selection_ok(GtkWidget * button, CalendarData *calendar)
}
}
void
calendar_select_font(GtkWidget * button, CalendarData *calendar)
void calendar_select_font( GtkWidget *button,
CalendarData *calendar )
{
GtkWidget *window;
@ -193,8 +238,7 @@ calendar_select_font(GtkWidget * button, CalendarData *calendar)
}
void
create_calendar()
void create_calendar()
{
GtkWidget *window;
GtkWidget *vbox, *vbox2, *vbox3;
@ -231,6 +275,7 @@ create_calendar()
}
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkCalendar Example");
gtk_container_border_width (GTK_CONTAINER (window), 5);
gtk_signal_connect(GTK_OBJECT(window), "destroy",
GTK_SIGNAL_FUNC(gtk_main_quit),
@ -257,7 +302,7 @@ create_calendar()
/* Calendar widget */
frame = gtk_frame_new("Calendar");
gtk_box_pack_start(GTK_BOX(hbbox), frame, TRUE, TRUE, DEF_PAD);
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);
@ -272,6 +317,18 @@ create_calendar()
gtk_signal_connect (GTK_OBJECT (calendar), "day_selected_double_click",
GTK_SIGNAL_FUNC (calendar_day_selected_double_click),
&calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "prev_month",
GTK_SIGNAL_FUNC (calendar_prev_month),
&calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "next_month",
GTK_SIGNAL_FUNC (calendar_next_month),
&calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "prev_year",
GTK_SIGNAL_FUNC (calendar_prev_year),
&calendar_data);
gtk_signal_connect (GTK_OBJECT (calendar), "next_year",
GTK_SIGNAL_FUNC (calendar_next_year),
&calendar_data);
separator = gtk_vseparator_new ();
@ -311,30 +368,31 @@ create_calendar()
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, 5);
hbox = gtk_hbox_new (FALSE, 3);
gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
label = gtk_label_new ("Day selected:");
label = gtk_label_new ("Signal:");
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
calendar_data.selected = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX (hbox), calendar_data.selected, 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, 5);
hbox = gtk_hbox_new (FALSE, 3);
gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
label = gtk_label_new ("Day selected double click:");
label = gtk_label_new ("Previous signal:");
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
calendar_data.selected_double_click = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX (hbox), calendar_data.selected_double_click, 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, 5);
hbox = gtk_hbox_new (FALSE, 3);
gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, TRUE, 0);
label = gtk_label_new ("Month change:");
label = gtk_label_new ("Second previous signal:");
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
calendar_data.month = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX (hbox), calendar_data.month, 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);
@ -351,12 +409,16 @@ create_calendar()
}
int
main(int argc, char *argv[])
int main(int argc,
char *argv[] )
{
gtk_set_locale ();
gtk_init (&argc, &argv);
create_calendar();
gtk_main ();
return 0;
gtk_main();
return(0);
}
/* example-end */