~ubuntu-branches/ubuntu/wily/docky/wily-proposed

« back to all changes in this revision

Viewing changes to Docky.Widgets/Docky.Widgets/WrapLabel.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// WrapLabel.cs
 
3
//
 
4
// Author:
 
5
//   Aaron Bockover <abockover@novell.com>
 
6
//
 
7
// Adapted for Docky by:
 
8
//   Chris Szikszoy <chris@szikszoy.com>
 
9
//
 
10
// Copyright (C) 2008 Novell, Inc.
 
11
//
 
12
// Permission is hereby granted, free of charge, to any person obtaining
 
13
// a copy of this software and associated documentation files (the
 
14
// "Software"), to deal in the Software without restriction, including
 
15
// without limitation the rights to use, copy, modify, merge, publish,
 
16
// distribute, sublicense, and/or sell copies of the Software, and to
 
17
// permit persons to whom the Software is furnished to do so, subject to
 
18
// the following conditions:
 
19
//
 
20
// The above copyright notice and this permission notice shall be
 
21
// included in all copies or substantial portions of the Software.
 
22
//
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
24
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
25
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
26
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
27
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
28
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
29
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
using System;
 
33
using Gtk;
 
34
 
 
35
namespace Docky.Widgets
 
36
{
 
37
        [System.ComponentModel.ToolboxItem(true)]
 
38
    public class WrapLabel : Widget
 
39
    {
 
40
        private string text;
 
41
        private bool use_markup = false;
 
42
        private bool wrap = true;
 
43
        private Pango.Layout layout;
 
44
        
 
45
        public WrapLabel ()
 
46
        {
 
47
            WidgetFlags |= WidgetFlags.NoWindow;
 
48
        }
 
49
        
 
50
        private void CreateLayout ()
 
51
        {
 
52
            if (layout != null) {
 
53
                layout.Dispose ();
 
54
            }
 
55
            
 
56
            layout = new Pango.Layout (PangoContext);
 
57
            layout.Wrap = Pango.WrapMode.Word;
 
58
        }
 
59
        
 
60
        private void UpdateLayout ()
 
61
        {
 
62
            if (layout == null) {
 
63
                CreateLayout ();
 
64
            }
 
65
            
 
66
            layout.Ellipsize = wrap ? Pango.EllipsizeMode.None : Pango.EllipsizeMode.End;
 
67
 
 
68
            if (text == null) {
 
69
                text = "";
 
70
            }
 
71
            
 
72
            if (use_markup) {
 
73
                layout.SetMarkup (text);
 
74
            } else {
 
75
                layout.SetText (text);
 
76
            }
 
77
            
 
78
            QueueResize ();
 
79
        }
 
80
        
 
81
        protected override void OnStyleSet (Style previous_style)
 
82
        {
 
83
            CreateLayout ();
 
84
            UpdateLayout ();
 
85
            base.OnStyleSet (previous_style);
 
86
        }
 
87
        
 
88
        protected override void OnRealized ()
 
89
        {
 
90
            GdkWindow = Parent.GdkWindow;
 
91
            base.OnRealized ();
 
92
        }
 
93
        
 
94
        protected override void OnSizeAllocated (Gdk.Rectangle allocation)
 
95
        {
 
96
            int lw, lh;
 
97
            
 
98
            layout.Width = (int)(allocation.Width * Pango.Scale.PangoScale);
 
99
            layout.GetPixelSize (out lw, out lh);
 
100
            
 
101
            HeightRequest = lh;
 
102
            
 
103
            base.OnSizeAllocated (allocation);
 
104
        }
 
105
 
 
106
        protected override bool OnExposeEvent (Gdk.EventExpose evnt)
 
107
        {
 
108
            if (evnt.Window == GdkWindow) {
 
109
                Gtk.Style.PaintLayout (Style, GdkWindow, State, false, 
 
110
                    evnt.Area, this, null, Allocation.X, Allocation.Y, layout);
 
111
            }
 
112
 
 
113
            return true;
 
114
        }
 
115
        
 
116
        public void MarkupFormat (string format, params object [] args)
 
117
        {
 
118
            if (args == null || args.Length == 0) {
 
119
                Markup = format;
 
120
                return;
 
121
            }
 
122
            
 
123
            for (int i = 0; i < args.Length; i++) {
 
124
                if (args[i] is string) {
 
125
                    args[i] = GLib.Markup.EscapeText ((string)args[i]);
 
126
                }
 
127
            }
 
128
            
 
129
            Markup = String.Format (format, args);
 
130
        }
 
131
        
 
132
        public bool Wrap {
 
133
            get { return wrap; }
 
134
            set {
 
135
                wrap = value;
 
136
                UpdateLayout ();
 
137
            }
 
138
        }
 
139
        
 
140
        public string Markup {
 
141
            get { return text; }
 
142
            set {
 
143
                use_markup = true;
 
144
                text = value;
 
145
                UpdateLayout ();
 
146
            }
 
147
        }
 
148
        
 
149
        public string Text {
 
150
            get { return text; }
 
151
            set {
 
152
                use_markup = false;
 
153
                text = value;
 
154
                UpdateLayout ();
 
155
            }
 
156
        }       
 
157
    }
 
158
}