titlebar.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <ClanLib/Display/display.h>
  17. #include <ClanLib/Display/keys.h>
  18. #include <ClanLib/Display/mouse.h>
  19. #include "fonts.hpp"
  20. #include "titlebar.hpp"
  21. class TitlebarImpl
  22. {
  23. public:
  24. CL_Component* window;
  25. Titlebar* parent;
  26. CL_Point click_pos;
  27. CL_Rect old_pos;
  28. std::string title;
  29. std::vector<CL_Slot> slots;
  30. bool pressed;
  31. TitlebarImpl(Titlebar* parent_) : parent(parent_) {}
  32. void on_mouse_move(const CL_InputEvent& event);
  33. void on_mouse_down(const CL_InputEvent& event);
  34. void on_mouse_up(const CL_InputEvent& event);
  35. void draw();
  36. };
  37. Titlebar::Titlebar(const CL_Rect& rect, const std::string& title, CL_Component* parent)
  38. : CL_Component(rect, parent),
  39. impl(new TitlebarImpl(this))
  40. {
  41. impl->title = title;
  42. impl->pressed = false;
  43. impl->window = parent;
  44. impl->slots.push_back(sig_mouse_down().connect(impl.get(), &TitlebarImpl::on_mouse_down));
  45. impl->slots.push_back(sig_mouse_move().connect(impl.get(), &TitlebarImpl::on_mouse_move));
  46. impl->slots.push_back(sig_mouse_up().connect(impl.get(), &TitlebarImpl::on_mouse_up));
  47. impl->slots.push_back(sig_paint().connect(impl.get(), &TitlebarImpl::draw));
  48. }
  49. void
  50. TitlebarImpl::on_mouse_up(const CL_InputEvent& event)
  51. {
  52. if (event.id == CL_MOUSE_LEFT)
  53. {
  54. pressed = false;
  55. parent->release_mouse();
  56. }
  57. }
  58. void
  59. TitlebarImpl::on_mouse_down(const CL_InputEvent& event)
  60. {
  61. if (event.id == CL_MOUSE_LEFT)
  62. {
  63. pressed = true;
  64. click_pos = event.mouse_pos;
  65. parent->capture_mouse();
  66. window->raise();
  67. old_pos = window->get_position();
  68. click_pos.x += old_pos.left;
  69. click_pos.y += old_pos.top;
  70. }
  71. }
  72. void
  73. TitlebarImpl::on_mouse_move(const CL_InputEvent& event)
  74. {
  75. if(pressed)
  76. {
  77. CL_Rect rect = window->get_position();
  78. CL_Point move(old_pos.left - (click_pos.x - (rect.left + event.mouse_pos.x)),
  79. old_pos.top - (click_pos.y - (rect.top + event.mouse_pos.y)));
  80. window->set_position(move.x, move.y);
  81. }
  82. }
  83. void
  84. TitlebarImpl::draw()
  85. {
  86. CL_Display::push_translate(parent->get_screen_x(), parent->get_screen_y());
  87. // FIXME: Hack should be done via has_mouse_over(), but that doesn't include child components
  88. if (parent->get_parent()->get_position().is_inside(CL_Point(CL_Mouse::get_x(),
  89. CL_Mouse::get_y())))
  90. //parent->get_parent()->has_mouse_over())
  91. {
  92. CL_Display::fill_rect(CL_Rect(CL_Point(0, 0),
  93. CL_Size(parent->get_width()-1, parent->get_height())),
  94. CL_Color(250, 250, 250));
  95. }
  96. else
  97. {
  98. CL_Display::fill_rect(CL_Rect(CL_Point(0, 0),
  99. CL_Size(parent->get_width()-1, parent->get_height())),
  100. CL_Color(240, 240, 240));
  101. }
  102. Fonts::verdana11.draw(4, 0, title);
  103. CL_Display::pop_modelview();
  104. }
  105. /* EOF */