SpreadsheetWidget.cpp 24 KB

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