1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimpdocumentview.c
5  * Copyright (C) 2001 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 <gegl.h>
24 #include <gtk/gtk.h>
25 
26 #include "libgimpwidgets/gimpwidgets.h"
27 
28 #include "widgets-types.h"
29 
30 #include "core/gimp.h"
31 #include "core/gimpcontainer.h"
32 #include "core/gimpcontext.h"
33 #include "core/gimpimagefile.h"
34 
35 #include "gimpcontainerview.h"
36 #include "gimpdocumentview.h"
37 #include "gimpdnd.h"
38 #include "gimpeditor.h"
39 #include "gimpmenufactory.h"
40 #include "gimpuimanager.h"
41 #include "gimpviewrenderer.h"
42 #include "gimpwidgets-utils.h"
43 
44 #include "gimp-intl.h"
45 
46 
47 static void    gimp_document_view_activate_item (GimpContainerEditor *editor,
48                                                  GimpViewable        *viewable);
49 static GList * gimp_document_view_drag_uri_list (GtkWidget           *widget,
50                                                  gpointer             data);
51 
52 
G_DEFINE_TYPE(GimpDocumentView,gimp_document_view,GIMP_TYPE_CONTAINER_EDITOR)53 G_DEFINE_TYPE (GimpDocumentView, gimp_document_view,
54                GIMP_TYPE_CONTAINER_EDITOR)
55 
56 #define parent_class gimp_document_view_parent_class
57 
58 
59 static void
60 gimp_document_view_class_init (GimpDocumentViewClass *klass)
61 {
62   GimpContainerEditorClass *editor_class = GIMP_CONTAINER_EDITOR_CLASS (klass);
63 
64   editor_class->activate_item = gimp_document_view_activate_item;
65 }
66 
67 static void
gimp_document_view_init(GimpDocumentView * view)68 gimp_document_view_init (GimpDocumentView *view)
69 {
70   view->open_button    = NULL;
71   view->remove_button  = NULL;
72   view->refresh_button = NULL;
73 }
74 
75 GtkWidget *
gimp_document_view_new(GimpViewType view_type,GimpContainer * container,GimpContext * context,gint view_size,gint view_border_width,GimpMenuFactory * menu_factory)76 gimp_document_view_new (GimpViewType     view_type,
77                         GimpContainer   *container,
78                         GimpContext     *context,
79                         gint             view_size,
80                         gint             view_border_width,
81                         GimpMenuFactory *menu_factory)
82 {
83   GimpDocumentView    *document_view;
84   GimpContainerEditor *editor;
85 
86   g_return_val_if_fail (GIMP_IS_CONTAINER (container), NULL);
87   g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
88   g_return_val_if_fail (view_size > 0 &&
89                         view_size <= GIMP_VIEWABLE_MAX_PREVIEW_SIZE, FALSE);
90   g_return_val_if_fail (view_border_width >= 0 &&
91                         view_border_width <= GIMP_VIEW_MAX_BORDER_WIDTH,
92                         FALSE);
93   g_return_val_if_fail (menu_factory == NULL ||
94                         GIMP_IS_MENU_FACTORY (menu_factory), NULL);
95 
96   document_view = g_object_new (GIMP_TYPE_DOCUMENT_VIEW,
97                                 "view-type",         view_type,
98                                 "container",         container,
99                                 "context",           context,
100                                 "view-size",         view_size,
101                                 "view-border-width", view_border_width,
102                                 "menu-factory",      menu_factory,
103                                 "menu-identifier",   "<Documents>",
104                                 "ui-path",           "/documents-popup",
105                                 NULL);
106 
107   editor = GIMP_CONTAINER_EDITOR (document_view);
108 
109   document_view->open_button =
110     gimp_editor_add_action_button (GIMP_EDITOR (editor->view), "documents",
111                                    "documents-open",
112                                    "documents-raise-or-open",
113                                    GDK_SHIFT_MASK,
114                                    "documents-file-open-dialog",
115                                    gimp_get_toggle_behavior_mask (),
116                                    NULL);
117   gimp_container_view_enable_dnd (editor->view,
118                                   GTK_BUTTON (document_view->open_button),
119                                   GIMP_TYPE_IMAGEFILE);
120 
121   document_view->remove_button =
122     gimp_editor_add_action_button (GIMP_EDITOR (editor->view), "documents",
123                                    "documents-remove", NULL);
124   gimp_container_view_enable_dnd (editor->view,
125                                   GTK_BUTTON (document_view->remove_button),
126                                   GIMP_TYPE_IMAGEFILE);
127 
128   gimp_editor_add_action_button (GIMP_EDITOR (editor->view), "documents",
129                                  "documents-clear", NULL);
130 
131   document_view->refresh_button =
132     gimp_editor_add_action_button (GIMP_EDITOR (editor->view), "documents",
133                                    "documents-recreate-preview",
134                                    "documents-reload-previews",
135                                    GDK_SHIFT_MASK,
136                                    "documents-remove-dangling",
137                                    gimp_get_toggle_behavior_mask (),
138                                    NULL);
139 
140   if (view_type == GIMP_VIEW_TYPE_LIST)
141     {
142       GtkWidget *dnd_widget;
143 
144       dnd_widget = gimp_container_view_get_dnd_widget (editor->view);
145 
146       gimp_dnd_uri_list_source_add (dnd_widget,
147                                     gimp_document_view_drag_uri_list,
148                                     editor);
149     }
150 
151   gimp_ui_manager_update (gimp_editor_get_ui_manager (GIMP_EDITOR (editor->view)),
152                           editor);
153 
154   return GTK_WIDGET (document_view);
155 }
156 
157 static void
gimp_document_view_activate_item(GimpContainerEditor * editor,GimpViewable * viewable)158 gimp_document_view_activate_item (GimpContainerEditor *editor,
159                                   GimpViewable        *viewable)
160 {
161   GimpDocumentView *view = GIMP_DOCUMENT_VIEW (editor);
162   GimpContainer    *container;
163 
164   if (GIMP_CONTAINER_EDITOR_CLASS (parent_class)->activate_item)
165     GIMP_CONTAINER_EDITOR_CLASS (parent_class)->activate_item (editor, viewable);
166 
167   container = gimp_container_view_get_container (editor->view);
168 
169   if (viewable && gimp_container_have (container, GIMP_OBJECT (viewable)))
170     {
171       gtk_button_clicked (GTK_BUTTON (view->open_button));
172     }
173 }
174 
175 static GList *
gimp_document_view_drag_uri_list(GtkWidget * widget,gpointer data)176 gimp_document_view_drag_uri_list (GtkWidget *widget,
177                                   gpointer   data)
178 {
179   GimpViewable *viewable = gimp_dnd_get_drag_data (widget);
180 
181   if (viewable)
182     {
183       const gchar *uri = gimp_object_get_name (viewable);
184 
185       return g_list_append (NULL, g_strdup (uri));
186     }
187 
188   return NULL;
189 }
190