mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2025-01-14 14:20:21 +00:00
gtk-demo: Cosmetic improvements to clocks demo
Reformat the code comments, and add a 10th clock.
This commit is contained in:
parent
087f42d998
commit
11f971d870
@ -41,7 +41,8 @@ enum {
|
|||||||
|
|
||||||
/* This function returns the current time in the clock's timezone.
|
/* This function returns the current time in the clock's timezone.
|
||||||
* Note that this returns a new object every time, so we need to
|
* Note that this returns a new object every time, so we need to
|
||||||
* remember to unref it after use. */
|
* remember to unref it after use.
|
||||||
|
*/
|
||||||
static GDateTime *
|
static GDateTime *
|
||||||
gtk_clock_get_time (GtkClock *clock)
|
gtk_clock_get_time (GtkClock *clock)
|
||||||
{
|
{
|
||||||
@ -51,10 +52,11 @@ gtk_clock_get_time (GtkClock *clock)
|
|||||||
return g_date_time_new_now_local ();
|
return g_date_time_new_now_local ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Here, we implement the functionality required by the GdkPaintable interface.
|
/* Here, we implement the functionality required by the GdkPaintable
|
||||||
* This way we have a trivial way to display an analog clock.
|
* interface. This way we have a trivial way to display an analog clock.
|
||||||
* It also allows demonstrating how to directly use objects in the listview
|
* It also allows demonstrating how to directly use objects in the
|
||||||
* later by making this object do something interesting. */
|
* listview later by making this object do something interesting.
|
||||||
|
*/
|
||||||
static void
|
static void
|
||||||
gtk_clock_snapshot (GdkPaintable *paintable,
|
gtk_clock_snapshot (GdkPaintable *paintable,
|
||||||
GdkSnapshot *snapshot,
|
GdkSnapshot *snapshot,
|
||||||
@ -68,26 +70,31 @@ gtk_clock_snapshot (GdkPaintable *paintable,
|
|||||||
#define BLACK ((GdkRGBA) { 0, 0, 0, 1 })
|
#define BLACK ((GdkRGBA) { 0, 0, 0, 1 })
|
||||||
|
|
||||||
/* save/restore() is necessary so we can undo the transforms we start
|
/* save/restore() is necessary so we can undo the transforms we start
|
||||||
* out with. */
|
* out with.
|
||||||
|
*/
|
||||||
gtk_snapshot_save (snapshot);
|
gtk_snapshot_save (snapshot);
|
||||||
|
|
||||||
/* First, we move the (0, 0) point to the center of the area so
|
/* First, we move the (0, 0) point to the center of the area so
|
||||||
* we can draw everything relative to it. */
|
* we can draw everything relative to it.
|
||||||
|
*/
|
||||||
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (width / 2, height / 2));
|
gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (width / 2, height / 2));
|
||||||
|
|
||||||
/* Next we scale it, so that we can pretend that the clock is
|
/* Next we scale it, so that we can pretend that the clock is
|
||||||
* 100px in size. That way, we don't need to do any complicated
|
* 100px in size. That way, we don't need to do any complicated
|
||||||
* math later.
|
* math later. We use MIN() here so that we use the smaller
|
||||||
* We use MIN() here so that we use the smaller dimension for sizing.
|
* dimension for sizing. That way we don't overdraw but keep
|
||||||
* That way we don't overdraw but keep the aspect ratio. */
|
* the aspect ratio.
|
||||||
|
*/
|
||||||
gtk_snapshot_scale (snapshot, MIN (width, height) / 100.0, MIN (width, height) / 100.0);
|
gtk_snapshot_scale (snapshot, MIN (width, height) / 100.0, MIN (width, height) / 100.0);
|
||||||
/* Now we have a circle with diameter 100px (and radius 50px) that
|
|
||||||
* has its (0, 0) point at the center.
|
|
||||||
* Let's draw a simple clock into it. */
|
|
||||||
|
|
||||||
|
/* Now we have a circle with diameter 100px (and radius 50px) that
|
||||||
|
* has its (0, 0) point at the center. Let's draw a simple clock into it.
|
||||||
|
*/
|
||||||
time = gtk_clock_get_time (self);
|
time = gtk_clock_get_time (self);
|
||||||
|
|
||||||
/* First, draw a circle. This is a neat little trick to draw a circle
|
/* First, draw a circle. This is a neat little trick to draw a circle
|
||||||
* without requiring Cairo. */
|
* without requiring Cairo.
|
||||||
|
*/
|
||||||
gsk_rounded_rect_init_from_rect (&outline, &GRAPHENE_RECT_INIT(-50, -50, 100, 100), 50);
|
gsk_rounded_rect_init_from_rect (&outline, &GRAPHENE_RECT_INIT(-50, -50, 100, 100), 50);
|
||||||
gtk_snapshot_append_border (snapshot,
|
gtk_snapshot_append_border (snapshot,
|
||||||
&outline,
|
&outline,
|
||||||
@ -95,9 +102,11 @@ gtk_clock_snapshot (GdkPaintable *paintable,
|
|||||||
(GdkRGBA [4]) { BLACK, BLACK, BLACK, BLACK });
|
(GdkRGBA [4]) { BLACK, BLACK, BLACK, BLACK });
|
||||||
|
|
||||||
/* Next, draw the hour hand.
|
/* Next, draw the hour hand.
|
||||||
* We do this using tranforms again: Instead of computing where the angle points
|
* We do this using tranforms again: Instead of computing where the angle
|
||||||
* to, we just rotate everything and then draw the hand as if if was :00.
|
* points to, we just rotate everything and then draw the hand as if it
|
||||||
* We don't even need to care about am/pm here because rotations just work. */
|
* was :00. We don't even need to care about am/pm here because rotations
|
||||||
|
* just work.
|
||||||
|
*/
|
||||||
gtk_snapshot_save (snapshot);
|
gtk_snapshot_save (snapshot);
|
||||||
gtk_snapshot_rotate (snapshot, 30 * g_date_time_get_hour (time) + 0.5 * g_date_time_get_minute (time));
|
gtk_snapshot_rotate (snapshot, 30 * g_date_time_get_hour (time) + 0.5 * g_date_time_get_minute (time));
|
||||||
gsk_rounded_rect_init_from_rect (&outline, &GRAPHENE_RECT_INIT(-2, -23, 4, 25), 2);
|
gsk_rounded_rect_init_from_rect (&outline, &GRAPHENE_RECT_INIT(-2, -23, 4, 25), 2);
|
||||||
@ -107,7 +116,8 @@ gtk_clock_snapshot (GdkPaintable *paintable,
|
|||||||
gtk_snapshot_restore (snapshot);
|
gtk_snapshot_restore (snapshot);
|
||||||
|
|
||||||
/* And the same as above for the minute hand. Just make this one longer
|
/* And the same as above for the minute hand. Just make this one longer
|
||||||
* so people can tell the hands apart. */
|
* so people can tell the hands apart.
|
||||||
|
*/
|
||||||
gtk_snapshot_save (snapshot);
|
gtk_snapshot_save (snapshot);
|
||||||
gtk_snapshot_rotate (snapshot, 6 * g_date_time_get_minute (time));
|
gtk_snapshot_rotate (snapshot, 6 * g_date_time_get_minute (time));
|
||||||
gsk_rounded_rect_init_from_rect (&outline, &GRAPHENE_RECT_INIT(-2, -43, 4, 45), 2);
|
gsk_rounded_rect_init_from_rect (&outline, &GRAPHENE_RECT_INIT(-2, -43, 4, 45), 2);
|
||||||
@ -125,8 +135,9 @@ gtk_clock_snapshot (GdkPaintable *paintable,
|
|||||||
gtk_snapshot_pop (snapshot);
|
gtk_snapshot_pop (snapshot);
|
||||||
gtk_snapshot_restore (snapshot);
|
gtk_snapshot_restore (snapshot);
|
||||||
|
|
||||||
/* And finally, don't forget to restore the initial save() that we did for
|
/* And finally, don't forget to restore the initial save() that
|
||||||
* the initial transformations. */
|
* we did for the initial transformations.
|
||||||
|
*/
|
||||||
gtk_snapshot_restore (snapshot);
|
gtk_snapshot_restore (snapshot);
|
||||||
|
|
||||||
g_date_time_unref (time);
|
g_date_time_unref (time);
|
||||||
@ -145,10 +156,9 @@ gtk_clock_get_intrinsic_height (GdkPaintable *paintable)
|
|||||||
return 100;
|
return 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the paintable interface. This way we turn our clock objects
|
/* Initialize the paintable interface. This way we turn our clocks
|
||||||
* into objects that can be drawn.
|
* into objects that can be drawn. There are more functions to this
|
||||||
* There are more functions to this interface to define desired size,
|
* interface to define desired size, but this is enough.
|
||||||
* but this is enough.
|
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
gtk_clock_paintable_init (GdkPaintableInterface *iface)
|
gtk_clock_paintable_init (GdkPaintableInterface *iface)
|
||||||
@ -158,8 +168,9 @@ gtk_clock_paintable_init (GdkPaintableInterface *iface)
|
|||||||
iface->get_intrinsic_height = gtk_clock_get_intrinsic_height;
|
iface->get_intrinsic_height = gtk_clock_get_intrinsic_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Finally, we define the type. The important part is adding the paintable
|
/* Finally, we define the type. The important part is adding the
|
||||||
* interface, so GTK knows that this object can indeed be drawm.
|
* paintable interface, so GTK knows that this object can indeed
|
||||||
|
* be drawn.
|
||||||
*/
|
*/
|
||||||
G_DEFINE_TYPE_WITH_CODE (GtkClock, gtk_clock, G_TYPE_OBJECT,
|
G_DEFINE_TYPE_WITH_CODE (GtkClock, gtk_clock, G_TYPE_OBJECT,
|
||||||
G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
|
G_IMPLEMENT_INTERFACE (GDK_TYPE_PAINTABLE,
|
||||||
@ -221,11 +232,14 @@ gtk_clock_set_property (GObject *object,
|
|||||||
|
|
||||||
/* This is the list of all the ticking clocks */
|
/* This is the list of all the ticking clocks */
|
||||||
static GSList *ticking_clocks = NULL;
|
static GSList *ticking_clocks = NULL;
|
||||||
/* This is the id of the timeout source that is updating all ticking clocks */
|
|
||||||
|
/* This is the ID of the timeout source that is updating all
|
||||||
|
* ticking clocks.
|
||||||
|
*/
|
||||||
static guint ticking_clock_id = 0;
|
static guint ticking_clock_id = 0;
|
||||||
|
|
||||||
/* Every second, this function is called to tell everybody that the
|
/* Every second, this function is called to tell everybody that
|
||||||
* clocks are ticking.
|
* the clocks are ticking.
|
||||||
*/
|
*/
|
||||||
static gboolean
|
static gboolean
|
||||||
gtk_clock_tick (gpointer unused)
|
gtk_clock_tick (gpointer unused)
|
||||||
@ -240,6 +254,7 @@ gtk_clock_tick (gpointer unused)
|
|||||||
* so notify about that.
|
* so notify about that.
|
||||||
*/
|
*/
|
||||||
g_object_notify_by_pspec (G_OBJECT (clock), properties[PROP_TIME]);
|
g_object_notify_by_pspec (G_OBJECT (clock), properties[PROP_TIME]);
|
||||||
|
|
||||||
/* We will also draw the hands of the clock differently.
|
/* We will also draw the hands of the clock differently.
|
||||||
* So notify about that, too.
|
* So notify about that, too.
|
||||||
*/
|
*/
|
||||||
@ -343,6 +358,9 @@ create_clocks_model (void)
|
|||||||
clock = gtk_clock_new ("San Francisco", g_time_zone_new ("America/Los_Angeles"));
|
clock = gtk_clock_new ("San Francisco", g_time_zone_new ("America/Los_Angeles"));
|
||||||
g_list_store_append (result, clock);
|
g_list_store_append (result, clock);
|
||||||
g_object_unref (clock);
|
g_object_unref (clock);
|
||||||
|
clock = gtk_clock_new ("Xalapa", g_time_zone_new ("America/Mexico_City"));
|
||||||
|
g_list_store_append (result, clock);
|
||||||
|
g_object_unref (clock);
|
||||||
clock = gtk_clock_new ("Boston", g_time_zone_new ("America/New_York"));
|
clock = gtk_clock_new ("Boston", g_time_zone_new ("America/New_York"));
|
||||||
g_list_store_append (result, clock);
|
g_list_store_append (result, clock);
|
||||||
g_object_unref (clock);
|
g_object_unref (clock);
|
||||||
|
Loading…
Reference in New Issue
Block a user