📄 gtktooltips.c
字号:
/* GTK - The GIMP Toolkit * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *//* * Modified by the GTK+ Team and others 1997-1999. See the AUTHORS * file for a list of people on the GTK+ Team. See the ChangeLog * files for a list of changes. These files are distributed with * GTK+ at ftp://ftp.gtk.org/pub/gtk/. */#include <stdlib.h>#include <string.h>#include <stdio.h>#include "gtkmain.h"#include "gtkwidget.h"#include "gtkwindow.h"#include "gtksignal.h"#include "gtkstyle.h"#include "gtktooltips.h"#define DEFAULT_DELAY 500 /* Default delay in ms */static void gtk_tooltips_class_init (GtkTooltipsClass *klass);static void gtk_tooltips_init (GtkTooltips *tooltips);static void gtk_tooltips_destroy (GtkObject *object);static gint gtk_tooltips_event_handler (GtkWidget *widget, GdkEvent *event);static void gtk_tooltips_widget_unmap (GtkWidget *widget, gpointer data);static void gtk_tooltips_widget_remove (GtkWidget *widget, gpointer data);static void gtk_tooltips_set_active_widget (GtkTooltips *tooltips, GtkWidget *widget);static gint gtk_tooltips_timeout (gpointer data);static gint gtk_tooltips_paint_window (GtkTooltips *tooltips);static void gtk_tooltips_draw_tips (GtkTooltips *tooltips);static GtkDataClass *parent_class;static const gchar *tooltips_data_key = "_GtkTooltipsData";GtkTypegtk_tooltips_get_type (void){ static GtkType tooltips_type = 0; if (!tooltips_type) { static const GtkTypeInfo tooltips_info = { "GtkTooltips", sizeof (GtkTooltips), sizeof (GtkTooltipsClass), (GtkClassInitFunc) gtk_tooltips_class_init, (GtkObjectInitFunc) gtk_tooltips_init, /* reserved_1 */ NULL, /* reserved_2 */ NULL, (GtkClassInitFunc) NULL, }; tooltips_type = gtk_type_unique (GTK_TYPE_DATA, &tooltips_info); } return tooltips_type;}static voidgtk_tooltips_class_init (GtkTooltipsClass *class){ GtkObjectClass *object_class; object_class = (GtkObjectClass*) class; parent_class = gtk_type_class (GTK_TYPE_DATA); object_class->destroy = gtk_tooltips_destroy;}static voidgtk_tooltips_init (GtkTooltips *tooltips){ tooltips->tip_window = NULL; tooltips->active_tips_data = NULL; tooltips->tips_data_list = NULL; tooltips->gc = NULL; tooltips->foreground = NULL; tooltips->background = NULL; tooltips->delay = DEFAULT_DELAY; tooltips->enabled = TRUE; tooltips->timer_tag = 0;}GtkTooltips *gtk_tooltips_new (void){ return gtk_type_new (GTK_TYPE_TOOLTIPS);}static voidgtk_tooltips_free_string (gpointer data, gpointer user_data){ if (data) g_free (data);}static voidgtk_tooltips_destroy_data (GtkTooltipsData *tooltipsdata){ g_free (tooltipsdata->tip_text); g_free (tooltipsdata->tip_private); if (tooltipsdata->row) { g_list_foreach (tooltipsdata->row, gtk_tooltips_free_string, 0); g_list_free (tooltipsdata->row); } gtk_signal_disconnect_by_data (GTK_OBJECT (tooltipsdata->widget), (gpointer) tooltipsdata); gtk_object_remove_data (GTK_OBJECT (tooltipsdata->widget), tooltips_data_key); gtk_widget_unref (tooltipsdata->widget); g_free (tooltipsdata);}static voidgtk_tooltips_destroy (GtkObject *object){ GtkTooltips *tooltips = GTK_TOOLTIPS (object); GList *current; GtkTooltipsData *tooltipsdata; g_return_if_fail (tooltips != NULL); if (tooltips->timer_tag) { gtk_timeout_remove (tooltips->timer_tag); tooltips->timer_tag = 0; } if (tooltips->tips_data_list != NULL) { current = g_list_first (tooltips->tips_data_list); while (current != NULL) { tooltipsdata = (GtkTooltipsData*) current->data; current = current->next; gtk_tooltips_widget_remove (tooltipsdata->widget, tooltipsdata); } } if (tooltips->tip_window) gtk_widget_destroy (tooltips->tip_window); if (tooltips->gc != NULL) { gdk_gc_destroy (tooltips->gc); tooltips->gc = NULL; }}voidgtk_tooltips_force_window (GtkTooltips *tooltips){ g_return_if_fail (tooltips != NULL); g_return_if_fail (GTK_IS_TOOLTIPS (tooltips)); if (!tooltips->tip_window) { tooltips->tip_window = gtk_window_new (GTK_WINDOW_POPUP); gtk_widget_set_app_paintable (tooltips->tip_window, TRUE); gtk_window_set_policy (GTK_WINDOW (tooltips->tip_window), FALSE, FALSE, TRUE); gtk_widget_set_name (tooltips->tip_window, "gtk-tooltips"); gtk_signal_connect_object (GTK_OBJECT (tooltips->tip_window), "expose_event", GTK_SIGNAL_FUNC (gtk_tooltips_paint_window), GTK_OBJECT (tooltips)); gtk_signal_connect_object (GTK_OBJECT (tooltips->tip_window), "draw", GTK_SIGNAL_FUNC (gtk_tooltips_paint_window), GTK_OBJECT (tooltips)); gtk_signal_connect (GTK_OBJECT (tooltips->tip_window), "destroy", gtk_widget_destroyed, &tooltips->tip_window); }}static voidgtk_tooltips_layout_text (GtkTooltips *tooltips, GtkTooltipsData *data){ gchar *row_end, *text, *row_text, *break_pos; gint i, row_width, window_width = 0; size_t len; if (!tooltips->tip_window) gtk_tooltips_force_window (tooltips); if (data->row) { g_list_foreach (data->row, gtk_tooltips_free_string, 0); g_list_free (data->row); } data->row = 0; data->font = tooltips->tip_window->style->font; data->width = 0; text = data->tip_text; if (!text) return; while (*text) { row_end = strchr (text, '\n'); if (!row_end) row_end = strchr (text, '\0'); len = row_end - text + 1; row_text = g_new(gchar, len); memcpy (row_text, text, len - 1); row_text[len - 1] = '\0'; /* now either adjust the window's width or shorten the row until it fits in the window */ while (1) { row_width = gdk_string_width (data->font, row_text); if (!window_width) { /* make an initial guess at window's width: */ if (row_width > gdk_screen_width () / 4) window_width = gdk_screen_width () / 4; else window_width = row_width; } if (row_width <= window_width) break; if (strchr (row_text, ' ')) { /* the row is currently too wide, but we have blanks in the row so we can break it into smaller pieces */ gint avg_width = row_width / strlen (row_text); i = window_width; if (avg_width) i /= avg_width; if ((size_t) i >= len) i = len - 1; break_pos = strchr (row_text + i, ' '); if (!break_pos) { break_pos = row_text + i; while (*--break_pos != ' '); } *break_pos = '\0'; } else { /* we can't break this row into any smaller pieces, so we have no choice but to widen the window: */ window_width = row_width; break; } } if (row_width > data->width) data->width = row_width; data->row = g_list_append (data->row, row_text); text += strlen (row_text); if (!*text) break; if (text[0] == '\n' && text[1]) /* end of paragraph and there is more text to come */ data->row = g_list_append (data->row, 0); ++text; /* skip blank or newline */ } data->width += 8; /* leave some border */}voidgtk_tooltips_enable (GtkTooltips *tooltips){ g_return_if_fail (tooltips != NULL); tooltips->enabled = TRUE;}voidgtk_tooltips_disable (GtkTooltips *tooltips){ g_return_if_fail (tooltips != NULL); gtk_tooltips_set_active_widget (tooltips, NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -