From e674fe5d80aec82d353f485e994aa09f963de046 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 6 Feb 2008 10:12:54 +0000 Subject: [PATCH] version 0.1 git-svn-id: http://svn.nickg.me.uk/work/xcowsay@270 a97b1542-0b21-0410-a459-e47997c36f34 --- display_cow.c | 28 ++++++++++++++++++++-------- floating_shape.c | 5 ++++- settings.h | 2 -- xcowsay.c | 3 +++ 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/display_cow.c b/display_cow.c index 67e0af1..1b0e0dc 100644 --- a/display_cow.c +++ b/display_cow.c @@ -15,11 +15,13 @@ #define CORNER_RADIUS 30 // Radius of corners on the speech bubble #define CORNER_DIAM CORNER_RADIUS*2 #define BUBBLE_BORDER 5 // Pixels to leave free around edge of bubble -#define BUBBLE_XOFFSET 10 // Distance from cow to bubble +#define BUBBLE_XOFFSET 5 // Distance from cow to bubble #define MIN_TIP_HEIGHT 15 #define TICK_TIMEOUT 100 +#define max(a, b) ((a) > (b) ? (a) : (b)) + typedef enum { csLeadIn, csDisplay, csLeadOut, csCleanup } cowstate_t; @@ -92,8 +94,7 @@ static GdkPixbuf *create_bubble(char *text) bubble_width = 2*BUBBLE_BORDER + CORNER_DIAM + TIP_WIDTH + text_width; bubble_height = BUBBLE_BORDER + CORNER_DIAM + text_height; - bubble_pixmap = gdk_pixmap_new(shape_window(xcowsay.cow)->window, - bubble_width, bubble_height, -1); + bubble_pixmap = gdk_pixmap_new(NULL, bubble_width, bubble_height, 24); bubble_gc = gdk_gc_new(bubble_pixmap); colormap = gdk_colormap_get_system(); @@ -191,7 +192,7 @@ static GdkPixbuf *create_bubble(char *text) gdk_draw_line(bubble_pixmap, bubble_gc, BUBBLE_BORDER + TIP_WIDTH + CORNER_RADIUS, bubble_height, bubble_width - CORNER_RADIUS, bubble_height); - + xcowsay.bubble_width = bubble_width + BUBBLE_BORDER; xcowsay.bubble_height = bubble_height + BUBBLE_BORDER; @@ -268,19 +269,30 @@ void display_cow(const char *text) size_t len = strlen(text_copy); if ('\n' == text_copy[len-1]) text_copy[len-1] = '\0'; + + xcowsay.bubble_pixbuf = create_bubble(text_copy); + free(text_copy); g_assert(xcowsay.cow_pixbuf); xcowsay.cow = make_shape_from_pixbuf(xcowsay.cow_pixbuf); - move_shape(xcowsay.cow, 10, 30); + + int total_width = shape_width(xcowsay.cow) + BUBBLE_XOFFSET + + xcowsay.bubble_width; + int total_height = max(shape_height(xcowsay.cow), xcowsay.bubble_height); + + GdkScreen *screen = gdk_screen_get_default(); + int area_w = gdk_screen_get_width(screen) - total_width; + int area_h = gdk_screen_get_height(screen) - total_height; + + move_shape(xcowsay.cow, rand()%area_w, rand()%area_h); show_shape(xcowsay.cow); - xcowsay.bubble_pixbuf = create_bubble(text_copy); - xcowsay.bubble = make_shape_from_pixbuf(xcowsay.bubble_pixbuf); + xcowsay.bubble = make_shape_from_pixbuf(xcowsay.bubble_pixbuf); int bx = shape_x(xcowsay.cow) + shape_width(xcowsay.cow) + BUBBLE_XOFFSET; int by = shape_y(xcowsay.cow) + (shape_height(xcowsay.cow) - shape_height(xcowsay.bubble))/2; move_shape(xcowsay.bubble, bx, by); - + xcowsay.state = csLeadIn; xcowsay.transition_timeout = get_int_option("lead_in_time"); g_timeout_add(TICK_TIMEOUT, tick, NULL); diff --git a/floating_shape.c b/floating_shape.c index 7892a6a..386e7b9 100644 --- a/floating_shape.c +++ b/floating_shape.c @@ -73,7 +73,7 @@ float_shape_t *make_shape_from_pixbuf(GdkPixbuf *pixbuf) s->width = gdk_pixbuf_get_width(pixbuf); s->height = gdk_pixbuf_get_height(pixbuf); - s->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + s->window = gtk_window_new(GTK_WINDOW_POPUP); gtk_window_set_decorated(GTK_WINDOW(s->window), FALSE); gtk_window_set_title(GTK_WINDOW(s->window), "shape"); gtk_window_set_skip_taskbar_hint(GTK_WINDOW(s->window), TRUE); @@ -96,6 +96,8 @@ void show_shape(float_shape_t *shape) gtk_window_move(GTK_WINDOW(shape->window), shape->x, shape->y); gtk_window_resize(GTK_WINDOW(shape->window), shape->width, shape->height); gtk_widget_show_all(shape->window); + + gdk_window_set_back_pixmap(shape->window->window, NULL, TRUE); } void hide_shape(float_shape_t *shape) @@ -115,6 +117,7 @@ void destroy_shape(float_shape_t *shape) g_assert(shape); gtk_widget_destroy(shape->window); + g_object_unref(shape->mask_bitmap); free(shape); } diff --git a/settings.h b/settings.h index 83bb363..b63c9f5 100644 --- a/settings.h +++ b/settings.h @@ -4,8 +4,6 @@ #include #include -void settings_init(); - void add_int_option(const char *name, int ival); void add_bool_option(const char *name, bool bval); void add_string_option(const char *name, const char *sval); diff --git a/xcowsay.c b/xcowsay.c index 1aeceba..422404a 100644 --- a/xcowsay.c +++ b/xcowsay.c @@ -1,5 +1,6 @@ #include #include +#include #include "display_cow.h" #include "settings.h" @@ -31,6 +32,8 @@ int main(int argc, char **argv) add_int_option("lead_in_time", DEF_LEAD_IN_TIME); add_int_option("display_time", DEF_DISPLAY_TIME); add_int_option("lead_out_time", get_int_option("lead_in_time")); + + srand((unsigned)time(NULL)); gtk_init(&argc, &argv); -- 2.39.2