nss: upgrade to release 3.73
[LibreOffice.git] / libreofficekit / qa / gtktiledviewer / gtv-application-window.cxx
blob4d2dbd41f2511a74a6954f055c16f203f7613c81
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 <memory>
14 #include <LibreOfficeKit/LibreOfficeKitGtk.h>
15 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
17 #include "gtv-application-window.hxx"
18 #include "gtv-main-toolbar.hxx"
19 #include "gtv-helpers.hxx"
20 #include "gtv-signal-handlers.hxx"
21 #include "gtv-lokdocview-signal-handlers.hxx"
22 #include "gtv-calc-header-bar.hxx"
23 #include "gtv-comments-sidebar.hxx"
24 #include "gtv-lok-dialog.hxx"
26 #include <boost/property_tree/json_parser.hpp>
28 namespace {
30 struct GtvApplicationWindowPrivate
32 GtkWidget* container;
33 GtkWidget* gridcontainer;
34 GtkWidget* toolbarcontainer;
35 GtkWidget* scrolledwindowcontainer;
37 bool toolbarBroadcast;
38 bool partSelectorBroadcast;
40 GList* m_pChildWindows;
42 // Rendering args; options with which lokdocview was rendered in this window
43 GtvRenderingArgs* m_pRenderingArgs;
48 #if defined __clang__
49 #if __has_warning("-Wdeprecated-volatile")
50 #pragma clang diagnostic push
51 #pragma clang diagnostic ignored "-Wdeprecated-volatile"
52 #endif
53 #endif
54 G_DEFINE_TYPE_WITH_PRIVATE(GtvApplicationWindow, gtv_application_window, GTK_TYPE_APPLICATION_WINDOW);
55 #if defined __clang__
56 #if __has_warning("-Wdeprecated-volatile")
57 #pragma clang diagnostic pop
58 #endif
59 #endif
61 static GtvApplicationWindowPrivate*
62 getPrivate(GtvApplicationWindow* win)
64 return static_cast<GtvApplicationWindowPrivate*>(gtv_application_window_get_instance_private(win));
67 static void
68 gtv_application_window_init(GtvApplicationWindow* win)
70 const std::string uiFilePath = GtvHelpers::getDirPath(__FILE__) + std::string(UI_FILE_NAME);
71 GtvGtkWrapper<GtkBuilder> builder(gtk_builder_new_from_file(uiFilePath.c_str()),
72 [](GtkBuilder* pBuilder) {
73 g_object_unref(pBuilder);
74 });
75 GtvApplicationWindowPrivate* priv = getPrivate(win);
77 // This is the parent GtkBox holding everything
78 priv->container = GTK_WIDGET(gtk_builder_get_object(builder.get(), "container"));
79 // Toolbar container
80 priv->toolbarcontainer = gtv_main_toolbar_new();
82 // Attach to the toolbar to main window
83 gtk_box_pack_start(GTK_BOX(priv->container), priv->toolbarcontainer, false, false, false);
84 gtk_box_reorder_child(GTK_BOX(priv->container), priv->toolbarcontainer, 0);
86 priv->gridcontainer = GTK_WIDGET(gtk_builder_get_object(builder.get(), "maingrid"));
87 // scrolled window containing the main drawing area
88 win->scrolledwindow = GTK_WIDGET(gtk_builder_get_object(builder.get(), "scrolledwindow"));
89 // scrolledwindow container
90 priv->scrolledwindowcontainer = GTK_WIDGET(gtk_builder_get_object(builder.get(), "scrolledwindowcontainer"));
92 GtkAdjustment* pHAdjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(win->scrolledwindow));
93 g_signal_connect(pHAdjustment, "value-changed", G_CALLBACK(docAdjustmentChanged), win);
94 GtkAdjustment* pVAdjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(win->scrolledwindow));
95 g_signal_connect(pVAdjustment, "value-changed", G_CALLBACK(docAdjustmentChanged), win);
97 // calc header row bar
98 win->cornerarea = gtv_calc_header_bar_new();
99 gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->cornerarea), CalcHeaderType::CORNER);
100 win->rowbar = gtv_calc_header_bar_new();
101 gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->rowbar), CalcHeaderType::ROW);
102 win->columnbar = gtv_calc_header_bar_new();
103 gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->columnbar), CalcHeaderType::COLUMN);
105 // attach row/column/corner to the container
106 gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->cornerarea, 0, 0, 1, 1);
107 gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->rowbar, 0, 1, 1, 1);
108 gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->columnbar, 1, 0, 1, 1);
110 // statusbar
111 win->statusbar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "statusbar"));
112 win->redlinelabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "redlinelabel"));
113 win->zoomlabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "zoomlabel"));
115 win->findtoolbar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findtoolbar"));
116 win->findbarlabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_label"));
117 win->findbarEntry = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_entry"));
118 win->findAll = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_findall"));
119 priv->toolbarBroadcast = true;
120 priv->partSelectorBroadcast = true;
122 gtk_container_add(GTK_CONTAINER(win), priv->container);
124 priv->m_pChildWindows = nullptr;
125 priv->m_pRenderingArgs = new GtvRenderingArgs();
128 static void
129 gtv_application_window_dispose(GObject* object)
131 GtvApplicationWindowPrivate* priv = getPrivate(GTV_APPLICATION_WINDOW(object));
133 delete priv->m_pRenderingArgs;
134 priv->m_pRenderingArgs = nullptr;
136 G_OBJECT_CLASS (gtv_application_window_parent_class)->dispose (object);
139 static void
140 gtv_application_window_class_init(GtvApplicationWindowClass* klass)
142 G_OBJECT_CLASS(klass)->dispose = gtv_application_window_dispose;
145 /// Helper function to do some tasks after widget is fully loaded (including
146 /// document load)
147 static void initWindow(GtvApplicationWindow* window)
149 GtvApplicationWindowPrivate* priv = getPrivate(window);
151 #ifdef __GNUC__
152 #pragma GCC diagnostic push
153 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
154 #endif
155 GList *focusChain = nullptr;
156 focusChain = g_list_append( focusChain, window->lokdocview );
158 gtk_container_set_focus_chain ( GTK_CONTAINER (priv->container), focusChain );
159 #ifdef __GNUC__
160 #pragma GCC diagnostic pop
161 #endif
163 // TODO: Implement progressbar in statusbar
164 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
165 if (pDocument)
167 LibreOfficeKitDocumentType eDocType = static_cast<LibreOfficeKitDocumentType>(pDocument->pClass->getDocumentType(pDocument));
168 if (eDocType == LOK_DOCTYPE_SPREADSHEET)
170 // Align to top left corner, so the tiles are in sync with the
171 // row/column bar, even when zooming out enough that not all space is
172 // used.
173 gtk_widget_set_halign(GTK_WIDGET(window->lokdocview), GTK_ALIGN_START);
174 gtk_widget_set_valign(GTK_WIDGET(window->lokdocview), GTK_ALIGN_START);
177 // By default make the document editable in a new window
178 lok_doc_view_set_edit(LOK_DOC_VIEW(window->lokdocview), true);
179 // Let toolbar adjust its button accordingly
180 gtv_main_toolbar_doc_loaded(GTV_MAIN_TOOLBAR(priv->toolbarcontainer), eDocType, true /* Edit button state */);
183 // Fill our comments sidebar
184 gboolean bTiledAnnotations;
185 g_object_get(G_OBJECT(window->lokdocview), "tiled-annotations", &bTiledAnnotations, nullptr);
186 if (!bTiledAnnotations && pDocument)
188 window->commentssidebar = gtv_comments_sidebar_new();
189 gtk_container_add(GTK_CONTAINER(priv->scrolledwindowcontainer), window->commentssidebar);
190 // fill the comments sidebar
191 gtv_comments_sidebar_view_annotations(GTV_COMMENTS_SIDEBAR(window->commentssidebar));
195 static void
196 gtv_application_open_document_callback(GObject* source_object, GAsyncResult* res, gpointer /*userdata*/)
198 LOKDocView* pDocView = LOK_DOC_VIEW (source_object);
199 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
200 GError* error = nullptr;
201 if (!lok_doc_view_open_document_finish(pDocView, res, &error))
203 GtkWidget* pDialog = gtk_message_dialog_new(GTK_WINDOW(window),
204 GTK_DIALOG_DESTROY_WITH_PARENT,
205 GTK_MESSAGE_ERROR,
206 GTK_BUTTONS_CLOSE,
207 "Error occurred while opening the document: '%s'",
208 error->message);
209 gtk_dialog_run(GTK_DIALOG(pDialog));
210 gtk_widget_destroy(pDialog);
212 g_error_free(error);
213 gtk_widget_destroy(GTK_WIDGET(pDocView));
214 gtk_main_quit();
215 return;
218 initWindow(window);
221 /// Get the visible area of the scrolled window
222 void gtv_application_window_get_visible_area(GtvApplicationWindow* pWindow, GdkRectangle* pArea)
224 GtkAdjustment* pHAdjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(pWindow->scrolledwindow));
225 GtkAdjustment* pVAdjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(pWindow->scrolledwindow));
227 pArea->x = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
228 gtk_adjustment_get_value(pHAdjustment));
229 pArea->y = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
230 gtk_adjustment_get_value(pVAdjustment));
231 pArea->width = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
232 gtk_adjustment_get_page_size(pHAdjustment));
233 pArea->height = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
234 gtk_adjustment_get_page_size(pVAdjustment));
237 void gtv_application_window_toggle_findbar(GtvApplicationWindow* window)
239 if (gtk_widget_get_visible(window->findtoolbar))
241 gtk_widget_hide(window->findtoolbar);
243 else
245 gtk_widget_show_all(window->findtoolbar);
246 gtk_widget_grab_focus(window->findtoolbar);
250 GtkToolItem* gtv_application_window_find_tool_by_unocommand(GtvApplicationWindow* window, const std::string& unoCmd)
252 GtvApplicationWindowPrivate* priv = getPrivate(window);
253 GtkToolItem* result = nullptr;
255 // Find in the first toolbar
256 GtkContainer* pToolbar1 = gtv_main_toolbar_get_first_toolbar(GTV_MAIN_TOOLBAR(priv->toolbarcontainer));
257 GtvGtkWrapper<GList> pList(gtk_container_get_children(pToolbar1),
258 [](GList* l)
260 g_list_free(l);
262 for (GList* l = pList.get(); l != nullptr; l = l->next)
264 if (GTK_IS_TOOL_BUTTON(l->data))
266 GtkToolButton* pButton = GTK_TOOL_BUTTON(l->data);
267 const gchar* pLabel = gtk_tool_button_get_label(pButton);
268 if (g_strcmp0(unoCmd.c_str(), pLabel) == 0)
270 result = GTK_TOOL_ITEM(pButton);
275 // Look in second toolbar if not found
276 GtkContainer* pToolbar2 = gtv_main_toolbar_get_second_toolbar(GTV_MAIN_TOOLBAR(priv->toolbarcontainer));
277 pList.reset(gtk_container_get_children(pToolbar2));
278 for (GList* l = pList.get(); result == nullptr && l != nullptr; l = l->next)
280 if (GTK_IS_TOOL_BUTTON(l->data))
282 GtkToolButton* pButton = GTK_TOOL_BUTTON(l->data);
283 const gchar* pLabel = gtk_tool_button_get_label(pButton);
284 if (g_strcmp0(unoCmd.c_str(), pLabel) == 0)
286 result = GTK_TOOL_ITEM(pButton);
291 return result;
294 static std::string
295 createRenderingArgsJSON(const GtvRenderingArgs* pRenderingArgs)
297 boost::property_tree::ptree aTree;
298 if (pRenderingArgs->m_bHidePageShadow)
300 aTree.put(boost::property_tree::ptree::path_type(".uno:ShowBorderShadow/type", '/'), "boolean");
301 aTree.put(boost::property_tree::ptree::path_type(".uno:ShowBorderShadow/value", '/'), false);
303 if (pRenderingArgs->m_bHideWhiteSpace)
305 aTree.put(boost::property_tree::ptree::path_type(".uno:HideWhitespace/type", '/'), "boolean");
306 aTree.put(boost::property_tree::ptree::path_type(".uno:HideWhitespace/value", '/'), true);
308 aTree.put(boost::property_tree::ptree::path_type(".uno:Author/type", '/'), "string");
309 aTree.put(boost::property_tree::ptree::path_type(".uno:Author/value", '/'), GtvHelpers::getNextAuthor());
310 std::stringstream aStream;
311 boost::property_tree::write_json(aStream, aTree);
312 return aStream.str();
315 static void setupDocView(GtvApplicationWindow* window)
317 GtvApplicationWindowPrivate* priv = getPrivate(window);
318 g_object_set(G_OBJECT(window->lokdocview),
319 "doc-password", true,
320 "doc-password-to-modify", true,
321 "tiled-annotations", priv->m_pRenderingArgs->m_bEnableTiledAnnotations,
322 nullptr);
324 #if GLIB_CHECK_VERSION(2,40,0)
325 g_assert_nonnull(window->lokdocview);
326 #endif
327 g_signal_connect(window->lokdocview, "edit-changed", G_CALLBACK(LOKDocViewSigHandlers::editChanged), nullptr);
328 g_signal_connect(window->lokdocview, "command-changed", G_CALLBACK(LOKDocViewSigHandlers::commandChanged), nullptr);
329 g_signal_connect(window->lokdocview, "command-result", G_CALLBACK(LOKDocViewSigHandlers::commandResult), nullptr);
330 g_signal_connect(window->lokdocview, "search-not-found", G_CALLBACK(LOKDocViewSigHandlers::searchNotFound), nullptr);
331 g_signal_connect(window->lokdocview, "search-result-count", G_CALLBACK(LOKDocViewSigHandlers::searchResultCount), nullptr);
332 g_signal_connect(window->lokdocview, "part-changed", G_CALLBACK(LOKDocViewSigHandlers::partChanged), nullptr);
333 g_signal_connect(window->lokdocview, "hyperlink-clicked", G_CALLBACK(LOKDocViewSigHandlers::hyperlinkClicked), nullptr);
334 g_signal_connect(window->lokdocview, "cursor-changed", G_CALLBACK(LOKDocViewSigHandlers::cursorChanged), nullptr);
335 g_signal_connect(window->lokdocview, "address-changed", G_CALLBACK(LOKDocViewSigHandlers::addressChanged), nullptr);
336 g_signal_connect(window->lokdocview, "formula-changed", G_CALLBACK(LOKDocViewSigHandlers::formulaChanged), nullptr);
337 g_signal_connect(window->lokdocview, "password-required", G_CALLBACK(LOKDocViewSigHandlers::passwordRequired), nullptr);
338 g_signal_connect(window->lokdocview, "comment", G_CALLBACK(LOKDocViewSigHandlers::comment), nullptr);
339 g_signal_connect(window->lokdocview, "window", G_CALLBACK(LOKDocViewSigHandlers::window), window);
341 g_signal_connect(window->lokdocview, "configure-event", G_CALLBACK(LOKDocViewSigHandlers::configureEvent), nullptr);
344 void
345 gtv_application_window_create_view_from_window(GtvApplicationWindow* window)
347 GtvApplicationWindowPrivate* priv = getPrivate(window);
348 GApplication* app = g_application_get_default();
350 GtvApplicationWindow* newWindow = GTV_APPLICATION_WINDOW(gtv_application_window_new(GTK_APPLICATION(app)));
351 const std::string aArguments = createRenderingArgsJSON(priv->m_pRenderingArgs);
352 newWindow->lokdocview = lok_doc_view_new_from_widget(LOK_DOC_VIEW(window->lokdocview), aArguments.c_str());
353 setupDocView(newWindow);
355 gtk_container_add(GTK_CONTAINER(newWindow->scrolledwindow), newWindow->lokdocview);
356 gtk_widget_show_all(newWindow->scrolledwindow);
357 gtk_window_present(GTK_WINDOW(newWindow));
359 initWindow(newWindow);
362 void
363 gtv_application_window_load_document(GtvApplicationWindow* window,
364 const GtvRenderingArgs* aArgs,
365 const std::string& aDocPath)
367 GtvApplicationWindowPrivate* priv = getPrivate(window);
368 // keep a copy of it; we need to use these for creating new views later
369 *(priv->m_pRenderingArgs) = *aArgs;
371 // setup lokdocview
372 const char *pUserProfile = priv->m_pRenderingArgs->m_aUserProfile.empty() ?
373 nullptr : priv->m_pRenderingArgs->m_aUserProfile.c_str();
375 window->lokdocview = GTK_WIDGET(
376 g_initable_new(LOK_TYPE_DOC_VIEW, nullptr, nullptr,
377 "lopath", priv->m_pRenderingArgs->m_aLoPath.c_str(),
378 "unipoll", priv->m_pRenderingArgs->m_bUnipoll,
379 "userprofileurl", pUserProfile,
380 "halign", GTK_ALIGN_CENTER,
381 "valign", GTK_ALIGN_CENTER,
382 nullptr));
384 gtk_container_add(GTK_CONTAINER(window->scrolledwindow), window->lokdocview);
386 setupDocView(window);
388 // Create argument JSON
389 const std::string aArguments = createRenderingArgsJSON(priv->m_pRenderingArgs);
390 lok_doc_view_open_document(LOK_DOC_VIEW(window->lokdocview), aDocPath.c_str(),
391 aArguments.c_str(), nullptr,
392 gtv_application_open_document_callback, window->lokdocview);
394 gtk_widget_show_all(GTK_WIDGET(window->scrolledwindow));
397 GtvMainToolbar*
398 gtv_application_window_get_main_toolbar(GtvApplicationWindow* window)
400 GtvApplicationWindowPrivate* priv = getPrivate(window);
401 return GTV_MAIN_TOOLBAR(priv->toolbarcontainer);
404 void
405 gtv_application_window_set_toolbar_broadcast(GtvApplicationWindow* window, bool broadcast)
407 GtvApplicationWindowPrivate* priv = getPrivate(window);
408 priv->toolbarBroadcast = broadcast;
411 gboolean
412 gtv_application_window_get_toolbar_broadcast(GtvApplicationWindow* window)
414 GtvApplicationWindowPrivate* priv = getPrivate(window);
415 return priv->toolbarBroadcast;
418 void
419 gtv_application_window_set_part_broadcast(GtvApplicationWindow* window, bool broadcast)
421 GtvApplicationWindowPrivate* priv = getPrivate(window);
422 priv->partSelectorBroadcast = broadcast;
425 gboolean
426 gtv_application_window_get_part_broadcast(GtvApplicationWindow* window)
428 GtvApplicationWindowPrivate* priv = getPrivate(window);
429 return priv->partSelectorBroadcast;
432 void
433 gtv_application_window_register_child_window(GtvApplicationWindow* window, GtkWindow* pChildWin)
435 guint dialogid = 0;
436 g_object_get(G_OBJECT(pChildWin), "dialogid", &dialogid, nullptr);
437 g_debug("Register child window: dialogid [%d] in window[%p]", dialogid, window);
438 GtvApplicationWindowPrivate* priv = getPrivate(window);
439 if (pChildWin)
440 priv->m_pChildWindows = g_list_append(priv->m_pChildWindows, pChildWin);
443 void
444 gtv_application_window_unregister_child_window(GtvApplicationWindow* window, GtkWindow* pChildWin)
446 guint dialogid = 0;
447 g_object_get(G_OBJECT(pChildWin), "dialogid", &dialogid, nullptr);
448 g_debug("Unregister child window: dialogid [%d] in window[%p]", dialogid, window);
449 GtvApplicationWindowPrivate* priv = getPrivate(window);
450 if (pChildWin)
452 priv->m_pChildWindows = g_list_remove(priv->m_pChildWindows, pChildWin);
453 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
454 guint dialogId = 0;
455 g_object_get(G_OBJECT(pChildWin), "dialogid", &dialogId, nullptr);
456 pDocument->pClass->postWindow(pDocument, dialogId, LOK_WINDOW_CLOSE, nullptr);
460 GtkWindow*
461 gtv_application_window_get_child_window_by_id(GtvApplicationWindow* window, guint nWinId)
463 GtvApplicationWindowPrivate* priv = getPrivate(window);
464 GList* pIt = nullptr;
465 GtkWindow* ret = nullptr;
466 // For now, only dialogs are registered as child window
467 for (pIt = priv->m_pChildWindows; pIt != nullptr; pIt = pIt->next)
469 guint dialogId = 0;
470 g_object_get(G_OBJECT(pIt->data), "dialogid", &dialogId, nullptr);
471 if (dialogId == nWinId)
473 ret = GTK_WINDOW(pIt->data);
474 break;
478 return ret;
481 GtkWidget*
482 gtv_application_window_get_parent(GtvApplicationWindow* window, guint nWinId)
484 GtvApplicationWindowPrivate* priv = getPrivate(window);
485 GList* pIt = nullptr;
486 for (pIt = priv->m_pChildWindows; pIt != nullptr; pIt = pIt->next)
488 if (gtv_lok_dialog_is_parent_of(GTV_LOK_DIALOG(pIt->data), nWinId))
489 return GTK_WIDGET(pIt->data);
491 return nullptr;
494 GtvApplicationWindow*
495 gtv_application_window_new(GtkApplication* app)
497 g_return_val_if_fail(GTK_IS_APPLICATION(app), nullptr);
499 return GTV_APPLICATION_WINDOW(g_object_new(GTV_TYPE_APPLICATION_WINDOW,
500 "application", app,
501 "width-request", 1024,
502 "height-request", 768,
503 "title", "LibreOffice GtkTiledViewer",
504 "window-position", GTK_WIN_POS_CENTER,
505 "show-menubar", false,
506 nullptr));
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */