bump product version to 6.4.0.3
[LibreOffice.git] / libreofficekit / qa / gtktiledviewer / gtv-main-toolbar.cxx
blobbdb1192f94da20f3bfd5c8f50b5056f86b99f57a
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 "gtv-application-window.hxx"
13 #include "gtv-main-toolbar.hxx"
14 #include "gtv-signal-handlers.hxx"
15 #include "gtv-helpers.hxx"
16 #include "gtv-calc-header-bar.hxx"
17 #include "gtv-lok-dialog.hxx"
19 #include <map>
20 #include <memory>
22 #include <boost/property_tree/json_parser.hpp>
23 #include <boost/optional.hpp>
25 struct GtvMainToolbarPrivateImpl
27 GtkWidget* toolbar1;
28 GtkWidget* toolbar2;
30 GtkWidget* m_pEnableEditing;
31 GtkWidget* m_pLeftpara;
32 GtkWidget* m_pCenterpara;
33 GtkWidget* m_pRightpara;
34 GtkWidget* m_pJustifypara;
35 GtkWidget* m_pDeleteComment;
36 GtkWidget* m_pPartSelector;
37 GtkWidget* m_pPartModeSelector;
38 GtkWidget* m_pRecentUnoSelector;
39 std::map<std::string, std::string> m_pRecentUnoCommands;
41 /// Sensitivity (enabled or disabled) for each tool item, ignoring edit state
42 std::map<GtkToolItem*, bool> m_aToolItemSensitivities;
44 GtvMainToolbarPrivateImpl() :
45 toolbar1(nullptr),
46 toolbar2(nullptr),
47 m_pEnableEditing(nullptr),
48 m_pLeftpara(nullptr),
49 m_pCenterpara(nullptr),
50 m_pRightpara(nullptr),
51 m_pJustifypara(nullptr),
52 m_pDeleteComment(nullptr),
53 m_pPartSelector(nullptr),
54 m_pPartModeSelector(nullptr),
55 m_pRecentUnoSelector(nullptr)
56 { }
59 struct GtvMainToolbarPrivate
61 GtvMainToolbarPrivateImpl* m_pImpl;
63 GtvMainToolbarPrivateImpl* operator->()
65 return m_pImpl;
69 #if defined __clang__
70 #if __has_warning("-Wdeprecated-volatile")
71 #pragma clang diagnostic push
72 #pragma clang diagnostic ignored "-Wdeprecated-volatile"
73 #endif
74 #endif
75 G_DEFINE_TYPE_WITH_PRIVATE(GtvMainToolbar, gtv_main_toolbar, GTK_TYPE_BOX);
76 #if defined __clang__
77 #if __has_warning("-Wdeprecated-volatile")
78 #pragma clang diagnostic pop
79 #endif
80 #endif
82 static GtvMainToolbarPrivate&
83 getPrivate(GtvMainToolbar* toolbar)
85 return *static_cast<GtvMainToolbarPrivate*>(gtv_main_toolbar_get_instance_private(toolbar));
88 static void
89 gtv_main_toolbar_init(GtvMainToolbar* toolbar)
91 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
92 priv.m_pImpl = new GtvMainToolbarPrivateImpl();
94 const std::string uiFilePath = GtvHelpers::getDirPath(__FILE__) + std::string(UI_FILE_NAME);
95 GtvGtkWrapper<GtkBuilder> builder(gtk_builder_new_from_file(uiFilePath.c_str()),
96 [](GtkBuilder* pBuilder) {
97 g_object_unref(pBuilder);
98 });
100 priv->toolbar1 = GTK_WIDGET(gtk_builder_get_object(builder.get(), "toolbar1"));
101 gtk_box_pack_start(GTK_BOX(toolbar), priv->toolbar1, false, false, false);
102 priv->toolbar2 = GTK_WIDGET(gtk_builder_get_object(builder.get(), "toolbar2"));
103 gtk_box_pack_start(GTK_BOX(toolbar), priv->toolbar2, false, false, false);
105 priv->m_pEnableEditing = GTK_WIDGET(gtk_builder_get_object(builder.get(), "btn_editmode"));
106 priv->m_pLeftpara = GTK_WIDGET(gtk_builder_get_object(builder.get(), "btn_justifyleft"));
107 priv->m_pCenterpara = GTK_WIDGET(gtk_builder_get_object(builder.get(), "btn_justifycenter"));
108 priv->m_pRightpara = GTK_WIDGET(gtk_builder_get_object(builder.get(), "btn_justifyright"));
109 priv->m_pJustifypara = GTK_WIDGET(gtk_builder_get_object(builder.get(), "btn_justifyfill"));
110 priv->m_pDeleteComment = GTK_WIDGET(gtk_builder_get_object(builder.get(), "btn_removeannotation"));
111 priv->m_pPartSelector = GTK_WIDGET(gtk_builder_get_object(builder.get(), "combo_partselector"));
112 priv->m_pPartModeSelector = GTK_WIDGET(gtk_builder_get_object(builder.get(), "combo_partsmodeselector"));
113 priv->m_pRecentUnoSelector = GTK_WIDGET(gtk_builder_get_object(builder.get(), "combo_recentunoselector"));
115 toolbar->m_pAddressbar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "addressbar_entry"));
116 toolbar->m_pFormulabar = GTK_WIDGET(gtk_builder_get_object(builder.get(), "formulabar_entry"));
118 // TODO: compile with -rdynamic and get rid of it
119 gtk_builder_add_callback_symbol(builder.get(), "btn_clicked", G_CALLBACK(btn_clicked));
120 gtk_builder_add_callback_symbol(builder.get(), "doCopy", G_CALLBACK(doCopy));
121 gtk_builder_add_callback_symbol(builder.get(), "doPaste", G_CALLBACK(doPaste));
122 gtk_builder_add_callback_symbol(builder.get(), "createView", G_CALLBACK(createView));
123 gtk_builder_add_callback_symbol(builder.get(), "getRulerState", G_CALLBACK(getRulerState));
124 gtk_builder_add_callback_symbol(builder.get(), "recentUnoChanged", G_CALLBACK(recentUnoChanged));
125 gtk_builder_add_callback_symbol(builder.get(), "unoCommandDebugger", G_CALLBACK(unoCommandDebugger));
126 gtk_builder_add_callback_symbol(builder.get(), "toggleEditing", G_CALLBACK(toggleEditing));
127 gtk_builder_add_callback_symbol(builder.get(), "changePartMode", G_CALLBACK(changePartMode));
128 gtk_builder_add_callback_symbol(builder.get(), "changePart", G_CALLBACK(changePart));
129 gtk_builder_add_callback_symbol(builder.get(), "changeZoom", G_CALLBACK(changeZoom));
130 gtk_builder_add_callback_symbol(builder.get(), "toggleFindbar", G_CALLBACK(toggleFindbar));
131 gtk_builder_add_callback_symbol(builder.get(), "documentRedline", G_CALLBACK(documentRedline));
132 gtk_builder_add_callback_symbol(builder.get(), "documentRepair", G_CALLBACK(documentRepair));
133 gtk_builder_add_callback_symbol(builder.get(), "signalAddressbar", G_CALLBACK(signalAddressbar));
134 gtk_builder_add_callback_symbol(builder.get(), "signalFormulabar", G_CALLBACK(signalFormulabar));
136 // find toolbar
137 // Note: These buttons are not the part of GtvMainToolbar
138 gtk_builder_add_callback_symbol(builder.get(), "signalSearchNext", G_CALLBACK(signalSearchNext));
139 gtk_builder_add_callback_symbol(builder.get(), "signalSearchPrev", G_CALLBACK(signalSearchPrev));
140 gtk_builder_add_callback_symbol(builder.get(), "signalFindbar", G_CALLBACK(signalFindbar));
141 gtk_builder_add_callback_symbol(builder.get(), "toggleFindAll", G_CALLBACK(toggleFindAll));
143 gtk_builder_connect_signals(builder.get(), nullptr);
145 gtk_widget_show_all(GTK_WIDGET(toolbar));
148 static void
149 gtv_main_toolbar_finalize(GObject* object)
151 GtvMainToolbarPrivate& priv = getPrivate(GTV_MAIN_TOOLBAR(object));
153 delete priv.m_pImpl;
154 priv.m_pImpl = nullptr;
156 G_OBJECT_CLASS (gtv_main_toolbar_parent_class)->finalize (object);
159 static void
160 gtv_main_toolbar_class_init(GtvMainToolbarClass* klass)
162 G_OBJECT_CLASS(klass)->finalize = gtv_main_toolbar_finalize;
165 static void populatePartSelector(GtvMainToolbar* toolbar)
167 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
168 GtvApplicationWindow* window = GTV_APPLICATION_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(toolbar)));
169 gtv_application_window_set_part_broadcast(window, false);
170 gtk_list_store_clear( GTK_LIST_STORE(
171 gtk_combo_box_get_model(
172 GTK_COMBO_BOX(priv->m_pPartSelector) )) );
174 if (!window->lokdocview)
176 return;
179 const int nMaxLength = 50;
180 char sText[nMaxLength];
182 int nParts = lok_doc_view_get_parts(LOK_DOC_VIEW(window->lokdocview));
183 for ( int i = 0; i < nParts; i++ )
185 char* pName = lok_doc_view_get_part_name(LOK_DOC_VIEW(window->lokdocview), i);
186 assert( pName );
187 snprintf( sText, nMaxLength, "%i (%s)", i+1, pName );
188 free( pName );
190 gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT(priv->m_pPartSelector), sText );
192 gtk_combo_box_set_active(GTK_COMBO_BOX(priv->m_pPartSelector), lok_doc_view_get_part(LOK_DOC_VIEW(window->lokdocview)));
194 gtv_application_window_set_part_broadcast(window, true);
197 static void populateRecentUnoSelector(GtvMainToolbar* toolbar)
199 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
200 GtkComboBoxText* pSelector = GTK_COMBO_BOX_TEXT(priv->m_pRecentUnoSelector);
202 unsigned counter = 0;
203 std::ifstream is("/tmp/gtv-recentunos.txt");
204 while (is.good() && counter < 10)
206 std::string unoCommandStr;
207 std::getline(is, unoCommandStr);
208 std::vector<std::string> aUnoCmd = GtvHelpers::split<std::string>(unoCommandStr, " | ", 2);
209 if (aUnoCmd.size() != 2)
210 continue;
211 auto it = priv->m_pRecentUnoCommands.emplace(aUnoCmd[0], aUnoCmd[1]);
212 if (it.second)
214 gtk_combo_box_text_append_text(pSelector, aUnoCmd[0].c_str());
215 ++counter;
220 void
221 gtv_main_toolbar_doc_loaded(GtvMainToolbar* toolbar, LibreOfficeKitDocumentType eDocType, bool bEditMode)
223 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
224 gtk_widget_set_visible(toolbar->m_pAddressbar, false);
225 gtk_widget_set_visible(toolbar->m_pFormulabar, false);
226 if (eDocType == LOK_DOCTYPE_SPREADSHEET)
228 gtk_tool_button_set_label(GTK_TOOL_BUTTON(priv->m_pLeftpara), ".uno:AlignLeft");
229 gtk_tool_button_set_label(GTK_TOOL_BUTTON(priv->m_pCenterpara), ".uno:AlignHorizontalCenter");
230 gtk_tool_button_set_label(GTK_TOOL_BUTTON(priv->m_pRightpara), ".uno:AlignRight");
231 gtk_widget_hide(priv->m_pJustifypara);
232 gtk_tool_button_set_label(GTK_TOOL_BUTTON(priv->m_pDeleteComment), ".uno:DeleteNote");
234 gtk_widget_set_visible(toolbar->m_pAddressbar, true);
235 gtk_widget_set_visible(toolbar->m_pFormulabar, true);
237 else if (eDocType == LOK_DOCTYPE_PRESENTATION)
239 gtk_tool_button_set_label(GTK_TOOL_BUTTON(priv->m_pDeleteComment), ".uno:DeleteAnnotation");
242 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(priv->m_pEnableEditing), bEditMode);
244 // populate combo boxes
245 populatePartSelector(toolbar);
247 // populate recent uno selector
248 populateRecentUnoSelector(toolbar);
251 void
252 gtv_main_toolbar_add_recent_uno(GtvMainToolbar* toolbar, const std::string& rUnoCmdStr)
254 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
255 GtkComboBoxText* pSelector = GTK_COMBO_BOX_TEXT(priv->m_pRecentUnoSelector);
257 const std::vector<std::string> aUnoCmd = GtvHelpers::split<std::string>(rUnoCmdStr, " | ", 2);
258 priv->m_pRecentUnoCommands[aUnoCmd[0]] = aUnoCmd[1];
259 // keep placeholder string at the top
260 gtk_combo_box_text_insert_text(pSelector, 1, aUnoCmd[0].c_str());
261 // TODO: Remove other text entries with same key
264 std::string
265 gtv_main_toolbar_get_recent_uno_args(GtvMainToolbar* toolbar, const std::string& rUnoCmd)
267 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
268 auto it = std::find_if(priv->m_pRecentUnoCommands.begin(), priv->m_pRecentUnoCommands.end(),
269 [&rUnoCmd](const std::pair<std::string, std::string>& pair) {
270 return rUnoCmd == pair.first;
272 std::string ret;
273 if (it != priv->m_pRecentUnoCommands.end())
274 ret = it->second;
275 return ret;
278 GtkContainer*
279 gtv_main_toolbar_get_first_toolbar(GtvMainToolbar* toolbar)
281 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
282 return GTK_CONTAINER(priv->toolbar1);
285 GtkContainer*
286 gtv_main_toolbar_get_second_toolbar(GtvMainToolbar* toolbar)
288 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
289 return GTK_CONTAINER(priv->toolbar2);
292 void
293 gtv_main_toolbar_set_sensitive_internal(GtvMainToolbar* toolbar, GtkToolItem* pItem, bool isSensitive)
295 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
296 priv->m_aToolItemSensitivities[pItem] = isSensitive;
299 static void setSensitiveIfEdit(GtvMainToolbar* toolbar, GtkToolItem* pItem, bool bEdit)
301 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
302 // some buttons remain enabled always
303 const gchar* pIconName = gtk_tool_button_get_icon_name(GTK_TOOL_BUTTON(pItem));
304 if (g_strcmp0(pIconName, "zoom-in-symbolic") != 0 &&
305 g_strcmp0(pIconName, "zoom-original-symbolic") != 0 &&
306 g_strcmp0(pIconName, "zoom-out-symbolic") != 0 &&
307 g_strcmp0(pIconName, "insert-text-symbolic") != 0 &&
308 g_strcmp0(pIconName, "view-continuous-symbolic") != 0 &&
309 g_strcmp0(pIconName, "document-properties") != 0 &&
310 g_strcmp0(pIconName, "system-run") != 0)
312 bool state = true;
313 if (priv->m_aToolItemSensitivities.find(pItem) != priv->m_aToolItemSensitivities.end())
314 state = priv->m_aToolItemSensitivities[pItem];
316 gtk_widget_set_sensitive(GTK_WIDGET(pItem), bEdit && state);
320 void
321 gtv_main_toolbar_set_edit(GtvMainToolbar* toolbar, gboolean bEdit)
323 GtvMainToolbarPrivate& priv = getPrivate(toolbar);
324 GtvGtkWrapper<GList> pList(gtk_container_get_children(GTK_CONTAINER(priv->toolbar1)),
325 [](GList* l)
327 g_list_free(l);
329 for (GList* l = pList.get(); l != nullptr; l = l->next)
331 if (GTK_IS_TOOL_BUTTON(l->data))
333 setSensitiveIfEdit(toolbar, GTK_TOOL_ITEM(l->data), bEdit);
337 pList.reset(gtk_container_get_children(GTK_CONTAINER(priv->toolbar2)));
338 for (GList* l = pList.get(); l != nullptr; l = l->next)
340 if (GTK_IS_TOOL_BUTTON(l->data))
342 setSensitiveIfEdit(toolbar, GTK_TOOL_ITEM(l->data), bEdit);
347 GtkWidget*
348 gtv_main_toolbar_new()
350 return GTK_WIDGET(g_object_new(GTV_TYPE_MAIN_TOOLBAR,
351 "orientation", GTK_ORIENTATION_VERTICAL,
352 nullptr));
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */