OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights | 3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights |
4 * reserved. | 4 * reserved. |
5 * | 5 * |
6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
10 * | 10 * |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 exploring_backwards = !next && (next != stop); | 123 exploring_backwards = !next && (next != stop); |
124 if (exploring_backwards) { | 124 if (exploring_backwards) { |
125 next = stop->PreviousInPreOrder(); | 125 next = stop->PreviousInPreOrder(); |
126 continue_exploring = next && !next->IsLayoutView(); | 126 continue_exploring = next && !next->IsLayoutView(); |
127 } | 127 } |
128 } | 128 } |
129 | 129 |
130 return next; | 130 return next; |
131 } | 131 } |
132 | 132 |
133 void LayoutSelection::SetSelection( | 133 // Objects each have a single selection rect to examine. |
134 LayoutObject* start, | 134 typedef HashMap<LayoutObject*, SelectionState> SelectedObjectMap; |
135 int start_pos, | 135 // Blocks contain selected objects and fill gaps between them, either on the |
136 LayoutObject* end, | 136 // left, right, or in between lines and blocks. |
137 int end_pos, | 137 // In order to get the visual rect right, we have to examine left, middle, and |
138 SelectionPaintInvalidationMode block_paint_invalidation_mode) { | 138 // right rects individually, since otherwise the union of those rects might |
139 // This code makes no assumptions as to if the layout tree is up to date or | 139 // remain the same even when changes have occurred. |
140 // not and will not try to update it. Currently clearSelection calls this | 140 typedef HashMap<LayoutBlock*, SelectionState> SelectedBlockMap; |
141 // (intentionally) without updating the layout tree as it doesn't care. | |
142 // Other callers may want to force recalc style before calling this. | |
143 | 141 |
144 // Make sure both our start and end objects are defined. | 142 static std::tuple<SelectedObjectMap, SelectedBlockMap> CollectSelectedMap( |
145 // Check www.msnbc.com and try clicking around to find the case where this | 143 LayoutObject* const selection_start, |
146 // happened. | 144 LayoutObject* const selection_end, |
147 if ((start && !end) || (end && !start)) | 145 int selection_end_pos, |
148 return; | 146 LayoutSelection::SelectionPaintInvalidationMode |
149 | 147 block_paint_invalidation_mode = |
150 // Just return if the selection hasn't changed. | 148 LayoutSelection::kPaintInvalidationNewXOROld) { |
151 if (selection_start_ == start && selection_start_pos_ == start_pos && | 149 SelectedObjectMap selected_objects; |
152 selection_end_ == end && selection_end_pos_ == end_pos) | 150 SelectedBlockMap selected_blocks; |
153 return; | 151 LayoutObject* layout_object = selection_start; |
154 | 152 LayoutObject* const stop = |
155 // Record the old selected objects. These will be used later when we compare | 153 LayoutObjectAfterPosition(selection_end, selection_end_pos); |
156 // against the new selected objects. | |
157 int old_start_pos = selection_start_pos_; | |
158 int old_end_pos = selection_end_pos_; | |
159 | |
160 // Objects each have a single selection rect to examine. | |
161 typedef HashMap<LayoutObject*, SelectionState> SelectedObjectMap; | |
162 SelectedObjectMap old_selected_objects; | |
163 // FIXME: |newSelectedObjects| doesn't really need to store the | |
164 // SelectionState, it's just more convenient to have it use the same data | |
165 // structure as |oldSelectedObjects|. | |
166 SelectedObjectMap new_selected_objects; | |
167 | |
168 // Blocks contain selected objects and fill gaps between them, either on the | |
169 // left, right, or in between lines and blocks. | |
170 // In order to get the visual rect right, we have to examine left, middle, and | |
171 // right rects individually, since otherwise the union of those rects might | |
172 // remain the same even when changes have occurred. | |
173 typedef HashMap<LayoutBlock*, SelectionState> SelectedBlockMap; | |
174 SelectedBlockMap old_selected_blocks; | |
175 // FIXME: |newSelectedBlocks| doesn't really need to store the SelectionState, | |
176 // it's just more convenient to have it use the same data structure as | |
177 // |oldSelectedBlocks|. | |
178 SelectedBlockMap new_selected_blocks; | |
179 | |
180 LayoutObject* os = selection_start_; | |
181 LayoutObject* stop = | |
182 LayoutObjectAfterPosition(selection_end_, selection_end_pos_); | |
183 bool exploring_backwards = false; | 154 bool exploring_backwards = false; |
184 bool continue_exploring = os && (os != stop); | 155 bool continue_exploring = layout_object && (layout_object != stop); |
185 while (continue_exploring) { | 156 while (continue_exploring) { |
186 if ((os->CanBeSelectionLeaf() || os == selection_start_ || | 157 if ((layout_object->CanBeSelectionLeaf() || |
187 os == selection_end_) && | 158 layout_object == selection_start || layout_object == selection_end) && |
188 os->GetSelectionState() != SelectionNone) { | 159 layout_object->GetSelectionState() != SelectionNone) { |
189 // Blocks are responsible for painting line gaps and margin gaps. They | 160 // Blocks are responsible for painting line gaps and margin gaps. They |
190 // must be examined as well. | 161 // must be examined as well. |
191 old_selected_objects.Set(os, os->GetSelectionState()); | 162 selected_objects.Set(layout_object, layout_object->GetSelectionState()); |
192 if (block_paint_invalidation_mode == kPaintInvalidationNewXOROld) { | 163 if (block_paint_invalidation_mode == |
193 LayoutBlock* cb = os->ContainingBlock(); | 164 LayoutSelection::kPaintInvalidationNewXOROld) { |
165 LayoutBlock* cb = layout_object->ContainingBlock(); | |
194 while (cb && !cb->IsLayoutView()) { | 166 while (cb && !cb->IsLayoutView()) { |
195 SelectedBlockMap::AddResult result = | 167 SelectedBlockMap::AddResult result = |
196 old_selected_blocks.insert(cb, cb->GetSelectionState()); | 168 selected_blocks.insert(cb, cb->GetSelectionState()); |
197 if (!result.is_new_entry) | 169 if (!result.is_new_entry) |
198 break; | 170 break; |
199 cb = cb->ContainingBlock(); | 171 cb = cb->ContainingBlock(); |
200 } | 172 } |
201 } | 173 } |
202 } | 174 } |
203 | 175 |
204 os = GetNextOrPrevLayoutObjectBasedOnDirection(os, stop, continue_exploring, | 176 layout_object = GetNextOrPrevLayoutObjectBasedOnDirection( |
205 exploring_backwards); | 177 layout_object, stop, continue_exploring, exploring_backwards); |
206 } | 178 } |
179 return std::forward_as_tuple(selected_objects, selected_blocks); | |
180 } | |
207 | 181 |
208 // Now clear the selection. | 182 static void SetSelectionNone(SelectedObjectMap map) { |
209 SelectedObjectMap::iterator old_objects_end = old_selected_objects.end(); | 183 for (auto iterator : map) |
yosin_UTC9
2017/04/27 09:50:29
nit: s/auto/const auto&/
nit: s/iterator/key_value
yoichio
2017/04/28 02:16:25
Done.
| |
210 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); | 184 iterator.key->SetSelectionStateIfNeeded(SelectionNone); |
211 i != old_objects_end; ++i) | 185 } |
212 i->key->SetSelectionStateIfNeeded(SelectionNone); | |
213 | 186 |
214 // set selection start and end | 187 static void MarkSelection(LayoutObject* start, LayoutObject* end, int end_pos) { |
215 selection_start_ = start; | |
216 selection_start_pos_ = start_pos; | |
217 selection_end_ = end; | |
218 selection_end_pos_ = end_pos; | |
219 | |
220 // Update the selection status of all objects between m_selectionStart and | 188 // Update the selection status of all objects between m_selectionStart and |
221 // m_selectionEnd | 189 // m_selectionEnd |
222 if (start && start == end) { | 190 if (start && start == end) { |
223 start->SetSelectionStateIfNeeded(SelectionBoth); | 191 start->SetSelectionStateIfNeeded(SelectionBoth); |
224 } else { | 192 } else { |
225 if (start) | 193 if (start) |
226 start->SetSelectionStateIfNeeded(SelectionStart); | 194 start->SetSelectionStateIfNeeded(SelectionStart); |
227 if (end) | 195 if (end) |
228 end->SetSelectionStateIfNeeded(SelectionEnd); | 196 end->SetSelectionStateIfNeeded(SelectionEnd); |
229 } | 197 } |
230 | 198 |
231 LayoutObject* o = start; | 199 LayoutObject* layout_object = start; |
232 stop = LayoutObjectAfterPosition(end, end_pos); | 200 LayoutObject* const stop = LayoutObjectAfterPosition(end, end_pos); |
201 while (layout_object && layout_object != stop) { | |
202 if (layout_object != start && layout_object != end && | |
203 layout_object->CanBeSelectionLeaf()) | |
204 layout_object->SetSelectionStateIfNeeded(SelectionInside); | |
205 layout_object = layout_object->NextInPreOrder(); | |
206 } | |
207 } | |
233 | 208 |
234 while (o && o != stop) { | 209 static void InvalidateLayoutObjects(LayoutObject* start, |
235 if (o != start && o != end && o->CanBeSelectionLeaf()) | 210 bool is_start_pos_changed, |
236 o->SetSelectionStateIfNeeded(SelectionInside); | 211 LayoutObject* end, |
237 o = o->NextInPreOrder(); | 212 bool is_end_pos_changed, |
238 } | 213 SelectedObjectMap& old_selected_objects, |
239 | 214 SelectedBlockMap& old_selected_blocks, |
240 // Now that the selection state has been updated for the new objects, walk | 215 SelectedObjectMap& new_selected_objects, |
241 // them again and put them in the new objects list. | 216 SelectedBlockMap& new_selected_blocks) { |
242 o = start; | |
243 exploring_backwards = false; | |
244 continue_exploring = o && (o != stop); | |
245 while (continue_exploring) { | |
246 if ((o->CanBeSelectionLeaf() || o == start || o == end) && | |
247 o->GetSelectionState() != SelectionNone) { | |
248 new_selected_objects.Set(o, o->GetSelectionState()); | |
249 LayoutBlock* cb = o->ContainingBlock(); | |
250 while (cb && !cb->IsLayoutView()) { | |
251 SelectedBlockMap::AddResult result = | |
252 new_selected_blocks.insert(cb, cb->GetSelectionState()); | |
253 if (!result.is_new_entry) | |
254 break; | |
255 cb = cb->ContainingBlock(); | |
256 } | |
257 } | |
258 | |
259 o = GetNextOrPrevLayoutObjectBasedOnDirection(o, stop, continue_exploring, | |
260 exploring_backwards); | |
261 } | |
262 | |
263 // TODO(yoichio): DCHECK(frame_selection_->,,,->GetFrameView()); | |
264 if (!frame_selection_->GetDocument().GetLayoutView()->GetFrameView()) | |
265 return; | |
266 | |
267 // Have any of the old selected objects changed compared to the new selection? | 217 // Have any of the old selected objects changed compared to the new selection? |
268 for (SelectedObjectMap::iterator i = old_selected_objects.begin(); | 218 for (auto iterator : old_selected_objects) { |
269 i != old_objects_end; ++i) { | 219 LayoutObject* obj = iterator.key; |
270 LayoutObject* obj = i->key; | |
271 SelectionState new_selection_state = obj->GetSelectionState(); | 220 SelectionState new_selection_state = obj->GetSelectionState(); |
272 SelectionState old_selection_state = i->value; | 221 SelectionState old_selection_state = iterator.value; |
273 if (new_selection_state != old_selection_state || | 222 if (new_selection_state != old_selection_state || |
274 (selection_start_ == obj && old_start_pos != selection_start_pos_) || | 223 (start == obj && is_start_pos_changed) || |
275 (selection_end_ == obj && old_end_pos != selection_end_pos_)) { | 224 (end == obj && is_end_pos_changed)) { |
276 obj->SetShouldInvalidateSelection(); | 225 obj->SetShouldInvalidateSelection(); |
277 new_selected_objects.erase(obj); | 226 new_selected_objects.erase(obj); |
278 } | 227 } |
279 } | 228 } |
280 | 229 |
281 // Any new objects that remain were not found in the old objects dict, and so | 230 // Any new objects that remain were not found in the old objects dict, and so |
282 // they need to be updated. | 231 // they need to be updated. |
283 SelectedObjectMap::iterator new_objects_end = new_selected_objects.end(); | 232 for (auto iterator : new_selected_objects) |
yosin_UTC9
2017/04/27 09:50:29
nit: s/auto/const auto&/
nit: s/iterator/key_value
yoichio
2017/04/28 02:16:25
Done.
| |
284 for (SelectedObjectMap::iterator i = new_selected_objects.begin(); | 233 iterator.key->SetShouldInvalidateSelection(); |
285 i != new_objects_end; ++i) | |
286 i->key->SetShouldInvalidateSelection(); | |
287 | 234 |
288 // Have any of the old blocks changed? | 235 // Have any of the old blocks changed? |
289 SelectedBlockMap::iterator old_blocks_end = old_selected_blocks.end(); | 236 for (auto iterator : old_selected_blocks) { |
yosin_UTC9
2017/04/27 09:50:29
nit: s/auto/const auto&/
nit: s/iterator/key_value
yoichio
2017/04/28 02:16:25
Done.
| |
290 for (SelectedBlockMap::iterator i = old_selected_blocks.begin(); | 237 LayoutBlock* block = iterator.key; |
291 i != old_blocks_end; ++i) { | |
292 LayoutBlock* block = i->key; | |
293 SelectionState new_selection_state = block->GetSelectionState(); | 238 SelectionState new_selection_state = block->GetSelectionState(); |
294 SelectionState old_selection_state = i->value; | 239 SelectionState old_selection_state = iterator.value; |
295 if (new_selection_state != old_selection_state) { | 240 if (new_selection_state != old_selection_state) { |
296 block->SetShouldInvalidateSelection(); | 241 block->SetShouldInvalidateSelection(); |
297 new_selected_blocks.erase(block); | 242 new_selected_blocks.erase(block); |
298 } | 243 } |
299 } | 244 } |
300 | 245 |
301 // Any new blocks that remain were not found in the old blocks dict, and so | 246 // Any new blocks that remain were not found in the old blocks dict, and so |
302 // they need to be updated. | 247 // they need to be updated. |
303 SelectedBlockMap::iterator new_blocks_end = new_selected_blocks.end(); | 248 for (auto iterator : new_selected_blocks) |
304 for (SelectedBlockMap::iterator i = new_selected_blocks.begin(); | 249 iterator.key->SetShouldInvalidateSelection(); |
305 i != new_blocks_end; ++i) | 250 } |
306 i->key->SetShouldInvalidateSelection(); | 251 |
252 void LayoutSelection::SetSelection( | |
253 LayoutObject* start, | |
254 int start_pos, | |
255 LayoutObject* end, | |
256 int end_pos, | |
257 SelectionPaintInvalidationMode block_paint_invalidation_mode) { | |
258 // This code makes no assumptions as to if the layout tree is up to date or | |
259 // not and will not try to update it. Currently clearSelection calls this | |
260 // (intentionally) without updating the layout tree as it doesn't care. | |
261 // Other callers may want to force recalc style before calling this. | |
262 | |
263 // Make sure both our start and end objects are defined. | |
264 // Check www.msnbc.com and try clicking around to find the case where this | |
265 // happened. | |
266 if ((start && !end) || (end && !start)) | |
267 return; | |
268 | |
269 // Just return if the selection hasn't changed. | |
270 if (selection_start_ == start && selection_start_pos_ == start_pos && | |
271 selection_end_ == end && selection_end_pos_ == end_pos) | |
272 return; | |
273 | |
274 SelectedObjectMap old_selected_objects; | |
275 SelectedBlockMap old_selected_blocks; | |
276 std::tie(old_selected_objects, old_selected_blocks) = | |
277 CollectSelectedMap(selection_start_, selection_end_, selection_end_pos_, | |
278 block_paint_invalidation_mode); | |
279 SetSelectionNone(old_selected_objects); | |
280 | |
281 MarkSelection(start, end, end_pos); | |
282 | |
283 // Now that the selection state has been updated for the new objects, walk | |
284 // them again and put them in the new objects list. | |
285 // FIXME: |new_selected_objects| doesn't really need to store the | |
286 // SelectionState, it's just more convenient to have it use the same data | |
287 // structure as |old_selected_objects|. | |
288 SelectedObjectMap new_selected_objects; | |
289 // FIXME: |new_selected_blocks| doesn't really need to store the | |
290 // SelectionState, it's just more convenient to have it use the same data | |
291 // structure as |old_selected_blocks|. | |
292 SelectedBlockMap new_selected_blocks; | |
293 std::tie(new_selected_objects, new_selected_blocks) = | |
294 CollectSelectedMap(start, end, end_pos); | |
295 | |
296 // TODO(yoichio): DCHECK(frame_selection_->,,,->GetFrameView()); | |
297 if (!frame_selection_->GetDocument().GetLayoutView()->GetFrameView()) | |
yosin_UTC9
2017/04/27 09:50:29
We should move this if-statement to beginning of t
yoichio
2017/04/28 02:16:25
Done.
| |
298 return; | |
299 | |
300 // Record the old selected offsets. These will be used later when we compare | |
301 // against the new selected offsets. | |
302 const bool is_start_pos_changed = selection_start_pos_ != start_pos; | |
303 const bool is_end_pos_changed = selection_end_pos_ != end_pos; | |
304 // set selection start and end | |
305 selection_start_ = start; | |
306 selection_start_pos_ = start_pos; | |
307 selection_end_ = end; | |
308 selection_end_pos_ = end_pos; | |
309 InvalidateLayoutObjects(selection_start_, is_start_pos_changed, | |
310 selection_end_, is_end_pos_changed, | |
311 old_selected_objects, old_selected_blocks, | |
312 new_selected_objects, new_selected_blocks); | |
307 } | 313 } |
308 | 314 |
309 void LayoutSelection::SelectionStartEnd(int& start_pos, int& end_pos) { | 315 void LayoutSelection::SelectionStartEnd(int& start_pos, int& end_pos) { |
310 Commit(); | 316 Commit(); |
311 start_pos = selection_start_pos_; | 317 start_pos = selection_start_pos_; |
312 end_pos = selection_end_pos_; | 318 end_pos = selection_end_pos_; |
313 } | 319 } |
314 | 320 |
315 void LayoutSelection::ClearSelection() { | 321 void LayoutSelection::ClearSelection() { |
316 // For querying Layer::compositingState() | 322 // For querying Layer::compositingState() |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
447 | 453 |
448 o->SetShouldInvalidateSelection(); | 454 o->SetShouldInvalidateSelection(); |
449 } | 455 } |
450 } | 456 } |
451 | 457 |
452 DEFINE_TRACE(LayoutSelection) { | 458 DEFINE_TRACE(LayoutSelection) { |
453 visitor->Trace(frame_selection_); | 459 visitor->Trace(frame_selection_); |
454 } | 460 } |
455 | 461 |
456 } // namespace blink | 462 } // namespace blink |
OLD | NEW |