nss: upgrade to release 3.73
[LibreOffice.git] / libreofficekit / qa / gtktiledviewer / gtv-signal-handlers.cxx
blobbe7cd44455366dc14e516cdc7a744952e7bbc2f7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <gtk/gtk.h>
12 #include "gtv-application-window.hxx"
13 #include "gtv-helpers.hxx"
14 #include "gtv-lokdocview-signal-handlers.hxx"
15 #include "gtv-signal-handlers.hxx"
17 #include <sal/macros.h>
19 #include <map>
20 #include <vector>
22 #include <boost/property_tree/json_parser.hpp>
23 #include <optional>
25 void btn_clicked(GtkWidget* pButton, gpointer)
27 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
28 GtkToolButton* pItem = GTK_TOOL_BUTTON(pButton);
29 const gchar* label = gtk_tool_button_get_label(pItem);
30 if (!(gtv_application_window_get_toolbar_broadcast(window) && g_str_has_prefix(label, ".uno:")))
31 return;
33 std::string aArguments;
34 if (g_strcmp0(label, ".uno:InsertAnnotation") == 0)
36 std::map<std::string, std::string> aEntries;
37 aEntries["Text"] = "";
38 GtvHelpers::userPromptDialog(GTK_WINDOW(window), "Insert Comment", aEntries);
40 boost::property_tree::ptree aTree;
41 aTree.put(boost::property_tree::ptree::path_type("Text/type", '/'), "string");
42 aTree.put(boost::property_tree::ptree::path_type("Text/value", '/'), aEntries["Text"]);
44 std::stringstream aStream;
45 boost::property_tree::write_json(aStream, aTree);
46 aArguments = aStream.str();
49 bool bNotify = g_strcmp0(label, ".uno:Save") == 0;
50 if (window->lokdocview)
51 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), label, aArguments.c_str(), bNotify);
54 void doCopy(GtkWidget* pButton, gpointer /*pItem*/)
56 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
57 char* pUsedFormat = nullptr;
58 // TODO: Should check `text-selection` signal before trying to copy
59 char* pSelection = lok_doc_view_copy_selection(LOK_DOC_VIEW(window->lokdocview), "text/html", &pUsedFormat);
60 if (!pSelection)
61 return;
63 GtkClipboard* pClipboard = gtk_clipboard_get_for_display(gtk_widget_get_display(pButton), GDK_SELECTION_CLIPBOARD);
64 std::string aUsedFormat(pUsedFormat);
65 if (aUsedFormat == "text/plain;charset=utf-8")
66 gtk_clipboard_set_text(pClipboard, pSelection, -1);
67 else
68 GtvHelpers::clipboardSetHtml(pClipboard, pSelection);
70 free(pSelection);
71 free(pUsedFormat);
74 void doPaste(GtkWidget* pButton, gpointer /*pItem*/)
76 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
77 GtkClipboard* pClipboard = gtk_clipboard_get_for_display(gtk_widget_get_display(pButton), GDK_SELECTION_CLIPBOARD);
78 GdkAtom* pTargets;
79 gint nTargets;
80 std::map<std::string, GdkAtom> aTargets;
81 if (gtk_clipboard_wait_for_targets(pClipboard, &pTargets, &nTargets))
83 for (gint i = 0; i < nTargets; ++i)
85 gchar* pName = gdk_atom_name(pTargets[i]);
86 aTargets[pName] = pTargets[i];
87 g_free(pName);
89 g_free(pTargets);
92 std::optional<GdkAtom> oTarget;
93 std::string aTargetName;
95 std::vector<std::string> aPreferredNames =
97 std::string("image/png"),
98 std::string("text/html")
100 for (const std::string& rName : aPreferredNames)
102 std::map<std::string, GdkAtom>::iterator it = aTargets.find(rName);
103 if (it != aTargets.end())
105 aTargetName = it->first;
106 oTarget = it->second;
107 break;
111 if (oTarget)
113 GtkSelectionData* pSelectionData = gtk_clipboard_wait_for_contents(pClipboard, *oTarget);
114 if (!pSelectionData)
116 return;
118 gint nLength;
119 const guchar* pData = gtk_selection_data_get_data_with_length(pSelectionData, &nLength);
120 bool bSuccess = lok_doc_view_paste(LOK_DOC_VIEW(window->lokdocview), aTargetName.c_str(), reinterpret_cast<const char*>(pData), nLength);
121 gtk_selection_data_free(pSelectionData);
122 if (bSuccess)
123 return;
126 gchar* pText = gtk_clipboard_wait_for_text(pClipboard);
127 if (pText)
128 lok_doc_view_paste(LOK_DOC_VIEW(window->lokdocview), "text/plain;charset=utf-8", pText, strlen(pText));
131 void createView(GtkWidget* pButton, gpointer /*pItem*/)
133 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
134 gtv_application_window_create_view_from_window(GTV_APPLICATION_WINDOW(window));
137 void getRulerState(GtkWidget* pButton, gpointer /*pItem*/)
139 const std::string type = ".uno:RulerState";
140 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
141 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
142 pDocument->pClass->getCommandValues(pDocument, type.c_str());
145 static void removeUnoParam(GtkWidget* pWidget, gpointer userdata)
147 GtkWidget* pParamAreaBox = GTK_WIDGET(userdata);
148 GtkWidget* pParamContainer = gtk_widget_get_parent(pWidget);
150 gtk_container_remove(GTK_CONTAINER(pParamAreaBox), pParamContainer);
153 static void addMoreUnoParam(GtkWidget* /*pWidget*/, gpointer userdata)
155 GtkWidget* pUnoParamAreaBox = GTK_WIDGET(userdata);
157 GtkWidget* pParamContainer = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
158 gtk_box_pack_start(GTK_BOX(pUnoParamAreaBox), pParamContainer, true, true, 2);
160 GtkWidget* pTypeEntry = gtk_entry_new();
161 gtk_box_pack_start(GTK_BOX(pParamContainer), pTypeEntry, true, true, 2);
162 gtk_entry_set_placeholder_text(GTK_ENTRY(pTypeEntry), "Param type (Eg. boolean, string etc.)");
164 GtkWidget* pNameEntry = gtk_entry_new();
165 gtk_box_pack_start(GTK_BOX(pParamContainer), pNameEntry, true, true, 2);
166 gtk_entry_set_placeholder_text(GTK_ENTRY(pNameEntry), "Param name");
168 GtkWidget* pValueEntry = gtk_entry_new();
169 gtk_box_pack_start(GTK_BOX(pParamContainer), pValueEntry, true, true, 2);
170 gtk_entry_set_placeholder_text(GTK_ENTRY(pValueEntry), "Param value");
172 GtkWidget* pRemoveButton = gtk_button_new_from_icon_name("list-remove-symbolic", GTK_ICON_SIZE_BUTTON);
173 g_signal_connect(pRemoveButton, "clicked", G_CALLBACK(removeUnoParam), pUnoParamAreaBox);
174 gtk_box_pack_start(GTK_BOX(pParamContainer), pRemoveButton, true, true, 2);
176 gtk_widget_show_all(pUnoParamAreaBox);
179 static void iterateUnoParams(GtkWidget* pWidget, gpointer userdata)
181 boost::property_tree::ptree *pTree = static_cast<boost::property_tree::ptree*>(userdata);
182 GtvGtkWrapper<GList> pChildren(gtk_container_get_children(GTK_CONTAINER(pWidget)),
183 [](GList* pList) {
184 g_list_free(pList);
186 GList* pIt = nullptr;
187 guint i = 0;
188 const gchar* unoParam[3];
189 for (pIt = pChildren.get(), i = 0; pIt != nullptr && i < 3; pIt = pIt->next, i++)
191 unoParam[i] = gtk_entry_get_text(GTK_ENTRY(pIt->data));
194 gchar* pPath = g_strconcat(unoParam[1], "/", "type", nullptr);
195 pTree->put(boost::property_tree::ptree::path_type(pPath, '/'), unoParam[0]);
196 g_free(pPath);
197 pPath = g_strconcat(unoParam[1], "/", "value", nullptr);
198 pTree->put(boost::property_tree::ptree::path_type(pPath, '/'), unoParam[2]);
199 g_free(pPath);
202 void recentUnoChanged( GtkWidget* pSelector, gpointer /* pItem */ )
204 GtvApplicationWindow* pWindow = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector));
205 gchar* pUnoCmd = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(pSelector));
207 GtvMainToolbar* pToolbar = gtv_application_window_get_main_toolbar(pWindow);
208 const std::string aUnoArgs = gtv_main_toolbar_get_recent_uno_args(pToolbar, pUnoCmd);
209 // this will also discard our default placeholder string, "Recent UNO"
210 if (aUnoArgs.empty())
211 return;
213 lok_doc_view_post_command(LOK_DOC_VIEW(pWindow->lokdocview), pUnoCmd, (aUnoArgs.empty() ? nullptr : aUnoArgs.c_str()), false);
214 g_free(pUnoCmd);
217 static void addToRecentUnoCommands(GtvApplicationWindow* pWindow, const std::string& rUnoCmd, std::string rArgs)
219 GtvMainToolbar* pToolbar = gtv_application_window_get_main_toolbar(pWindow);
220 rArgs.erase(std::find(rArgs.begin(), rArgs.end(), '\n'));
221 const std::string rUnoCmdStr = rUnoCmd + " | " + rArgs;
224 // add to file
225 std::ofstream outfile("/tmp/gtv-recentunos.txt", std::ios_base::app | std::ios_base::out);
226 if (outfile.good())
227 outfile << rUnoCmdStr << '\n';
229 // add to combo box
230 gtv_main_toolbar_add_recent_uno(pToolbar, rUnoCmdStr);
233 void unoCommandDebugger(GtkWidget* pButton, gpointer /* pItem */)
235 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
236 GtkWidget* pUnoCmdDialog = gtk_dialog_new_with_buttons ("Execute UNO command",
237 GTK_WINDOW (window),
238 GTK_DIALOG_MODAL,
239 "Execute",
240 GTK_RESPONSE_OK,
241 nullptr);
242 g_object_set(G_OBJECT(pUnoCmdDialog), "resizable", FALSE, nullptr);
243 GtkWidget* pDialogMessageArea = gtk_dialog_get_content_area (GTK_DIALOG (pUnoCmdDialog));
244 GtkWidget* pUnoCmdAreaBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
245 gtk_box_pack_start(GTK_BOX(pDialogMessageArea), pUnoCmdAreaBox, true, true, 2);
247 GtkWidget* pUnoCmdLabel = gtk_label_new("Enter UNO command");
248 gtk_box_pack_start(GTK_BOX(pUnoCmdAreaBox), pUnoCmdLabel, true, true, 2);
250 GtkWidget* pUnoCmdEntry = gtk_entry_new ();
251 gtk_box_pack_start(GTK_BOX(pUnoCmdAreaBox), pUnoCmdEntry, true, true, 2);
252 gtk_entry_set_placeholder_text(GTK_ENTRY(pUnoCmdEntry), "UNO command (Eg. Bold, Italic etc.)");
253 GtkWidget* pUnoParamAreaBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
254 gtk_box_pack_start(GTK_BOX(pDialogMessageArea), pUnoParamAreaBox, true, true, 2);
256 GtkWidget* pAddMoreButton = gtk_button_new_with_label("Add UNO parameter");
257 gtk_box_pack_start(GTK_BOX(pDialogMessageArea), pAddMoreButton, true, true, 2);
258 g_signal_connect(G_OBJECT(pAddMoreButton), "clicked", G_CALLBACK(addMoreUnoParam), pUnoParamAreaBox);
260 gtk_widget_show_all(pUnoCmdDialog);
262 gint res = gtk_dialog_run (GTK_DIALOG(pUnoCmdDialog));
263 if (res == GTK_RESPONSE_OK)
265 gchar* sUnoCmd = g_strconcat(".uno:", gtk_entry_get_text(GTK_ENTRY(pUnoCmdEntry)), nullptr);
267 boost::property_tree::ptree aTree;
268 gtk_container_foreach(GTK_CONTAINER(pUnoParamAreaBox), iterateUnoParams, &aTree);
270 std::stringstream aStream;
271 boost::property_tree::write_json(aStream, aTree, false);
272 std::string aArguments = aStream.str();
274 g_info("Generated UNO command: %s %s", sUnoCmd, aArguments.c_str());
276 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), sUnoCmd, (aArguments.empty() ? nullptr : aArguments.c_str()), false);
277 addToRecentUnoCommands(window, sUnoCmd, aArguments);
279 g_free(sUnoCmd);
282 gtk_widget_destroy(pUnoCmdDialog);
285 void toggleEditing(GtkWidget* pButton, gpointer /*pItem*/)
287 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
288 bool bActive = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(pButton));
289 if (bool(lok_doc_view_get_edit(LOK_DOC_VIEW(window->lokdocview))) != bActive)
290 lok_doc_view_set_edit(LOK_DOC_VIEW(window->lokdocview), bActive);
293 void changePart( GtkWidget* pSelector, gpointer /* pItem */ )
295 int nPart = gtk_combo_box_get_active( GTK_COMBO_BOX(pSelector) );
296 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector));
298 if (gtv_application_window_get_part_broadcast(window) && window->lokdocview)
300 lok_doc_view_set_part( LOK_DOC_VIEW(window->lokdocview), nPart );
301 lok_doc_view_reset_view(LOK_DOC_VIEW(window->lokdocview));
305 void changePartMode( GtkWidget* pSelector, gpointer /* pItem */ )
307 // Just convert directly back to the LibreOfficeKitPartMode enum.
308 // I.e. the ordering above should match the enum member ordering.
309 LibreOfficeKitPartMode ePartMode =
310 LibreOfficeKitPartMode( gtk_combo_box_get_active( GTK_COMBO_BOX(pSelector) ) );
311 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector));
313 if ( window->lokdocview )
315 lok_doc_view_set_partmode( LOK_DOC_VIEW(window->lokdocview), ePartMode );
319 void changeZoom( GtkWidget* pButton, gpointer /* pItem */ )
321 static const float fZooms[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 5.0 };
322 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
323 const char *sName = gtk_tool_button_get_icon_name( GTK_TOOL_BUTTON(pButton) );
325 float fZoom = 0;
326 float fCurrentZoom = 0;
328 if ( window->lokdocview )
330 fCurrentZoom = lok_doc_view_get_zoom( LOK_DOC_VIEW(window->lokdocview) );
333 if ( strcmp(sName, "zoom-in-symbolic") == 0)
335 for ( size_t i = 0; i < SAL_N_ELEMENTS( fZooms ); i++ )
337 if ( fCurrentZoom < fZooms[i] )
339 fZoom = fZooms[i];
340 break;
344 else if ( strcmp(sName, "zoom-original-symbolic") == 0)
346 fZoom = 1;
348 else if ( strcmp(sName, "zoom-out-symbolic") == 0)
350 for ( size_t i = 0; i < SAL_N_ELEMENTS( fZooms ); i++ )
352 if ( fCurrentZoom > fZooms[i] )
354 fZoom = fZooms[i];
359 if ( fZoom != 0 && window->lokdocview )
361 lok_doc_view_set_zoom( LOK_DOC_VIEW(window->lokdocview), fZoom );
362 GdkRectangle aVisibleArea;
363 gtv_application_window_get_visible_area(window, &aVisibleArea);
364 lok_doc_view_set_visible_area(LOK_DOC_VIEW(window->lokdocview), &aVisibleArea);
366 const std::string aZoom = std::string("Zoom: ") + std::to_string(int(fZoom * 100)) + std::string("%");
367 gtk_label_set_text(GTK_LABEL(window->zoomlabel), aZoom.c_str());
370 void documentRedline(GtkWidget* pButton, gpointer /*pItem*/)
372 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
373 // Get the data.
374 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
375 char* pValues = pDocument->pClass->getCommandValues(pDocument, ".uno:AcceptTrackedChanges");
376 if (!pValues)
377 return;
379 std::stringstream aInfo;
380 aInfo << "lok::Document::getCommandValues('.uno:AcceptTrackedChanges') returned '" << pValues << "'" << std::endl;
381 g_info("%s", aInfo.str().c_str());
382 std::stringstream aStream(pValues);
383 free(pValues);
384 assert(!aStream.str().empty());
385 boost::property_tree::ptree aTree;
386 boost::property_tree::read_json(aStream, aTree);
388 // Create the dialog.
389 GtkWidget* pDialog = gtk_dialog_new_with_buttons("Manage Changes",
390 GTK_WINDOW (window),
391 GTK_DIALOG_MODAL,
392 "Accept",
393 GTK_RESPONSE_YES,
394 "Reject",
395 GTK_RESPONSE_NO,
396 "Jump",
397 GTK_RESPONSE_APPLY,
398 nullptr);
399 gtk_window_set_default_size(GTK_WINDOW(pDialog), 800, 600);
400 GtkWidget* pContentArea = gtk_dialog_get_content_area(GTK_DIALOG (pDialog));
401 GtkWidget* pScrolledWindow = gtk_scrolled_window_new(nullptr, nullptr);
403 // Build the table.
404 GtkTreeStore* pTreeStore = gtk_tree_store_new(6, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
405 for (const auto& rValue : aTree.get_child("redlines"))
407 GtkTreeIter aTreeIter;
408 gtk_tree_store_append(pTreeStore, &aTreeIter, nullptr);
409 gtk_tree_store_set(pTreeStore, &aTreeIter,
410 0, rValue.second.get<int>("index"),
411 1, rValue.second.get<std::string>("author").c_str(),
412 2, rValue.second.get<std::string>("type").c_str(),
413 3, rValue.second.get<std::string>("comment").c_str(),
414 4, rValue.second.get<std::string>("description").c_str(),
415 5, rValue.second.get<std::string>("dateTime").c_str(),
416 -1);
418 GtkWidget* pTreeView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pTreeStore));
419 std::vector<std::string> aColumns = {"Index", "Author", "Type", "Comment", "Description", "Timestamp"};
420 for (size_t nColumn = 0; nColumn < aColumns.size(); ++nColumn)
422 GtkCellRenderer* pRenderer = gtk_cell_renderer_text_new();
423 GtkTreeViewColumn* pColumn = gtk_tree_view_column_new_with_attributes(aColumns[nColumn].c_str(),
424 pRenderer,
425 "text", nColumn,
426 nullptr);
427 gtk_tree_view_append_column(GTK_TREE_VIEW(pTreeView), pColumn);
429 gtk_container_add(GTK_CONTAINER(pScrolledWindow), pTreeView);
430 gtk_box_pack_start(GTK_BOX(pContentArea), pScrolledWindow, true, true, 2);
432 // Show the dialog.
433 gtk_widget_show_all(pDialog);
434 gint res = gtk_dialog_run(GTK_DIALOG(pDialog));
436 // Dispatch the matching command, if necessary.
437 if (res == GTK_RESPONSE_YES || res == GTK_RESPONSE_NO || res == GTK_RESPONSE_APPLY)
439 GtkTreeSelection* pSelection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pTreeView));
440 GtkTreeIter aTreeIter;
441 GtkTreeModel* pTreeModel;
442 if (gtk_tree_selection_get_selected(pSelection, &pTreeModel, &aTreeIter))
444 gint nIndex = 0;
445 // 0: index
446 gtk_tree_model_get(pTreeModel, &aTreeIter, 0, &nIndex, -1);
447 std::string aCommand;
448 if (res == GTK_RESPONSE_YES)
449 aCommand = ".uno:AcceptTrackedChange";
450 else if (res == GTK_RESPONSE_NO)
451 aCommand = ".uno:RejectTrackedChange";
452 else
453 // Just select the given redline, don't accept or reject it.
454 aCommand = ".uno:NextTrackedChange";
455 // Without the '.uno:' prefix.
456 std::string aKey = aCommand.substr(strlen(".uno:"));
458 // Post the command.
459 boost::property_tree::ptree aCommandTree;
460 aCommandTree.put(boost::property_tree::ptree::path_type(aKey + "/type", '/'), "unsigned short");
461 aCommandTree.put(boost::property_tree::ptree::path_type(aKey + "/value", '/'), nIndex);
463 aStream.str(std::string());
464 boost::property_tree::write_json(aStream, aCommandTree);
465 std::string aArguments = aStream.str();
466 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), aCommand.c_str(), aArguments.c_str(), false);
470 gtk_widget_destroy(pDialog);
473 void documentRepair(GtkWidget* pButton, gpointer /*pItem*/)
475 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
476 // Get the data.
477 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
478 // Show it in linear time, so first redo in reverse order, then undo.
479 std::vector<std::string> aTypes = {".uno:Redo", ".uno:Undo"};
480 std::vector<boost::property_tree::ptree> aTrees;
481 for (size_t nType = 0; nType < aTypes.size(); ++nType)
483 const std::string& rType = aTypes[nType];
484 char* pValues = pDocument->pClass->getCommandValues(pDocument, rType.c_str());
485 std::stringstream aInfo;
486 aInfo << "lok::Document::getCommandValues('" << rType << "') returned '" << pValues << "'" << std::endl;
487 g_info("%s", aInfo.str().c_str());
488 std::stringstream aStream(pValues);
489 free(pValues);
490 assert(!aStream.str().empty());
491 boost::property_tree::ptree aTree;
492 boost::property_tree::read_json(aStream, aTree);
493 aTrees.push_back(aTree);
496 // Create the dialog.
497 GtkWidget* pDialog = gtk_dialog_new_with_buttons("Repair document",
498 GTK_WINDOW (window),
499 GTK_DIALOG_MODAL,
500 "Jump to state",
501 GTK_RESPONSE_OK,
502 nullptr);
503 gtk_window_set_default_size(GTK_WINDOW(pDialog), 800, 600);
504 GtkWidget* pContentArea = gtk_dialog_get_content_area(GTK_DIALOG (pDialog));
505 GtkWidget* pScrolledWindow = gtk_scrolled_window_new(nullptr, nullptr);
507 // Build the table.
508 GtkTreeStore* pTreeStore = gtk_tree_store_new(5, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
509 for (size_t nTree = 0; nTree < aTrees.size(); ++nTree)
511 const auto& rTree = aTrees[nTree];
512 for (const auto& rValue : rTree.get_child("actions"))
514 GtkTreeIter aTreeIter;
515 gtk_tree_store_append(pTreeStore, &aTreeIter, nullptr);
516 gtk_tree_store_set(pTreeStore, &aTreeIter,
517 0, aTypes[nTree].c_str(),
518 1, rValue.second.get<int>("index"),
519 2, rValue.second.get<std::string>("comment").c_str(),
520 3, rValue.second.get<std::string>("viewId").c_str(),
521 4, rValue.second.get<std::string>("dateTime").c_str(),
522 -1);
525 GtkWidget* pTreeView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pTreeStore));
526 std::vector<std::string> aColumns = {"Type", "Index", "Comment", "View ID", "Timestamp"};
527 for (size_t nColumn = 0; nColumn < aColumns.size(); ++nColumn)
529 GtkCellRenderer* pRenderer = gtk_cell_renderer_text_new();
530 GtkTreeViewColumn* pColumn = gtk_tree_view_column_new_with_attributes(aColumns[nColumn].c_str(),
531 pRenderer,
532 "text", nColumn,
533 nullptr);
534 gtk_tree_view_append_column(GTK_TREE_VIEW(pTreeView), pColumn);
536 gtk_container_add(GTK_CONTAINER(pScrolledWindow), pTreeView);
537 gtk_box_pack_start(GTK_BOX(pContentArea), pScrolledWindow, true, true, 2);
539 // Show the dialog.
540 gtk_widget_show_all(pDialog);
541 gint res = gtk_dialog_run(GTK_DIALOG(pDialog));
543 // Dispatch the matching command, if necessary.
544 if (res == GTK_RESPONSE_OK)
546 GtkTreeSelection* pSelection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pTreeView));
547 GtkTreeIter aTreeIter;
548 GtkTreeModel* pTreeModel;
549 if (gtk_tree_selection_get_selected(pSelection, &pTreeModel, &aTreeIter))
551 gchar* pType = nullptr;
552 gint nIndex = 0;
553 // 0: type, 1: index
554 gtk_tree_model_get(pTreeModel, &aTreeIter, 0, &pType, 1, &nIndex, -1);
555 // '.uno:Undo' or '.uno:Redo'
556 const std::string aType(pType);
557 // Without the '.uno:' prefix.
558 std::string aKey = aType.substr(strlen(".uno:"));
559 g_free(pType);
561 // Post the command.
562 boost::property_tree::ptree aTree;
563 aTree.put(boost::property_tree::ptree::path_type(aKey + "/type", '/'), "unsigned short");
564 aTree.put(boost::property_tree::ptree::path_type(aKey + "/value", '/'), nIndex + 1);
566 // Without this, we could only undo our own commands.
567 aTree.put(boost::property_tree::ptree::path_type("Repair/type", '/'), "boolean");
568 aTree.put(boost::property_tree::ptree::path_type("Repair/value", '/'), true);
570 std::stringstream aStream;
571 boost::property_tree::write_json(aStream, aTree);
572 std::string aArguments = aStream.str();
573 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), aType.c_str(), aArguments.c_str(), false);
577 gtk_widget_destroy(pDialog);
580 void toggleFindbar(GtkWidget* pButton, gpointer /*pItem*/)
582 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
583 gtv_application_window_toggle_findbar(window);
586 void docAdjustmentChanged(GtkAdjustment*, gpointer pData)
588 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(pData);
589 if (window->lokdocview)
590 LOKDocViewSigHandlers::configureEvent(window->lokdocview, nullptr, nullptr);
593 void signalSearchNext(GtkWidget* pButton, gpointer /*pItem*/)
595 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
596 GtkEntry* pEntry = GTK_ENTRY(window->findbarEntry);
597 const char* pText = gtk_entry_get_text(pEntry);
598 bool findAll = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(window->findAll));
599 lok_doc_view_find_next(LOK_DOC_VIEW(window->lokdocview), pText, findAll);
602 void signalSearchPrev(GtkWidget* pButton, gpointer /*pItem*/)
604 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
605 GtkEntry* pEntry = GTK_ENTRY(window->findbarEntry);
606 const char* pText = gtk_entry_get_text(pEntry);
607 bool findAll = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(window->findAll));
608 lok_doc_view_find_prev(LOK_DOC_VIEW(window->lokdocview), pText, findAll);
611 gboolean signalFindbar(GtkWidget* pWidget, GdkEventKey* pEvent, gpointer /*pData*/)
613 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget));
614 gtk_label_set_text(GTK_LABEL(window->findbarlabel), "");
615 switch(pEvent->keyval)
617 case GDK_KEY_Return:
619 // Search forward.
620 signalSearchNext(pWidget, nullptr);
621 return true;
623 case GDK_KEY_Escape:
625 // Hide the findbar.
626 gtk_widget_hide(GTK_WIDGET(window->findtoolbar));
627 return true;
630 return FALSE;
633 void toggleFindAll(GtkWidget* pButton, gpointer /*pItem*/)
635 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton));
636 GtkEntry* pEntry = GTK_ENTRY(window->findbarEntry);
637 const char* pText = gtk_entry_get_text(pEntry);
638 bool findAll = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(window->findAll));
639 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(window->findAll), !findAll);
640 lok_doc_view_highlight_all(LOK_DOC_VIEW(window->lokdocview), pText);
643 void editButtonClicked(GtkWidget* pWidget, gpointer userdata)
645 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget));
646 std::map<std::string, std::string> aEntries;
647 aEntries["Text"] = "";
649 GtvHelpers::userPromptDialog(GTK_WINDOW(window), "Edit comment", aEntries);
651 gchar *commentId = static_cast<gchar*>(g_object_get_data(G_OBJECT(userdata), "id"));
653 boost::property_tree::ptree aTree;
654 aTree.put(boost::property_tree::ptree::path_type("Id/type", '/'), "string");
655 aTree.put(boost::property_tree::ptree::path_type("Id/value", '/'), std::string(commentId));
657 aTree.put(boost::property_tree::ptree::path_type("Text/type", '/'), "string");
658 aTree.put(boost::property_tree::ptree::path_type("Text/value", '/'), aEntries["Text"]);
660 std::stringstream aStream;
661 boost::property_tree::write_json(aStream, aTree);
662 std::string aArguments = aStream.str();
664 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), ".uno:EditAnnotation", aArguments.c_str(), false);
667 void replyButtonClicked(GtkWidget* pWidget, gpointer userdata)
669 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget));
670 std::map<std::string, std::string> aEntries;
671 aEntries["Text"] = "";
673 GtvHelpers::userPromptDialog(GTK_WINDOW(window), "Reply comment", aEntries);
675 gchar *commentId = static_cast<gchar*>(g_object_get_data(G_OBJECT(userdata), "id"));
677 boost::property_tree::ptree aTree;
678 aTree.put(boost::property_tree::ptree::path_type("Id/type", '/'), "string");
679 aTree.put(boost::property_tree::ptree::path_type("Id/value", '/'), std::string(commentId));
681 aTree.put(boost::property_tree::ptree::path_type("Text/type", '/'), "string");
682 aTree.put(boost::property_tree::ptree::path_type("Text/value", '/'), aEntries["Text"]);
684 std::stringstream aStream;
685 boost::property_tree::write_json(aStream, aTree);
686 std::string aArguments = aStream.str();
688 // Different reply UNO command for impress
689 std::string replyCommand = ".uno:ReplyComment";
690 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
691 if (pDocument && pDocument->pClass->getDocumentType(pDocument) == LOK_DOCTYPE_PRESENTATION)
692 replyCommand = ".uno:ReplyToAnnotation";
693 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), replyCommand.c_str(), aArguments.c_str(), false);
696 void deleteCommentButtonClicked(GtkWidget* pWidget, gpointer userdata)
698 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget));
699 gchar *commentid = static_cast<gchar*>(g_object_get_data(G_OBJECT(userdata), "id"));
701 boost::property_tree::ptree aTree;
702 aTree.put(boost::property_tree::ptree::path_type("Id/type", '/'), "string");
703 aTree.put(boost::property_tree::ptree::path_type("Id/value", '/'), std::string(commentid));
705 std::stringstream aStream;
706 boost::property_tree::write_json(aStream, aTree);
707 std::string aArguments = aStream.str();
709 // Different reply UNO command for impress
710 std::string deleteCommand = ".uno:DeleteComment";
711 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
712 if (pDocument)
714 if (pDocument->pClass->getDocumentType(pDocument) == LOK_DOCTYPE_PRESENTATION)
715 deleteCommand = ".uno:DeleteAnnotation";
716 else if (pDocument->pClass->getDocumentType(pDocument) == LOK_DOCTYPE_SPREADSHEET)
717 deleteCommand = ".uno:DeleteNote";
720 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), deleteCommand.c_str(), aArguments.c_str(), false);
723 /// Handles the key-press-event of the address entry widget.
724 gboolean signalAddressbar(GtkWidget* pWidget, GdkEventKey* pEvent, gpointer /*pData*/)
726 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget));
727 switch(pEvent->keyval)
729 case GDK_KEY_Return:
731 GtkEntry* pEntry = GTK_ENTRY(pWidget);
732 const char* pText = gtk_entry_get_text(pEntry);
734 boost::property_tree::ptree aTree;
735 aTree.put(boost::property_tree::ptree::path_type("ToPoint/type", '/'), "string");
736 aTree.put(boost::property_tree::ptree::path_type("ToPoint/value", '/'), pText);
737 std::stringstream aStream;
738 boost::property_tree::write_json(aStream, aTree);
739 std::string aArguments = aStream.str();
741 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), ".uno:GoToCell", aArguments.c_str(), false);
742 gtk_widget_grab_focus(window->lokdocview);
743 return true;
745 case GDK_KEY_Escape:
747 std::string aArguments;
748 lok_doc_view_post_command(LOK_DOC_VIEW(window->lokdocview), ".uno:Cancel", aArguments.c_str(), false);
749 gtk_widget_grab_focus(window->lokdocview);
750 return true;
753 return FALSE;
756 /// Handles the key-press-event of the formula entry widget.
757 gboolean signalFormulabar(GtkWidget* /*pWidget*/, GdkEventKey* /*pEvent*/, gpointer /*pData*/)
759 // for now it just displays the callback
760 // TODO - submit the edited formula
761 return true;
764 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */