OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 225 |
226 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); | 226 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); |
227 LayoutSize windowSize = LayoutSize(view->width(), view->height()); | 227 LayoutSize windowSize = LayoutSize(view->width(), view->height()); |
228 | 228 |
229 float widthScale = windowSize.width().toFloat() / imageSize.width().toFloat(
); | 229 float widthScale = windowSize.width().toFloat() / imageSize.width().toFloat(
); |
230 float heightScale = windowSize.height().toFloat() / imageSize.height().toFlo
at(); | 230 float heightScale = windowSize.height().toFloat() / imageSize.height().toFlo
at(); |
231 | 231 |
232 return min(widthScale, heightScale); | 232 return min(widthScale, heightScale); |
233 } | 233 } |
234 | 234 |
235 void ImageDocument::resizeImageToFit() | 235 void ImageDocument::resizeImageToFit(bool resizeZoomedDocument) |
236 { | 236 { |
237 if (!m_imageElement || m_imageElement->document() != this || pageZoomFactor(
this) > 1) | 237 if (!m_imageElement || m_imageElement->document() != this || (pageZoomFactor
(this) > 1 && !resizeZoomedDocument)) |
238 return; | 238 return; |
239 | 239 |
240 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); | 240 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); |
241 | 241 |
242 float scale = this->scale(); | 242 float scale = this->scale(); |
243 m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale)); | 243 m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale)); |
244 m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale)); | 244 m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale)); |
245 | 245 |
246 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueWebkitZoom
In); | 246 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueWebkitZoom
In); |
247 } | 247 } |
248 | 248 |
249 void ImageDocument::imageClicked(int x, int y) | 249 void ImageDocument::imageClicked(int x, int y) |
250 { | 250 { |
251 if (!m_imageSizeIsKnown || imageFitsInWindow()) | 251 if (!m_imageSizeIsKnown || imageFitsInWindow()) |
252 return; | 252 return; |
253 | 253 |
254 m_shouldShrinkImage = !m_shouldShrinkImage; | 254 m_shouldShrinkImage = !m_shouldShrinkImage; |
255 | 255 |
256 if (m_shouldShrinkImage) | 256 if (m_shouldShrinkImage) |
257 windowSizeChanged(); | 257 windowSizeChanged(true); |
258 else { | 258 else { |
259 restoreImageSize(); | 259 restoreImageSize(true); |
260 | 260 |
261 updateLayout(); | 261 updateLayout(); |
262 | 262 |
263 float scale = this->scale(); | 263 float scale = this->scale(); |
264 | 264 |
265 int scrollX = static_cast<int>(x / scale - (float)frame()->view()->width
() / 2); | 265 int scrollX = static_cast<int>(x / scale - (float)frame()->view()->width
() / 2); |
266 int scrollY = static_cast<int>(y / scale - (float)frame()->view()->heigh
t() / 2); | 266 int scrollY = static_cast<int>(y / scale - (float)frame()->view()->heigh
t() / 2); |
267 | 267 |
268 frame()->view()->setScrollPosition(IntPoint(scrollX, scrollY)); | 268 frame()->view()->setScrollPosition(IntPoint(scrollX, scrollY)); |
269 } | 269 } |
270 } | 270 } |
271 | 271 |
272 void ImageDocument::imageUpdated() | 272 void ImageDocument::imageUpdated() |
273 { | 273 { |
274 ASSERT(m_imageElement); | 274 ASSERT(m_imageElement); |
275 | 275 |
276 if (m_imageSizeIsKnown) | 276 if (m_imageSizeIsKnown) |
277 return; | 277 return; |
278 | 278 |
279 if (m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->rend
erer(), pageZoomFactor(this)).isEmpty()) | 279 if (m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->rend
erer(), pageZoomFactor(this)).isEmpty()) |
280 return; | 280 return; |
281 | 281 |
282 m_imageSizeIsKnown = true; | 282 m_imageSizeIsKnown = true; |
283 | 283 |
284 if (shouldShrinkToFit()) { | 284 if (shouldShrinkToFit()) { |
285 // Force resizing of the image | 285 // Force resizing of the image |
286 windowSizeChanged(); | 286 windowSizeChanged(false); |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 void ImageDocument::restoreImageSize() | 290 void ImageDocument::restoreImageSize(bool restoreZoomedDocument) |
291 { | 291 { |
292 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this || pageZoomFactor(this) < 1) | 292 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this || (pageZoomFactor(this) < 1 && !restoreZoomedDocument)) |
293 return; | 293 return; |
294 | 294 |
295 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), 1.0f); | 295 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), 1.0f); |
296 m_imageElement->setWidth(imageSize.width()); | 296 m_imageElement->setWidth(imageSize.width()); |
297 m_imageElement->setHeight(imageSize.height()); | 297 m_imageElement->setHeight(imageSize.height()); |
298 | 298 |
299 if (imageFitsInWindow()) | 299 if (imageFitsInWindow()) |
300 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); | 300 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); |
301 else | 301 else |
302 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueWebkit
ZoomOut); | 302 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueWebkit
ZoomOut); |
303 | 303 |
304 m_didShrinkImage = false; | 304 m_didShrinkImage = false; |
305 } | 305 } |
306 | 306 |
307 bool ImageDocument::imageFitsInWindow() const | 307 bool ImageDocument::imageFitsInWindow() const |
308 { | 308 { |
309 if (!m_imageElement || m_imageElement->document() != this) | 309 if (!m_imageElement || m_imageElement->document() != this) |
310 return true; | 310 return true; |
311 | 311 |
312 FrameView* view = frame()->view(); | 312 FrameView* view = frame()->view(); |
313 if (!view) | 313 if (!view) |
314 return true; | 314 return true; |
315 | 315 |
316 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); | 316 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); |
317 LayoutSize windowSize = LayoutSize(view->width(), view->height()); | 317 LayoutSize windowSize = LayoutSize(view->width(), view->height()); |
318 | 318 |
319 return imageSize.width() <= windowSize.width() && imageSize.height() <= wind
owSize.height(); | 319 return imageSize.width() <= windowSize.width() && imageSize.height() <= wind
owSize.height(); |
320 } | 320 } |
321 | 321 |
322 void ImageDocument::windowSizeChanged() | 322 void ImageDocument::windowSizeChanged(bool resizeZoomedDocument) |
323 { | 323 { |
324 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this) | 324 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this) |
325 return; | 325 return; |
326 | 326 |
327 bool fitsInWindow = imageFitsInWindow(); | 327 bool fitsInWindow = imageFitsInWindow(); |
328 | 328 |
329 // If the image has been explicitly zoomed in, restore the cursor if the ima
ge fits | 329 // If the image has been explicitly zoomed in, restore the cursor if the ima
ge fits |
330 // and set it to a zoom out cursor if the image doesn't fit | 330 // and set it to a zoom out cursor if the image doesn't fit |
331 if (!m_shouldShrinkImage) { | 331 if (!m_shouldShrinkImage) { |
332 if (fitsInWindow) | 332 if (fitsInWindow) |
333 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); | 333 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); |
334 else | 334 else |
335 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueWe
bkitZoomOut); | 335 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueWe
bkitZoomOut); |
336 return; | 336 return; |
337 } | 337 } |
338 | 338 |
339 if (m_didShrinkImage) { | 339 if (m_didShrinkImage) { |
340 // If the window has been resized so that the image fits, restore the im
age size | 340 // If the window has been resized so that the image fits, restore the im
age size |
341 // otherwise update the restored image size. | 341 // otherwise update the restored image size. |
342 if (fitsInWindow) | 342 if (fitsInWindow) |
343 restoreImageSize(); | 343 restoreImageSize(resizeZoomedDocument); |
344 else | 344 else |
345 resizeImageToFit(); | 345 resizeImageToFit(resizeZoomedDocument); |
346 } else { | 346 } else { |
347 // If the image isn't resized but needs to be, then resize it. | 347 // If the image isn't resized but needs to be, then resize it. |
348 if (!fitsInWindow) { | 348 if (!fitsInWindow) { |
349 resizeImageToFit(); | 349 resizeImageToFit(resizeZoomedDocument); |
350 m_didShrinkImage = true; | 350 m_didShrinkImage = true; |
351 } | 351 } |
352 } | 352 } |
353 } | 353 } |
354 | 354 |
355 ImageResource* ImageDocument::cachedImage() | 355 ImageResource* ImageDocument::cachedImage() |
356 { | 356 { |
357 if (!m_imageElement) | 357 if (!m_imageElement) |
358 createDocumentStructure(); | 358 createDocumentStructure(); |
359 | 359 |
360 return m_imageElement->cachedImage(); | 360 return m_imageElement->cachedImage(); |
361 } | 361 } |
362 | 362 |
363 bool ImageDocument::shouldShrinkToFit() const | 363 bool ImageDocument::shouldShrinkToFit() const |
364 { | 364 { |
365 return frame()->settings()->shrinksStandaloneImagesToFit() && frame()->isMai
nFrame(); | 365 return frame()->settings()->shrinksStandaloneImagesToFit() && frame()->isMai
nFrame(); |
366 } | 366 } |
367 | 367 |
368 void ImageDocument::dispose() | 368 void ImageDocument::dispose() |
369 { | 369 { |
370 m_imageElement = nullptr; | 370 m_imageElement = nullptr; |
371 HTMLDocument::dispose(); | 371 HTMLDocument::dispose(); |
372 } | 372 } |
373 | 373 |
374 // -------- | 374 // -------- |
375 | 375 |
376 void ImageEventListener::handleEvent(ExecutionContext*, Event* event) | 376 void ImageEventListener::handleEvent(ExecutionContext*, Event* event) |
377 { | 377 { |
378 if (event->type() == EventTypeNames::resize) | 378 if (event->type() == EventTypeNames::resize) |
379 m_doc->windowSizeChanged(); | 379 m_doc->windowSizeChanged(false); |
380 else if (event->type() == EventTypeNames::click && event->isMouseEvent()) { | 380 else if (event->type() == EventTypeNames::click && event->isMouseEvent()) { |
381 MouseEvent* mouseEvent = toMouseEvent(event); | 381 MouseEvent* mouseEvent = toMouseEvent(event); |
382 m_doc->imageClicked(mouseEvent->x(), mouseEvent->y()); | 382 m_doc->imageClicked(mouseEvent->x(), mouseEvent->y()); |
383 } | 383 } |
384 } | 384 } |
385 | 385 |
386 bool ImageEventListener::operator==(const EventListener& listener) | 386 bool ImageEventListener::operator==(const EventListener& listener) |
387 { | 387 { |
388 if (const ImageEventListener* imageEventListener = ImageEventListener::cast(
&listener)) | 388 if (const ImageEventListener* imageEventListener = ImageEventListener::cast(
&listener)) |
389 return m_doc == imageEventListener->m_doc; | 389 return m_doc == imageEventListener->m_doc; |
390 return false; | 390 return false; |
391 } | 391 } |
392 | 392 |
393 } | 393 } |
OLD | NEW |