| 1 | // Aseprite UI Library |
| 2 | // Copyright (C) 2020 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2016 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
| 12 | #include "ui/image_view.h" |
| 13 | |
| 14 | #include "os/surface.h" |
| 15 | #include "ui/graphics.h" |
| 16 | #include "ui/message.h" |
| 17 | #include "ui/paint_event.h" |
| 18 | #include "ui/size_hint_event.h" |
| 19 | #include "ui/system.h" |
| 20 | #include "ui/theme.h" |
| 21 | |
| 22 | namespace ui { |
| 23 | |
| 24 | ImageView::ImageView(const os::SurfaceRef& sur, int align) |
| 25 | : Widget(kImageViewWidget) |
| 26 | , m_sur(sur) |
| 27 | { |
| 28 | setAlign(align); |
| 29 | } |
| 30 | |
| 31 | void ImageView::onSizeHint(SizeHintEvent& ev) |
| 32 | { |
| 33 | gfx::Rect box; |
| 34 | getTextIconInfo(&box, nullptr, nullptr, |
| 35 | align(), m_sur->width(), m_sur->height()); |
| 36 | |
| 37 | ev.setSizeHint( |
| 38 | gfx::Size( |
| 39 | box.w + border().width(), |
| 40 | box.h + border().height())); |
| 41 | } |
| 42 | |
| 43 | void ImageView::onPaint(PaintEvent& ev) |
| 44 | { |
| 45 | Graphics* g = ev.graphics(); |
| 46 | gfx::Rect bounds = clientBounds(); |
| 47 | gfx::Rect icon; |
| 48 | getTextIconInfo( |
| 49 | nullptr, nullptr, &icon, align(), |
| 50 | m_sur->width(), m_sur->height()); |
| 51 | |
| 52 | g->fillRect(bgColor(), bounds); |
| 53 | g->drawRgbaSurface(m_sur.get(), icon.x, icon.y); |
| 54 | } |
| 55 | |
| 56 | } // namespace ui |
| 57 | |