/* aflag_anim.c   displays a waving American flag*/
/* Copyright (C) 2001 Brad Abel */
/* This is the earth_anim code modified to display a flag instead */
#include <gkrellm/gkrellm.h>
#include "aflag_10.xpm"  /* edit this file to change image animation */
                         /* adjust the 4 defines below to match image. */ 
#define EARTH_PANEL_HEIGHT 48    /* height of panel image displays in */
#define FRAME_WIDTH 54
#define FRAME_HEIGHT 44
#define FRAMES_COUNT 10   /* no. of frames in image */
#define IMAGE_ARRAY_NAME aflag_10_xpm  /* points to array in xpm image  file  
*/
                                       /* use text editor to view xpm file and 
*/
				       /* see what the array name is */
#define TTSPEED 1     /* raise this value to slow the animation a '2' would */ 
                      /* make it twice as slow*/
#define PLUGIN_PLACEMENT  (MON_MAIL | GRAVITY(0))
#define STYLE_NAME "aflag"
#define PLUGIN_CONFIG_KEYWORD    "aflag"
#define EARTH_ANIM_VERSION "0.1"

static Panel   *panel;
static gint    style_id;
static GdkPixmap *earth_image = NULL;
static Decal     *earth = NULL; 
static GdkBitmap *earth_mask = NULL;
int start_stop_anim_value = 0;

static void
load_animation()
{
    GdkImlibImage  *image = NULL;

    gkrellm_load_image(NULL, IMAGE_ARRAY_NAME, &image, NULL);
    gkrellm_render_to_pixmap(image, &earth_image, &earth_mask, 0, 0);
}

static gint
panel_expose_event(GtkWidget *widget, GdkEventExpose *ev)
{
    gdk_draw_pixmap(widget->window,
		    widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		    panel->pixmap, ev->area.x, ev->area.y, ev->area.x, ev->area.y,
		    ev->area.width, ev->area.height);
    return FALSE;
}

static void
earth_update_plugin()
{
    static int frame_number;
    if (start_stop_anim_value == 1) return;

if ((GK.timer_ticks % TTSPEED) == 0)
    {
    ++frame_number;
    frame_number %= FRAMES_COUNT;
    gkrellm_draw_decal_pixmap(panel, earth, frame_number);
    gkrellm_draw_layers(panel);
    }
}

static void
earth_create_tab(GtkWidget *tab_vbox)
{
    GtkWidget		*tabs;
    
    tabs = gtk_notebook_new();
    gtk_notebook_set_tab_pos(GTK_NOTEBOOK(tabs), GTK_POS_TOP);
    gtk_box_pack_start(GTK_BOX(tab_vbox), tabs, TRUE, TRUE, 0);
	
	{
	    gchar *plugin_about_text;
	    GtkWidget *label, *text;

	    plugin_about_text = g_strdup_printf(
		"American Flag Animation %s\n"
		"GKrellM Aflag Plugin\n\n"
		"Copyright (C) 2001 Brad Abel\n"
		"envy@cybergoth.zzn.com\n\n"
		"Released under the GNU Public Licence",
		EARTH_ANIM_VERSION);
	    
	    text = gtk_label_new(plugin_about_text); 
	    label = gtk_label_new("About");
	    gtk_notebook_append_page(GTK_NOTEBOOK(tabs),text,label);
	    g_free(plugin_about_text);
	}
}    

static gint
start_stop_anim (GtkWidget *widget, GdkEventButton *ev)
{
	switch (ev->button)
	{
	case 1:
		if ( start_stop_anim_value == 1 )
			start_stop_anim_value = 0;
		else start_stop_anim_value = 1;
		break;
	case 3:
		if (start_stop_anim_value == 1)
			start_stop_anim_value = 0;
		else start_stop_anim_value = 1;
		break;
	}
	return FALSE;
}

static void
earth_create_plugin(GtkWidget *vbox, gint first_create)
{
   Style	   *style = NULL;
    int            frame_x_offset;
    int            frame_y_offset;

    load_animation();

    if (first_create) panel = gkrellm_panel_new0();
    else {gkrellm_destroy_decal_list(panel);}

    style = gkrellm_meter_style(style_id);

    frame_x_offset = (gkrellm_chart_width() - FRAME_WIDTH) / 2;
    frame_y_offset = (EARTH_PANEL_HEIGHT - FRAME_HEIGHT) / 2;

    earth = gkrellm_create_decal_pixmap(panel, earth_image, earth_mask,
				       FRAMES_COUNT,
				       style, frame_x_offset, frame_y_offset);

    panel->textstyle = gkrellm_meter_textstyle(style_id);
    panel->label->h_panel = EARTH_PANEL_HEIGHT;      
    gkrellm_create_panel(vbox, panel, gkrellm_bg_meter_image(style_id));
    gkrellm_monitor_height_adjust(panel->h);

    if (first_create) {
        gtk_signal_connect(GTK_OBJECT(panel->drawing_area),
			   "expose_event", (GtkSignalFunc) panel_expose_event,
			   NULL);
        gtk_signal_connect (GTK_OBJECT (panel->drawing_area),
		      "button_press_event", (GtkSignalFunc) start_stop_anim , NULL);
	              }
    gkrellm_draw_decal_pixmap(panel, earth, 0);
    gkrellm_draw_layers(panel);
}

static Monitor  plugin_mon  =
        {
        "Aflag Animation",         /* Name, for config tab.  */
        0,              /* Id,  0 if a plugin           */
        earth_create_plugin,  /* The create_plugin() function */
        earth_update_plugin,  /* The update_plugin() function */
        earth_create_tab,     /* The create_plugin_tab() config function */
        NULL,           /* The apply_plugin_config() function  */

        NULL,           /* The save_plugin_config() function  */
        NULL,           /* The load_plugin_config() function  */
        PLUGIN_CONFIG_KEYWORD,     /* config keyword  */

        NULL,           /* Undefined 2  */
        NULL,           /* Undefined 1  */
        NULL,           /* Undefined 0  */

        MON_INSERT_AFTER|MON_CLOCK, /* Insert plugin before this monitor.*/
        NULL,           /* Handle if a plugin, filled in by GKrellM */
        NULL            /* path if a plugin, filled in by GKrellM   */
        };

Monitor *
init_plugin(void)
        {
        style_id = gkrellm_add_meter_style(&plugin_mon, STYLE_NAME);
	return &plugin_mon;
        }

