3
* GNOME Do is the legal property of its developers. Please refer to the
4
* COPYRIGHT file distributed with this source distribution.
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.
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.
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/>.
21
using System.Collections.Generic;
23
using System.Runtime.InteropServices;
33
namespace Do.Interface.Widgets
35
public delegate void OnSelectionChanged (object sender,
36
ResultsWindowSelectionEventArgs args);
38
public class ResultsWindow : Gtk.Window
40
protected int DefaultResultIconSize = 32;
41
protected int DefaultWindowWidth = 352;
42
protected int NumberResultsDisplayed = 6;
46
public string ResultInfoFormat
48
set { resultInfoFormat = value; }
49
get { return resultInfoFormat; }
52
protected string resultInfoFormat = "<b>{0}</b>\n<small>{1}</small>";
53
const string QueryLabelFormat = "<b>{0}</b>";
55
public event OnSelectionChanged SelectionChanged;
57
protected enum Column {
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;
71
protected Toolbar toolbar;
72
protected Label resultsLabel, queryLabel;
73
protected IUIContext context = null;
76
protected int[] secondary = new int[0];
78
protected bool pushedUpdate, clearing, update_needed = false;
81
public ResultsWindow (Gdk.Color backgroundColor, int NumberResults)
82
: base (Gtk.WindowType.Toplevel)
84
this.backgroundColor = backgroundColor;
85
this.NumberResultsDisplayed = NumberResults;
88
results = new Do.Universe.Item[0];
91
public ResultsWindow (Gdk.Color backgroundColor, int DefaultIconSize,
92
int WindowWidth, int NumberResults)
93
: base (Gtk.WindowType.Toplevel)
95
this.backgroundColor = backgroundColor;
96
this.DefaultResultIconSize = DefaultIconSize;
97
this.DefaultWindowWidth = WindowWidth;
98
this.NumberResultsDisplayed = NumberResults;
101
results = new Do.Universe.Item[0];
104
protected void NotifySelectionChanged ()
107
if (!resultsTreeview.Selection.GetSelected (out iter)) return;
109
cursor = Convert.ToInt32 (resultsTreeview.Model.GetStringFromIter (iter));
112
ResultsWindowSelectionEventArgs args;
114
args = new ResultsWindowSelectionEventArgs (SelectedIndex, SelectedObject);
115
if (null != SelectionChanged) {
116
SelectionChanged (this, args);
120
protected virtual void Build ()
123
TreeViewColumn column;
130
// This typehint gets the window to raise all the way to top.
131
TypeHint = WindowTypeHint.Splashscreen;
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;
144
vbox = new VBox (false, 0);
147
vbox.BorderWidth = 3;
150
//---------The breadcrum bar---------
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;
162
vbox.PackStart (toolbar, false, false, 0);
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 ();
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;
179
resultsScrolledWindow.Add (resultsTreeview);
180
resultsTreeview.Show ();
182
resultsTreeview.Model = new ListStore (new Type[] {
183
typeof (Do.Universe.Item),
187
column = new TreeViewColumn ();
188
column.Sizing = Gtk.TreeViewColumnSizing.Fixed;
189
// because resultsTreeview.FixedHeightMode = true:
191
cell = new CellRendererPixbuf ();
192
cell.SetFixedSize (-1, 4 + DefaultResultIconSize - (int) cell.Ypad);
195
cell.GetFixedSize (out width, out height);
197
column.PackStart (cell, false);
198
column.SetCellDataFunc (cell, new TreeCellDataFunc (IconDataFunc));
200
vbox.SetSizeRequest (DefaultWindowWidth,
201
(height + 2) * NumberResultsDisplayed +
202
(int) (vbox.BorderWidth * 2) + 20);
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);
209
resultsTreeview.AppendColumn (column);
211
resultsTreeview.Selection.Changed += OnResultRowSelected;
216
public void UpdateColors (Gdk.Color backgroundColor)
218
this.backgroundColor = backgroundColor;
219
frame.FillColor = backgroundColor;
222
protected void IconDataFunc (TreeViewColumn column, CellRenderer cell,
223
TreeModel model, TreeIter iter)
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 ())
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,
239
DefaultResultIconSize,
240
DefaultResultIconSize);
242
source.CopyArea (0, 0, source.Width, source.Height, final, 0, 0);
244
emblem.Composite (final,
247
DefaultResultIconSize,
248
DefaultResultIconSize,
257
final = IconProvider.PixbufFromIconName (o.Icon, DefaultResultIconSize);
259
renderer.Pixbuf = final;
263
protected void OnResultRowSelected (object sender, EventArgs args)
265
if (!clearing && !pushedUpdate) {
266
NotifySelectionChanged ();
270
public virtual void Clear ()
272
(resultsTreeview.Model as ListStore).Clear ();
274
resultsLabel.Markup = "--/--";
275
queryLabel.Markup = "";
276
update_needed = false;
279
public IUIContext Context
284
update_needed = true;
289
if (value == null || !value.Results.Any ()) {
290
Results = new Do.Universe.Item [0];
294
if (results.GetHashCode () != value.Results.GetHashCode ()) {
295
results = value.Results;
298
startResult = value.Cursor - 5;
302
endResult = startResult + 8;
303
offset = startResult;
305
if (endResult > results.Count)
306
endResult = results.Count;
308
Do.Universe.Item[] resultsArray = new Do.Universe.Item[endResult - startResult];
309
Array.Copy (results.ToArray (), startResult, resultsArray, 0, resultsArray.Length);
311
cursor = value.Cursor - offset;
313
Results = resultsArray;
319
int[] secArray = new int[value.SecondaryCursors.Length];
320
for (int i=0; i<secArray.Length; i++) {
321
secArray[i] = value.SecondaryCursors[i] - offset;
324
secondary = secArray;
328
UpdateQueryLabel (value);
329
resultsLabel.Markup = string.Format ("{1}/{0}",
332
Gtk.Application.Invoke (delegate {
333
pushedUpdate = false;
338
public int SelectedIndex
340
get { return cursor + offset; }
342
cursor = value - offset;
348
protected void UpdateQueryLabel (IUIContext context)
350
string query = context.Query;
351
StringBuilder builder = new StringBuilder ();
354
while (context.ParentContext != null && count < 2) {
355
builder.Insert (0, context.ParentContext.Selection.Name + " > ");
356
context = context.ParentContext;
359
queryLabel.Markup = string.Format ("{0}<b>{1}</b>",
360
GLib.Markup.EscapeText (builder.ToString ()),
361
GLib.Markup.EscapeText (query));
364
private void UpdateCursors ()
369
path = new TreePath (cursor.ToString ());
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);
379
public Do.Universe.Item SelectedObject
383
return results [SelectedIndex];
390
public IList<Do.Universe.Item> Results
393
if (stunted_results == null)
394
stunted_results = new List<Do.Universe.Item> (0);
395
return stunted_results;
398
stunted_results = value;
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 ();
411
Gtk.Application.Invoke (delegate {
412
store = resultsTreeview.Model as ListStore;
415
foreach (Do.Universe.Item result in value) {
417
info = string.Format (ResultInfoFormat,
418
GLib.Markup.EscapeText (result.Name),
419
GLib.Markup.EscapeText (result.Description));
420
store.AppendValues (new object[] {
437
queryLabel.Markup = string.Format (QueryLabelFormat, GLib.Markup.EscapeText (query));
439
get { return query; }
442
protected void OnShown (object o, EventArgs args)
446
update_needed = false;
449
// Draw a border around the window.
450
protected override bool OnExposeEvent (EventExpose evnt)
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;
461
return base.OnExposeEvent (evnt);
464
protected virtual void SetColormap ()
466
Gdk.Colormap colormap;
468
colormap = Screen.RgbaColormap;
469
if (colormap == null) {
470
colormap = Screen.RgbColormap;
471
Console.Error.WriteLine ("No alpha support.");