Re: Showing a progess in clist column



Hi ALL,

Sven Herzberg wrote:
 
does anyone know how to implement a progressbar-like thingy inserted in
a clist (this is for a filesharing client)?

Please tell me if you know (even in case you don't)

Below an excerpt of code that do that for two columns (a part of this is
originally in Harlow's book, chapter 14, but I've made a few changes).
You must still create the clist somewhere else. These are just the
functions that populate it correctly. The first column has a pixtext
(i.e., icon+text), the second the progress bar (whose color changes from
red to yellow & to green, according to the size of the bar, in steps of
33%). Enjoy! :-)

GdkPixmap *bar_pixmap[5];
GdkBitmap *bar_mask[5];


/* Function to populate the clist */

void
populate_clist                         (GtkCList *clist)
{
  GtkWidget *cpu_pixmap, *ram_pixmap, *hd_pixmap, *nic_pixmap,
*isdn_pixmap;
  GdkPixmap *cpu_gdk_pixmap, *ram_gdk_pixmap, *hd_gdk_pixmap;
  GdkPixmap *nic_gdk_pixmap, *isdn_gdk_pixmap;
  enum bar_pixmaps {CPU_BAR, RAM_BAR, HD_BAR, NIC_BAR, ISDN_BAR};
  GdkBitmap *cpu_mask, *ram_mask, *hd_mask; 
  GdkBitmap *nic_mask, *isdn_mask;
  gchar *cpu_label  = "Processor";
  gchar *ram_label  = "RAM";
  gchar *hd_label   = "Harddisk(s)";
  gchar *nic_label  = "NICs";
  gchar *isdn_label = "ISDN Card";
  gchar cpu_bar_size[8];
  gchar ram_bar_size[8];
  gchar hd_bar_size[8];
  gchar nic_bar_size[8];
  gchar isdn_bar_size[8];
  guint8 spacing    = 2;
  gint line_number;
  gchar *initializer[2] = {"", ""};
  gint width[3];
  gint offset = 25;

  /* Create clist 1st. column pixmaps */
  cpu_pixmap  =
create_pixmap(gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                              "cpu.xpm");
  gtk_pixmap_get (GTK_PIXMAP (cpu_pixmap), &cpu_gdk_pixmap, &cpu_mask);
  ram_pixmap  =
create_pixmap(gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                              "ram.xpm");
  gtk_pixmap_get (GTK_PIXMAP (ram_pixmap), &ram_gdk_pixmap, &ram_mask);
  hd_pixmap   =
create_pixmap(gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                              "harddisk.xpm");
  gtk_pixmap_get (GTK_PIXMAP (hd_pixmap), &hd_gdk_pixmap, &hd_mask);
  nic_pixmap  =
create_pixmap(gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                              "nic.xpm");
  gtk_pixmap_get (GTK_PIXMAP (nic_pixmap), &nic_gdk_pixmap, &nic_mask);
  isdn_pixmap =
create_pixmap(gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                              "isdncard.xpm");
  gtk_pixmap_get (GTK_PIXMAP (isdn_pixmap), &isdn_gdk_pixmap,
&isdn_mask);

  /* Freeze clist (in order to speed up clist data insertion)*/
  gtk_clist_freeze(clist);

  /* Clear test_clist */
  gtk_clist_clear(clist);

  /* Initialilze clist first column (that is static) */
  for (line_number = 0; line_number < 5; line_number++)
    gtk_clist_append(clist, initializer);

  /* Create test_clist first column */
  gtk_clist_set_pixtext(GTK_CLIST(clist), 0, 0, cpu_label, 
                        spacing, cpu_gdk_pixmap, cpu_mask);
  gtk_clist_set_pixtext(GTK_CLIST(clist), 1, 0, ram_label, 
                        spacing, ram_gdk_pixmap, ram_mask);
  gtk_clist_set_pixtext(GTK_CLIST(clist), 2, 0, hd_label, 
                        spacing, hd_gdk_pixmap, hd_mask);
  gtk_clist_set_pixtext(GTK_CLIST(clist), 3, 0, nic_label, 
                        spacing, nic_gdk_pixmap, nic_mask);
  gtk_clist_set_pixtext(GTK_CLIST(clist), 4, 0, isdn_label, 
                        spacing, isdn_gdk_pixmap, isdn_mask);

  /* Get the current optimal width of test_clist first column */
  width[0] = gtk_clist_optimal_column_width(GTK_CLIST(clist), 0);

  /* Get test_clist current width */
  width[1] = GTK_WIDGET(clist)->allocation.width;

  /* Calculate new optimal width for the test_clist second column */
  width[2] = width[1] - width[0] - offset;

  /* Set the bar sizes (give in %) */
  cpu_bar_size  = "50%";
  ram_bar_size  = "30%"
  hd_bar_size   = "20%"
  nic_bar_size  = "70%"
  isdn_bar_size = "90%"

  /* Create bar pixmaps */
  create_bar_pixmap(CPU_BAR,
                    gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                    16, width[2], atoi(cpu_bar_size));
  create_bar_pixmap(RAM_BAR,
                    gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                    16, width[2], atoi(ram_bar_size));
  create_bar_pixmap(HD_BAR,
                    gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                    16, width[2], atoi(hd_bar_size));
  create_bar_pixmap(NIC_BAR,
                    gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                    16, width[2], atoi(nic_bar_size));
  create_bar_pixmap(ISDN_BAR,
                    gtk_widget_get_toplevel(GTK_WIDGET(clist)),
                    16, width[2], atoi(isdn_bar_size));

  /* Insert bar pixmaps into clist (into second column) */
  gtk_clist_set_pixmap(GTK_CLIST(clist), 0, 1,
                       bar_pixmap[0], bar_mask[0]);
  gtk_clist_set_pixmap(GTK_CLIST(clist), 1, 1,
                       bar_pixmap[1], bar_mask[1]);
  gtk_clist_set_pixmap(GTK_CLIST(clist), 2, 1,
                       bar_pixmap[2], bar_mask[2]);
  gtk_clist_set_pixmap(GTK_CLIST(clist), 3, 1,
                       bar_pixmap[3], bar_mask[3]);
  gtk_clist_set_pixmap(GTK_CLIST(clist), 4, 1,
                       bar_pixmap[4], bar_mask[4]);

  /* Thaw clist, in order to show the changes made */
  gtk_clist_thaw(clist);
}


/* Function to create a bar pixmap to be inserted into test_clist */

void
create_bar_pixmap                      (gint bar_type,
                                        GtkWidget *widget, gint height, 
                                        gint width, gint size)
{
  gchar **pixmap_d, *color_code;

  /* Select bar color code (and so the bar color) according to 
   * current test status
   */
  if(size < 33)
    color_code = "#ff0000"; /* Red */
  else if (size < 67)
    color_code = "#ffd600"; /* Yellow gold */
  else
    color_code = "#006100"; /* Dark green */

  /* Get the data for the bar pixmap */
  pixmap_d = create_bar_bitmap (height, width, size, color_code);

  /* Create and return pixmap */
  bar_pixmap[bar_type] = gdk_pixmap_create_from_xpm_d(widget->window, 
                                                      &bar_mask[bar_type],
                                                      NULL,
                                                      (gpointer) pixmap_d);

  /*Free bar pixmap data */
  free_bar_bitmap (pixmap_d);
}


/* Function to create a bar bitmap as needed by the function
 * create_bar_bitmap()
 */

gchar **
create_bar_bitmap                      (gint height, gint width, 
                                        gint size, gchar *color_code)
{
  gchar **bitmap, *buffer_copy, buffer[width];
  gint real_size, i;

  /* Calculate the real bar size from size (size is given in %) */
  real_size = size * width / 100;

  /* Allocate memory for the data */
  bitmap = g_malloc((height + 1 + 2) * sizeof (gchar *));

  /* Create bitmap header: height/width/colors/number of chars */
  g_snprintf(buffer, 90, "%d %d 2 1", width, height);
  bitmap[0] = g_strdup(buffer);

  /* Define the "none" color */
  bitmap[1] = g_strdup("  c None");

 /* Define the filled in color */
  g_snprintf(buffer, 90, "X c %s", color_code);
  bitmap[2] = g_strdup(buffer);

  /* Fill in the buffer with the size */
  strcpy(buffer, " ");
  for(i = 0; i < real_size; i++) {
    strcat(buffer, "X");
  }
   
  /* Leave the remainder not filled in */
  while (i < width) {
    strcat(buffer, " ");
    i++;
  }

  /* Pad bitmap with blank */
  strcat(buffer, " ");

  /* Copy the string */
  buffer_copy = g_strdup(buffer);

  /* Make it the data for all the strings */
  for (i = 3; i < height+3; i++) {
    bitmap[i] = buffer_copy;
  }

  /* Return the bitmap */
  return (bitmap);
}


/* Function to free the memory allocated to create the bitmap */

void 
free_bar_bitmap                        (char **bitmap)
{
  g_free (bitmap[0]);
  g_free (bitmap[1]);
  g_free (bitmap[2]);
  g_free (bitmap[3]);
  g_free (bitmap);
}


Best regards,

Rafael (8-)
-- 
rafael peregrino innominate com
software engineer                                        innominate AG
server appliances                                 the linux architects
tel: +49.30.308806-81  fax: -77              http://www.innominate.com





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