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
|
/* -*- 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 "gtv-application-window.hxx"
#include "gtv-helpers.hxx"
#include "gtv-signal-handlers.hxx"
#include "gtv-calc-header-bar.hxx"
#include "gtv-comments-sidebar.hxx"
#include "gtv-lokdocview-signal-handlers.hxx"
#include "gtv-lok-dialog.hxx"
#include <boost/property_tree/json_parser.hpp>
#include <boost/optional.hpp>
#include <iostream>
static gboolean deleteLokDialog(GtkWidget* pWidget, GdkEvent* /*event*/, gpointer userdata)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(userdata);
g_info("deleteLokDialog");
gtv_application_window_unregister_child_window(window, GTK_WINDOW(pWidget));
return FALSE;
}
static gboolean destroyLokDialog(GtkWidget* pWidget, gpointer userdata)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(userdata);
g_info("destroyLokDialog");
gtv_application_window_unregister_child_window(window, GTK_WINDOW(pWidget));
return FALSE;
}
void LOKDocViewSigHandlers::editChanged(LOKDocView* pDocView, gboolean bWasEdit, gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
gboolean bEdit = lok_doc_view_get_edit(LOK_DOC_VIEW(window->lokdocview));
g_info("signalEdit: %d -> %d", bWasEdit, bEdit);
// Let the main toolbar know, so that it can enable disable the button
GtvMainToolbar* pMainToolbar = gtv_application_window_get_main_toolbar(GTV_APPLICATION_WINDOW(window));
gtv_main_toolbar_set_edit(pMainToolbar, bEdit);
}
void LOKDocViewSigHandlers::commandChanged(LOKDocView* pDocView, char* pPayload, gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
std::string aPayload(pPayload);
size_t nPosition = aPayload.find('=');
if (nPosition != std::string::npos)
{
const std::string aKey = aPayload.substr(0, nPosition);
const std::string aValue = aPayload.substr(nPosition + 1);
GtkToolItem* pItem = gtv_application_window_find_tool_by_unocommand(window, aKey);
if (pItem != nullptr)
{
if (aValue == "true" || aValue == "false") {
gboolean bEdit = aValue == "true";
if (gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(pItem)) != bEdit)
{
// Avoid invoking lok_doc_view_post_command().
// FIXME: maybe block/unblock the signal (see
// g_signal_handlers_block_by_func) ?
gtv_application_window_set_toolbar_broadcast(window, false);
gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(pItem), bEdit);
gtv_application_window_set_toolbar_broadcast(window, true);
}
} else if (aValue == "enabled" || aValue == "disabled") {
gboolean bSensitive = aValue == "enabled";
gtk_widget_set_sensitive(GTK_WIDGET(pItem), bSensitive);
// Remember state, so in case edit is disable and enabled
// later, the correct sensitivity can be restored.
GtvMainToolbar* pMainToolbar = gtv_application_window_get_main_toolbar(window);
gtv_main_toolbar_set_sensitive_internal(pMainToolbar, pItem, bSensitive);
}
}
else if (aKey == ".uno:TrackedChangeIndex")
{
std::string aText = std::string("Current redline: ");
if (aValue.empty())
aText += "none";
else
aText += aValue;
gtk_label_set_text(GTK_LABEL(window->redlinelabel), aText.c_str());
}
}
}
void LOKDocViewSigHandlers::commandResult(LOKDocView*, char* pPayload, gpointer)
{
fprintf(stderr, "Command finished: %s\n", pPayload);
}
void LOKDocViewSigHandlers::searchNotFound(LOKDocView* pDocView, char* , gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
gtk_label_set_text(GTK_LABEL(window->findbarlabel), "Search key not found");
}
void LOKDocViewSigHandlers::searchResultCount(LOKDocView* pDocView, char* pPayload, gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
std::stringstream ss;
ss << pPayload << " match(es)";
gtk_label_set_text(GTK_LABEL(window->findbarlabel), ss.str().c_str());
}
void LOKDocViewSigHandlers::partChanged(LOKDocView* /*pDocView*/, int, gpointer)
{
// GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
//rWindow.m_bPartSelectorBroadcast = false;
// gtk_combo_box_set_active(GTK_COMBO_BOX(rWindow.m_pPartSelector), nPart);
// rWindow.m_bPartSelectorBroadcast = true;
}
void LOKDocViewSigHandlers::hyperlinkClicked(LOKDocView* pDocView, char* pPayload, gpointer)
{
GError* pError = nullptr;
#if GTK_CHECK_VERSION(3,22,0)
gtk_show_uri_on_window(
GTK_WINDOW (gtk_widget_get_toplevel(GTK_WIDGET(pDocView))),
pPayload, GDK_CURRENT_TIME, &pError);
#else
(void) pDocView;
gtk_show_uri(nullptr, pPayload, GDK_CURRENT_TIME, &pError);
#endif
if (pError != nullptr)
{
g_warning("Unable to show URI %s : %s", pPayload, pError->message);
g_error_free(pError);
}
}
void LOKDocViewSigHandlers::cursorChanged(LOKDocView* pDocView, gint nX, gint nY,
gint /*nWidth*/, gint /*nHeight*/, gpointer /*pData*/)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
GtkAdjustment* vadj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(window->scrolledwindow));
GtkAdjustment* hadj = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(window->scrolledwindow));
GdkRectangle visArea;
gdouble upper;
gint x = -1, y = -1;
gtv_application_window_get_visible_area(window, &visArea);
// check vertically
if (nY < visArea.y)
{
y = nY - visArea.height/2;
if (y < 0)
y = gtk_adjustment_get_lower(vadj);
}
else if (nY > visArea.y + visArea.height)
{
y = nY - visArea.height/2;
upper = lok_doc_view_pixel_to_twip(pDocView, gtk_adjustment_get_upper(vadj));
if (y > upper)
y = upper;
}
if (nX < visArea.x)
{
x = nX - visArea.width/2;
if (x < 0)
x = gtk_adjustment_get_lower(hadj);
}
else if (nX > visArea.x + visArea.width)
{
x = nX - visArea.width/2;
upper = lok_doc_view_pixel_to_twip(pDocView, gtk_adjustment_get_upper(hadj));
if (x > upper)
x = upper;
}
if (y!=-1)
gtk_adjustment_set_value(vadj, lok_doc_view_twip_to_pixel(pDocView, y));
if (x!=-1)
gtk_adjustment_set_value(hadj, lok_doc_view_twip_to_pixel(pDocView, x));
}
void LOKDocViewSigHandlers::addressChanged(LOKDocView* pDocView, char* pPayload, gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
GtvMainToolbar* toolbar = gtv_application_window_get_main_toolbar(window);
GtkEntry* pAddressbar = GTK_ENTRY(toolbar->m_pAddressbar);
gtk_entry_set_text(pAddressbar, pPayload);
}
void LOKDocViewSigHandlers::formulaChanged(LOKDocView* pDocView, char* pPayload, gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
GtvMainToolbar* toolbar = gtv_application_window_get_main_toolbar(window);
GtkEntry* pFormulabar = GTK_ENTRY(toolbar->m_pFormulabar);
gtk_entry_set_text(pFormulabar, pPayload);
}
void LOKDocViewSigHandlers::passwordRequired(LOKDocView* pDocView, char* pUrl, gboolean bModify, gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
GtkWidget* pPasswordDialog = gtk_dialog_new_with_buttons ("Password required",
GTK_WINDOW (window),
GTK_DIALOG_MODAL,
"OK",
GTK_RESPONSE_OK,
nullptr);
g_object_set(G_OBJECT(pPasswordDialog), "resizable", FALSE, nullptr);
GtkWidget* pDialogMessageArea = gtk_dialog_get_content_area (GTK_DIALOG (pPasswordDialog));
GtkWidget* pPasswordEntry = gtk_entry_new ();
gtk_entry_set_visibility (GTK_ENTRY(pPasswordEntry), FALSE);
gtk_entry_set_invisible_char (GTK_ENTRY(pPasswordEntry), '*');
gtk_box_pack_end(GTK_BOX(pDialogMessageArea), pPasswordEntry, TRUE, TRUE, 2);
if (bModify)
{
GtkWidget* pSecondaryLabel = gtk_label_new ("Document requires password to edit");
gtk_box_pack_end(GTK_BOX(pDialogMessageArea), pSecondaryLabel, TRUE, TRUE, 2);
gtk_dialog_add_button (GTK_DIALOG (pPasswordDialog), "Open as read-only", GTK_RESPONSE_ACCEPT);
}
gtk_widget_show_all(pPasswordDialog);
gint res = gtk_dialog_run (GTK_DIALOG(pPasswordDialog));
switch (res)
{
case GTK_RESPONSE_OK:
lok_doc_view_set_document_password (LOK_DOC_VIEW(window->lokdocview), pUrl, gtk_entry_get_text(GTK_ENTRY(pPasswordEntry)));
break;
case GTK_RESPONSE_ACCEPT:
// User accepts to open this document as read-only
case GTK_RESPONSE_DELETE_EVENT:
lok_doc_view_set_document_password (LOK_DOC_VIEW(window->lokdocview), pUrl, nullptr);
break;
}
gtk_widget_destroy(pPasswordDialog);
}
void LOKDocViewSigHandlers::comment(LOKDocView* pDocView, gchar* pComment, gpointer)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pDocView)));
std::stringstream aStream(pComment);
boost::property_tree::ptree aRoot;
boost::property_tree::read_json(aStream, aRoot);
boost::property_tree::ptree aComment = aRoot.get_child("comment");
GtvCommentsSidebar* sidebar = GTV_COMMENTS_SIDEBAR(window->commentssidebar);
GtkWidget* pCommentsGrid = sidebar->commentsgrid;
GtvGtkWrapper<GList> pChildren(gtk_container_get_children(GTK_CONTAINER(pCommentsGrid)),
[](GList* l)
{
g_list_free(l);
});
GtkWidget* pSelf = nullptr;
GtkWidget* pParent = nullptr;
for (GList* l = pChildren.get(); l != nullptr; l = l->next)
{
gchar *id = static_cast<gchar*>(g_object_get_data(G_OBJECT(l->data), "id"));
if (g_strcmp0(id, aComment.get<std::string>("id").c_str()) == 0)
pSelf = GTK_WIDGET(l->data);
// There is no 'parent' in Remove callbacks
if (g_strcmp0(id, aComment.get("parent", std::string("0")).c_str()) == 0)
pParent = GTK_WIDGET(l->data);
}
if (aComment.get<std::string>("action") == "Remove")
{
if (pSelf)
gtk_widget_destroy(pSelf);
else
g_warning("Can't find the comment to remove in the list !!");
}
else if (aComment.get<std::string>("action") == "Add" || aComment.get<std::string>("action") == "Modify")
{
GtkWidget* pCommentBox = GtvHelpers::createCommentBox(aComment);
if (pSelf != nullptr || pParent != nullptr)
{
gtk_grid_insert_next_to(GTK_GRID(pCommentsGrid), pSelf != nullptr ? pSelf : pParent, GTK_POS_BOTTOM);
gtk_grid_attach_next_to(GTK_GRID(pCommentsGrid), pCommentBox, pSelf != nullptr ? pSelf : pParent, GTK_POS_BOTTOM, 1, 1);
}
else
gtk_container_add(GTK_CONTAINER(pCommentsGrid), pCommentBox);
gtk_widget_show_all(pCommentBox);
// We added the widget already below the existing one, so destroy the
// already existing one now
if (pSelf)
gtk_widget_destroy(pSelf);
}
}
void LOKDocViewSigHandlers::window(LOKDocView* pDocView, gchar* pPayload, gpointer pData)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(pData);
std::stringstream aStream(pPayload);
boost::property_tree::ptree aRoot;
boost::property_tree::read_json(aStream, aRoot);
const unsigned nWinId = aRoot.get<unsigned>("id");
const std::string aAction = aRoot.get<std::string>("action");
if (aAction == "created")
{
const std::string aType = aRoot.get<std::string>("type");
const std::string aSize = aRoot.get<std::string>("size");
std::vector<int> aSizePoints = GtvHelpers::split<int>(aSize, ", ", 2);
if (aType == "dialog")
{
GtkWidget* pDialog = gtv_lok_dialog_new(pDocView, nWinId, aSizePoints[0], aSizePoints[1]);
g_info("created dialog, for dialogid: %d with size: %s", nWinId, aSize.c_str());
gtv_application_window_register_child_window(window, GTK_WINDOW(pDialog));
g_signal_connect(pDialog, "destroy", G_CALLBACK(destroyLokDialog), window);
g_signal_connect(pDialog, "delete-event", G_CALLBACK(deleteLokDialog), window);
gtk_window_set_resizable(GTK_WINDOW(pDialog), false);
gtk_widget_show_all(GTK_WIDGET(pDialog));
gtk_window_present(GTK_WINDOW(pDialog));
}
else if (aType == "child")
{
const unsigned nParentId = std::atoi(aRoot.get<std::string>("parentId").c_str());
GtkWindow* pDialog = gtv_application_window_get_child_window_by_id(window, nParentId);
const std::string aPos = aRoot.get<std::string>("position");
std::vector<int> aPosPoints = GtvHelpers::split<int>(aPos, ", ", 2);
gtv_lok_dialog_child_create(GTV_LOK_DIALOG(pDialog), nWinId, aPosPoints[0], aPosPoints[1], aSizePoints[0], aSizePoints[1]);
}
}
else
{
// check if it's a child window
GtkWidget* pParent = gtv_application_window_get_parent(window, nWinId);
if (pParent) // it's a floating window in the dialog
{
if (aAction == "invalidate")
gtv_lok_dialog_child_invalidate(GTV_LOK_DIALOG(pParent));
else if (aAction == "close")
gtv_lok_dialog_child_close(GTV_LOK_DIALOG(pParent));
}
else if (GtkWindow* pDialog = gtv_application_window_get_child_window_by_id(window, nWinId))
{ // it's the dialog window itself
if (aAction == "close")
gtk_widget_destroy(GTK_WIDGET(pDialog));
else if (aAction == "size_changed")
{
const std::string aSize = aRoot.get<std::string>("size");
std::vector<int> aSizePoints = GtvHelpers::split<int>(aSize, ", ", 2);
if (aSizePoints.size() != 2)
{
g_error("Malformed size_changed callback");
return;
}
g_object_set(G_OBJECT(pDialog),
"width", aSizePoints[0],
"height", aSizePoints[1],
nullptr);
GdkRectangle aGdkRectangle = {0, 0, 0, 0};
gtv_lok_dialog_invalidate(GTV_LOK_DIALOG(pDialog), aGdkRectangle);
}
else if (aAction == "invalidate")
{
GdkRectangle aGdkRectangle = {0, 0, 0, 0};
try
{
const std::string aRectangle = aRoot.get<std::string>("rectangle");
std::vector<int> aRectPoints = GtvHelpers::split<int>(aRectangle, ", ", 4);
if (aRectPoints.size() == 4)
aGdkRectangle = {aRectPoints[0], aRectPoints[1], aRectPoints[2], aRectPoints[3]};
}
catch(const std::exception& e)
{}
gtv_lok_dialog_invalidate(GTV_LOK_DIALOG(pDialog), aGdkRectangle);
}
else if (aAction == "title_changed")
{
const std::string aTitle = aRoot.get<std::string>("title", "");
gtk_window_set_title(pDialog, aTitle.c_str());
}
}
}
}
gboolean LOKDocViewSigHandlers::configureEvent(GtkWidget* pWidget, GdkEventConfigure* /*pEvent*/, gpointer /*pData*/)
{
GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(pWidget)));
LibreOfficeKitDocument* pDocument = lok_doc_view_get_document(LOK_DOC_VIEW(window->lokdocview));
if (pDocument && pDocument->pClass->getDocumentType(pDocument) == LOK_DOCTYPE_SPREADSHEET)
{
GtkAdjustment* pVAdjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(window->scrolledwindow));
int rowSizePixel = GTV_CALC_HEADER_BAR(window->rowbar)->m_nSizePixel = gtk_adjustment_get_page_size(pVAdjustment);
int rowPosPixel = GTV_CALC_HEADER_BAR(window->rowbar)->m_nPositionPixel = gtk_adjustment_get_value(pVAdjustment);
GtkAdjustment* pHAdjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(window->scrolledwindow));
int colSizePixel = GTV_CALC_HEADER_BAR(window->columnbar)->m_nSizePixel = gtk_adjustment_get_page_size(pHAdjustment);
int colPosPixel = GTV_CALC_HEADER_BAR(window->columnbar)->m_nPositionPixel = gtk_adjustment_get_value(pHAdjustment);
std::stringstream aCommand;
aCommand << ".uno:ViewRowColumnHeaders";
aCommand << "?x=" << int(lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(window->lokdocview), colPosPixel));
aCommand << "&width=" << int(lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(window->lokdocview), colSizePixel));
aCommand << "&y=" << int(lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(window->lokdocview), rowPosPixel));
aCommand << "&height=" << int(lok_doc_view_pixel_to_twip(LOK_DOC_VIEW(window->lokdocview), rowSizePixel));
std::stringstream ss;
ss << "lok::Document::getCommandValues(" << aCommand.str() << ")";
g_info("%s", ss.str().c_str());
char* pValues = pDocument->pClass->getCommandValues(pDocument, aCommand.str().c_str());
g_info("lok::Document::getCommandValues() returned '%s'", pValues);
std::stringstream aStream(pValues);
free(pValues);
assert(!aStream.str().empty());
boost::property_tree::ptree aTree;
boost::property_tree::read_json(aStream, aTree);
gtv_calc_header_bar_configure(GTV_CALC_HEADER_BAR(window->rowbar), &aTree.get_child("rows"));
gtv_calc_header_bar_configure(GTV_CALC_HEADER_BAR(window->columnbar), &aTree.get_child("columns"));
gtv_calc_header_bar_configure(GTV_CALC_HEADER_BAR(window->cornerarea), nullptr);
}
return TRUE;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|