document-manager: Fixes on find in files
[anjuta.git] / plugins / sourceview / assist-tip.c
blob746b2139d8e4f2958b2cbd9836179920b9f26fd0
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 GtkStyle* style;
47 gtk_widget_set_name (GTK_WIDGET(object), "gtk-tooltip");
48 gtk_widget_set_app_paintable (GTK_WIDGET(object), TRUE);
50 style = gtk_widget_get_style (window);
51 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
52 style->ythickness,
53 style->ythickness,
54 style->xthickness,
55 style->xthickness);
56 object->label = gtk_label_new ("");
57 gtk_widget_show (object->label);
59 gtk_container_add (GTK_CONTAINER(alignment), object->label);
60 gtk_container_add (GTK_CONTAINER(object), alignment);
62 gtk_widget_show (alignment);
65 static void
66 assist_tip_finalize (GObject *object)
68 G_OBJECT_CLASS (assist_tip_parent_class)->finalize (object);
71 static void
72 assist_tip_class_init (AssistTipClass *klass)
74 GObjectClass* object_class = G_OBJECT_CLASS (klass);
76 object_class->finalize = assist_tip_finalize;
79 void
80 assist_tip_set_tips (AssistTip* tip, GList* tips)
82 GList* cur_tip;
83 gchar* text = NULL;
84 gchar* tip_text;
86 if (tips == NULL)
87 return;
89 for (cur_tip = tips; cur_tip != NULL; cur_tip = g_list_next (cur_tip))
91 if (!strlen (cur_tip->data))
92 continue;
93 if (!text)
95 text = g_strdup(cur_tip->data);
96 continue;
98 gchar* new_text =
99 g_strconcat(text, "\n", cur_tip->data, NULL);
100 g_free(text);
101 text = new_text;
103 tip_text = g_markup_printf_escaped ("<tt>%s</tt>", text);
104 gtk_label_set_markup(GTK_LABEL(tip->label), tip_text);
105 gtk_widget_show (tip->label);
106 g_free(text);
107 g_free(tip_text);
108 /* Make the window as small as possible */
109 gtk_window_resize (GTK_WINDOW (tip), 1, 1);
112 /* Return a tuple containing the (x, y) position of the cursor + 1 line */
113 static void
114 assist_tip_get_coordinates(GtkWidget* view, int* x, int* y, GtkTextIter* iter, GtkWidget* entry)
116 int xor, yor;
117 /* We need Rectangles because if we step to the next line
118 the x position is lost */
119 GtkRequisition entry_req;
120 GdkRectangle rect;
121 gint view_width;
122 gint width_left;
123 GdkWindow* window;
125 gtk_text_view_get_iter_location(GTK_TEXT_VIEW(view), iter, &rect);
126 window = gtk_text_view_get_window(GTK_TEXT_VIEW(view), GTK_TEXT_WINDOW_TEXT);
127 gtk_text_view_buffer_to_window_coords(GTK_TEXT_VIEW(view), GTK_TEXT_WINDOW_TEXT,
128 rect.x, rect.y, x, y);
130 gdk_window_get_origin(window, &xor, &yor);
131 *x = *x + xor;
132 *y = *y + yor;
135 /* Compute entry width/height */
136 gtk_widget_size_request(entry, &entry_req);
138 /* ensure that the tip is inside the text_view */
139 gdk_window_get_geometry (window, NULL, NULL, &view_width, NULL);
140 width_left = (xor + view_width) - (*x + entry_req.width);
141 DEBUG_PRINT ("width_left: %d", width_left);
142 if (width_left < 0)
144 *x += width_left;
147 *y -= (entry_req.height + 5);
150 void
151 assist_tip_move(AssistTip* assist_tip, GtkTextView* text_view, GtkTextIter* iter)
153 int x,y;
154 assist_tip_get_coordinates(GTK_WIDGET(text_view), &x, &y, iter, assist_tip->label);
155 gtk_window_move(GTK_WINDOW(assist_tip), x, y);
159 gint assist_tip_get_position (AssistTip* tip)
161 return tip->position;
164 GtkWidget*
165 assist_tip_new (GtkTextView* view, GList* tips)
167 GtkTextBuffer* buffer;
168 GtkTextIter iter;
169 GtkTextMark* mark;
170 AssistTip* assist_tip;
171 GObject* object =
172 g_object_new (ASSIST_TYPE_TIP,
173 "type", GTK_WINDOW_POPUP,
174 "type_hint", GDK_WINDOW_TYPE_HINT_TOOLTIP,
175 NULL);
178 assist_tip = ASSIST_TIP (object);
180 assist_tip_set_tips (assist_tip, tips);
182 buffer = gtk_text_view_get_buffer (view);
183 mark = gtk_text_buffer_get_insert (buffer);
184 gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
185 assist_tip->position = gtk_text_iter_get_offset (&iter);
187 /* Position is off by one for '(' brace */
188 assist_tip->position--;
190 return GTK_WIDGET(object);