2006-02-27 Gabor Kelemen <kelemeng@gnome.hu>
[beagle.git] / search / GroupView.cs
blob30d1a449db62373aaaad930ecd5127c77ce41fae
1 using System;
2 using System.Collections;
4 using Mono.Posix;
5 using Gtk;
6 using Beagle;
7 using Search.Tiles;
9 namespace Search {
11 public delegate void TileHandler (Tiles.Tile tile);
13 public enum MatchType {
14 None,
15 NoneInScope,
16 Matched
19 public class GroupView : VBox {
21 public event TileHandler TileSelected;
22 private Gtk.SizeGroup tileSizeGroup;
23 private Hashtable categories;
24 private Gtk.Widget selection;
26 public GroupView () : base (false, 0)
28 categories = new Hashtable ();
29 tileSizeGroup = new Gtk.SizeGroup (Gtk.SizeGroupMode.Both);
30 Category box = null;
32 foreach (Tiles.TileGroupInfo info in Tiles.Utils.GroupInfo) {
34 if (info.Group == Tiles.TileGroup.Conversations)
35 box = new ConversationCategory (info.Name, info.Rows);
36 else
37 box = new TileCategory (info.Name, info.Rows, tileSizeGroup);
39 PackStart (box, false, false, 0);
40 box.NoShowAll = true;
41 categories [info.Group] = box;
44 // FIXME: Add the Best match category
47 public void AddHit (Tiles.Tile tile)
49 tile.Show ();
50 tile.Selected += OnTileSelected;
51 tile.Deselected += OnTileDeselected;
53 Category box = (Category)categories [tile.Group];
54 box.Add (tile);
56 if (GroupInScope (tile.Group))
57 box.Show ();
60 public void SubtractHit (Uri uri)
62 foreach (Category box in categories.Values) {
63 foreach (Tile tile in box.AllTiles) {
64 if (tile.Hit.Uri.Equals (uri)) {
65 box.Remove (tile);
66 return;
72 public void Finished (bool grabFocus)
74 Category first = null;
75 bool others = false;
77 foreach (Category category in categories.Values) {
78 if (category.Visible) {
79 if (first == null)
80 first = category;
81 else {
82 others = true;
83 break;
88 if (first != null)
89 first.Select (grabFocus, !others);
92 public MatchType MatchState {
93 get {
94 bool hiddenMatches = false;
95 foreach (Category category in categories.Values) {
96 if (category.Visible)
97 return MatchType.Matched;
98 else if (!category.Empty)
99 hiddenMatches = true;
102 return hiddenMatches ? MatchType.NoneInScope : MatchType.None;
106 private void OnTileSelected (object tile, EventArgs args)
108 if (tile == selection)
109 return;
111 if (TileSelected != null)
112 TileSelected ((Tiles.Tile)tile);
114 if (selection != null)
115 selection.State = StateType.Normal;
116 selection = (Gtk.Widget)tile;
117 if (selection != null)
118 selection.State = StateType.Selected;
121 private void OnTileDeselected (object tile, EventArgs args)
123 if (tile != selection)
124 return;
125 OnTileSelected (null, args);
128 private ScopeType scope;
129 public ScopeType Scope {
130 set {
131 scope = value;
132 foreach (TileGroup group in categories.Keys) {
133 Category category = (Category)categories[group];
134 if (!GroupInScope (group))
135 category.Hide ();
136 else if (!category.Empty)
137 category.Show ();
142 private bool GroupInScope (TileGroup group)
144 switch (scope) {
145 case ScopeType.Everywhere:
146 return true;
148 case ScopeType.Applications:
149 return group == TileGroup.Application;
151 case ScopeType.Contacts:
152 return group == TileGroup.Contact;
154 case ScopeType.Documents:
155 return group == TileGroup.Documents;
157 case ScopeType.Conversations:
158 return group == TileGroup.Conversations;
160 case ScopeType.Images:
161 return group == TileGroup.Image;
163 case ScopeType.Media:
164 return group == TileGroup.Audio || group == TileGroup.Video;
167 return false;
170 public SortType Sort {
171 set {
172 foreach (Category category in categories.Values)
173 category.Sort = value;
177 public void Clear ()
179 foreach (Category box in categories.Values) {
180 box.Clear ();
181 box.Hide ();