VBForm.cpp 18 KB

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