Updated Spanish translation
[anjuta-git-plugin.git] / plugins / sourceview / assist-tip.c
blob6824987325372e851a5f02882daba66b9f28d18b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta-trunk
4 * Copyright (C) Johannes Schmid 2007 <jhs@gnome.org>
5 *
6 * anjuta-trunk is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta-trunk is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta-trunk. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include "assist-tip.h"
26 #include <gtk/gtk.h>
28 #include <libanjuta/anjuta-debug.h>
30 #include <string.h>
32 G_DEFINE_TYPE (AssistTip, assist_tip, GTK_TYPE_WINDOW);
34 enum
36 COLUMN_TIP,
37 N_COLUMNS
40 static void
41 assist_tip_init (AssistTip *object)
43 GtkWidget* alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
44 GtkWidget* window = GTK_WIDGET(object);
46 gtk_widget_set_name (GTK_WIDGET(object), "gtk-tooltip");
47 gtk_widget_set_app_paintable (GTK_WIDGET(object), TRUE);
49 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
50 window->style->ythickness,
51 window->style->ythickness,
52 window->style->xthickness,
53 window->style->xthickness);
54 object->label = gtk_label_new ("");
55 gtk_widget_show (object->label);
57 gtk_container_add (GTK_CONTAINER(alignment), object->label);
58 gtk_container_add (GTK_CONTAINER(object), alignment);
60 gtk_widget_show (alignment);
63 static void
64 assist_tip_finalize (GObject *object)
66 G_OBJECT_CLASS (assist_tip_parent_class)->finalize (object);
69 static void
70 assist_tip_class_init (AssistTipClass *klass)
72 GObjectClass* object_class = G_OBJECT_CLASS (klass);
74 object_class->finalize = assist_tip_finalize;
77 void
78 assist_tip_set_tips (AssistTip* tip, GList* tips)
80 GList* cur_tip;
81 gchar* text = NULL;
82 gchar* tip_text;
84 if (tips == NULL)
85 return;
87 for (cur_tip = tips; cur_tip != NULL; cur_tip = g_list_next (cur_tip))
89 if (!strlen (cur_tip->data))
90 continue;
91 if (!text)
93 text = g_strdup(cur_tip->data);
94 continue;
96 gchar* new_text =
97 g_strconcat(text, "\n", cur_tip->data, NULL);
98 g_free(text);
99 text = new_text;
101 tip_text = g_strdup_printf("<tt>%s</tt>", text);
102 gtk_label_set_markup(GTK_LABEL(tip->label), tip_text);
103 gtk_widget_show (tip->label);
104 g_free(text);
105 g_free(tip_text);
106 /* Make the window as small as possible */
107 gtk_window_resize (GTK_WINDOW (tip), 1, 1);
110 /* Return a tuple containing the (x, y) position of the cursor + 1 line */
111 static void
112 assist_tip_get_coordinates(GtkWidget* view, int offset, int* x, int* y, GtkWidget* entry)
114 int xor, yor;
115 /* We need to Rectangles because if we step to the next line
116 the x position is lost */
117 GtkRequisition entry_req;
118 GdkRectangle rectx;
119 GdkRectangle recty;
120 gint view_width;
121 gint width_left;
122 GdkWindow* window;
123 GtkTextIter iter;
124 GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
126 gtk_text_buffer_get_iter_at_offset(buffer, &iter,
127 offset);
128 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(view), &iter, &rectx);
130 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(view), &iter, &recty);
131 window = gtk_text_view_get_window(GTK_TEXT_VIEW(view), GTK_TEXT_WINDOW_TEXT);
132 gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(view), GTK_TEXT_WINDOW_TEXT,
133 rectx.x + rectx.width, recty.y, x, y);
135 gdk_window_get_origin(window, &xor, &yor);
136 *x = *x + xor;
137 *y = *y + yor;
140 /* Compute entry width/height */
141 gtk_widget_size_request(entry, &entry_req);
143 /* ensure that the tip is inside the text_view */
144 gdk_drawable_get_size (GDK_DRAWABLE(window), &view_width, NULL);
145 width_left = (xor + view_width) - (*x + entry_req.width);
146 DEBUG_PRINT ("width_left: %d", width_left);
147 if (width_left < 0)
149 *x += width_left;
152 *y -= (entry_req.height + 5);
155 void
156 assist_tip_move(AssistTip* assist_tip, GtkTextView* text_view, int offset)
158 int x,y;
159 assist_tip_get_coordinates(GTK_WIDGET(text_view), offset, &x, &y, assist_tip->label);
160 gtk_window_move(GTK_WINDOW(assist_tip), x, y);
161 /* Make it slightly transparent */
162 #if (GTK_MAJOR_VERSION > 2 || (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 12))
163 gtk_window_set_opacity (GTK_WINDOW (assist_tip), 0.85);
164 #endif
168 gint assist_tip_get_position (AssistTip* tip)
170 return tip->position;
173 GtkWidget*
174 assist_tip_new (GtkTextView* view, GList* tips)
176 GtkTextBuffer* buffer;
177 GtkTextIter iter;
178 GtkTextMark* mark;
179 AssistTip* assist_tip;
180 GObject* object =
181 g_object_new (ASSIST_TYPE_TIP,
182 "type", GTK_WINDOW_POPUP,
183 "type_hint", GDK_WINDOW_TYPE_HINT_TOOLTIP,
184 NULL);
187 assist_tip = ASSIST_TIP (object);
189 assist_tip_set_tips (assist_tip, tips);
191 buffer = gtk_text_view_get_buffer (view);
192 mark = gtk_text_buffer_get_insert (buffer);
193 gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
194 assist_tip->position = gtk_text_iter_get_offset (&iter);
196 /* Position is off by one for '(' brace */
197 assist_tip->position--;
199 return GTK_WIDGET(object);