Updated Spanish translation
[anjuta.git] / plugins / sourceview / assist-tip.c
blobee0f90c91d0b7808f0bc7f2840e3f181b1aa198f
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);
45 GtkStyleContext* context;
46 GtkBorder padding;
48 gtk_widget_set_name (GTK_WIDGET(object), "gtk-tooltip");
49 gtk_widget_set_app_paintable (GTK_WIDGET(object), TRUE);
51 context = gtk_widget_get_style_context (window);
52 gtk_style_context_get_padding (context, GTK_STATE_FLAG_NORMAL, &padding);
53 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
54 padding.top,
55 padding.bottom,
56 padding.left,
57 padding.right);
58 object->label = gtk_label_new ("");
59 gtk_widget_show (object->label);
61 gtk_container_add (GTK_CONTAINER(alignment), object->label);
62 gtk_container_add (GTK_CONTAINER(object), alignment);
64 gtk_widget_show (alignment);
67 static void
68 assist_tip_finalize (GObject *object)
70 G_OBJECT_CLASS (assist_tip_parent_class)->finalize (object);
73 static void
74 assist_tip_class_init (AssistTipClass *klass)
76 GObjectClass* object_class = G_OBJECT_CLASS (klass);
78 object_class->finalize = assist_tip_finalize;
81 void
82 assist_tip_set_tips (AssistTip* tip, GList* tips)
84 GList* cur_tip;
85 gchar* text = NULL;
86 gchar* tip_text;
88 if (tips == NULL)
89 return;
91 for (cur_tip = tips; cur_tip != NULL; cur_tip = g_list_next (cur_tip))
93 if (!strlen (cur_tip->data))
94 continue;
95 if (!text)
97 text = g_strdup(cur_tip->data);
98 continue;
100 gchar* new_text =
101 g_strconcat(text, "\n", cur_tip->data, NULL);
102 g_free(text);
103 text = new_text;
105 tip_text = g_markup_printf_escaped ("<tt>%s</tt>", text);
106 gtk_label_set_markup(GTK_LABEL(tip->label), tip_text);
107 gtk_widget_show (tip->label);
108 g_free(text);
109 g_free(tip_text);
110 /* Make the window as small as possible */
111 gtk_window_resize (GTK_WINDOW (tip), 1, 1);
114 /* Return a tuple containing the (x, y) position of the cursor + 1 line */
115 static void
116 assist_tip_get_coordinates(GtkWidget* view, int* x, int* y, GtkTextIter* iter, GtkWidget* entry)
118 int xor, yor;
119 /* We need Rectangles because if we step to the next line
120 the x position is lost */
121 GtkRequisition entry_req;
122 GdkRectangle rect;
123 gint view_width;
124 gint width_left;
125 GdkWindow* window;
127 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(view), iter, &rect);
128 window = gtk_text_view_get_window(GTK_TEXT_VIEW(view), GTK_TEXT_WINDOW_TEXT);
129 gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(view), GTK_TEXT_WINDOW_TEXT,
130 rect.x, rect.y, x, y);
132 gdk_window_get_origin(window, &xor, &yor);
133 *x = *x + xor;
134 *y = *y + yor;
137 /* Compute entry width/height */
138 gtk_widget_get_preferred_size (entry, &entry_req, NULL);
140 /* ensure that the tip is inside the text_view */
141 gdk_window_get_geometry (window, NULL, NULL, &view_width, NULL);
142 width_left = (xor + view_width) - (*x + entry_req.width);
143 DEBUG_PRINT ("width_left: %d", width_left);
144 if (width_left < 0)
146 *x += width_left;
149 *y -= (entry_req.height + 5);
152 void
153 assist_tip_move(AssistTip* assist_tip, GtkTextView* text_view, GtkTextIter* iter)
155 int x,y;
156 assist_tip_get_coordinates(GTK_WIDGET(text_view), &x, &y, iter, assist_tip->label);
157 gtk_window_move(GTK_WINDOW(assist_tip), x, y);
161 gint assist_tip_get_position (AssistTip* tip)
163 return tip->position;
166 GtkWidget*
167 assist_tip_new (GtkTextView* view, GList* tips)
169 GtkTextBuffer* buffer;
170 GtkTextIter iter;
171 GtkTextMark* mark;
172 AssistTip* assist_tip;
173 GObject* object =
174 g_object_new (ASSIST_TYPE_TIP,
175 "type", GTK_WINDOW_POPUP,
176 "type_hint", GDK_WINDOW_TYPE_HINT_TOOLTIP,
177 NULL);
180 assist_tip = ASSIST_TIP (object);
182 assist_tip_set_tips (assist_tip, tips);
184 buffer = gtk_text_view_get_buffer (view);
185 mark = gtk_text_buffer_get_insert (buffer);
186 gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
187 assist_tip->position = gtk_text_iter_get_offset (&iter);
189 /* Position is off by one for '(' brace */
190 assist_tip->position--;
192 return GTK_WIDGET(object);