4 // Copyright (C) 2005 Novell, Inc.
8 using System
.Collections
;
9 using System
.Runtime
.InteropServices
;
17 public delegate void SearchDelegate (string query
);
21 private EventBox eventbox
;
22 private NotificationArea notification_area
;
24 private ArrayList recent_searches
;
25 private Hashtable menu_to_query_map
;
27 public event EventHandler Clicked
;
28 public SearchDelegate Search
;
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;
60 menu_to_query_map
= null;
62 item
= new Gtk
.ImageMenuItem (Catalog
.GetString ("Recent Searches"));
63 item
.Sensitive
= false;
64 item
.Image
= new Gtk
.Image (Stock
.Find
, IconSize
.Menu
);
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
);
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
);
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
);
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
)
106 Search ((string) menu_to_query_map
[sender
]);
109 private void OnQuit (object sender
, EventArgs args
)
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
;
132 y
+= menu
.AttachWidget
.Allocation
.Height
;
137 static void DeactivateMenu (object sender
, EventArgs args
)
139 Gtk
.Menu menu
= (Gtk
.Menu
) sender
;
143 // Place the menu underneath an arbitrary parent widget. The
144 // parent widget must be set using menu.AttachToWidget before
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
)
159 switch (args
.Event
.Button
) {
165 PopupMenu (popup
, args
.Event
);