1 |
persson |
1799 |
/* ************************************************************************* |
2 |
|
|
* Copyright (c) 2005 VMware, Inc. |
3 |
schoenebeck |
3184 |
* Copyright (c) 2011 - 2017 Andreas Persson |
4 |
persson |
1799 |
* |
5 |
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
6 |
|
|
* a copy of this software and associated documentation files (the |
7 |
|
|
* "Software"), to deal in the Software without restriction, including |
8 |
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
9 |
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
10 |
|
|
* permit persons to whom the Software is furnished to do so, subject to |
11 |
|
|
* the following conditions: |
12 |
|
|
* |
13 |
|
|
* The above copyright notice and this permission notice shall be |
14 |
|
|
* included in all copies or substantial portions of the Software. |
15 |
|
|
* |
16 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 |
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
18 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
19 |
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
20 |
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
21 |
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
22 |
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
23 |
|
|
* *************************************************************************/ |
24 |
|
|
|
25 |
|
|
#include <wrapLabel.hh> |
26 |
|
|
|
27 |
|
|
|
28 |
|
|
/* |
29 |
|
|
* wrapLabel.cc -- |
30 |
|
|
* |
31 |
|
|
* A wrappable label widget. |
32 |
|
|
*/ |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
namespace view { |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
/* |
39 |
|
|
*----------------------------------------------------------------------------- |
40 |
|
|
* |
41 |
|
|
* view::WrapLabel::WrapLabel -- |
42 |
|
|
* |
43 |
|
|
* Constructor. |
44 |
|
|
* |
45 |
|
|
* Results: |
46 |
|
|
* None. |
47 |
|
|
* |
48 |
|
|
* Side effects: |
49 |
|
|
* None. |
50 |
|
|
* |
51 |
|
|
*----------------------------------------------------------------------------- |
52 |
|
|
*/ |
53 |
|
|
|
54 |
|
|
WrapLabel::WrapLabel(const Glib::ustring &text) // IN: The label text |
55 |
persson |
2169 |
: mWrapWidth(0), |
56 |
|
|
mWrapHeight(0) |
57 |
persson |
1799 |
{ |
58 |
|
|
get_layout()->set_wrap(Pango::WRAP_WORD_CHAR); |
59 |
|
|
set_alignment(0.0, 0.0); |
60 |
|
|
set_text(text); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/* |
65 |
|
|
*----------------------------------------------------------------------------- |
66 |
|
|
* |
67 |
|
|
* view::WrapLabel::set_text -- |
68 |
|
|
* |
69 |
|
|
* Override function for Label::set_text() that re-sets the wrapping |
70 |
|
|
* width after the text is set. |
71 |
|
|
* |
72 |
|
|
* Results: |
73 |
|
|
* None. |
74 |
|
|
* |
75 |
|
|
* Side effects: |
76 |
|
|
* None. |
77 |
|
|
* |
78 |
|
|
*----------------------------------------------------------------------------- |
79 |
|
|
*/ |
80 |
|
|
|
81 |
|
|
void |
82 |
|
|
WrapLabel::set_text(const Glib::ustring &str) // IN: The text to set |
83 |
|
|
{ |
84 |
|
|
Label::set_text(str); |
85 |
|
|
|
86 |
|
|
SetWrapWidth(mWrapWidth); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
|
90 |
|
|
/* |
91 |
|
|
*----------------------------------------------------------------------------- |
92 |
|
|
* |
93 |
|
|
* view::WrapLabel::set_markup -- |
94 |
|
|
* |
95 |
|
|
* Override function for Label::set_markup() that re-sets the wrapping |
96 |
|
|
* width after the text is set. |
97 |
|
|
* |
98 |
|
|
* Results: |
99 |
|
|
* None. |
100 |
|
|
* |
101 |
|
|
* Side effects: |
102 |
|
|
* None. |
103 |
|
|
* |
104 |
|
|
*----------------------------------------------------------------------------- |
105 |
|
|
*/ |
106 |
|
|
|
107 |
|
|
void |
108 |
|
|
WrapLabel::set_markup(const Glib::ustring &str) // IN: The text to set |
109 |
|
|
{ |
110 |
|
|
Label::set_markup(str); |
111 |
|
|
|
112 |
|
|
SetWrapWidth(mWrapWidth); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
/* |
117 |
|
|
*----------------------------------------------------------------------------- |
118 |
|
|
* |
119 |
|
|
* view::WrapLabel::on_size_request -- |
120 |
|
|
* |
121 |
|
|
* Override handler for the "size_request" signal. Forces the height |
122 |
|
|
* to be the size necessary for the Pango layout, while allowing the |
123 |
|
|
* width to be flexible. |
124 |
|
|
* |
125 |
|
|
* Results: |
126 |
|
|
* None. |
127 |
|
|
* |
128 |
|
|
* Side effects: |
129 |
|
|
* None. |
130 |
|
|
* |
131 |
|
|
*----------------------------------------------------------------------------- |
132 |
|
|
*/ |
133 |
|
|
|
134 |
|
|
void |
135 |
|
|
WrapLabel::on_size_request(Gtk::Requisition *req) // OUT: Our requested size |
136 |
|
|
{ |
137 |
persson |
2169 |
req->width = 0; |
138 |
|
|
req->height = mWrapHeight; |
139 |
|
|
} |
140 |
persson |
1799 |
|
141 |
|
|
|
142 |
|
|
/* |
143 |
|
|
*----------------------------------------------------------------------------- |
144 |
|
|
* |
145 |
|
|
* view::WrapLabel::on_size_allocate -- |
146 |
|
|
* |
147 |
|
|
* Override handler for the "size_allocate" signal. Sets the wrap width |
148 |
|
|
* to the be width allocated to us. |
149 |
|
|
* |
150 |
|
|
* Results: |
151 |
|
|
* None. |
152 |
|
|
* |
153 |
|
|
* Side effects: |
154 |
|
|
* None. |
155 |
|
|
* |
156 |
|
|
*----------------------------------------------------------------------------- |
157 |
|
|
*/ |
158 |
|
|
|
159 |
|
|
void |
160 |
|
|
WrapLabel::on_size_allocate(Gtk::Allocation &alloc) // IN: Our allocation |
161 |
|
|
{ |
162 |
|
|
Gtk::Label::on_size_allocate(alloc); |
163 |
|
|
|
164 |
|
|
SetWrapWidth(alloc.get_width()); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
/* |
169 |
|
|
*----------------------------------------------------------------------------- |
170 |
|
|
* |
171 |
|
|
* view::WrapLabel::SetWrapWidth -- |
172 |
|
|
* |
173 |
|
|
* Sets the point at which the text should wrap. |
174 |
|
|
* |
175 |
|
|
* Results: |
176 |
|
|
* None. |
177 |
|
|
* |
178 |
|
|
* Side effects: |
179 |
|
|
* None. |
180 |
|
|
* |
181 |
|
|
*----------------------------------------------------------------------------- |
182 |
|
|
*/ |
183 |
|
|
|
184 |
|
|
void |
185 |
persson |
2169 |
WrapLabel::SetWrapWidth(int width) // IN: The wrap width |
186 |
persson |
1799 |
{ |
187 |
|
|
if (width == 0) { |
188 |
|
|
return; |
189 |
|
|
} |
190 |
|
|
|
191 |
schoenebeck |
3184 |
int xPadding, yPadding; |
192 |
|
|
get_padding(xPadding, yPadding); |
193 |
|
|
|
194 |
|
|
width -= 2 * xPadding; |
195 |
|
|
|
196 |
persson |
1799 |
/* |
197 |
|
|
* We may need to reset the wrap width, so do this regardless of whether |
198 |
|
|
* or not we've changed the width. |
199 |
|
|
*/ |
200 |
|
|
get_layout()->set_width(width * Pango::SCALE); |
201 |
|
|
|
202 |
persson |
2169 |
int unused; |
203 |
|
|
get_layout()->get_pixel_size(unused, mWrapHeight); |
204 |
schoenebeck |
3184 |
mWrapHeight += 2 * yPadding; |
205 |
persson |
2169 |
|
206 |
persson |
1799 |
if (mWrapWidth != width) { |
207 |
|
|
mWrapWidth = width; |
208 |
|
|
queue_resize(); |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
|
213 |
|
|
}; /* namespace view */ |