chiark / gitweb /
unit: use the UNIT() macro consistently
[elogind.git] / src / wraplabel.vala
1 // Copyright (c) 2005 VMware, Inc.
2
3 // This is a translation of http://git.gnome.org/browse/meld/tree/meld/ui/wraplabel.py,
4 // which in turn is a translation of WrapLabel from libview.
5
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23
24 // Python translation from wrapLabel.{cc|h} by Gian Mario Tagliaretti
25 // Vala translation from wraplabel.py by Zbigniew JÄ™drzejewski-Szmek
26
27 public class WrapLabel : Gtk.Label {
28         private int _wrap_width;
29
30         public WrapLabel(string? text = null) {
31                 this._wrap_width = 0;
32                 var layout = get_layout();
33                 layout.set_wrap(Pango.WrapMode.WORD_CHAR);
34                 if (text != null)
35                         this.set_text(text);
36                 this.set_alignment(0, 0);
37         }
38
39         public override void size_request(out Gtk.Requisition requisition) {
40                 int width, height;
41                 var layout = get_layout();
42                 layout.get_pixel_size(out width, out height);
43                 requisition.width = 0;
44                 requisition.height = height;
45         }
46
47         public override void size_allocate(Gdk.Rectangle allocation) {
48                 base.size_allocate (allocation);
49                 this._set_wrap_width(allocation.width);
50         }
51
52         public new void set_text(string str) {
53                 base.set_text(str);
54                 this._set_wrap_width(this._wrap_width);
55         }
56
57         public new void set_markup(string str) {
58                 base.set_markup(str);
59                 this._set_wrap_width(this._wrap_width);
60         }
61
62         private void _set_wrap_width(int width) {
63                 if (width == 0)
64                         return;
65
66                 var layout = get_layout();
67                 layout.set_width(width * Pango.SCALE);
68                 if (_wrap_width != width) {
69                         this._wrap_width = width;
70                         this.queue_resize();
71                 }
72         }
73 }