1 /* gtktools.c  -  The GNU Privacy Assistant
2    Copyright (C) 2000, 2001 G-N-U GmbH.
3    Copyright (C) 2008, 2014 g10 Code GmbH.
4 
5    This file is part of GPA.
6 
7    GPA is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    GPA is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 
24 /* NOTE: Here are a lot of old GTK+ functions and wrappers.  They
25    should be replaced by modern GTK+ code and some of the wrappers are
26    not needed anymore. */
27 
28 #include <stdlib.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <gtk/gtk.h>
31 
32 #include "gpa.h"
33 #include "gtktools.h"
34 #include "gpawindowkeeper.h"
35 #include "icons.h"
36 
37 /* Deprecated - use gpa_show_warning instead.  */
38 void
gpa_window_error(const gchar * message,GtkWidget * messenger)39 gpa_window_error (const gchar *message, GtkWidget *messenger)
40 {
41   gpa_show_warn (messenger, NULL, "%s", message);
42 }
43 
44 
45 /* Deprecated - use gpa_show_info instead.  */
46 void
gpa_window_message(const gchar * message,GtkWidget * messenger)47 gpa_window_message (const gchar *message, GtkWidget * messenger)
48 {
49   gpa_show_info (messenger, "%s", message);
50 }
51 
52 
53 /* Create a dialog with a textview containing STRING.  */
54 static GtkWidget *
create_diagnostics_dialog(GtkWidget * parent,const char * string)55 create_diagnostics_dialog (GtkWidget *parent, const char *string)
56 {
57   GtkWidget *widget, *scrollwidget, *textview;
58   GtkDialog *dialog;
59   GtkTextBuffer *textbuffer;
60 
61   widget = gtk_dialog_new_with_buttons ("Diagnostics",
62                                         parent? GTK_WINDOW (parent):NULL,
63                                         GTK_DIALOG_MODAL,
64                                         GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL,
65                                         NULL);
66   dialog = GTK_DIALOG (widget);
67   gtk_dialog_set_has_separator (dialog, FALSE);
68   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
69   gtk_box_set_spacing (GTK_BOX (dialog->vbox), 2);
70   gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 5);
71   gtk_window_set_default_size (GTK_WINDOW (dialog), 570, 320);
72   gtk_dialog_set_default_response (dialog, GTK_RESPONSE_CANCEL);
73 
74   scrollwidget = gtk_scrolled_window_new (NULL, NULL);
75   gtk_container_set_border_width (GTK_CONTAINER (scrollwidget), 5);
76   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrollwidget),
77                                        GTK_SHADOW_IN);
78   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollwidget),
79                                   GTK_POLICY_AUTOMATIC,
80                                   GTK_POLICY_AUTOMATIC);
81   gtk_box_pack_start (GTK_BOX (dialog->vbox), scrollwidget, TRUE, TRUE, 0);
82 
83   textview = gtk_text_view_new ();
84   gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (textview), GTK_WRAP_NONE);
85   gtk_text_view_set_editable (GTK_TEXT_VIEW (textview), FALSE);
86   gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (textview), FALSE);
87   textbuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
88   gtk_text_buffer_set_text (textbuffer, string, -1);
89 
90   gtk_container_add (GTK_CONTAINER (scrollwidget), textview);
91 
92   gtk_widget_show_all (widget);
93 
94   return widget;
95 }
96 
97 
98 static void
show_gtk_message(GtkWidget * parent,GtkMessageType mtype,GpaContext * ctx,const char * format,va_list arg_ptr)99 show_gtk_message (GtkWidget *parent, GtkMessageType mtype, GpaContext *ctx,
100                   const char *format, va_list arg_ptr)
101 {
102   GtkWidget *dialog, *dialog2;
103   char *buffer;
104 
105   buffer = g_strdup_vprintf (format, arg_ptr);
106   dialog = gtk_message_dialog_new (parent? GTK_WINDOW (parent):NULL,
107                                    GTK_DIALOG_MODAL,
108                                    mtype,
109                                    GTK_BUTTONS_CLOSE,
110                                    "%s", buffer);
111   g_free (buffer);
112   buffer = NULL;
113   if (ctx)
114     gtk_dialog_add_buttons (GTK_DIALOG (dialog),
115                             _("_Details"), GTK_RESPONSE_HELP,
116                             NULL);
117 
118   gtk_widget_show_all (dialog);
119   if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_HELP && ctx)
120     {
121       /* If requested and possible get diagnostics from GPGME.  */
122       buffer = gpa_context_get_diag (ctx);
123       if (!buffer)
124         gpa_show_info (parent, "No diagnostic data available");
125       else
126         {
127           dialog2 = create_diagnostics_dialog (parent, buffer);
128           g_free (buffer);
129           gtk_dialog_run (GTK_DIALOG (dialog2));
130           gtk_widget_destroy (dialog2);
131         }
132     }
133 
134   gtk_widget_destroy (dialog);
135 }
136 
137 
138 /* Show a modal info message. */
139 void
gpa_show_info(GtkWidget * parent,const char * format,...)140 gpa_show_info (GtkWidget *parent, const char *format, ...)
141 {
142   va_list arg_ptr;
143 
144   va_start (arg_ptr, format);
145   show_gtk_message (parent, GTK_MESSAGE_INFO, NULL, format, arg_ptr);
146   va_end (arg_ptr);
147 }
148 
149 
150 /* Show a modal warning message.  PARENT is the parent windows, CTX is
151  * eitehr NULL or a related GPGME context to be used to allow shoing
152  * additional information. */
153 void
gpa_show_warn(GtkWidget * parent,GpaContext * ctx,const char * format,...)154 gpa_show_warn (GtkWidget *parent, GpaContext *ctx, const char *format, ...)
155 {
156   va_list arg_ptr;
157 
158   va_start (arg_ptr, format);
159   show_gtk_message (parent, GTK_MESSAGE_WARNING, ctx, format, arg_ptr);
160   va_end (arg_ptr);
161 }
162 
163 
164 /* Set a tooltip TEXT to WIDGET.  TEXT and WIDGET may both be NULL.
165    This function is useful so that GPA can be build with older GTK+
166    versions.  */
167 void
gpa_add_tooltip(GtkWidget * widget,const char * text)168 gpa_add_tooltip (GtkWidget *widget, const char *text)
169 {
170 #if GTK_CHECK_VERSION (2, 12, 0)
171   if (widget && text && *text)
172     gtk_widget_set_tooltip_text (widget, text);
173 #endif
174 }
175 
176 /* Set the title of COLUMN to TITLE and also set TOOLTIP. */
177 void
gpa_set_column_title(GtkTreeViewColumn * column,const char * title,const char * tooltip)178 gpa_set_column_title (GtkTreeViewColumn *column,
179                       const char *title, const char *tooltip)
180 {
181   GtkWidget *label;
182 
183   label = gtk_label_new (title);
184   /* We need to show the label before setting the widget.  */
185   gtk_widget_show (label);
186   gtk_tree_view_column_set_widget (column, label);
187   if (tooltip)
188     gpa_add_tooltip (gtk_tree_view_column_get_widget (column), tooltip);
189 }
190 
191 
192 static void
set_homogeneous(GtkWidget * widget,gpointer data)193 set_homogeneous (GtkWidget *widget, gpointer data)
194 {
195   gboolean *is_hom_p = data;
196 
197   gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (widget), *is_hom_p);
198 }
199 
200 
201 /* Set the homogeneous property for all children of TOOLBAR to IS_HOM.  */
202 void
gpa_toolbar_set_homogeneous(GtkToolbar * toolbar,gboolean is_hom)203 gpa_toolbar_set_homogeneous (GtkToolbar *toolbar, gboolean is_hom)
204 {
205   gtk_container_foreach (GTK_CONTAINER (toolbar),
206 			 (GtkCallback) set_homogeneous, &is_hom);
207 }
208 
209 
210 /* Customized set title function.  */
211 void
gpa_window_set_title(GtkWindow * window,const char * string)212 gpa_window_set_title (GtkWindow *window, const char *string)
213 {
214   const char *prefix = GPA_LONG_NAME;
215   char *buffer;
216 
217   if (!string || !*string)
218     {
219       gtk_window_set_title (window, prefix);
220     }
221   else
222     {
223       buffer = g_strdup_printf ("%s - %s", prefix, string);
224       gtk_window_set_title (window, buffer);
225       g_free (buffer);
226     }
227 }
228