finish text icon implementation for displaying unread messages count in mail

applications
This commit is contained in:
foudfou 2011-09-19 18:29:18 +02:00
parent 74f9cf7da3
commit 0ccfb11cfc
14 changed files with 259 additions and 128 deletions

View File

@ -91,8 +91,8 @@ window.addEventListener(
// // TEST - can we catch minimize event ? // // TEST - can we catch minimize event ?
// window.addEventListener( // window.addEventListener(
// 'DOMAttrModified', function (e) { // focus // 'command', function (e) {
// removeEventListener('deactivate', arguments.callee, true); // removeEventListener('command', arguments.callee, true);
// WARN("Got deactivated: "+e.originalTarget.windowState); // Ci.nsIDOMChromeWindow.STATE_MINIMIZED|STATE_NORMAL // WARN("Got deactivated: "+e.originalTarget.windowState); // Ci.nsIDOMChromeWindow.STATE_MINIMIZED|STATE_NORMAL
// WARN("attrName: "+e.attrName); // WARN("attrName: "+e.attrName);
// }, // },

Binary file not shown.

Before

Width:  |  Height:  |  Size: 770 B

View File

@ -44,7 +44,7 @@
<Description> <Description>
<em:id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</em:id> <em:id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</em:id>
<em:minVersion>2.0a1</em:minVersion> <em:minVersion>2.0a1</em:minVersion>
<em:maxVersion>2.4.* </em:maxVersion> <em:maxVersion>2.4.*</em:maxVersion>
</Description> </Description>
</em:targetApplication> </em:targetApplication>

View File

@ -50,7 +50,7 @@ mozt.Handler = {
if (winType == "BaseWindow") if (winType == "BaseWindow")
winOut = winInterface.getInterface(Ci.nsIBaseWindow); winOut = winInterface.getInterface(Ci.nsIBaseWindow);
else if (winType == "XUL") else if (winType == "XUL")
winOut = winInterface.getInterface(Ci.nsIXULWindow); winOut = winInterface.getInterface(Ci.nsIXULWindow);
else { else {
ERROR("MOZTRAY: unknown winType '" + winType + "'"); ERROR("MOZTRAY: unknown winType '" + winType + "'");
return null; return null;

View File

@ -8,9 +8,11 @@ const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/ctypes.jsm"); Cu.import("resource://gre/modules/ctypes.jsm");
Cu.import("resource://moztray/cairo.jsm");
Cu.import("resource://moztray/gobject.jsm"); Cu.import("resource://moztray/gobject.jsm");
Cu.import("resource://moztray/gdk.jsm"); Cu.import("resource://moztray/gdk.jsm");
Cu.import("resource://moztray/gtk.jsm"); Cu.import("resource://moztray/gtk.jsm");
Cu.import("resource://moztray/pango.jsm");
Cu.import("resource://moztray/commons.js"); Cu.import("resource://moztray/commons.js");
if ("undefined" == typeof(mozt.Handler)) if ("undefined" == typeof(mozt.Handler))
@ -26,8 +28,10 @@ mozt.IconLinux = {
tryIcon: null, tryIcon: null,
menu: null, menu: null,
appName: null, appName: null,
ICON_FILENAME_DEFAULT: null, FILENAME_DEFAULT: null,
ICON_SUFFIX: "32.png", FILENAME_SUFFIX: "32.png",
FILENAME_NEWMAIL: "newmail.png",
MIN_FONT_SIZE: 4,
init: function() { init: function() {
@ -36,10 +40,12 @@ mozt.IconLinux = {
// init tray icon, some variables // init tray icon, some variables
this.trayIcon = gtk.gtk_status_icon_new(); this.trayIcon = gtk.gtk_status_icon_new();
this.appName = Services.appinfo.name.toLowerCase(); this.appName = Services.appinfo.name.toLowerCase();
this.ICON_FILENAME_DEFAULT = mozt.Utils.chromeToPath( this.FILENAME_DEFAULT = mozt.Utils.chromeToPath(
"chrome://moztray/skin/" + this.appName + this.ICON_SUFFIX); "chrome://moztray/skin/" + this.appName + this.FILENAME_SUFFIX);
this.FILENAME_NEWMAIL = mozt.Utils.chromeToPath(
"chrome://moztray/skin/newmail.png");
this.setDefaultImage(); this.setImageDefault();
// build icon popup menu // build icon popup menu
this.menu = gtk.gtk_menu_new(); this.menu = gtk.gtk_menu_new();
@ -68,7 +74,7 @@ mozt.IconLinux = {
gobject.g_signal_connect(this.trayIcon, "popup-menu", gobject.g_signal_connect(this.trayIcon, "popup-menu",
mozt_popupMenuCb, this.menu); mozt_popupMenuCb, this.menu);
this.setDefaultTooltip(); this.setTooltipDefault();
// watch out for binding problems ! here we prefer to keep 'this' in // watch out for binding problems ! here we prefer to keep 'this' in
// showHideToTray() and abandon the args. // showHideToTray() and abandon the args.
@ -77,7 +83,6 @@ mozt.IconLinux = {
gobject.g_signal_connect(this.trayIcon, "activate", gobject.g_signal_connect(this.trayIcon, "activate",
mozt_iconActivateCb, null); mozt_iconActivateCb, null);
} catch (x) { } catch (x) {
ERROR(x); ERROR(x);
return false; return false;
@ -108,10 +113,10 @@ mozt.IconLinux = {
return true; return true;
}, },
setDefaultImage: function() { setImageDefault: function() {
if (!this.ICON_FILENAME_DEFAULT) if (!this.FILENAME_DEFAULT)
throw "Default application icon filename not set"; throw "Default application icon filename not set";
this.setImage(this.ICON_FILENAME_DEFAULT); this.setImage(this.FILENAME_DEFAULT);
}, },
// GTK bug: Gdk-CRITICAL **: IA__gdk_window_get_root_coords: assertion `GDK_IS_WINDOW (window)' failed // GTK bug: Gdk-CRITICAL **: IA__gdk_window_get_root_coords: assertion `GDK_IS_WINDOW (window)' failed
@ -123,104 +128,110 @@ mozt.IconLinux = {
return true; return true;
}, },
setDefaultTooltip: function() { setTooltipDefault: function() {
if (!this.appName) if (!this.appName)
throw "application name not initialized"; throw "application name not initialized";
this.setTooltip(this.appName); this.setTooltip(this.appName);
}, },
// SetIconText(GtkStatusIcon *tray_icon, const char *text, const char *color) { setText: function(text, color) { // TODO: split into smaller functions;
setText: function() { LOG("setText");
if (typeof(text) != "string" ) {
ERROR("'text' arguement must be toString()'d: ");
return false;
}
// // build background from image // build background from image
// GdkPixbuf* special_icon = gdk_pixbuf_new_from_file("newmail.png", NULL); // GError **error); let specialIcon = gdk.gdk_pixbuf_new_from_file(this.FILENAME_NEWMAIL, null); // GError **error);
// GdkPixbuf *dest = gdk_pixbuf_copy(special_icon); let dest = gdk.gdk_pixbuf_copy(specialIcon);
// int w=gdk_pixbuf_get_width(special_icon); let w = gdk.gdk_pixbuf_get_width(specialIcon);
// int h=gdk_pixbuf_get_height(special_icon); let h = gdk.gdk_pixbuf_get_height(specialIcon);
// // prepare colors/alpha // prepare colors/alpha
// GdkColormap* cmap=gdk_screen_get_system_colormap(gdk_screen_get_default()); let colorMap = gdk.gdk_screen_get_system_colormap(gdk.gdk_screen_get_default());
// int screen_depth=24; let visual = gdk.gdk_colormap_get_visual(colorMap);
// GdkVisual* visual = gdk_colormap_get_visual(cmap); let screenDepth = 24; // = visual.depth; // FIXME: was visual->depth
// screen_depth = visual->depth; let fore = new gdk.GdkColor;
// GdkColor fore = { 0, 0, 0, 0 }; fore.pixel = fore.red = fore.green = fore.blue = 0;
// GdkColor alpha = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}; let alpha = new gdk.GdkColor;
// gdk_color_parse (color, &fore) ) alpha.pixel = alpha.red = alpha.green = alpha.blue = 0xFFFF;
// if(fore.red==alpha.red && fore.green==alpha.green && fore.blue==alpha.blue) { if (!fore || !alpha)
// alpha.red=0; //make sure alpha is different from fore WARN("Undefined GdkColor fore or alpha");
// } gdk.gdk_color_parse(color, fore.address());
// gdk_colormap_alloc_color (cmap, &fore, TRUE, TRUE); if(fore.red == alpha.red && fore.green == alpha.green && fore.blue == alpha.blue) {
// gdk_colormap_alloc_color (cmap, &alpha, TRUE, TRUE); alpha.red=0; // make sure alpha is different from fore
}
gdk.gdk_colormap_alloc_color(colorMap, fore.address(), true, true);
gdk.gdk_colormap_alloc_color(colorMap, alpha.address(), true, true);
// // build pixmap with rectangle // build pixmap with rectangle
// GdkPixmap *pm = gdk_pixmap_new (NULL, w, h, screen_depth); let pm = gdk.gdk_pixmap_new(null, w, h, screenDepth);
// GdkGC *gc = gdk_gc_new (pm); // graphic context. DEPRECATED ? let pmDrawable = ctypes.cast(pm, gdk.GdkDrawable.ptr);
// gdk_gc_set_foreground(gc,&alpha); let cr = gdk.gdk_cairo_create(pmDrawable);
// /* gdk_draw_rectangle(pm,gc,TRUE, 0, 0, w ,h ); */ gdk.gdk_cairo_set_source_color(cr, alpha.address());
// cairo_t *cr; cairo.cairo_rectangle(cr, 0, 0, w, h);
// cr = gdk_cairo_create(pm); cairo.cairo_set_source_rgb(cr, 1, 1, 1);
// cairo_rectangle(cr, 0, 0, w, h); cairo.cairo_fill(cr);
// /* void cairo_rectangle (cairo_t *cr, */
// /* double x, */
// /* double y, */
// /* double width, */
// /* double height); */
// cairo_set_source_rgb(cr, 1, 1, 1); /* TODO: consider cairo_set_source_rgba (notice the ending "a" for alpha) */
// cairo_fill(cr);
// cairo_destroy(cr);
// // build text // build text
// GtkWidget *scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL); let scratch = gtk.gtk_window_new(gtk.GTK_WINDOW_TOPLEVEL);
// PangoLayout *layout = gtk_widget_create_pango_layout(scratch, NULL); let layout = gtk.gtk_widget_create_pango_layout(scratch, null);
// gtk_widget_destroy(scratch); gtk.gtk_widget_destroy(scratch);
// PangoFontDescription *fnt = pango_font_description_from_string("Sans 18"); let fnt = pango.pango_font_description_from_string("Sans 18");
// pango_font_description_set_weight (fnt,PANGO_WEIGHT_SEMIBOLD); pango.pango_font_description_set_weight(fnt,pango.PANGO_WEIGHT_SEMIBOLD);
// pango_layout_set_spacing (layout,0); pango.pango_layout_set_spacing(layout,0);
// pango_layout_set_font_description (layout, fnt); pango.pango_layout_set_font_description(layout, fnt);
// pango_layout_set_text (layout, (gchar *)text,-1); LOG("layout="+layout);
// int tw=0; LOG("text="+text);
// int th=0; pango.pango_layout_set_text(layout, text,-1);
// int sz; let tw = new ctypes.int;
// int border=4; let th = new ctypes.int;
// pango_layout_get_pixel_size(layout, &tw, &th); let sz;
// while( (tw>w - border || th > h - border)) //fit text to the icon by decreasing font size let border = 4;
// { pango.pango_layout_get_pixel_size(layout, tw.address(), th.address());
// sz=pango_font_description_get_size (fnt); LOG("tw="+tw.value+" th="+th.value);
// if(sz<MIN_FONT_SIZE) { // fit text to the icon by decreasing font size
// sz=MIN_FONT_SIZE; while ( tw.value > (w - border) || th.value > (h - border) ) {
// break; sz = pango.pango_font_description_get_size(fnt);
// } if(sz < this.MIN_FONT_SIZE) {
// sz-=PANGO_SCALE; sz = this.MIN_FONT_SIZE;
// pango_font_description_set_size (fnt,sz); break;
// pango_layout_set_font_description (layout, fnt); }
// pango_layout_get_pixel_size(layout, &tw, &th); sz -= pango.PANGO_SCALE;
// } pango.pango_font_description_set_size(fnt,sz);
// // center text pango.pango_layout_set_font_description(layout, fnt);
// int px, py; pango.pango_layout_get_pixel_size(layout, tw.address(), th.address());
// px=(w-tw)/2; }
// py=(h-th)/2; LOG("tw="+tw.value+" th="+th.value);
pango.pango_font_description_free(fnt);
// center text
let px = (w-tw.value)/2;
let py = (h-th.value)/2;
// // draw text on pixmap // draw text on pixmap
// gdk_draw_layout_with_colors (pm, gc, px, py, layout, &fore, NULL); gdk.gdk_cairo_set_source_color(cr, fore.address());
cairo.cairo_move_to(cr, px, py);
pangocairo.pango_cairo_show_layout(cr, layout);
cairo.cairo_destroy(cr);
gobject.g_object_unref(layout);
// GdkPixbuf *buf = gdk_pixbuf_get_from_drawable (NULL, pm, NULL, 0, 0, 0, 0, w, h); let buf = gdk.gdk_pixbuf_get_from_drawable(null, pmDrawable, null, 0, 0, 0, 0, w, h);
// g_object_unref (pm); gobject.g_object_unref(pm);
// GdkPixbuf *alpha_buf = gdk_pixbuf_add_alpha (buf, TRUE, (guchar)alpha.red, (guchar)alpha.green, (guchar)alpha.blue); LOG("alpha="+alpha);
let alphaRed = gobject.guint16(alpha.red);
let alphaRed_guchar = ctypes.cast(alphaRed, gobject.guchar);
let alphaGreen = gobject.guint16(alpha.green);
let alphaGreen_guchar = ctypes.cast(alphaGreen, gobject.guchar);
let alphaBlue = gobject.guint16(alpha.blue);
let alphaBlue_guchar = ctypes.cast(alphaBlue, gobject.guchar);
let bufAlpha = gdk.gdk_pixbuf_add_alpha(buf, true, alphaRed_guchar, alphaGreen_guchar, alphaBlue_guchar);
gobject.g_object_unref(buf);
// // cleaning // merge the rendered text on top
// g_object_unref (buf); gdk.gdk_pixbuf_composite(bufAlpha,dest,0,0,w,h,0,0,1,1,gdk.GDK_INTERP_NEAREST,255);
// g_object_unref (layout); gobject.g_object_unref(bufAlpha);
// pango_font_description_free (fnt);
// g_object_unref (gc);
// //merge the rendered text on top
// gdk_pixbuf_composite (alpha_buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255);
// g_object_unref(alpha_buf);
// gtk_status_icon_set_from_pixbuf(GTK_STATUS_ICON(tray_icon), GDK_PIXBUF(dest));
gtk.gtk_status_icon_set_from_pixbuf(this.trayIcon, dest);
} }
}; // mozt.IconLinux }; // mozt.IconLinux

View File

@ -19,6 +19,7 @@ const FLDR_UNINTERESTING =
Ci.nsMsgFolderFlags.SentMail | Ci.nsMsgFolderFlags.SentMail |
Ci.nsMsgFolderFlags.Templates | Ci.nsMsgFolderFlags.Templates |
Ci.nsMsgFolderFlags.Trash; Ci.nsMsgFolderFlags.Trash;
const ICON_TEXT_COLOR = "#00000";
/** /**
* mozt namespace. * mozt namespace.
@ -107,11 +108,10 @@ mozt.Messaging = {
// update icon // update icon
if (this._unreadMsgCount == 0) { if (this._unreadMsgCount == 0) {
mozt.IconLinux.setDefaultImage(); mozt.IconLinux.setImageDefault();
mozt.IconLinux.setDefaultTooltip(); mozt.IconLinux.setTooltipDefault();
} else if (this._unreadMsgCount > 0) { } else if (this._unreadMsgCount > 0) {
mozt.IconLinux.setImage( mozt.IconLinux.setText(this._unreadMsgCount.toString(), ICON_TEXT_COLOR);
mozt.Utils.chromeToPath("chrome://moztray/skin/message-mail-new.png"));
let localizedMessage = PluralForm.get( let localizedMessage = PluralForm.get(
this._unreadMsgCount, this._unreadMsgCount,
mozt.Utils.strings.GetStringFromName("tooltip.unread_messages")) mozt.Utils.strings.GetStringFromName("tooltip.unread_messages"))

28
src/modules/cairo.jsm Normal file
View File

@ -0,0 +1,28 @@
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
var EXPORTED_SYMBOLS = [ "cairo" ];
const CAIRO_LIBNAME = "cairo";
const CAIRO_ABIS = [ 2 ];
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/ctypes.jsm");
Cu.import("resource://moztray/ctypes-utils.jsm");
function cairo_defines(lib) {
this.cairo_t = ctypes.StructType("cairo_t");
lib.lazy_bind("cairo_rectangle", ctypes.void_t, this.cairo_t.ptr, ctypes.double, ctypes.double, ctypes.double, ctypes.double);
lib.lazy_bind("cairo_set_source_rgb", ctypes.void_t, this.cairo_t.ptr, ctypes.double, ctypes.double, ctypes.double);
lib.lazy_bind("cairo_fill", ctypes.void_t, this.cairo_t.ptr);
lib.lazy_bind("cairo_move_to", ctypes.void_t, this.cairo_t.ptr, ctypes.double, ctypes.double);
lib.lazy_bind("cairo_destroy", ctypes.void_t, this.cairo_t.ptr);
}
if (!cairo) {
var cairo = new ctypes_library(CAIRO_LIBNAME, CAIRO_ABIS, cairo_defines);
}

View File

@ -38,6 +38,18 @@ mozt.Utils = {
prefService: Services.prefs.getBranch("extensions.moztray."), prefService: Services.prefs.getBranch("extensions.moztray."),
strings: Services.strings.createBundle("chrome://moztray/locale/overlay.properties"), strings: Services.strings.createBundle("chrome://moztray/locale/overlay.properties"),
dumpObj: function(obj) {
let str = "";
for(i in obj) {
try {
str += "obj["+i+"]: " + obj[i] + "\n";
} catch(e) {
str += "obj["+i+"]: Unavailable\n";
}
}
LOG(str);
},
// adapted from http://forums.mozillazine.org/viewtopic.php?p=921150#921150 // adapted from http://forums.mozillazine.org/viewtopic.php?p=921150#921150
chromeToPath: function(aPath) { chromeToPath: function(aPath) {
if (!aPath || !(/^chrome:/.test(aPath))) if (!aPath || !(/^chrome:/.test(aPath)))

View File

@ -63,7 +63,9 @@ function ctypes_library(name, abis, defines) {
this.ABI = abi; this.ABI = abi;
LOG("Successfully loaded " + soname); LOG("Successfully loaded " + soname);
break; break;
} catch(e) {} } catch(e) {
ERROR(soname+" unfound.");
}
} }
this.close = function() { this.close = function() {

View File

@ -48,11 +48,12 @@ const Ci = Components.interfaces;
Cu.import("resource://gre/modules/ctypes.jsm"); Cu.import("resource://gre/modules/ctypes.jsm");
Cu.import("resource://moztray/ctypes-utils.jsm"); Cu.import("resource://moztray/ctypes-utils.jsm");
Cu.import("resource://moztray/cairo.jsm");
Cu.import("resource://moztray/glib.jsm"); Cu.import("resource://moztray/glib.jsm");
Cu.import("resource://moztray/gobject.jsm"); Cu.import("resource://moztray/gobject.jsm");
function gdk_defines(lib) { function gdk_defines(lib) {
this.GDK_INTERP_NEAREST = 1, // GdkInterpType this.GDK_INTERP_NEAREST = 1, // enum GdkInterpType
this.GdkWindow = ctypes.StructType("GdkWindow"); this.GdkWindow = ctypes.StructType("GdkWindow");
this.GdkByteOrder = ctypes.int; // enum this.GdkByteOrder = ctypes.int; // enum
@ -110,6 +111,7 @@ function gdk_defines(lib) {
this.GdkPixmap = ctypes.StructType("GdkPixmap"); this.GdkPixmap = ctypes.StructType("GdkPixmap");
this.GdkDrawable = ctypes.StructType("GdkDrawable"); this.GdkDrawable = ctypes.StructType("GdkDrawable");
this.GdkGC = ctypes.StructType("GdkGC"); this.GdkGC = ctypes.StructType("GdkGC");
this.GdkInterpType = ctypes.int;
lib.lazy_bind("gdk_window_new", this.GdkWindow.ptr, this.GdkWindow.ptr, this.GdkWindowAttributes.ptr, gobject.gint); lib.lazy_bind("gdk_window_new", this.GdkWindow.ptr, this.GdkWindow.ptr, this.GdkWindowAttributes.ptr, gobject.gint);
lib.lazy_bind("gdk_window_destroy", ctypes.void_t, this.GdkWindow.ptr); lib.lazy_bind("gdk_window_destroy", ctypes.void_t, this.GdkWindow.ptr);
@ -127,9 +129,17 @@ function gdk_defines(lib) {
lib.lazy_bind("gdk_color_parse", gobject.gboolean, gobject.gchar.ptr, this.GdkColor.ptr); lib.lazy_bind("gdk_color_parse", gobject.gboolean, gobject.gchar.ptr, this.GdkColor.ptr);
lib.lazy_bind("gdk_colormap_alloc_color", gobject.gboolean, this.GdkColormap.ptr, this.GdkColor.ptr, gobject.gboolean, gobject.gboolean); lib.lazy_bind("gdk_colormap_alloc_color", gobject.gboolean, this.GdkColormap.ptr, this.GdkColor.ptr, gobject.gboolean, gobject.gboolean);
lib.lazy_bind("gdk_pixmap_new", this.GdkPixmap.ptr, this.GdkDrawable.ptr, gobject.gint, gobject.gint, gobject.gint); lib.lazy_bind("gdk_pixmap_new", this.GdkPixmap.ptr, this.GdkDrawable.ptr, gobject.gint, gobject.gint, gobject.gint);
lib.lazy_bind("gdk_gc_new", this.GdkGC.ptr, this.GdkDrawable.ptr);
lib.lazy_bind("gdk_gc_set_foreground", ctypes.void_t, this.GdkGC.ptr, this.GdkColor.ptr); // DEPRECATED
lib.lazy_bind("gdk_draw_rectangle", ctypes.void_t, this.GdkDrawable.ptr, this.GdkGC.ptr, gobject.gboolean, gobject.gint, gobject.gint, gobject.gint, gobject.gint); // lib.lazy_bind("gdk_gc_new", this.GdkGC.ptr, this.GdkDrawable.ptr);
// lib.lazy_bind("gdk_gc_set_foreground", ctypes.void_t, this.GdkGC.ptr, this.GdkColor.ptr);
// lib.lazy_bind("gdk_draw_rectangle", ctypes.void_t, this.GdkDrawable.ptr, this.GdkGC.ptr, gobject.gboolean, gobject.gint, gobject.gint, gobject.gint, gobject.gint);
lib.lazy_bind("gdk_cairo_create", cairo.cairo_t.ptr, this.GdkDrawable.ptr);
lib.lazy_bind("gdk_cairo_set_source_color", ctypes.void_t, cairo.cairo_t.ptr, this.GdkColor.ptr);
lib.lazy_bind("gdk_pixbuf_get_from_drawable", this.GdkPixbuf.ptr, this.GdkPixbuf.ptr, this.GdkDrawable.ptr, this.GdkColormap.ptr, ctypes.int, ctypes.int, ctypes.int, ctypes.int, ctypes.int, ctypes.int);
lib.lazy_bind("gdk_pixbuf_add_alpha", this.GdkPixbuf.ptr, this.GdkPixbuf.ptr, gobject.gboolean, gobject.guchar, gobject.guchar, gobject.guchar);
lib.lazy_bind("gdk_pixbuf_composite", ctypes.void_t, this.GdkPixbuf.ptr, this.GdkPixbuf.ptr, ctypes.int, ctypes.int, ctypes.int, ctypes.int, ctypes.double, ctypes.double, ctypes.double, ctypes.double, this.GdkInterpType, ctypes.int);
} }

View File

@ -58,6 +58,7 @@ function gobject_defines(lib) {
this.guint16 = ctypes.uint16_t; this.guint16 = ctypes.uint16_t;
this.gint = ctypes.int; this.gint = ctypes.int;
this.gchar = ctypes.unsigned_char; this.gchar = ctypes.unsigned_char;
this.guchar = ctypes.unsigned_char;
this.gboolean = this.gint; this.gboolean = this.gint;
this.gfloat = ctypes.float; this.gfloat = ctypes.float;
this.GCallback = ctypes.voidptr_t; this.GCallback = ctypes.voidptr_t;

View File

@ -13,9 +13,11 @@ Cu.import("resource://gre/modules/ctypes.jsm");
Cu.import("resource://moztray/ctypes-utils.jsm"); Cu.import("resource://moztray/ctypes-utils.jsm");
Cu.import("resource://moztray/gdk.jsm"); Cu.import("resource://moztray/gdk.jsm");
Cu.import("resource://moztray/gobject.jsm"); Cu.import("resource://moztray/gobject.jsm");
Cu.import("resource://moztray/pango.jsm");
function gtk_defines(lib) { function gtk_defines(lib) {
this.GTK_ICON_SIZE_MENU = 1; this.GTK_ICON_SIZE_MENU = 1;
this.GTK_WINDOW_TOPLEVEL = 1;
this.GtkStatusIcon = ctypes.StructType("GtkStatusIcon"); this.GtkStatusIcon = ctypes.StructType("GtkStatusIcon");
this.GtkStyle = ctypes.StructType("GtkStyle"); this.GtkStyle = ctypes.StructType("GtkStyle");
@ -44,6 +46,8 @@ function gtk_defines(lib) {
// use ctypes.cast(menu, LibGtkStatusIcon.GtkMenuShell.ptr); // use ctypes.cast(menu, LibGtkStatusIcon.GtkMenuShell.ptr);
this.GtkMenuShell = ctypes.StructType("GtkMenuShell"); this.GtkMenuShell = ctypes.StructType("GtkMenuShell");
this.GtkImageMenuItem = ctypes.StructType("GtkImageMenuItem"); this.GtkImageMenuItem = ctypes.StructType("GtkImageMenuItem");
this.GtkWindow = ctypes.StructType("GtkWindow");
this.GtkWindowType = ctypes.int; // enum
this.GtkMenuPositionFunc_t = ctypes.FunctionType( this.GtkMenuPositionFunc_t = ctypes.FunctionType(
ctypes.default_abi, ctypes.void_t, ctypes.default_abi, ctypes.void_t,
@ -77,6 +81,11 @@ function gtk_defines(lib) {
this.GtkMenu.ptr, gobject.gint.ptr, gobject.gint.ptr, this.GtkMenu.ptr, gobject.gint.ptr, gobject.gint.ptr,
gobject.gboolean.ptr, gobject.gpointer); gobject.gboolean.ptr, gobject.gpointer);
lib.lazy_bind("gtk_window_new", this.GtkWidget.ptr, this.GtkWindowType);
lib.lazy_bind("gtk_widget_create_pango_layout", pango.PangoLayout.ptr, this.GtkWidget.ptr, gobject.gchar.ptr);
lib.lazy_bind("gtk_widget_destroy", ctypes.void_t, this.GtkWidget.ptr);
lib.lazy_bind("gtk_status_icon_set_from_pixbuf", ctypes.void_t, this.GtkStatusIcon.ptr, gdk.GdkPixbuf.ptr);
} }
if (!gtk) { if (!gtk) {

60
src/modules/pango.jsm Normal file
View File

@ -0,0 +1,60 @@
/* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
var EXPORTED_SYMBOLS = [ "pango", "pangocairo" ];
const PANGO_LIBNAME = "pango-1.0";
const PANGO_ABIS = [ 0 ];
const PANGOCAIRO_LIBNAME = "pangocairo-1.0";
const PANGOCAIRO_ABIS = [ 0 ];
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/ctypes.jsm");
Cu.import("resource://moztray/ctypes-utils.jsm");
Cu.import("resource://moztray/cairo.jsm");
Cu.import("resource://moztray/gobject.jsm");
function pango_defines(lib) {
this.PANGO_WEIGHT_THIN = 100,
this.PANGO_WEIGHT_ULTRALIGHT = 200,
this.PANGO_WEIGHT_LIGHT = 300,
this.PANGO_WEIGHT_BOOK = 380,
this.PANGO_WEIGHT_NORMAL = 400,
this.PANGO_WEIGHT_MEDIUM = 500,
this.PANGO_WEIGHT_SEMIBOLD = 600,
this.PANGO_WEIGHT_BOLD = 700,
this.PANGO_WEIGHT_ULTRABOLD = 800,
this.PANGO_WEIGHT_HEAVY = 900,
this.PANGO_WEIGHT_ULTRAHEAVY = 1000,
this.PANGO_SCALE = 1024,
this.PangoFontDescription = ctypes.StructType("PangoFontDescription");
this.PangoLayout = ctypes.StructType("PangoLayout");
this.PangoWeight = ctypes.int; // enum
lib.lazy_bind("pango_font_description_from_string", this.PangoFontDescription.ptr, ctypes.char.ptr);
lib.lazy_bind("pango_font_description_set_weight", ctypes.void_t, this.PangoFontDescription.ptr, this.PangoWeight);
lib.lazy_bind("pango_layout_set_spacing", ctypes.void_t, this.PangoLayout.ptr, ctypes.int);
lib.lazy_bind("pango_layout_set_font_description", ctypes.void_t, this.PangoLayout.ptr, this.PangoFontDescription.ptr);
lib.lazy_bind("pango_layout_set_text", ctypes.void_t, this.PangoLayout.ptr, ctypes.char.ptr, ctypes.int);
lib.lazy_bind("pango_layout_get_pixel_size", ctypes.void_t, this.PangoLayout.ptr, ctypes.int.ptr, ctypes.int.ptr);
lib.lazy_bind("pango_font_description_get_size", gobject.gint, this.PangoFontDescription.ptr);
lib.lazy_bind("pango_font_description_set_size", ctypes.void_t, this.PangoFontDescription.ptr, gobject.gint);
lib.lazy_bind("pango_font_description_free", ctypes.void_t, this.PangoFontDescription.ptr);
}
if (!pango) {
var pango = new ctypes_library(PANGO_LIBNAME, PANGO_ABIS, pango_defines);
}
function pangocairo_defines(lib) {
lib.lazy_bind("pango_cairo_show_layout", ctypes.void_t, cairo.cairo_t.ptr, pango.PangoLayout.ptr);
}
if (!pangocairo) {
var pangocairo = new ctypes_library(PANGOCAIRO_LIBNAME, PANGOCAIRO_ABIS, pangocairo_defines);
}

View File

@ -83,29 +83,27 @@ SetIconText(GtkStatusIcon *tray_icon, const char *text, const char *color) {
screen_depth = visual->depth; screen_depth = visual->depth;
GdkColor fore = { 0, 0, 0, 0 }; GdkColor fore = { 0, 0, 0, 0 };
GdkColor alpha = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF}; GdkColor alpha = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
gdk_color_parse (color, &fore) ) gdk_color_parse(color, &fore);
if(fore.red==alpha.red && fore.green==alpha.green && fore.blue==alpha.blue) { if(fore.red==alpha.red && fore.green==alpha.green && fore.blue==alpha.blue) {
alpha.red=0; //make sure alpha is different from fore alpha.red=0; // make sure alpha is different from fore
} }
gdk_colormap_alloc_color (cmap, &fore, TRUE, TRUE); gdk_colormap_alloc_color (cmap, &fore, TRUE, TRUE);
gdk_colormap_alloc_color (cmap, &alpha, TRUE, TRUE); gdk_colormap_alloc_color (cmap, &alpha, TRUE, TRUE);
// build pixmap with rectangle // build pixmap with rectangle
GdkPixmap *pm = gdk_pixmap_new (NULL, w, h, screen_depth); GdkPixmap *pm = gdk_pixmap_new (NULL, w, h, screen_depth);
GdkGC *gc = gdk_gc_new (pm); // graphic context. DEPRECATED ? cairo_t *cr = gdk_cairo_create(pm);
gdk_gc_set_foreground(gc,&alpha); gdk_cairo_set_source_color(cr, &alpha);
/* gdk_draw_rectangle(pm,gc,TRUE, 0, 0, w ,h ); */ /* void gdk_cairo_set_source_color (cairo_t *cr, */
cairo_t *cr; /* const GdkColor *color); */
cr = gdk_cairo_create(pm);
cairo_rectangle(cr, 0, 0, w, h); cairo_rectangle(cr, 0, 0, w, h);
/* void cairo_rectangle (cairo_t *cr, */ /* void cairo_rectangle (cairo_t *cr, */
/* double x, */ /* double x, */
/* double y, */ /* double y, */
/* double width, */ /* double width, */
/* double height); */ /* double height); */
cairo_set_source_rgb(cr, 1, 1, 1); /* TODO: consider cairo_set_source_rgba (notice the ending "a" for alpha) */ cairo_set_source_rgb(cr, 1, 1, 1);
cairo_fill(cr); cairo_fill(cr);
cairo_destroy(cr);
// build text // build text
GtkWidget *scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL); GtkWidget *scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@ -133,29 +131,29 @@ SetIconText(GtkStatusIcon *tray_icon, const char *text, const char *color) {
pango_layout_set_font_description (layout, fnt); pango_layout_set_font_description (layout, fnt);
pango_layout_get_pixel_size(layout, &tw, &th); pango_layout_get_pixel_size(layout, &tw, &th);
} }
pango_font_description_free (fnt);
// center text // center text
int px, py; int px, py;
px=(w-tw)/2; px=(w-tw)/2;
py=(h-th)/2; py=(h-th)/2;
// draw text on pixmap // draw text on pixmap
gdk_draw_layout_with_colors (pm, gc, px, py, layout, &fore,NULL); gdk_cairo_set_source_color(cr, &fore);
cairo_move_to (cr, px, py);
pango_cairo_show_layout (cr, layout);
cairo_destroy(cr);
g_object_unref (layout);
GdkPixbuf *buf = gdk_pixbuf_get_from_drawable (NULL, pm, NULL, 0, 0, 0, 0, w, h); GdkPixbuf *buf = gdk_pixbuf_get_from_drawable (NULL, pm, NULL, 0, 0, 0, 0, w, h);
g_object_unref (pm); g_object_unref (pm);
GdkPixbuf *alpha_buf = gdk_pixbuf_add_alpha (buf, TRUE, (guchar)alpha.red, (guchar)alpha.green, (guchar)alpha.blue); GdkPixbuf *alpha_buf = gdk_pixbuf_add_alpha(buf, TRUE, (guchar)alpha.red, (guchar)alpha.green, (guchar)alpha.blue);
// cleaning
g_object_unref (buf); g_object_unref (buf);
g_object_unref (layout);
pango_font_description_free (fnt);
g_object_unref (gc);
//merge the rendered text on top //merge the rendered text on top
gdk_pixbuf_composite (alpha_buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); gdk_pixbuf_composite(alpha_buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255);
g_object_unref(alpha_buf); g_object_unref(alpha_buf);
/* gdk_pixbuf_composite(buf,dest,0,0,w,h,0,0,1,1,GDK_INTERP_NEAREST,255); */
/* g_object_unref(buf); */
gtk_status_icon_set_from_pixbuf(GTK_STATUS_ICON(tray_icon), GDK_PIXBUF(dest)); gtk_status_icon_set_from_pixbuf(GTK_STATUS_ICON(tray_icon), GDK_PIXBUF(dest));
} }
@ -180,7 +178,7 @@ int main(int argc, char **argv) {
gtk_widget_show_all (window); gtk_widget_show_all (window);
/* TESTING */ /* TESTING */
SetIconText(tray_icon,"F", "#000000"); SetIconText(tray_icon,"1", "#000000");
gtk_main(); gtk_main();