gimpcontainerbox.c 8.24 KB
Newer Older
1 2 3 4
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcontainerbox.c
5
 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "widgets-types.h"

#include "core/gimpcontainer.h"
#include "core/gimpcontext.h"

#include "gimpcontainerbox.h"
36
#include "gimpcontainerview.h"
37 38
#include "gimpdnd.h"
#include "gimpdocked.h"
39
#include "gimppropwidgets.h"
40
#include "gimpview.h"
41
#include "gimpviewrenderer.h"
42 43 44 45


static void   gimp_container_box_class_init      (GimpContainerBoxClass *klass);
static void   gimp_container_box_init            (GimpContainerBox      *box);
46
static void   gimp_container_box_view_iface_init   (GimpContainerViewInterface *view_iface);
47 48
static void   gimp_container_box_docked_iface_init (GimpDockedInterface *docked_iface);

49 50 51 52
static GtkWidget * gimp_container_box_get_preview  (GimpDocked   *docked,
                                                    GimpContext  *context,
                                                    GtkIconSize   size);
static void        gimp_container_box_set_context  (GimpDocked   *docked,
53
                                                    GimpContext  *context);
54 55


56
static GimpEditorClass *parent_class = NULL;
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78


GType
gimp_container_box_get_type (void)
{
  static GType type = 0;

  if (! type)
    {
      static const GTypeInfo box_info =
      {
        sizeof (GimpContainerBoxClass),
        NULL,           /* base_init */
        NULL,           /* base_finalize */
        (GClassInitFunc) gimp_container_box_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GimpContainerBox),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_container_box_init,
      };

79 80 81 82 83 84
      static const GInterfaceInfo view_iface_info =
      {
        (GInterfaceInitFunc) gimp_container_box_view_iface_init,
        NULL,           /* iface_finalize */
        NULL            /* iface_data     */
      };
85 86 87 88 89 90 91
      static const GInterfaceInfo docked_iface_info =
      {
        (GInterfaceInitFunc) gimp_container_box_docked_iface_init,
        NULL,           /* iface_finalize */
        NULL            /* iface_data     */
      };

92
      type = g_type_register_static (GIMP_TYPE_EDITOR,
93 94 95
                                     "GimpContainerBox",
                                     &box_info, 0);

96 97
      g_type_add_interface_static (type, GIMP_TYPE_CONTAINER_VIEW,
                                   &view_iface_info);
98 99 100 101 102 103 104 105 106 107
      g_type_add_interface_static (type, GIMP_TYPE_DOCKED,
                                   &docked_iface_info);
    }

  return type;
}

static void
gimp_container_box_class_init (GimpContainerBoxClass *klass)
{
108 109
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

110
  parent_class = g_type_class_peek_parent (klass);
111

112 113
  object_class->set_property = gimp_container_view_set_property;
  object_class->get_property = gimp_container_view_get_property;
114

115 116
  g_object_class_override_property (object_class,
                                    GIMP_CONTAINER_VIEW_PROP_CONTAINER,
117
                                    "container");
118 119
  g_object_class_override_property (object_class,
                                    GIMP_CONTAINER_VIEW_PROP_CONTEXT,
120
                                    "context");
121 122
  g_object_class_override_property (object_class,
                                    GIMP_CONTAINER_VIEW_PROP_REORDERABLE,
123
                                    "reorderable");
124 125 126 127 128 129
  g_object_class_override_property (object_class,
                                    GIMP_CONTAINER_VIEW_PROP_PREVIEW_SIZE,
                                    "preview-size");
  g_object_class_override_property (object_class,
                                    GIMP_CONTAINER_VIEW_PROP_PREVIEW_BORDER_WIDTH,
                                    "preview-border-width");
130 131 132 133 134
}

static void
gimp_container_box_init (GimpContainerBox *box)
{
135
  GimpContainerView *view = GIMP_CONTAINER_VIEW (box);
136 137 138 139 140 141 142 143

  box->scrolled_win = gtk_scrolled_window_new (NULL, NULL);
  gtk_box_pack_start (GTK_BOX (box), box->scrolled_win, TRUE, TRUE, 0);
  gtk_widget_show (box->scrolled_win);

  GTK_WIDGET_UNSET_FLAGS (GTK_SCROLLED_WINDOW (box->scrolled_win)->vscrollbar,
                          GTK_CAN_FOCUS);

144 145 146 147 148 149
  gimp_container_view_set_dnd_widget (view, box->scrolled_win);
}

static void
gimp_container_box_view_iface_init (GimpContainerViewInterface *view_iface)
{
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

static void
gimp_container_box_docked_iface_init (GimpDockedInterface *docked_iface)
{
  docked_iface->get_preview = gimp_container_box_get_preview;
  docked_iface->set_context = gimp_container_box_set_context;
}

void
gimp_container_box_set_size_request (GimpContainerBox *box,
                                     gint              width,
                                     gint              height)
{
  GimpContainerView      *view;
  GtkScrolledWindowClass *sw_class;
  GtkRequisition          req;
167
  gint                    preview_size;
168 169 170 171 172 173 174 175
  gint                    scrollbar_width;
  gint                    border_x;
  gint                    border_y;

  g_return_if_fail (GIMP_IS_CONTAINER_BOX (box));

  view = GIMP_CONTAINER_VIEW (box);

176 177 178 179
  preview_size = gimp_container_view_get_preview_size (view, NULL);

  g_return_if_fail (width  <= 0 || width  >= preview_size);
  g_return_if_fail (height <= 0 || height >= preview_size);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

  sw_class = GTK_SCROLLED_WINDOW_GET_CLASS (box->scrolled_win);

  if (sw_class->scrollbar_spacing >= 0)
    scrollbar_width = sw_class->scrollbar_spacing;
  else
    gtk_widget_style_get (GTK_WIDGET (box->scrolled_win),
                          "scrollbar_spacing", &scrollbar_width,
                          NULL);

  gtk_widget_size_request (GTK_SCROLLED_WINDOW (box->scrolled_win)->vscrollbar,
                           &req);
  scrollbar_width += req.width;

  border_x = box->scrolled_win->style->xthickness * 2 + scrollbar_width;
  border_y = box->scrolled_win->style->ythickness * 2;

  gtk_widget_set_size_request (box->scrolled_win,
                               width  > 0 ? width  + border_x : -1,
                               height > 0 ? height + border_y : -1);
}

static void
gimp_container_box_set_context (GimpDocked  *docked,
204
                                GimpContext *context)
205 206 207 208 209 210 211 212 213 214 215
{
  gimp_container_view_set_context (GIMP_CONTAINER_VIEW (docked), context);
}

static GtkWidget *
gimp_container_box_get_preview (GimpDocked   *docked,
                                GimpContext  *context,
                                GtkIconSize   size)
{
  GimpContainerBox  *box  = GIMP_CONTAINER_BOX (docked);
  GimpContainerView *view = GIMP_CONTAINER_VIEW (docked);
216
  GimpContainer     *container;
217 218 219 220 221 222 223
  GtkWidget         *preview;
  GdkScreen         *screen;
  gint               width;
  gint               height;
  gint               border_width = 1;
  const gchar       *prop_name;

224 225 226
  container = gimp_container_view_get_container (view);

  g_return_val_if_fail (container != NULL, NULL);
227 228 229 230 231

  screen = gtk_widget_get_screen (GTK_WIDGET (box));
  gtk_icon_size_lookup_for_settings (gtk_settings_get_for_screen (screen),
                                     size, &width, &height);

232
  prop_name = gimp_context_type_to_prop_name (container->children_type);
233 234

  preview = gimp_prop_preview_new (G_OBJECT (context), prop_name, height);
235
  GIMP_VIEW (preview)->renderer->size = -1;
236 237 238 239 240

  gimp_container_view_get_preview_size (view, &border_width);

  border_width = MIN (1, border_width);

241 242
  gimp_view_renderer_set_size_full (GIMP_VIEW (preview)->renderer,
                                    width, height, border_width);
243 244 245

  return preview;
}