| 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 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 226 |
| 227 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); | 227 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); |
| 228 LayoutSize windowSize = LayoutSize(view->width(), view->height()); | 228 LayoutSize windowSize = LayoutSize(view->width(), view->height()); |
| 229 | 229 |
| 230 float widthScale = windowSize.width().toFloat() / imageSize.width().toFloat(
); | 230 float widthScale = windowSize.width().toFloat() / imageSize.width().toFloat(
); |
| 231 float heightScale = windowSize.height().toFloat() / imageSize.height().toFlo
at(); | 231 float heightScale = windowSize.height().toFloat() / imageSize.height().toFlo
at(); |
| 232 | 232 |
| 233 return min(widthScale, heightScale); | 233 return min(widthScale, heightScale); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void ImageDocument::resizeImageToFit() | 236 void ImageDocument::resizeImageToFit(ScaleType type) |
| 237 { | 237 { |
| 238 if (!m_imageElement || m_imageElement->document() != this || pageZoomFactor(
this) > 1) | 238 if (!m_imageElement || m_imageElement->document() != this || (pageZoomFactor
(this) > 1 && type == ScaleOnlyUnzoomedDocument)) |
| 239 return; | 239 return; |
| 240 | 240 |
| 241 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); | 241 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); |
| 242 | 242 |
| 243 float scale = this->scale(); | 243 float scale = this->scale(); |
| 244 m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale)); | 244 m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale)); |
| 245 m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale)); | 245 m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale)); |
| 246 | 246 |
| 247 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZoomIn); | 247 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZoomIn); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void ImageDocument::imageClicked(int x, int y) | 250 void ImageDocument::imageClicked(int x, int y) |
| 251 { | 251 { |
| 252 if (!m_imageSizeIsKnown || imageFitsInWindow()) | 252 if (!m_imageSizeIsKnown || imageFitsInWindow()) |
| 253 return; | 253 return; |
| 254 | 254 |
| 255 m_shouldShrinkImage = !m_shouldShrinkImage; | 255 m_shouldShrinkImage = !m_shouldShrinkImage; |
| 256 | 256 |
| 257 if (m_shouldShrinkImage) | 257 if (m_shouldShrinkImage) |
| 258 windowSizeChanged(); | 258 windowSizeChanged(ScaleZoomedDocument); |
| 259 else { | 259 else { |
| 260 restoreImageSize(); | 260 restoreImageSize(ScaleZoomedDocument); |
| 261 | 261 |
| 262 updateLayout(); | 262 updateLayout(); |
| 263 | 263 |
| 264 float scale = this->scale(); | 264 float scale = this->scale(); |
| 265 | 265 |
| 266 int scrollX = static_cast<int>(x / scale - (float)frame()->view()->width
() / 2); | 266 int scrollX = static_cast<int>(x / scale - (float)frame()->view()->width
() / 2); |
| 267 int scrollY = static_cast<int>(y / scale - (float)frame()->view()->heigh
t() / 2); | 267 int scrollY = static_cast<int>(y / scale - (float)frame()->view()->heigh
t() / 2); |
| 268 | 268 |
| 269 frame()->view()->setScrollPosition(IntPoint(scrollX, scrollY)); | 269 frame()->view()->setScrollPosition(IntPoint(scrollX, scrollY)); |
| 270 } | 270 } |
| 271 } | 271 } |
| 272 | 272 |
| 273 void ImageDocument::imageUpdated() | 273 void ImageDocument::imageUpdated() |
| 274 { | 274 { |
| 275 ASSERT(m_imageElement); | 275 ASSERT(m_imageElement); |
| 276 | 276 |
| 277 if (m_imageSizeIsKnown) | 277 if (m_imageSizeIsKnown) |
| 278 return; | 278 return; |
| 279 | 279 |
| 280 if (m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->rend
erer(), pageZoomFactor(this)).isEmpty()) | 280 if (m_imageElement->cachedImage()->imageSizeForRenderer(m_imageElement->rend
erer(), pageZoomFactor(this)).isEmpty()) |
| 281 return; | 281 return; |
| 282 | 282 |
| 283 m_imageSizeIsKnown = true; | 283 m_imageSizeIsKnown = true; |
| 284 | 284 |
| 285 if (shouldShrinkToFit()) { | 285 if (shouldShrinkToFit()) { |
| 286 // Force resizing of the image | 286 // Force resizing of the image |
| 287 windowSizeChanged(); | 287 windowSizeChanged(ScaleOnlyUnzoomedDocument); |
| 288 } | 288 } |
| 289 } | 289 } |
| 290 | 290 |
| 291 void ImageDocument::restoreImageSize() | 291 void ImageDocument::restoreImageSize(ScaleType type) |
| 292 { | 292 { |
| 293 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this || pageZoomFactor(this) < 1) | 293 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this || (pageZoomFactor(this) < 1 && type == ScaleOnlyUnzoomedDocument)) |
| 294 return; | 294 return; |
| 295 | 295 |
| 296 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), 1.0f); | 296 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), 1.0f); |
| 297 m_imageElement->setWidth(imageSize.width()); | 297 m_imageElement->setWidth(imageSize.width()); |
| 298 m_imageElement->setHeight(imageSize.height()); | 298 m_imageElement->setHeight(imageSize.height()); |
| 299 | 299 |
| 300 if (imageFitsInWindow()) | 300 if (imageFitsInWindow()) |
| 301 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); | 301 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); |
| 302 else | 302 else |
| 303 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZoomOu
t); | 303 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZoomOu
t); |
| 304 | 304 |
| 305 m_didShrinkImage = false; | 305 m_didShrinkImage = false; |
| 306 } | 306 } |
| 307 | 307 |
| 308 bool ImageDocument::imageFitsInWindow() const | 308 bool ImageDocument::imageFitsInWindow() const |
| 309 { | 309 { |
| 310 if (!m_imageElement || m_imageElement->document() != this) | 310 if (!m_imageElement || m_imageElement->document() != this) |
| 311 return true; | 311 return true; |
| 312 | 312 |
| 313 FrameView* view = frame()->view(); | 313 FrameView* view = frame()->view(); |
| 314 if (!view) | 314 if (!view) |
| 315 return true; | 315 return true; |
| 316 | 316 |
| 317 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); | 317 LayoutSize imageSize = m_imageElement->cachedImage()->imageSizeForRenderer(m
_imageElement->renderer(), pageZoomFactor(this)); |
| 318 LayoutSize windowSize = LayoutSize(view->width(), view->height()); | 318 LayoutSize windowSize = LayoutSize(view->width(), view->height()); |
| 319 | 319 |
| 320 return imageSize.width() <= windowSize.width() && imageSize.height() <= wind
owSize.height(); | 320 return imageSize.width() <= windowSize.width() && imageSize.height() <= wind
owSize.height(); |
| 321 } | 321 } |
| 322 | 322 |
| 323 void ImageDocument::windowSizeChanged() | 323 void ImageDocument::windowSizeChanged(ScaleType type) |
| 324 { | 324 { |
| 325 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this) | 325 if (!m_imageElement || !m_imageSizeIsKnown || m_imageElement->document() !=
this) |
| 326 return; | 326 return; |
| 327 | 327 |
| 328 bool fitsInWindow = imageFitsInWindow(); | 328 bool fitsInWindow = imageFitsInWindow(); |
| 329 | 329 |
| 330 // If the image has been explicitly zoomed in, restore the cursor if the ima
ge fits | 330 // If the image has been explicitly zoomed in, restore the cursor if the ima
ge fits |
| 331 // and set it to a zoom out cursor if the image doesn't fit | 331 // and set it to a zoom out cursor if the image doesn't fit |
| 332 if (!m_shouldShrinkImage) { | 332 if (!m_shouldShrinkImage) { |
| 333 if (fitsInWindow) | 333 if (fitsInWindow) |
| 334 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); | 334 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); |
| 335 else | 335 else |
| 336 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZo
omOut); | 336 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZo
omOut); |
| 337 return; | 337 return; |
| 338 } | 338 } |
| 339 | 339 |
| 340 if (m_didShrinkImage) { | 340 if (m_didShrinkImage) { |
| 341 // If the window has been resized so that the image fits, restore the im
age size | 341 // If the window has been resized so that the image fits, restore the im
age size |
| 342 // otherwise update the restored image size. | 342 // otherwise update the restored image size. |
| 343 if (fitsInWindow) | 343 if (fitsInWindow) |
| 344 restoreImageSize(); | 344 restoreImageSize(type); |
| 345 else | 345 else |
| 346 resizeImageToFit(); | 346 resizeImageToFit(type); |
| 347 } else { | 347 } else { |
| 348 // If the image isn't resized but needs to be, then resize it. | 348 // If the image isn't resized but needs to be, then resize it. |
| 349 if (!fitsInWindow) { | 349 if (!fitsInWindow) { |
| 350 resizeImageToFit(); | 350 resizeImageToFit(type); |
| 351 m_didShrinkImage = true; | 351 m_didShrinkImage = true; |
| 352 } | 352 } |
| 353 } | 353 } |
| 354 } | 354 } |
| 355 | 355 |
| 356 ImageResource* ImageDocument::cachedImage() | 356 ImageResource* ImageDocument::cachedImage() |
| 357 { | 357 { |
| 358 if (!m_imageElement) | 358 if (!m_imageElement) |
| 359 createDocumentStructure(); | 359 createDocumentStructure(); |
| 360 | 360 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 378 { | 378 { |
| 379 visitor->trace(m_imageElement); | 379 visitor->trace(m_imageElement); |
| 380 HTMLDocument::trace(visitor); | 380 HTMLDocument::trace(visitor); |
| 381 } | 381 } |
| 382 | 382 |
| 383 // -------- | 383 // -------- |
| 384 | 384 |
| 385 void ImageEventListener::handleEvent(ExecutionContext*, Event* event) | 385 void ImageEventListener::handleEvent(ExecutionContext*, Event* event) |
| 386 { | 386 { |
| 387 if (event->type() == EventTypeNames::resize) | 387 if (event->type() == EventTypeNames::resize) |
| 388 m_doc->windowSizeChanged(); | 388 m_doc->windowSizeChanged(ImageDocument::ScaleOnlyUnzoomedDocument); |
| 389 else if (event->type() == EventTypeNames::click && event->isMouseEvent()) { | 389 else if (event->type() == EventTypeNames::click && event->isMouseEvent()) { |
| 390 MouseEvent* mouseEvent = toMouseEvent(event); | 390 MouseEvent* mouseEvent = toMouseEvent(event); |
| 391 m_doc->imageClicked(mouseEvent->x(), mouseEvent->y()); | 391 m_doc->imageClicked(mouseEvent->x(), mouseEvent->y()); |
| 392 } | 392 } |
| 393 } | 393 } |
| 394 | 394 |
| 395 bool ImageEventListener::operator==(const EventListener& listener) | 395 bool ImageEventListener::operator==(const EventListener& listener) |
| 396 { | 396 { |
| 397 if (const ImageEventListener* imageEventListener = ImageEventListener::cast(
&listener)) | 397 if (const ImageEventListener* imageEventListener = ImageEventListener::cast(
&listener)) |
| 398 return m_doc == imageEventListener->m_doc; | 398 return m_doc == imageEventListener->m_doc; |
| 399 return false; | 399 return false; |
| 400 } | 400 } |
| 401 | 401 |
| 402 } | 402 } |
| OLD | NEW |