gnome-games r8635 - trunk/libgames-support



Author: chpe
Date: Tue Feb  3 14:39:13 2009
New Revision: 8635
URL: http://svn.gnome.org/viewvc/gnome-games?rev=8635&view=rev

Log:
Rewrite GamesFrame. It was much too complicated for the simple purpose
it serves. Now it's just a GtkVBox containing a label and a GtkAlignment
and works just as well!

Modified:
   trunk/libgames-support/games-frame.c
   trunk/libgames-support/games-frame.h

Modified: trunk/libgames-support/games-frame.c
==============================================================================
--- trunk/libgames-support/games-frame.c	(original)
+++ trunk/libgames-support/games-frame.c	Tue Feb  3 14:39:13 2009
@@ -1,14 +1,10 @@
-/* GTK - The GIMP Toolkit
- * Copyright (C) 1995-1997, 2003 Peter Mattis, Spencer Kimball and Josh MacDonald
- *
- * GamesFrame: Create a bold-titled, indented frame
- *
- *     Copyright 2003 William Jon McCann
+/*
+ * Copyright  2009 Christian Persch
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,314 +17,131 @@
  * Boston, MA 02111-1307, USA.
  */
 
-#include "config.h"
-#include <string.h>
-#include <gtk/gtk.h>
-#include "games-frame.h"
-
-#define DEFAULT_LABEL_SPACING       6
-#define DEFAULT_LABEL_BOLD          TRUE
+#include <config.h>
 
-#define GAMES_FRAME_INDENT_KEY       "games-frame-indent"
-#define GAMES_FRAME_IN_EXPANDER_KEY  "games-frame-in-expander"
-
-
-static void games_frame_class_init (GamesFrameClass * klass);
-static void games_frame_init (GamesFrame * frame);
-
-static void games_frame_size_request (GtkWidget * widget,
-				      GtkRequisition * requisition);
-static void games_frame_size_allocate (GtkWidget * widget,
-				       GtkAllocation * allocation);
-static void games_frame_child_allocate (GtkFrame * frame,
-					GtkAllocation * allocation);
-static void games_frame_style_set (GtkWidget * widget, GtkStyle * previous);
-static gboolean games_frame_expose_event (GtkWidget * widget,
-					  GdkEventExpose * event);
-static void games_frame_label_widget_notify (GtkFrame * frame);
-static gint games_frame_get_indent (GtkWidget * widget);
-static gint games_frame_get_label_spacing (GtkFrame * frame);
-
-G_DEFINE_TYPE (GamesFrame, games_frame, GTK_TYPE_FRAME);
+#include <gtk/gtk.h>
 
-static void
-games_frame_class_init (GamesFrameClass * klass)
-{
-  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
-  GtkFrameClass *frame_class = GTK_FRAME_CLASS (klass);
+#include "games-frame.h"
 
-  widget_class->size_request = games_frame_size_request;
-  widget_class->size_allocate = games_frame_size_allocate;
-  widget_class->style_set = games_frame_style_set;
-  widget_class->expose_event = games_frame_expose_event;
-
-  frame_class->compute_child_allocation = games_frame_child_allocate;
-
-  gtk_widget_class_install_style_property (widget_class,
-					   g_param_spec_boolean ("label-bold",
-								 NULL, NULL,
-								 DEFAULT_LABEL_BOLD,
-								 G_PARAM_READABLE));
-  gtk_widget_class_install_style_property (widget_class,
-					   g_param_spec_int ("label-spacing",
-							     NULL, NULL,
-							     0,
-							     G_MAXINT,
-							     DEFAULT_LABEL_SPACING,
-							     G_PARAM_READABLE));
-}
+enum {
+  PROP_0,
+  PROP_LABEL
+};
 
+G_DEFINE_TYPE (GamesFrame, games_frame, GTK_TYPE_VBOX);
 
 static void
 games_frame_init (GamesFrame * frame)
 {
-  g_signal_connect (frame, "notify::label-widget",
-		    G_CALLBACK (games_frame_label_widget_notify), NULL);
-}
-
-static void
-games_frame_size_request (GtkWidget * widget, GtkRequisition * requisition)
-{
-  GtkFrame *frame = GTK_FRAME (widget);
-  GtkBin *bin = GTK_BIN (widget);
-  GtkRequisition child_requisition;
+  GtkBox *box = GTK_BOX (frame);
+  PangoAttrList *attr_list;
+  PangoAttribute *attr;
 
-  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget)) {
-    gtk_widget_size_request (frame->label_widget, requisition);
-  } else {
-    requisition->width = 0;
-    requisition->height = 0;
-  }
-
-  requisition->height += games_frame_get_label_spacing (frame);
-
-  if (bin->child && GTK_WIDGET_VISIBLE (bin->child)) {
-    gint indent = games_frame_get_indent (widget);
-
-    gtk_widget_size_request (bin->child, &child_requisition);
-
-    requisition->width = MAX (requisition->width,
-			      child_requisition.width + indent);
-    requisition->height += child_requisition.height;
-  }
-
-  requisition->width += 2 * GTK_CONTAINER (widget)->border_width;
-  requisition->height += 2 * GTK_CONTAINER (widget)->border_width;
-}
+  gtk_box_set_spacing (box, 6);
+  gtk_box_set_homogeneous (box, FALSE);
+#if GTK_CHECK_VERSION (2, 15, 0)
+  gtk_orientable_set_orientation (GTK_ORIENTABLE (frame), GTK_ORIENTATION_VERTICAL);
+#endif
 
-static void
-games_frame_size_allocate (GtkWidget * widget, GtkAllocation * allocation)
-{
-  GtkFrame *frame = GTK_FRAME (widget);
-  GtkBin *bin = GTK_BIN (widget);
-
-  widget->allocation = *allocation;
-
-  games_frame_child_allocate (frame, &frame->child_allocation);
-
-  if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
-    gtk_widget_size_allocate (bin->child, &frame->child_allocation);
+  frame->label = gtk_label_new (NULL);
+  gtk_misc_set_alignment (GTK_MISC (frame->label), 0.0, 0.5);
 
-  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget)) {
-    GtkAllocation label_allocation;
-    GtkRequisition label_requisition;
-    gint border = GTK_CONTAINER (widget)->border_width;
+  attr_list = pango_attr_list_new ();
+  attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
+  attr->start_index = 0;
+  attr->end_index = -1;
+  pango_attr_list_insert (attr_list, attr);
+  gtk_label_set_attributes (GTK_LABEL (frame->label), attr_list);
+  pango_attr_list_unref (attr_list);
 
-    gtk_widget_get_child_requisition (frame->label_widget,
-				      &label_requisition);
+  frame->alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
+  gtk_alignment_set_padding (GTK_ALIGNMENT (frame->alignment), 0, 0, 12, 0);
 
-    label_allocation.x = allocation->x + border;
-    label_allocation.y = allocation->y + border;
-    label_allocation.width = MAX (label_requisition.width,
-				  allocation->width - 2 * border);
-    label_allocation.height = label_requisition.height;
+  gtk_box_pack_start (box, frame->label, FALSE, FALSE, 0);
+  gtk_box_pack_start (box, frame->alignment, TRUE, TRUE, 0);
 
-    gtk_widget_size_allocate (frame->label_widget, &label_allocation);
-  }
+  gtk_widget_set_no_show_all (frame->label, TRUE);
+  gtk_widget_show (frame->alignment);
 }
 
 static void
-games_frame_child_allocate (GtkFrame * frame,
-			    GtkAllocation * child_allocation)
+games_frame_add (GtkContainer *container,
+                 GtkWidget *child)
 {
-  GtkWidget *widget = GTK_WIDGET (frame);
-  GtkAllocation *allocation = &widget->allocation;
-  gint border = GTK_CONTAINER (frame)->border_width;
-  gint spacing = 0;
-  gint indent = games_frame_get_indent (widget);
-
-  if (frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget)) {
-    GtkRequisition child_requisition;
-
-    gtk_widget_get_child_requisition (frame->label_widget,
-				      &child_requisition);
-    spacing += child_requisition.height;
-  }
-
-  spacing += games_frame_get_label_spacing (frame);
-
-  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
-    child_allocation->x = border + indent;
-  else
-    child_allocation->x = border;
-
-  child_allocation->y = border + spacing;
-  child_allocation->width = MAX (1, allocation->width - 2 * border - indent);
-  child_allocation->height = MAX (1,
-				  allocation->height -
-				  child_allocation->y - border);
+  GamesFrame *frame = GAMES_FRAME (container);
 
-  child_allocation->x += allocation->x;
-  child_allocation->y += allocation->y;
+  gtk_container_add (GTK_CONTAINER (frame->alignment), child);
 }
 
 static void
-games_frame_style_set (GtkWidget * widget, GtkStyle * previous)
+games_frame_set_property (GObject *object,
+                          guint prop_id,
+                          const GValue *value,
+                          GParamSpec *pspec)
 {
-  /*  font changes invalidate the indentation  */
-  g_object_set_data (G_OBJECT (widget), GAMES_FRAME_INDENT_KEY, NULL);
+  GamesFrame *frame = GAMES_FRAME (object);
 
-  /*  for "label_bold"  */
-  games_frame_label_widget_notify (GTK_FRAME (widget));
-}
-
-static gboolean
-games_frame_expose_event (GtkWidget * widget, GdkEventExpose * event)
-{
-  if (GTK_WIDGET_DRAWABLE (widget)) {
-    GtkWidgetClass *widget_class = g_type_class_peek_parent (games_frame_parent_class);
-
-    return widget_class->expose_event (widget, event);
+  switch (prop_id) {
+    case PROP_LABEL:
+      games_frame_set_label (frame, g_value_get_string (value));
+      break;
   }
-
-  return FALSE;
 }
 
 static void
-games_frame_label_widget_notify (GtkFrame * frame)
-{
-  if (frame->label_widget) {
-    GtkLabel *label = NULL;
-
-    if (GTK_IS_LABEL (frame->label_widget)) {
-      label = GTK_LABEL (frame->label_widget);
-
-      gtk_misc_set_alignment (GTK_MISC (label),
-			      frame->label_xalign, frame->label_yalign);
-    } else if (GTK_IS_BIN (frame->label_widget)) {
-      GtkWidget *child = gtk_bin_get_child (GTK_BIN (frame->label_widget));
-
-      if (GTK_IS_LABEL (child))
-	label = GTK_LABEL (child);
-    }
-
-    if (label) {
-      PangoAttrList *attrs = pango_attr_list_new ();
-      PangoAttribute *attr;
-      gboolean bold;
-
-      gtk_widget_style_get (GTK_WIDGET (frame), "label_bold", &bold, NULL);
-
-      attr = pango_attr_weight_new (bold ?
-				    PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
-      attr->start_index = 0;
-      attr->end_index = -1;
-      pango_attr_list_insert (attrs, attr);
-
-      gtk_label_set_attributes (label, attrs);
-
-      pango_attr_list_unref (attrs);
-    }
-  }
-}
-
-static gint
-games_frame_get_indent (GtkWidget * widget)
-{
-  gint width = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
-						   GAMES_FRAME_INDENT_KEY));
-
-  if (!width) {
-    PangoLayout *layout;
-
-    /*  the HIG suggests to use four spaces so do just that  */
-    layout = gtk_widget_create_pango_layout (widget, "    ");
-    pango_layout_get_pixel_size (layout, &width, NULL);
-    g_object_unref (layout);
-
-    g_object_set_data (G_OBJECT (widget),
-		       GAMES_FRAME_INDENT_KEY, GINT_TO_POINTER (width));
-  }
-
-  return width;
-}
-
-static gint
-games_frame_get_label_spacing (GtkFrame * frame)
+games_frame_class_init (GamesFrameClass * klass)
 {
-  gint spacing = 0;
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
 
-  if ((frame->label_widget && GTK_WIDGET_VISIBLE (frame->label_widget)) ||
-      (g_object_get_data (G_OBJECT (frame), GAMES_FRAME_IN_EXPANDER_KEY))) {
-    gtk_widget_style_get (GTK_WIDGET (frame),
-			  "label_spacing", &spacing, NULL);
-  }
+  object_class->set_property = games_frame_set_property;
+  container_class->add = games_frame_add;
 
-  return spacing;
+  g_object_class_install_property
+    (object_class,
+     PROP_LABEL,
+     g_param_spec_string ("label", NULL, NULL,
+                          NULL,
+                          G_PARAM_WRITABLE |
+                          G_PARAM_STATIC_NAME |
+                          G_PARAM_STATIC_NICK |
+                          G_PARAM_STATIC_BLURB));
 }
 
 /**
  * games_frame_new:
- * @label: text to set as the frame's title label (or %NULL for no title)
+ * @label: the frame's title, or %NULL
  *
- * Creates a #GamesFrame widget. A #GamesFrame is a HIG-compliant
- * variant of #GtkFrame. It doesn't render a frame at all but
- * otherwise behaves like a frame. The frame's title is rendered in
- * bold and the frame content is indented as suggested by the GNOME 
- * HIG (see http://developer.gnome.org/projects/gup/hig/).
- *
- * Return value: a new #GamesFrame widget
- *
- * Since: GNOME-GAMES 2.11
+ * Returns: a new #GamesFrame
  **/
 GtkWidget *
-games_frame_new (const gchar * label)
+games_frame_new (const char * label)
 {
-  GtkWidget *frame;
-  gboolean expander = FALSE;
-
-  /*  somewhat hackish, should perhaps be an object property of GamesFrame  */
-  if (label && strcmp (label, "<expander>") == 0) {
-    expander = TRUE;
-    label = NULL;
-  }
-
-  frame = g_object_new (GAMES_TYPE_FRAME, "label", label, NULL);
-
-  if (expander)
-    g_object_set_data (G_OBJECT (frame),
-		       GAMES_FRAME_IN_EXPANDER_KEY, (gpointer) TRUE);
-
-  return frame;
+  return g_object_new (GAMES_TYPE_FRAME,
+                       "label", label,
+                       NULL);
 }
 
+/**
+ * games_frame_set_label:
+ * @frame:
+ * @label:
+ *
+ * Sets @frame's title label.
+ */
 void
-games_frame_set_label (GamesFrame * frame, const gchar * label)
+games_frame_set_label (GamesFrame *frame,
+                       const char *label)
 {
-  gchar *markup;
-  GtkWidget *child;
-
-  g_return_if_fail (GTK_IS_FRAME (frame));
+  g_return_if_fail (GAMES_IS_FRAME (frame));
 
-  if (!label) {
-    gtk_frame_set_label_widget (GTK_FRAME (frame), NULL);
+  if (label) {
+    gtk_label_set_text (GTK_LABEL (frame->label), label);
   } else {
-    markup = g_strdup_printf ("<span weight=\"bold\">%s</span>", label);
-    child = gtk_label_new (markup);
-    g_free (markup);
-    gtk_label_set_use_markup (GTK_LABEL (child), TRUE);
-    gtk_misc_set_alignment (GTK_MISC (child), 0, 0.5);
-    gtk_widget_show (child);
-    gtk_frame_set_label_widget (GTK_FRAME (frame), child);
+    gtk_label_set_text (GTK_LABEL (frame->label), "");
   }
+
+  g_object_set (frame->label, "visible", label && label[0], NULL);
+
+  g_object_notify (G_OBJECT (frame), "label");
 }

Modified: trunk/libgames-support/games-frame.h
==============================================================================
--- trunk/libgames-support/games-frame.h	(original)
+++ trunk/libgames-support/games-frame.h	Tue Feb  3 14:39:13 2009
@@ -1,10 +1,11 @@
-/* GTK - The GIMP Toolkit
+/*
  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ * Copyright  2009 Christian Persch
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -17,8 +18,8 @@
  * Boston, MA 02111-1307, USA.
  */
 
-#ifndef __GAMES_FRAME_H__
-#define __GAMES_FRAME_H__
+#ifndef GAMES_FRAME_H
+#define GAMES_FRAME_H
 
 #include <gtk/gtk.h>
 
@@ -27,33 +28,30 @@
 #define GAMES_TYPE_FRAME            (games_frame_get_type ())
 #define GAMES_FRAME(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_FRAME, GamesFrame))
 #define GAMES_FRAME_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_FRAME, GamesFrameClass))
-#define IS_GAMES_FRAME(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_FRAME))
-#define IS_GAMES_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_FRAME))
+#define GAMES_IS_FRAME(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_FRAME))
+#define GAMES_IS_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_FRAME))
 #define GAMES_FRAME_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_FRAME, GamesFrameClass))
 
+typedef struct _GamesFrame      GamesFrame;
+typedef struct _GamesFrameClass GamesFrameClass;
 
-  typedef struct _GamesFrame GamesFrame;
-  typedef struct _GamesFrameClass GamesFrameClass;
+struct _GamesFrame {
+  GtkVBox parent_instance;
+  GtkWidget *label;
+  GtkWidget *alignment;
+};
 
-  struct _GamesFrame {
-    GtkFrame parent_instance;
-  };
-
-  struct _GamesFrameClass {
-    GtkFrameClass parent_class;
-
-    /* Padding for future expansion */
-    void (*_games_reserved1) (void);
-    void (*_games_reserved2) (void);
-    void (*_games_reserved3) (void);
-    void (*_games_reserved4) (void);
-  };
-
-
-  GType games_frame_get_type (void) G_GNUC_CONST;
-  GtkWidget *games_frame_new (const gchar * label);
-  void games_frame_set_label (GamesFrame * frame, const gchar * label);
+struct _GamesFrameClass {
+  GtkVBoxClass parent_class;
+};
+
+GType games_frame_get_type (void);
+
+GtkWidget *games_frame_new (const char *label);
+
+void games_frame_set_label (GamesFrame *frame,
+                            const char *label);
 
 G_END_DECLS
 
-#endif				/* __GAMES_FRAME_H__ */
+#endif /* !GAMES_FRAME_H */



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