GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / dialogs / dialog-cell-comment.c
blob4478c7cea31c5331dd35da6cd286bc8054451f8d
1 /*
2 * dialog-cell-comment.c: Dialog box for editing a cell comment
4 * Author:
5 * Miguel de Icaza (miguel@gnu.org)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
20 #include <gnumeric-config.h>
21 #include <glib/gi18n-lib.h>
22 #include <gnumeric.h>
23 #include "dialogs.h"
24 #include "help.h"
26 #include <gui-util.h>
27 #include <sheet.h>
28 #include <cell.h>
29 #include <sheet-object-cell-comment.h>
30 #include <wbc-gtk.h>
31 #include <ranges.h>
32 #include <commands.h>
33 #include <widgets/gnumeric-text-view.h>
35 #define COMMENT_DIALOG_KEY "cell-comment-dialog"
37 typedef struct {
38 WBCGtk *wbcg;
39 Sheet *sheet;
40 GnmCellPos const *pos;
41 GtkWidget *dialog;
42 GtkWidget *ok_button;
43 GtkWidget *cancel_button;
44 GnmTextView *gtv;
45 GtkBuilder *gui;
46 } CommentState;
48 static void
49 cb_dialog_cell_comment_destroy (CommentState *state)
51 if (state->gui != NULL)
52 g_object_unref (state->gui);
53 g_free (state);
56 static void
57 cb_cell_comment_cancel_clicked (G_GNUC_UNUSED GtkWidget *button,
58 CommentState *state)
60 gtk_widget_destroy (state->dialog);
63 static void
64 cb_cell_comment_ok_clicked (G_GNUC_UNUSED GtkWidget *button,
65 CommentState *state)
67 char *text;
68 PangoAttrList *attr;
69 char const *author;
71 author = gtk_entry_get_text
72 (GTK_ENTRY (go_gtk_builder_get_widget
73 (state->gui, "new-author-entry")));
74 g_object_get (G_OBJECT (state->gtv), "text", &text,
75 "attributes", &attr, NULL);
76 if (!cmd_set_comment (GNM_WBC (state->wbcg),
77 state->sheet, state->pos, text, attr, author))
78 gtk_widget_destroy (state->dialog);
79 g_free (text);
80 pango_attr_list_unref (attr);
83 static void
84 cb_wrap_toggled (GtkToggleButton *button, GObject *gtv)
86 g_object_set (gtv, "wrap",
87 gtk_toggle_button_get_active (button) ? GTK_WRAP_WORD : GTK_WRAP_NONE,
88 NULL);
91 void
92 dialog_cell_comment (WBCGtk *wbcg, Sheet *sheet, GnmCellPos const *pos)
94 CommentState *state;
95 GtkWidget *box, *check, *old_author, *new_author;
96 GnmComment *comment;
97 GtkBuilder *gui;
98 char *title, *cell_name;
99 char const*real_user;
100 GnmCellRef ref;
101 GnmParsePos pp;
102 GnmConventionsOut out;
104 g_return_if_fail (wbcg != NULL);
105 g_return_if_fail (sheet != NULL);
106 g_return_if_fail (pos != NULL);
108 if (gnm_dialog_raise_if_exists (wbcg, COMMENT_DIALOG_KEY))
109 return;
110 gui = gnm_gtk_builder_load ("res:ui/cell-comment.ui", NULL, GO_CMD_CONTEXT (wbcg));
111 if (gui == NULL)
112 return;
114 state = g_new (CommentState, 1);
115 state->wbcg = wbcg;
116 state->sheet = sheet;
117 state->pos = pos;
118 state->gui = gui;
120 state->dialog = go_gtk_builder_get_widget (state->gui, "comment_dialog");
121 g_return_if_fail (state->dialog != NULL);
123 box = go_gtk_builder_get_widget (state->gui, "dialog-vbox");
124 g_return_if_fail (box != NULL);
125 state->gtv = gnm_text_view_new ();
126 gtk_widget_show_all (GTK_WIDGET (state->gtv));
127 gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (state->gtv),
128 TRUE, TRUE, TRUE);
129 g_object_set (state->gtv, "wrap", GTK_WRAP_WORD, NULL);
131 gnm_cellref_init (&ref, sheet, pos->col, pos->row, FALSE);
132 out.accum = g_string_new (NULL);
133 parse_pos_init_sheet (&pp, sheet);
134 out.pp = &pp;
135 out.convs = sheet->convs;
136 cellref_as_string (&out, &ref, FALSE);
137 cell_name = g_string_free (out.accum, FALSE);
139 old_author = go_gtk_builder_get_widget (state->gui, "old-author-entry");
140 new_author = go_gtk_builder_get_widget (state->gui, "new-author-entry");
142 real_user = g_get_real_name ();
143 if ((real_user != NULL) && g_utf8_validate (real_user, -1, NULL)) {
144 gtk_entry_set_text (GTK_ENTRY (new_author), real_user);
146 gtk_widget_grab_focus (GTK_WIDGET (state->gtv));
148 comment = sheet_get_comment (sheet, pos);
149 if (comment) {
150 char const *text;
151 PangoAttrList *attr;
152 g_object_get (G_OBJECT (comment), "text", &text,
153 "markup", &attr, NULL);
154 g_object_set (state->gtv, "text", text,
155 "attributes", attr, NULL);
156 if (attr != NULL)
157 pango_attr_list_unref (attr);
159 text = cell_comment_author_get (comment);
160 if (text != NULL)
161 gtk_label_set_text (GTK_LABEL (old_author),
162 text);
163 title = g_strdup_printf (_("Edit Cell Comment (%s)"),
164 cell_name);
165 } else {
166 title = g_strdup_printf (_("New Cell Comment (%s)"),
167 cell_name);
168 gtk_widget_hide (old_author);
169 gtk_widget_hide (go_gtk_builder_get_widget (state->gui,
170 "old-author-label"));
172 gtk_window_set_title (GTK_WINDOW (state->dialog), title);
173 g_free (title);
175 state->ok_button = go_gtk_builder_get_widget (state->gui, "ok_button");
176 g_signal_connect (G_OBJECT (state->ok_button),
177 "clicked",
178 G_CALLBACK (cb_cell_comment_ok_clicked), state);
180 state->cancel_button = go_gtk_builder_get_widget (state->gui, "cancel_button");
181 g_signal_connect (G_OBJECT (state->cancel_button),
182 "clicked",
183 G_CALLBACK (cb_cell_comment_cancel_clicked), state);
185 check = go_gtk_builder_get_widget (state->gui, "wrap-check");
186 g_signal_connect (G_OBJECT (check),
187 "toggled",
188 G_CALLBACK (cb_wrap_toggled), state->gtv);
189 cb_wrap_toggled (GTK_TOGGLE_BUTTON (check), G_OBJECT (state->gtv));
191 gnm_init_help_button (
192 go_gtk_builder_get_widget (state->gui, "help_button"),
193 GNUMERIC_HELP_LINK_CELL_COMMENT);
195 wbc_gtk_attach_guru (state->wbcg, state->dialog);
196 g_object_set_data_full (G_OBJECT (state->dialog),
197 "state", state, (GDestroyNotify) cb_dialog_cell_comment_destroy);
199 gnm_dialog_setup_destroy_handlers (GTK_DIALOG (state->dialog), state->wbcg,
200 GNM_DIALOG_DESTROY_CURRENT_SHEET_REMOVED);
202 gnm_keyed_dialog (state->wbcg, GTK_WINDOW (state->dialog),
203 COMMENT_DIALOG_KEY);
204 gtk_widget_show (state->dialog);