1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Johannes Schmid 2007 <jhs@gnome.org>
6 * anjuta-trunk is free software.
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)
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"
28 #include <libanjuta/anjuta-debug.h>
32 G_DEFINE_TYPE (AssistTip
, assist_tip
, GTK_TYPE_WINDOW
);
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
);
64 assist_tip_finalize (GObject
*object
)
66 G_OBJECT_CLASS (assist_tip_parent_class
)->finalize (object
);
70 assist_tip_class_init (AssistTipClass
*klass
)
72 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
74 object_class
->finalize
= assist_tip_finalize
;
78 assist_tip_set_tips (AssistTip
* tip
, GList
* tips
)
87 for (cur_tip
= tips
; cur_tip
!= NULL
; cur_tip
= g_list_next (cur_tip
))
89 if (!strlen (cur_tip
->data
))
93 text
= g_strdup(cur_tip
->data
);
97 g_strconcat(text
, "\n", cur_tip
->data
, NULL
);
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
);
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 */
112 assist_tip_get_coordinates(GtkWidget
* view
, int offset
, int* x
, int* y
, GtkWidget
* entry
)
115 /* We need to Rectangles because if we step to the next line
116 the x position is lost */
117 GtkRequisition entry_req
;
124 GtkTextBuffer
* buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(view
));
126 gtk_text_buffer_get_iter_at_offset(buffer
, &iter
,
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
);
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
);
152 *y
-= (entry_req
.height
+ 5);
156 assist_tip_move(AssistTip
* assist_tip
, GtkTextView
* text_view
, int offset
)
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);
168 gint
assist_tip_get_position (AssistTip
* tip
)
170 return tip
->position
;
174 assist_tip_new (GtkTextView
* view
, GList
* tips
)
176 GtkTextBuffer
* buffer
;
179 AssistTip
* assist_tip
;
181 g_object_new (ASSIST_TYPE_TIP
,
182 "type", GTK_WINDOW_POPUP
,
183 "type_hint", GDK_WINDOW_TYPE_HINT_TOOLTIP
,
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
);