Bump version to 24.04.3.4
[LibreOffice.git] / libreofficekit / qa / gtktiledviewer / gtv-helpers.cxx
blobf2c1e9b9351a0dee07d5e6bf3c0530fa1f0c02eb
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 <cstring>
14 #include "gtv-helpers.hxx"
15 #include "gtv-signal-handlers.hxx"
17 #include <boost/property_tree/ptree.hpp>
19 void GtvHelpers::userPromptDialog(GtkWindow* pWindow, const std::string& aTitle, std::map<std::string, std::string>& aEntries)
21 GtkWidget* pDialog = gtk_dialog_new_with_buttons (aTitle.c_str(),
22 pWindow,
23 GTK_DIALOG_MODAL,
24 "Ok",
25 GTK_RESPONSE_OK,
26 nullptr);
28 GtkWidget* pDialogMessageArea = gtk_dialog_get_content_area (GTK_DIALOG (pDialog));
29 GtkWidget* pEntryArea = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
30 gtk_container_add(GTK_CONTAINER(pDialogMessageArea), pEntryArea);
31 for (const auto& entry : aEntries)
33 GtkWidget* pEntry = gtk_entry_new();
34 gtk_entry_set_placeholder_text(GTK_ENTRY(pEntry), entry.first.c_str());
35 gtk_container_add(GTK_CONTAINER(pEntryArea), pEntry);
38 gtk_widget_show_all(pDialog);
40 gint res = gtk_dialog_run(GTK_DIALOG(pDialog));
41 switch(res)
43 case GTK_RESPONSE_OK:
44 GtvGtkWrapper<GList> pList(gtk_container_get_children(GTK_CONTAINER(pEntryArea)),
45 [](GList* l)
47 g_list_free(l);
48 });
50 for (GList* l = pList.get(); l != nullptr; l = l->next)
52 const gchar* pKey = gtk_entry_get_placeholder_text(GTK_ENTRY(l->data));
53 aEntries[std::string(pKey)] = std::string(gtk_entry_get_text(GTK_ENTRY(l->data)));
55 break;
58 gtk_widget_destroy(pDialog);
61 /// Our GtkClipboardGetFunc implementation for HTML.
62 static void htmlGetFunc(GtkClipboard* /*pClipboard*/, GtkSelectionData* pSelectionData, guint /*info*/, gpointer pUserData)
64 GdkAtom aAtom(gdk_atom_intern("text/html", false));
65 const gchar* pSelection = static_cast<const gchar*>(pUserData);
66 gtk_selection_data_set(pSelectionData, aAtom, 8, reinterpret_cast<const guchar *>(pSelection), strlen(pSelection));
69 /// Our GtkClipboardClearFunc implementation for HTML.
70 static void htmlClearFunc(GtkClipboard* /*pClipboard*/, gpointer pData)
72 g_free(pData);
75 void GtvHelpers::clipboardSetHtml(GtkClipboard* pClipboard, const char* pSelection)
77 GtvGtkWrapper<GtkTargetList> pList(gtk_target_list_new(nullptr, 0),
78 [](GtkTargetList* pTargetList)
80 gtk_target_list_unref(pTargetList);
81 });
82 GdkAtom aAtom(gdk_atom_intern("text/html", false));
83 gtk_target_list_add(pList.get(), aAtom, 0, 0);
84 gint nTargets = 0;
85 GtkTargetEntry* pTargets = gtk_target_table_new_from_list(pList.get(), &nTargets);
87 gtk_clipboard_set_with_data(pClipboard, pTargets, nTargets, htmlGetFunc, htmlClearFunc, g_strdup(pSelection));
89 gtk_target_table_free(pTargets, nTargets);
92 std::string GtvHelpers::getNextAuthor()
94 static int nCounter = 0;
95 const gchar* pname = g_get_real_name();
96 std::string name = pname ? std::string(pname) : std::string();
97 return name + " #" + std::to_string(++nCounter);
100 GtkWidget* GtvHelpers::createCommentBox(const boost::property_tree::ptree& aComment)
102 GtkWidget* pCommentVBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
103 gchar *id = g_strndup(aComment.get<std::string>("id").c_str(), 20);
104 g_object_set_data_full(G_OBJECT(pCommentVBox), "id", id, g_free);
106 // Set background if it's a reply comment
107 if (aComment.get("parent", -1) > 0)
109 GtkStyleContext* pStyleContext = gtk_widget_get_style_context(pCommentVBox);
110 GtkCssProvider* pCssProvider = gtk_css_provider_new();
111 gtk_style_context_add_class(pStyleContext, "commentbox");
112 gtk_style_context_add_provider(pStyleContext, GTK_STYLE_PROVIDER(pCssProvider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
113 gtk_css_provider_load_from_data(pCssProvider, ".commentbox {background-color: lightgreen;}", -1, nullptr);
116 GtkWidget* pCommentText = gtk_label_new(aComment.get<std::string>("text").c_str());
117 GtkWidget* pCommentAuthor = gtk_label_new(aComment.get<std::string>("author").c_str());
118 GtkWidget* pCommentDate = gtk_label_new(aComment.get<std::string>("dateTime").c_str());
119 GtkWidget* pControlsHBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
120 GtkWidget* pEditButton = gtk_button_new_with_label("Edit");
121 GtkWidget* pReplyButton = gtk_button_new_with_label("Reply");
122 GtkWidget* pDeleteButton = gtk_button_new_with_label("Delete");
123 g_signal_connect(G_OBJECT(pEditButton), "clicked", G_CALLBACK(editButtonClicked), pCommentVBox);
124 g_signal_connect(G_OBJECT(pReplyButton), "clicked", G_CALLBACK(replyButtonClicked), pCommentVBox);
125 g_signal_connect(G_OBJECT(pDeleteButton), "clicked", G_CALLBACK(deleteCommentButtonClicked), pCommentVBox);
127 gtk_container_add(GTK_CONTAINER(pControlsHBox), pEditButton);
128 gtk_container_add(GTK_CONTAINER(pControlsHBox), pReplyButton);
129 gtk_container_add(GTK_CONTAINER(pControlsHBox), pDeleteButton);
130 GtkWidget* pCommentSeparator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
132 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentText);
133 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentAuthor);
134 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentDate);
135 gtk_container_add(GTK_CONTAINER(pCommentVBox), pControlsHBox);
136 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentSeparator);
138 gtk_label_set_line_wrap(GTK_LABEL(pCommentText), true);
139 gtk_label_set_max_width_chars(GTK_LABEL(pCommentText), 35);
141 return pCommentVBox;
144 std::string GtvHelpers::getDirPath(const std::string& filePath)
146 int position = filePath.find_last_of('/');
147 const std::string dirPath = filePath.substr(0, position + 1);
148 return dirPath;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */