1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <gtk/gtk.h>
#include <memory>
#include <LibreOfficeKit/LibreOfficeKitGtk.h>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
#include <gtv-application-window.hxx>
#include <gtv-main-toolbar.hxx>
#include <gtv-helpers.hxx>
#include <gtv-signal-handlers.hxx>
#include <gtv-lokdocview-signal-handlers.hxx>
#include <gtv-calc-header-bar.hxx>
#include <gtv-comments-sidebar.hxx>
#include <boost/property_tree/json_parser.hpp>
#include <boost/optional.hpp>
struct GtvApplicationWindowPrivate
{
GtkWidget* container;
GtkWidget* gridcontainer;
GtkWidget* toolbarcontainer;
GtkWidget* scrolledwindowcontainer;
GtkWidget* lokDialog;
gboolean toolbarBroadcast;
gboolean partSelectorBroadcast;
GList* m_pChildWindows;
// Rendering args; options with which lokdocview was rendered in this window
GtvRenderingArgs* m_pRenderingArgs;
};
G_DEFINE_TYPE_WITH_PRIVATE(GtvApplicationWindow, gtv_application_window, GTK_TYPE_APPLICATION_WINDOW);
static GtvApplicationWindowPrivate*
getPrivate(GtvApplicationWindow* win)
{
return static_cast<GtvApplicationWindowPrivate*>(gtv_application_window_get_instance_private(win));
}
static void
gtv_application_window_init(GtvApplicationWindow* win)
{
const std::string uiFilePath = GtvHelpers::getDirPath(__FILE__) + std::string(UI_FILE_NAME);
GtvGtkWrapper<GtkBuilder> builder(gtk_builder_new_from_file(uiFilePath.c_str()),
[](GtkBuilder* pBuilder) {
g_object_unref(pBuilder);
});
GtvApplicationWindowPrivate* priv = getPrivate(win);
// This is the parent GtkBox holding everything
priv->container = GTK_WIDGET(gtk_builder_get_object(builder.get(), "container"));
// Toolbar container
priv->toolbarcontainer = gtv_main_toolbar_new();
// Attach to the toolbar to main window
gtk_box_pack_start(GTK_BOX(priv->container), priv->toolbarcontainer, false, false, false);
gtk_box_reorder_child(GTK_BOX(priv->container), priv->toolbarcontainer, 0);
priv->gridcontainer = GTK_WIDGET(gtk_builder_get_object(builder.get(), "maingrid"));
// scrolled window containing the main drawing area
win->scrolledwindow = GTK_WIDGET(gtk_builder_get_object(builder.get(), "scrolledwindow"));
// scrolledwindow container
priv->scrolledwindowcontainer = GTK_WIDGET(gtk_builder_get_object(builder.get(), "scrolledwindowcontainer"));
GtkAdjustment* pHAdjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(win->scrolledwindow));
g_signal_connect(pHAdjustment, "value-changed", G_CALLBACK(docAdjustmentChanged), win);
GtkAdjustment* pVAdjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(win->scrolledwindow));
g_signal_connect(pVAdjustment, "value-changed", G_CALLBACK(docAdjustmentChanged), win);
// calc header row bar
win->cornerarea = gtv_calc_header_bar_new();
gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->cornerarea), CalcHeaderType::CORNER);
win->rowbar = gtv_calc_header_bar_new();
gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->rowbar), CalcHeaderType::ROW);
win->columnbar = gtv_calc_header_bar_new();
gtv_calc_header_bar_set_type_and_width(GTV_CALC_HEADER_BAR(win->columnbar), CalcHeaderType::COLUMN);
// attach row/column/corner to the container
gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->cornerarea, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->rowbar, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(priv->gridcontainer), win->columnbar, 1, 0, 1, 1);
// statusbar
win->statusbar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "statusbar"));
win->redlinelabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "redlinelabel"));
win->zoomlabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "zoomlabel"));
win->findtoolbar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findtoolbar"));
win->findbarlabel = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_label"));
win->findbarEntry = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_entry"));
win->findAll = GTK_WIDGET(gtk_builder_get_object(builder.get(), "findbar_findall"));
priv->toolbarBroadcast = true;
priv->partSelectorBroadcast = true;
gtk_container_add(GTK_CONTAINER(win), priv->container);
priv->m_pChildWindows = nullptr;
priv->m_pRenderingArgs = new GtvRenderingArgs();
}
static void
gtv_application_window_dispose(GObject* object)
{
GtvApplicationWindowPrivate* priv = getPrivate(GTV_APPLICATION_WINDOW(object));
delete priv->m_pRenderingArgs;
priv->m_pRenderingArgs = nullptr;
G_OBJECT_CLASS (gtv_application_window_parent_class)->dispose (object);
}
static void
gtv_application_window_class_init(GtvApplicationWindowClass* klass)
{
G_OBJECT_CLASS(klass)->dispose = gtv_application_window_dispose;
}
/// Helper function to do some tasks after widget is fully loaded (including
/// document load)
static void initWindow(GtvApplicationWindow* window)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
GList *focusChain = nullptr;
focusChain = g_list_append( focusChain, window->lokdocview );
gtk_container_set_focus_chain ( GTK_CONTAINER (priv->container), focusChain );
// TODO: Implement progressbar in statusbar
LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
if (pDocument)
{
LibreOfficeKitDocumentType eDocType = static_cast<LibreOfficeKitDocumentType>(pDocument->pClass->getDocumentType(pDocument));
if (eDocType == LOK_DOCTYPE_SPREADSHEET)
{
// Align to top left corner, so the tiles are in sync with the
// row/column bar, even when zooming out enough that not all space is
// used.
gtk_widget_set_halign(GTK_WIDGET(window->lokdocview), GTK_ALIGN_START);
gtk_widget_set_valign(GTK_WIDGET(window->lokdocview), GTK_ALIGN_START);
}
// By default make the document editable in a new window
lok_doc_view_set_edit(LOK_DOC_VIEW(window->lokdocview), true);
// Let toolbar adjust its button accordingly
gtv_main_toolbar_doc_loaded(GTV_MAIN_TOOLBAR(priv->toolbarcontainer), eDocType, true /* Edit button state */);
}
// Fill our comments sidebar
gboolean bTiledAnnotations;
g_object_get(G_OBJECT(window->lokdocview), "tiled-annotations", &bTiledAnnotations, nullptr);
if (!bTiledAnnotations && pDocument)
{
window->commentssidebar = gtv_comments_sidebar_new();
gtk_container_add(GTK_CONTAINER(priv->scrolledwindowcontainer), window->commentssidebar);
// fill the comments sidebar
gtv_comments_sidebar_view_annotations(GTV_COMMENTS_SIDEBAR(window->commentssidebar));
}
}
static void
gtv_application_open_document_callback(GObject* source_object, GAsyncResult* res, gpointer /*userdata*/)
{
LOKDocView* pDocView = LOK_DOC_VIEW (source_object);
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
GError* error = nullptr;
if (!lok_doc_view_open_document_finish(pDocView, res, &error))
{
GtkWidget* pDialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE,
"Error occurred while opening the document: '%s'",
error->message);
gtk_dialog_run(GTK_DIALOG(pDialog));
gtk_widget_destroy(pDialog);
g_error_free(error);
gtk_widget_destroy(GTK_WIDGET(pDocView));
gtk_main_quit();
return;
}
initWindow(window);
}
/// Get the visible area of the scrolled window
void gtv_application_window_get_visible_area(GtvApplicationWindow* pWindow, GdkRectangle* pArea)
{
GtkAdjustment* pHAdjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(pWindow->scrolledwindow));
GtkAdjustment* pVAdjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(pWindow->scrolledwindow));
pArea->x = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
gtk_adjustment_get_value(pHAdjustment));
pArea->y = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
gtk_adjustment_get_value(pVAdjustment));
pArea->width = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
gtk_adjustment_get_page_size(pHAdjustment));
pArea->height = lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(pWindow->lokdocview),
gtk_adjustment_get_page_size(pVAdjustment));
}
void gtv_application_window_toggle_findbar(GtvApplicationWindow* window)
{
if (gtk_widget_get_visible(window->findtoolbar))
{
gtk_widget_hide(window->findtoolbar);
}
else
{
gtk_widget_show_all(window->findtoolbar);
gtk_widget_grab_focus(window->findtoolbar);
}
}
GtkToolItem* gtv_application_window_find_tool_by_unocommand(GtvApplicationWindow* window, const std::string& unoCmd)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
GtkToolItem* result = nullptr;
// Find in the first toolbar
GtkContainer* pToolbar1 = gtv_main_toolbar_get_first_toolbar(GTV_MAIN_TOOLBAR(priv->toolbarcontainer));
GtvGtkWrapper<GList> pList(gtk_container_get_children(pToolbar1),
[](GList* l)
{
g_list_free(l);
});
for (GList* l = pList.get(); l != nullptr; l = l->next)
{
if (GTK_IS_TOOL_BUTTON(l->data))
{
GtkToolButton* pButton = GTK_TOOL_BUTTON(l->data);
const gchar* pLabel = gtk_tool_button_get_label(pButton);
if (g_strcmp0(unoCmd.c_str(), pLabel) == 0)
{
result = GTK_TOOL_ITEM(pButton);
}
}
}
// Look in second toolbar if not found
GtkContainer* pToolbar2 = gtv_main_toolbar_get_second_toolbar(GTV_MAIN_TOOLBAR(priv->toolbarcontainer));
pList.reset(gtk_container_get_children(pToolbar2));
for (GList* l = pList.get(); result == nullptr && l != nullptr; l = l->next)
{
if (GTK_IS_TOOL_BUTTON(l->data))
{
GtkToolButton* pButton = GTK_TOOL_BUTTON(l->data);
const gchar* pLabel = gtk_tool_button_get_label(pButton);
if (g_strcmp0(unoCmd.c_str(), pLabel) == 0)
{
result = GTK_TOOL_ITEM(pButton);
}
}
}
return result;
}
static const std::string
createRenderingArgsJSON(const GtvRenderingArgs* pRenderingArgs)
{
boost::property_tree::ptree aTree;
if (pRenderingArgs->m_bHidePageShadow)
{
aTree.put(boost::property_tree::ptree::path_type(".uno:ShowBorderShadow/type", '/'), "boolean");
aTree.put(boost::property_tree::ptree::path_type(".uno:ShowBorderShadow/value", '/'), false);
}
if (pRenderingArgs->m_bHideWhiteSpace)
{
aTree.put(boost::property_tree::ptree::path_type(".uno:HideWhitespace/type", '/'), "boolean");
aTree.put(boost::property_tree::ptree::path_type(".uno:HideWhitespace/value", '/'), true);
}
aTree.put(boost::property_tree::ptree::path_type(".uno:Author/type", '/'), "string");
aTree.put(boost::property_tree::ptree::path_type(".uno:Author/value", '/'), GtvHelpers::getNextAuthor());
std::stringstream aStream;
boost::property_tree::write_json(aStream, aTree);
return aStream.str();
}
static void setupDocView(GtvApplicationWindow* window)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
g_object_set(G_OBJECT(window->lokdocview),
"doc-password", TRUE,
"doc-password-to-modify", TRUE,
"tiled-annotations", priv->m_pRenderingArgs->m_bEnableTiledAnnotations,
nullptr);
#if GLIB_CHECK_VERSION(2,40,0)
g_assert_nonnull(window->lokdocview);
#endif
g_signal_connect(window->lokdocview, "edit-changed", G_CALLBACK(LOKDocViewSigHandlers::editChanged), nullptr);
g_signal_connect(window->lokdocview, "command-changed", G_CALLBACK(LOKDocViewSigHandlers::commandChanged), nullptr);
g_signal_connect(window->lokdocview, "command-result", G_CALLBACK(LOKDocViewSigHandlers::commandResult), nullptr);
g_signal_connect(window->lokdocview, "search-not-found", G_CALLBACK(LOKDocViewSigHandlers::searchNotFound), nullptr);
g_signal_connect(window->lokdocview, "search-result-count", G_CALLBACK(LOKDocViewSigHandlers::searchResultCount), nullptr);
g_signal_connect(window->lokdocview, "part-changed", G_CALLBACK(LOKDocViewSigHandlers::partChanged), nullptr);
g_signal_connect(window->lokdocview, "hyperlink-clicked", G_CALLBACK(LOKDocViewSigHandlers::hyperlinkClicked), nullptr);
g_signal_connect(window->lokdocview, "cursor-changed", G_CALLBACK(LOKDocViewSigHandlers::cursorChanged), nullptr);
g_signal_connect(window->lokdocview, "address-changed", G_CALLBACK(LOKDocViewSigHandlers::addressChanged), nullptr);
g_signal_connect(window->lokdocview, "formula-changed", G_CALLBACK(LOKDocViewSigHandlers::formulaChanged), nullptr);
g_signal_connect(window->lokdocview, "password-required", G_CALLBACK(LOKDocViewSigHandlers::passwordRequired), nullptr);
g_signal_connect(window->lokdocview, "comment", G_CALLBACK(LOKDocViewSigHandlers::comment), nullptr);
g_signal_connect(window->lokdocview, "dialog-invalidate", G_CALLBACK(LOKDocViewSigHandlers::dialogInvalidate), nullptr);
g_signal_connect(window->lokdocview, "configure-event", G_CALLBACK(LOKDocViewSigHandlers::configureEvent), nullptr);
}
void
gtv_application_window_create_view_from_window(GtvApplicationWindow* window)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
GApplication* app = g_application_get_default();
GtvApplicationWindow* newWindow = GTV_APPLICATION_WINDOW(gtv_application_window_new(GTK_APPLICATION(app)));
const std::string aArguments = createRenderingArgsJSON(priv->m_pRenderingArgs);
newWindow->lokdocview = lok_doc_view_new_from_widget(LOK_DOC_VIEW(window->lokdocview), aArguments.c_str());
setupDocView(newWindow);
gtk_container_add(GTK_CONTAINER(newWindow->scrolledwindow), newWindow->lokdocview);
gtk_widget_show_all(newWindow->scrolledwindow);
gtk_window_present(GTK_WINDOW(newWindow));
initWindow(newWindow);
}
void
gtv_application_window_load_document(GtvApplicationWindow* window,
const GtvRenderingArgs* aArgs,
const std::string& aDocPath)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
// keep a copy of it; we need to use these for creating new views later
*(priv->m_pRenderingArgs) = *aArgs;
// setup lokdocview
window->lokdocview = lok_doc_view_new_from_user_profile(priv->m_pRenderingArgs->m_aLoPath.c_str(),
priv->m_pRenderingArgs->m_aUserProfile.empty() ? nullptr : priv->m_pRenderingArgs->m_aUserProfile.c_str(),
nullptr, nullptr);
gtk_container_add(GTK_CONTAINER(window->scrolledwindow), window->lokdocview);
setupDocView(window);
// Create argument JSON
const std::string aArguments = createRenderingArgsJSON(priv->m_pRenderingArgs);
lok_doc_view_open_document(LOK_DOC_VIEW(window->lokdocview), aDocPath.c_str(),
aArguments.c_str(), nullptr,
gtv_application_open_document_callback, window->lokdocview);
gtk_widget_show_all(GTK_WIDGET(window->scrolledwindow));
}
GtvMainToolbar*
gtv_application_window_get_main_toolbar(GtvApplicationWindow* window)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
return GTV_MAIN_TOOLBAR(priv->toolbarcontainer);
}
void
gtv_application_window_set_toolbar_broadcast(GtvApplicationWindow* window, bool broadcast)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
priv->toolbarBroadcast = broadcast;
}
gboolean
gtv_application_window_get_toolbar_broadcast(GtvApplicationWindow* window)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
return priv->toolbarBroadcast;
}
void
gtv_application_window_set_part_broadcast(GtvApplicationWindow* window, bool broadcast)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
priv->partSelectorBroadcast = broadcast;
}
gboolean
gtv_application_window_get_part_broadcast(GtvApplicationWindow* window)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
return priv->partSelectorBroadcast;
}
void
gtv_application_window_register_child_window(GtvApplicationWindow* window, GtkWindow* pChildWin)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
if (pChildWin)
priv->m_pChildWindows = g_list_append(priv->m_pChildWindows, pChildWin);
}
void
gtv_application_window_unregister_child_window(GtvApplicationWindow* window, GtkWindow* pChildWin)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
if (pChildWin)
priv->m_pChildWindows = g_list_remove(priv->m_pChildWindows, pChildWin);
}
GtkWindow*
gtv_application_window_get_child_window_by_id(GtvApplicationWindow* window, const gchar* pWinId)
{
GtvApplicationWindowPrivate* priv = getPrivate(window);
GList* pIt = nullptr;
GtkWindow* ret = nullptr;
// For now, only dialogs are registered as child window
for (pIt = priv->m_pChildWindows; pIt != nullptr; pIt = pIt->next)
{
gchar* dialogId = nullptr;
g_object_get(G_OBJECT(pIt->data), "dialogid", &dialogId, nullptr);
// prepend .uno:
gchar* completeWinId = nullptr;
if (pWinId != nullptr)
{
completeWinId = g_strconcat(".uno:", pWinId, nullptr);
}
if (dialogId != nullptr && g_str_equal(dialogId, completeWinId))
{
ret = GTK_WINDOW(pIt->data);
break;
}
}
return ret;
}
GtvApplicationWindow*
gtv_application_window_new(GtkApplication* app)
{
g_return_val_if_fail(GTK_IS_APPLICATION(app), nullptr);
return GTV_APPLICATION_WINDOW(g_object_new(GTV_TYPE_APPLICATION_WINDOW,
"application", app,
"width-request", 1024,
"height-request", 768,
"title", "LibreOffice GtkTiledViewer",
"window-position", GTK_WIN_POS_CENTER,
"show-menubar", false,
nullptr));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|