VBForm.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 "VBForm.h"
  27. #include "VBProperty.h"
  28. #include "VBWidget.h"
  29. #include "VBWidgetRegistry.h"
  30. #include <AK/JsonArray.h>
  31. #include <AK/JsonObject.h>
  32. #include <AK/StringBuilder.h>
  33. #include <LibCore/File.h>
  34. #include <LibGUI/Action.h>
  35. #include <LibGUI/BoxLayout.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/MessageBox.h>
  38. #include <LibGUI/Painter.h>
  39. static VBForm* s_current;
  40. VBForm* VBForm::current()
  41. {
  42. return s_current;
  43. }
  44. VBForm::VBForm(const String& name)
  45. : m_name(name)
  46. {
  47. s_current = this;
  48. set_fill_with_background_color(true);
  49. set_greedy_for_hits(true);
  50. m_context_menu = GUI::Menu::construct();
  51. m_context_menu->add_action(GUI::CommonActions::make_move_to_front_action([this](auto&) {
  52. if (auto* widget = single_selected_widget())
  53. widget->gwidget()->move_to_front();
  54. }));
  55. m_context_menu->add_action(GUI::CommonActions::make_move_to_back_action([this](auto&) {
  56. if (auto* widget = single_selected_widget())
  57. widget->gwidget()->move_to_back();
  58. }));
  59. m_context_menu->add_separator();
  60. m_context_menu->add_action(GUI::Action::create("Lay out horizontally", Gfx::Bitmap::load_from_file("/res/icons/16x16/layout-horizontally.png"), [this](auto&) {
  61. if (auto* widget = single_selected_widget()) {
  62. dbg() << "Giving " << *widget->gwidget() << " a horizontal box layout";
  63. widget->gwidget()->set_layout(make<GUI::HorizontalBoxLayout>());
  64. }
  65. }));
  66. m_context_menu->add_action(GUI::Action::create("Lay out vertically", Gfx::Bitmap::load_from_file("/res/icons/16x16/layout-vertically.png"), [this](auto&) {
  67. if (auto* widget = single_selected_widget()) {
  68. dbg() << "Giving " << *widget->gwidget() << " a vertical box layout";
  69. widget->gwidget()->set_layout(make<GUI::VerticalBoxLayout>());
  70. }
  71. }));
  72. m_context_menu->add_separator();
  73. m_context_menu->add_action(GUI::CommonActions::make_delete_action([this](auto&) {
  74. delete_selected_widgets();
  75. }));
  76. }
  77. void VBForm::context_menu_event(GUI::ContextMenuEvent& event)
  78. {
  79. m_context_menu->popup(event.screen_position());
  80. }
  81. void VBForm::insert_widget(VBWidgetType type)
  82. {
  83. auto* insertion_parent = single_selected_widget();
  84. auto widget = VBWidget::create(type, *this, insertion_parent);
  85. Gfx::Point insertion_position = m_next_insertion_position;
  86. if (insertion_parent)
  87. insertion_position.move_by(insertion_parent->gwidget()->window_relative_rect().location());
  88. widget->set_rect({ insertion_position, { m_grid_size * 10 + 1, m_grid_size * 5 + 1 } });
  89. m_next_insertion_position.move_by(m_grid_size, m_grid_size);
  90. m_widgets.append(move(widget));
  91. }
  92. VBForm::~VBForm()
  93. {
  94. }
  95. void VBForm::paint_event(GUI::PaintEvent& event)
  96. {
  97. GUI::Painter painter(*this);
  98. painter.add_clip_rect(event.rect());
  99. for (int y = 0; y < height(); y += m_grid_size) {
  100. for (int x = 0; x < width(); x += m_grid_size) {
  101. painter.set_pixel({ x, y }, Color::from_rgb(0x404040));
  102. }
  103. }
  104. }
  105. void VBForm::second_paint_event(GUI::PaintEvent& event)
  106. {
  107. GUI::Painter painter(*this);
  108. painter.add_clip_rect(event.rect());
  109. for (auto& widget : m_widgets) {
  110. if (widget.is_selected()) {
  111. for_each_direction([&](auto direction) {
  112. bool in_layout = widget.is_in_layout();
  113. auto grabber_rect = widget.grabber_rect(direction);
  114. painter.fill_rect(grabber_rect, in_layout ? Color::White : Color::Black);
  115. if (in_layout)
  116. painter.draw_rect(grabber_rect, Color::Black);
  117. });
  118. }
  119. }
  120. }
  121. bool VBForm::is_selected(const VBWidget& widget) const
  122. {
  123. // FIXME: Fix HashTable and remove this const_cast.
  124. return m_selected_widgets.contains(const_cast<VBWidget*>(&widget));
  125. }
  126. VBWidget* VBForm::widget_at(const Gfx::Point& position)
  127. {
  128. auto result = hit_test(position, GUI::Widget::ShouldRespectGreediness::No);
  129. if (!result.widget)
  130. return nullptr;
  131. auto* gwidget = result.widget;
  132. while (gwidget) {
  133. if (auto* widget = m_gwidget_map.get(gwidget).value_or(nullptr))
  134. return widget;
  135. gwidget = gwidget->parent_widget();
  136. }
  137. return nullptr;
  138. }
  139. void VBForm::grabber_mousedown_event(GUI::MouseEvent& event, Direction grabber)
  140. {
  141. m_transform_event_origin = event.position();
  142. for_each_selected_widget([](auto& widget) { widget.capture_transform_origin_rect(); });
  143. m_resize_direction = grabber;
  144. }
  145. void VBForm::keydown_event(GUI::KeyEvent& event)
  146. {
  147. if (event.key() == KeyCode::Key_Delete) {
  148. delete_selected_widgets();
  149. return;
  150. }
  151. if (event.key() == KeyCode::Key_Tab) {
  152. if (m_widgets.is_empty())
  153. return;
  154. if (m_selected_widgets.is_empty()) {
  155. set_single_selected_widget(&m_widgets.first());
  156. update();
  157. return;
  158. }
  159. size_t selected_widget_index = 0;
  160. for (; selected_widget_index < m_widgets.size(); ++selected_widget_index) {
  161. if (&m_widgets[selected_widget_index] == *m_selected_widgets.begin())
  162. break;
  163. }
  164. ++selected_widget_index;
  165. if (selected_widget_index == m_widgets.size())
  166. selected_widget_index = 0;
  167. set_single_selected_widget(&m_widgets[selected_widget_index]);
  168. update();
  169. return;
  170. }
  171. if (!m_selected_widgets.is_empty()) {
  172. switch (event.key()) {
  173. case KeyCode::Key_Up:
  174. update();
  175. for_each_selected_widget([this](auto& widget) {
  176. if (widget.is_in_layout())
  177. return;
  178. widget.gwidget()->move_by(0, -m_grid_size);
  179. });
  180. break;
  181. case KeyCode::Key_Down:
  182. update();
  183. for_each_selected_widget([this](auto& widget) {
  184. if (widget.is_in_layout())
  185. return;
  186. widget.gwidget()->move_by(0, m_grid_size);
  187. });
  188. break;
  189. case KeyCode::Key_Left:
  190. update();
  191. for_each_selected_widget([this](auto& widget) {
  192. if (widget.is_in_layout())
  193. return;
  194. widget.gwidget()->move_by(-m_grid_size, 0);
  195. });
  196. break;
  197. case KeyCode::Key_Right:
  198. update();
  199. for_each_selected_widget([this](auto& widget) {
  200. if (widget.is_in_layout())
  201. return;
  202. widget.gwidget()->move_by(m_grid_size, 0);
  203. });
  204. break;
  205. }
  206. return;
  207. }
  208. }
  209. void VBForm::set_single_selected_widget(VBWidget* widget)
  210. {
  211. if (!widget) {
  212. if (!m_selected_widgets.is_empty()) {
  213. m_selected_widgets.clear();
  214. on_widget_selected(nullptr);
  215. update();
  216. }
  217. return;
  218. }
  219. m_selected_widgets.clear();
  220. m_selected_widgets.set(widget);
  221. on_widget_selected(m_selected_widgets.size() == 1 ? widget : nullptr);
  222. update();
  223. }
  224. void VBForm::add_to_selection(VBWidget& widget)
  225. {
  226. m_selected_widgets.set(&widget);
  227. update();
  228. }
  229. void VBForm::remove_from_selection(VBWidget& widget)
  230. {
  231. m_selected_widgets.remove(&widget);
  232. update();
  233. }
  234. void VBForm::mousedown_event(GUI::MouseEvent& event)
  235. {
  236. if (m_resize_direction == Direction::None) {
  237. bool hit_grabber = false;
  238. for_each_selected_widget([&](auto& widget) {
  239. if (widget.is_in_layout())
  240. return;
  241. auto grabber = widget.grabber_at(event.position());
  242. if (grabber != Direction::None) {
  243. hit_grabber = true;
  244. return grabber_mousedown_event(event, grabber);
  245. }
  246. });
  247. if (hit_grabber)
  248. return;
  249. }
  250. auto* widget = widget_at(event.position());
  251. if (!widget) {
  252. set_single_selected_widget(nullptr);
  253. return;
  254. }
  255. if (event.button() == GUI::MouseButton::Left || event.button() == GUI::MouseButton::Right) {
  256. m_transform_event_origin = event.position();
  257. if (event.modifiers() == Mod_Ctrl)
  258. remove_from_selection(*widget);
  259. else if (event.modifiers() == Mod_Shift)
  260. add_to_selection(*widget);
  261. else if (!m_selected_widgets.contains(widget))
  262. set_single_selected_widget(widget);
  263. for_each_selected_widget([](auto& widget) { widget.capture_transform_origin_rect(); });
  264. on_widget_selected(single_selected_widget());
  265. }
  266. }
  267. void VBForm::mousemove_event(GUI::MouseEvent& event)
  268. {
  269. if (event.buttons() & GUI::MouseButton::Left) {
  270. if (m_resize_direction == Direction::None) {
  271. update();
  272. auto delta = event.position() - m_transform_event_origin;
  273. for_each_selected_widget([&](auto& widget) {
  274. if (widget.is_in_layout())
  275. return;
  276. auto new_rect = widget.transform_origin_rect().translated(delta);
  277. new_rect.set_x(new_rect.x() - (new_rect.x() % m_grid_size));
  278. new_rect.set_y(new_rect.y() - (new_rect.y() % m_grid_size));
  279. widget.set_rect(new_rect);
  280. });
  281. return;
  282. }
  283. int diff_x = event.x() - m_transform_event_origin.x();
  284. int diff_y = event.y() - m_transform_event_origin.y();
  285. int change_x = 0;
  286. int change_y = 0;
  287. int change_w = 0;
  288. int change_h = 0;
  289. switch (m_resize_direction) {
  290. case Direction::DownRight:
  291. change_w = diff_x;
  292. change_h = diff_y;
  293. break;
  294. case Direction::Right:
  295. change_w = diff_x;
  296. break;
  297. case Direction::UpRight:
  298. change_w = diff_x;
  299. change_y = diff_y;
  300. change_h = -diff_y;
  301. break;
  302. case Direction::Up:
  303. change_y = diff_y;
  304. change_h = -diff_y;
  305. break;
  306. case Direction::UpLeft:
  307. change_x = diff_x;
  308. change_w = -diff_x;
  309. change_y = diff_y;
  310. change_h = -diff_y;
  311. break;
  312. case Direction::Left:
  313. change_x = diff_x;
  314. change_w = -diff_x;
  315. break;
  316. case Direction::DownLeft:
  317. change_x = diff_x;
  318. change_w = -diff_x;
  319. change_h = diff_y;
  320. break;
  321. case Direction::Down:
  322. change_h = diff_y;
  323. break;
  324. default:
  325. ASSERT_NOT_REACHED();
  326. }
  327. update();
  328. for_each_selected_widget([&](auto& widget) {
  329. if (widget.is_in_layout())
  330. return;
  331. auto new_rect = widget.transform_origin_rect();
  332. Gfx::Size minimum_size { 5, 5 };
  333. new_rect.set_x(new_rect.x() + change_x);
  334. new_rect.set_y(new_rect.y() + change_y);
  335. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  336. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  337. new_rect.set_x(new_rect.x() - (new_rect.x() % m_grid_size));
  338. new_rect.set_y(new_rect.y() - (new_rect.y() % m_grid_size));
  339. new_rect.set_width(new_rect.width() - (new_rect.width() % m_grid_size) + 1);
  340. new_rect.set_height(new_rect.height() - (new_rect.height() % m_grid_size) + 1);
  341. widget.set_rect(new_rect);
  342. });
  343. set_cursor_type_from_grabber(m_resize_direction);
  344. } else {
  345. for (auto& widget : m_selected_widgets) {
  346. if (widget->is_in_layout())
  347. continue;
  348. auto grabber_at = widget->grabber_at(event.position());
  349. set_cursor_type_from_grabber(grabber_at);
  350. if (grabber_at != Direction::None)
  351. break;
  352. }
  353. }
  354. }
  355. void VBForm::load_from_file(const String& path)
  356. {
  357. auto file = Core::File::construct(path);
  358. if (!file->open(Core::IODevice::ReadOnly)) {
  359. GUI::MessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  360. return;
  361. }
  362. auto file_contents = file->read_all();
  363. auto form_json = JsonValue::from_string(file_contents);
  364. if (!form_json.is_object()) {
  365. GUI::MessageBox::show(String::format("Could not parse '%s'", path.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  366. return;
  367. }
  368. m_name = form_json.as_object().get("name").to_string();
  369. auto widgets = form_json.as_object().get("widgets").as_array();
  370. widgets.for_each([&](const JsonValue& widget_value) {
  371. auto& widget_object = widget_value.as_object();
  372. auto widget_class = widget_object.get("class").as_string();
  373. auto widget_type = widget_type_from_class_name(widget_class);
  374. // FIXME: Construct VBWidget within the right parent..
  375. auto vbwidget = VBWidget::create(widget_type, *this, nullptr);
  376. widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) {
  377. (void)property_name;
  378. (void)property_value;
  379. VBProperty& property = vbwidget->property(property_name);
  380. dbgprintf("Set property %s.%s to '%s'\n", widget_class.characters(), property_name.characters(), property_value.to_string().characters());
  381. property.set_value(property_value);
  382. });
  383. m_widgets.append(vbwidget);
  384. });
  385. }
  386. void VBForm::write_to_file(const String& path)
  387. {
  388. auto file = Core::File::construct(path);
  389. if (!file->open(Core::IODevice::WriteOnly)) {
  390. GUI::MessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window());
  391. return;
  392. }
  393. JsonObject form_object;
  394. form_object.set("name", m_name);
  395. JsonArray widget_array;
  396. for (auto& widget : m_widgets) {
  397. JsonObject widget_object;
  398. widget.for_each_property([&](auto& property) {
  399. if (property.value().is_bool())
  400. widget_object.set(property.name(), property.value().to_bool());
  401. else if (property.value().is_i32())
  402. widget_object.set(property.name(), property.value().to_i32());
  403. else if (property.value().is_i64())
  404. widget_object.set(property.name(), property.value().to_i64());
  405. else
  406. widget_object.set(property.name(), property.value().to_string());
  407. });
  408. widget_array.append(widget_object);
  409. }
  410. form_object.set("widgets", widget_array);
  411. file->write(form_object.to_string());
  412. }
  413. void VBForm::dump()
  414. {
  415. dbgprintf("[Form]\n");
  416. dbgprintf("Name=%s\n", m_name.characters());
  417. dbgprintf("\n");
  418. int i = 0;
  419. for (auto& widget : m_widgets) {
  420. dbgprintf("[Widget %d]\n", i++);
  421. widget.for_each_property([](auto& property) {
  422. dbgprintf("%s=%s\n", property.name().characters(), property.value().to_string().characters());
  423. });
  424. dbgprintf("\n");
  425. }
  426. }
  427. void VBForm::mouseup_event(GUI::MouseEvent& event)
  428. {
  429. if (event.button() == GUI::MouseButton::Left) {
  430. m_transform_event_origin = {};
  431. m_resize_direction = Direction::None;
  432. }
  433. }
  434. void VBForm::delete_selected_widgets()
  435. {
  436. Vector<VBWidget*> to_delete;
  437. for_each_selected_widget([&](auto& widget) {
  438. to_delete.append(&widget);
  439. });
  440. if (to_delete.is_empty())
  441. return;
  442. for (auto& widget : to_delete)
  443. m_widgets.remove_first_matching([&widget](auto& entry) { return entry == widget; });
  444. on_widget_selected(single_selected_widget());
  445. update();
  446. }
  447. template<typename Callback>
  448. void VBForm::for_each_selected_widget(Callback callback)
  449. {
  450. for (auto& widget : m_selected_widgets)
  451. callback(*widget);
  452. }
  453. void VBForm::set_cursor_type_from_grabber(Direction grabber)
  454. {
  455. if (grabber == m_mouse_direction_type)
  456. return;
  457. switch (grabber) {
  458. case Direction::Up:
  459. case Direction::Down:
  460. window()->set_override_cursor(GUI::StandardCursor::ResizeVertical);
  461. break;
  462. case Direction::Left:
  463. case Direction::Right:
  464. window()->set_override_cursor(GUI::StandardCursor::ResizeHorizontal);
  465. break;
  466. case Direction::UpLeft:
  467. case Direction::DownRight:
  468. window()->set_override_cursor(GUI::StandardCursor::ResizeDiagonalTLBR);
  469. break;
  470. case Direction::UpRight:
  471. case Direction::DownLeft:
  472. window()->set_override_cursor(GUI::StandardCursor::ResizeDiagonalBLTR);
  473. break;
  474. case Direction::None:
  475. window()->set_override_cursor(GUI::StandardCursor::None);
  476. break;
  477. }
  478. m_mouse_direction_type = grabber;
  479. }
  480. VBWidget* VBForm::single_selected_widget()
  481. {
  482. if (m_selected_widgets.size() != 1)
  483. return nullptr;
  484. return *m_selected_widgets.begin();
  485. }