~bas-dotbas/do/win32-next

« back to all changes in this revision

Viewing changes to Do/src/Do.UI/SymbolWindow.cs

  • Committer: djsiegel at gmail
  • Date: 2007-10-13 21:55:22 UTC
  • Revision ID: djsiegel@gmail.com-20071013215522-n0vjgog0tyjfnpno
Restructured plugins library.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// GSymbolWindow.cs created with MonoDevelop
 
2
// User: dave at 11:15 AM 8/25/2007
 
3
//
 
4
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
 
5
//
 
6
 
 
7
using System;
 
8
using System.Runtime.InteropServices;
 
9
using Gtk;
 
10
using Gdk;
 
11
 
 
12
using Do.Universe;
 
13
using Do.Addins;
 
14
// Do.Core dependency needs to be removed
 
15
using Do.Core;
 
16
 
 
17
namespace Do.UI
 
18
{
 
19
        
 
20
        public class SymbolWindow : Gtk.Window
 
21
        {
 
22
                class DefaultIconBoxObject : IObject {
 
23
                        public string Icon { get { return "gtk-find"; } }
 
24
                        public string Name { get { return ""; } }
 
25
                        public string Description { get { return ""; } }
 
26
                }
 
27
                
 
28
                class NoItemsFoundObject : IObject {
 
29
                        public string Icon { get { return "gtk-dialog-question"; } }
 
30
                        public string Name { get { return "No items found."; } }
 
31
                        public string Description { get { return ""; } }
 
32
                }
 
33
 
 
34
                class NoCommandsFoundObject : IObject {
 
35
                        public string Icon { get { return "gtk-dialog-question"; } }
 
36
                        public string Name { get { return "No commands found."; } }
 
37
                        public string Description { get { return ""; } }
 
38
                }
 
39
                
 
40
                const int IconBoxIconSize = 128;
 
41
                const double WindowTransparency = 0.91;
 
42
                
 
43
                protected enum WindowFocus {
 
44
                        ItemFocus = 0,
 
45
                        CommandFocus = 1,
 
46
                        ModifierItemFocus = 2,
 
47
                }
 
48
                
 
49
                RoundedFrame frame;
 
50
                SymbolDisplayLabel displayLabel;
 
51
                ResultsWindow resultsWindow;
 
52
                HBox resultsHBox;
 
53
                IconBox itemBox;
 
54
                IconBox commandBox;
 
55
                IconBox modItemBox;
 
56
                
 
57
                protected Commander commander;
 
58
                protected Command currentCommand;
 
59
                protected Core.Item currentItem;
 
60
                protected int currentItemIndex;
 
61
                protected int currentCommandIndex;
 
62
                protected WindowFocus focus;
 
63
                protected string searchString;
 
64
                protected string itemSearchString;
 
65
                
 
66
                public SymbolWindow (Commander commander) : base ("GNOME Go")
 
67
                {
 
68
                        Build ();
 
69
                        
 
70
                        this.commander = commander;
 
71
                        SetDefaultState ();
 
72
                        
 
73
                        commander.SetDefaultStateEvent += OnDefaultStateEvent;
 
74
                        commander.SetSearchingItemsStateEvent += OnSearchingStateEvent;
 
75
                        commander.SetSearchingCommandsStateEvent += OnSearchingStateEvent;
 
76
                        commander.SetItemSearchCompleteStateEvent += OnSearchCompleteStateEvent;
 
77
                        commander.SetCommandSearchCompleteStateEvent += OnSearchCompleteStateEvent;
 
78
                }
 
79
                
 
80
                protected void Build ()
 
81
                {
 
82
                        VBox         vbox;
 
83
                        Alignment align;
 
84
                        
 
85
                        AppPaintable = true;
 
86
                        KeepAbove = true;
 
87
                        Decorated = false;
 
88
                        // This typehint gets the window to raise all the way to top.
 
89
                        TypeHint = WindowTypeHint.Splashscreen;
 
90
                                
 
91
                        try { SetIconFromFile ("/usr/share/icons/gnome/scalable/actions/system-run.svg"); } catch { }
 
92
                        SetColormap ();
 
93
 
 
94
                        resultsWindow = new ResultsWindow ();
 
95
                        resultsWindow.SelectionChanged += OnResultsWindowSelectionChanged;
 
96
 
 
97
                        focus = WindowFocus.ItemFocus;
 
98
                        searchString = itemSearchString = "";
 
99
                        
 
100
                        frame = new RoundedFrame ();
 
101
                        frame.DrawFill = true;
 
102
                        frame.FillColor = new Gdk.Color (0x35, 0x30, 0x45);
 
103
                        frame.FillAlpha = WindowTransparency;
 
104
                        SetFrameRadius ();
 
105
                        Add (frame);
 
106
                        frame.Show ();
 
107
                        
 
108
                        vbox = new VBox (false, 0);
 
109
                        frame.Add (vbox);
 
110
                        vbox.BorderWidth = 6;
 
111
                        vbox.Show ();           
 
112
                        
 
113
                        resultsHBox = new HBox (false, 12);
 
114
                        resultsHBox.BorderWidth = 6;
 
115
                        vbox.PackStart (resultsHBox, false, false, 0);
 
116
                        resultsHBox.Show ();
 
117
                        
 
118
                        itemBox = new IconBox (IconBoxIconSize);
 
119
                        itemBox.IsFocused = true;
 
120
                        resultsHBox.PackStart (itemBox, false, false, 0);
 
121
                        itemBox.Show ();
 
122
                        
 
123
                        commandBox = new IconBox (IconBoxIconSize);
 
124
                        commandBox.IsFocused = false;
 
125
                        resultsHBox.PackStart (commandBox, false, false, 0);
 
126
                        commandBox.Show ();
 
127
                        
 
128
                        modItemBox = new IconBox (IconBoxIconSize);
 
129
                        modItemBox.IsFocused = false;
 
130
                        // resultsHBox.PackStart (modItemBox, false, false, 0);
 
131
                        modItemBox.Show ();
 
132
        
 
133
                        align = new Alignment (0.5F, 0.5F, 1, 1);
 
134
                        align.SetPadding (0, 0, 0, 0);
 
135
                        displayLabel = new SymbolDisplayLabel ();
 
136
                        align.Add (displayLabel);
 
137
                        vbox.PackStart (align, false, false, 0);
 
138
                        displayLabel.Show ();
 
139
                        align.Show ();
 
140
                                
 
141
                        ScreenChanged += OnScreenChanged;
 
142
                
 
143
                        Reposition ();
 
144
                }
 
145
        
 
146
                protected virtual void SetColormap ()
 
147
                {
 
148
                        Gdk.Colormap  colormap;
 
149
 
 
150
                        colormap = Screen.RgbaColormap;
 
151
                        if (colormap == null) {
 
152
                                colormap = Screen.RgbColormap;
 
153
                                Console.WriteLine ("No alpha support.");
 
154
                        }
 
155
                        Colormap = colormap;
 
156
                }
 
157
                
 
158
                protected override bool OnButtonPressEvent (EventButton evnt)
 
159
                {
 
160
                        Hide ();
 
161
                        return false;
 
162
                }
 
163
                
 
164
                public virtual new void Hide ()
 
165
                {
 
166
                        base.Hide ();
 
167
                        resultsWindow.Hide ();
 
168
                        commander.State = CommanderState.Default;
 
169
                }
 
170
                
 
171
                public void Reposition ()
 
172
                {
 
173
                        int monitor;
 
174
                        Gdk.Rectangle geo, main, results;
 
175
                        
 
176
                        GetPosition (out main.X, out main.Y);
 
177
                        GetSize (out main.Width, out main.Height);
 
178
                        monitor = Screen.GetMonitorAtPoint (main.X, main.Y);
 
179
                        geo = Screen.GetMonitorGeometry (monitor);
 
180
                        main.X = (geo.Width - main.Width) / 2;
 
181
                        main.Y =  (int) ((geo.Height - main.Height) / 2.5);
 
182
                        Move (main.X, main.Y);
 
183
                        
 
184
                        resultsWindow.GetSize (out results.Width, out results.Height);
 
185
                        results.Y = main.Y + main.Height;
 
186
                        results.X = main.X + (IconBoxIconSize + 60) * (int) focus + 10;
 
187
                        resultsWindow.Move (results.X, results.Y);                      
 
188
                }
 
189
                
 
190
                protected override bool OnKeyPressEvent (EventKey evnt)
 
191
                {
 
192
                        Gdk.Key key;
 
193
                        
 
194
                        key = (Gdk.Key) evnt.KeyValue;
 
195
                        
 
196
                        // Handle command keys (Quit, etc.)
 
197
                        if (((int) evnt.State & (int) ModifierType.ControlMask) != 0) {
 
198
                                switch (key) {
 
199
                                case Gdk.Key.q:
 
200
                                        Application.Quit ();
 
201
                                        break;
 
202
                                }
 
203
                                return false;
 
204
                        }
 
205
                        
 
206
                        switch (key) {
 
207
                        // Throwaway keys
 
208
                        case Gdk.Key.Shift_L:
 
209
                        case Gdk.Key.Control_L:
 
210
                                break;
 
211
                        case Gdk.Key.Escape:
 
212
                                if (focus == WindowFocus.ItemFocus) {
 
213
                                        if (searchString.Length == 0) {
 
214
                                                Hide ();
 
215
                                        }
 
216
                                        resultsWindow.Hide ();
 
217
                                        commander.State = CommanderState.Default;
 
218
                                } else {
 
219
                                        if (searchString.Length == 0) {
 
220
                                                resultsWindow.Hide ();
 
221
                                                commander.State = CommanderState.Default;
 
222
                                        } else {
 
223
                                                searchString = "";
 
224
                                                QueueSearch ();
 
225
                                        }
 
226
                                }
 
227
                                break;
 
228
                        case Gdk.Key.Return:
 
229
                        case Gdk.Key.ISO_Enter:
 
230
                                ActivateCommand ();
 
231
                                break;
 
232
                        case Gdk.Key.Delete:
 
233
                        case Gdk.Key.BackSpace:
 
234
                                if (searchString.Length > 1) {
 
235
                                        searchString = searchString.Substring (0, searchString.Length-1);
 
236
                                        QueueSearch ();
 
237
                                } else {
 
238
                                        commander.State = CommanderState.Default;
 
239
                                }
 
240
                                break;
 
241
                        case Gdk.Key.Tab:
 
242
                                resultsWindow.Hide ();
 
243
                                if (focus == WindowFocus.ItemFocus && commander.CurrentItem != null) {
 
244
                                        SetWindowFocus (WindowFocus.CommandFocus);
 
245
                                } else if (focus == WindowFocus.CommandFocus) {
 
246
                                        SetWindowFocus (WindowFocus.ItemFocus);
 
247
                                }
 
248
                                break;
 
249
                        case Gdk.Key.Up:
 
250
                        case Gdk.Key.Down:
 
251
                        {       
 
252
                                if (key == Gdk.Key.Up) {
 
253
                                        if (resultsWindow.SelectedIndex == 0) {
 
254
                                                resultsWindow.Hide ();
 
255
                                        } else {
 
256
                                                resultsWindow.SelectPrev ();
 
257
                                        }
 
258
                                }
 
259
                                else if (key == Gdk.Key.Down) {
 
260
                                        if (resultsWindow.Visible) {
 
261
                                                resultsWindow.SelectNext ();
 
262
                                        } else {                                        
 
263
                                                resultsWindow.Show ();
 
264
                                        }
 
265
                                }
 
266
                        }
 
267
                                break;
 
268
                        case Gdk.Key.Right:
 
269
                        case Gdk.Key.Left:
 
270
                                break;
 
271
                        default:
 
272
                                char c;
 
273
                                
 
274
                                c = (char) Gdk.Keyval.ToUnicode ((uint) key);   
 
275
                                if (char.IsLetterOrDigit (c)
 
276
                                    || char.IsPunctuation (c)
 
277
                                    || c == ' '
 
278
                                    || char.IsSymbol (c)) {
 
279
                                        searchString += c;
 
280
 
 
281
                                        QueueSearch ();
 
282
                                }
 
283
                                break;
 
284
                        }
 
285
                        return false;
 
286
                }
 
287
                
 
288
                protected virtual void ActivateCommand ()
 
289
                {
 
290
                        commander.Execute ();
 
291
                        Hide ();
 
292
                }
 
293
                
 
294
                private void OnResultsWindowSelectionChanged (object sender, ResultsWindowSelectionEventArgs args)
 
295
                {
 
296
                        if (focus == WindowFocus.ItemFocus) {
 
297
                                SetItemIndex (args.SelectedIndex, searchString);
 
298
                        } else if (focus == WindowFocus.CommandFocus) {
 
299
                                SetCommandIndex (args.SelectedIndex, searchString);
 
300
                        }                       
 
301
                }
 
302
 
 
303
                private void OnScreenChanged (object sender, EventArgs args)
 
304
                {
 
305
                        SetColormap ();
 
306
                }
 
307
                
 
308
                protected virtual void SetFrameRadius ()
 
309
                {
 
310
                        if (Screen.IsComposited) {
 
311
                                frame.Radius = 10;
 
312
                        } else {
 
313
                                frame.Radius = 0;
 
314
                        }
 
315
                }
 
316
                
 
317
                protected override bool OnExposeEvent (EventExpose evnt)
 
318
                {
 
319
                        Cairo.Context cairo;
 
320
                        
 
321
                        cairo = Gdk.CairoHelper.Create (GdkWindow);
 
322
                        cairo.Rectangle (evnt.Area.X, evnt.Area.Y, evnt.Area.Width, evnt.Area.Height);
 
323
                        cairo.Color = new Cairo.Color (1.0, 1.0, 1.0, 0.0);
 
324
                        cairo.Operator = Cairo.Operator.Source;
 
325
                        cairo.Paint ();
 
326
 
 
327
                        return base.OnExposeEvent (evnt);
 
328
                }
 
329
                
 
330
                protected virtual void SetWindowFocus (WindowFocus focus)
 
331
                {       
 
332
                        IObject[] results;
 
333
                        int selectedIndex;
 
334
                        
 
335
                        this.focus = focus;
 
336
                        results = null;
 
337
                        selectedIndex = -1;
 
338
                        switch (focus) {
 
339
                        case WindowFocus.CommandFocus:
 
340
                                searchString = commander.CommandSearchString;
 
341
                                results = commander.CurrentCommands;
 
342
                                selectedIndex = commander.CurrentCommandIndex;
 
343
                                break;
 
344
                        case WindowFocus.ItemFocus:
 
345
                                searchString = commander.ItemSearchString;
 
346
                                results = commander.CurrentItems;
 
347
                                selectedIndex = commander.CurrentItemIndex;
 
348
                                break;
 
349
                        }
 
350
 
 
351
                        resultsWindow.Results = results;
 
352
                        resultsWindow.SelectedIndex = selectedIndex;
 
353
                        
 
354
                        displayLabel.DisplayObject = resultsWindow.SelectedObject;
 
355
                        displayLabel.Highlight = searchString;
 
356
                        itemBox.IsFocused = (focus == WindowFocus.ItemFocus);
 
357
                        commandBox.IsFocused = (focus == WindowFocus.CommandFocus);
 
358
                        modItemBox.IsFocused = (focus == WindowFocus.ModifierItemFocus);
 
359
 
 
360
                        Reposition ();
 
361
                }
 
362
                
 
363
                protected virtual void QueueSearch ()
 
364
                {
 
365
                        switch (focus) {
 
366
                        case WindowFocus.ItemFocus:
 
367
                                commander.SearchItems (searchString);
 
368
                                break;
 
369
                        case WindowFocus.CommandFocus:
 
370
                                commander.SearchCommands (searchString);
 
371
                                break;
 
372
                        }
 
373
                }
 
374
                
 
375
                protected virtual void SetItemIndex (int itemIndex, string match)
 
376
                {       
 
377
                        if (itemIndex >= commander.CurrentItems.Length) {
 
378
                                SetItemIndex (commander.CurrentItems.Length-1, match);
 
379
                                return;
 
380
                        }
 
381
                        try {
 
382
                                commander.CurrentItemIndex = itemIndex;
 
383
                        } catch (IndexOutOfRangeException) {
 
384
                                return;
 
385
                        }
 
386
                        itemBox.DisplayObject = commander.CurrentItem;
 
387
                        itemBox.Highlight = match;
 
388
                        if (focus == WindowFocus.ItemFocus) {
 
389
                                displayLabel.DisplayObject = commander.CurrentItem;
 
390
                                displayLabel.Highlight = searchString;
 
391
                        }
 
392
                        SetCommandIndex (0, "");
 
393
                }
 
394
                
 
395
                protected virtual void SetCommandIndex (int commandIndex, string match)
 
396
                {
 
397
                        try {
 
398
                                commander.CurrentCommandIndex = commandIndex;
 
399
                        } catch (IndexOutOfRangeException) {
 
400
                                return;
 
401
                        }
 
402
 
 
403
                        commandBox.DisplayObject = commander.CurrentCommand;
 
404
                        commandBox.Highlight = match;
 
405
                        if (focus == WindowFocus.CommandFocus) {
 
406
                                displayLabel.DisplayObject = commander.CurrentCommand;
 
407
                                displayLabel.Highlight = match;
 
408
                        }
 
409
                }
 
410
                
 
411
                protected virtual void SetDefaultState ()
 
412
                {
 
413
                        searchString = itemSearchString = "";
 
414
 
 
415
                        SetWindowFocus (WindowFocus.ItemFocus);
 
416
                        
 
417
                        itemBox.DisplayObject = new DefaultIconBoxObject ();
 
418
                        commandBox.Clear ();
 
419
                        
 
420
                        displayLabel.SetDisplayLabel ("Type to begin searching", "Type to start searching.");                   
 
421
                }
 
422
                
 
423
                protected virtual void SetNoResultsFoundState ()
 
424
                {
 
425
                        switch (focus) {
 
426
                        case WindowFocus.CommandFocus:
 
427
                                commandBox.DisplayObject = new NoCommandsFoundObject ();
 
428
                                break;
 
429
                        default:
 
430
                        // case WindowFocus.ItemFocus:
 
431
                                commandBox.Clear ();
 
432
                                itemBox.DisplayObject = new NoItemsFoundObject ();
 
433
                                break;
 
434
                        }
 
435
                        displayLabel.Text = "";
 
436
                }
 
437
                
 
438
                protected void OnDefaultStateEvent ()
 
439
                {
 
440
                        SetDefaultState ();
 
441
                }
 
442
                
 
443
                protected void OnSearchingStateEvent ()
 
444
                {
 
445
                }
 
446
                
 
447
                protected void OnSearchCompleteStateEvent ()
 
448
                {
 
449
                        GCObject[] results;
 
450
                        
 
451
                        switch (focus) {
 
452
                        case WindowFocus.ItemFocus:
 
453
                                results = commander.CurrentItems;
 
454
                                break;
 
455
                        case WindowFocus.CommandFocus:
 
456
                                results = commander.CurrentCommands;
 
457
                                break;
 
458
                        default:
 
459
                                results = new GCObject [0];
 
460
                                break;
 
461
                        }
 
462
                
 
463
                        resultsWindow.Results = results;
 
464
                        if (results.Length == 0) {
 
465
                                SetNoResultsFoundState ();
 
466
                        } else {
 
467
                                SetItemIndex (commander.CurrentItemIndex, commander.ItemSearchString);
 
468
                                SetCommandIndex (commander.CurrentCommandIndex, commander.CommandSearchString);
 
469
                        }
 
470
                }
 
471
                
 
472
        }
 
473
 
 
474
        
 
475
}