]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkpreview.c
Adapt cast macros to standard.
[~andy/gtk] / gtk / gtkpreview.c
index 05df6d98367687494e318f950b8e96c3342d57fb..39d2618a0f3c0a49c4feb076c49f2f8a19f7bd40 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
+/*
+ * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
+ * file for a list of people on the GTK+ Team.  See the ChangeLog
+ * files for a list of changes.  These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
+ */
+
+#include "config.h"
+
 #include <math.h>
 #include <string.h>
 #include <sys/types.h>
+#ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
-#include "gdk/gdkx.h"
+#endif
 #include "gdk/gdkrgb.h"
 #include "gtkpreview.h"
 #include "gtksignal.h"
 
 #define PREVIEW_CLASS(w)      GTK_PREVIEW_CLASS (GTK_OBJECT (w)->klass)
 
+enum {
+  ARG_0,
+  ARG_EXPAND
+};
+
 
 static void   gtk_preview_class_init    (GtkPreviewClass  *klass);
 static void   gtk_preview_init          (GtkPreview       *preview);
+static void   gtk_preview_set_arg      (GtkObject        *object,
+                                        GtkArg           *arg,
+                                        guint             arg_id);
+static void   gtk_preview_get_arg      (GtkObject        *object,
+                                        GtkArg           *arg,
+                                        guint             arg_id);
 static void   gtk_preview_finalize      (GtkObject        *object);
 static void   gtk_preview_realize       (GtkWidget        *widget);
+static void   gtk_preview_size_allocate (GtkWidget        *widget,
+                                        GtkAllocation    *allocation);
 static gint   gtk_preview_expose        (GtkWidget        *widget,
                                         GdkEventExpose   *event);
 static void   gtk_preview_make_buffer   (GtkPreview       *preview);
@@ -44,10 +67,10 @@ static GtkPreviewClass *preview_class = NULL;
 static gint install_cmap = FALSE;
 
 
-guint
+GtkType
 gtk_preview_get_type (void)
 {
-  static guint preview_type = 0;
+  static GtkType preview_type = 0;
 
   if (!preview_type)
     {
@@ -63,7 +86,7 @@ gtk_preview_get_type (void)
         (GtkClassInitFunc) NULL,
       };
 
-      preview_type = gtk_type_unique (gtk_widget_get_type (), &preview_info);
+      preview_type = gtk_type_unique (GTK_TYPE_WIDGET, &preview_info);
     }
 
   return preview_type;
@@ -78,12 +101,15 @@ gtk_preview_class_init (GtkPreviewClass *klass)
   object_class = (GtkObjectClass*) klass;
   widget_class = (GtkWidgetClass*) klass;
 
-  parent_class = gtk_type_class (gtk_widget_get_type ());
+  parent_class = gtk_type_class (GTK_TYPE_WIDGET);
   preview_class = klass;
 
+  object_class->set_arg = gtk_preview_set_arg;
+  object_class->get_arg = gtk_preview_get_arg;
   object_class->finalize = gtk_preview_finalize;
 
   widget_class->realize = gtk_preview_realize;
+  widget_class->size_allocate = gtk_preview_size_allocate;
   widget_class->expose_event = gtk_preview_expose;
 
   klass->info.visual = NULL;
@@ -96,6 +122,46 @@ gtk_preview_class_init (GtkPreviewClass *klass)
   gdk_rgb_init ();
   klass->info.cmap = gdk_rgb_get_cmap ();
   klass->info.visual = gdk_rgb_get_visual ();
+
+  gtk_object_add_arg_type ("GtkPreview::expand",
+                          GTK_TYPE_BOOL,
+                          GTK_ARG_READWRITE,
+                          ARG_EXPAND);
+}
+
+static void
+gtk_preview_set_arg (GtkObject *object,
+                    GtkArg    *arg,
+                    guint      arg_id)
+{
+  GtkPreview *preview = GTK_PREVIEW (object);
+  
+  switch (arg_id)
+    {
+    case ARG_EXPAND:
+      gtk_preview_set_expand (preview, GTK_VALUE_BOOL (*arg));
+      break;
+    }
+}
+
+static void
+gtk_preview_get_arg (GtkObject *object,
+                    GtkArg    *arg,
+                    guint      arg_id)
+{
+  GtkPreview *preview;
+  
+  preview = GTK_PREVIEW (object);
+  
+  switch (arg_id)
+    {
+    case ARG_EXPAND:
+      GTK_VALUE_BOOL (*arg) = preview->expand;
+      break;
+    default:
+      arg->type = GTK_TYPE_INVALID;
+      break;
+    }
 }
 
 void
@@ -225,17 +291,6 @@ gtk_preview_put (GtkPreview   *preview,
                        
 }
 
-void
-gtk_preview_put_row (GtkPreview *preview,
-                    guchar     *src,
-                    guchar     *dest,
-                    gint        x,
-                    gint        y,
-                    gint        w)
-{
-  g_warning ("gtk_preview_put_row not implemented (deprecated)\n");
-}
-
 void
 gtk_preview_draw_row (GtkPreview *preview,
                      guchar     *data,
@@ -293,12 +348,18 @@ gtk_preview_draw_row (GtkPreview *preview,
 
 void
 gtk_preview_set_expand (GtkPreview *preview,
-                       gint        expand)
+                       gboolean    expand)
 {
   g_return_if_fail (preview != NULL);
   g_return_if_fail (GTK_IS_PREVIEW (preview));
 
-  preview->expand = (expand != FALSE);
+  expand = expand != FALSE;
+
+  if (preview->expand != expand)
+    {
+      preview->expand = expand;
+      gtk_widget_queue_resize (GTK_WIDGET (preview));
+    }
 }
 
 void
@@ -406,10 +467,21 @@ gtk_preview_realize (GtkWidget *widget)
   preview = GTK_PREVIEW (widget);
 
   attributes.window_type = GDK_WINDOW_CHILD;
-  attributes.x = widget->allocation.x;
-  attributes.y = widget->allocation.y;
-  attributes.width = widget->allocation.width;
-  attributes.height = widget->allocation.height;
+
+  if (preview->expand)
+    {
+      attributes.width = widget->allocation.width;
+      attributes.height = widget->allocation.height;
+    }
+  else
+    {
+      attributes.width = MIN (widget->requisition.width, widget->allocation.width);
+      attributes.height = MIN (widget->requisition.height, widget->allocation.height);
+    }
+
+  attributes.x = widget->allocation.x + (widget->allocation.width - attributes.width) / 2;
+  attributes.y = widget->allocation.y + (widget->allocation.height - attributes.height) / 2;;
+
   attributes.wclass = GDK_INPUT_OUTPUT;
   attributes.visual = preview_class->info.visual;
   attributes.colormap = preview_class->info.cmap;
@@ -421,7 +493,39 @@ gtk_preview_realize (GtkWidget *widget)
 
   widget->style = gtk_style_attach (widget->style, widget->window);
   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
-  gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
+}
+
+static void   
+gtk_preview_size_allocate (GtkWidget        *widget,
+                          GtkAllocation    *allocation)
+{
+  GtkPreview *preview;
+  gint width, height;
+
+  g_return_if_fail (widget != NULL);
+  g_return_if_fail (GTK_IS_PREVIEW (widget));
+
+  preview = GTK_PREVIEW (widget);
+  widget->allocation = *allocation;
+
+  if (GTK_WIDGET_REALIZED (widget))
+    {
+      if (preview->expand)
+       {
+         width = widget->allocation.width;
+         height = widget->allocation.height;
+       }
+      else
+       {
+         width = MIN (widget->allocation.width, widget->requisition.width);
+         height = MIN (widget->allocation.height, widget->requisition.height);
+       }
+
+      gdk_window_move_resize (widget->window,
+                             widget->allocation.x + (widget->allocation.width - width) / 2,
+                             widget->allocation.y + (widget->allocation.height - height) / 2,
+                             width, height);
+    }
 }
 
 static gint
@@ -429,6 +533,7 @@ gtk_preview_expose (GtkWidget      *widget,
                    GdkEventExpose *event)
 {
   GtkPreview *preview;
+  gint width, height;
 
   g_return_val_if_fail (widget != NULL, FALSE);
   g_return_val_if_fail (GTK_IS_PREVIEW (widget), FALSE);
@@ -438,12 +543,12 @@ gtk_preview_expose (GtkWidget      *widget,
     {
       preview = GTK_PREVIEW (widget);
       
+      gdk_window_get_size (widget->window, &width, &height);
+
       gtk_preview_put (GTK_PREVIEW (widget),
                       widget->window, widget->style->black_gc,
-                      event->area.x -
-                      (widget->allocation.width - preview->buffer_width)/2,
-                      event->area.y -
-                      (widget->allocation.height - preview->buffer_height)/2,
+                      event->area.x - (width - preview->buffer_width)/2,
+                      event->area.y - (height - preview->buffer_height)/2,
                       event->area.x, event->area.y,
                       event->area.width, event->area.height);
     }
@@ -493,7 +598,7 @@ gtk_preview_make_buffer (GtkPreview *preview)
     }
 }
 
-/* This will be useful for implementing gamma. */
+/* This is used for implementing gamma. */
 static void
 gtk_fill_lookup_array (guchar *array)
 {