~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/ClassicInterface/ClassicWindow.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
/* DoClassicWindow.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 Mono.Unix;
 
24
 
 
25
using Gdk;
 
26
using Gtk;
 
27
 
 
28
using Do.Universe;
 
29
using Do.Universe.Common;
 
30
using Do.Platform;
 
31
using Do.Interface.Widgets;
 
32
 
 
33
namespace Do.Interface {
 
34
        
 
35
        public class ClassicWindow : Gtk.Window, IDoWindow {
 
36
                
 
37
                //-------------------Class Members------------------
 
38
                Do.Interface.Widgets.Frame frame;
 
39
                SymbolDisplayLabel label;
 
40
                ResultsWindow resultsWindow;
 
41
                PositionWindow positionWindow;
 
42
                HBox resultsHBox;
 
43
                IconBox[] iconbox;
 
44
                GConf.Client gconfClient;
 
45
                IDoController controller;
 
46
                
 
47
                const int IconBoxIconSize = 128;
 
48
                const uint IconBoxPadding = 6;
 
49
                const int IconBoxRadius = 20;
 
50
                const int NumberResultsDisplayed = 6;
 
51
 
 
52
                const double WindowTransparency = 0.91;
 
53
                
 
54
                Pane currentPane;
 
55
                
 
56
                //-------------------Events-----------------------
 
57
                public new event DoEventKeyDelegate KeyPressEvent;
 
58
                        
 
59
                //-------------------Properties-------------------
 
60
 
 
61
                public new string Name {
 
62
                        get { return "Simple"; }
 
63
                }
 
64
                
 
65
                public Pane CurrentPane {
 
66
                        get {
 
67
                                return currentPane;
 
68
                        }
 
69
                        set {
 
70
                                if (currentPane == value) return;
 
71
 
 
72
                                currentPane = value;
 
73
                                iconbox[0].IsFocused = value == Pane.First;
 
74
                                iconbox[1].IsFocused = value == Pane.Second;
 
75
                                iconbox[2].IsFocused = value == Pane.Third;
 
76
 
 
77
                                Reposition ();
 
78
                        }
 
79
                }
 
80
                
 
81
                public PositionWindow PositionWindow {
 
82
                        get {
 
83
                                if (positionWindow == null)
 
84
                                        positionWindow = new PositionWindow (this, resultsWindow);
 
85
                                return positionWindow;
 
86
                        }
 
87
                }
 
88
                
 
89
                //-------------------ctor----------------------
 
90
                public ClassicWindow () : base (Gtk.WindowType.Toplevel)
 
91
                {
 
92
                        
 
93
                }
 
94
                
 
95
                public void Initialize (IDoController controller)
 
96
                {
 
97
                        this.controller = controller;
 
98
                        
 
99
                        gconfClient = new GConf.Client ();
 
100
                        gconfClient.AddNotify ("/desktop/gnome/interface",
 
101
                                new GConf.NotifyEventHandler (DesktopThemeChanged));
 
102
                        
 
103
                        Build ();
 
104
                }
 
105
                
 
106
                //-------------------methods------------------
 
107
                protected void Build ()
 
108
                {
 
109
                        VBox      vbox;
 
110
                        Alignment align;
 
111
                        Gtk.Image settings_icon;
 
112
 
 
113
                        AppPaintable = true;
 
114
                        KeepAbove = true;
 
115
                        Decorated = false;
 
116
                        // This typehint gets the window to raise all the way to top.
 
117
                        TypeHint = WindowTypeHint.Splashscreen;
 
118
 
 
119
                        try {
 
120
                                SetIconFromFile ("/usr/share/icons/gnome/scalable/actions/system-run.svg");
 
121
                        } catch { }
 
122
                        SetColormap ();
 
123
 
 
124
                        resultsWindow = new ResultsWindow (BackgroundColor, 
 
125
                                                           NumberResultsDisplayed);
 
126
                        resultsWindow.SelectionChanged += OnResultsWindowSelectionChanged;
 
127
 
 
128
                        currentPane = Pane.First;
 
129
 
 
130
                        frame = new GlossyRoundedFrame ();
 
131
                        frame.DrawFill = frame.DrawFrame = true;
 
132
                        frame.FillColor = frame.FrameColor = BackgroundColor;
 
133
                        frame.FillAlpha = frame.FrameAlpha = WindowTransparency;
 
134
                        frame.Radius = Screen.IsComposited ? IconBoxRadius : 0;
 
135
                        Add (frame);
 
136
                        frame.Show ();
 
137
 
 
138
                        vbox = new VBox (false, 0);
 
139
                        frame.Add (vbox);
 
140
                        vbox.BorderWidth = IconBoxPadding;
 
141
                        vbox.Show ();
 
142
 
 
143
                        settings_icon = new Gtk.Image (GetType().Assembly, "settings-triangle.png");
 
144
 
 
145
                        align = new Alignment (1.0F, 0.0F, 0, 0);
 
146
                        align.SetPadding (3, 0, 0, IconBoxPadding);
 
147
                        align.Add (settings_icon);
 
148
                        vbox.PackStart (align, false, false, 0);
 
149
                        settings_icon.Show ();
 
150
                        align.Show ();
 
151
 
 
152
                        resultsHBox = new HBox (false, (int) IconBoxPadding * 2);
 
153
                        resultsHBox.BorderWidth = IconBoxPadding;
 
154
                        vbox.PackStart (resultsHBox, false, false, 0);
 
155
                        resultsHBox.Show ();
 
156
 
 
157
                        iconbox = new IconBox[3];
 
158
 
 
159
                        iconbox[0] = new IconBox (IconBoxIconSize);
 
160
                        iconbox[0].IsFocused = true;
 
161
                        iconbox[0].Radius = IconBoxRadius;
 
162
                        resultsHBox.PackStart (iconbox[0], false, false, 0);
 
163
                        iconbox[0].Show ();
 
164
 
 
165
                        iconbox[1] = new IconBox (IconBoxIconSize);
 
166
                        iconbox[1].IsFocused = false;
 
167
                        iconbox[1].Radius = IconBoxRadius;
 
168
                        resultsHBox.PackStart (iconbox[1], false, false, 0);
 
169
                        iconbox[1].Show ();
 
170
 
 
171
                        iconbox[2] = new IconBox (IconBoxIconSize);
 
172
                        iconbox[2].IsFocused = false;
 
173
                        iconbox[2].Radius = IconBoxRadius;
 
174
                        resultsHBox.PackStart (iconbox[2], false, false, 0);
 
175
                        // iconbox[2].Show ();
 
176
 
 
177
                        align = new Alignment (0.5F, 0.5F, 1, 1);
 
178
                        align.SetPadding (0, 2, 0, 0);
 
179
                        label = new SymbolDisplayLabel ();
 
180
                        align.Add (label);
 
181
                        vbox.PackStart (align, false, false, 0);
 
182
                        label.Show ();
 
183
                        align.Show ();
 
184
 
 
185
                        ScreenChanged += OnScreenChanged;
 
186
                        ConfigureEvent += OnConfigureEvent;
 
187
                        SizeAllocated += delegate { Reposition (); };
 
188
                        iconbox[0].LinesChanged += OnLineChangedEvent;
 
189
                        iconbox[1].LinesChanged += OnLineChangedEvent;
 
190
                        iconbox[2].LinesChanged += OnLineChangedEvent;
 
191
                        Reposition ();
 
192
                }
 
193
                
 
194
                protected override bool OnKeyPressEvent (EventKey evnt)
 
195
                {
 
196
                        KeyPressEvent (evnt);
 
197
 
 
198
                        return base.OnKeyPressEvent (evnt);
 
199
                }
 
200
                
 
201
                protected virtual void SetColormap ()
 
202
                {
 
203
                        Gdk.Colormap  colormap;
 
204
 
 
205
                        colormap = Screen.RgbaColormap;
 
206
                        if (colormap == null) {
 
207
                                colormap = Screen.RgbColormap;
 
208
                                Console.Error.WriteLine ("No alpha support.");
 
209
                        }
 
210
                        
 
211
                        Colormap = colormap;
 
212
                        colormap.Dispose ();
 
213
                }
 
214
                
 
215
                private Gdk.Color BackgroundColor
 
216
                {
 
217
                        get {
 
218
                                byte r, g, b;
 
219
                                Gdk.Color bgColor;
 
220
 
 
221
                                using (Gtk.Style style = Gtk.Rc.GetStyle (this)) {
 
222
                                        bgColor = style.Backgrounds[(int) StateType.Selected];
 
223
                                }
 
224
                                r = (byte) ((bgColor.Red) >> 8);
 
225
                                g = (byte) ((bgColor.Green) >> 8);
 
226
                                b = (byte) ((bgColor.Blue) >> 8);
 
227
                                
 
228
                                // Useful for making overbright themes less ugly. Still trying
 
229
                                // to find a happy balance between 50 and 90...
 
230
                                byte maxLum = 60;
 
231
                                double hue, sat, val;
 
232
                                Interface.Util.Appearance.RGBToHSV(r, g, b, out hue, 
 
233
                                                                out sat, out val);
 
234
                                val = Math.Min (val, maxLum);
 
235
                                
 
236
                                Interface.Util.Appearance.HSVToRGB(hue, sat, val, out r,
 
237
                                                                out g, out b);
 
238
                                
 
239
                                return new Gdk.Color (r, g, b);
 
240
                        }
 
241
                }
 
242
                
 
243
                private void OnScreenChanged (object sender, EventArgs args)
 
244
                {
 
245
                        SetColormap ();
 
246
                }
 
247
                
 
248
                private void OnConfigureEvent (object sender, ConfigureEventArgs args)
 
249
                {
 
250
                        Reposition ();
 
251
                }
 
252
                
 
253
                private void DesktopThemeChanged (object o, GConf.NotifyEventArgs e)
 
254
                {
 
255
                        //this is needed to account for the delay between the gconf change
 
256
                        //and the theme change propogating to this application.
 
257
                        GLib.Timeout.Add (3000, delegate {
 
258
                                Gdk.Threads.Enter ();
 
259
                                frame.FrameColor = frame.FillColor = BackgroundColor;
 
260
                                resultsWindow.UpdateColors (BackgroundColor);
 
261
                                Gdk.Threads.Leave ();
 
262
                                return false;
 
263
                        });
 
264
                }
 
265
                
 
266
                public virtual void Reposition ()
 
267
                {
 
268
                        Gtk.Application.Invoke (delegate {
 
269
                                Gdk.Rectangle offset;
 
270
                                int iconboxWidth;
 
271
                                
 
272
                                offset = new Rectangle (IconBoxRadius, 0, 0 ,0);
 
273
                                iconboxWidth = IconBoxIconSize + 60;
 
274
                                
 
275
                                PositionWindow.UpdatePosition (iconboxWidth, currentPane, offset);
 
276
                        });
 
277
                }
 
278
                
 
279
                protected void OnLineChangedEvent (object o, EventArgs a)
 
280
                {
 
281
                        if ((int) o <= 2) return;
 
282
                        this.QueueDraw ();
 
283
                        this.Resize (1, 1);
 
284
                        Reposition ();
 
285
                }
 
286
                
 
287
                protected override bool OnButtonPressEvent (EventButton evnt)
 
288
                {
 
289
                        int start_x, start_y, end_x, end_y;
 
290
                        int click_x, click_y;
 
291
                        bool click_on_window, click_near_settings_icon;
 
292
 
 
293
                        GetPosition (out start_x, out start_y);
 
294
                        GetSize (out end_x, out end_y);
 
295
                        end_x += start_x;
 
296
                        end_y += start_y;
 
297
                        click_x = (int) evnt.XRoot;
 
298
                        click_y = (int) evnt.YRoot;
 
299
                        click_on_window = start_x <= click_x && click_x < end_x &&
 
300
                                          start_y <= click_y && click_y < end_y;
 
301
                        click_near_settings_icon = (end_x - 27) <= click_x && click_x < end_x &&
 
302
                                                    start_y <= click_y && click_y < (start_y + 27);
 
303
                        if (click_near_settings_icon) {
 
304
                                Services.Windowing.ShowMainMenu (end_x - 21, start_y + 16);
 
305
                                // Have to re-grab the pane from the menu.
 
306
                                Interface.Windowing.PresentWindow (this);
 
307
                        } else if (!click_on_window) {
 
308
                                controller.ButtonPressOffWindow ();
 
309
                        }
 
310
                        return base.OnButtonPressEvent (evnt);
 
311
                }
 
312
                
 
313
                private void OnResultsWindowSelectionChanged (object sender,
 
314
                                ResultsWindowSelectionEventArgs args)
 
315
                {
 
316
                        controller.NewContextSelection (CurrentPane, args.SelectedIndex);
 
317
                }
 
318
 
 
319
                  ///////////////////////
 
320
                 //     IDoWindow     //
 
321
                ///////////////////////
 
322
                
 
323
                public void Summon ()
 
324
                {
 
325
                        frame.Radius = Screen.IsComposited ? IconBoxRadius : 0;
 
326
 
 
327
                        Resize (1, 1);
 
328
                        Reposition ();
 
329
                        Show ();
 
330
                        Interface.Windowing.PresentWindow (this);
 
331
                }
 
332
 
 
333
                public void Vanish ()
 
334
                {
 
335
                        Hide ();
 
336
                }
 
337
 
 
338
                public void Reset ()
 
339
                {
 
340
                        resultsWindow.Clear ();
 
341
                        
 
342
                        iconbox[0].Clear ();
 
343
                        iconbox[1].Clear ();
 
344
                        iconbox[2].Clear ();
 
345
                        
 
346
                        QueueDraw ();
 
347
                        Resize (1, 1);
 
348
                        Reposition ();
 
349
                        
 
350
                        iconbox[0].DisplayObject = new Do.Interface.Widgets.DefaultIconBoxItem ();
 
351
                        label.SetDisplayLabel (Catalog.GetString ("Type to begin searching"), 
 
352
                                               Catalog.GetString ("Type to start searching."));
 
353
                }
 
354
 
 
355
                public void Grow ()
 
356
                {
 
357
                        iconbox[2].Show ();
 
358
                        Resize (1, 1);
 
359
                }
 
360
 
 
361
                public void Shrink ()
 
362
                {
 
363
                        iconbox[2].Hide ();
 
364
                        Resize (1, 1);
 
365
                }
 
366
                
 
367
                public void GrowResults ()
 
368
                {
 
369
                        if (!resultsWindow.Visible)
 
370
                                resultsWindow.Show ();
 
371
                }
 
372
                
 
373
                public void ShrinkResults ()
 
374
                {
 
375
                        if (resultsWindow.Visible)
 
376
                                resultsWindow.Hide ();
 
377
                }
 
378
                
 
379
                public void SetPaneContext (Pane pane, IUIContext context)
 
380
                {
 
381
                        if (!context.Results.Any () && !context.LargeTextDisplay) {
 
382
                                if (pane == Pane.First && context.ParentContext == null) {
 
383
                                        iconbox[0].TextOverlay = context.LargeTextDisplay;
 
384
                                        iconbox[0].DisplayObject = new Do.Interface.Widgets.DefaultIconBoxItem ();
 
385
                                        label.SetDisplayLabel (Catalog.GetString ("Type to begin searching"), 
 
386
                                                               Catalog.GetString ("Type to start searching."));
 
387
                                } else {
 
388
                                        Do.Universe.Item noRes = new NoResultsFoundItem (context.Query);
 
389
                                        for (int i = (int) pane; i < 3; i++) {
 
390
                                                iconbox[i].Clear ();
 
391
                                                iconbox[i].DisplayObject = noRes;
 
392
                                                if (i == (int) CurrentPane) {
 
393
                                                        label.SetDisplayLabel (noRes.Name, noRes.Description);
 
394
                                                        resultsWindow.Clear ();
 
395
                                                }
 
396
                                        }
 
397
                                }
 
398
                                return;
 
399
                        }
 
400
                        
 
401
                        if (string.IsNullOrEmpty (context.Query) && context.LargeTextDisplay) {
 
402
                                iconbox[(int) pane].TextOverlay = context.LargeTextDisplay;
 
403
                                iconbox[(int) pane].DisplayObject = new TextItem ("Enter Text") as Do.Universe.Item;
 
404
 
 
405
                                if (!context.Results.Any ()) return;
 
406
                        } else {
 
407
                                iconbox[(int) pane].TextOverlay = context.LargeTextDisplay;
 
408
                                iconbox[(int) pane].DisplayObject = context.Selection;
 
409
                                
 
410
                                if (!context.LargeTextDisplay)
 
411
                                        iconbox[(int) pane].Highlight = context.Query;
 
412
                        }
 
413
                        
 
414
                        if (context.Selection == null) return;
 
415
                        
 
416
                        if (pane == CurrentPane) {
 
417
                                resultsWindow.Context = context;
 
418
                                if (!context.LargeTextDisplay)
 
419
                                        label.SetDisplayLabel (context.Selection.Name, context.Selection.Description);
 
420
                                else
 
421
                                        label.SetDisplayLabel ("", "Raw Text Mode");
 
422
                        }
 
423
                }
 
424
 
 
425
                public void ClearPane (Pane pane)
 
426
                {
 
427
                        iconbox[(int) pane].Clear ();
 
428
                        
 
429
                        if (pane == CurrentPane)
 
430
                                resultsWindow.Clear ();
 
431
                }
 
432
                
 
433
                public bool ResultsCanHide { get { return true; } }
 
434
                
 
435
                protected override bool OnExposeEvent (EventExpose evnt)
 
436
                {
 
437
                        Cairo.Context cairo;
 
438
                        
 
439
                        using (cairo = Gdk.CairoHelper.Create (GdkWindow)) {
 
440
                                cairo.Rectangle (evnt.Area.X, evnt.Area.Y, evnt.Area.Width, evnt.Area.Height);
 
441
                                cairo.Color = new Cairo.Color (1.0, 1.0, 1.0, 0.0);
 
442
                                cairo.Operator = Cairo.Operator.Source;
 
443
                                cairo.Paint ();
 
444
                        }
 
445
                        return base.OnExposeEvent (evnt);
 
446
                }
 
447
        }
 
448
}