]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkprogressbar.c
gtk/gtkaccellabel.c gtk/gtkaction.c gtk/gtkclist.c gtk/gtkcolorbutton.c
[~andy/gtk] / gtk / gtkprogressbar.c
index 0bf8661f286cb6378d2e38bb39536e7d4512b11f..96b316748a53f31603770acf0734cf121a5befe0 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#if HAVE_CONFIG_H
-#  include <config.h>
-#  if STDC_HEADERS
-#    include <string.h>
-#    include <stdio.h>
-#  endif
-#else
-#  include <stdio.h>
-#endif
+#include "config.h"
+
+#include <string.h>
+
+#undef GTK_DISABLE_DEPRECATED
+#define __GTK_PROGRESS_BAR_C__
 
 #include "gtkprogressbar.h"
 #include "gtkprivate.h"
@@ -44,8 +41,6 @@
 #define MIN_HORIZONTAL_BAR_HEIGHT  20
 #define MIN_VERTICAL_BAR_WIDTH     22
 #define MIN_VERTICAL_BAR_HEIGHT    80
-#define MAX_TEXT_LENGTH            80
-#define TEXT_SPACING               2
 
 enum {
   PROP_0,
@@ -73,8 +68,12 @@ static void gtk_progress_bar_get_property  (GObject             *object,
                                            guint                prop_id,
                                            GValue              *value,
                                            GParamSpec          *pspec);
+static gboolean gtk_progress_bar_expose    (GtkWidget           *widget,
+                                           GdkEventExpose      *event);
 static void gtk_progress_bar_size_request  (GtkWidget           *widget,
                                            GtkRequisition      *requisition);
+static void gtk_progress_bar_style_set     (GtkWidget           *widget,
+                                           GtkStyle            *previous);
 static void gtk_progress_bar_real_update   (GtkProgress         *progress);
 static void gtk_progress_bar_paint         (GtkProgress         *progress);
 static void gtk_progress_bar_act_mode_enter (GtkProgress        *progress);
@@ -105,7 +104,9 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
   gobject_class->set_property = gtk_progress_bar_set_property;
   gobject_class->get_property = gtk_progress_bar_get_property;
   
+  widget_class->expose_event = gtk_progress_bar_expose;
   widget_class->size_request = gtk_progress_bar_size_request;
+  widget_class->style_set = gtk_progress_bar_style_set;
 
   progress_class->paint = gtk_progress_bar_paint;
   progress_class->update = gtk_progress_bar_real_update;
@@ -142,9 +143,7 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
                                    g_param_spec_uint ("activity-step",
                                                      P_("Activity Step"),
                                                      P_("The increment used for each iteration in activity mode (Deprecated)"),
-                                                     0,
-                                                     G_MAXUINT,
-                                                     3,
+                                                     0, G_MAXUINT, 3,
                                                      GTK_PARAM_READWRITE));
 
   g_object_class_install_property (gobject_class,
@@ -152,9 +151,7 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
                                    g_param_spec_uint ("activity-blocks",
                                                      P_("Activity Blocks"),
                                                      P_("The number of blocks which can fit in the progress bar area in activity mode (Deprecated)"),
-                                                     2,
-                                                     G_MAXUINT,
-                                                     5,
+                                                     2, G_MAXUINT, 5,
                                                      GTK_PARAM_READWRITE));
 
   g_object_class_install_property (gobject_class,
@@ -162,9 +159,7 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
                                    g_param_spec_uint ("discrete-blocks",
                                                      P_("Discrete Blocks"),
                                                      P_("The number of discrete blocks in a progress bar (when shown in the discrete style)"),
-                                                     2,
-                                                     G_MAXUINT,
-                                                     10,
+                                                     2, G_MAXUINT, 10,
                                                      GTK_PARAM_READWRITE));
   
   g_object_class_install_property (gobject_class,
@@ -172,9 +167,7 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
                                   g_param_spec_double ("fraction",
                                                        P_("Fraction"),
                                                        P_("The fraction of total work that has been completed"),
-                                                       0.0,
-                                                       1.0,
-                                                       0.0,
+                                                       0.0, 1.0, 0.0,
                                                        GTK_PARAM_READWRITE));  
   
   g_object_class_install_property (gobject_class,
@@ -182,9 +175,7 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
                                   g_param_spec_double ("pulse-step",
                                                        P_("Pulse Step"),
                                                        P_("The fraction of total progress to move the bouncing block when pulsed"),
-                                                       0.0,
-                                                       1.0,
-                                                       0.1,
+                                                       0.0, 1.0, 0.1,
                                                        GTK_PARAM_READWRITE));  
   
   g_object_class_install_property (gobject_class,
@@ -192,7 +183,7 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
                                   g_param_spec_string ("text",
                                                        P_("Text"),
                                                        P_("Text to be displayed in the progress bar"),
-                                                       "%P %%",
+                                                       NULL,
                                                        GTK_PARAM_READWRITE));
 
   /**
@@ -213,11 +204,76 @@ gtk_progress_bar_class_init (GtkProgressBarClass *class)
                                   PROP_ELLIPSIZE,
                                    g_param_spec_enum ("ellipsize",
                                                       P_("Ellipsize"),
-                                                      P_("The preferred place to ellipsize the string, if the progressbar does not have enough room to display the entire string, if at all"),
+                                                      P_("The preferred place to ellipsize the string, if the progress bar "
+                                                         "does not have enough room to display the entire string, if at all."),
                                                      PANGO_TYPE_ELLIPSIZE_MODE,
                                                      PANGO_ELLIPSIZE_NONE,
                                                       GTK_PARAM_READWRITE));
+  gtk_widget_class_install_style_property (widget_class,
+                                           g_param_spec_int ("xspacing",
+                                                             P_("XSpacing"),
+                                                             P_("Extra spacing applied to the width of a progress bar."),
+                                                             0, G_MAXINT, 7,
+                                                             G_PARAM_READWRITE));
+  gtk_widget_class_install_style_property (widget_class,
+                                           g_param_spec_int ("yspacing",
+                                                             P_("YSpacing"),
+                                                             P_("Extra spacing applied to the height of a progress bar."),
+                                                             0, G_MAXINT, 7,
+                                                             G_PARAM_READWRITE));
 
+  /**
+   * GtkProgressBar:min-horizontal-bar-width:
+   *
+   * The minimum horizontal width of the progress bar.
+   *
+   * Since: 2.14
+   */
+  gtk_widget_class_install_style_property (widget_class,
+                                           g_param_spec_int ("min-horizontal-bar-width",
+                                                             P_("Min horizontal bar width"),
+                                                             P_("The minimum horizontal width of the progress bar"),
+                                                             1, G_MAXINT, MIN_HORIZONTAL_BAR_WIDTH,
+                                                             G_PARAM_READWRITE));
+  /**
+   * GtkProgressBar:min-horizontal-bar-height:
+   *
+   * Minimum horizontal height of the progress bar.
+   *
+   * Since: 2.14
+   */
+  gtk_widget_class_install_style_property (widget_class,
+                                           g_param_spec_int ("min-horizontal-bar-height",
+                                                             P_("Min horizontal bar height"),
+                                                             P_("Minimum horizontal height of the progress bar"),
+                                                             1, G_MAXINT, MIN_HORIZONTAL_BAR_HEIGHT,
+                                                             G_PARAM_READWRITE));
+  /**
+   * GtkProgressBar:min-vertical-bar-width:
+   *
+   * The minimum vertical width of the progress bar.
+   *
+   * Since: 2.14
+   */
+  gtk_widget_class_install_style_property (widget_class,
+                                           g_param_spec_int ("min-vertical-bar-width",
+                                                             P_("Min vertical bar width"),
+                                                             P_("The minimum vertical width of the progress bar"),
+                                                             1, G_MAXINT, MIN_VERTICAL_BAR_WIDTH,
+                                                             G_PARAM_READWRITE));
+  /**
+   * GtkProgressBar:min-vertical-bar-height:
+   *
+   * The minimum vertical height of the progress bar.
+   *
+   * Since: 2.14
+   */
+  gtk_widget_class_install_style_property (widget_class,
+                                           g_param_spec_int ("min-vertical-bar-height",
+                                                             P_("Min vertical bar height"),
+                                                             P_("The minimum vertical height of the progress bar"),
+                                                             1, G_MAXINT, MIN_VERTICAL_BAR_HEIGHT,
+                                                             G_PARAM_READWRITE));
 }
 
 static void
@@ -337,7 +393,7 @@ gtk_progress_bar_new (void)
 {
   GtkWidget *pbar;
 
-  pbar = gtk_widget_new (GTK_TYPE_PROGRESS_BAR, NULL);
+  pbar = g_object_new (GTK_TYPE_PROGRESS_BAR, NULL);
 
   return pbar;
 }
@@ -349,7 +405,7 @@ gtk_progress_bar_new_with_adjustment (GtkAdjustment *adjustment)
 
   g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), NULL);
 
-  pbar = gtk_widget_new (GTK_TYPE_PROGRESS_BAR,
+  pbar = g_object_new (GTK_TYPE_PROGRESS_BAR,
                         "adjustment", adjustment,
                         NULL);
 
@@ -438,7 +494,7 @@ gtk_progress_bar_real_update (GtkProgress *progress)
                }
            }
        }
-      gtk_progress_bar_paint (progress);
+      pbar->dirty = TRUE;
       gtk_widget_queue_draw (GTK_WIDGET (progress));
     }
   else
@@ -451,12 +507,28 @@ gtk_progress_bar_real_update (GtkProgress *progress)
       if (pbar->in_block != in_block)
        {
          pbar->in_block = in_block;
-         gtk_progress_bar_paint (progress);
+         pbar->dirty = TRUE;
          gtk_widget_queue_draw (GTK_WIDGET (progress));
        }
     }
 }
 
+static gboolean
+gtk_progress_bar_expose (GtkWidget      *widget,
+                    GdkEventExpose *event)
+{
+  GtkProgressBar *pbar;
+
+  g_return_val_if_fail (GTK_IS_PROGRESS_BAR (widget), FALSE);
+
+  pbar = GTK_PROGRESS_BAR (widget);
+
+  if (GTK_WIDGET_DRAWABLE (widget) && pbar->dirty)
+    gtk_progress_bar_paint (GTK_PROGRESS (pbar));
+
+  return GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->expose_event (widget, event);
+}
+
 static void
 gtk_progress_bar_size_request (GtkWidget      *widget,
                               GtkRequisition *requisition)
@@ -467,15 +539,22 @@ gtk_progress_bar_size_request (GtkWidget      *widget,
   PangoRectangle logical_rect;
   PangoLayout *layout;
   gint width, height;
+  gint xspacing, yspacing;
+  gint min_width, min_height;
 
   g_return_if_fail (GTK_IS_PROGRESS_BAR (widget));
   g_return_if_fail (requisition != NULL);
 
+  gtk_widget_style_get (widget,
+                        "xspacing", &xspacing,
+                        "yspacing", &yspacing,
+                        NULL);
+
   progress = GTK_PROGRESS (widget);
   pbar = GTK_PROGRESS_BAR (widget);
 
-  width = 2 * widget->style->xthickness + 3 + 2 * TEXT_SPACING;
-  height = 2 * widget->style->ythickness + 3 + 2 * TEXT_SPACING;
+  width = 2 * widget->style->xthickness + xspacing;
+  height = 2 * widget->style->ythickness + yspacing;
 
   if (progress->show_text && pbar->bar_style != GTK_PROGRESS_DISCRETE)
     {
@@ -514,15 +593,29 @@ gtk_progress_bar_size_request (GtkWidget      *widget,
   
   if (pbar->orientation == GTK_PROGRESS_LEFT_TO_RIGHT ||
       pbar->orientation == GTK_PROGRESS_RIGHT_TO_LEFT)
-    {
-      requisition->width = MAX (MIN_HORIZONTAL_BAR_WIDTH, width);
-      requisition->height = MAX (MIN_HORIZONTAL_BAR_HEIGHT, height);
-    }
+    gtk_widget_style_get (widget,
+                         "min-horizontal-bar-width", &min_width,
+                         "min-horizontal-bar-height", &min_height,
+                         NULL);
   else
-    {
-      requisition->width = MAX (MIN_VERTICAL_BAR_WIDTH, width);
-      requisition->height = MAX (MIN_VERTICAL_BAR_HEIGHT, height);
-    }
+    gtk_widget_style_get (widget,
+                         "min-vertical-bar-width", &min_width,
+                         "min-vertical-bar-height", &min_height,
+                         NULL);
+
+  requisition->width = MAX (min_width, width);
+  requisition->height = MAX (min_height, height);
+}
+
+static void
+gtk_progress_bar_style_set (GtkWidget      *widget,
+    GtkStyle *previous)
+{
+  GtkProgressBar *pbar = GTK_PROGRESS_BAR (widget);
+
+  pbar->dirty = TRUE;
+
+  GTK_WIDGET_CLASS (gtk_progress_bar_parent_class)->style_set (widget, previous);
 }
 
 static void
@@ -579,6 +672,30 @@ gtk_progress_bar_act_mode_enter (GtkProgress *progress)
     }
 }
 
+static void
+gtk_progress_bar_get_activity (GtkProgressBar            *pbar,
+                              GtkProgressBarOrientation  orientation,
+                              gint                      *offset,
+                              gint                      *amount)
+{
+  GtkWidget *widget = GTK_WIDGET (pbar);
+
+  *offset = pbar->activity_pos;
+
+  switch (orientation)
+    {
+    case GTK_PROGRESS_LEFT_TO_RIGHT:
+    case GTK_PROGRESS_RIGHT_TO_LEFT:
+      *amount = MAX (2, widget->allocation.width / pbar->activity_blocks);
+      break;
+
+    case GTK_PROGRESS_TOP_TO_BOTTOM:
+    case GTK_PROGRESS_BOTTOM_TO_TOP:
+      *amount = MAX (2, widget->allocation.height / pbar->activity_blocks);
+      break;
+    }
+}
+
 static void
 gtk_progress_bar_paint_activity (GtkProgressBar            *pbar,
                                 GtkProgressBarOrientation  orientation)
@@ -591,18 +708,16 @@ gtk_progress_bar_paint_activity (GtkProgressBar            *pbar,
     {
     case GTK_PROGRESS_LEFT_TO_RIGHT:
     case GTK_PROGRESS_RIGHT_TO_LEFT:
-      area.x = pbar->activity_pos;
+      gtk_progress_bar_get_activity (pbar, orientation, &area.x, &area.width);
       area.y = widget->style->ythickness;
-      area.width = MAX (2, widget->allocation.width / pbar->activity_blocks);
       area.height = widget->allocation.height - 2 * widget->style->ythickness;
       break;
 
     case GTK_PROGRESS_TOP_TO_BOTTOM:
     case GTK_PROGRESS_BOTTOM_TO_TOP:
+      gtk_progress_bar_get_activity (pbar, orientation, &area.y, &area.height);
       area.x = widget->style->xthickness;
-      area.y = pbar->activity_pos;
       area.width = widget->allocation.width - 2 * widget->style->xthickness;
-      area.height = MAX (2, widget->allocation.height / pbar->activity_blocks);
       break;
 
     default:
@@ -719,20 +834,25 @@ gtk_progress_bar_paint_discrete (GtkProgressBar            *pbar,
 
 static void
 gtk_progress_bar_paint_text (GtkProgressBar            *pbar,
+                            gint                       offset,
                             gint                       amount,
                             GtkProgressBarOrientation  orientation)
 {
   GtkProgress *progress = GTK_PROGRESS (pbar);
   GtkWidget *widget = GTK_WIDGET (pbar);
-  
   gint x;
   gint y;
   gchar *buf;
   GdkRectangle rect;
   PangoLayout *layout;
   PangoRectangle logical_rect;
-  GdkRectangle prelight_clip, normal_clip;
-  
+  GdkRectangle prelight_clip, start_clip, end_clip;
+  gfloat text_xalign = progress->x_align;
+  gfloat text_yalign = progress->y_align;
+
+  if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
+    text_xalign = 1.0 - text_xalign;
+
   buf = gtk_progress_get_current_text (progress);
   
   layout = gtk_widget_create_pango_layout (widget, buf);
@@ -742,50 +862,86 @@ gtk_progress_bar_paint_text (GtkProgressBar            *pbar,
 
   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
   
-  x = widget->style->xthickness + 1 +
-    (widget->allocation.width - 2 * widget->style->xthickness -
-     2 - logical_rect.width)
-    * progress->x_align; 
+  x = widget->style->xthickness + 1 + text_xalign *
+      (widget->allocation.width - 2 * widget->style->xthickness -
+       2 - logical_rect.width);
 
-  y = widget->style->ythickness + 1 +
-    (widget->allocation.height - 2 * widget->style->ythickness -
-     2 - logical_rect.height)
-    * progress->y_align;
+  y = widget->style->ythickness + 1 + text_yalign *
+      (widget->allocation.height - 2 * widget->style->ythickness -
+       2 - logical_rect.height);
 
   rect.x = widget->style->xthickness;
   rect.y = widget->style->ythickness;
   rect.width = widget->allocation.width - 2 * widget->style->xthickness;
   rect.height = widget->allocation.height - 2 * widget->style->ythickness;
 
-  prelight_clip = normal_clip = rect;
+  prelight_clip = start_clip = end_clip = rect;
 
   switch (orientation)
     {
     case GTK_PROGRESS_LEFT_TO_RIGHT:
+      if (offset != -1)
+       prelight_clip.x = offset;
       prelight_clip.width = amount;
-      normal_clip.x += amount;
-      normal_clip.width -= amount;
+      start_clip.width = prelight_clip.x - start_clip.x;
+      end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
+      end_clip.width -= prelight_clip.width + start_clip.width;
       break;
       
     case GTK_PROGRESS_RIGHT_TO_LEFT:
-      normal_clip.width -= amount;
-      prelight_clip.x += normal_clip.width;
-      prelight_clip.width -= normal_clip.width;
+      if (offset != -1)
+       prelight_clip.x = offset;
+      else
+       prelight_clip.x = rect.x + rect.width - amount;
+      prelight_clip.width = amount;
+      start_clip.width = prelight_clip.x - start_clip.x;
+      end_clip.x = start_clip.x + start_clip.width + prelight_clip.width;
+      end_clip.width -= prelight_clip.width + start_clip.width;
       break;
        
     case GTK_PROGRESS_TOP_TO_BOTTOM:
+      if (offset != -1)
+       prelight_clip.y = offset;
       prelight_clip.height = amount;
-      normal_clip.y += amount;
-      normal_clip.height -= amount;
+      start_clip.height = prelight_clip.y - start_clip.y;
+      end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
+      end_clip.height -= prelight_clip.height + start_clip.height;
       break;
       
     case GTK_PROGRESS_BOTTOM_TO_TOP:
-      normal_clip.height -= amount;
-      prelight_clip.y += normal_clip.height;
-      prelight_clip.height -= normal_clip.height;
+      if (offset != -1)
+       prelight_clip.y = offset;
+      else
+       prelight_clip.y = rect.y + rect.height - amount;
+      prelight_clip.height = amount;
+      start_clip.height = prelight_clip.y - start_clip.y;
+      end_clip.y = start_clip.y + start_clip.height + prelight_clip.height;
+      end_clip.height -= prelight_clip.height + start_clip.height;
       break;
     }
-  
+
+  if (start_clip.width > 0 && start_clip.height > 0)
+    gtk_paint_layout (widget->style,
+                     progress->offscreen_pixmap,
+                     GTK_STATE_NORMAL,
+                     FALSE,
+                     &start_clip,
+                     widget,
+                     "progressbar",
+                     x, y,
+                     layout);
+
+  if (end_clip.width > 0 && end_clip.height > 0)
+    gtk_paint_layout (widget->style,
+                     progress->offscreen_pixmap,
+                     GTK_STATE_NORMAL,
+                     FALSE,
+                     &end_clip,
+                     widget,
+                     "progressbar",
+                     x, y,
+                     layout);
+
   gtk_paint_layout (widget->style,
                    progress->offscreen_pixmap,
                    GTK_STATE_PRELIGHT,
@@ -795,16 +951,6 @@ gtk_progress_bar_paint_text (GtkProgressBar            *pbar,
                    "progressbar",
                    x, y,
                    layout);
-  
-  gtk_paint_layout (widget->style,
-                   progress->offscreen_pixmap,
-                   GTK_STATE_NORMAL,
-                   FALSE,
-                   &normal_clip,
-                   widget,
-                   "progressbar",
-                   x, y,
-                   layout);
 
   g_object_unref (layout);
   g_free (buf);
@@ -845,6 +991,15 @@ gtk_progress_bar_paint (GtkProgress *progress)
       if (progress->activity_mode)
        {
          gtk_progress_bar_paint_activity (pbar, orientation);
+
+         if (GTK_PROGRESS (pbar)->show_text)
+           {
+             gint offset;
+             gint amount;
+
+             gtk_progress_bar_get_activity (pbar, orientation, &offset, &amount);
+             gtk_progress_bar_paint_text (pbar, offset, amount, orientation);
+           }
        }
       else
        {
@@ -865,11 +1020,13 @@ gtk_progress_bar_paint (GtkProgress *progress)
              gtk_progress_bar_paint_continuous (pbar, amount, orientation);
 
              if (GTK_PROGRESS (pbar)->show_text)
-               gtk_progress_bar_paint_text (pbar, amount, orientation);
+               gtk_progress_bar_paint_text (pbar, -1, amount, orientation);
            }
          else
            gtk_progress_bar_paint_discrete (pbar, orientation);
        }
+
+      pbar->dirty = FALSE;
     }
 }
 
@@ -1224,5 +1381,4 @@ gtk_progress_bar_get_ellipsize (GtkProgressBar *pbar)
   return pbar->ellipsize;
 }
 
-#define __GTK_PROGRESS_BAR_C__
 #include "gtkaliasdef.c"