Fixed #374055:Only the first "tag" is detected in digikam.
[beagle.git] / search / Tray / TrayIcon.cs
blob25a6147ad5cca8217918c13bc02fe7ab83af4275
1 //
2 // TrayIcon.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 using System;
8 using System.Collections;
9 using System.Runtime.InteropServices;
10 using Mono.Unix;
12 using Gtk;
13 using Gdk;
15 namespace Search.Tray
17 public delegate void SearchDelegate (string query);
19 public class TrayIcon
21 private EventBox eventbox;
22 private NotificationArea notification_area;
23 private Menu popup;
24 private ArrayList recent_searches;
25 private Hashtable menu_to_query_map;
27 public event EventHandler Clicked;
28 public SearchDelegate Search;
30 public TrayIcon ()
32 notification_area = new NotificationArea (Catalog.GetString ("Desktop Search"));
34 eventbox = new EventBox ();
35 eventbox.ButtonPressEvent += OnClick;
37 Gdk.Pixbuf pixbuf = Beagle.Images.GetPixbuf ("system-search.png");
38 eventbox.Add (new Gtk.Image (pixbuf.ScaleSimple (24, 24, Gdk.InterpType.Hyper)));
40 notification_area.Add (eventbox);
41 notification_area.ShowAll ();
43 recent_searches = new ArrayList ();
45 popup = MakeMenu (eventbox);
48 private Gtk.Menu MakeMenu (Gtk.Widget parent)
50 Gtk.Menu menu = new Gtk.Menu ();
51 menu.AttachToWidget (parent, new Gtk.MenuDetachFunc (DetachWidget));
53 Gtk.ImageMenuItem item;
55 // Quick Search menu items
56 if (recent_searches.Count == 0) {
57 item = new Gtk.ImageMenuItem (Catalog.GetString ("No Recent Searches"));
58 item.Sensitive = false;
59 menu.Append (item);
60 menu_to_query_map = null;
61 } else {
62 item = new Gtk.ImageMenuItem (Catalog.GetString ("Recent Searches"));
63 item.Sensitive = false;
64 item.Image = new Gtk.Image (Stock.Find, IconSize.Menu);
65 menu.Append (item);
67 menu_to_query_map = new Hashtable ();
69 foreach (string s in recent_searches) {
70 item = new Gtk.ImageMenuItem (s);
71 item.Activated += new EventHandler (OnSearch);
72 menu.Append (item);
73 menu_to_query_map [item] = s;
77 if (recent_searches.Count > 0) {
78 item = new Gtk.ImageMenuItem (Catalog.GetString ("Clear"));
79 item.Image = new Gtk.Image (Gtk.Stock.Clear, Gtk.IconSize.Menu);
80 item.Activated += new EventHandler (OnClear);
81 menu.Append (item);
84 menu.Append (new Gtk.SeparatorMenuItem ());
86 item = new Gtk.ImageMenuItem (Catalog.GetString ("Quit"));
87 item.Image = new Gtk.Image (Gtk.Stock.Quit, Gtk.IconSize.Menu);
88 item.Activated += new EventHandler (OnQuit);
89 menu.Append (item);
91 menu.ShowAll ();
92 return menu;
95 public void AddSearch (string query)
97 if (! recent_searches.Contains (query)) {
98 recent_searches.Add (query);
99 popup = MakeMenu (eventbox);
103 private void OnSearch (object sender, EventArgs args)
105 if (Search != null)
106 Search ((string) menu_to_query_map [sender]);
109 private void OnQuit (object sender, EventArgs args)
111 Application.Quit ();
114 private void OnClear (object sender, EventArgs args)
116 recent_searches.Clear ();
117 popup = MakeMenu (eventbox);
120 private void DetachWidget (Gtk.Widget attach_widget, Gtk.Menu menu)
124 public static void GetMenuPosition (Gtk.Menu menu, out int x, out int y, out bool push_in)
126 Gtk.Requisition menu_req = menu.SizeRequest ();
127 menu.AttachWidget.GdkWindow.GetOrigin (out x, out y);
129 if (y + menu_req.Height >= menu.AttachWidget.Screen.Height)
130 y -= menu_req.Height;
131 else
132 y += menu.AttachWidget.Allocation.Height;
134 push_in = true;
137 static void DeactivateMenu (object sender, EventArgs args)
139 Gtk.Menu menu = (Gtk.Menu) sender;
140 menu.Popdown ();
143 // Place the menu underneath an arbitrary parent widget. The
144 // parent widget must be set using menu.AttachToWidget before
145 // calling this
146 public static void PopupMenu (Gtk.Menu menu, Gdk.EventButton ev)
148 menu.Deactivated += DeactivateMenu;
149 menu.Popup (null, null, new Gtk.MenuPositionFunc (GetMenuPosition),
150 (ev == null) ? 0 : ev.Button,
151 (ev == null) ? Gtk.Global.CurrentEventTime : ev.Time);
154 private void OnClick (object o, ButtonPressEventArgs args)
156 if (args.Event.Type != EventType.ButtonPress)
157 return;
159 switch (args.Event.Button) {
160 case 1:
161 if (Clicked != null)
162 Clicked (o, args);
163 break;
164 case 3:
165 PopupMenu (popup, args.Event);
166 break;
169 args.RetVal = false;