~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/Do.Interface.Widgets/ResultsWindow.cs

  • Committer: Hardeep S
  • Date: 2009-06-23 03:53:12 UTC
  • Revision ID: ootz0rz@gmail.com-20090623035312-it8tb5wkha6nf31p
Added Do.Interface.Windows, and a bunch of cleanup with old MonoDevelop files elsewhere

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ResultsWindow.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this source distribution.
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
using System;
 
21
using System.Collections.Generic;
 
22
using System.Linq;
 
23
using System.Runtime.InteropServices;
 
24
using System.Text;
 
25
 
 
26
using Gtk;
 
27
using Gdk;
 
28
 
 
29
using Do.Universe;
 
30
using Do.Platform;
 
31
using Do.Interface;
 
32
 
 
33
namespace Do.Interface.Widgets
 
34
{
 
35
        public delegate void OnSelectionChanged (object sender, 
 
36
                                                 ResultsWindowSelectionEventArgs args);
 
37
 
 
38
        public class ResultsWindow : Gtk.Window
 
39
        {
 
40
                protected int DefaultResultIconSize = 32;
 
41
                protected int DefaultWindowWidth = 352;
 
42
                protected int NumberResultsDisplayed = 6;
 
43
                
 
44
                protected int offset;
 
45
                
 
46
                public string ResultInfoFormat
 
47
                {
 
48
                        set { resultInfoFormat = value; }
 
49
                        get { return resultInfoFormat; }
 
50
                }
 
51
                
 
52
                protected string resultInfoFormat = "<b>{0}</b>\n<small>{1}</small>";
 
53
                const string QueryLabelFormat = "<b>{0}</b>";
 
54
 
 
55
                public event OnSelectionChanged SelectionChanged;
 
56
 
 
57
                protected enum Column {
 
58
                        ItemColumn = 0,
 
59
                        NameColumn = 1,
 
60
                        NumberColumns = 2
 
61
                }
 
62
 
 
63
                protected ScrolledWindow resultsScrolledWindow;
 
64
                protected TreeView resultsTreeview;
 
65
                protected IList<Do.Universe.Item> results, stunted_results;
 
66
                protected int startResult, endResult;
 
67
                protected Frame frame;
 
68
                protected string query;
 
69
                protected Gdk.Color backgroundColor;
 
70
                protected VBox vbox;
 
71
                protected Toolbar toolbar;
 
72
                protected Label resultsLabel, queryLabel;
 
73
                protected IUIContext context = null;
 
74
                
 
75
                protected int cursor;
 
76
                protected int[] secondary = new int[0];
 
77
                
 
78
                protected bool pushedUpdate, clearing, update_needed = false;
 
79
 
 
80
 
 
81
                public ResultsWindow (Gdk.Color backgroundColor, int NumberResults) 
 
82
                        : base (Gtk.WindowType.Toplevel)
 
83
                {
 
84
                        this.backgroundColor = backgroundColor;
 
85
                        this.NumberResultsDisplayed = NumberResults;
 
86
                        
 
87
                        Build ();
 
88
                        results = new Do.Universe.Item[0];
 
89
                }
 
90
                
 
91
                public ResultsWindow (Gdk.Color backgroundColor, int DefaultIconSize, 
 
92
                                      int WindowWidth, int NumberResults) 
 
93
                        : base (Gtk.WindowType.Toplevel)
 
94
                {
 
95
                        this.backgroundColor = backgroundColor;
 
96
                        this.DefaultResultIconSize = DefaultIconSize;
 
97
                        this.DefaultWindowWidth = WindowWidth;
 
98
                        this.NumberResultsDisplayed = NumberResults;
 
99
                        
 
100
                        Build ();
 
101
                        results = new Do.Universe.Item[0];
 
102
                }
 
103
 
 
104
                protected void NotifySelectionChanged ()
 
105
                {
 
106
                        TreeIter iter;
 
107
                        if (!resultsTreeview.Selection.GetSelected (out iter)) return;
 
108
                        try {
 
109
                                cursor = Convert.ToInt32 (resultsTreeview.Model.GetStringFromIter (iter));
 
110
                        } catch { return; }
 
111
                        
 
112
                        ResultsWindowSelectionEventArgs args;
 
113
 
 
114
                        args = new ResultsWindowSelectionEventArgs (SelectedIndex, SelectedObject);
 
115
                        if (null != SelectionChanged) {
 
116
                                SelectionChanged (this, args);
 
117
                        }
 
118
                }
 
119
 
 
120
                protected virtual void Build ()
 
121
                {
 
122
                        Alignment align;
 
123
                        TreeViewColumn column;
 
124
                        CellRenderer   cell;
 
125
                        HBox hbox;
 
126
 
 
127
                        KeepAbove = true;
 
128
                        AppPaintable = true;
 
129
                        AcceptFocus = false;
 
130
                        // This typehint gets the window to raise all the way to top.
 
131
                        TypeHint = WindowTypeHint.Splashscreen;
 
132
                        
 
133
                        SetColormap ();
 
134
                        
 
135
                        frame = new Frame ();
 
136
                        frame.DrawFill = true;
 
137
                        frame.DrawFrame = true;
 
138
                        frame.FillColor = frame.FrameColor = backgroundColor;
 
139
                        frame.FillAlpha = .55;
 
140
                        frame.FrameAlpha = .7;
 
141
                        frame.Radius = 0;
 
142
                        frame.Show ();
 
143
                        
 
144
                        vbox = new VBox (false, 0);
 
145
                        Add (frame);
 
146
                        frame.Add (vbox);
 
147
                        vbox.BorderWidth = 3;
 
148
                        vbox.Show ();
 
149
                        
 
150
                        //---------The breadcrum bar---------
 
151
                        hbox = new HBox ();
 
152
                        toolbar = new Toolbar ();
 
153
                        align = new Alignment (0, .5f, 0, 0);
 
154
                        resultsLabel = new Label ();
 
155
                        queryLabel = new Label ();
 
156
                        align.Add (queryLabel);
 
157
                        hbox.PackStart (align, true, true, 4);
 
158
                        hbox.PackStart (resultsLabel, false, false, 0);
 
159
                        hbox.WidthRequest = DefaultWindowWidth - 10;
 
160
                        toolbar.Add (hbox);
 
161
                        toolbar.ShowAll ();
 
162
                        vbox.PackStart (toolbar, false, false, 0);
 
163
                        
 
164
                        
 
165
                        //---------Results Window
 
166
                        resultsScrolledWindow = new ScrolledWindow ();
 
167
                        resultsScrolledWindow.SetPolicy (PolicyType.Never, PolicyType.Never);
 
168
                        resultsScrolledWindow.ShadowType = ShadowType.None;
 
169
                        vbox.PackStart (resultsScrolledWindow, true, true, 0);
 
170
                        resultsScrolledWindow.Show ();
 
171
 
 
172
                        resultsTreeview = new TreeView ();
 
173
                        resultsTreeview.EnableSearch = false;
 
174
                        resultsTreeview.HeadersVisible = false;
 
175
                        // If this is not set the tree will call IconDataFunc for all rows to 
 
176
                        // determine the total height of the tree
 
177
                        resultsTreeview.FixedHeightMode = true;
 
178
                        
 
179
                        resultsScrolledWindow.Add (resultsTreeview);
 
180
                        resultsTreeview.Show ();
 
181
 
 
182
                        resultsTreeview.Model = new ListStore (new Type[] {
 
183
                                typeof (Do.Universe.Item),                              
 
184
                                typeof (string), 
 
185
                        });
 
186
 
 
187
                        column = new TreeViewColumn ();                 
 
188
                        column.Sizing = Gtk.TreeViewColumnSizing.Fixed; 
 
189
                                // because resultsTreeview.FixedHeightMode = true:  
 
190
                                
 
191
                        cell = new CellRendererPixbuf ();                               
 
192
                        cell.SetFixedSize (-1, 4 + DefaultResultIconSize - (int) cell.Ypad);
 
193
 
 
194
                        int width, height;
 
195
                        cell.GetFixedSize (out width, out height);
 
196
                                
 
197
                        column.PackStart (cell, false);
 
198
                        column.SetCellDataFunc (cell, new TreeCellDataFunc (IconDataFunc));
 
199
                                
 
200
                        vbox.SetSizeRequest (DefaultWindowWidth, 
 
201
                                             (height + 2) * NumberResultsDisplayed + 
 
202
                                             (int) (vbox.BorderWidth * 2) + 20);
 
203
                        
 
204
                        cell = new CellRendererText ();
 
205
                        (cell as CellRendererText).Ellipsize = Pango.EllipsizeMode.End;
 
206
                        column.PackStart (cell, true);
 
207
                        column.AddAttribute (cell, "markup", (int) Column.NameColumn);
 
208
                        
 
209
                        resultsTreeview.AppendColumn (column);
 
210
 
 
211
                        resultsTreeview.Selection.Changed += OnResultRowSelected;
 
212
                        Shown += OnShown;
 
213
                }
 
214
                
 
215
                        
 
216
                public void UpdateColors (Gdk.Color backgroundColor)
 
217
                {
 
218
                        this.backgroundColor = backgroundColor;
 
219
                        frame.FillColor = backgroundColor;
 
220
                }
 
221
                                                
 
222
                protected void IconDataFunc (TreeViewColumn column, CellRenderer cell, 
 
223
                                           TreeModel model, TreeIter iter)
 
224
                {                       
 
225
                        CellRendererPixbuf renderer = cell as CellRendererPixbuf;
 
226
                        Do.Universe.Item o = (resultsTreeview.Model as ListStore).GetValue (iter, 0) as Do.Universe.Item;
 
227
                        bool isSecondary = false;
 
228
                        foreach (int i in secondary)
 
229
                                if (model.GetStringFromIter (iter) == i.ToString ())
 
230
                                        isSecondary = true;
 
231
                        
 
232
                        Gdk.Pixbuf final;
 
233
                        if (isSecondary) {
 
234
                                using (Gdk.Pixbuf source = IconProvider.PixbufFromIconName (o.Icon, DefaultResultIconSize))
 
235
                                using (Gdk.Pixbuf emblem = IconProvider.PixbufFromIconName ("gtk-add", DefaultResultIconSize)) {
 
236
                                        final = new Pixbuf (Colorspace.Rgb, 
 
237
                                                            true, 
 
238
                                                            8,
 
239
                                                            DefaultResultIconSize,
 
240
                                                            DefaultResultIconSize);
 
241
                                        
 
242
                                        source.CopyArea (0, 0, source.Width, source.Height, final, 0, 0);
 
243
                                        
 
244
                                        emblem.Composite (final, 
 
245
                                                          0, 
 
246
                                                          0, 
 
247
                                                          DefaultResultIconSize, 
 
248
                                                          DefaultResultIconSize, 
 
249
                                                          0, 
 
250
                                                          0, 
 
251
                                                          1,
 
252
                                                          1, 
 
253
                                                          InterpType.Bilinear, 
 
254
                                                          220);
 
255
                                }
 
256
                        } else {
 
257
                                final = IconProvider.PixbufFromIconName (o.Icon, DefaultResultIconSize);
 
258
                        }
 
259
                        renderer.Pixbuf = final;
 
260
                        final.Dispose ();
 
261
                }
 
262
 
 
263
                protected void OnResultRowSelected (object sender, EventArgs args)
 
264
                {
 
265
                        if (!clearing && !pushedUpdate) {
 
266
                                NotifySelectionChanged ();
 
267
                        }
 
268
                }
 
269
 
 
270
                public virtual void Clear ()
 
271
                {
 
272
                        (resultsTreeview.Model as ListStore).Clear ();
 
273
                        cursor = 0;
 
274
                        resultsLabel.Markup = "--/--";
 
275
                        queryLabel.Markup = "";
 
276
                        update_needed = false;
 
277
                }
 
278
 
 
279
                public IUIContext Context
 
280
                {
 
281
                        set {
 
282
                                context = value;
 
283
                                if (!Visible) {
 
284
                                        update_needed = true;
 
285
                                        return;
 
286
                                }
 
287
                                
 
288
                                pushedUpdate = true;
 
289
                                if (value == null || !value.Results.Any ()) {
 
290
                                        Results = new Do.Universe.Item [0];
 
291
                                        return;
 
292
                                }
 
293
                                
 
294
                                if (results.GetHashCode () != value.Results.GetHashCode ()) {
 
295
                                        results = value.Results;
 
296
                                }
 
297
                                
 
298
                                startResult = value.Cursor - 5;
 
299
                                
 
300
                                if (startResult < 0)
 
301
                                        startResult = 0;
 
302
                                endResult = startResult + 8;
 
303
                                offset = startResult;
 
304
                                
 
305
                                if (endResult > results.Count)
 
306
                                        endResult = results.Count;
 
307
                                
 
308
                                Do.Universe.Item[] resultsArray = new Do.Universe.Item[endResult - startResult];
 
309
                                Array.Copy (results.ToArray (), startResult, resultsArray, 0, resultsArray.Length); 
 
310
                                
 
311
                                cursor = value.Cursor - offset;
 
312
                                
 
313
                                Results = resultsArray;
 
314
                                
 
315
                                Query = value.Query;
 
316
                                
 
317
                                
 
318
                                
 
319
                                int[] secArray = new int[value.SecondaryCursors.Length];
 
320
                                for (int i=0; i<secArray.Length; i++) {
 
321
                                        secArray[i] = value.SecondaryCursors[i] - offset;
 
322
                                }
 
323
                                
 
324
                                secondary = secArray;
 
325
                                
 
326
                                
 
327
                                UpdateCursors ();
 
328
                                UpdateQueryLabel (value);
 
329
                                resultsLabel.Markup = string.Format ("{1}/{0}", 
 
330
                                                                     value.Results.Count, 
 
331
                                                                     value.Cursor + 1);
 
332
                                Gtk.Application.Invoke (delegate {
 
333
                                        pushedUpdate = false;
 
334
                                });
 
335
                        }
 
336
                }
 
337
 
 
338
                public int SelectedIndex
 
339
                {
 
340
                        get { return cursor + offset; }
 
341
                        set { 
 
342
                                cursor = value - offset;
 
343
                                
 
344
                                UpdateCursors ();
 
345
                        }
 
346
                }
 
347
                        
 
348
                protected void UpdateQueryLabel (IUIContext context)
 
349
                {
 
350
                        string query = context.Query;
 
351
                        StringBuilder builder = new StringBuilder ();
 
352
                        
 
353
                        int count = 0;
 
354
                        while (context.ParentContext != null && count < 2) {
 
355
                                builder.Insert (0, context.ParentContext.Selection.Name + " > ");
 
356
                                context = context.ParentContext;
 
357
                                count++;
 
358
                        }
 
359
                        queryLabel.Markup = string.Format ("{0}<b>{1}</b>", 
 
360
                                                           GLib.Markup.EscapeText (builder.ToString ()),
 
361
                                                           GLib.Markup.EscapeText (query)); 
 
362
                }
 
363
                
 
364
                private void UpdateCursors () 
 
365
                {
 
366
 
 
367
                        Gtk.TreePath path;
 
368
                        
 
369
                        path = new TreePath (cursor.ToString ());
 
370
                        
 
371
                        //makes this just a tiny bit smoother overall
 
372
                        Gtk.Application.Invoke (delegate {
 
373
                                resultsTreeview.Selection.UnselectAll ();
 
374
                                resultsTreeview.Selection.SelectPath (path);
 
375
                                resultsTreeview.ScrollToCell (path, null, true, 0.5F, 0.0F);
 
376
                        });
 
377
                }
 
378
                
 
379
                public Do.Universe.Item SelectedObject
 
380
                {
 
381
                        get {
 
382
                                try {
 
383
                                        return results [SelectedIndex];
 
384
                                } catch {
 
385
                                        return null;
 
386
                                }
 
387
                        }
 
388
                }
 
389
                
 
390
                public IList<Do.Universe.Item> Results
 
391
                {
 
392
                        get {
 
393
                                if (stunted_results == null)
 
394
                                        stunted_results = new List<Do.Universe.Item> (0);
 
395
                                return stunted_results;
 
396
                        }
 
397
                        set {
 
398
                                stunted_results = value;
 
399
                                //some memory hacks.
 
400
                                foreach (CellRenderer rend in resultsTreeview.Columns[0].CellRenderers) {
 
401
                                        if (rend is CellRendererPixbuf && (rend as CellRendererPixbuf).Pixbuf != null) {
 
402
                                                (rend as CellRendererPixbuf).Pixbuf.Dispose ();
 
403
                                        }
 
404
                                        rend.Dispose ();
 
405
                                }
 
406
                                
 
407
                                ListStore store;
 
408
                                string info;
 
409
 
 
410
                                clearing = true;
 
411
                                Gtk.Application.Invoke (delegate {
 
412
                                        store = resultsTreeview.Model as ListStore;
 
413
                                        store.Clear ();
 
414
                                        
 
415
                                        foreach (Do.Universe.Item result in value) {                                    
 
416
                                                
 
417
                                                info = string.Format (ResultInfoFormat, 
 
418
                                                                      GLib.Markup.EscapeText (result.Name), 
 
419
                                                                      GLib.Markup.EscapeText (result.Description)); 
 
420
                                                store.AppendValues (new object[] {
 
421
                                                        result,
 
422
                                                        info,
 
423
                                                });
 
424
                                                
 
425
                                        }
 
426
                                        clearing = false;
 
427
                                });
 
428
//                              UpdateCursors ();
 
429
                        }
 
430
                }
 
431
 
 
432
 
 
433
                public string Query
 
434
                {
 
435
                        set {
 
436
                                query = value ?? "";
 
437
                                queryLabel.Markup = string.Format (QueryLabelFormat, GLib.Markup.EscapeText (query)); 
 
438
                        }
 
439
                        get { return query; }
 
440
                }
 
441
                
 
442
                protected void OnShown (object o, EventArgs args)
 
443
                {
 
444
                        if (update_needed)
 
445
                                Context = context;
 
446
                        update_needed = false;
 
447
                }
 
448
 
 
449
                // Draw a border around the window.
 
450
                protected override bool OnExposeEvent (EventExpose evnt)
 
451
                {
 
452
                        Cairo.Context cairo;
 
453
                        
 
454
                        using (cairo = Gdk.CairoHelper.Create (GdkWindow)) {
 
455
                                cairo.Rectangle (evnt.Area.X, evnt.Area.Y, evnt.Area.Width, evnt.Area.Height);
 
456
                                cairo.Color = new Cairo.Color (1.0, 1.0, 1.0, 0.0);
 
457
                                cairo.Operator = Cairo.Operator.Source;
 
458
                                cairo.Paint ();
 
459
                        }
 
460
 
 
461
                        return base.OnExposeEvent (evnt);
 
462
                }
 
463
                
 
464
                protected virtual void SetColormap ()
 
465
                {
 
466
                        Gdk.Colormap  colormap;
 
467
 
 
468
                        colormap = Screen.RgbaColormap;
 
469
                        if (colormap == null) {
 
470
                                colormap = Screen.RgbColormap;
 
471
                                Console.Error.WriteLine ("No alpha support.");
 
472
                        }
 
473
                        Colormap = colormap;
 
474
                        colormap.Dispose ();
 
475
                }
 
476
        }
 
477
}