Fix #362058. Check for illegal length in PPT filter.
[beagle.git] / Best / BestTray.cs
blob7860a3b3987f1b2b16c32b0e76644010697487fa
1 //
2 // Beagle tray icon.
3 //
4 //
5 // Copyright 2004 Novell, Inc.
6 //
8 using System;
9 using System.Collections;
10 using System.Diagnostics;
11 using System.Runtime.InteropServices;
13 using Gtk;
14 using Gdk;
16 using Mono.Unix;
18 using Beagle;
19 using Beagle.Tile;
20 using Beagle.Util;
22 namespace Best {
24 // This class is from Tomboy, removed some functions which weren't needed
25 public class GuiUtils
27 public static void GetMenuPosition (Gtk.Menu menu,
28 out int x,
29 out int y,
30 out bool push_in)
32 Gtk.Requisition menu_req = menu.SizeRequest ();
34 menu.AttachWidget.GdkWindow.GetOrigin (out x, out y);
36 if (y + menu_req.Height >= menu.AttachWidget.Screen.Height)
37 y -= menu_req.Height;
38 else
39 y += menu.AttachWidget.Allocation.Height;
41 push_in = true;
44 static void DeactivateMenu (object sender, EventArgs args)
46 Gtk.Menu menu = (Gtk.Menu) sender;
47 menu.Popdown ();
50 // Place the menu underneath an arbitrary parent widget. The
51 // parent widget must be set using menu.AttachToWidget before
52 // calling this
53 public static void PopupMenu (Gtk.Menu menu, Gdk.EventButton ev)
55 menu.Deactivated += DeactivateMenu;
56 menu.Popup (null,
57 null,
58 new Gtk.MenuPositionFunc (GetMenuPosition),
59 (ev == null) ? 0 : ev.Button,
60 (ev == null) ? Gtk.Global.CurrentEventTime : ev.Time);
65 public class BestTray : Gtk.Plug
67 BestWindow win;
68 bool autostarted = false;
70 Gtk.EventBox eventbox;
71 Gtk.Tooltips tips;
72 Beagle.Util.XKeybinder keybinder;
74 [DllImport ("libbeagleuiglue")]
75 private static extern IntPtr egg_tray_icon_new (string name);
77 public BestTray (BestWindow bw, bool autostarted)
79 this.autostarted = autostarted;
81 Raw = egg_tray_icon_new ("Search");
83 win = bw;
84 win.DeleteEvent += new DeleteEventHandler (WindowDeleteEvent);
86 eventbox = new Gtk.EventBox ();
87 eventbox.CanFocus = true;
88 eventbox.ButtonPressEvent += new ButtonPressEventHandler (ButtonPress);
90 Gdk.Pixbuf smalldog = Images.GetPixbuf ("best.png");
91 eventbox.Add (new Gtk.Image (smalldog.ScaleSimple (24, 24, Gdk.InterpType.Hyper)));
93 KeyBinding binding = Conf.Searching.ShowSearchWindowBinding;
95 string tooltip = String.Format ("Beagle Search ({0})", binding.ToReadableString ());
96 tips = new Gtk.Tooltips ();
97 tips.SetTip (eventbox, tooltip, null);
98 tips.Enable ();
100 Add (eventbox);
101 eventbox.ShowAll ();
103 keybinder = new Beagle.Util.XKeybinder ();
104 keybinder.Bind (binding.ToString (),
105 new EventHandler (ShowBeaglePressed));
108 private void ShowBeaglePressed (object o, EventArgs args)
110 if (!win.WindowIsVisible) {
111 win.Present ();
112 win.FocusEntry ();
113 } else {
114 win.Hide ();
118 void ButtonPress (object sender, Gtk.ButtonPressEventArgs args)
120 Gdk.EventButton eb = args.Event;
121 if (eb.Button == 1) {
122 if (! win.WindowIsVisible) {
123 win.Present ();
124 win.FocusEntry ();
125 } else {
126 win.Hide ();
128 } else {
130 Gtk.Menu recent_menu = MakeMenu ((Gtk.Widget) sender);
131 GuiUtils.PopupMenu (recent_menu, args.Event);
135 void WindowDeleteEvent (object sender, DeleteEventArgs args)
137 win.Hide ();
138 args.RetVal = (object)true;
141 void QuitEvent (object sender, EventArgs args)
143 if (autostarted) {
144 HigMessageDialog dialog = new HigMessageDialog (win,
145 DialogFlags.Modal,
146 MessageType.Question,
147 ButtonsType.YesNo,
148 Catalog.GetString ("Disable Searching"),
149 Catalog.GetString ("You are about to close the search tray. The search tray is automatically started at login-time. Would you like to disable it for future sessions?"));
151 Gtk.ResponseType response = (Gtk.ResponseType) dialog.Run ();
153 if (response == Gtk.ResponseType.Yes) {
154 Conf.Searching.Autostart = false;
155 Conf.Save (true);
157 // If the user doesn't want to have Best autostart, he probably doesn't
158 // want to keep the daemon around either. Bad call dude, bad call.
160 Process p = new Process ();
161 p.StartInfo.UseShellExecute = false;
162 p.StartInfo.FileName = "beagle-shutdown";
164 try {
165 p.Start ();
166 } catch (Exception ex) {}
169 win.StoreSettingsInConf (true);
170 Application.Quit ();
173 void QuickSearchEvent (object sender, EventArgs args)
175 string quickQuery = (string) menu_to_query_map [sender];
177 if (! win.WindowIsVisible) {
178 win.Present ();
179 win.QuickSearch (quickQuery);
180 } else {
181 win.QuickSearch (quickQuery);
186 private void ClearEvent (object sender, EventArgs args)
188 win.ClearHistory ();
191 private void DetachWidget (Gtk.Widget attach_widget, Gtk.Menu menu)
195 Hashtable menu_to_query_map = null;
197 private Gtk.Menu MakeMenu (Gtk.Widget parent)
199 Gtk.Menu menu = new Gtk.Menu ();
200 menu.AttachToWidget (parent, new Gtk.MenuDetachFunc (DetachWidget));
202 Gtk.ImageMenuItem item;
204 // Quick Search menu items
205 ArrayList list = win.RetriveSearches ();
206 if (list == null || list.Count == 0 ) {
207 item = new Gtk.ImageMenuItem (Catalog.GetString ("No Recent Searches"));
208 item.Sensitive = false;
209 menu.Append (item);
210 menu_to_query_map = null;
211 } else {
212 item = new Gtk.ImageMenuItem (Catalog.GetString ("Recent Searches"));
213 item.Sensitive = false;
214 item.Image = new Gtk.Image (Images.GetPixbuf ("icon-search.png"));
215 menu.Append (item);
217 menu_to_query_map = new Hashtable ();
219 foreach (string s in list) {
220 item = new Gtk.ImageMenuItem (s);
221 item.Activated += new EventHandler (QuickSearchEvent);
222 menu.Append (item);
223 menu_to_query_map [item] = s;
227 if (list != null && list.Count > 0) {
228 item = new Gtk.ImageMenuItem (Catalog.GetString ("Clear"));
229 item.Image = new Gtk.Image (Gtk.Stock.Clear, Gtk.IconSize.Menu);
230 item.Activated += new EventHandler (ClearEvent);
231 menu.Append (item);
234 menu.Append (new Gtk.SeparatorMenuItem ());
236 item = new Gtk.ImageMenuItem (Catalog.GetString ("Quit"));
237 item.Image = new Gtk.Image (Gtk.Stock.Quit, Gtk.IconSize.Menu);
238 item.Activated += new EventHandler (QuitEvent);
239 menu.Append (item);
241 menu.ShowAll ();
242 return menu;