bump product version to 6.4.0.3
[LibreOffice.git] / libreofficekit / qa / gtktiledviewer / gtv-application-window.cxx
blob7692b0b4a162d2f8d0d98580ae6aaa1aa4e486c2
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>
27 #include <boost/optional.hpp>
29 struct GtvApplicationWindowPrivate
31 GtkWidget* container;
32 GtkWidget* gridcontainer;
33 GtkWidget* toolbarcontainer;
34 GtkWidget* scrolledwindowcontainer;
35 GtkWidget* lokDialog;
37 gboolean toolbarBroadcast;
38 gboolean partSelectorBroadcast;
40 GList* m_pChildWindows;
42 // Rendering args; options with which lokdocview was rendered in this window
43 GtvRenderingArgs* m_pRenderingArgs;
46 #if defined __clang__
47 #if __has_warning("-Wdeprecated-volatile")
48 #pragma clang diagnostic push
49 #pragma clang diagnostic ignored "-Wdeprecated-volatile"
50 #endif
51 #endif
52 G_DEFINE_TYPE_WITH_PRIVATE(GtvApplicationWindow, gtv_application_window, GTK_TYPE_APPLICATION_WINDOW);
53 #if defined __clang__
54 #if __has_warning("-Wdeprecated-volatile")
55 #pragma clang diagnostic pop
56 #endif
57 #endif
59 static GtvApplicationWindowPrivate*
60 getPrivate(GtvApplicationWindow* win)
62 return static_cast<GtvApplicationWindowPrivate*>(gtv_application_window_get_instance_private(win));
65 static void
66 gtv_application_window_init(GtvApplicationWindow* win)
68 const std::string uiFilePath = GtvHelpers::getDirPath(__FILE__) + std::string(UI_FILE_NAME);
69 GtvGtkWrapper<GtkBuilder> builder(gtk_builder_new_from_file(uiFilePath.c_str()),
70 [](GtkBuilder* pBuilder) {
71 g_object_unref(pBuilder);
72 });
73 GtvApplicationWindowPrivate* priv = getPrivate(win);
75 // This is the parent GtkBox holding everything
76 priv->container = GTK_WIDGET(gtk_builder_get_object(builder.get(), "container"));
77 // Toolbar container
78 priv->toolbarcontainer = gtv_main_toolbar_new();
80 // Attach to the toolbar to main window
81 gtk_box_pack_start(GTK_BOX(priv->container), priv->toolbarcontainer, false, false, false);
82 gtk_box_reorder_child(GTK_BOX(priv->container), priv->toolbarcontainer, 0);
84 priv->gridcontainer = GTK_WIDGET(gtk_builder_get_object(builder.get(), "maingrid"));
85 // scrolled window containing the main drawing area
86 win->scrolledwindow = GTK_WIDGET(gtk_builder_get_object(builder.get(), "scrolledwindow"));
87 // scrolledwindow container
88 priv->scrolledwindowcontainer = GTK_WIDGET(gtk_builder_get_object(builder.get(), "scrolledwindowcontainer"));
90 GtkAdjustment* pHAdjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(win->scrolledwindow));
91 g_signal_connect(pHAdjustment, "value-changed", G_CALLBACK(docAdjustmentChanged), win);
92 GtkAdjustment* pVAdjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(win->scrolledwindow));
93 g_signal_connect(pVAdjustment, "value-changed", G_CALLBACK(docAdjustmentChanged), win);
95 // calc header row bar
96 win->cornerarea = gtv_calc_header_bar_new();
97 gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->cornerarea), CalcHeaderType::CORNER);
98 win->rowbar = gtv_calc_header_bar_new();
99 gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->rowbar), CalcHeaderType::ROW);
100 win->columnbar = gtv_calc_header_bar_new();
101 gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->columnbar), CalcHeaderType::COLUMN);
103 // attach row/column/corner to the container
104 gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->cornerarea, 0, 0, 1, 1);
105 gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->rowbar, 0, 1, 1, 1);
106 gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->columnbar, 1, 0, 1, 1);
108 // statusbar
109 win->statusbar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "statusbar"));
110 win->redlinelabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "redlinelabel"));
111 win->zoomlabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "zoomlabel"));
113 win->findtoolbar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findtoolbar"));
114 win->findbarlabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_label"));
115 win->findbarEntry = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_entry"));
116 win->findAll = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_findall"));
117 priv->toolbarBroadcast = true;
118 priv->partSelectorBroadcast = true;
120 gtk_container_add(GTK_CONTAINER(win), priv->container);
122 priv->m_pChildWindows = nullptr;
123 priv->m_pRenderingArgs = new GtvRenderingArgs();
126 static void
127 gtv_application_window_dispose(GObject* object)
129 GtvApplicationWindowPrivate* priv = getPrivate(GTV_APPLICATION_WINDOW(object));
131 delete priv->m_pRenderingArgs;
132 priv->m_pRenderingArgs = nullptr;
134 G_OBJECT_CLASS (gtv_application_window_parent_class)->dispose (object);
137 static void
138 gtv_application_window_class_init(GtvApplicationWindowClass* klass)
140 G_OBJECT_CLASS(klass)->dispose = gtv_application_window_dispose;
143 /// Helper function to do some tasks after widget is fully loaded (including
144 /// document load)
145 static void initWindow(GtvApplicationWindow* window)
147 GtvApplicationWindowPrivate* priv = getPrivate(window);
149 #ifdef __GNUC__
150 #pragma GCC diagnostic push
151 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
152 #endif
153 GList *focusChain = nullptr;
154 focusChain = g_list_append( focusChain, window->lokdocview );
156 gtk_container_set_focus_chain ( GTK_CONTAINER (priv->container), focusChain );
157 #ifdef __GNUC__
158 #pragma GCC diagnostic pop
159 #endif
161 // TODO: Implement progressbar in statusbar
162 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
163 if (pDocument)
165 LibreOfficeKitDocumentType eDocType = static_cast<LibreOfficeKitDocumentType>(pDocument->pClass->getDocumentType(pDocument));
166 if (eDocType == LOK_DOCTYPE_SPREADSHEET)
168 // Align to top left corner, so the tiles are in sync with the
169 // row/column bar, even when zooming out enough that not all space is
170 // used.
171 gtk_widget_set_halign(GTK_WIDGET(window->lokdocview), GTK_ALIGN_START);
172 gtk_widget_set_valign(GTK_WIDGET(window->lokdocview), GTK_ALIGN_START);
175 // By default make the document editable in a new window
176 lok_doc_view_set_edit(LOK_DOC_VIEW(window->lokdocview), true);
177 // Let toolbar adjust its button accordingly
178 gtv_main_toolbar_doc_loaded(GTV_MAIN_TOOLBAR(priv->toolbarcontainer), eDocType, true /* Edit button state */);
181 // Fill our comments sidebar
182 gboolean bTiledAnnotations;
183 g_object_get(G_OBJECT(window->lokdocview), "tiled-annotations", &bTiledAnnotations, nullptr);
184 if (!bTiledAnnotations && pDocument)
186 window->commentssidebar = gtv_comments_sidebar_new();
187 gtk_container_add(GTK_CONTAINER(priv->scrolledwindowcontainer), window->commentssidebar);
188 // fill the comments sidebar
189 gtv_comments_sidebar_view_annotations(GTV_COMMENTS_SIDEBAR(window->commentssidebar));
193 static void
194 gtv_application_open_document_callback(GObject* source_object, GAsyncResult* res, gpointer /*userdata*/)
196 LOKDocView* pDocView = LOK_DOC_VIEW (source_object);
197 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
198 GError* error = nullptr;
199 if (!lok_doc_view_open_document_finish(pDocView, res, &error))
201 GtkWidget* pDialog = gtk_message_dialog_new(GTK_WINDOW(window),
202 GTK_DIALOG_DESTROY_WITH_PARENT,
203 GTK_MESSAGE_ERROR,
204 GTK_BUTTONS_CLOSE,
205 "Error occurred while opening the document: '%s'",
206 error->message);
207 gtk_dialog_run(GTK_DIALOG(pDialog));
208 gtk_widget_destroy(pDialog);
210 g_error_free(error);
211 gtk_widget_destroy(GTK_WIDGET(pDocView));
212 gtk_main_quit();
213 return;
216 initWindow(window);
219 /// Get the visible area of the scrolled window
220 void gtv_application_window_get_visible_area(GtvApplicationWindow* pWindow, GdkRectangle* pArea)
222 GtkAdjustment* pHAdjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(pWindow->scrolledwindow));
223 GtkAdjustment* pVAdjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(pWindow->scrolledwindow));
225 pArea->x = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
226 gtk_adjustment_get_value(pHAdjustment));
227 pArea->y = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
228 gtk_adjustment_get_value(pVAdjustment));
229 pArea->width = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
230 gtk_adjustment_get_page_size(pHAdjustment));
231 pArea->height = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
232 gtk_adjustment_get_page_size(pVAdjustment));
235 void gtv_application_window_toggle_findbar(GtvApplicationWindow* window)
237 if (gtk_widget_get_visible(window->findtoolbar))
239 gtk_widget_hide(window->findtoolbar);
241 else
243 gtk_widget_show_all(window->findtoolbar);
244 gtk_widget_grab_focus(window->findtoolbar);
248 GtkToolItem* gtv_application_window_find_tool_by_unocommand(GtvApplicationWindow* window, const std::string& unoCmd)
250 GtvApplicationWindowPrivate* priv = getPrivate(window);
251 GtkToolItem* result = nullptr;
253 // Find in the first toolbar
254 GtkContainer* pToolbar1 = gtv_main_toolbar_get_first_toolbar(GTV_MAIN_TOOLBAR(priv->toolbarcontainer));
255 GtvGtkWrapper<GList> pList(gtk_container_get_children(pToolbar1),
256 [](GList* l)
258 g_list_free(l);
260 for (GList* l = pList.get(); l != nullptr; l = l->next)
262 if (GTK_IS_TOOL_BUTTON(l->data))
264 GtkToolButton* pButton = GTK_TOOL_BUTTON(l->data);
265 const gchar* pLabel = gtk_tool_button_get_label(pButton);
266 if (g_strcmp0(unoCmd.c_str(), pLabel) == 0)
268 result = GTK_TOOL_ITEM(pButton);
273 // Look in second toolbar if not found
274 GtkContainer* pToolbar2 = gtv_main_toolbar_get_second_toolbar(GTV_MAIN_TOOLBAR(priv->toolbarcontainer));
275 pList.reset(gtk_container_get_children(pToolbar2));
276 for (GList* l = pList.get(); result == nullptr && l != nullptr; l = l->next)
278 if (GTK_IS_TOOL_BUTTON(l->data))
280 GtkToolButton* pButton = GTK_TOOL_BUTTON(l->data);
281 const gchar* pLabel = gtk_tool_button_get_label(pButton);
282 if (g_strcmp0(unoCmd.c_str(), pLabel) == 0)
284 result = GTK_TOOL_ITEM(pButton);
289 return result;
292 static std::string
293 createRenderingArgsJSON(const GtvRenderingArgs* pRenderingArgs)
295 boost::property_tree::ptree aTree;
296 if (pRenderingArgs->m_bHidePageShadow)
298 aTree.put(boost::property_tree::ptree::path_type(".uno:ShowBorderShadow/type", '/'), "boolean");
299 aTree.put(boost::property_tree::ptree::path_type(".uno:ShowBorderShadow/value", '/'), false);
301 if (pRenderingArgs->m_bHideWhiteSpace)
303 aTree.put(boost::property_tree::ptree::path_type(".uno:HideWhitespace/type", '/'), "boolean");
304 aTree.put(boost::property_tree::ptree::path_type(".uno:HideWhitespace/value", '/'), true);
306 aTree.put(boost::property_tree::ptree::path_type(".uno:Author/type", '/'), "string");
307 aTree.put(boost::property_tree::ptree::path_type(".uno:Author/value", '/'), GtvHelpers::getNextAuthor());
308 std::stringstream aStream;
309 boost::property_tree::write_json(aStream, aTree);
310 return aStream.str();
313 static void setupDocView(GtvApplicationWindow* window)
315 GtvApplicationWindowPrivate* priv = getPrivate(window);
316 g_object_set(G_OBJECT(window->lokdocview),
317 "doc-password", TRUE,
318 "doc-password-to-modify", TRUE,
319 "tiled-annotations", priv->m_pRenderingArgs->m_bEnableTiledAnnotations,
320 nullptr);
322 #if GLIB_CHECK_VERSION(2,40,0)
323 g_assert_nonnull(window->lokdocview);
324 #endif
325 g_signal_connect(window->lokdocview, "edit-changed", G_CALLBACK(LOKDocViewSigHandlers::editChanged), nullptr);
326 g_signal_connect(window->lokdocview, "command-changed", G_CALLBACK(LOKDocViewSigHandlers::commandChanged), nullptr);
327 g_signal_connect(window->lokdocview, "command-result", G_CALLBACK(LOKDocViewSigHandlers::commandResult), nullptr);
328 g_signal_connect(window->lokdocview, "search-not-found", G_CALLBACK(LOKDocViewSigHandlers::searchNotFound), nullptr);
329 g_signal_connect(window->lokdocview, "search-result-count", G_CALLBACK(LOKDocViewSigHandlers::searchResultCount), nullptr);
330 g_signal_connect(window->lokdocview, "part-changed", G_CALLBACK(LOKDocViewSigHandlers::partChanged), nullptr);
331 g_signal_connect(window->lokdocview, "hyperlink-clicked", G_CALLBACK(LOKDocViewSigHandlers::hyperlinkClicked), nullptr);
332 g_signal_connect(window->lokdocview, "cursor-changed", G_CALLBACK(LOKDocViewSigHandlers::cursorChanged), nullptr);
333 g_signal_connect(window->lokdocview, "address-changed", G_CALLBACK(LOKDocViewSigHandlers::addressChanged), nullptr);
334 g_signal_connect(window->lokdocview, "formula-changed", G_CALLBACK(LOKDocViewSigHandlers::formulaChanged), nullptr);
335 g_signal_connect(window->lokdocview, "password-required", G_CALLBACK(LOKDocViewSigHandlers::passwordRequired), nullptr);
336 g_signal_connect(window->lokdocview, "comment", G_CALLBACK(LOKDocViewSigHandlers::comment), nullptr);
337 g_signal_connect(window->lokdocview, "window", G_CALLBACK(LOKDocViewSigHandlers::window), window);
339 g_signal_connect(window->lokdocview, "configure-event", G_CALLBACK(LOKDocViewSigHandlers::configureEvent), nullptr);
342 void
343 gtv_application_window_create_view_from_window(GtvApplicationWindow* window)
345 GtvApplicationWindowPrivate* priv = getPrivate(window);
346 GApplication* app = g_application_get_default();
348 GtvApplicationWindow* newWindow = GTV_APPLICATION_WINDOW(gtv_application_window_new(GTK_APPLICATION(app)));
349 const std::string aArguments = createRenderingArgsJSON(priv->m_pRenderingArgs);
350 newWindow->lokdocview = lok_doc_view_new_from_widget(LOK_DOC_VIEW(window->lokdocview), aArguments.c_str());
351 setupDocView(newWindow);
353 gtk_container_add(GTK_CONTAINER(newWindow->scrolledwindow), newWindow->lokdocview);
354 gtk_widget_show_all(newWindow->scrolledwindow);
355 gtk_window_present(GTK_WINDOW(newWindow));
357 initWindow(newWindow);
360 void
361 gtv_application_window_load_document(GtvApplicationWindow* window,
362 const GtvRenderingArgs* aArgs,
363 const std::string& aDocPath)
365 GtvApplicationWindowPrivate* priv = getPrivate(window);
366 // keep a copy of it; we need to use these for creating new views later
367 *(priv->m_pRenderingArgs) = *aArgs;
369 // setup lokdocview
370 const char *pUserProfile = priv->m_pRenderingArgs->m_aUserProfile.empty() ?
371 nullptr : priv->m_pRenderingArgs->m_aUserProfile.c_str();
373 window->lokdocview = GTK_WIDGET(
374 g_initable_new(LOK_TYPE_DOC_VIEW, nullptr, nullptr,
375 "lopath", priv->m_pRenderingArgs->m_aLoPath.c_str(),
376 "unipoll", priv->m_pRenderingArgs->m_bUnipoll,
377 "userprofileurl", pUserProfile,
378 "halign", GTK_ALIGN_CENTER,
379 "valign", GTK_ALIGN_CENTER,
380 nullptr));
382 gtk_container_add(GTK_CONTAINER(window->scrolledwindow), window->lokdocview);
384 setupDocView(window);
386 // Create argument JSON
387 const std::string aArguments = createRenderingArgsJSON(priv->m_pRenderingArgs);
388 lok_doc_view_open_document(LOK_DOC_VIEW(window->lokdocview), aDocPath.c_str(),
389 aArguments.c_str(), nullptr,
390 gtv_application_open_document_callback, window->lokdocview);
392 gtk_widget_show_all(GTK_WIDGET(window->scrolledwindow));
395 GtvMainToolbar*
396 gtv_application_window_get_main_toolbar(GtvApplicationWindow* window)
398 GtvApplicationWindowPrivate* priv = getPrivate(window);
399 return GTV_MAIN_TOOLBAR(priv->toolbarcontainer);
402 void
403 gtv_application_window_set_toolbar_broadcast(GtvApplicationWindow* window, bool broadcast)
405 GtvApplicationWindowPrivate* priv = getPrivate(window);
406 priv->toolbarBroadcast = broadcast;
409 gboolean
410 gtv_application_window_get_toolbar_broadcast(GtvApplicationWindow* window)
412 GtvApplicationWindowPrivate* priv = getPrivate(window);
413 return priv->toolbarBroadcast;
416 void
417 gtv_application_window_set_part_broadcast(GtvApplicationWindow* window, bool broadcast)
419 GtvApplicationWindowPrivate* priv = getPrivate(window);
420 priv->partSelectorBroadcast = broadcast;
423 gboolean
424 gtv_application_window_get_part_broadcast(GtvApplicationWindow* window)
426 GtvApplicationWindowPrivate* priv = getPrivate(window);
427 return priv->partSelectorBroadcast;
430 void
431 gtv_application_window_register_child_window(GtvApplicationWindow* window, GtkWindow* pChildWin)
433 guint dialogid = 0;
434 g_object_get(G_OBJECT(pChildWin), "dialogid", &dialogid, nullptr);
435 g_debug("Register child window: dialogid [%d] in window[%p]", dialogid, window);
436 GtvApplicationWindowPrivate* priv = getPrivate(window);
437 if (pChildWin)
438 priv->m_pChildWindows = g_list_append(priv->m_pChildWindows, pChildWin);
441 void
442 gtv_application_window_unregister_child_window(GtvApplicationWindow* window, GtkWindow* pChildWin)
444 guint dialogid = 0;
445 g_object_get(G_OBJECT(pChildWin), "dialogid", &dialogid, nullptr);
446 g_debug("Unregister child window: dialogid [%d] in window[%p]", dialogid, window);
447 GtvApplicationWindowPrivate* priv = getPrivate(window);
448 if (pChildWin)
450 priv->m_pChildWindows = g_list_remove(priv->m_pChildWindows, pChildWin);
451 LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
452 guint dialogId = 0;
453 g_object_get(G_OBJECT(pChildWin), "dialogid", &dialogId, nullptr);
454 pDocument->pClass->postWindow(pDocument, dialogId, LOK_WINDOW_CLOSE, nullptr);
458 GtkWindow*
459 gtv_application_window_get_child_window_by_id(GtvApplicationWindow* window, guint nWinId)
461 GtvApplicationWindowPrivate* priv = getPrivate(window);
462 GList* pIt = nullptr;
463 GtkWindow* ret = nullptr;
464 // For now, only dialogs are registered as child window
465 for (pIt = priv->m_pChildWindows; pIt != nullptr; pIt = pIt->next)
467 guint dialogId = 0;
468 g_object_get(G_OBJECT(pIt->data), "dialogid", &dialogId, nullptr);
469 if (dialogId == nWinId)
471 ret = GTK_WINDOW(pIt->data);
472 break;
476 return ret;
479 GtkWidget*
480 gtv_application_window_get_parent(GtvApplicationWindow* window, guint nWinId)
482 GtvApplicationWindowPrivate* priv = getPrivate(window);
483 GList* pIt = nullptr;
484 for (pIt = priv->m_pChildWindows; pIt != nullptr; pIt = pIt->next)
486 if (gtv_lok_dialog_is_parent_of(GTV_LOK_DIALOG(pIt->data), nWinId))
487 return GTK_WIDGET(pIt->data);
489 return nullptr;
492 GtvApplicationWindow*
493 gtv_application_window_new(GtkApplication* app)
495 g_return_val_if_fail(GTK_IS_APPLICATION(app), nullptr);
497 return GTV_APPLICATION_WINDOW(g_object_new(GTV_TYPE_APPLICATION_WINDOW,
498 "application", app,
499 "width-request", 1024,
500 "height-request", 768,
501 "title", "LibreOffice GtkTiledViewer",
502 "window-position", GTK_WIN_POS_CENTER,
503 "show-menubar", false,
504 nullptr));
507 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */