1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
2 
3    caja-window-pane.c: Caja window pane
4 
5    Copyright (C) 2008 Free Software Foundation, Inc.
6 
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (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 GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public
18    License along with this program; if not, write to the
19    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20    Boston, MA 02110-1301, USA.
21 
22    Author: Holger Berndt <berndth@gmx.de>
23 */
24 
25 #include <eel/eel-gtk-macros.h>
26 
27 #include "caja-window-pane.h"
28 #include "caja-window-private.h"
29 #include "caja-navigation-window-pane.h"
30 #include "caja-window-manage-views.h"
31 
32 static void caja_window_pane_dispose    (GObject *object);
33 
G_DEFINE_TYPE(CajaWindowPane,caja_window_pane,G_TYPE_OBJECT)34 G_DEFINE_TYPE (CajaWindowPane,
35                caja_window_pane,
36                G_TYPE_OBJECT)
37 #define parent_class caja_window_pane_parent_class
38 
39 
40 static inline CajaWindowSlot *
41 get_first_inactive_slot (CajaWindowPane *pane)
42 {
43     GList *l;
44     CajaWindowSlot *slot = NULL;
45 
46     for (l = pane->slots; l != NULL; l = l->next)
47     {
48         slot = CAJA_WINDOW_SLOT (l->data);
49         if (slot != pane->active_slot)
50         {
51             return slot;
52         }
53     }
54 
55     return NULL;
56 }
57 
58 void
caja_window_pane_show(CajaWindowPane * pane)59 caja_window_pane_show (CajaWindowPane *pane)
60 {
61     pane->visible = TRUE;
62     EEL_CALL_METHOD (CAJA_WINDOW_PANE_CLASS, pane,
63                      show, (pane));
64 }
65 
66 void
caja_window_pane_zoom_in(CajaWindowPane * pane)67 caja_window_pane_zoom_in (CajaWindowPane *pane)
68 {
69     CajaWindowSlot *slot;
70 
71     g_assert (pane != NULL);
72 
73     caja_window_set_active_pane (pane->window, pane);
74 
75     slot = pane->active_slot;
76     if (slot->content_view != NULL)
77     {
78         caja_view_bump_zoom_level (slot->content_view, 1);
79     }
80 }
81 
82 void
caja_window_pane_zoom_to_level(CajaWindowPane * pane,CajaZoomLevel level)83 caja_window_pane_zoom_to_level (CajaWindowPane *pane,
84                                 CajaZoomLevel level)
85 {
86     CajaWindowSlot *slot;
87 
88     g_assert (pane != NULL);
89 
90     caja_window_set_active_pane (pane->window, pane);
91 
92     slot = pane->active_slot;
93     if (slot->content_view != NULL)
94     {
95         caja_view_zoom_to_level (slot->content_view, level);
96     }
97 }
98 
99 void
caja_window_pane_zoom_out(CajaWindowPane * pane)100 caja_window_pane_zoom_out (CajaWindowPane *pane)
101 {
102     CajaWindowSlot *slot;
103 
104     g_assert (pane != NULL);
105 
106     caja_window_set_active_pane (pane->window, pane);
107 
108     slot = pane->active_slot;
109     if (slot->content_view != NULL)
110     {
111         caja_view_bump_zoom_level (slot->content_view, -1);
112     }
113 }
114 
115 void
caja_window_pane_zoom_to_default(CajaWindowPane * pane)116 caja_window_pane_zoom_to_default (CajaWindowPane *pane)
117 {
118     CajaWindowSlot *slot;
119 
120     g_assert (pane != NULL);
121 
122     caja_window_set_active_pane (pane->window, pane);
123 
124     slot = pane->active_slot;
125     if (slot->content_view != NULL)
126     {
127         caja_view_restore_default_zoom_level (slot->content_view);
128     }
129 }
130 
131 void
caja_window_pane_slot_close(CajaWindowPane * pane,CajaWindowSlot * slot)132 caja_window_pane_slot_close (CajaWindowPane *pane, CajaWindowSlot *slot)
133 {
134     if (pane->window)
135     {
136         CajaWindow *window;
137         window = pane->window;
138         if (pane->active_slot == slot)
139         {
140             g_assert (pane->active_slots != NULL);
141             g_assert (pane->active_slots->data == slot);
142 
143             CajaWindowSlot *next_slot;
144 
145             next_slot = NULL;
146             if (pane->active_slots->next != NULL)
147             {
148                 next_slot = CAJA_WINDOW_SLOT (pane->active_slots->next->data);
149             }
150 
151             if (next_slot == NULL)
152             {
153                 next_slot = get_first_inactive_slot (CAJA_WINDOW_PANE (pane));
154             }
155 
156             caja_window_set_active_slot (window, next_slot);
157         }
158         caja_window_close_slot (slot);
159 
160         /* If that was the last slot in the active pane, close the pane or even the whole window. */
161         if (window->details->active_pane->slots == NULL)
162         {
163             CajaWindowPane *next_pane;
164             next_pane = caja_window_get_next_pane (window);
165 
166             /* If next_pane is non-NULL, we have more than one pane available. In this
167              * case, close the current pane and switch to the next one. If there is
168              * no next pane, close the window. */
169             if(next_pane)
170             {
171                 caja_window_close_pane (pane);
172                 caja_window_pane_switch_to (next_pane);
173                 if (CAJA_IS_NAVIGATION_WINDOW (window))
174                 {
175                     caja_navigation_window_update_show_hide_menu_items (CAJA_NAVIGATION_WINDOW (window));
176                 }
177             }
178             else
179             {
180                 caja_window_close (window);
181             }
182         }
183     }
184 }
185 
186 static void
real_sync_location_widgets(CajaWindowPane * pane)187 real_sync_location_widgets (CajaWindowPane *pane)
188 {
189     CajaWindowSlot *slot;
190 
191     /* TODO: Would be nice with a real subclass for spatial panes */
192     g_assert (CAJA_IS_SPATIAL_WINDOW (pane->window));
193 
194     slot = pane->active_slot;
195 
196     /* Change the location button to match the current location. */
197     caja_spatial_window_set_location_button (CAJA_SPATIAL_WINDOW (pane->window),
198             slot->location);
199 }
200 
201 
202 void
caja_window_pane_sync_location_widgets(CajaWindowPane * pane)203 caja_window_pane_sync_location_widgets (CajaWindowPane *pane)
204 {
205     EEL_CALL_METHOD (CAJA_WINDOW_PANE_CLASS, pane,
206                      sync_location_widgets, (pane));
207 }
208 
209 void
caja_window_pane_sync_search_widgets(CajaWindowPane * pane)210 caja_window_pane_sync_search_widgets (CajaWindowPane *pane)
211 {
212     g_assert (CAJA_IS_WINDOW_PANE (pane));
213 
214     EEL_CALL_METHOD (CAJA_WINDOW_PANE_CLASS, pane,
215                      sync_search_widgets, (pane));
216 }
217 
218 void
caja_window_pane_grab_focus(CajaWindowPane * pane)219 caja_window_pane_grab_focus (CajaWindowPane *pane)
220 {
221     if (CAJA_IS_WINDOW_PANE (pane) && pane->active_slot)
222     {
223         caja_view_grab_focus (pane->active_slot->content_view);
224     }
225 }
226 
227 void
caja_window_pane_switch_to(CajaWindowPane * pane)228 caja_window_pane_switch_to (CajaWindowPane *pane)
229 {
230     caja_window_pane_grab_focus (pane);
231 }
232 
233 static void
caja_window_pane_init(CajaWindowPane * pane)234 caja_window_pane_init (CajaWindowPane *pane)
235 {
236     pane->slots = NULL;
237     pane->active_slots = NULL;
238     pane->active_slot = NULL;
239     pane->is_active = FALSE;
240 }
241 
242 void
caja_window_pane_set_active(CajaWindowPane * pane,gboolean is_active)243 caja_window_pane_set_active (CajaWindowPane *pane, gboolean is_active)
244 {
245     if (is_active == pane->is_active)
246     {
247         return;
248     }
249 
250     pane->is_active = is_active;
251 
252     /* notify the current slot about its activity state (so that it can e.g. modify the bg color) */
253     caja_window_slot_is_in_active_pane (pane->active_slot, is_active);
254 
255     EEL_CALL_METHOD (CAJA_WINDOW_PANE_CLASS, pane,
256                      set_active, (pane, is_active));
257 }
258 
259 static void
caja_window_pane_class_init(CajaWindowPaneClass * class)260 caja_window_pane_class_init (CajaWindowPaneClass *class)
261 {
262     G_OBJECT_CLASS (class)->dispose = caja_window_pane_dispose;
263     CAJA_WINDOW_PANE_CLASS (class)->sync_location_widgets = real_sync_location_widgets;
264 }
265 
266 static void
caja_window_pane_dispose(GObject * object)267 caja_window_pane_dispose (GObject *object)
268 {
269     CajaWindowPane *pane = CAJA_WINDOW_PANE (object);
270 
271     g_assert (pane->slots == NULL);
272 
273     pane->window = NULL;
274     G_OBJECT_CLASS (parent_class)->dispose (object);
275 }
276 
277 CajaWindowPane *
caja_window_pane_new(CajaWindow * window)278 caja_window_pane_new (CajaWindow *window)
279 {
280     CajaWindowPane *pane;
281 
282     pane = g_object_new (CAJA_TYPE_WINDOW_PANE, NULL);
283     pane->window = window;
284     return pane;
285 }
286 
287 CajaWindowSlot *
caja_window_pane_get_slot_for_content_box(CajaWindowPane * pane,GtkWidget * content_box)288 caja_window_pane_get_slot_for_content_box (CajaWindowPane *pane,
289         GtkWidget *content_box)
290 {
291     GList *l;
292     CajaWindowSlot *slot = NULL;
293 
294     for (l = pane->slots; l != NULL; l = l->next)
295     {
296         slot = CAJA_WINDOW_SLOT (l->data);
297 
298         if (slot->content_box == content_box)
299         {
300             return slot;
301         }
302     }
303     return NULL;
304 }
305