1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpcontainerbox.c
5  * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <string.h>
24 
25 #include <gegl.h>
26 #include <gtk/gtk.h>
27 
28 #include "libgimpwidgets/gimpwidgets.h"
29 
30 #include "widgets-types.h"
31 
32 #include "core/gimpcontainer.h"
33 #include "core/gimpcontext.h"
34 
35 #include "gimpcontainerbox.h"
36 #include "gimpcontainerview.h"
37 #include "gimpdnd.h"
38 #include "gimpdocked.h"
39 #include "gimppropwidgets.h"
40 #include "gimpview.h"
41 #include "gimpviewrenderer.h"
42 
43 
44 static void   gimp_container_box_view_iface_init   (GimpContainerViewInterface *iface);
45 static void   gimp_container_box_docked_iface_init (GimpDockedInterface *iface);
46 
47 static void   gimp_container_box_constructed       (GObject      *object);
48 
49 static GtkWidget * gimp_container_box_get_preview  (GimpDocked   *docked,
50                                                     GimpContext  *context,
51                                                     GtkIconSize   size);
52 static void        gimp_container_box_set_context  (GimpDocked   *docked,
53                                                     GimpContext  *context);
54 
55 
G_DEFINE_TYPE_WITH_CODE(GimpContainerBox,gimp_container_box,GIMP_TYPE_EDITOR,G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONTAINER_VIEW,gimp_container_box_view_iface_init)G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCKED,gimp_container_box_docked_iface_init))56 G_DEFINE_TYPE_WITH_CODE (GimpContainerBox, gimp_container_box,
57                          GIMP_TYPE_EDITOR,
58                          G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONTAINER_VIEW,
59                                                 gimp_container_box_view_iface_init)
60                          G_IMPLEMENT_INTERFACE (GIMP_TYPE_DOCKED,
61                                                 gimp_container_box_docked_iface_init))
62 
63 #define parent_class gimp_container_box_parent_class
64 
65 
66 static void
67 gimp_container_box_class_init (GimpContainerBoxClass *klass)
68 {
69   GObjectClass *object_class = G_OBJECT_CLASS (klass);
70 
71   object_class->constructed  = gimp_container_box_constructed;
72   object_class->set_property = gimp_container_view_set_property;
73   object_class->get_property = gimp_container_view_get_property;
74 
75   gimp_container_view_install_properties (object_class);
76 }
77 
78 static void
gimp_container_box_init(GimpContainerBox * box)79 gimp_container_box_init (GimpContainerBox *box)
80 {
81   GtkWidget *sb;
82 
83   box->scrolled_win = gtk_scrolled_window_new (NULL, NULL);
84   gtk_box_pack_start (GTK_BOX (box), box->scrolled_win, TRUE, TRUE, 0);
85   gtk_widget_show (box->scrolled_win);
86 
87   sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (box->scrolled_win));
88 
89   gtk_widget_set_can_focus (sb, FALSE);
90 }
91 
92 static void
gimp_container_box_view_iface_init(GimpContainerViewInterface * iface)93 gimp_container_box_view_iface_init (GimpContainerViewInterface *iface)
94 {
95 }
96 
97 static void
gimp_container_box_docked_iface_init(GimpDockedInterface * iface)98 gimp_container_box_docked_iface_init (GimpDockedInterface *iface)
99 {
100   iface->get_preview = gimp_container_box_get_preview;
101   iface->set_context = gimp_container_box_set_context;
102 }
103 
104 static void
gimp_container_box_constructed(GObject * object)105 gimp_container_box_constructed (GObject *object)
106 {
107   GimpContainerBox *box = GIMP_CONTAINER_BOX (object);
108 
109   /* This is evil: the hash table of "insert_data" is created on
110    * demand when GimpContainerView API is used, using a
111    * value_free_func that is set in the interface_init functions of
112    * its implementors. Therefore, no GimpContainerView API must be
113    * called from any init() function, because the interface_init()
114    * function of a subclass that sets the right value_free_func might
115    * not have been called yet, leaving the insert_data hash table
116    * without memory management.
117    *
118    * Call GimpContainerView API from GObject::constructed() instead,
119    * which runs after everything is set up correctly.
120    */
121   gimp_container_view_set_dnd_widget (GIMP_CONTAINER_VIEW (box),
122                                       box->scrolled_win);
123 
124   G_OBJECT_CLASS (parent_class)->constructed (object);
125 }
126 
127 void
gimp_container_box_set_size_request(GimpContainerBox * box,gint width,gint height)128 gimp_container_box_set_size_request (GimpContainerBox *box,
129                                      gint              width,
130                                      gint              height)
131 {
132   GimpContainerView      *view;
133   GtkScrolledWindowClass *sw_class;
134   GtkStyle               *sw_style;
135   GtkWidget              *sb;
136   GtkRequisition          req;
137   gint                    view_size;
138   gint                    scrollbar_width;
139   gint                    border_x;
140   gint                    border_y;
141 
142   g_return_if_fail (GIMP_IS_CONTAINER_BOX (box));
143 
144   view = GIMP_CONTAINER_VIEW (box);
145 
146   view_size = gimp_container_view_get_view_size (view, NULL);
147 
148   g_return_if_fail (width  <= 0 || width  >= view_size);
149   g_return_if_fail (height <= 0 || height >= view_size);
150 
151   sw_class = GTK_SCROLLED_WINDOW_GET_CLASS (box->scrolled_win);
152 
153   if (sw_class->scrollbar_spacing >= 0)
154     scrollbar_width = sw_class->scrollbar_spacing;
155   else
156     gtk_widget_style_get (GTK_WIDGET (box->scrolled_win),
157                           "scrollbar-spacing", &scrollbar_width,
158                           NULL);
159 
160   sb = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (box->scrolled_win));
161 
162   gtk_widget_size_request (sb, &req);
163   scrollbar_width += req.width;
164 
165   border_x = border_y = gtk_container_get_border_width (GTK_CONTAINER (box));
166 
167   sw_style = gtk_widget_get_style (box->scrolled_win);
168 
169   border_x += sw_style->xthickness * 2 + scrollbar_width;
170   border_y += sw_style->ythickness * 2;
171 
172   gtk_widget_set_size_request (box->scrolled_win,
173                                width  > 0 ? width  + border_x : -1,
174                                height > 0 ? height + border_y : -1);
175 }
176 
177 static void
gimp_container_box_set_context(GimpDocked * docked,GimpContext * context)178 gimp_container_box_set_context (GimpDocked  *docked,
179                                 GimpContext *context)
180 {
181   gimp_container_view_set_context (GIMP_CONTAINER_VIEW (docked), context);
182 }
183 
184 static GtkWidget *
gimp_container_box_get_preview(GimpDocked * docked,GimpContext * context,GtkIconSize size)185 gimp_container_box_get_preview (GimpDocked   *docked,
186                                 GimpContext  *context,
187                                 GtkIconSize   size)
188 {
189   GimpContainerBox  *box  = GIMP_CONTAINER_BOX (docked);
190   GimpContainerView *view = GIMP_CONTAINER_VIEW (docked);
191   GimpContainer     *container;
192   GtkWidget         *preview;
193   gint               width;
194   gint               height;
195   gint               border_width = 1;
196   const gchar       *prop_name;
197 
198   container = gimp_container_view_get_container (view);
199 
200   g_return_val_if_fail (container != NULL, NULL);
201 
202   gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (GTK_WIDGET (box)),
203                                      size, &width, &height);
204 
205   prop_name = gimp_context_type_to_prop_name (gimp_container_get_children_type (container));
206 
207   preview = gimp_prop_view_new (G_OBJECT (context), prop_name,
208                                 context, height);
209   GIMP_VIEW (preview)->renderer->size = -1;
210 
211   gimp_container_view_get_view_size (view, &border_width);
212 
213   border_width = MIN (1, border_width);
214 
215   gimp_view_renderer_set_size_full (GIMP_VIEW (preview)->renderer,
216                                     width, height, border_width);
217 
218   return preview;
219 }
220