1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
23 #include <boost/property_tree/json_parser.hpp>
26 void btn_clicked(GtkWidget
* pButton
, gpointer
)
28 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
29 GtkToolButton
* pItem
= GTK_TOOL_BUTTON(pButton
);
30 const gchar
* label
= gtk_tool_button_get_label(pItem
);
31 if (!(gtv_application_window_get_toolbar_broadcast(window
) && g_str_has_prefix(label
, ".uno:")))
34 std::string aArguments
;
35 if (g_strcmp0(label
, ".uno:InsertAnnotation") == 0)
37 std::map
<std::string
, std::string
> aEntries
;
38 aEntries
["Text"] = "";
39 GtvHelpers::userPromptDialog(GTK_WINDOW(window
), "Insert Comment", aEntries
);
41 boost::property_tree::ptree aTree
;
42 aTree
.put(boost::property_tree::ptree::path_type("Text/type", '/'), "string");
43 aTree
.put(boost::property_tree::ptree::path_type("Text/value", '/'), aEntries
["Text"]);
45 std::stringstream aStream
;
46 boost::property_tree::write_json(aStream
, aTree
);
47 aArguments
= aStream
.str();
50 bool bNotify
= g_strcmp0(label
, ".uno:Save") == 0;
51 if (window
->lokdocview
)
52 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), label
, aArguments
.c_str(), bNotify
);
55 void doCopy(GtkWidget
* pButton
, gpointer
/*pItem*/)
57 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
58 char* pUsedFormat
= nullptr;
59 // TODO: Should check `text-selection` signal before trying to copy
60 char* pSelection
= lok_doc_view_copy_selection(LOK_DOC_VIEW(window
->lokdocview
), "text/html", &pUsedFormat
);
64 GtkClipboard
* pClipboard
= gtk_clipboard_get_for_display(gtk_widget_get_display(pButton
), GDK_SELECTION_CLIPBOARD
);
65 std::string
aUsedFormat(pUsedFormat
);
66 if (aUsedFormat
== "text/plain;charset=utf-8")
67 gtk_clipboard_set_text(pClipboard
, pSelection
, -1);
69 GtvHelpers::clipboardSetHtml(pClipboard
, pSelection
);
75 void doPaste(GtkWidget
* pButton
, gpointer
/*pItem*/)
77 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
78 GtkClipboard
* pClipboard
= gtk_clipboard_get_for_display(gtk_widget_get_display(pButton
), GDK_SELECTION_CLIPBOARD
);
81 std::map
<std::string
, GdkAtom
> aTargets
;
82 if (gtk_clipboard_wait_for_targets(pClipboard
, &pTargets
, &nTargets
))
84 for (gint i
= 0; i
< nTargets
; ++i
)
86 gchar
* pName
= gdk_atom_name(pTargets
[i
]);
87 aTargets
[pName
] = pTargets
[i
];
93 std::optional
<GdkAtom
> oTarget
;
94 std::string aTargetName
;
96 std::vector
<std::string
> aPreferredNames
=
98 std::string("image/png"),
99 std::string("text/html")
101 for (const std::string
& rName
: aPreferredNames
)
103 std::map
<std::string
, GdkAtom
>::iterator it
= aTargets
.find(rName
);
104 if (it
!= aTargets
.end())
106 aTargetName
= it
->first
;
107 oTarget
= it
->second
;
114 GtkSelectionData
* pSelectionData
= gtk_clipboard_wait_for_contents(pClipboard
, *oTarget
);
120 const guchar
* pData
= gtk_selection_data_get_data_with_length(pSelectionData
, &nLength
);
121 bool bSuccess
= lok_doc_view_paste(LOK_DOC_VIEW(window
->lokdocview
), aTargetName
.c_str(), reinterpret_cast<const char*>(pData
), nLength
);
122 gtk_selection_data_free(pSelectionData
);
127 gchar
* pText
= gtk_clipboard_wait_for_text(pClipboard
);
129 lok_doc_view_paste(LOK_DOC_VIEW(window
->lokdocview
), "text/plain;charset=utf-8", pText
, strlen(pText
));
132 void createView(GtkWidget
* pButton
, gpointer
/*pItem*/)
134 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
135 gtv_application_window_create_view_from_window(GTV_APPLICATION_WINDOW(window
));
138 void getRulerState(GtkWidget
* pButton
, gpointer
/*pItem*/)
140 const std::string type
= ".uno:RulerState";
141 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
142 LibreOfficeKitDocument
* pDocument
= lok_doc_view_get_document(LOK_DOC_VIEW(window
->lokdocview
));
143 pDocument
->pClass
->getCommandValues(pDocument
, type
.c_str());
146 static void removeUnoParam(GtkWidget
* pWidget
, gpointer userdata
)
148 GtkWidget
* pParamAreaBox
= GTK_WIDGET(userdata
);
149 GtkWidget
* pParamContainer
= gtk_widget_get_parent(pWidget
);
151 gtk_container_remove(GTK_CONTAINER(pParamAreaBox
), pParamContainer
);
154 static void addMoreUnoParam(GtkWidget
* /*pWidget*/, gpointer userdata
)
156 GtkWidget
* pUnoParamAreaBox
= GTK_WIDGET(userdata
);
158 GtkWidget
* pParamContainer
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
159 gtk_box_pack_start(GTK_BOX(pUnoParamAreaBox
), pParamContainer
, true, true, 2);
161 GtkWidget
* pTypeEntry
= gtk_entry_new();
162 gtk_box_pack_start(GTK_BOX(pParamContainer
), pTypeEntry
, true, true, 2);
163 gtk_entry_set_placeholder_text(GTK_ENTRY(pTypeEntry
), "Param type (Eg. boolean, string etc.)");
165 GtkWidget
* pNameEntry
= gtk_entry_new();
166 gtk_box_pack_start(GTK_BOX(pParamContainer
), pNameEntry
, true, true, 2);
167 gtk_entry_set_placeholder_text(GTK_ENTRY(pNameEntry
), "Param name");
169 GtkWidget
* pValueEntry
= gtk_entry_new();
170 gtk_box_pack_start(GTK_BOX(pParamContainer
), pValueEntry
, true, true, 2);
171 gtk_entry_set_placeholder_text(GTK_ENTRY(pValueEntry
), "Param value");
173 GtkWidget
* pRemoveButton
= gtk_button_new_from_icon_name("list-remove-symbolic", GTK_ICON_SIZE_BUTTON
);
174 g_signal_connect(pRemoveButton
, "clicked", G_CALLBACK(removeUnoParam
), pUnoParamAreaBox
);
175 gtk_box_pack_start(GTK_BOX(pParamContainer
), pRemoveButton
, true, true, 2);
177 gtk_widget_show_all(pUnoParamAreaBox
);
180 static void iterateUnoParams(GtkWidget
* pWidget
, gpointer userdata
)
182 boost::property_tree::ptree
*pTree
= static_cast<boost::property_tree::ptree
*>(userdata
);
183 GtvGtkWrapper
<GList
> pChildren(gtk_container_get_children(GTK_CONTAINER(pWidget
)),
187 GList
* pIt
= nullptr;
189 const gchar
* unoParam
[3];
190 for (pIt
= pChildren
.get(), i
= 0; i
< 3; pIt
= pIt
->next
, i
++)
192 assert(pIt
!= nullptr);
193 unoParam
[i
] = gtk_entry_get_text(GTK_ENTRY(pIt
->data
));
196 gchar
* pPath
= g_strconcat(unoParam
[1], "/", "type", nullptr);
197 pTree
->put(boost::property_tree::ptree::path_type(pPath
, '/'), unoParam
[0]);
199 pPath
= g_strconcat(unoParam
[1], "/", "value", nullptr);
200 pTree
->put(boost::property_tree::ptree::path_type(pPath
, '/'), unoParam
[2]);
204 void recentUnoChanged( GtkWidget
* pSelector
, gpointer
/* pItem */ )
206 GtvApplicationWindow
* pWindow
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector
));
207 gchar
* pUnoCmd
= gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(pSelector
));
209 GtvMainToolbar
* pToolbar
= gtv_application_window_get_main_toolbar(pWindow
);
210 const std::string aUnoArgs
= gtv_main_toolbar_get_recent_uno_args(pToolbar
, pUnoCmd
);
211 // this will also discard our default placeholder string, "Recent UNO"
212 if (aUnoArgs
.empty())
215 lok_doc_view_post_command(LOK_DOC_VIEW(pWindow
->lokdocview
), pUnoCmd
, aUnoArgs
.c_str(), false);
219 static void addToRecentUnoCommands(GtvApplicationWindow
* pWindow
, const std::string
& rUnoCmd
, std::string aArgs
)
221 GtvMainToolbar
* pToolbar
= gtv_application_window_get_main_toolbar(pWindow
);
222 auto it
= std::find(aArgs
.begin(), aArgs
.end(), '\n');
223 if (it
!= aArgs
.end())
225 const std::string rUnoCmdStr
= rUnoCmd
+ " | " + aArgs
;
229 std::ofstream
outfile("/tmp/gtv-recentunos.txt", std::ios_base::app
| std::ios_base::out
);
231 outfile
<< rUnoCmdStr
<< '\n';
234 gtv_main_toolbar_add_recent_uno(pToolbar
, rUnoCmdStr
);
237 void unoCommandDebugger(GtkWidget
* pButton
, gpointer
/* pItem */)
239 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
240 GtkWidget
* pUnoCmdDialog
= gtk_dialog_new_with_buttons ("Execute UNO command",
246 g_object_set(G_OBJECT(pUnoCmdDialog
), "resizable", FALSE
, nullptr);
247 GtkWidget
* pDialogMessageArea
= gtk_dialog_get_content_area (GTK_DIALOG (pUnoCmdDialog
));
248 GtkWidget
* pUnoCmdAreaBox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
249 gtk_box_pack_start(GTK_BOX(pDialogMessageArea
), pUnoCmdAreaBox
, true, true, 2);
251 GtkWidget
* pUnoCmdLabel
= gtk_label_new("Enter UNO command");
252 gtk_box_pack_start(GTK_BOX(pUnoCmdAreaBox
), pUnoCmdLabel
, true, true, 2);
254 GtkWidget
* pUnoCmdEntry
= gtk_entry_new ();
255 gtk_box_pack_start(GTK_BOX(pUnoCmdAreaBox
), pUnoCmdEntry
, true, true, 2);
256 gtk_entry_set_placeholder_text(GTK_ENTRY(pUnoCmdEntry
), "UNO command (Eg. Bold, Italic etc.)");
257 GtkWidget
* pUnoParamAreaBox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 0);
258 gtk_box_pack_start(GTK_BOX(pDialogMessageArea
), pUnoParamAreaBox
, true, true, 2);
260 GtkWidget
* pAddMoreButton
= gtk_button_new_with_label("Add UNO parameter");
261 gtk_box_pack_start(GTK_BOX(pDialogMessageArea
), pAddMoreButton
, true, true, 2);
262 g_signal_connect(G_OBJECT(pAddMoreButton
), "clicked", G_CALLBACK(addMoreUnoParam
), pUnoParamAreaBox
);
264 gtk_widget_show_all(pUnoCmdDialog
);
266 gint res
= gtk_dialog_run (GTK_DIALOG(pUnoCmdDialog
));
267 if (res
== GTK_RESPONSE_OK
)
269 gchar
* sUnoCmd
= g_strconcat(".uno:", gtk_entry_get_text(GTK_ENTRY(pUnoCmdEntry
)), nullptr);
271 boost::property_tree::ptree aTree
;
272 gtk_container_foreach(GTK_CONTAINER(pUnoParamAreaBox
), iterateUnoParams
, &aTree
);
274 std::stringstream aStream
;
275 boost::property_tree::write_json(aStream
, aTree
, false);
276 std::string aArguments
= aStream
.str();
278 g_info("Generated UNO command: %s %s", sUnoCmd
, aArguments
.c_str());
280 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), sUnoCmd
, (aArguments
.empty() ? nullptr : aArguments
.c_str()), false);
281 addToRecentUnoCommands(window
, sUnoCmd
, std::move(aArguments
));
286 gtk_widget_destroy(pUnoCmdDialog
);
289 void commandValuesDebugger(GtkWidget
* pButton
, gpointer
/* pItem */)
291 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
292 GtkWidget
* pUnoCmdDialog
= gtk_dialog_new_with_buttons ("Get command values",
298 g_object_set(G_OBJECT(pUnoCmdDialog
), "resizable", FALSE
, nullptr);
299 GtkWidget
* pDialogMessageArea
= gtk_dialog_get_content_area (GTK_DIALOG (pUnoCmdDialog
));
300 GtkWidget
* pUnoCmdAreaBox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
301 gtk_box_pack_start(GTK_BOX(pDialogMessageArea
), pUnoCmdAreaBox
, true, true, 2);
303 GtkWidget
* pUnoCmdLabel
= gtk_label_new("Enter UNO command");
304 gtk_box_pack_start(GTK_BOX(pUnoCmdAreaBox
), pUnoCmdLabel
, true, true, 2);
306 GtkWidget
* pUnoCmdEntry
= gtk_entry_new ();
307 gtk_box_pack_start(GTK_BOX(pUnoCmdAreaBox
), pUnoCmdEntry
, true, true, 2);
308 gtk_entry_set_placeholder_text(GTK_ENTRY(pUnoCmdEntry
), "e.g. .uno:Undo");
310 gtk_widget_show_all(pUnoCmdDialog
);
312 gint res
= gtk_dialog_run (GTK_DIALOG(pUnoCmdDialog
));
313 if (res
== GTK_RESPONSE_OK
)
315 const gchar
* pUnoCmd
= gtk_entry_get_text(GTK_ENTRY(pUnoCmdEntry
));
316 gchar
* pValues
= lok_doc_view_get_command_values(LOK_DOC_VIEW(window
->lokdocview
), pUnoCmd
);
317 g_info("lok::Document::getCommandValues(%s) : %s", pUnoCmd
, pValues
);
321 gtk_widget_destroy(pUnoCmdDialog
);
324 void toggleEditing(GtkWidget
* pButton
, gpointer
/*pItem*/)
326 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
327 bool bActive
= gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(pButton
));
328 if (bool(lok_doc_view_get_edit(LOK_DOC_VIEW(window
->lokdocview
))) != bActive
)
329 lok_doc_view_set_edit(LOK_DOC_VIEW(window
->lokdocview
), bActive
);
332 void changePart( GtkWidget
* pSelector
, gpointer
/* pItem */ )
334 int nPart
= gtk_combo_box_get_active( GTK_COMBO_BOX(pSelector
) );
335 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector
));
337 if (gtv_application_window_get_part_broadcast(window
) && window
->lokdocview
)
339 lok_doc_view_set_part( LOK_DOC_VIEW(window
->lokdocview
), nPart
);
340 lok_doc_view_reset_view(LOK_DOC_VIEW(window
->lokdocview
));
344 void changePartMode( GtkWidget
* pSelector
, gpointer
/* pItem */ )
346 // Just convert directly back to the LibreOfficeKitPartMode enum.
347 // I.e. the ordering above should match the enum member ordering.
348 LibreOfficeKitPartMode ePartMode
=
349 LibreOfficeKitPartMode( gtk_combo_box_get_active( GTK_COMBO_BOX(pSelector
) ) );
350 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector
));
352 if ( window
->lokdocview
)
354 lok_doc_view_set_partmode( LOK_DOC_VIEW(window
->lokdocview
), ePartMode
);
358 void changeContentControl(GtkWidget
* pSelector
, gpointer
/*pItem*/)
360 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector
));
361 if (gtv_application_window_get_part_broadcast(window
) && window
->lokdocview
)
363 int nItem
= gtk_combo_box_get_active(GTK_COMBO_BOX(pSelector
));
364 boost::property_tree::ptree aValues
;
365 aValues
.put("type", "drop-down");
366 aValues
.put("selected", std::to_string(nItem
));
367 std::stringstream aStream
;
368 boost::property_tree::write_json(aStream
, aValues
);
369 std::string aJson
= aStream
.str();
370 lok_doc_view_send_content_control_event(LOK_DOC_VIEW(window
->lokdocview
), aJson
.c_str());
374 void changeDateContentControl(GtkWidget
* pSelector
, gpointer
/*pItem*/)
376 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pSelector
));
377 if (gtv_application_window_get_part_broadcast(window
) && window
->lokdocview
)
379 GtkPopover
* pPopover
= GTK_POPOVER(gtk_widget_get_parent(gtk_widget_get_parent(pSelector
)));
380 guint nYear
, nMonth
, nDay
;
381 gtk_calendar_get_date(GTK_CALENDAR(pSelector
), &nYear
, &nMonth
, &nDay
);
382 gtk_popover_popdown(pPopover
);
384 std::stringstream aDate
;
385 aDate
<< std::setfill('0') << std::setw(4) << nYear
;
387 aDate
<< std::setfill('0') << std::setw(2) << (nMonth
+ 1);
389 aDate
<< std::setfill('0') << std::setw(2) << nDay
;
390 aDate
<< "T00:00:00Z";
391 boost::property_tree::ptree aValues
;
392 aValues
.put("type", "date");
393 aValues
.put("selected", aDate
.str());
394 std::stringstream aStream
;
395 boost::property_tree::write_json(aStream
, aValues
);
396 std::string aJson
= aStream
.str();
397 lok_doc_view_send_content_control_event(LOK_DOC_VIEW(window
->lokdocview
), aJson
.c_str());
401 void changeZoom( GtkWidget
* pButton
, gpointer
/* pItem */ )
403 static const float fZooms
[] = { 0.25, 0.5, 0.75, 1.0, 1.5, 2.0, 3.0, 5.0 };
404 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
405 const char *sName
= gtk_tool_button_get_icon_name( GTK_TOOL_BUTTON(pButton
) );
408 float fCurrentZoom
= 0;
410 if ( window
->lokdocview
)
412 fCurrentZoom
= lok_doc_view_get_zoom( LOK_DOC_VIEW(window
->lokdocview
) );
415 if ( strcmp(sName
, "zoom-in-symbolic") == 0)
417 for ( const auto& i
: fZooms
)
419 if ( fCurrentZoom
< i
)
426 else if ( strcmp(sName
, "zoom-original-symbolic") == 0)
430 else if ( strcmp(sName
, "zoom-out-symbolic") == 0)
432 for ( const auto& i
: fZooms
)
434 if ( fCurrentZoom
> i
)
441 if ( fZoom
!= 0 && window
->lokdocview
)
443 lok_doc_view_set_zoom( LOK_DOC_VIEW(window
->lokdocview
), fZoom
);
444 GdkRectangle aVisibleArea
;
445 gtv_application_window_get_visible_area(window
, &aVisibleArea
);
446 lok_doc_view_set_visible_area(LOK_DOC_VIEW(window
->lokdocview
), &aVisibleArea
);
448 const std::string aZoom
= std::string("Zoom: ") + std::to_string(int(fZoom
* 100)) + std::string("%");
449 gtk_label_set_text(GTK_LABEL(window
->zoomlabel
), aZoom
.c_str());
452 void documentRedline(GtkWidget
* pButton
, gpointer
/*pItem*/)
454 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
456 LibreOfficeKitDocument
* pDocument
= lok_doc_view_get_document(LOK_DOC_VIEW(window
->lokdocview
));
457 char* pValues
= pDocument
->pClass
->getCommandValues(pDocument
, ".uno:AcceptTrackedChanges");
461 std::stringstream aInfo
;
462 aInfo
<< "lok::Document::getCommandValues('.uno:AcceptTrackedChanges') returned '" << pValues
<< "'" << std::endl
;
463 g_info("%s", aInfo
.str().c_str());
464 std::stringstream
aStream(pValues
);
466 assert(!aStream
.str().empty());
467 boost::property_tree::ptree aTree
;
468 boost::property_tree::read_json(aStream
, aTree
);
470 // Create the dialog.
471 GtkWidget
* pDialog
= gtk_dialog_new_with_buttons("Manage Changes",
481 gtk_window_set_default_size(GTK_WINDOW(pDialog
), 800, 600);
482 GtkWidget
* pContentArea
= gtk_dialog_get_content_area(GTK_DIALOG (pDialog
));
483 GtkWidget
* pScrolledWindow
= gtk_scrolled_window_new(nullptr, nullptr);
486 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
);
487 for (const auto& rValue
: aTree
.get_child("redlines"))
489 GtkTreeIter aTreeIter
;
490 gtk_tree_store_append(pTreeStore
, &aTreeIter
, nullptr);
491 gtk_tree_store_set(pTreeStore
, &aTreeIter
,
492 0, rValue
.second
.get
<int>("index"),
493 1, rValue
.second
.get
<std::string
>("author").c_str(),
494 2, rValue
.second
.get
<std::string
>("type").c_str(),
495 3, rValue
.second
.get
<std::string
>("comment").c_str(),
496 4, rValue
.second
.get
<std::string
>("description").c_str(),
497 5, rValue
.second
.get
<std::string
>("dateTime").c_str(),
500 GtkWidget
* pTreeView
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(pTreeStore
));
501 std::vector
<std::string
> aColumns
= {"Index", "Author", "Type", "Comment", "Description", "Timestamp"};
502 for (size_t nColumn
= 0; nColumn
< aColumns
.size(); ++nColumn
)
504 GtkCellRenderer
* pRenderer
= gtk_cell_renderer_text_new();
505 GtkTreeViewColumn
* pColumn
= gtk_tree_view_column_new_with_attributes(aColumns
[nColumn
].c_str(),
509 gtk_tree_view_append_column(GTK_TREE_VIEW(pTreeView
), pColumn
);
511 gtk_container_add(GTK_CONTAINER(pScrolledWindow
), pTreeView
);
512 gtk_box_pack_start(GTK_BOX(pContentArea
), pScrolledWindow
, true, true, 2);
515 gtk_widget_show_all(pDialog
);
516 gint res
= gtk_dialog_run(GTK_DIALOG(pDialog
));
518 // Dispatch the matching command, if necessary.
519 if (res
== GTK_RESPONSE_YES
|| res
== GTK_RESPONSE_NO
|| res
== GTK_RESPONSE_APPLY
)
521 GtkTreeSelection
* pSelection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pTreeView
));
522 GtkTreeIter aTreeIter
;
523 GtkTreeModel
* pTreeModel
;
524 if (gtk_tree_selection_get_selected(pSelection
, &pTreeModel
, &aTreeIter
))
528 gtk_tree_model_get(pTreeModel
, &aTreeIter
, 0, &nIndex
, -1);
529 std::string aCommand
;
530 if (res
== GTK_RESPONSE_YES
)
531 aCommand
= ".uno:AcceptTrackedChange";
532 else if (res
== GTK_RESPONSE_NO
)
533 aCommand
= ".uno:RejectTrackedChange";
535 // Just select the given redline, don't accept or reject it.
536 aCommand
= ".uno:NextTrackedChange";
537 // Without the '.uno:' prefix.
538 std::string aKey
= aCommand
.substr(strlen(".uno:"));
541 boost::property_tree::ptree aCommandTree
;
542 aCommandTree
.put(boost::property_tree::ptree::path_type(aKey
+ "/type", '/'), "unsigned short");
543 aCommandTree
.put(boost::property_tree::ptree::path_type(aKey
+ "/value", '/'), nIndex
);
545 aStream
.str(std::string());
546 boost::property_tree::write_json(aStream
, aCommandTree
);
547 std::string aArguments
= aStream
.str();
548 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), aCommand
.c_str(), aArguments
.c_str(), false);
552 gtk_widget_destroy(pDialog
);
555 void documentRepair(GtkWidget
* pButton
, gpointer
/*pItem*/)
557 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
559 LibreOfficeKitDocument
* pDocument
= lok_doc_view_get_document(LOK_DOC_VIEW(window
->lokdocview
));
560 // Show it in linear time, so first redo in reverse order, then undo.
561 std::vector
<std::string
> aTypes
= {".uno:Redo", ".uno:Undo"};
562 std::vector
<boost::property_tree::ptree
> aTrees
;
563 for (size_t nType
= 0; nType
< aTypes
.size(); ++nType
)
565 const std::string
& rType
= aTypes
[nType
];
566 char* pValues
= pDocument
->pClass
->getCommandValues(pDocument
, rType
.c_str());
567 std::stringstream aInfo
;
568 aInfo
<< "lok::Document::getCommandValues('" << rType
<< "') returned '" << pValues
<< "'" << std::endl
;
569 g_info("%s", aInfo
.str().c_str());
570 std::stringstream
aStream(pValues
);
572 assert(!aStream
.str().empty());
573 boost::property_tree::ptree aTree
;
574 boost::property_tree::read_json(aStream
, aTree
);
575 aTrees
.push_back(aTree
);
578 // Create the dialog.
579 GtkWidget
* pDialog
= gtk_dialog_new_with_buttons("Repair document",
585 gtk_window_set_default_size(GTK_WINDOW(pDialog
), 800, 600);
586 GtkWidget
* pContentArea
= gtk_dialog_get_content_area(GTK_DIALOG (pDialog
));
587 GtkWidget
* pScrolledWindow
= gtk_scrolled_window_new(nullptr, nullptr);
590 GtkTreeStore
* pTreeStore
= gtk_tree_store_new(5, G_TYPE_STRING
, G_TYPE_INT
, G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_STRING
);
591 for (size_t nTree
= 0; nTree
< aTrees
.size(); ++nTree
)
593 const auto& rTree
= aTrees
[nTree
];
594 for (const auto& rValue
: rTree
.get_child("actions"))
596 GtkTreeIter aTreeIter
;
597 gtk_tree_store_append(pTreeStore
, &aTreeIter
, nullptr);
598 gtk_tree_store_set(pTreeStore
, &aTreeIter
,
599 0, aTypes
[nTree
].c_str(),
600 1, rValue
.second
.get
<int>("index"),
601 2, rValue
.second
.get
<std::string
>("comment").c_str(),
602 3, rValue
.second
.get
<std::string
>("viewId").c_str(),
603 4, rValue
.second
.get
<std::string
>("dateTime").c_str(),
607 GtkWidget
* pTreeView
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(pTreeStore
));
608 std::vector
<std::string
> aColumns
= {"Type", "Index", "Comment", "View ID", "Timestamp"};
609 for (size_t nColumn
= 0; nColumn
< aColumns
.size(); ++nColumn
)
611 GtkCellRenderer
* pRenderer
= gtk_cell_renderer_text_new();
612 GtkTreeViewColumn
* pColumn
= gtk_tree_view_column_new_with_attributes(aColumns
[nColumn
].c_str(),
616 gtk_tree_view_append_column(GTK_TREE_VIEW(pTreeView
), pColumn
);
618 gtk_container_add(GTK_CONTAINER(pScrolledWindow
), pTreeView
);
619 gtk_box_pack_start(GTK_BOX(pContentArea
), pScrolledWindow
, true, true, 2);
622 gtk_widget_show_all(pDialog
);
623 gint res
= gtk_dialog_run(GTK_DIALOG(pDialog
));
625 // Dispatch the matching command, if necessary.
626 if (res
== GTK_RESPONSE_OK
)
628 GtkTreeSelection
* pSelection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(pTreeView
));
629 GtkTreeIter aTreeIter
;
630 GtkTreeModel
* pTreeModel
;
631 if (gtk_tree_selection_get_selected(pSelection
, &pTreeModel
, &aTreeIter
))
633 gchar
* pType
= nullptr;
636 gtk_tree_model_get(pTreeModel
, &aTreeIter
, 0, &pType
, 1, &nIndex
, -1);
637 // '.uno:Undo' or '.uno:Redo'
638 const std::string
aType(pType
);
639 // Without the '.uno:' prefix.
640 std::string aKey
= aType
.substr(strlen(".uno:"));
644 boost::property_tree::ptree aTree
;
645 aTree
.put(boost::property_tree::ptree::path_type(aKey
+ "/type", '/'), "unsigned short");
646 aTree
.put(boost::property_tree::ptree::path_type(aKey
+ "/value", '/'), nIndex
+ 1);
648 // Without this, we could only undo our own commands.
649 aTree
.put(boost::property_tree::ptree::path_type("Repair/type", '/'), "boolean");
650 aTree
.put(boost::property_tree::ptree::path_type("Repair/value", '/'), true);
652 std::stringstream aStream
;
653 boost::property_tree::write_json(aStream
, aTree
);
654 std::string aArguments
= aStream
.str();
655 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), aType
.c_str(), aArguments
.c_str(), false);
659 gtk_widget_destroy(pDialog
);
662 void toggleFindbar(GtkWidget
* pButton
, gpointer
/*pItem*/)
664 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
665 gtv_application_window_toggle_findbar(window
);
668 void docAdjustmentChanged(GtkAdjustment
*, gpointer pData
)
670 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(pData
);
671 if (window
->lokdocview
)
672 LOKDocViewSigHandlers::configureEvent(window
->lokdocview
, nullptr, nullptr);
675 void signalSearchNext(GtkWidget
* pButton
, gpointer
/*pItem*/)
677 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
678 GtkEntry
* pEntry
= GTK_ENTRY(window
->findbarEntry
);
679 const char* pText
= gtk_entry_get_text(pEntry
);
680 bool findAll
= gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(window
->findAll
));
681 lok_doc_view_find_next(LOK_DOC_VIEW(window
->lokdocview
), pText
, findAll
);
684 void signalSearchPrev(GtkWidget
* pButton
, gpointer
/*pItem*/)
686 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
687 GtkEntry
* pEntry
= GTK_ENTRY(window
->findbarEntry
);
688 const char* pText
= gtk_entry_get_text(pEntry
);
689 bool findAll
= gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(window
->findAll
));
690 lok_doc_view_find_prev(LOK_DOC_VIEW(window
->lokdocview
), pText
, findAll
);
693 gboolean
signalFindbar(GtkWidget
* pWidget
, GdkEventKey
* pEvent
, gpointer
/*pData*/)
695 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget
));
696 gtk_label_set_text(GTK_LABEL(window
->findbarlabel
), "");
697 switch(pEvent
->keyval
)
702 signalSearchNext(pWidget
, nullptr);
708 gtk_widget_hide(GTK_WIDGET(window
->findtoolbar
));
715 void toggleFindAll(GtkWidget
* pButton
, gpointer
/*pItem*/)
717 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pButton
));
718 GtkEntry
* pEntry
= GTK_ENTRY(window
->findbarEntry
);
719 const char* pText
= gtk_entry_get_text(pEntry
);
720 bool findAll
= gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(window
->findAll
));
721 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(window
->findAll
), !findAll
);
722 lok_doc_view_highlight_all(LOK_DOC_VIEW(window
->lokdocview
), pText
);
725 void editButtonClicked(GtkWidget
* pWidget
, gpointer userdata
)
727 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget
));
728 std::map
<std::string
, std::string
> aEntries
;
729 aEntries
["Text"] = "";
731 GtvHelpers::userPromptDialog(GTK_WINDOW(window
), "Edit comment", aEntries
);
733 gchar
*commentId
= static_cast<gchar
*>(g_object_get_data(G_OBJECT(userdata
), "id"));
735 boost::property_tree::ptree aTree
;
736 aTree
.put(boost::property_tree::ptree::path_type("Id/type", '/'), "string");
737 aTree
.put(boost::property_tree::ptree::path_type("Id/value", '/'), std::string(commentId
));
739 aTree
.put(boost::property_tree::ptree::path_type("Text/type", '/'), "string");
740 aTree
.put(boost::property_tree::ptree::path_type("Text/value", '/'), aEntries
["Text"]);
742 std::stringstream aStream
;
743 boost::property_tree::write_json(aStream
, aTree
);
744 std::string aArguments
= aStream
.str();
746 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), ".uno:EditAnnotation", aArguments
.c_str(), false);
749 void replyButtonClicked(GtkWidget
* pWidget
, gpointer userdata
)
751 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget
));
752 std::map
<std::string
, std::string
> aEntries
;
753 aEntries
["Text"] = "";
755 GtvHelpers::userPromptDialog(GTK_WINDOW(window
), "Reply comment", aEntries
);
757 gchar
*commentId
= static_cast<gchar
*>(g_object_get_data(G_OBJECT(userdata
), "id"));
759 boost::property_tree::ptree aTree
;
760 aTree
.put(boost::property_tree::ptree::path_type("Id/type", '/'), "string");
761 aTree
.put(boost::property_tree::ptree::path_type("Id/value", '/'), std::string(commentId
));
763 aTree
.put(boost::property_tree::ptree::path_type("Text/type", '/'), "string");
764 aTree
.put(boost::property_tree::ptree::path_type("Text/value", '/'), aEntries
["Text"]);
766 std::stringstream aStream
;
767 boost::property_tree::write_json(aStream
, aTree
);
768 std::string aArguments
= aStream
.str();
770 // Different reply UNO command for impress
771 std::string replyCommand
= ".uno:ReplyComment";
772 LibreOfficeKitDocument
* pDocument
= lok_doc_view_get_document(LOK_DOC_VIEW(window
->lokdocview
));
773 if (pDocument
&& pDocument
->pClass
->getDocumentType(pDocument
) == LOK_DOCTYPE_PRESENTATION
)
774 replyCommand
= ".uno:ReplyToAnnotation";
775 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), replyCommand
.c_str(), aArguments
.c_str(), false);
778 void deleteCommentButtonClicked(GtkWidget
* pWidget
, gpointer userdata
)
780 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget
));
781 gchar
*commentid
= static_cast<gchar
*>(g_object_get_data(G_OBJECT(userdata
), "id"));
783 boost::property_tree::ptree aTree
;
784 aTree
.put(boost::property_tree::ptree::path_type("Id/type", '/'), "string");
785 aTree
.put(boost::property_tree::ptree::path_type("Id/value", '/'), std::string(commentid
));
787 std::stringstream aStream
;
788 boost::property_tree::write_json(aStream
, aTree
);
789 std::string aArguments
= aStream
.str();
791 // Different reply UNO command for impress
792 std::string deleteCommand
= ".uno:DeleteComment";
793 LibreOfficeKitDocument
* pDocument
= lok_doc_view_get_document(LOK_DOC_VIEW(window
->lokdocview
));
796 if (pDocument
->pClass
->getDocumentType(pDocument
) == LOK_DOCTYPE_PRESENTATION
)
797 deleteCommand
= ".uno:DeleteAnnotation";
798 else if (pDocument
->pClass
->getDocumentType(pDocument
) == LOK_DOCTYPE_SPREADSHEET
)
799 deleteCommand
= ".uno:DeleteNote";
802 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), deleteCommand
.c_str(), aArguments
.c_str(), false);
805 /// Handles the key-press-event of the address entry widget.
806 gboolean
signalAddressbar(GtkWidget
* pWidget
, GdkEventKey
* pEvent
, gpointer
/*pData*/)
808 GtvApplicationWindow
* window
= GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(pWidget
));
809 switch(pEvent
->keyval
)
813 GtkEntry
* pEntry
= GTK_ENTRY(pWidget
);
814 const char* pText
= gtk_entry_get_text(pEntry
);
816 boost::property_tree::ptree aTree
;
817 aTree
.put(boost::property_tree::ptree::path_type("ToPoint/type", '/'), "string");
818 aTree
.put(boost::property_tree::ptree::path_type("ToPoint/value", '/'), pText
);
819 std::stringstream aStream
;
820 boost::property_tree::write_json(aStream
, aTree
);
821 std::string aArguments
= aStream
.str();
823 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), ".uno:GoToCell", aArguments
.c_str(), false);
824 gtk_widget_grab_focus(window
->lokdocview
);
829 std::string aArguments
;
830 lok_doc_view_post_command(LOK_DOC_VIEW(window
->lokdocview
), ".uno:Cancel", aArguments
.c_str(), false);
831 gtk_widget_grab_focus(window
->lokdocview
);
838 /// Handles the key-press-event of the formula entry widget.
839 gboolean
signalFormulabar(GtkWidget
* /*pWidget*/, GdkEventKey
* /*pEvent*/, gpointer
/*pData*/)
841 // for now it just displays the callback
842 // TODO - submit the edited formula
846 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */