You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

204 lines
5.8 KiB

  1. /*
  2. * Copyright (C) 2009-2010 Nick Schermer <nick@xfce.org>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include <config.h>
  20. #endif
  21. #ifdef HAVE_LOCALE_H
  22. #include <locale.h>
  23. #endif
  24. #include <libxfce4ui/libxfce4ui.h>
  25. #include <exo/exo.h>
  26. #include <common/panel-private.h>
  27. #include <common/panel-utils.h>
  28. static void
  29. panel_utils_help_button_clicked (GtkWidget *button,
  30. XfcePanelPlugin *panel_plugin)
  31. {
  32. GtkWidget *toplevel;
  33. panel_return_if_fail (XFCE_IS_PANEL_PLUGIN (panel_plugin));
  34. panel_return_if_fail (GTK_IS_WIDGET (button));
  35. toplevel = gtk_widget_get_toplevel (button);
  36. panel_utils_show_help (GTK_WINDOW (toplevel),
  37. xfce_panel_plugin_get_name (panel_plugin),
  38. NULL);
  39. }
  40. GtkBuilder *
  41. panel_utils_builder_new (XfcePanelPlugin *panel_plugin,
  42. const gchar *buffer,
  43. gsize length,
  44. GObject **dialog_return)
  45. {
  46. GError *error = NULL;
  47. GtkBuilder *builder;
  48. GObject *dialog, *button;
  49. panel_return_val_if_fail (XFCE_IS_PANEL_PLUGIN (panel_plugin), NULL);
  50. builder = gtk_builder_new ();
  51. if (gtk_builder_add_from_string (builder, buffer, length, &error))
  52. {
  53. dialog = gtk_builder_get_object (builder, "dialog");
  54. if (G_LIKELY (dialog != NULL))
  55. {
  56. g_object_weak_ref (G_OBJECT (dialog),
  57. (GWeakNotify) g_object_unref, builder);
  58. xfce_panel_plugin_take_window (panel_plugin, GTK_WINDOW (dialog));
  59. xfce_panel_plugin_block_menu (panel_plugin);
  60. g_object_weak_ref (G_OBJECT (dialog),
  61. (GWeakNotify) xfce_panel_plugin_unblock_menu,
  62. panel_plugin);
  63. button = gtk_builder_get_object (builder, "close-button");
  64. if (G_LIKELY (button != NULL))
  65. g_signal_connect_swapped (G_OBJECT (button), "clicked",
  66. G_CALLBACK (gtk_widget_destroy), dialog);
  67. button = gtk_builder_get_object (builder, "help-button");
  68. if (G_LIKELY (button != NULL))
  69. g_signal_connect (G_OBJECT (button), "clicked",
  70. G_CALLBACK (panel_utils_help_button_clicked), panel_plugin);
  71. if (G_LIKELY (dialog_return != NULL))
  72. *dialog_return = dialog;
  73. return builder;
  74. }
  75. else
  76. {
  77. g_set_error_literal (&error, 0, 0, "No widget with the name \"dialog\" found");
  78. }
  79. }
  80. g_critical ("Faild to construct the builder for plugin %s-%d: %s.",
  81. xfce_panel_plugin_get_name (panel_plugin),
  82. xfce_panel_plugin_get_unique_id (panel_plugin),
  83. error->message);
  84. g_error_free (error);
  85. g_object_unref (G_OBJECT (builder));
  86. return NULL;
  87. }
  88. void
  89. panel_utils_show_help (GtkWindow *parent,
  90. const gchar *page,
  91. const gchar *offset)
  92. {
  93. xfce_dialog_show_help (parent, "xfce4-panel", page, offset);
  94. }
  95. gboolean
  96. panel_utils_grab_available (void)
  97. {
  98. GdkScreen *screen;
  99. GdkWindow *root;
  100. GdkGrabStatus grab_pointer = GDK_GRAB_FROZEN;
  101. GdkGrabStatus grab_keyboard = GDK_GRAB_FROZEN;
  102. gboolean grab_succeed = FALSE;
  103. guint i;
  104. GdkEventMask pointer_mask = GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
  105. | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK
  106. | GDK_POINTER_MOTION_MASK;
  107. screen = xfce_gdk_screen_get_active (NULL);
  108. root = gdk_screen_get_root_window (screen);
  109. /* don't try to get the grab for longer then 1/4 second */
  110. for (i = 0; i < (G_USEC_PER_SEC / 100 / 4); i++)
  111. {
  112. grab_keyboard = gdk_keyboard_grab (root, TRUE, GDK_CURRENT_TIME);
  113. if (grab_keyboard == GDK_GRAB_SUCCESS)
  114. {
  115. grab_pointer = gdk_pointer_grab (root, TRUE, pointer_mask,
  116. NULL, NULL, GDK_CURRENT_TIME);
  117. if (grab_pointer == GDK_GRAB_SUCCESS)
  118. {
  119. grab_succeed = TRUE;
  120. break;
  121. }
  122. }
  123. g_usleep (100);
  124. }
  125. /* release the grab so the gtk_menu_popup() can take it */
  126. if (grab_pointer == GDK_GRAB_SUCCESS)
  127. gdk_pointer_ungrab (GDK_CURRENT_TIME);
  128. if (grab_keyboard == GDK_GRAB_SUCCESS)
  129. gdk_keyboard_ungrab (GDK_CURRENT_TIME);
  130. if (!grab_succeed)
  131. {
  132. g_printerr (PACKAGE_NAME ": Unable to get keyboard and mouse "
  133. "grab. Menu popup failed.\n");
  134. }
  135. return grab_succeed;
  136. }
  137. void
  138. panel_utils_set_atk_info (GtkWidget *widget,
  139. const gchar *name,
  140. const gchar *description)
  141. {
  142. static gboolean initialized = FALSE;
  143. static gboolean atk_enabled = TRUE;
  144. AtkObject *object;
  145. panel_return_if_fail (GTK_IS_WIDGET (widget));
  146. if (atk_enabled)
  147. {
  148. object = gtk_widget_get_accessible (widget);
  149. if (!initialized)
  150. {
  151. initialized = TRUE;
  152. atk_enabled = GTK_IS_ACCESSIBLE (object);
  153. if (!atk_enabled)
  154. return;
  155. }
  156. if (name != NULL)
  157. atk_object_set_name (object, name);
  158. if (description != NULL)
  159. atk_object_set_description (object, description);
  160. }
  161. }