VBForm.cpp 18 KB

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