main.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/SystemTheme.h>
  38. #include <LibGfx/WindowTheme.h>
  39. const int WIDTH = 300;
  40. const int HEIGHT = 200;
  41. class Canvas final : public GUI::Widget {
  42. C_OBJECT(Canvas)
  43. public:
  44. virtual ~Canvas() override;
  45. private:
  46. Canvas();
  47. RefPtr<Gfx::Bitmap> m_bitmap_1x;
  48. RefPtr<Gfx::Bitmap> m_bitmap_2x;
  49. void draw(Gfx::Painter& painter);
  50. virtual void paint_event(GUI::PaintEvent&) override;
  51. };
  52. Canvas::Canvas()
  53. {
  54. m_bitmap_1x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH, HEIGHT });
  55. m_bitmap_2x = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH * 2, HEIGHT * 2 });
  56. Gfx::Painter painter_1x(*m_bitmap_1x, 1);
  57. draw(painter_1x);
  58. Gfx::Painter painter_2x(*m_bitmap_2x, 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. painter.draw_rect({ 20, 34, WIDTH - 40, HEIGHT - 45 }, palette().color(Gfx::ColorRole::Selection), true);
  78. painter.draw_rect({ 24, 38, WIDTH - 48, HEIGHT - 53 }, palette().color(Gfx::ColorRole::Selection));
  79. }
  80. int main(int argc, char** argv)
  81. {
  82. auto app = GUI::Application::construct(argc, argv);
  83. if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
  84. perror("pledge");
  85. return 1;
  86. }
  87. if (unveil("/res", "r") < 0) {
  88. perror("unveil");
  89. return 1;
  90. }
  91. if (unveil(nullptr, nullptr) < 0) {
  92. perror("unveil");
  93. return 1;
  94. }
  95. auto window = GUI::Window::construct();
  96. window->set_title("LibGfx Scale Demo");
  97. window->set_resizable(false);
  98. window->resize(WIDTH * 2, HEIGHT * 3);
  99. auto app_icon = GUI::Icon::default_icon("app-libgfx-demo");
  100. window->set_icon(app_icon.bitmap_for_size(16));
  101. window->set_main_widget<Canvas>();
  102. window->show();
  103. return app->exec();
  104. }