Re: [gtk-list] Re: WM icons




I just figured out how to use icons (even the funky animated ones)
from gtk and produced a somewhat silly demo that might help you.
Here it is:

/* Mixed from testgtk examples.  

This example shows how to create "active icons", ie icons 
whose content is drawn by the application. This could be used
to eg show some kind of progress bar during a long computation.
I have often wanted to have such progress indicators on Netscapes
download icons. */

#include <gtk/gtk.h>


static char * WheelbarrowFull_xpm[] = {
"48 48 64 1",
"       c None",
".      c #DF7DCF3CC71B",
"X      c #965875D669A6",
"o      c #71C671C671C6",
"O      c #A699A289A699",
"+      c #965892489658",
"@      c #8E38410330C2",
"#      c #D75C7DF769A6",
"$      c #F7DECF3CC71B",
"%      c #96588A288E38",
"&      c #A69992489E79",
"*      c #8E3886178E38",
"=      c #104008200820",
"-      c #596510401040",
";      c #C71B30C230C2",
":      c #C71B9A699658",
">      c #618561856185",
",      c #20811C712081",
"<      c #104000000000",
"1      c #861720812081",
"2      c #DF7D4D344103",
"3      c #79E769A671C6",
"4      c #861782078617",
"5      c #41033CF34103",
"6      c #000000000000",
"7      c #49241C711040",
"8      c #492445144924",
"9      c #082008200820",
"0      c #69A618611861",
"q      c #B6DA71C65144",
"w      c #410330C238E3",
"e      c #CF3CBAEAB6DA",
"r      c #71C6451430C2",
"t      c #EFBEDB6CD75C",
"y      c #28A208200820",
"u      c #186110401040",
"i      c #596528A21861",
"p      c #71C661855965",
"a      c #A69996589658",
"s      c #30C228A230C2",
"d      c #BEFBA289AEBA",
"f      c #596545145144",
"g      c #30C230C230C2",
"h      c #8E3882078617",
"j      c #208118612081",
"k      c #38E30C300820",
"l      c #30C2208128A2",
"z      c #38E328A238E3",
"x      c #514438E34924",
"c      c #618555555965",
"v      c #30C2208130C2",
"b      c #38E328A230C2",
"n      c #28A228A228A2",
"m      c #41032CB228A2",
"M      c #104010401040",
"N      c #492438E34103",
"B      c #28A2208128A2",
"V      c #A699596538E3",
"C      c #30C21C711040",
"Z      c #30C218611040",
"A      c #965865955965",
"S      c #618534D32081",
"D      c #38E31C711040",
"F      c #082000000820",
"                                                ",
"          .XoO                                  ",
"         +@#$%o&                                ",
"         *=-;#::o+                              ",
"           >,<12#:34                            ",
"             45671#:X3                          ",
"               +89<02qwo                        ",
"e*                >,67;ro                       ",
"ty>                 459@>+&&                    ",
"$2u+                  ><ipas8*                  ",
"%$;=*                *3:.Xa.dfg>                ",
"Oh$;ya             *3d.a8j,Xe.d3g8+             ",
" Oh$;ka          *3d$a8lz,,xxc:.e3g54           ",
"  Oh$;kO       *pd$%svbzz,sxxxxfX..&wn>         ",
"   Oh$@mO    *3dthwlsslszjzxxxxxxx3:td8M4       ",
"    Oh$@g& *3d$XNlvvvlllm,mNwxxxxxxxfa.:,B*     ",
"     Oh$@,Od.czlllllzlmmqV@V#V@fxxxxxxxf:%j5&   ",
"      Oh$1hd5lllslllCCZrV#r#:#2AxxxxxxxxxcdwM*  ",
"       OXq6c.%8vvvllZZiqqApA:mq:Xxcpcxxxxxfdc9* ",
"        2r<6gde3bllZZrVi7S@SV77A::qApxxxxxxfdcM ",
":,q-6MN.dfmZZrrSS:#riirDSAX@Af5xxxxxfevo",
"         +A26jguXtAZZZC7iDiCCrVVii7Cmmmxxxxxx%3g",
"          *#16jszN..3DZZZZrCVSA2rZrV7Dmmwxxxx&en",
"           p2yFvzssXe:fCZZCiiD7iiZDiDSSZwwxx8e*>",
"           OA1<jzxwwc:$d%NDZZZZCCCZCCZZCmxxfd.B ",
"            3206Bwxxszx%et.eaAp77m77mmmf3&eeeg* ",
"             @26MvzxNzvlbwfpdettttttttttt.c,n&  ",
"             *;16=lsNwwNwgsvslbwwvccc3pcfu<o    ",
"              p;<69BvwwsszslllbBlllllllu<5+     ",
"              OS0y6FBlvvvzvzss,u=Blllj=54       ",
"               c1-699Blvlllllu7k96MMMg4         ",
"               *10y8n6FjvllllB<166668           ",
"                S-kg+>666<M<996-y6n<8*          ",
"                p71=4 m69996kD8Z-66698&&        ",
"                &i0ycm6n4 ogk17,0<6666g         ",
"                 N-k-<>     >=01-kuu666>        ",
"                 ,6ky&      &46-10ul,66,        ",
"                 Ou0<>       o66y<ulw<66&       ",
"                  *kk5       >66By7=xu664       ",
"                   <<M4      466lj<Mxu66o       ",
"                   *>>       +66uv,zN666*       ",
"                              566,xxj669        ",
"                              4666FF666>        ",
"                               >966666M         ",
"                                oM6668+         ",
"                                  *4            ",
"                                                ",
"                                                "};

void hello (GtkWidget *widget, gpointer data)
{
    g_print ("Hello World\n");
}
     
gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
    g_print ("delete event occured\n");
    return (TRUE);
}

void destroy (GtkWidget *widget, gpointer data)
{
    gtk_main_quit ();
}

static int color_idle = 0;

gint
color_idle_func (GtkWidget *preview)
{
  static int count = 1;
  guchar buf[768];
  int i, j, k;

  for (i = 0; i < 256; i++)
    {
      for (j = 0, k = 0; j < 256; j++)
	{
	  buf[k+0] = i + count;
	  buf[k+1] = 0;
	  buf[k+2] = j + count;
	  k += 3;
	}

      gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, i, 256);
    }

  count += 1;

  gtk_widget_draw (preview, NULL);

  return TRUE;
}

static void
color_preview_destroy (GtkWidget  *widget,
		       GtkWidget **window)
{
  gtk_idle_remove (color_idle);
  color_idle = 0;

  *window = NULL;
}

GtkWidget *create_color_preview (void)
{
  GtkWidget *window;
  GtkWidget *preview;
  guchar buf[768];
  int i, j, k;

      gtk_widget_push_visual (gdk_rgb_get_visual ());
      gtk_widget_push_colormap (gdk_rgb_get_cmap ());
      window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_widget_pop_colormap ();
      gtk_widget_pop_visual ();

      gtk_signal_connect (GTK_OBJECT (window), "destroy",
			  GTK_SIGNAL_FUNC(color_preview_destroy),
			  &window);

      gtk_window_set_title (GTK_WINDOW (window), "test");
      gtk_container_border_width (GTK_CONTAINER (window), 10);

      preview = gtk_preview_new (GTK_PREVIEW_COLOR);
      gtk_preview_size (GTK_PREVIEW (preview), 256, 256);
      gtk_container_add (GTK_CONTAINER (window), preview);

      gtk_widget_show (preview);
      for (i = 0; i < 256; i++)
	{
	  for (j = 0, k = 0; j < 256; j++)
	    {
	      buf[k+0] = i;
	      buf[k+1] = 0;
	      buf[k+2] = j;
	      k += 3;
	    }

	  gtk_preview_draw_row (GTK_PREVIEW (preview), buf, 0, i, 256);
	}

      color_idle = gtk_idle_add ((GtkFunction) color_idle_func, preview);
      return window;
}

GdkWindow *icon_window;
GdkPixmap *icon_pixmap;
GdkBitmap *icon_mask;

void 
set_icon_window (GtkWidget *window)
{
  gdk_window_set_icon (window->window, icon_window, NULL, NULL);
}    


void 
set_pixmap_icon (GtkWidget *window)
{
  gdk_window_set_icon (window->window, NULL, icon_pixmap, icon_mask);
}    

void 
set_no_icon (GtkWidget *window)
{
  /* is this a bug of XSetWMHints ? having flags=0 seems to do nothing */
  gdk_window_set_icon (window->window, NULL, NULL, icon_mask);
}    

int main (int argc, char *argv[])
{
    GtkWidget *window, *icon, *button1, *button2, *button3, *vbox;
    GtkStyle *style;
    
    gtk_init (&argc, &argv);

    /* This is not a very useful icon, but it was something
       readily available and shows some change in the icon. */
    icon = create_color_preview();
    /* have to realize it first, to ensure that icon->window 
       exists. Never gtk_widget_show the icon though; the ICCCM 
       states that clients should never map or unmap icon windows.
    */
    gtk_widget_realize(icon);
    /* fvwm2 makes no attempt to resize the icon window to 
       its needs (although the ICCCM states that wms are free
       to do so). Thus the call to gtk_window_move_resize
       is necessary, otherwise we end up with a 1x1 window
       (having a program set size of 256x256) */
    gdk_window_set_hints(icon->window, 0,0,256,256,256,256,
			 GDK_HINT_MIN_SIZE|GDK_HINT_MAX_SIZE);
    gdk_window_move_resize(icon->window, 0,0,256,256);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_realize (window);

    /* this is how we attach the icon to the window. Again
       realize window first to create window->window. You 
       probably want to include an icon pixmap and mask in
       the last two arguments as well, in case the window manager
       doesn't deal with icon windows (ICCCM: Most, but not
       all wm's will support icon windows. )
    */
    icon_window = icon->window;
    
    style = gtk_widget_get_default_style();
    icon_pixmap = gdk_pixmap_create_from_xpm_d(window->window,
					       &icon_mask, 
					       &style->bg[GTK_STATE_NORMAL],
					       WheelbarrowFull_xpm);
    
    set_pixmap_icon(window);

    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
                        GTK_SIGNAL_FUNC (delete_event), NULL);

    gtk_signal_connect (GTK_OBJECT (window), "destroy",
                        GTK_SIGNAL_FUNC (destroy), NULL);

    gtk_container_border_width (GTK_CONTAINER (window), 10);
    button1 = gtk_radio_button_new_with_label (NULL, "pixmap icon");
    button2 = gtk_radio_button_new_with_label (
	gtk_radio_button_group (GTK_RADIO_BUTTON(button1)), 
	"icon window");
    button3 = gtk_radio_button_new_with_label (
	gtk_radio_button_group (GTK_RADIO_BUTTON(button2)), 
	"no icon");
    gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON(button1), TRUE);

    vbox = gtk_vbox_new (FALSE, 0);
    gtk_box_pack_start ( GTK_BOX(vbox), button1, TRUE, TRUE, 0);
    gtk_box_pack_start ( GTK_BOX(vbox), button2, TRUE, TRUE, 0);
    gtk_box_pack_start ( GTK_BOX(vbox), button3, TRUE, TRUE, 0);

    gtk_signal_connect_object 
      (GTK_OBJECT(button1),
       "clicked", GTK_SIGNAL_FUNC(set_pixmap_icon), GTK_OBJECT(window));

    gtk_signal_connect_object
      (GTK_OBJECT(button2),
       "clicked", GTK_SIGNAL_FUNC(set_icon_window), GTK_OBJECT(window));

    gtk_signal_connect_object
      (GTK_OBJECT(button3),
       "clicked", GTK_SIGNAL_FUNC(set_no_icon), GTK_OBJECT(window));

    gtk_container_add (GTK_CONTAINER (window), vbox);

    gtk_widget_show_all (window);

    gtk_main ();
                        
    return 0;
}



-- 
Matthias Clasen, 
Tel. 0761/203-5606
Email: clasen@mathematik.uni-freiburg.de
Mathematisches Institut, Albert-Ludwigs-Universitaet Freiburg



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]