~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Components/SearchEntry.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
 
30
30
using System;
31
31
using Gtk;
 
32
using MonoDevelop.Ide.Gui;
32
33
 
33
34
namespace MonoDevelop.Components
34
35
{
35
36
        [System.ComponentModel.ToolboxItem(true)]
36
37
        public class SearchEntry : EventBox
37
38
        {
 
39
                Alignment alignment;
 
40
                Alignment entryAlignment;
38
41
                private HBox box;
39
42
                private Entry entry;
40
43
                private HoverImageButton filter_button;
50
53
 
51
54
                private event EventHandler filter_changed;
52
55
                private event EventHandler entry_changed;
 
56
                EventHandler activated_event;
 
57
                bool roundedShape;
 
58
                bool hasFrame = true;
 
59
                bool customRoundedShapeDrawing = false;
53
60
 
54
61
                public event EventHandler Changed {
55
62
                        add { entry_changed += value; }
57
64
                }
58
65
 
59
66
                public event EventHandler Activated {
60
 
                        add { entry.Activated += value; }
61
 
                        remove { entry.Activated -= value; }
 
67
                        add { activated_event += value; }
 
68
                        remove { activated_event -= value; }
62
69
                }
63
70
 
64
71
                public event EventHandler FilterChanged {
66
73
                        remove { filter_changed -= value; }
67
74
                }
68
75
                
69
 
                bool forceFilterButtonVisible;
 
76
                bool forceFilterButtonVisible = true;
70
77
                public bool ForceFilterButtonVisible {
71
78
                        get {
72
79
                                return forceFilterButtonVisible; 
86
93
                        get { return this.entry; }
87
94
                }
88
95
 
 
96
                public bool HasFrame {
 
97
                        get { return hasFrame; }
 
98
                        set { hasFrame = value; QueueDraw (); }
 
99
                }
 
100
 
 
101
                public bool RoundedShape {
 
102
                        get { return roundedShape; }
 
103
                        set {
 
104
                                roundedShape = value;
 
105
                                if (value)
 
106
                                        entry.Name = "search-entry";
 
107
                                else
 
108
                                        entry.Name = "";
 
109
                                ShowHideButtons ();
 
110
                                QueueDraw ();
 
111
                        }
 
112
                }
 
113
 
89
114
                public SearchEntry ()
90
115
                {
91
116
                        AppPaintable = true;
103
128
                                filter_button.Pixbuf = value;
104
129
                        }
105
130
                }
106
 
                
 
131
 
107
132
                private void BuildWidget ()
108
133
                {
 
134
                        alignment = new Alignment (0.5f, 0.5f, 1f, 0f);
 
135
                        alignment.SetPadding (1, 1, 3, 3);
 
136
                        VisibleWindow = false;
 
137
 
109
138
                        box = new HBox ();
110
139
                        entry = new FramelessEntry (this);
111
 
                        filter_button = new HoverImageButton (IconSize.Menu, new string[] { Stock.Find });
112
 
                        clear_button = new HoverImageButton (IconSize.Menu, new string[] { Stock.Clear });
113
 
                        
 
140
                        filter_button = new HoverImageButton (IconSize.Menu, "md-searchbox-search");
 
141
                        clear_button = new HoverImageButton (IconSize.Menu, "md-searchbox-clear");
 
142
 
 
143
                        entryAlignment = new Gtk.Alignment (0.5f, 0.5f, 1f, 1f);
 
144
                        alignment.SetPadding (0, 0, 3, 3);
 
145
                        entryAlignment.Add (entry);
114
146
                        box.PackStart (filter_button, false, false, 0);
115
 
                        box.PackStart (entry, true, true, 0);
 
147
                        box.PackStart (entryAlignment, true, true, 0);
116
148
                        box.PackStart (clear_button, false, false, 0);
117
 
                        Add (box);
118
 
                        box.ShowAll ();
 
149
                        alignment.Add (box);
 
150
                        Add (alignment);
 
151
                        alignment.ShowAll ();
119
152
                        
120
153
                        entry.StyleSet += OnInnerEntryStyleSet;
121
154
                        entry.StateChanged += OnInnerEntryStateChanged;
122
155
                        entry.FocusInEvent += OnInnerEntryFocusEvent;
123
156
                        entry.FocusOutEvent += OnInnerEntryFocusEvent;
124
157
                        entry.Changed += OnInnerEntryChanged;
125
 
                        
126
 
                        filter_button.Image.Xpad = 2;
127
 
                        clear_button.Image.Xpad = 2;
 
158
                        entry.Activated += delegate {
 
159
                                NotifyActivated ();
 
160
                        };
 
161
 
 
162
                        filter_button.Image.Xpad = 0;
 
163
                        clear_button.Image.Xpad = 0;
128
164
                        filter_button.CanFocus = false;
129
165
                        clear_button.CanFocus = false;
130
166
                        
131
167
                        filter_button.ButtonReleaseEvent += OnButtonReleaseEvent;
132
168
                        clear_button.ButtonReleaseEvent += OnButtonReleaseEvent;
133
169
                        clear_button.Clicked += OnClearButtonClicked;
134
 
                        
135
 
                        filter_button.Visible = false;
136
 
                        clear_button.Visible = false;
137
 
                        
 
170
 
 
171
                        ShowHideButtons ();
 
172
                }
 
173
 
 
174
                protected override void OnSizeRequested (ref Requisition requisition)
 
175
                {
 
176
                        if (HeightRequest != -1 && box.HeightRequest != HeightRequest)
 
177
                                box.HeightRequest = HeightRequest;
 
178
                        if (box.HeightRequest != -1 && HeightRequest == -1)
 
179
                                box.HeightRequest = -1;
 
180
                        base.OnSizeRequested (ref requisition);
138
181
                }
139
182
 
140
183
                Gtk.EventBox statusLabelEventBox;
150
193
                        return statusLabelEventBox;
151
194
                }
152
195
 
 
196
                void NotifyActivated ()
 
197
                {
 
198
                        if (activated_event != null)
 
199
                                activated_event (this, EventArgs.Empty);
 
200
                }
 
201
 
153
202
                private void BuildMenu ()
154
203
                {
155
204
                        menu = new Menu ();
173
222
                private void ShowHideButtons ()
174
223
                {
175
224
                        clear_button.Visible = entry.Text.Length > 0;
 
225
                        entryAlignment.RightPadding = (uint) (!clear_button.Visible && roundedShape ? 6 : 0);
 
226
 
176
227
                        filter_button.Visible = ForceFilterButtonVisible || (menu != null && menu.Children.Length > 0);
 
228
                        entryAlignment.LeftPadding = (uint) (!filter_button.Visible && roundedShape ? 6 : 0);
177
229
                }
178
230
 
179
231
                private void OnPositionMenu (Menu menu, out int x, out int y, out bool push_in)
245
297
                        clear_button.ModifyBg (entry.State, color);
246
298
                        if (statusLabelEventBox != null)
247
299
                                statusLabelEventBox.ModifyBg (entry.State, color);
248
 
                        box.BorderWidth = (uint)entry.Style.XThickness;
 
300
 
 
301
                        box.BorderWidth = 0;
 
302
                        var h = entry.SizeRequest ().Height + entry.Style.Ythickness * 2;
 
303
                        var req = entry.SizeRequest ().Height;
 
304
                        req = Math.Max (req, filter_button.SizeRequest ().Height);
 
305
                        req = Math.Max (req, clear_button.SizeRequest ().Height);
 
306
                        var diff = h - req;
 
307
                        if (diff > 1)
 
308
                                box.BorderWidth = (uint)(diff / 2);
249
309
                }
250
310
 
251
311
                private void OnInnerEntryStyleSet (object o, StyleSetArgs args)
293
353
                {
294
354
                        active_filter_id = 0;
295
355
                        entry.Text = String.Empty;
 
356
                        NotifyActivated ();
296
357
                }
297
358
                
298
359
                protected override void OnDestroyed ()
309
370
                        if (evnt.Key == Gdk.Key.Escape) {
310
371
                                active_filter_id = 0;
311
372
                                entry.Text = String.Empty;
 
373
                                NotifyActivated ();
312
374
                                return true;
313
375
                        }
314
376
                        return base.OnKeyPressEvent (evnt);
316
378
 
317
379
                protected override bool OnExposeEvent (Gdk.EventExpose evnt)
318
380
                {
319
 
                        Style.PaintFlatBox (entry.Style, GdkWindow, State, ShadowType.None,
320
 
                                evnt.Area, this, "entry_bg", 0, 0, Allocation.Width, Allocation.Height);
 
381
                        var alloc = new Gdk.Rectangle (alignment.Allocation.X, box.Allocation.Y, alignment.Allocation.Width, box.Allocation.Height);
 
382
 
 
383
                        if (hasFrame && (!roundedShape || (roundedShape && !customRoundedShapeDrawing))) {
 
384
                                Style.PaintShadow (entry.Style, GdkWindow, StateType.Normal, ShadowType.In,
 
385
                                                   evnt.Area, entry, "entry", alloc.X, alloc.Y, alloc.Width, alloc.Height);
 
386
/*                              using (var ctx = Gdk.CairoHelper.Create (GdkWindow)) {
 
387
                                        ctx.LineWidth = 1;
 
388
                                        ctx.Rectangle (alloc.X + 0.5, alloc.Y + 0.5, alloc.Width - 1, alloc.Height - 1);
 
389
                                        ctx.Color = new Cairo.Color (1,0,0);
 
390
                                        ctx.Stroke ();
 
391
                                }*/
 
392
                        }
 
393
                        else if (!roundedShape) {
 
394
                                using (var ctx = Gdk.CairoHelper.Create (GdkWindow)) {
 
395
                                        CairoExtensions.RoundedRectangle (ctx, alloc.X + 0.5, alloc.Y + 0.5, alloc.Width - 1, alloc.Height - 1, 4);
 
396
                                        ctx.Color = entry.Style.Base (Gtk.StateType.Normal).ToCairoColor ();
 
397
                                        ctx.Fill ();
 
398
                                }
 
399
                        }
 
400
                        else {
 
401
                                using (var ctx = Gdk.CairoHelper.Create (GdkWindow)) {
 
402
                                        RoundBorder (ctx, alloc.X + 0.5, alloc.Y + 0.5, alloc.Width - 1, alloc.Height - 1);
 
403
                                        ctx.Color = entry.Style.Base (Gtk.StateType.Normal).ToCairoColor ();
 
404
                                        ctx.Fill ();
 
405
                                }
 
406
                        }
 
407
 
321
408
                        PropagateExpose (Child, evnt);
322
 
                        Style.PaintShadow (entry.Style, GdkWindow, StateType.Normal, ShadowType.In,
323
 
                                evnt.Area, entry, "entry", 0, 0, Allocation.Width, Allocation.Height);
 
409
 
 
410
                        if (hasFrame && roundedShape && customRoundedShapeDrawing) {
 
411
                                using (var ctx = Gdk.CairoHelper.Create (GdkWindow)) {
 
412
                                        RoundBorder (ctx, alloc.X + 0.5, alloc.Y + 0.5, alloc.Width - 1, alloc.Height - 1);
 
413
                                        ctx.Color = Styles.WidgetBorderColor;
 
414
                                        ctx.LineWidth = 1;
 
415
                                        ctx.Stroke ();
 
416
                                }
 
417
                        }
324
418
                        return true;
325
419
                }
326
420
 
 
421
                static void RoundBorder (Cairo.Context ctx, double x, double y, double w, double h)
 
422
                {
 
423
                        double r = h / 2;
 
424
                        ctx.Arc (x + r, y + r, r, Math.PI / 2, Math.PI + Math.PI / 2);
 
425
                        ctx.LineTo (x + w - r, y);
 
426
                        
 
427
                        ctx.Arc (x + w - r, y + r, r, Math.PI + Math.PI / 2, Math.PI + Math.PI + Math.PI / 2);
 
428
                        
 
429
                        ctx.LineTo (x + r, y + h);
 
430
                        
 
431
                        ctx.ClosePath ();
 
432
                }
 
433
 
327
434
                protected override void OnShown ()
328
435
                {
329
436
                        base.OnShown ();
373
480
                        return item;
374
481
                }
375
482
 
 
483
                public MenuItem AddMenuItem (string label)
 
484
                {
 
485
                        var item = new MenuItem (label);
 
486
                        menu.Append (item);
 
487
                        return item;
 
488
                }
 
489
 
376
490
                public void AddFilterSeparator ()
377
491
                {
378
492
                        menu.Append (new SeparatorMenuItem ());