nss: upgrade to release 3.73
[LibreOffice.git] / libreofficekit / qa / gtktiledviewer / gtv-helpers.cxx
blobb557fe4ba99bbe00142152ed645780a0bf3cb188
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 <pwd.h>
14 #include <cstring>
16 #include "gtv-helpers.hxx"
17 #include "gtv-signal-handlers.hxx"
19 #include <boost/property_tree/ptree.hpp>
21 void GtvHelpers::userPromptDialog(GtkWindow* pWindow, const std::string& aTitle, std::map<std::string, std::string>& aEntries)
23 GtkWidget* pDialog = gtk_dialog_new_with_buttons (aTitle.c_str(),
24 pWindow,
25 GTK_DIALOG_MODAL,
26 "Ok",
27 GTK_RESPONSE_OK,
28 nullptr);
30 GtkWidget* pDialogMessageArea = gtk_dialog_get_content_area (GTK_DIALOG (pDialog));
31 GtkWidget* pEntryArea = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
32 gtk_container_add(GTK_CONTAINER(pDialogMessageArea), pEntryArea);
33 for (const auto& entry : aEntries)
35 GtkWidget* pEntry = gtk_entry_new();
36 gtk_entry_set_placeholder_text(GTK_ENTRY(pEntry), entry.first.c_str());
37 gtk_container_add(GTK_CONTAINER(pEntryArea), pEntry);
40 gtk_widget_show_all(pDialog);
42 gint res = gtk_dialog_run(GTK_DIALOG(pDialog));
43 switch(res)
45 case GTK_RESPONSE_OK:
46 GtvGtkWrapper<GList> pList(gtk_container_get_children(GTK_CONTAINER(pEntryArea)),
47 [](GList* l)
49 g_list_free(l);
50 });
52 for (GList* l = pList.get(); l != nullptr; l = l->next)
54 const gchar* pKey = gtk_entry_get_placeholder_text(GTK_ENTRY(l->data));
55 aEntries[std::string(pKey)] = std::string(gtk_entry_get_text(GTK_ENTRY(l->data)));
57 break;
60 gtk_widget_destroy(pDialog);
63 /// Our GtkClipboardGetFunc implementation for HTML.
64 static void htmlGetFunc(GtkClipboard* /*pClipboard*/, GtkSelectionData* pSelectionData, guint /*info*/, gpointer pUserData)
66 GdkAtom aAtom(gdk_atom_intern("text/html", false));
67 const gchar* pSelection = static_cast<const gchar*>(pUserData);
68 gtk_selection_data_set(pSelectionData, aAtom, 8, reinterpret_cast<const guchar *>(pSelection), strlen(pSelection));
71 /// Our GtkClipboardClearFunc implementation for HTML.
72 static void htmlClearFunc(GtkClipboard* /*pClipboard*/, gpointer pData)
74 g_free(pData);
77 void GtvHelpers::clipboardSetHtml(GtkClipboard* pClipboard, const char* pSelection)
79 GtvGtkWrapper<GtkTargetList> pList(gtk_target_list_new(nullptr, 0),
80 [](GtkTargetList* pTargetList)
82 gtk_target_list_unref(pTargetList);
83 });
84 GdkAtom aAtom(gdk_atom_intern("text/html", false));
85 gtk_target_list_add(pList.get(), aAtom, 0, 0);
86 gint nTargets = 0;
87 GtkTargetEntry* pTargets = gtk_target_table_new_from_list(pList.get(), &nTargets);
89 gtk_clipboard_set_with_data(pClipboard, pTargets, nTargets, htmlGetFunc, htmlClearFunc, g_strdup(pSelection));
91 gtk_target_table_free(pTargets, nTargets);
94 std::string GtvHelpers::getNextAuthor()
96 static int nCounter = 0;
97 struct passwd* pPasswd = getpwuid(getuid());
98 return std::string(pPasswd->pw_gecos) + " #" + std::to_string(++nCounter);
101 GtkWidget* GtvHelpers::createCommentBox(const boost::property_tree::ptree& aComment)
103 GtkWidget* pCommentVBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
104 gchar *id = g_strndup(aComment.get<std::string>("id").c_str(), 20);
105 g_object_set_data_full(G_OBJECT(pCommentVBox), "id", id, g_free);
107 // Set background if it's a reply comment
108 if (aComment.get("parent", -1) > 0)
110 GtkStyleContext* pStyleContext = gtk_widget_get_style_context(pCommentVBox);
111 GtkCssProvider* pCssProvider = gtk_css_provider_new();
112 gtk_style_context_add_class(pStyleContext, "commentbox");
113 gtk_style_context_add_provider(pStyleContext, GTK_STYLE_PROVIDER(pCssProvider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
114 gtk_css_provider_load_from_data(pCssProvider, ".commentbox {background-color: lightgreen;}", -1, nullptr);
117 GtkWidget* pCommentText = gtk_label_new(aComment.get<std::string>("text").c_str());
118 GtkWidget* pCommentAuthor = gtk_label_new(aComment.get<std::string>("author").c_str());
119 GtkWidget* pCommentDate = gtk_label_new(aComment.get<std::string>("dateTime").c_str());
120 GtkWidget* pControlsHBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
121 GtkWidget* pEditButton = gtk_button_new_with_label("Edit");
122 GtkWidget* pReplyButton = gtk_button_new_with_label("Reply");
123 GtkWidget* pDeleteButton = gtk_button_new_with_label("Delete");
124 g_signal_connect(G_OBJECT(pEditButton), "clicked", G_CALLBACK(editButtonClicked), pCommentVBox);
125 g_signal_connect(G_OBJECT(pReplyButton), "clicked", G_CALLBACK(replyButtonClicked), pCommentVBox);
126 g_signal_connect(G_OBJECT(pDeleteButton), "clicked", G_CALLBACK(deleteCommentButtonClicked), pCommentVBox);
128 gtk_container_add(GTK_CONTAINER(pControlsHBox), pEditButton);
129 gtk_container_add(GTK_CONTAINER(pControlsHBox), pReplyButton);
130 gtk_container_add(GTK_CONTAINER(pControlsHBox), pDeleteButton);
131 GtkWidget* pCommentSeparator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
133 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentText);
134 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentAuthor);
135 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentDate);
136 gtk_container_add(GTK_CONTAINER(pCommentVBox), pControlsHBox);
137 gtk_container_add(GTK_CONTAINER(pCommentVBox), pCommentSeparator);
139 gtk_label_set_line_wrap(GTK_LABEL(pCommentText), true);
140 gtk_label_set_max_width_chars(GTK_LABEL(pCommentText), 35);
142 return pCommentVBox;
145 std::string GtvHelpers::getDirPath(const std::string& filePath)
147 int position = filePath.find_last_of('/');
148 const std::string dirPath = filePath.substr(0, position + 1);
149 return dirPath;
152 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */