1 |
/* ************************************************************************* |
2 |
* Copyright (c) 2005 VMware, Inc. |
3 |
* Copyright (c) 2011 - 2017 Andreas Persson |
4 |
* |
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 |
#ifdef PANGOMM_HEADER_FILE |
27 |
# include PANGOMM_HEADER_FILE(pangomm.h) |
28 |
# include PANGOMM_HEADER_FILE(pangommconfig.h) |
29 |
#else |
30 |
# include <pangomm/pangomm.h> |
31 |
# include <pangommconfig.h> |
32 |
#endif |
33 |
|
34 |
// we don't need this WrapLabel helper class on GTKMM 3 and higher |
35 |
#if GTKMM_MAJOR_VERSION < 3 |
36 |
|
37 |
/* |
38 |
* wrapLabel.cc -- |
39 |
* |
40 |
* A wrappable label widget. |
41 |
*/ |
42 |
|
43 |
|
44 |
namespace view { |
45 |
|
46 |
|
47 |
/* |
48 |
*----------------------------------------------------------------------------- |
49 |
* |
50 |
* view::WrapLabel::WrapLabel -- |
51 |
* |
52 |
* Constructor. |
53 |
* |
54 |
* Results: |
55 |
* None. |
56 |
* |
57 |
* Side effects: |
58 |
* None. |
59 |
* |
60 |
*----------------------------------------------------------------------------- |
61 |
*/ |
62 |
|
63 |
WrapLabel::WrapLabel(const Glib::ustring &text) // IN: The label text |
64 |
: mWrapWidth(0), |
65 |
mWrapHeight(0) |
66 |
{ |
67 |
// pangomm >= 2.35.1 |
68 |
#if PANGOMM_MAJOR_VERSION > 2 || (PANGOMM_MAJOR_VERSION == 2 && (PANGOMM_MINOR_VERSION > 35 || (PANGOMM_MINOR_VERSION == 35 && PANGOMM_MICRO_VERSION >= 1))) |
69 |
get_layout()->set_wrap(Pango::WrapMode::WORD_CHAR); |
70 |
#else |
71 |
get_layout()->set_wrap(Pango::WRAP_WORD_CHAR); |
72 |
#endif |
73 |
set_alignment(0.0, 0.0); |
74 |
set_text(text); |
75 |
} |
76 |
|
77 |
|
78 |
/* |
79 |
*----------------------------------------------------------------------------- |
80 |
* |
81 |
* view::WrapLabel::set_text -- |
82 |
* |
83 |
* Override function for Label::set_text() that re-sets the wrapping |
84 |
* width after the text is set. |
85 |
* |
86 |
* Results: |
87 |
* None. |
88 |
* |
89 |
* Side effects: |
90 |
* None. |
91 |
* |
92 |
*----------------------------------------------------------------------------- |
93 |
*/ |
94 |
|
95 |
void |
96 |
WrapLabel::set_text(const Glib::ustring &str) // IN: The text to set |
97 |
{ |
98 |
Label::set_text(str); |
99 |
|
100 |
SetWrapWidth(mWrapWidth); |
101 |
} |
102 |
|
103 |
|
104 |
/* |
105 |
*----------------------------------------------------------------------------- |
106 |
* |
107 |
* view::WrapLabel::set_markup -- |
108 |
* |
109 |
* Override function for Label::set_markup() that re-sets the wrapping |
110 |
* width after the text is set. |
111 |
* |
112 |
* Results: |
113 |
* None. |
114 |
* |
115 |
* Side effects: |
116 |
* None. |
117 |
* |
118 |
*----------------------------------------------------------------------------- |
119 |
*/ |
120 |
|
121 |
void |
122 |
WrapLabel::set_markup(const Glib::ustring &str) // IN: The text to set |
123 |
{ |
124 |
Label::set_markup(str); |
125 |
|
126 |
SetWrapWidth(mWrapWidth); |
127 |
} |
128 |
|
129 |
|
130 |
/* |
131 |
*----------------------------------------------------------------------------- |
132 |
* |
133 |
* view::WrapLabel::on_size_request -- |
134 |
* |
135 |
* Override handler for the "size_request" signal. Forces the height |
136 |
* to be the size necessary for the Pango layout, while allowing the |
137 |
* width to be flexible. |
138 |
* |
139 |
* Results: |
140 |
* None. |
141 |
* |
142 |
* Side effects: |
143 |
* None. |
144 |
* |
145 |
*----------------------------------------------------------------------------- |
146 |
*/ |
147 |
|
148 |
void |
149 |
WrapLabel::on_size_request(Gtk::Requisition *req) // OUT: Our requested size |
150 |
{ |
151 |
req->width = 0; |
152 |
req->height = mWrapHeight; |
153 |
} |
154 |
|
155 |
|
156 |
/* |
157 |
*----------------------------------------------------------------------------- |
158 |
* |
159 |
* view::WrapLabel::on_size_allocate -- |
160 |
* |
161 |
* Override handler for the "size_allocate" signal. Sets the wrap width |
162 |
* to the be width allocated to us. |
163 |
* |
164 |
* Results: |
165 |
* None. |
166 |
* |
167 |
* Side effects: |
168 |
* None. |
169 |
* |
170 |
*----------------------------------------------------------------------------- |
171 |
*/ |
172 |
|
173 |
void |
174 |
WrapLabel::on_size_allocate(Gtk::Allocation &alloc) // IN: Our allocation |
175 |
{ |
176 |
Gtk::Label::on_size_allocate(alloc); |
177 |
|
178 |
SetWrapWidth(alloc.get_width()); |
179 |
} |
180 |
|
181 |
|
182 |
/* |
183 |
*----------------------------------------------------------------------------- |
184 |
* |
185 |
* view::WrapLabel::SetWrapWidth -- |
186 |
* |
187 |
* Sets the point at which the text should wrap. |
188 |
* |
189 |
* Results: |
190 |
* None. |
191 |
* |
192 |
* Side effects: |
193 |
* None. |
194 |
* |
195 |
*----------------------------------------------------------------------------- |
196 |
*/ |
197 |
|
198 |
void |
199 |
WrapLabel::SetWrapWidth(int width) // IN: The wrap width |
200 |
{ |
201 |
if (width == 0) { |
202 |
return; |
203 |
} |
204 |
|
205 |
int xPadding, yPadding; |
206 |
get_padding(xPadding, yPadding); |
207 |
|
208 |
width -= 2 * xPadding; |
209 |
|
210 |
/* |
211 |
* We may need to reset the wrap width, so do this regardless of whether |
212 |
* or not we've changed the width. |
213 |
*/ |
214 |
get_layout()->set_width(width * Pango::SCALE); |
215 |
|
216 |
int unused; |
217 |
get_layout()->get_pixel_size(unused, mWrapHeight); |
218 |
mWrapHeight += 2 * yPadding; |
219 |
|
220 |
if (mWrapWidth != width) { |
221 |
mWrapWidth = width; |
222 |
queue_resize(); |
223 |
} |
224 |
} |
225 |
|
226 |
|
227 |
}; /* namespace view */ |
228 |
|
229 |
#endif // GTKMM_MAJOR_VERSION < 3 |