/**

This is mozop.c.  A little test app I've written to play around with GTK and the mozilla
XPCOM interfaces.  It allows you to make a web page your xwindows desktop pattern.

Usage: moztop <URL>

Version: 03.04.14 (Looks a lot like a date doesn't it?)
Author: kevinh@geeksville.com - Copyright 2003 S. Kevin Hester-Chow

*/

#include <stdio.h>
#include <gtk/gtk.h>
#include <gtk/gtkwindow.h>
#include <mozilla/gtkembedmoz/gtkmozembed.h>


static gboolean ignore_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
{
    /* If you return FALSE in the "delete_event" signal handler,
     * GTK will emit the "destroy" signal. Returning TRUE means
     * you don't want the window to be destroyed.
     * This is useful for popping up 'are you sure you want to quit?'
     * type dialogs. */

    g_print ("delete event occurred\n");

    /* Change TRUE to FALSE and the main window will be destroyed with
     * a "delete_event". */

    return TRUE;
}

/* Another callback */
static void destroy( GtkWidget *widget,
                     gpointer   data )
{
    gtk_main_quit ();
}

int main( int   argc,
          char *argv[] )
{
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GdkWindow *gdkwindow;
    GtkWidget *webwidget;
    const char *url;

    /* This is called in all GTK applications. Arguments are parsed
     * from the command line and are returned to the application. */
    gtk_init (&argc, &argv);

    // Parse arguments 
    // We currently accept just a URL
    if(argc < 2)
        {
        fprintf(stderr, "Error, usage: moztop <URL>\n");
	return 1;
	}

    url = argv[1];
    printf("Setting wallpaper to: %s", url);

    /* create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    // Turn off the border on our window
    gtk_window_set_decorated(GTK_WINDOW(window), FALSE);

    // Appear on all desktops
    gtk_window_stick(GTK_WINDOW(window));

    // Listen for raise - and say we don't support that
    // g_signal_connect (G_OBJECT (window), "button-release-event",
    // 	      G_CALLBACK (ignore_event), NULL);
    
    /* Here we connect the "destroy" event to a signal handler.  
     * This event occurs when we call gtk_widget_destroy() on the window,
     * or if we return FALSE in the "delete_event" callback. */
    g_signal_connect (G_OBJECT (window), "destroy",
		      G_CALLBACK (destroy), NULL);
    
    /* Sets the border width of the window. */
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    // Create our web browser component
    webwidget = gtk_moz_embed_new();

    /* This packs the button into the window (a gtk container). */
    gtk_container_add (GTK_CONTAINER (window), webwidget);

    // Turn off all features
    gtk_moz_embed_set_chrome_mask(GTK_MOZ_EMBED(webwidget), 0);

    // Show something
    gtk_moz_embed_load_url(GTK_MOZ_EMBED(webwidget), url);

    /* The final step is to display this newly created widget. */
    gtk_widget_show (webwidget);
    
    /* and the window */
    gtk_widget_show (window);
    
    // Make fullscreen
    gtk_window_maximize(GTK_WINDOW(window));

    // Don't let the user resize
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

    // Get our frame (yuck - but needed to lower)
    gdkwindow = window->window;
    gdk_window_lower(gdkwindow);

    // don't raise on click
    gdk_window_set_type_hint(gdkwindow, GDK_WINDOW_TYPE_HINT_DESKTOP);

    // don't show in pager or taskbar
    gdk_window_set_skip_taskbar_hint(gdkwindow, TRUE);
    gdk_window_set_skip_pager_hint(gdkwindow, TRUE);

    /* All GTK applications must have a gtk_main(). Control ends here
     * and waits for an event to occur (like a key press or
     * mouse event). */
    gtk_main ();
    
    return 0;
}

