1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* This file is part of the KDE libraries |
---|
3 | Copyright (C) 1997 Martin Jones (mjones@kde.org) |
---|
4 | (C) 1997 Torben Weis (weis@kde.org) |
---|
5 | |
---|
6 | This library is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU Library General Public |
---|
8 | License as published by the Free Software Foundation; either |
---|
9 | version 2 of the License, or (at your option) any later version. |
---|
10 | |
---|
11 | This library is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | Library General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU Library General Public License |
---|
17 | along with this library; see the file COPYING.LIB. If not, write to |
---|
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | #include "htmlclueh.h" |
---|
24 | |
---|
25 | |
---|
26 | static HTMLClueClass *parent_class = NULL; |
---|
27 | |
---|
28 | HTMLClueHClass html_clueh_class; |
---|
29 | |
---|
30 | |
---|
31 | static void |
---|
32 | copy (HTMLObject *self, |
---|
33 | HTMLObject *dest) |
---|
34 | { |
---|
35 | (* HTML_OBJECT_CLASS (parent_class)->copy) (self, dest); |
---|
36 | |
---|
37 | HTML_CLUEH (dest)->indent = HTML_CLUEH (self)->indent; |
---|
38 | } |
---|
39 | |
---|
40 | static void |
---|
41 | set_max_width (HTMLObject *o, HTMLPainter *painter, gint w) |
---|
42 | { |
---|
43 | HTMLObject *obj; |
---|
44 | |
---|
45 | o->max_width = w; |
---|
46 | |
---|
47 | /* First calculate width minus fixed width objects */ |
---|
48 | for (obj = HTML_CLUE (o)->head; obj != 0; obj = obj->next) { |
---|
49 | if (obj->percent <= 0) /* i.e. fixed width objects */ |
---|
50 | w -= obj->width; |
---|
51 | } |
---|
52 | |
---|
53 | /* Now call set_max_width for variable objects */ |
---|
54 | for (obj = HTML_CLUE (o)->head; obj != 0; obj = obj->next) { |
---|
55 | if (obj->percent > 0) |
---|
56 | html_object_set_max_width (obj, painter, |
---|
57 | w - HTML_CLUEH (o)->indent); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | static gboolean |
---|
62 | calc_size (HTMLObject *clue, HTMLPainter *painter, GList **changed_objs) |
---|
63 | { |
---|
64 | HTMLObject *obj; |
---|
65 | gint lmargin = 0; |
---|
66 | gint a = 0, d = 0; |
---|
67 | gboolean changed; |
---|
68 | |
---|
69 | changed = FALSE; |
---|
70 | |
---|
71 | /* Make sure the children are properly sized */ |
---|
72 | html_object_set_max_width (clue, painter, clue->max_width); |
---|
73 | |
---|
74 | changed = HTML_OBJECT_CLASS (&html_clue_class)->calc_size (clue, painter, changed_objs); |
---|
75 | |
---|
76 | if (clue->parent != NULL) |
---|
77 | lmargin = html_object_get_left_margin (clue->parent, painter, clue->y, TRUE); |
---|
78 | |
---|
79 | clue->width = lmargin + HTML_CLUEH (clue)->indent; |
---|
80 | clue->descent = 0; |
---|
81 | clue->ascent = 0; |
---|
82 | |
---|
83 | for (obj = HTML_CLUE (clue)->head; obj != 0; obj = obj->next) { |
---|
84 | html_object_fit_line (obj, |
---|
85 | painter, |
---|
86 | (obj == HTML_CLUE (clue)->head), |
---|
87 | TRUE, FALSE, -1); |
---|
88 | obj->x = clue->width; |
---|
89 | clue->width += obj->width; |
---|
90 | if (obj->ascent > a) |
---|
91 | a = obj->ascent; |
---|
92 | if (obj->descent > d) |
---|
93 | d = obj->descent; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | switch (HTML_CLUE (clue)->valign) { |
---|
98 | case HTML_VALIGN_TOP: |
---|
99 | clue->ascent = a + d; |
---|
100 | for (obj = HTML_CLUE (clue)->head; obj != 0; obj = obj->next) { |
---|
101 | if (obj->y != obj->ascent) { |
---|
102 | obj->y = obj->ascent; |
---|
103 | changed = TRUE; |
---|
104 | } |
---|
105 | } |
---|
106 | break; |
---|
107 | |
---|
108 | case HTML_VALIGN_MIDDLE: |
---|
109 | clue->ascent = a + d; |
---|
110 | for (obj = HTML_CLUE (clue)->head; obj != 0; obj = obj->next) { |
---|
111 | if (obj->y != clue->ascent / 2) { |
---|
112 | obj->y = clue->ascent / 2; |
---|
113 | changed = TRUE; |
---|
114 | } |
---|
115 | } |
---|
116 | break; |
---|
117 | |
---|
118 | default: |
---|
119 | clue->ascent = a + d; |
---|
120 | for (obj = HTML_CLUE (clue)->head; obj != 0; obj = obj->next) { |
---|
121 | if (obj->y != clue->ascent - d) { |
---|
122 | obj->y = clue->ascent - d; |
---|
123 | changed = TRUE; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | return changed; |
---|
129 | } |
---|
130 | |
---|
131 | static gint |
---|
132 | calc_min_width (HTMLObject *o, |
---|
133 | HTMLPainter *painter) |
---|
134 | { |
---|
135 | HTMLObject *obj; |
---|
136 | gint minWidth = 0; |
---|
137 | |
---|
138 | for (obj = HTML_CLUE (o)->head; obj != 0; obj = obj->next) |
---|
139 | minWidth += html_object_calc_min_width (obj, painter); |
---|
140 | |
---|
141 | return minWidth + HTML_CLUEH (o)->indent; |
---|
142 | } |
---|
143 | |
---|
144 | static gint |
---|
145 | calc_preferred_width (HTMLObject *o, |
---|
146 | HTMLPainter *painter) |
---|
147 | { |
---|
148 | HTMLObject *obj; |
---|
149 | gint prefWidth = 0; |
---|
150 | |
---|
151 | for (obj = HTML_CLUE (o)->head; obj != 0; obj = obj->next) |
---|
152 | prefWidth += html_object_calc_preferred_width (obj, painter); |
---|
153 | |
---|
154 | return prefWidth + HTML_CLUEH (o)->indent; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | void |
---|
159 | html_clueh_type_init (void) |
---|
160 | { |
---|
161 | html_clueh_class_init (&html_clueh_class, HTML_TYPE_CLUEH, sizeof (HTMLClueH)); |
---|
162 | } |
---|
163 | |
---|
164 | void |
---|
165 | html_clueh_class_init (HTMLClueHClass *klass, |
---|
166 | HTMLType type, |
---|
167 | guint size) |
---|
168 | { |
---|
169 | HTMLClueClass *clue_class; |
---|
170 | HTMLObjectClass *object_class; |
---|
171 | |
---|
172 | clue_class = HTML_CLUE_CLASS (klass); |
---|
173 | object_class = HTML_OBJECT_CLASS (klass); |
---|
174 | |
---|
175 | html_clue_class_init (clue_class, type, size); |
---|
176 | |
---|
177 | object_class->copy = copy; |
---|
178 | object_class->set_max_width = set_max_width; |
---|
179 | object_class->calc_size = calc_size; |
---|
180 | object_class->calc_min_width = calc_min_width; |
---|
181 | object_class->calc_preferred_width = calc_preferred_width; |
---|
182 | |
---|
183 | parent_class = &html_clue_class; |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | html_clueh_init (HTMLClueH *clueh, HTMLClueHClass *klass, |
---|
188 | gint x, gint y, gint max_width) |
---|
189 | { |
---|
190 | HTMLObject *object; |
---|
191 | HTMLClue *clue; |
---|
192 | |
---|
193 | clue = HTML_CLUE (clueh); |
---|
194 | object = HTML_OBJECT (clueh); |
---|
195 | |
---|
196 | html_clue_init (clue, HTML_CLUE_CLASS (klass)); |
---|
197 | |
---|
198 | clue->valign = HTML_VALIGN_BOTTOM; |
---|
199 | clue->halign = HTML_HALIGN_LEFT; |
---|
200 | clue->head = clue->tail = clue->curr = 0; |
---|
201 | |
---|
202 | object->x = x; |
---|
203 | object->y = y; |
---|
204 | object->max_width = max_width; |
---|
205 | object->percent = 100; |
---|
206 | object->width = object->max_width; |
---|
207 | object->flags &= ~ HTML_OBJECT_FLAG_FIXEDWIDTH; |
---|
208 | } |
---|
209 | |
---|
210 | HTMLObject * |
---|
211 | html_clueh_new (gint x, gint y, gint max_width) |
---|
212 | { |
---|
213 | HTMLClueH *clueh; |
---|
214 | |
---|
215 | clueh = g_new0 (HTMLClueH, 1); |
---|
216 | html_clueh_init (clueh, &html_clueh_class, x, y, max_width); |
---|
217 | |
---|
218 | return HTML_OBJECT (clueh); |
---|
219 | } |
---|