WSMenuItem.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.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 "WSMenuItem.h"
  27. #include "WSClientConnection.h"
  28. #include "WSMenu.h"
  29. #include "WSWindowManager.h"
  30. #include <LibGfx/Bitmap.h>
  31. WSMenuItem::WSMenuItem(WSMenu& menu, unsigned identifier, const String& text, const String& shortcut_text, bool enabled, bool checkable, bool checked, const Gfx::Bitmap* icon)
  32. : m_menu(menu)
  33. , m_type(Text)
  34. , m_enabled(enabled)
  35. , m_checkable(checkable)
  36. , m_checked(checked)
  37. , m_identifier(identifier)
  38. , m_text(text)
  39. , m_shortcut_text(shortcut_text)
  40. , m_icon(icon)
  41. {
  42. }
  43. WSMenuItem::WSMenuItem(WSMenu& menu, Type type)
  44. : m_menu(menu)
  45. , m_type(type)
  46. {
  47. }
  48. WSMenuItem::~WSMenuItem()
  49. {
  50. }
  51. void WSMenuItem::set_enabled(bool enabled)
  52. {
  53. if (m_enabled == enabled)
  54. return;
  55. m_enabled = enabled;
  56. m_menu.redraw();
  57. }
  58. void WSMenuItem::set_checked(bool checked)
  59. {
  60. if (m_checked == checked)
  61. return;
  62. m_checked = checked;
  63. m_menu.redraw();
  64. }
  65. WSMenu* WSMenuItem::submenu()
  66. {
  67. ASSERT(is_submenu());
  68. if (m_menu.client())
  69. return m_menu.client()->find_menu_by_id(m_submenu_id);
  70. return WSMenuManager::the().find_internal_menu_by_id(m_submenu_id);
  71. }
  72. Rect WSMenuItem::rect() const
  73. {
  74. if (!m_menu.is_scrollable())
  75. return m_rect;
  76. return m_rect.translated(0, m_menu.item_height() - (m_menu.scroll_offset() * m_menu.item_height()));
  77. }