1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
/*
* Copyright (C) 2003, 2004, 2005 Jorn Baayen <jbaayen@gnome.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
using System;
using Gtk;
using Gdk;
using Mono.Unix;
using Muine.PluginLib;
namespace Muine
{
public class TrayIcon : Plugin
{
// Strings
private static readonly string string_program =
Catalog.GetString ("Muine music player");
// Strings :: Tooltip Format
// song artists - song title
private static readonly string string_tooltip_format =
Catalog.GetString ("{0} - {1}");
// Widgets
private Plug icon;
private EventBox ebox;
private Gtk.Image image;
private Tooltips tooltips;
private Menu menu;
// Objects
private IPlayer player;
// Variables
private int menu_x;
private int menu_y;
private string tooltip = "";
private bool playing = false;
// Plugin initializer
public override void Initialize (IPlayer player)
{
// Initialize gettext
Catalog.Init ("muine", Defines.GNOME_LOCALE_DIR);
// Load stock icons
InitStockIcons ();
// Connect to player
this.player = player;
player.SongChangedEvent += OnSongChangedEvent ;
player.StateChangedEvent += OnStateChangedEvent;
// Install "Hide Window" menu item
player.UIManager.AddUi (player.UIManager.NewMergeId (),
"/MenuBar/FileMenu/ExtraFileActions", "ToggleVisibleMenuItem",
"ToggleVisible", UIManagerItemType.Menuitem, false);
// Build menu
player.UIManager.AddUiFromResource ("TrayIcon.xml");
menu = (Menu) player.UIManager.GetWidget ("/Menu");
menu.Deactivated += OnMenuDeactivated;
// Init tooltips -- we init into "not playing" state
tooltips = new Tooltips ();
tooltips.Disable ();
// init icon
Init ();
}
// Methods
// Methods :: Public
// Methods :: Public :: Init
public void Init ()
{
icon = new NotificationArea (string_program);
icon.DestroyEvent += OnDestroyEvent;
ebox = new EventBox ();
ebox.ButtonPressEvent += OnButtonPressEvent;
image = new Gtk.Image ();
ebox.Add (image);
icon.Add (ebox);
UpdateImage ();
UpdateTooltip ();
icon.ShowAll ();
}
// Methods :: Private
// Methods :: Private :: UpdateTooltip
private void UpdateTooltip ()
{
tooltips.SetTip (icon, tooltip, null);
}
// Methods :: Private :: UpdateImage
private void UpdateImage ()
{
string icon = (playing) ? "muine-tray-playing" : "muine-tray-paused";
image.SetFromStock (icon, IconSize.Menu);
}
// Methods :: Private :: PositionMenu
private void PositionMenu (Menu menu, out int x, out int y, out bool push_in)
{
x = menu_x;
y = menu_y;
int monitor = ((Widget) menu).Screen.GetMonitorAtPoint (x, y );
Gdk.Rectangle rect = ((Widget) menu).Screen.GetMonitorGeometry (monitor);
int space_above = y - rect.Y;
int space_below = rect.Y + rect.Height - y;
Requisition requisition = menu.SizeRequest ();
if (requisition.Height <= space_above ||
requisition.Height <= space_below) {
if (requisition.Height <= space_below)
y += ebox.Allocation.Height;
else
y -= requisition.Height;
} else if (requisition.Height > space_below &&
requisition.Height > space_above) {
if (space_below >= space_above)
y = rect.Y + rect.Height - requisition.Height;
else
y = rect.Y;
} else {
y = rect.Y;
}
push_in = true;
}
// Methods :: Private :: CreateTooltip
private string CreateTooltip (ISong song)
{
return String.Format (string_tooltip_format,
StringUtils.JoinHumanReadable (song.Artists), song.Title);
}
// Methods :: Private :: InitStockIcons
private void InitStockIcons ()
{
string [] stock_icons = {
"muine-tray-paused",
"muine-tray-playing"
};
IconFactory factory = new IconFactory ();
factory.AddDefault ();
// Stock Icons
foreach (string name in stock_icons) {
Pixbuf pixbuf = new Pixbuf (null, name + ".png");
IconSet iconset = new IconSet (pixbuf);
factory.Add (name, iconset);
}
}
// Handlers
// Handlers :: OnButtonPressEvent
private void OnButtonPressEvent (object o, ButtonPressEventArgs args)
{
if (args.Event.Type != EventType.ButtonPress)
return;
switch (args.Event.Button)
{
case 1:
case 3:
icon.State = StateType.Active;
menu_x = (int) args.Event.XRoot - (int) args.Event.X;
menu_y = (int) args.Event.YRoot - (int) args.Event.Y;
menu.Popup (null, null, new MenuPositionFunc (PositionMenu),
args.Event.Button, args.Event.Time);
break;
case 2:
player.SetWindowVisible (!player.WindowVisible, args.Event.Time);
break;
default:
break;
}
args.RetVal = false;
}
// Handlers :: OnMenuDeactivated
private void OnMenuDeactivated (object o, EventArgs args)
{
icon.State = StateType.Normal;
}
// Handlers :: OnDestroyEvent
private void OnDestroyEvent (object o, DestroyEventArgs args)
{
Init ();
}
// Handlers :: OnSongChangedEvent
private void OnSongChangedEvent (ISong song)
{
tooltip = (song == null) ? null : CreateTooltip (song);
UpdateTooltip ();
}
// Handlers :: OnStateChangedEvent
private void OnStateChangedEvent (bool playing)
{
if (playing)
tooltips.Enable ();
else
tooltips.Disable ();
this.playing = playing;
UpdateImage ();
}
}
}
|