~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.DesignerSupport/MonoDevelop.DesignerSupport.Toolbox/CompactToolboxView.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// CompactToolboxView.cs
3
 
//
4
 
// Author:
5
 
//   Mike Krüger <mkrueger@novell.com>
6
 
//
7
 
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8
 
//
9
 
// Permission is hereby granted, free of charge, to any person obtaining
10
 
// a copy of this software and associated documentation files (the
11
 
// "Software"), to deal in the Software without restriction, including
12
 
// without limitation the rights to use, copy, modify, merge, publish,
13
 
// distribute, sublicense, and/or sell copies of the Software, and to
14
 
// permit persons to whom the Software is furnished to do so, subject to
15
 
// the following conditions:
16
 
// 
17
 
// The above copyright notice and this permission notice shall be
18
 
// included in all copies or substantial portions of the Software.
19
 
// 
20
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
 
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
 
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
 
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
 
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
 
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
 
//
28
 
 
29
 
using System;
30
 
using System.Collections;
31
 
using System.Collections.Generic;
32
 
using Gtk;
33
 
using Gdk;
34
 
 
35
 
namespace MonoDevelop.DesignerSupport.Toolbox
36
 
{
37
 
        public class ToolboxObject
38
 
        {
39
 
                Gdk.Pixbuf icon;
40
 
                string     text;
41
 
                object     tag;
42
 
                
43
 
                public Gdk.Pixbuf Icon {
44
 
                        get {
45
 
                                return icon;
46
 
                        }
47
 
                        set {
48
 
                                icon = value;
49
 
                        }
50
 
                }
51
 
                
52
 
                public string Text {
53
 
                        get {
54
 
                                return text;
55
 
                        }
56
 
                        set {
57
 
                                text = value;
58
 
                        }
59
 
                }
60
 
                
61
 
                public object Tag {
62
 
                        get {
63
 
                                return tag;
64
 
                        }
65
 
                        set {
66
 
                                tag = value;
67
 
                        }
68
 
                }
69
 
                
70
 
                public ToolboxObject (Gdk.Pixbuf icon, string text, object tag)
71
 
                {
72
 
                        this.icon = icon;
73
 
                        this.text = text;
74
 
                        this.tag  = tag;
75
 
                }
76
 
                
77
 
                public bool FilterItem (string filter)
78
 
                {
79
 
                        return !String.IsNullOrEmpty (filter) && Text.ToUpper ().IndexOf (filter.ToUpper ()) < 0;                       
80
 
                }
81
 
        }
82
 
        
83
 
        public class ToolboxCategory
84
 
        {
85
 
                string name;
86
 
                List<ToolboxObject> items = new List<ToolboxObject> (); 
87
 
                        
88
 
                public string Name {
89
 
                        get {
90
 
                                return name;
91
 
                        }
92
 
                        set {
93
 
                                name = value;
94
 
                        }
95
 
                }
96
 
                
97
 
                public List<ToolboxObject> Items {
98
 
                        get {
99
 
                                return items;
100
 
                        }
101
 
                }
102
 
                
103
 
                public ToolboxCategory (string name)
104
 
                {
105
 
                        this.name = name;
106
 
                }
107
 
                
108
 
                public bool ContainsIcons (string filter)
109
 
                {
110
 
                        foreach (ToolboxObject item in Items)
111
 
                                if (!item.FilterItem (filter))
112
 
                                        return true;
113
 
                        return false;
114
 
                }
115
 
        }
116
 
        
117
 
        public class CompactWidget : Gtk.DrawingArea
118
 
        {
119
 
                ToolboxObject         selectedItem = null;
120
 
                List<ToolboxCategory> categories   = new List<ToolboxCategory> ();
121
 
                string filter = "";
122
 
                bool showCategories = true;
123
 
                bool realSizeRequest;
124
 
                const int spacing            = 4;
125
 
                const int categoryHeaderSize = 22;
126
 
                
127
 
                public bool ShowCategories {
128
 
                        get {
129
 
                                return showCategories;
130
 
                        }
131
 
                        set {
132
 
                                if (this.showCategories != value) {
133
 
                                        this.showCategories = value;
134
 
                                        this.QueueDraw ();
135
 
                                }
136
 
                        }
137
 
                }
138
 
                public string Filter {
139
 
                        get {
140
 
                                return filter;
141
 
                        }
142
 
                        set {
143
 
                                if (filter != value) {
144
 
                                        filter = value;
145
 
                                        this.QueueDraw ();
146
 
                                }
147
 
                        }
148
 
                }
149
 
                public IEnumerable<ToolboxObject> AllItems {
150
 
                        get {
151
 
                                foreach (ToolboxCategory category in this.categories) {
152
 
                                        foreach (ToolboxObject item in category.Items) {
153
 
                                                yield return item;
154
 
                                        }
155
 
                                }
156
 
                        }
157
 
                }
158
 
                
159
 
                public int ItemCount {
160
 
                        get {
161
 
                                int result = 0;
162
 
                                foreach (ToolboxCategory category in this.categories) 
163
 
                                        result += category.Items.Count;
164
 
                                return result;
165
 
                        }
166
 
                }
167
 
                
168
 
                public List<ToolboxCategory> Categories {
169
 
                        get {
170
 
                                return categories;
171
 
                        }
172
 
                }
173
 
                
174
 
                public ToolboxObject SelectedItem {
175
 
                        get {
176
 
                                return selectedItem;
177
 
                        }
178
 
                        set {
179
 
                                if (this.selectedItem != value) {
180
 
                                        this.selectedItem = value;
181
 
                                        this.QueueDraw ();
182
 
                                        OnSelectedItemChanged (EventArgs.Empty);
183
 
                                }
184
 
                        }
185
 
                }
186
 
                
187
 
                protected virtual void OnSelectedItemChanged (EventArgs e)
188
 
                {
189
 
                        if (SelectedItemChanged != null) 
190
 
                                SelectedItemChanged (this, e);
191
 
                }
192
 
                
193
 
                public event EventHandler SelectedItemChanged;
194
 
                
195
 
                public CompactWidget ()
196
 
                {
197
 
                        this.Events =  EventMask.ExposureMask | 
198
 
                                           EventMask.EnterNotifyMask |
199
 
                                           EventMask.LeaveNotifyMask |
200
 
                                           EventMask.ButtonPressMask | 
201
 
                                           EventMask.ButtonReleaseMask | 
202
 
                                               EventMask.PointerMotionMask
203
 
                        ;
204
 
                }
205
 
                
206
 
                public ToolboxCategory GetCategory (string name)
207
 
                {
208
 
                        foreach (ToolboxCategory category in this.categories) 
209
 
                                if (category.Name == name)
210
 
                                        return category;
211
 
                        ToolboxCategory result = new ToolboxCategory (name);
212
 
                        this.categories.Add (result);
213
 
                        return result;
214
 
                }
215
 
                
216
 
                System.Drawing.Size IconSize {
217
 
                        get {
218
 
                                int width  = 0;
219
 
                                int height = 0;
220
 
                                foreach (ToolboxObject item in this.AllItems) { 
221
 
                                        width  = Math.Max (width, item.Icon.Width);
222
 
                                        height = Math.Max (height, item.Icon.Height);
223
 
                                }
224
 
                                return new System.Drawing.Size (width, height);
225
 
                        }
226
 
                }
227
 
                
228
 
                protected override bool OnLeaveNotifyEvent (Gdk.EventCrossing e)
229
 
                {
230
 
                        // Don't clear the selection when clicking on a button
231
 
                        if (e.Detail != NotifyType.Ancestor || e.Mode != Gdk.CrossingMode.Grab)
232
 
                                ClearMouseOverItem ();
233
 
                        return true;
234
 
                        //return OnLeaveNotifyEvent (e);
235
 
                }
236
 
                
237
 
                protected override void OnSizeRequested (ref Requisition req)
238
 
                {
239
 
                        if (!realSizeRequest) {
240
 
                                // Request a minimal width, to size recalculation infinite loops with
241
 
                                // small widths, due to the vscrollbar being shown and hidden.
242
 
                                req.Width = 50;
243
 
                                req.Height = 0;
244
 
                                return;
245
 
                        }
246
 
                        int xpos = spacing;
247
 
                        int ypos = spacing;
248
 
                        Iterate (ref xpos, ref ypos, null, null);
249
 
                        req.Width  = 50; 
250
 
                        req.Height = ypos;
251
 
                }
252
 
                
253
 
                protected override void OnSizeAllocated (Gdk.Rectangle allocation)              
254
 
                {
255
 
                        base.OnSizeAllocated (allocation);
256
 
                        if (!realSizeRequest) {
257
 
                                realSizeRequest = true;
258
 
                                QueueResize ();
259
 
                        }
260
 
                        else
261
 
                                realSizeRequest = false;
262
 
                }
263
 
                
264
 
                protected override bool OnButtonPressEvent (Gdk.EventButton e)
265
 
                {
266
 
                        SelectedItem = mouseOverItem;
267
 
                        return base.OnButtonPressEvent (e);
268
 
                }
269
 
                
270
 
                ToolboxObject mouseOverItem;
271
 
                
272
 
                delegate void CategoryAction (ToolboxCategory category);
273
 
                delegate void Action (ToolboxObject item);
274
 
                void IterateIcons (IEnumerable<ToolboxObject> collection, ref int xpos, ref int ypos, Action action)
275
 
                {
276
 
                        foreach (ToolboxObject item in collection) {
277
 
                                if (item.FilterItem (filter))
278
 
                                        continue;
279
 
                                if (xpos + IconSize.Width + spacing >= this.GdkWindow.VisibleRegion.Clipbox.Width) {
280
 
                                        xpos = spacing;
281
 
                                        ypos += IconSize.Height + spacing;
282
 
                                }
283
 
                                if (action != null)
284
 
                                        action (item);
285
 
                                xpos += IconSize.Width + spacing;
286
 
                        }
287
 
                        ypos += IconSize.Height + spacing;
288
 
                }
289
 
                void Iterate (ref int xpos, ref int ypos,  CategoryAction catAction, Action action)
290
 
                {
291
 
                        if (this.showCategories) {
292
 
                                foreach (ToolboxCategory category in this.Categories) {
293
 
                                        if (!category.ContainsIcons (filter))
294
 
                                                continue;
295
 
                                        xpos = spacing;
296
 
                                        if (catAction != null)
297
 
                                                catAction (category);
298
 
                                        ypos += categoryHeaderSize;
299
 
                                        IterateIcons (category.Items, ref xpos, ref  ypos, action);
300
 
                                }
301
 
                        } else {
302
 
                                IterateIcons (this.AllItems, ref xpos, ref  ypos, action);
303
 
                        }
304
 
                }
305
 
                
306
 
                void ClearMouseOverItem ()
307
 
                {
308
 
                        if (mouseOverItem != null) {
309
 
                                mouseOverItem = null;
310
 
                                HideTooltipWindow ();
311
 
                                this.QueueDraw ();
312
 
                        }
313
 
                }
314
 
                
315
 
                CustomTooltipWindow tooltipWindow = null;
316
 
                public void HideTooltipWindow ()
317
 
                {
318
 
                        if (tooltipWindow != null) {
319
 
                                tooltipWindow.Destroy ();
320
 
                                tooltipWindow = null;
321
 
                        }
322
 
                }
323
 
                public void ShowTooltip (string text, int x, int y, int iconWidth)
324
 
                {
325
 
                        HideTooltipWindow (); 
326
 
                        tooltipWindow = new CustomTooltipWindow ();
327
 
                        tooltipWindow.Tooltip = text;
328
 
                        int ox, oy;
329
 
                        this.GdkWindow.GetOrigin (out ox, out oy);
330
 
                        int w = tooltipWindow.Child.SizeRequest().Width;
331
 
                        tooltipWindow.Move (ox + x - (w - iconWidth)/2, oy + y);
332
 
                        tooltipWindow.ShowAll ();
333
 
                }
334
 
 
335
 
                protected override bool OnMotionNotifyEvent (Gdk.EventMotion e)
336
 
                {
337
 
                        int xpos = spacing;
338
 
                        int ypos = spacing;
339
 
                        bool found = false;
340
 
                        Iterate (ref xpos, ref ypos, null, delegate (ToolboxObject item) {
341
 
                                if (xpos <= e.X && e.X <= xpos + IconSize.Width + spacing  &&
342
 
                                    ypos <= e.Y && e.Y <= ypos + IconSize.Height + spacing) {
343
 
                                        found = true;
344
 
                                        if (mouseOverItem != item) {
345
 
                                                mouseOverItem = item;
346
 
                                                ShowTooltip (item.Text, xpos - 2, ypos + IconSize.Height + 3, IconSize.Width + 4);
347
 
                                                this.QueueDraw ();
348
 
                                        }
349
 
                                }
350
 
                        });
351
 
                        if (!found) {
352
 
                                ClearMouseOverItem ();
353
 
                        }
354
 
                        return base.OnMotionNotifyEvent (e);
355
 
                }
356
 
                
357
 
                protected override bool OnExposeEvent (Gdk.EventExpose e)
358
 
                {
359
 
                        Gdk.Window win = e.Window;
360
 
                        Gdk.Rectangle area = e.Area;
361
 
                        win.DrawRectangle (Style.BaseGC (StateType.Normal), true, area);
362
 
                        
363
 
                        int xpos = spacing;
364
 
                        int ypos = spacing;
365
 
                        Iterate (ref xpos, ref ypos, 
366
 
                        delegate (ToolboxCategory category) {
367
 
                                Pango.Layout layout = new Pango.Layout (this.PangoContext);
368
 
                                layout.Width = Pango.Units.FromPixels (320);
369
 
                                layout.Wrap = Pango.WrapMode.Word;
370
 
                                layout.Alignment = Pango.Alignment.Left;
371
 
                                layout.FontDescription = Pango.FontDescription.FromString ("Ahafoni CLM Bold 10");
372
 
                                layout.SetMarkup (category.Name);
373
 
                                xpos = spacing;
374
 
                                win.DrawLayout (Style.ForegroundGC (StateType.Normal), xpos, ypos, layout);
375
 
                        }, 
376
 
                        delegate (ToolboxObject item) {
377
 
                                if (item == selectedItem) {
378
 
                                        win.DrawRectangle (Style.BaseGC (StateType.Selected), 
379
 
                                                           true, 
380
 
                                                           new Gdk.Rectangle (xpos - 1, 
381
 
                                                                              ypos - 1, 
382
 
                                                                              IconSize.Width  + 2, 
383
 
                                                                              IconSize.Height + 2));
384
 
                                }
385
 
                                win.DrawPixbuf (this.Style.ForegroundGC (StateType.Normal), 
386
 
                                                item.Icon, 0, 0, 
387
 
                                                xpos + 1 + (IconSize.Width  - item.Icon.Width) / 2, 
388
 
                                                ypos + 1 + (IconSize.Height - item.Icon.Height) / 2, 
389
 
                                                item.Icon.Width, item.Icon.Height, Gdk.RgbDither.None, 0, 0);
390
 
                                
391
 
                                if (item == mouseOverItem) {
392
 
                                        win.DrawRectangle (Style.DarkGC (StateType.Prelight), 
393
 
                                                           false, 
394
 
                                                           new Gdk.Rectangle (xpos - 2, 
395
 
                                                                              ypos - 2 , 
396
 
                                                                              IconSize.Width + 3, 
397
 
                                                                              IconSize.Height + 3));
398
 
                                }
399
 
                        });
400
 
                        return true;            
401
 
                }
402
 
        }
403
 
        
404
 
        public class CompactToolboxView : Gtk.ScrolledWindow
405
 
        {
406
 
                CompactWidget compactWidget;
407
 
                
408
 
                public object CurrentlySelected {
409
 
                        get {
410
 
                                return compactWidget.SelectedItem != null ? compactWidget.SelectedItem.Tag : null;
411
 
                        }
412
 
                }
413
 
                
414
 
                public bool ShowCategories {
415
 
                        get {
416
 
                                return compactWidget.ShowCategories;
417
 
                        }
418
 
                        set {
419
 
                                compactWidget.ShowCategories = value;
420
 
                        }
421
 
                }
422
 
                public string Filter {
423
 
                        get {
424
 
                                return compactWidget.Filter;
425
 
                        }
426
 
                        set {
427
 
                                compactWidget.Filter = value;
428
 
                        }
429
 
                }
430
 
 
431
 
                public CompactToolboxView()
432
 
                {
433
 
                        compactWidget = new CompactWidget ();
434
 
                                        
435
 
                        compactWidget.SelectedItemChanged += delegate {
436
 
                                OnSelectionChanged (EventArgs.Empty);
437
 
                        };
438
 
                        
439
 
                        this.ShadowType = ShadowType.None;
440
 
                        Viewport v = new Viewport ();
441
 
                        this.Add (v);
442
 
                        v.ShadowType = ShadowType.None;
443
 
                        v.Add (compactWidget);
444
 
                        this.HscrollbarPolicy = PolicyType.Never;
445
 
                        this.VscrollbarPolicy = PolicyType.Automatic;
446
 
                }
447
 
                
448
 
                public void SetNodes (ICollection nodes)
449
 
                {
450
 
                        compactWidget.Categories.Clear ();
451
 
                        foreach (BaseToolboxNode node in nodes) {
452
 
                                ToolboxObject newItem = new ToolboxObject (node.ViewIcon, node.Label, node);
453
 
                                if (node is ItemToolboxNode) {
454
 
                                        ToolboxCategory category = compactWidget.GetCategory (((ItemToolboxNode)node).Category);
455
 
                                        category.Items.Add (newItem);
456
 
                                }
457
 
                        }
458
 
                        QueueDraw ();
459
 
                }
460
 
                
461
 
                protected virtual void OnSelectionChanged (EventArgs e)
462
 
                {
463
 
                        if (this.SelectionChanged != null)
464
 
                                this.SelectionChanged (this, e);
465
 
                }
466
 
                
467
 
                public event EventHandler SelectionChanged;
468
 
        }
469
 
        
470
 
        public class CustomTooltipWindow : Gtk.Window
471
 
        {
472
 
                string tooltip;
473
 
                public string Tooltip {
474
 
                        get {
475
 
                                return tooltip;
476
 
                        }
477
 
                        set {
478
 
                                tooltip = value;
479
 
                                label.Markup = tooltip;
480
 
                        }
481
 
                }
482
 
                
483
 
                Label label = new Label ();
484
 
                public CustomTooltipWindow () : base (Gtk.WindowType.Popup)
485
 
                {
486
 
                        Name = "gtk-tooltips";
487
 
                        label.Xalign = 0;
488
 
                        label.Xpad = 3;
489
 
                        label.Ypad = 3;
490
 
                        Add (label);
491
 
                }
492
 
                
493
 
                protected override bool OnExposeEvent (Gdk.EventExpose ev)
494
 
                {
495
 
                        base.OnExposeEvent (ev);
496
 
                        Gtk.Requisition req = SizeRequest ();
497
 
                        Gtk.Style.PaintFlatBox (this.Style, 
498
 
                                                this.GdkWindow, 
499
 
                                                Gtk.StateType.Normal, 
500
 
                                                Gtk.ShadowType.Out, 
501
 
                                                Gdk.Rectangle.Zero, 
502
 
                                                this, "tooltip", 0, 0, req.Width, req.Height);
503
 
                        return true;
504
 
                }
505
 
        }       
506
 
}