main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2020, Nico Weber <thakis@chromium.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Application.h>
  27. #include <LibGUI/Icon.h>
  28. #include <LibGUI/Painter.h>
  29. #include <LibGUI/Widget.h>
  30. #include <LibGUI/Window.h>
  31. #include <LibGfx/Bitmap.h>
  32. #include <LibGfx/Font.h>
  33. #include <LibGfx/FontDatabase.h>
  34. #include <LibGfx/Painter.h>
  35. #include <LibGfx/Palette.h>
  36. #include <LibGfx/Path.h>
  37. #include <LibGfx/WindowTheme.h>
  38. const int WIDTH = 300;
  39. const int HEIGHT = 200;
  40. class Canvas final : public GUI::Widget {
  41. C_OBJECT(Canvas)
  42. public:
  43. virtual ~Canvas() override;
  44. private:
  45. Canvas();
  46. RefPtr<Gfx::Bitmap> m_bitmap_1x;
  47. RefPtr<Gfx::Bitmap> m_bitmap_2x;
  48. void draw(Gfx::Painter& painter);
  49. virtual void paint_event(GUI::PaintEvent&) override;
  50. };
  51. Canvas::Canvas()
  52. {
  53. m_bitmap_1x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH, HEIGHT });
  54. m_bitmap_2x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH * 2, HEIGHT * 2 });
  55. Gfx::Painter painter_1x(*m_bitmap_1x);
  56. draw(painter_1x);
  57. Gfx::Painter painter_2x(*m_bitmap_2x);
  58. painter_2x.scale(2);
  59. draw(painter_2x);
  60. update();
  61. }
  62. Canvas::~Canvas()
  63. {
  64. }
  65. void Canvas::paint_event(GUI::PaintEvent& event)
  66. {
  67. GUI::Painter painter(*this);
  68. painter.add_clip_rect(event.rect());
  69. painter.fill_rect(event.rect(), Color::Magenta);
  70. painter.blit({ 0, 0 }, *m_bitmap_1x, m_bitmap_1x->rect());
  71. painter.blit({ 0, HEIGHT }, *m_bitmap_2x, m_bitmap_2x->rect());
  72. }
  73. void Canvas::draw(Gfx::Painter& painter)
  74. {
  75. auto active_window_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window.png");
  76. Gfx::WindowTheme::current().paint_normal_frame(painter, Gfx::WindowTheme::WindowState::Active, { 4, 18, WIDTH - 8, HEIGHT - 29 }, "Well hello friends", *active_window_icon, palette(), { WIDTH - 20, 6, 16, 16 });
  77. }
  78. int main(int argc, char** argv)
  79. {
  80. auto app = GUI::Application::construct(argc, argv);
  81. if (pledge("stdio rpath shared_buffer", nullptr) < 0) {
  82. perror("pledge");
  83. return 1;
  84. }
  85. if (unveil("/res", "r") < 0) {
  86. perror("unveil");
  87. return 1;
  88. }
  89. if (unveil(nullptr, nullptr) < 0) {
  90. perror("unveil");
  91. return 1;
  92. }
  93. auto window = GUI::Window::construct();
  94. window->set_title("LibGfx Scale Demo");
  95. window->set_resizable(false);
  96. window->resize(WIDTH * 2, HEIGHT * 3);
  97. auto app_icon = GUI::Icon::default_icon("app-libgfx-demo");
  98. window->set_icon(app_icon.bitmap_for_size(16));
  99. window->set_main_widget<Canvas>();
  100. window->show();
  101. return app->exec();
  102. }