SpreadsheetWidget.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SpreadsheetWidget.h"
  7. #include "CellSyntaxHighlighter.h"
  8. #include "HelpWindow.h"
  9. #include "LibFileSystemAccessClient/Client.h"
  10. #include <LibGUI/Application.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/Button.h>
  13. #include <LibGUI/InputBox.h>
  14. #include <LibGUI/Label.h>
  15. #include <LibGUI/Menu.h>
  16. #include <LibGUI/MessageBox.h>
  17. #include <LibGUI/Splitter.h>
  18. #include <LibGUI/TabWidget.h>
  19. #include <LibGUI/TextEditor.h>
  20. #include <LibGUI/Toolbar.h>
  21. #include <LibGUI/ToolbarContainer.h>
  22. #include <LibGfx/Font/FontDatabase.h>
  23. #include <string.h>
  24. namespace Spreadsheet {
  25. SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, NonnullRefPtrVector<Sheet>&& sheets, bool should_add_sheet_if_empty)
  26. : m_workbook(make<Workbook>(move(sheets), parent_window))
  27. {
  28. set_fill_with_background_color(true);
  29. set_layout<GUI::VerticalBoxLayout>().set_margins(2);
  30. auto& toolbar_container = add<GUI::ToolbarContainer>();
  31. auto& toolbar = toolbar_container.add<GUI::Toolbar>();
  32. auto& container = add<GUI::VerticalSplitter>();
  33. auto& top_bar = container.add<GUI::Frame>();
  34. top_bar.set_layout<GUI::HorizontalBoxLayout>().set_spacing(1);
  35. top_bar.set_fixed_height(26);
  36. auto& current_cell_label = top_bar.add<GUI::Label>("");
  37. current_cell_label.set_fixed_width(50);
  38. auto& help_button = top_bar.add<GUI::Button>("");
  39. help_button.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-help.png"sv).release_value_but_fixme_should_propagate_errors());
  40. help_button.set_tooltip("Functions Help");
  41. help_button.set_fixed_size(20, 20);
  42. help_button.on_click = [&](auto) {
  43. if (!current_view()) {
  44. GUI::MessageBox::show_error(window(), "Can only show function documentation/help when a worksheet exists and is open"sv);
  45. } else if (auto* sheet_ptr = current_worksheet_if_available()) {
  46. auto docs = sheet_ptr->gather_documentation();
  47. auto help_window = HelpWindow::the(window());
  48. help_window->set_docs(move(docs));
  49. help_window->set_window_mode(GUI::WindowMode::Modeless);
  50. help_window->show();
  51. }
  52. };
  53. auto& cell_value_editor = top_bar.add<GUI::TextEditor>(GUI::TextEditor::Type::SingleLine);
  54. cell_value_editor.set_font(Gfx::FontDatabase::default_fixed_width_font());
  55. cell_value_editor.set_scrollbars_enabled(false);
  56. cell_value_editor.on_return_pressed = [this]() {
  57. current_view()->move_cursor(GUI::AbstractView::CursorMovement::Down);
  58. };
  59. cell_value_editor.set_syntax_highlighter(make<CellSyntaxHighlighter>());
  60. cell_value_editor.set_enabled(false);
  61. current_cell_label.set_enabled(false);
  62. m_tab_widget = container.add<GUI::TabWidget>();
  63. m_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  64. m_cell_value_editor = cell_value_editor;
  65. m_current_cell_label = current_cell_label;
  66. m_inline_documentation_window = GUI::Window::construct(window());
  67. m_inline_documentation_window->set_rect(m_cell_value_editor->rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
  68. m_inline_documentation_window->set_window_type(GUI::WindowType::Tooltip);
  69. m_inline_documentation_window->set_resizable(false);
  70. auto& inline_widget = m_inline_documentation_window->set_main_widget<GUI::Frame>();
  71. inline_widget.set_fill_with_background_color(true);
  72. inline_widget.set_layout<GUI::VerticalBoxLayout>().set_margins(4);
  73. inline_widget.set_frame_shape(Gfx::FrameShape::Box);
  74. m_inline_documentation_label = inline_widget.add<GUI::Label>();
  75. m_inline_documentation_label->set_fill_with_background_color(true);
  76. m_inline_documentation_label->set_autosize(false);
  77. m_inline_documentation_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  78. if (!m_workbook->has_sheets() && should_add_sheet_if_empty)
  79. m_workbook->add_sheet("Sheet 1"sv);
  80. m_tab_context_menu = GUI::Menu::construct();
  81. m_rename_action = GUI::CommonActions::make_rename_action([this](auto&) {
  82. VERIFY(m_tab_context_menu_sheet_view);
  83. auto* sheet_ptr = m_tab_context_menu_sheet_view->sheet_if_available();
  84. VERIFY(sheet_ptr); // How did we get here without a sheet?
  85. auto& sheet = *sheet_ptr;
  86. String new_name;
  87. if (GUI::InputBox::show(window(), new_name, String::formatted("New name for '{}'", sheet.name()), "Rename sheet"sv) == GUI::Dialog::ExecResult::OK) {
  88. sheet.set_name(new_name);
  89. sheet.update();
  90. m_tab_widget->set_tab_title(static_cast<GUI::Widget&>(*m_tab_context_menu_sheet_view), new_name);
  91. }
  92. });
  93. m_tab_context_menu->add_action(*m_rename_action);
  94. m_tab_context_menu->add_action(GUI::Action::create("Add new sheet...", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new-tab.png"sv).release_value_but_fixme_should_propagate_errors(), [this](auto&) {
  95. String name;
  96. if (GUI::InputBox::show(window(), name, "Name for new sheet"sv, "Create sheet"sv) == GUI::Dialog::ExecResult::OK) {
  97. NonnullRefPtrVector<Sheet> new_sheets;
  98. new_sheets.append(m_workbook->add_sheet(name));
  99. setup_tabs(move(new_sheets));
  100. }
  101. }));
  102. setup_tabs(m_workbook->sheets());
  103. m_new_action = GUI::Action::create("Add New Sheet", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new-tab.png"sv).release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  104. add_sheet();
  105. });
  106. m_open_action = GUI::CommonActions::make_open_action([&](auto&) {
  107. if (!request_close())
  108. return;
  109. auto response = FileSystemAccessClient::Client::the().try_open_file(window());
  110. if (response.is_error())
  111. return;
  112. load_file(*response.value());
  113. });
  114. m_import_action = GUI::Action::create("Import sheets...", [&](auto&) {
  115. auto response = FileSystemAccessClient::Client::the().try_open_file(window());
  116. if (response.is_error())
  117. return;
  118. import_sheets(*response.value());
  119. });
  120. m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
  121. if (current_filename().is_empty()) {
  122. m_save_as_action->activate();
  123. return;
  124. }
  125. auto response = FileSystemAccessClient::Client::the().try_request_file(window(), current_filename(), Core::OpenMode::WriteOnly);
  126. if (response.is_error())
  127. return;
  128. save(*response.value());
  129. });
  130. m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
  131. String name = "workbook";
  132. auto response = FileSystemAccessClient::Client::the().try_save_file(window(), name, "sheets");
  133. if (response.is_error())
  134. return;
  135. save(*response.value());
  136. update_window_title();
  137. });
  138. m_quit_action = GUI::CommonActions::make_quit_action([&](auto&) {
  139. if (!request_close())
  140. return;
  141. GUI::Application::the()->quit(0);
  142. });
  143. m_cut_action = GUI::CommonActions::make_cut_action([&](auto&) { clipboard_action(true); }, window());
  144. m_copy_action = GUI::CommonActions::make_copy_action([&](auto&) { clipboard_action(false); }, window());
  145. m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
  146. ScopeGuard update_after_paste { [&] { update(); } };
  147. auto* worksheet_ptr = current_worksheet_if_available();
  148. if (!worksheet_ptr) {
  149. GUI::MessageBox::show_error(window(), "There are no active worksheets"sv);
  150. return;
  151. }
  152. auto& sheet = *worksheet_ptr;
  153. auto& cells = sheet.selected_cells();
  154. VERIFY(!cells.is_empty());
  155. const auto& data = GUI::Clipboard::the().fetch_data_and_type();
  156. if (auto spreadsheet_data = data.metadata.get("text/x-spreadsheet-data"); spreadsheet_data.has_value()) {
  157. Vector<Spreadsheet::Position> source_positions, target_positions;
  158. auto lines = spreadsheet_data.value().split_view('\n');
  159. auto action = lines.take_first();
  160. for (auto& line : lines) {
  161. dbgln("Paste line '{}'", line);
  162. auto position = sheet.position_from_url(line);
  163. if (position.has_value())
  164. source_positions.append(position.release_value());
  165. }
  166. for (auto& position : sheet.selected_cells())
  167. target_positions.append(position);
  168. if (source_positions.is_empty())
  169. return;
  170. auto first_position = source_positions.take_first();
  171. auto cell_changes = sheet.copy_cells(move(source_positions), move(target_positions), first_position, action == "cut" ? Spreadsheet::Sheet::CopyOperation::Cut : Spreadsheet::Sheet::CopyOperation::Copy);
  172. undo_stack().push(make<CellsUndoCommand>(cell_changes));
  173. } else {
  174. for (auto& cell : sheet.selected_cells())
  175. sheet.ensure(cell).set_data(StringView { data.data.data(), data.data.size() });
  176. update();
  177. }
  178. },
  179. window());
  180. m_undo_action = GUI::CommonActions::make_undo_action([&](auto&) {
  181. undo();
  182. });
  183. m_redo_action = GUI::CommonActions::make_redo_action([&](auto&) {
  184. redo();
  185. });
  186. m_undo_stack.on_state_change = [this] {
  187. m_undo_action->set_enabled(m_undo_stack.can_undo());
  188. m_redo_action->set_enabled(m_undo_stack.can_redo());
  189. };
  190. m_undo_action->set_enabled(false);
  191. m_redo_action->set_enabled(false);
  192. m_functions_help_action = GUI::Action::create(
  193. "&Functions Help", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-help.png"sv).release_value_but_fixme_should_propagate_errors(), [&](auto&) {
  194. if (auto* worksheet_ptr = current_worksheet_if_available()) {
  195. auto docs = worksheet_ptr->gather_documentation();
  196. auto help_window = Spreadsheet::HelpWindow::the(window());
  197. help_window->set_docs(move(docs));
  198. help_window->show();
  199. } else {
  200. GUI::MessageBox::show_error(window(), "Cannot prepare documentation/help without an active worksheet"sv);
  201. }
  202. },
  203. window());
  204. m_about_action = GUI::CommonActions::make_about_action("Spreadsheet", GUI::Icon::default_icon("app-spreadsheet"sv), window());
  205. toolbar.add_action(*m_new_action);
  206. toolbar.add_action(*m_open_action);
  207. toolbar.add_action(*m_save_action);
  208. toolbar.add_separator();
  209. toolbar.add_action(*m_cut_action);
  210. toolbar.add_action(*m_copy_action);
  211. toolbar.add_action(*m_paste_action);
  212. toolbar.add_action(*m_undo_action);
  213. toolbar.add_action(*m_redo_action);
  214. m_cut_action->set_enabled(false);
  215. m_copy_action->set_enabled(false);
  216. m_paste_action->set_enabled(false);
  217. m_tab_widget->on_change = [this](auto& selected_widget) {
  218. // for keyboard shortcuts and command palette
  219. m_tab_context_menu_sheet_view = static_cast<SpreadsheetView&>(selected_widget);
  220. };
  221. m_tab_widget->on_context_menu_request = [&](auto& widget, auto& event) {
  222. m_tab_context_menu_sheet_view = static_cast<SpreadsheetView&>(widget);
  223. m_tab_context_menu->popup(event.screen_position());
  224. };
  225. m_tab_widget->on_double_click = [&](auto& widget) {
  226. m_tab_context_menu_sheet_view = static_cast<SpreadsheetView&>(widget);
  227. m_rename_action->activate();
  228. };
  229. }
  230. void SpreadsheetWidget::resize_event(GUI::ResizeEvent& event)
  231. {
  232. GUI::Widget::resize_event(event);
  233. if (m_inline_documentation_window && m_cell_value_editor && window())
  234. m_inline_documentation_window->set_rect(m_cell_value_editor->screen_relative_rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
  235. }
  236. void SpreadsheetWidget::clipboard_content_did_change(String const& mime_type)
  237. {
  238. if (auto* sheet = current_worksheet_if_available())
  239. m_paste_action->set_enabled(!sheet->selected_cells().is_empty() && mime_type.starts_with("text/"sv));
  240. }
  241. void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
  242. {
  243. for (auto& sheet : new_sheets) {
  244. auto& new_view = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
  245. new_view.model()->on_cell_data_change = [&](auto& cell, auto& previous_data) {
  246. undo_stack().push(make<CellsUndoCommand>(cell, previous_data));
  247. window()->set_modified(true);
  248. };
  249. new_view.model()->on_cells_data_change = [&](Vector<CellChange> cell_changes) {
  250. undo_stack().push(make<CellsUndoCommand>(cell_changes));
  251. window()->set_modified(true);
  252. };
  253. new_view.on_selection_changed = [&](Vector<Position>&& selection) {
  254. auto* sheet_ptr = current_worksheet_if_available();
  255. // How did this even happen?
  256. VERIFY(sheet_ptr);
  257. auto& sheet = *sheet_ptr;
  258. VERIFY(!selection.is_empty());
  259. m_cut_action->set_enabled(true);
  260. m_copy_action->set_enabled(true);
  261. m_paste_action->set_enabled(GUI::Clipboard::the().fetch_mime_type().starts_with("text/"sv));
  262. m_current_cell_label->set_enabled(true);
  263. m_cell_value_editor->set_enabled(true);
  264. if (selection.size() == 1) {
  265. auto& position = selection.first();
  266. m_current_cell_label->set_text(position.to_cell_identifier(sheet));
  267. auto& cell = sheet.ensure(position);
  268. m_cell_value_editor->on_change = nullptr;
  269. m_cell_value_editor->set_text(cell.source());
  270. m_cell_value_editor->on_change = [&] {
  271. auto text = m_cell_value_editor->text();
  272. // FIXME: Lines?
  273. auto offset = m_cell_value_editor->cursor().column();
  274. try_generate_tip_for_input_expression(text, offset);
  275. cell.set_data(move(text));
  276. sheet.update();
  277. update();
  278. };
  279. static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&cell);
  280. return;
  281. }
  282. // There are many cells selected, change all of them.
  283. StringBuilder builder;
  284. builder.appendff("<{}>", selection.size());
  285. m_current_cell_label->set_text(builder.string_view());
  286. Vector<Cell&> cells;
  287. for (auto& position : selection)
  288. cells.append(sheet.ensure(position));
  289. auto& first_cell = cells.first();
  290. m_cell_value_editor->on_change = nullptr;
  291. m_cell_value_editor->set_text(""sv);
  292. m_should_change_selected_cells = false;
  293. m_cell_value_editor->on_focusin = [this] { m_should_change_selected_cells = true; };
  294. m_cell_value_editor->on_focusout = [this] { m_should_change_selected_cells = false; };
  295. m_cell_value_editor->on_change = [cells = move(cells), this]() mutable {
  296. if (m_should_change_selected_cells) {
  297. auto* sheet_ptr = current_worksheet_if_available();
  298. if (!sheet_ptr)
  299. return;
  300. auto& sheet = *sheet_ptr;
  301. auto text = m_cell_value_editor->text();
  302. // FIXME: Lines?
  303. auto offset = m_cell_value_editor->cursor().column();
  304. try_generate_tip_for_input_expression(text, offset);
  305. for (auto& cell : cells)
  306. cell.set_data(text);
  307. sheet.update();
  308. update();
  309. }
  310. };
  311. static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&first_cell);
  312. };
  313. new_view.on_selection_dropped = [&]() {
  314. m_current_cell_label->set_enabled(false);
  315. m_current_cell_label->set_text({});
  316. m_cell_value_editor->on_change = nullptr;
  317. m_cell_value_editor->on_focusin = nullptr;
  318. m_cell_value_editor->on_focusout = nullptr;
  319. m_cell_value_editor->set_text({});
  320. m_cell_value_editor->set_enabled(false);
  321. m_cut_action->set_enabled(false);
  322. m_copy_action->set_enabled(false);
  323. m_paste_action->set_enabled(false);
  324. static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(nullptr);
  325. };
  326. }
  327. }
  328. void SpreadsheetWidget::try_generate_tip_for_input_expression(StringView source, size_t cursor_offset)
  329. {
  330. auto* sheet_ptr = current_view()->sheet_if_available();
  331. if (!sheet_ptr)
  332. return;
  333. auto& sheet = *sheet_ptr;
  334. m_inline_documentation_window->set_rect(m_cell_value_editor->screen_relative_rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
  335. if (!current_view() || !source.starts_with('=')) {
  336. m_inline_documentation_window->hide();
  337. return;
  338. }
  339. cursor_offset = min(cursor_offset, source.length());
  340. auto maybe_function_and_argument = get_function_and_argument_index(source.substring_view(0, cursor_offset));
  341. if (!maybe_function_and_argument.has_value()) {
  342. m_inline_documentation_window->hide();
  343. return;
  344. }
  345. auto& [name, index] = maybe_function_and_argument.value();
  346. auto text = sheet.generate_inline_documentation_for(name, index);
  347. if (text.is_empty()) {
  348. m_inline_documentation_window->hide();
  349. } else {
  350. m_inline_documentation_label->set_text(move(text));
  351. m_inline_documentation_window->show();
  352. }
  353. }
  354. void SpreadsheetWidget::undo()
  355. {
  356. if (!m_undo_stack.can_undo())
  357. return;
  358. m_undo_stack.undo();
  359. update();
  360. }
  361. void SpreadsheetWidget::redo()
  362. {
  363. if (!m_undo_stack.can_redo())
  364. return;
  365. m_undo_stack.redo();
  366. update();
  367. }
  368. void SpreadsheetWidget::save(Core::File& file)
  369. {
  370. auto result = m_workbook->write_to_file(file);
  371. if (result.is_error()) {
  372. GUI::MessageBox::show_error(window(), result.error());
  373. return;
  374. }
  375. undo_stack().set_current_unmodified();
  376. window()->set_modified(false);
  377. }
  378. void SpreadsheetWidget::load_file(Core::File& file)
  379. {
  380. auto result = m_workbook->open_file(file);
  381. if (result.is_error()) {
  382. GUI::MessageBox::show_error(window(), result.error());
  383. return;
  384. }
  385. m_cell_value_editor->on_change = nullptr;
  386. m_current_cell_label->set_text("");
  387. m_should_change_selected_cells = false;
  388. while (auto* widget = m_tab_widget->active_widget()) {
  389. m_tab_widget->remove_tab(*widget);
  390. }
  391. setup_tabs(m_workbook->sheets());
  392. update_window_title();
  393. }
  394. void SpreadsheetWidget::import_sheets(Core::File& file)
  395. {
  396. auto result = m_workbook->import_file(file);
  397. if (result.is_error()) {
  398. GUI::MessageBox::show_error(window(), result.error());
  399. return;
  400. }
  401. if (!result.value())
  402. return;
  403. window()->set_modified(true);
  404. m_cell_value_editor->on_change = nullptr;
  405. m_current_cell_label->set_text("");
  406. m_should_change_selected_cells = false;
  407. while (auto* widget = m_tab_widget->active_widget()) {
  408. m_tab_widget->remove_tab(*widget);
  409. }
  410. setup_tabs(m_workbook->sheets());
  411. update_window_title();
  412. }
  413. bool SpreadsheetWidget::request_close()
  414. {
  415. if (!undo_stack().is_current_modified())
  416. return true;
  417. auto result = GUI::MessageBox::ask_about_unsaved_changes(window(), current_filename());
  418. if (result == GUI::MessageBox::ExecResult::Yes) {
  419. m_save_action->activate();
  420. return !m_workbook->dirty();
  421. }
  422. if (result == GUI::MessageBox::ExecResult::No)
  423. return true;
  424. return false;
  425. }
  426. void SpreadsheetWidget::add_sheet()
  427. {
  428. StringBuilder name;
  429. name.append("Sheet"sv);
  430. name.appendff(" {}", m_workbook->sheets().size() + 1);
  431. NonnullRefPtrVector<Sheet> new_sheets;
  432. new_sheets.append(m_workbook->add_sheet(name.string_view()));
  433. setup_tabs(move(new_sheets));
  434. }
  435. void SpreadsheetWidget::add_sheet(NonnullRefPtr<Sheet>&& sheet)
  436. {
  437. VERIFY(m_workbook == &sheet->workbook());
  438. NonnullRefPtrVector<Sheet> new_sheets;
  439. new_sheets.append(move(sheet));
  440. m_workbook->sheets().extend(new_sheets);
  441. setup_tabs(new_sheets);
  442. }
  443. void SpreadsheetWidget::update_window_title()
  444. {
  445. StringBuilder builder;
  446. if (current_filename().is_empty())
  447. builder.append("Untitled"sv);
  448. else
  449. builder.append(current_filename());
  450. builder.append("[*] - Spreadsheet"sv);
  451. window()->set_title(builder.to_string());
  452. }
  453. void SpreadsheetWidget::clipboard_action(bool is_cut)
  454. {
  455. /// text/x-spreadsheet-data:
  456. /// - action: copy/cut
  457. /// - currently selected cell
  458. /// - selected cell+
  459. auto* worksheet_ptr = current_worksheet_if_available();
  460. if (!worksheet_ptr) {
  461. GUI::MessageBox::show_error(window(), "There are no active worksheets"sv);
  462. return;
  463. }
  464. auto& worksheet = *worksheet_ptr;
  465. auto& cells = worksheet.selected_cells();
  466. VERIFY(!cells.is_empty());
  467. StringBuilder text_builder, url_builder;
  468. url_builder.append(is_cut ? "cut\n"sv : "copy\n"sv);
  469. bool first = true;
  470. auto cursor = current_selection_cursor();
  471. if (cursor) {
  472. Spreadsheet::Position position { (size_t)cursor->column(), (size_t)cursor->row() };
  473. url_builder.append(position.to_url(worksheet).to_string());
  474. url_builder.append('\n');
  475. }
  476. for (auto& cell : cells) {
  477. if (first && !cursor) {
  478. url_builder.append(cell.to_url(worksheet).to_string());
  479. url_builder.append('\n');
  480. }
  481. url_builder.append(cell.to_url(worksheet).to_string());
  482. url_builder.append('\n');
  483. auto cell_data = worksheet.at(cell);
  484. if (!first)
  485. text_builder.append('\t');
  486. if (cell_data)
  487. text_builder.append(cell_data->data());
  488. first = false;
  489. }
  490. HashMap<String, String> metadata;
  491. metadata.set("text/x-spreadsheet-data", url_builder.to_string());
  492. dbgln(url_builder.to_string());
  493. GUI::Clipboard::the().set_data(text_builder.string_view().bytes(), "text/plain", move(metadata));
  494. }
  495. void SpreadsheetWidget::initialize_menubar(GUI::Window& window)
  496. {
  497. auto& file_menu = window.add_menu("&File");
  498. file_menu.add_action(*m_new_action);
  499. file_menu.add_action(*m_open_action);
  500. file_menu.add_action(*m_save_action);
  501. file_menu.add_action(*m_save_as_action);
  502. file_menu.add_separator();
  503. file_menu.add_action(*m_import_action);
  504. file_menu.add_separator();
  505. file_menu.add_action(*m_quit_action);
  506. auto& edit_menu = window.add_menu("&Edit");
  507. edit_menu.add_action(*m_undo_action);
  508. edit_menu.add_action(*m_redo_action);
  509. edit_menu.add_separator();
  510. edit_menu.add_action(*m_cut_action);
  511. edit_menu.add_action(*m_copy_action);
  512. edit_menu.add_action(*m_paste_action);
  513. auto& help_menu = window.add_menu("&Help");
  514. help_menu.add_action(*m_functions_help_action);
  515. help_menu.add_action(*m_about_action);
  516. }
  517. }