Debian Codegraph @

Global-Menu/src/applet-notifications.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
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
9 * as published by the Free Software Foundation; either version 3
10 * of the 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
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include "gdk/gdkx.h"
23
24 #include "applet-struct.h"
25 #include "applet-draw.h"
26 #include "applet-app.h"
27 #include "applet-notifications.h"
28
29 #define FORCE_REMOVE_DOUBLE_SEPARATORS
30
31 #ifdef FORCE_REMOVE_DOUBLE_SEPARATORS
32 /// REMOVE ME WHEN IT'S POSSIBLE! :)
33 static void _remove_double_separators (GtkWidget *pWidget)
34 {
35 if (pWidget == NULL)
36 return;
37
38 gboolean bPrevIsSeparator = TRUE; // to remove the first entry if it's a separator
39
40 GList *pChildren = gtk_container_get_children (GTK_CONTAINER (pWidget));
41 GList *ic;
42 GtkWidget *pCurrentWidget;
43 GtkWidget *pSubMenu;
44 for (ic = pChildren; ic != NULL; ic = ic->next)
45 {
46 pCurrentWidget = ic->data;
47 if (GTK_IS_SEPARATOR_MENU_ITEM (pCurrentWidget))
48 {
49 if (bPrevIsSeparator)
50 gtk_widget_destroy (pCurrentWidget); // or ? gtk_container_remove (pContainer, pCurrentWidget);
51 bPrevIsSeparator = TRUE;
52 }
53 else if (GTK_IS_MENU_ITEM (pCurrentWidget))
54 {
55 pSubMenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (pCurrentWidget));
56 if (pSubMenu != NULL)
57 {
58 bPrevIsSeparator = TRUE;
59 _remove_double_separators (pSubMenu);
60 }
61 else
62 bPrevIsSeparator = FALSE;
63 }
64 else
65 bPrevIsSeparator = FALSE;
66 }
67 g_list_free (pChildren);
68 }
69 #endif
70
71 static void _show_menu (gboolean bOnMouse)
72 {
73 if (! myConfig.bDisplayMenu)
74 return;
75
76 if (myData.pMenu != NULL)
77 {
78 #ifdef FORCE_REMOVE_DOUBLE_SEPARATORS
79 _remove_double_separators (GTK_WIDGET (myData.pMenu));
80 #endif
81 if (bOnMouse)
82 {
83 gtk_widget_show_all (GTK_WIDGET (myData.pMenu));
84 gtk_menu_popup (GTK_MENU (myData.pMenu),
85 NULL,
86 NULL,
87 (GtkMenuPositionFunc) NULL,
88 NULL,
89 0,
90 gtk_get_current_event_time ());
91 }
92 else
93 {
94 CD_APPLET_POPUP_MENU_ON_MY_ICON (GTK_WIDGET (myData.pMenu));
95 }
96 }
97 else /// either show a message, or remember the user demand, so that we pop the menu as soon as we get it...
98 {
99 gldi_dialog_show_temporary_with_icon (D_("The application didn't send its menu to us."), myIcon, myContainer, 4000., "same icon");
100 }
101 }
102
103 //\___________ Action on click: show the menu
104 CD_APPLET_ON_CLICK_BEGIN
105 if (myData.pCurrentWindow == NULL)
106 return GLDI_NOTIFICATION_LET_PASS;
107 if (myConfig.bDisplayControls)
108 {
109 int iNumButton = cd_app_menu_find_button (myApplet);
110 if (iNumButton >= 0)
111 {
112 switch (iNumButton)
113 {
114 case CD_BUTTON_MENU:
115 _show_menu (FALSE);
116 break;
117 case CD_BUTTON_MINIMIZE:
118 if (myData.bCanMinimize)
119 gldi_window_minimize (myData.pCurrentWindow);
120 break;
121 case CD_BUTTON_MAXIMIZE:
122 if (myData.bCanMaximize)
123 {
124 gldi_window_maximize (myData.pCurrentWindow, ! myData.pCurrentWindow->bIsMaximized);
125 }
126 break;
127 case CD_BUTTON_CLOSE:
128 if (myData.bCanClose)
129 gldi_window_close (myData.pCurrentWindow);
130 break;
131 }
132 }
133 }
134 else if (myConfig.bDisplayMenu)
135 _show_menu (FALSE);
136 CD_APPLET_ON_CLICK_END
137
138
139 //\___________ Other actions are defined to mime the usual actions available on windows top border
140 CD_APPLET_ON_MIDDLE_CLICK_BEGIN
141 // set the window behind all the others.
142 GldiWindowActor *actor = gldi_windows_get_active();
143 if (actor)
144 gldi_window_lower (actor);
145 CD_APPLET_ON_MIDDLE_CLICK_END
146
147
148 CD_APPLET_ON_SCROLL_BEGIN
149 // minimize the window (we could also use the scroll to (un)shade the window, but I'm afraid that a maximized shaded window would be too much hidden, users could be confused).
150 GldiWindowActor *actor = gldi_windows_get_active();
151 if (actor && CD_APPLET_SCROLL_DOWN)
152 gldi_window_minimize (actor);
153 CD_APPLET_ON_SCROLL_END
154
155
156 CD_APPLET_ON_DOUBLE_CLICK_BEGIN
157 // maximize/restaure the window.
158 if (myData.pCurrentWindow != 0)
159 {
160 gldi_window_maximize (myData.pCurrentWindow, ! myData.pCurrentWindow->bIsMaximized);
161 }
162 CD_APPLET_ON_DOUBLE_CLICK_END
163
164
165 /*
166 CD_APPLET_ON_BUILD_MENU_BEGIN
167 // nothing to do here, since the icon is considered as an appli, the dock will fill it for free !
168 CD_APPLET_ON_BUILD_MENU_END
169 */
170
171 void cd_app_menu_on_keybinding_pull (const gchar *keystring, GldiModuleInstance *myApplet)
172 {
173 CD_APPLET_ENTER;
174 _show_menu (myConfig.bMenuOnMouse);
175 CD_APPLET_LEAVE();
176 }
177
178
179 //\___________ Other notifications, that are not from the user (but from the Applications-manager)
180 static void _check_dock_is_active (gchar *cDockName, CairoDock *pDock, gboolean *data)
181 {
182 if (gldi_container_is_active (CAIRO_CONTAINER (pDock)))
183 *data = 1;
184 }
185 gboolean cd_app_menu_on_active_window_changed (GldiModuleInstance *myApplet, GldiWindowActor *actor)
186 {
187 // check if a dock has the focus (we don't want to control the dock, it wouldn't make sense anyway).
188 if (actor)
189 {
190 gboolean is_dock = FALSE;
191 gldi_docks_foreach ((GHFunc) _check_dock_is_active, &is_dock);
192 if (is_dock) // it's a dock, ignore it.
193 actor = NULL;
194 }
195
196 // take this new window (possibly NULL).
197 cd_app_menu_set_current_window (actor);
198
199 return GLDI_NOTIFICATION_LET_PASS;
200 }
201
202 gboolean cd_app_menu_on_state_changed (GldiModuleInstance *myApplet, GldiWindowActor *actor, gboolean bHiddenChanged, gboolean bMaximizedChanged, gboolean bFullScreenChanged)
203 {
204 if (actor == myData.pCurrentWindow)
205 {
206 if (bMaximizedChanged)
207 {
208 gldi_window_set_border (actor, ! actor->bIsMaximized);
209 cd_app_menu_redraw_buttons ();
210 }
211 }
212 return GLDI_NOTIFICATION_LET_PASS;
213 }
214
215 gboolean cd_app_menu_on_name_changed (GldiModuleInstance *myApplet, GldiWindowActor *actor)
216 {
217 if (actor == myData.pCurrentWindow)
218 {
219 CD_APPLET_SET_NAME_FOR_MY_ICON (actor->cName);
220 }
221 return GLDI_NOTIFICATION_LET_PASS;
222 }
223
224 gboolean cd_app_menu_on_new_appli (GldiModuleInstance *myApplet, GldiWindowActor *actor)
225 {
226 if (actor->bIsMaximized)
227 {
228 gldi_window_set_border (actor, ! actor->bIsMaximized);
229 }
230 return GLDI_NOTIFICATION_LET_PASS;
231 }
232
233
234 //\___________ Other notifications, for animation of the buttons.
235
236 gboolean on_mouse_moved (GldiModuleInstance *myApplet, GldiContainer *pContainer, gboolean *bStartAnimation)
237 {
238 CD_APPLET_ENTER;
239 if (! myIcon->bPointed || ! pContainer->bInside)
240 CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
241
242 // find the pointed button
243 int iNumButton = cd_app_menu_find_button (myApplet);
244 if (iNumButton >= 0 && iNumButton < myData.iNbButtons)
245 {
246 // trigger animation
247 *bStartAnimation = TRUE;
248 }
249
250 CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
251 }
252
253
254 static gboolean _update_button_image_no_loop (CairoDockImageBuffer *pImage, int *iStep)
255 {
256 gboolean bButtonAnimating = FALSE;
257 if (cairo_dock_image_buffer_is_animated (pImage))
258 {
259 if (pImage->iCurrentFrame != 0) // in the loop
260 {
261 gboolean bLastFrame = cairo_dock_image_buffer_next_frame_no_loop (pImage);
262 if (bLastFrame)
263 pImage->iCurrentFrame = 0;
264 else
265 bButtonAnimating = TRUE;
266 }
267 }
268 else // update the step
269 {
270 if (*iStep != 0) // in the loop
271 {
272 ++ *iStep;
273 if (*iStep >= CD_ANIM_STEPS)
274 *iStep = 0;
275 else
276 bButtonAnimating = TRUE;
277 }
278 }
279 return bButtonAnimating;
280 }
281
282 static void _update_button_image_loop (CairoDockImageBuffer *pImage, int *iStep)
283 {
284 if (cairo_dock_image_buffer_is_animated (pImage))
285 {
286 cairo_dock_image_buffer_next_frame (pImage);
287 }
288 else // update the step
289 {
290 ++ *iStep;
291 if (*iStep >= CD_ANIM_STEPS)
292 *iStep = 0;
293 }
294 }
295
296 static gboolean _update_button_image (CairoDockImageBuffer *pImage, int *iStep, gboolean bLoop)
297 {
298 if (bLoop)
299 {
300 _update_button_image_loop (pImage, iStep);
301 return TRUE;
302 }
303 else
304 {
305 return _update_button_image_no_loop (pImage, iStep);
306 }
307 }
308
309 gboolean cd_app_menu_on_update_container (GldiModuleInstance *myApplet, GldiContainer *pContainer, gboolean *bContinueAnimation)
310 {
311 CD_APPLET_ENTER;
312
313 if (! myIcon->bPointed || ! pContainer->bInside) // our icon is not pointed, only update it to finish the current button animations.
314 {
315 if (myData.bButtonAnimating) // one or more button is animating.
316 {
317 myData.bButtonAnimating = FALSE;
318
319 myData.bButtonAnimating |= _update_button_image_no_loop (&myData.minimizeButton, &myData.iAnimIterMin);
320
321 myData.bButtonAnimating |= _update_button_image_no_loop (&myData.maximizeButton, &myData.iAnimIterMax);
322
323 myData.bButtonAnimating |= _update_button_image_no_loop (&myData.restoreButton, &myData.iAnimIterRestore);
324
325 myData.bButtonAnimating |= _update_button_image_no_loop (&myData.closeButton, &myData.iAnimIterClose);
326
327 cd_app_menu_redraw_buttons ();
328 }
329 }
330 else // our button is currently pointed.
331 {
332 myData.bButtonAnimating = FALSE;
333 int iNumButton = cd_app_menu_find_button (myApplet);
334
335 myData.bButtonAnimating |= _update_button_image (&myData.minimizeButton, &myData.iAnimIterMin, iNumButton == CD_BUTTON_MINIMIZE);
336
337 myData.bButtonAnimating |= _update_button_image (&myData.maximizeButton, &myData.iAnimIterMax, iNumButton == CD_BUTTON_MAXIMIZE);
338
339 myData.bButtonAnimating |= _update_button_image (&myData.restoreButton, &myData.iAnimIterRestore, iNumButton == CD_BUTTON_MAXIMIZE);
340
341 myData.bButtonAnimating |= _update_button_image (&myData.closeButton, &myData.iAnimIterClose, iNumButton == CD_BUTTON_CLOSE);
342
343 cd_app_menu_redraw_buttons ();
344 }
345
346 if (myData.bButtonAnimating)
347 *bContinueAnimation = TRUE;
348 CD_APPLET_LEAVE (GLDI_NOTIFICATION_LET_PASS);
349 }