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
);
45 GtkStyleContext
* context
;
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
),
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
);
68 assist_tip_finalize (GObject
*object
)
70 G_OBJECT_CLASS (assist_tip_parent_class
)->finalize (object
);
74 assist_tip_class_init (AssistTipClass
*klass
)
76 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
78 object_class
->finalize
= assist_tip_finalize
;
82 assist_tip_set_tips (AssistTip
* tip
, GList
* tips
)
91 for (cur_tip
= tips
; cur_tip
!= NULL
; cur_tip
= g_list_next (cur_tip
))
93 if (!strlen (cur_tip
->data
))
97 text
= g_strdup(cur_tip
->data
);
101 g_strconcat(text
, "\n", cur_tip
->data
, NULL
);
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
);
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 */
116 assist_tip_get_coordinates(GtkWidget
* view
, int* x
, int* y
, GtkTextIter
* iter
, GtkWidget
* entry
)
119 /* We need Rectangles because if we step to the next line
120 the x position is lost */
121 GtkRequisition entry_req
;
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
);
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
);
149 *y
-= (entry_req
.height
+ 5);
153 assist_tip_move(AssistTip
* assist_tip
, GtkTextView
* text_view
, GtkTextIter
* iter
)
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
;
167 assist_tip_new (GtkTextView
* view
, GList
* tips
)
169 GtkTextBuffer
* buffer
;
172 AssistTip
* assist_tip
;
174 g_object_new (ASSIST_TYPE_TIP
,
175 "type", GTK_WINDOW_POPUP
,
176 "type_hint", GDK_WINDOW_TYPE_HINT_TOOLTIP
,
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
);