[Pkg-mozext-commits] [firetray] 11/399: attempt to get signals from tray icon using LibGObject
David Prévot
taffit at alioth.debian.org
Tue Oct 29 18:23:05 UTC 2013
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch dfsg-clean
in repository firetray.
commit 29bf5a42bd0fd9c3365d9018cf43cfb3120e0e8f
Author: foudfou <foudil.newbie+git at gmail.com>
Date: Sun Jul 10 12:45:36 2011 +0200
attempt to get signals from tray icon using LibGObject
needs refactoring !
---
src/chrome/content/overlay.js | 22 +++
src/modules/LibGObject.js | 160 ++++++++++++++++++++
testing/Makefile | 17 ++-
testing/gtk_icon_example | Bin 66933 -> 68328 bytes
testing/{gkt_icon_example.c => gtk_icon_example.c} | 42 +++++
testing/hide.c | 53 +++++++
testing/trayicon.c | 109 +++++++++++++
7 files changed, 401 insertions(+), 2 deletions(-)
diff --git a/src/chrome/content/overlay.js b/src/chrome/content/overlay.js
index 404d3a5..903888e 100644
--- a/src/chrome/content/overlay.js
+++ b/src/chrome/content/overlay.js
@@ -1,11 +1,24 @@
/* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+// TODO: Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://moztray/commons.js");
Components.utils.import("resource://moztray/LibGtkStatusIcon.js");
+Components.utils.import("resource://moztray/LibGObject.js");
const MOZT_ICON_DIR = "chrome/skin/";
const MOZT_ICON_SUFFIX = "32.png";
+
+var mozt_hideWinCb;
+/* NOTE: arguments come obviously from the GCallbackFunction definition:
+ * [gpointer, guint, gpointer]
+ */
+var mozt_hideWinJs = function(aInstance, aTimestamp, aUserData) {
+ try {
+ alert("Hide");
+ } catch(e) {Cu.reportError(ex);}
+};
+
mozt.Main = {
onLoad: function() {
@@ -29,6 +42,15 @@ mozt.Main = {
var icon_filename = MOZT_ICON_DIR + mozApp + MOZT_ICON_SUFFIX;
LibGtkStatusIcon.gtk_status_icon_set_from_file(this.tray_icon,
icon_filename);
+ // gtk_status_icon_set_tooltip(tray_icon,
+ // "Example Tray Icon");
+ // gtk_status_icon_set_visible(tray_icon, TRUE);
+
+ mozt_hideWinCb = LibGObject.GCallbackFunction(mozt_hideWinJs);
+
+ LibGObject.g_signal_connect(this.tray_icon, "activate",
+ mozt_hideWinCb, null);
+
mozt.Debug.dump('Moztray LOADED !');
this.initialized = true;
diff --git a/src/modules/LibGObject.js b/src/modules/LibGObject.js
new file mode 100644
index 0000000..0c3c8af
--- /dev/null
+++ b/src/modules/LibGObject.js
@@ -0,0 +1,160 @@
+/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is messagingmenu-extension
+ *
+ * The Initial Developer of the Original Code is
+ * Mozilla Messaging, Ltd.
+ * Portions created by the Initial Developer are Copyright (C) 2010
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Mike Conley <mconley at mozillamessaging.com>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+var EXPORTED_SYMBOLS = ["LibGObject"];
+
+const LIB_GOBJECT = "libgobject-2.0.so.0";
+
+const Cu = Components.utils;
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+
+Cu.import("resource://gre/modules/ctypes.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyGetter(this, "libgobject", function() {
+ var libgobject = ctypes.open(LIB_GOBJECT);
+ if (!libgobject)
+ throw "libgobject is unavailable";
+
+ return libgobject;
+});
+
+XPCOMUtils.defineLazyGetter(this, "GCallback", function() {
+ return ctypes.voidptr_t;
+});
+
+XPCOMUtils.defineLazyGetter(this, "gpointer", function() {
+ return ctypes.voidptr_t;
+});
+
+XPCOMUtils.defineLazyGetter(this, "gulong", function() {
+ return ctypes.unsigned_long;
+});
+
+XPCOMUtils.defineLazyGetter(this, "guint", function() {
+ return ctypes.unsigned_int;
+});
+
+XPCOMUtils.defineLazyGetter(this, "guint32", function() {
+ return ctypes.unsigned_int;
+});
+
+XPCOMUtils.defineLazyGetter(this, "gint", function() {
+ return ctypes.int;
+});
+
+XPCOMUtils.defineLazyGetter(this, "gchar", function() {
+ return ctypes.unsigned_char;
+});
+
+XPCOMUtils.defineLazyGetter(this, "gboolean", function() {
+ return gint;
+});
+
+XPCOMUtils.defineLazyGetter(this, "GConnectFlags", function() {
+ return guint;
+});
+
+XPCOMUtils.defineLazyGetter(this, "GClosureNotify", function() {
+ return gpointer;
+});
+
+XPCOMUtils.defineLazyGetter(this, "GCallbackFunction", function() {
+ var GCallbackFunction =
+ ctypes.FunctionType(ctypes.default_abi,
+ ctypes.void_t,
+ [gpointer,
+ guint,
+ gpointer]).ptr;
+ if (!GCallbackFunction)
+ throw "GCallbackFunction is unavailable";
+
+ return GCallbackFunction;
+});
+
+XPCOMUtils.defineLazyGetter(this, "g_signal_connect_data", function() {
+ var g_signal_connect_data =
+ libgobject.declare("g_signal_connect_data",
+ ctypes.default_abi,
+ gulong,
+ gpointer, // instance
+ gchar.ptr, // detailed_signal
+ GCallback, // handler
+ gpointer, // data
+ GClosureNotify, // NULL
+ GConnectFlags); // 0
+
+ if (!g_signal_connect_data)
+ throw "g_signal_connect_data is unavailable";
+
+ return g_signal_connect_data;
+});
+
+XPCOMUtils.defineLazyGetter(this, "g_object_unref", function() {
+ var g_object_unref =
+ libgobject.declare("g_object_unref",
+ ctypes.default_abi,
+ ctypes.void_t,
+ gpointer);
+
+ if (!g_object_unref)
+ throw "g_object_unref is unavailable";
+
+ return g_object_unref;
+});
+
+var LibGObject = {
+ GCallback: GCallback,
+ GCallbackFunction: GCallbackFunction,
+ gpointer: gpointer,
+ gulong: gulong,
+ guint: guint,
+ guint32: guint32,
+ gint: gint,
+ gchar: gchar,
+ gboolean: gboolean,
+ GConnectFlags: GConnectFlags,
+ GClosureNotify: GClosureNotify,
+ g_object_unref: g_object_unref,
+
+ g_signal_connect: function(instance, detailed_signal, handler, data) {
+ return g_signal_connect_data(instance, detailed_signal,
+ handler, data, null, 0);
+ }
+};
diff --git a/testing/Makefile b/testing/Makefile
index b257336..146b217 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -1,4 +1,17 @@
includes := $(shell pkg-config --libs --cflags gtk+-2.0)
-all:
- gcc $(includes) -o gtk_icon_example gkt_icon_example.c
+.PHONY: all
+all: gtk_icon_example trayicon hide
+
+.PHONY: clean
+clean:
+ rm gtk_icon_example trayicon hide
+
+gtk_icon_example: gtk_icon_example.c
+ gcc $(includes) -o gtk_icon_example gtk_icon_example.c
+
+trayicon: trayicon.c
+ gcc $(includes) -o trayicon trayicon.c
+
+hide: hide.c
+ gcc $(includes) -o hide hide.c
diff --git a/testing/gtk_icon_example b/testing/gtk_icon_example
index a57224f..91ecccf 100755
Binary files a/testing/gtk_icon_example and b/testing/gtk_icon_example differ
diff --git a/testing/gkt_icon_example.c b/testing/gtk_icon_example.c
similarity index 51%
rename from testing/gkt_icon_example.c
rename to testing/gtk_icon_example.c
index b9e3d18..0255d1a 100644
--- a/testing/gkt_icon_example.c
+++ b/testing/gtk_icon_example.c
@@ -1,3 +1,5 @@
+#include <stdio.h>
+
#include <gtk/gtk.h>
#include "firefox.xpm"
@@ -37,11 +39,51 @@ static GtkStatusIcon *create_tray_icon() {
return tray_icon;
}
+/*
+- Hide/Show window to avoid minimizing it to the task bar (yes, use gtk_widget_hide() and gtk_widget_show()) when the user clicks on the system tray icon
+- Listen to the "window-state-event" (GObject's signal) to detect when minimizing and, instead of doing that, hide the window (ie, "minimize to the tray").
+*/
+
+/* This callback quits the program */
+gint delete_event( GtkWidget *widget,
+ GdkEvent *event,
+ gpointer
+ data )
+{
+ gtk_main_quit ();
+ return FALSE;
+}
+
+static void winShowHide(GtkMenuItem *item, gpointer window)
+{
+ /* GdkWindow * tl_window = gdk_window_get_toplevel((GdkWindow*)window); */
+
+ /* GdkWindowState ws = gdk_window_get_state((GdkWindow*)tl_window); */
+ /* printf("GdkWindowState: %d", ws); */
+
+ gdk_window_hide(window->window);
+ /* gtk_widget_show(GTK_WIDGET(window)); */
+ /* gtk_widget_hide(GTK_WIDGET(window)); */
+}
+
int main(int argc, char **argv) {
GtkStatusIcon *tray_icon;
gtk_init(&argc, &argv);
tray_icon = create_tray_icon();
+
+ GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_title (GTK_WINDOW (window), "GtkStatusIcon Example");
+ gtk_widget_set_size_request (window, 200, -1);
+
+ /* Set a handler for delete_event that immediately exits GTK. */
+ g_signal_connect (G_OBJECT (window), "delete_event",
+ G_CALLBACK (delete_event), NULL);
+
+ g_signal_connect(G_OBJECT(tray_icon), "activate", G_CALLBACK(winShowHide), window);
+
+ gtk_widget_show_all (window);
+
gtk_main();
return 0;
diff --git a/testing/hide.c b/testing/hide.c
new file mode 100644
index 0000000..85b537d
--- /dev/null
+++ b/testing/hide.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <gtk/gtk.h>
+
+
+int main(int argc, char **argv)
+{
+ GtkWidget *window;
+ GtkWidget *vbox;
+ GtkWidget *button;
+
+ gtk_init(&argc, &argv);
+
+ window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ gtk_container_set_border_width(GTK_CONTAINER(window), 10);
+ g_signal_connect(window, "delete_event", (GCallback)gtk_main_quit, 0);
+
+ vbox = gtk_vbox_new(TRUE, 0);
+ gtk_container_add(GTK_CONTAINER(window), vbox);
+
+ button = gtk_button_new_with_label("Hide");
+ gtk_box_pack_start(GTK_BOX(vbox), button, TRUE,TRUE, 0);
+ g_signal_connect_swapped(button, "clicked",
+ (GCallback)gtk_widget_hide, window);
+
+ button = gtk_button_new_from_stock(GTK_STOCK_QUIT);
+ gtk_box_pack_start(GTK_BOX(vbox), button, TRUE,TRUE, 0);
+ g_signal_connect(button, "clicked", (GCallback)gtk_main_quit, 0);
+
+ gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
+
+ // gtk_window_set_default_size(GTK_WINDOW(window),500,500);
+
+ // gtk_window_stick(GTK_WINDOW(window)); /* on all desktops or not */
+
+ gtk_window_set_keep_above(GTK_WINDOW(window), TRUE);
+
+ //gtk_window_set_keep_below(GTK_WINDOW(window),TRUE);
+
+ gtk_window_set_skip_taskbar_hint(GTK_WINDOW(window), TRUE);
+ gtk_window_set_skip_pager_hint(GTK_WINDOW(window), TRUE);
+
+ gtk_window_move(GTK_WINDOW(window), 100, 100);
+
+ // gtk_window_set_opacity(GTK_WINDOW(window), 0); /* not working hmmmm*/
+
+ gtk_widget_show_all(window);
+
+ gtk_main();
+
+ return (EXIT_SUCCESS);
+}
diff --git a/testing/trayicon.c b/testing/trayicon.c
new file mode 100644
index 0000000..351da1c
--- /dev/null
+++ b/testing/trayicon.c
@@ -0,0 +1,109 @@
+// http://www.codeproject.com/KB/cross-platform/GTKTrayIcon.aspx
+#include <gtk/gtk.h>
+
+static void trayView(GtkMenuItem *item, gpointer user_data);
+static void trayExit(GtkMenuItem *item, gpointer user_data);
+static void trayIconActivated(GObject *trayIcon, gpointer data);
+static void trayIconPopup(GtkStatusIcon *status_icon, guint button, guint32 activate_time, gpointer popUpMenu);
+static void destroy (GtkWidget*, gpointer);
+static gboolean delete_event (GtkWidget*, GdkEvent*, gpointer);
+static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer user_data);
+
+int main(int argc, char *argv[])
+{
+ gtk_init (&argc, &argv);
+
+ GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_title (GTK_WINDOW (window), "GtkStatusIcon Example");
+ gtk_widget_set_size_request (window, 200, -1);
+
+ //set try icon file
+ GtkStatusIcon *trayIcon = gtk_status_icon_new_from_file ("firefox32.png");
+ //set popup menu for tray icon
+ GtkWidget *menu, *menuItemView, *menuItemExit;
+ menu = gtk_menu_new();
+ menuItemView = gtk_menu_item_new_with_label ("View");
+ menuItemExit = gtk_menu_item_new_with_label ("Exit");
+ g_signal_connect (G_OBJECT (menuItemView), "activate", G_CALLBACK (trayView), window);
+ g_signal_connect (G_OBJECT (menuItemExit), "activate", G_CALLBACK (trayExit), NULL);
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemView);
+ gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemExit);
+ gtk_widget_show_all (menu);
+ //set tooltip
+ gtk_status_icon_set_tooltip (trayIcon, "MsgWatcherGTK");
+ //connect handlers for mouse events
+ g_signal_connect(GTK_STATUS_ICON (trayIcon), "activate", GTK_SIGNAL_FUNC (trayIconActivated), window);
+ g_signal_connect(GTK_STATUS_ICON (trayIcon), "popup-menu", GTK_SIGNAL_FUNC (trayIconPopup), menu);
+ gtk_status_icon_set_visible(trayIcon, FALSE); //set icon initially invisible
+
+
+ GtkWidget *menuBar, *menuItemTopLvl, *mainMenu, *mainMenuItemExit;
+ menuBar = gtk_menu_bar_new ();
+ menuItemTopLvl = gtk_menu_item_new_with_label ("Menu");
+ gtk_menu_shell_append (GTK_MENU_SHELL (menuBar), menuItemTopLvl);
+
+ mainMenu = gtk_menu_new ();
+ gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuItemTopLvl), mainMenu);
+ mainMenuItemExit = gtk_menu_item_new_with_label ("Quit");
+ g_signal_connect (G_OBJECT (mainMenuItemExit), "activate", G_CALLBACK (trayExit), NULL);
+ gtk_menu_shell_append (GTK_MENU_SHELL (mainMenu), mainMenuItemExit);
+
+ g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
+ g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), trayIcon);
+ g_signal_connect (G_OBJECT (window), "window-state-event", G_CALLBACK (window_state_event), trayIcon);
+ gtk_container_add (GTK_CONTAINER (window), menuBar);
+ gtk_widget_show_all (window);
+ gtk_main ();
+ return 0;
+}
+
+static void trayView(GtkMenuItem *item, gpointer window)
+{
+ gtk_widget_show(GTK_WIDGET(window));
+ gtk_window_deiconify(GTK_WINDOW(window));
+}
+
+static void trayExit(GtkMenuItem *item, gpointer user_data)
+{
+ printf("exit");
+ gtk_main_quit();
+}
+
+static void trayIconActivated(GObject *trayIcon, gpointer window)
+{
+ gtk_widget_show(GTK_WIDGET(window));
+ gtk_window_deiconify(GTK_WINDOW(window));
+}
+
+static void trayIconPopup(GtkStatusIcon *status_icon, guint button, guint32 activate_time, gpointer popUpMenu)
+{
+ gtk_menu_popup(GTK_MENU(popUpMenu), NULL, NULL, gtk_status_icon_position_menu, status_icon, button, activate_time);
+}
+
+static void destroy (GtkWidget *window, gpointer data)
+{
+ gtk_main_quit ();
+}
+
+static gboolean delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
+{
+ return FALSE;
+}
+
+static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer trayIcon)
+{
+ if(event->changed_mask == GDK_WINDOW_STATE_ICONIFIED &&
+ (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED
+ || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
+ {
+ gtk_widget_hide (GTK_WIDGET(widget));
+ gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), TRUE);
+ }
+ else if(event->changed_mask == GDK_WINDOW_STATE_WITHDRAWN &&
+ (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED
+ || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
+ {
+ gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
+ }
+ return TRUE;
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/firetray.git
More information about the Pkg-mozext-commits
mailing list