1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
/*
* This file is part of GtkSourceView
*
* Copyright (C) 2007 -2009 Jesús Barbero Rodríguez <chuchiperriman@gmail.com>
* Copyright (C) 2009 - Jesse van den Kieboom <jessevdk@gnome.org>
* Copyright (C) 2012 - Sébastien Wilmet <swilmet@gnome.org>
*
* GtkSourceView is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GtkSourceView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/**
* SECTION:completioninfo
* @title: GtkSourceCompletionInfo
* @short_description: Calltips object
*
* This object can be used to show a calltip or help for the
* current completion proposal.
*
* The info window has always the same size as the natural size of its child
* widget, added with gtk_container_add(). If you want a fixed size instead, a
* possibility is to use a scrolled window, as the following example
* demonstrates.
*
* <example>
* <title>Fixed size with a scrolled window.</title>
* <programlisting>
* GtkSourceCompletionInfo *info;
* GtkWidget *your_widget;
* GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
*
* gtk_widget_set_size_request (scrolled_window, 300, 200);
* gtk_container_add (GTK_CONTAINER (scrolled_window), your_widget);
* gtk_container_add (GTK_CONTAINER (info), scrolled_window);
* </programlisting>
* </example>
*
* If the calltip is displayed on top of a certain widget, say a #GtkTextView,
* you should attach the calltip window to the #GtkTextView with
* gtk_window_set_attached_to(). By doing this, the calltip will be hidden when
* the #GtkWidget::focus-out-event signal is emitted by the #GtkTextView. You
* may also be interested by the #GtkTextBuffer:cursor-position property (when
* its value is modified). If you use the #GtkSourceCompletionInfo through the
* #GtkSourceCompletion machinery, you don't need to worry about this.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gtksourcecompletioninfo.h"
#include <glib/gi18n-lib.h>
struct _GtkSourceCompletionInfoPrivate
{
guint idle_resize;
GtkWidget *attached_to;
gulong focus_out_event_handler;
gint xoffset;
guint transient_set : 1;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceCompletionInfo, gtk_source_completion_info, GTK_TYPE_WINDOW);
/* Resize the window */
static gboolean
idle_resize (GtkSourceCompletionInfo *info)
{
GtkWidget *child = gtk_bin_get_child (GTK_BIN (info));
GtkRequisition nat_size;
guint border_width;
gint window_width;
gint window_height;
gint cur_window_width;
gint cur_window_height;
info->priv->idle_resize = 0;
if (child == NULL)
{
return G_SOURCE_REMOVE;
}
gtk_widget_get_preferred_size (child, NULL, &nat_size);
border_width = gtk_container_get_border_width (GTK_CONTAINER (info));
window_width = nat_size.width + 2 * border_width;
window_height = nat_size.height + 2 * border_width;
gtk_window_get_size (GTK_WINDOW (info), &cur_window_width, &cur_window_height);
/* Avoid an infinite loop */
if (cur_window_width != window_width || cur_window_height != window_height)
{
gtk_window_resize (GTK_WINDOW (info),
MAX (1, window_width),
MAX (1, window_height));
}
return G_SOURCE_REMOVE;
}
static void
queue_resize (GtkSourceCompletionInfo *info)
{
if (info->priv->idle_resize == 0)
{
info->priv->idle_resize = g_idle_add ((GSourceFunc)idle_resize, info);
}
}
static void
gtk_source_completion_info_check_resize (GtkContainer *container)
{
GtkSourceCompletionInfo *info = GTK_SOURCE_COMPLETION_INFO (container);
queue_resize (info);
GTK_CONTAINER_CLASS (gtk_source_completion_info_parent_class)->check_resize (container);
}
/* Geometry management */
static GtkSizeRequestMode
gtk_source_completion_info_get_request_mode (GtkWidget *widget)
{
return GTK_SIZE_REQUEST_CONSTANT_SIZE;
}
static void
gtk_source_completion_info_get_preferred_width (GtkWidget *widget,
gint *min_width,
gint *nat_width)
{
GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
gint width = 0;
if (child != NULL)
{
GtkRequisition nat_size;
gtk_widget_get_preferred_size (child, NULL, &nat_size);
width = nat_size.width;
}
if (min_width != NULL)
{
*min_width = width;
}
if (nat_width != NULL)
{
*nat_width = width;
}
}
static void
gtk_source_completion_info_get_preferred_height (GtkWidget *widget,
gint *min_height,
gint *nat_height)
{
GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
gint height = 0;
if (child != NULL)
{
GtkRequisition nat_size;
gtk_widget_get_preferred_size (child, NULL, &nat_size);
height = nat_size.height;
}
if (min_height != NULL)
{
*min_height = height;
}
if (nat_height != NULL)
{
*nat_height = height;
}
}
/* Init, dispose, finalize, ... */
static gboolean
focus_out_event_cb (GtkSourceCompletionInfo *info)
{
gtk_widget_hide (GTK_WIDGET (info));
return FALSE;
}
static void
set_attached_to (GtkSourceCompletionInfo *info,
GtkWidget *attached_to)
{
if (info->priv->attached_to != NULL)
{
g_object_remove_weak_pointer (G_OBJECT (info->priv->attached_to),
(gpointer *) &info->priv->attached_to);
if (info->priv->focus_out_event_handler != 0)
{
g_signal_handler_disconnect (info->priv->attached_to,
info->priv->focus_out_event_handler);
info->priv->focus_out_event_handler = 0;
}
}
info->priv->attached_to = attached_to;
if (attached_to == NULL)
{
return;
}
g_object_add_weak_pointer (G_OBJECT (attached_to),
(gpointer *) &info->priv->attached_to);
info->priv->focus_out_event_handler =
g_signal_connect_swapped (attached_to,
"focus-out-event",
G_CALLBACK (focus_out_event_cb),
info);
info->priv->transient_set = FALSE;
}
static void
update_attached_to (GtkSourceCompletionInfo *info)
{
set_attached_to (info, gtk_window_get_attached_to (GTK_WINDOW (info)));
}
static void
gtk_source_completion_info_init (GtkSourceCompletionInfo *info)
{
info->priv = gtk_source_completion_info_get_instance_private (info);
g_signal_connect (info,
"notify::attached-to",
G_CALLBACK (update_attached_to),
NULL);
update_attached_to (info);
/* Tooltip style */
gtk_window_set_title (GTK_WINDOW (info), _("Completion Info"));
gtk_widget_set_name (GTK_WIDGET (info), "gtk-tooltip");
gtk_window_set_type_hint (GTK_WINDOW (info),
GDK_WINDOW_TYPE_HINT_COMBO);
gtk_container_set_border_width (GTK_CONTAINER (info), 1);
}
static void
gtk_source_completion_info_dispose (GObject *object)
{
GtkSourceCompletionInfo *info = GTK_SOURCE_COMPLETION_INFO (object);
if (info->priv->idle_resize != 0)
{
g_source_remove (info->priv->idle_resize);
info->priv->idle_resize = 0;
}
set_attached_to (info, NULL);
G_OBJECT_CLASS (gtk_source_completion_info_parent_class)->dispose (object);
}
static void
gtk_source_completion_info_show (GtkWidget *widget)
{
GtkSourceCompletionInfo *info = GTK_SOURCE_COMPLETION_INFO (widget);
if (info->priv->attached_to != NULL && !info->priv->transient_set)
{
GtkWidget *toplevel;
toplevel = gtk_widget_get_toplevel (GTK_WIDGET (info->priv->attached_to));
if (gtk_widget_is_toplevel (toplevel))
{
gtk_window_set_transient_for (GTK_WINDOW (info),
GTK_WINDOW (toplevel));
info->priv->transient_set = TRUE;
}
}
GTK_WIDGET_CLASS (gtk_source_completion_info_parent_class)->show (widget);
}
static gboolean
gtk_source_completion_info_draw (GtkWidget *widget,
cairo_t *cr)
{
GTK_WIDGET_CLASS (gtk_source_completion_info_parent_class)->draw (widget, cr);
gtk_render_frame (gtk_widget_get_style_context (widget),
cr,
0, 0,
gtk_widget_get_allocated_width (widget),
gtk_widget_get_allocated_height (widget));
return FALSE;
}
static void
gtk_source_completion_info_class_init (GtkSourceCompletionInfoClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
object_class->dispose = gtk_source_completion_info_dispose;
widget_class->show = gtk_source_completion_info_show;
widget_class->draw = gtk_source_completion_info_draw;
widget_class->get_request_mode = gtk_source_completion_info_get_request_mode;
widget_class->get_preferred_width = gtk_source_completion_info_get_preferred_width;
widget_class->get_preferred_height = gtk_source_completion_info_get_preferred_height;
container_class->check_resize = gtk_source_completion_info_check_resize;
}
void
_gtk_source_completion_info_set_xoffset (GtkSourceCompletionInfo *window,
gint xoffset)
{
g_return_if_fail (GTK_SOURCE_IS_COMPLETION_INFO (window));
window->priv->xoffset = xoffset;
}
/* Move to iter */
static void
get_iter_pos (GtkTextView *text_view,
GtkTextIter *iter,
gint *x,
gint *y,
gint *height)
{
GdkWindow *win;
GdkRectangle location;
gint win_x;
gint win_y;
gint xx;
gint yy;
gtk_text_view_get_iter_location (text_view, iter, &location);
gtk_text_view_buffer_to_window_coords (text_view,
GTK_TEXT_WINDOW_WIDGET,
location.x,
location.y,
&win_x,
&win_y);
win = gtk_text_view_get_window (text_view, GTK_TEXT_WINDOW_WIDGET);
gdk_window_get_origin (win, &xx, &yy);
*x = win_x + xx;
*y = win_y + yy + location.height;
*height = location.height;
}
static void
compensate_for_gravity (GtkSourceCompletionInfo *window,
gint *x,
gint *y,
gint w,
gint h)
{
GdkGravity gravity = gtk_window_get_gravity (GTK_WINDOW (window));
/* Horizontal */
switch (gravity)
{
case GDK_GRAVITY_NORTH:
case GDK_GRAVITY_SOUTH:
case GDK_GRAVITY_CENTER:
*x = w / 2;
break;
case GDK_GRAVITY_NORTH_EAST:
case GDK_GRAVITY_SOUTH_EAST:
case GDK_GRAVITY_EAST:
*x = w;
break;
case GDK_GRAVITY_NORTH_WEST:
case GDK_GRAVITY_WEST:
case GDK_GRAVITY_SOUTH_WEST:
case GDK_GRAVITY_STATIC:
default:
*x = 0;
break;
}
/* Vertical */
switch (gravity)
{
case GDK_GRAVITY_WEST:
case GDK_GRAVITY_CENTER:
case GDK_GRAVITY_EAST:
*y = w / 2;
break;
case GDK_GRAVITY_SOUTH_EAST:
case GDK_GRAVITY_SOUTH:
case GDK_GRAVITY_SOUTH_WEST:
*y = w;
break;
case GDK_GRAVITY_NORTH:
case GDK_GRAVITY_NORTH_EAST:
case GDK_GRAVITY_NORTH_WEST:
case GDK_GRAVITY_STATIC:
default:
*y = 0;
break;
}
}
static void
move_overlap (gint *y,
gint h,
gint oy,
gint cy,
gint line_height,
gboolean move_up)
{
/* Test if there is overlap */
if (*y - cy < oy && *y - cy + h > oy - line_height)
{
if (move_up)
{
*y = oy - line_height - h + cy;
}
else
{
*y = oy + cy;
}
}
}
static void
move_to_iter (GtkSourceCompletionInfo *window,
GtkTextView *view,
GtkTextIter *iter)
{
GdkScreen *screen;
gint x, y;
gint w, h;
gint sw, sh;
gint cx, cy;
gint oy;
gint height;
gboolean overlapup;
screen = gtk_window_get_screen (GTK_WINDOW (window));
sw = gdk_screen_get_width (screen);
sh = gdk_screen_get_height (screen);
get_iter_pos (view, iter, &x, &y, &height);
gtk_window_get_size (GTK_WINDOW (window), &w, &h);
x += window->priv->xoffset;
oy = y;
compensate_for_gravity (window, &cx, &cy, w, h);
/* Push window inside screen */
if (x - cx + w > sw)
{
x = (sw - w) + cx;
}
else if (x - cx < 0)
{
x = cx;
}
if (y - cy + h > sh)
{
y = (sh - h) + cy;
overlapup = TRUE;
}
else if (y - cy < 0)
{
y = cy;
overlapup = FALSE;
}
else
{
overlapup = TRUE;
}
/* Make sure that text is still readable */
move_overlap (&y, h, oy, cy, height, overlapup);
gtk_window_move (GTK_WINDOW (window), x, y);
}
static void
move_to_cursor (GtkSourceCompletionInfo *window,
GtkTextView *view)
{
GtkTextBuffer *buffer;
GtkTextIter insert;
buffer = gtk_text_view_get_buffer (view);
gtk_text_buffer_get_iter_at_mark (buffer, &insert, gtk_text_buffer_get_insert (buffer));
move_to_iter (window, view, &insert);
}
/* Public functions */
/**
* gtk_source_completion_info_new:
*
* Returns: a new GtkSourceCompletionInfo.
*/
GtkSourceCompletionInfo *
gtk_source_completion_info_new (void)
{
return g_object_new (GTK_SOURCE_TYPE_COMPLETION_INFO,
"type", GTK_WINDOW_POPUP,
"border-width", 3,
NULL);
}
/**
* gtk_source_completion_info_move_to_iter:
* @info: a #GtkSourceCompletionInfo.
* @view: a #GtkTextView on which the info window should be positioned.
* @iter: (nullable): a #GtkTextIter.
*
* Moves the #GtkSourceCompletionInfo to @iter. If @iter is %NULL @info is
* moved to the cursor position. Moving will respect the #GdkGravity setting
* of the info window and will ensure the line at @iter is not occluded by
* the window.
*/
void
gtk_source_completion_info_move_to_iter (GtkSourceCompletionInfo *info,
GtkTextView *view,
GtkTextIter *iter)
{
g_return_if_fail (GTK_SOURCE_IS_COMPLETION_INFO (info));
g_return_if_fail (GTK_IS_TEXT_VIEW (view));
if (iter == NULL)
{
move_to_cursor (info, view);
}
else
{
move_to_iter (info, view, iter);
}
}
|