2011-02-09 14:41:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2014-07-28 19:19:31 +00:00
|
|
|
typedef struct {
|
|
|
|
GtkTextView parent;
|
|
|
|
} MyTextView;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GtkTextViewClass parent_class;
|
|
|
|
} MyTextViewClass;
|
|
|
|
|
2020-03-06 17:05:43 +00:00
|
|
|
static GType my_text_view_get_type (void);
|
2014-07-28 19:19:31 +00:00
|
|
|
G_DEFINE_TYPE (MyTextView, my_text_view, GTK_TYPE_TEXT_VIEW);
|
|
|
|
|
2018-07-14 03:26:33 +00:00
|
|
|
static void snapshot_background (GtkWidget *widget, GtkSnapshot *snapshot);
|
2014-07-28 19:19:31 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
my_text_view_init (MyTextView *text_view)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-14 03:26:33 +00:00
|
|
|
static void my_text_view_snapshot_layer (GtkTextView *textview,
|
|
|
|
GtkTextViewLayer layer,
|
|
|
|
GtkSnapshot *snapshot)
|
2014-07-28 19:19:31 +00:00
|
|
|
{
|
2017-12-29 18:13:36 +00:00
|
|
|
if (layer == GTK_TEXT_VIEW_LAYER_BELOW_TEXT)
|
2018-07-14 03:26:33 +00:00
|
|
|
snapshot_background (GTK_WIDGET (textview), snapshot);
|
2014-07-28 19:19:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_text_view_class_init (MyTextViewClass *klass)
|
|
|
|
{
|
2018-07-14 03:26:33 +00:00
|
|
|
GTK_TEXT_VIEW_CLASS (klass)->snapshot_layer = my_text_view_snapshot_layer;
|
2014-07-28 19:19:31 +00:00
|
|
|
}
|
2011-02-09 14:41:39 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
create_tags (GtkTextBuffer *buffer)
|
|
|
|
{
|
|
|
|
|
|
|
|
gtk_text_buffer_create_tag (buffer, "italic",
|
|
|
|
"style", PANGO_STYLE_ITALIC, NULL);
|
|
|
|
|
|
|
|
gtk_text_buffer_create_tag (buffer, "bold",
|
|
|
|
"weight", PANGO_WEIGHT_BOLD, NULL);
|
|
|
|
|
|
|
|
gtk_text_buffer_create_tag (buffer, "x-large",
|
|
|
|
"scale", PANGO_SCALE_X_LARGE, NULL);
|
|
|
|
|
|
|
|
gtk_text_buffer_create_tag (buffer, "semi_blue_foreground",
|
2011-02-14 07:47:57 +00:00
|
|
|
"foreground", "rgba(0,0,255,0.7)", NULL);
|
2011-02-09 14:41:39 +00:00
|
|
|
|
|
|
|
gtk_text_buffer_create_tag (buffer, "semi_red_background",
|
|
|
|
"background", "rgba(255,0,0,0.5)", NULL);
|
|
|
|
|
2011-02-14 06:27:42 +00:00
|
|
|
gtk_text_buffer_create_tag (buffer, "semi_orange_paragraph_background",
|
|
|
|
"paragraph-background", "rgba(255,165,0,0.5)", NULL);
|
|
|
|
|
2011-02-09 14:41:39 +00:00
|
|
|
gtk_text_buffer_create_tag (buffer, "word_wrap",
|
|
|
|
"wrap_mode", GTK_WRAP_WORD, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-08 13:02:17 +00:00
|
|
|
static GtkTextChildAnchor *
|
2011-02-09 14:41:39 +00:00
|
|
|
insert_text (GtkTextBuffer *buffer)
|
|
|
|
{
|
2011-02-14 06:27:42 +00:00
|
|
|
GtkTextIter iter;
|
|
|
|
GtkTextIter start, end;
|
|
|
|
GtkTextMark *para_start;
|
2014-01-08 13:02:17 +00:00
|
|
|
GtkTextChildAnchor *anchor;
|
2011-02-09 14:41:39 +00:00
|
|
|
|
|
|
|
/* get start of buffer; each insertion will revalidate the
|
|
|
|
* iterator to point to just after the inserted text.
|
|
|
|
*/
|
|
|
|
gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter,
|
|
|
|
"This test shows text view rendering some text with rgba colors.\n\n", -1);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, "For example, you can have ", -1);
|
|
|
|
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
|
|
|
|
"italic translucent blue text", -1,
|
|
|
|
"italic",
|
|
|
|
"semi_blue_foreground",
|
|
|
|
"x-large",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, ", or ", -1);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, ", ", -1);
|
|
|
|
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
|
|
|
|
"bold text with translucent red background", -1,
|
|
|
|
"bold",
|
|
|
|
"semi_red_background",
|
|
|
|
"x-large",
|
|
|
|
NULL);
|
2011-02-14 06:27:42 +00:00
|
|
|
gtk_text_buffer_insert (buffer, &iter, ".\n\n", -1);
|
|
|
|
|
2014-01-08 13:02:17 +00:00
|
|
|
anchor = gtk_text_buffer_create_child_anchor (buffer, &iter);
|
|
|
|
|
2011-02-14 06:27:42 +00:00
|
|
|
/* Store the beginning of the other paragraph */
|
|
|
|
para_start = gtk_text_buffer_create_mark (buffer, "para_start", &iter, TRUE);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter,
|
|
|
|
"Paragraph background colors can also be set with rgba color values .\n", -1);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, "For instance, you can have ", -1);
|
|
|
|
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
|
|
|
|
"bold translucent blue text", -1,
|
|
|
|
"bold",
|
|
|
|
"semi_blue_foreground",
|
|
|
|
"x-large",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, ", or ", -1);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, ", ", -1);
|
|
|
|
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
|
|
|
|
"italic text with translucent red background", -1,
|
|
|
|
"italic",
|
|
|
|
"semi_red_background",
|
|
|
|
"x-large",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
gtk_text_buffer_insert (buffer, &iter, " all rendered onto a translucent orange paragraph background.\n", -1);
|
2011-02-09 14:41:39 +00:00
|
|
|
|
|
|
|
gtk_text_buffer_get_bounds (buffer, &start, &end);
|
|
|
|
|
2011-02-14 06:27:42 +00:00
|
|
|
gtk_text_buffer_get_iter_at_mark (buffer, &iter, para_start);
|
|
|
|
gtk_text_buffer_apply_tag_by_name (buffer, "semi_orange_paragraph_background", &iter, &end);
|
2011-02-09 14:41:39 +00:00
|
|
|
|
2011-02-14 06:27:42 +00:00
|
|
|
/* Apply word_wrap tag to whole buffer */
|
|
|
|
gtk_text_buffer_get_bounds (buffer, &start, &end);
|
|
|
|
gtk_text_buffer_apply_tag_by_name (buffer, "word_wrap", &start, &end);
|
2014-01-08 13:02:17 +00:00
|
|
|
|
|
|
|
return anchor;
|
2011-02-09 14:41:39 +00:00
|
|
|
}
|
|
|
|
|
2011-02-14 07:47:57 +00:00
|
|
|
|
|
|
|
/* Size of checks and gray levels for alpha compositing checkerboard */
|
|
|
|
#define CHECK_SIZE 10
|
|
|
|
#define CHECK_DARK (1.0 / 3.0)
|
|
|
|
#define CHECK_LIGHT (2.0 / 3.0)
|
|
|
|
|
2011-02-09 14:41:39 +00:00
|
|
|
static void
|
2018-07-14 03:26:33 +00:00
|
|
|
snapshot_background (GtkWidget *widget,
|
|
|
|
GtkSnapshot *snapshot)
|
2011-02-09 14:41:39 +00:00
|
|
|
{
|
2014-07-28 19:19:31 +00:00
|
|
|
GdkRectangle visible_rect;
|
2011-02-09 14:41:39 +00:00
|
|
|
|
2014-07-28 19:19:31 +00:00
|
|
|
gtk_text_view_get_visible_rect (GTK_TEXT_VIEW (widget), &visible_rect);
|
2011-02-09 14:41:39 +00:00
|
|
|
|
2018-07-14 03:26:33 +00:00
|
|
|
gtk_snapshot_append_color (snapshot,
|
|
|
|
&(GdkRGBA) { CHECK_DARK, CHECK_DARK, CHECK_DARK, 1.0 },
|
|
|
|
&GRAPHENE_RECT_INIT(visible_rect.x, visible_rect.y, visible_rect.width, visible_rect.height));
|
|
|
|
|
|
|
|
gtk_snapshot_push_repeat (snapshot,
|
|
|
|
&GRAPHENE_RECT_INIT(visible_rect.x, visible_rect.y, visible_rect.width, visible_rect.height),
|
|
|
|
&GRAPHENE_RECT_INIT(visible_rect.x, visible_rect.y, CHECK_SIZE * 2, CHECK_SIZE * 2));
|
|
|
|
gtk_snapshot_append_color (snapshot,
|
|
|
|
&(GdkRGBA) { CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT, 1.0 },
|
|
|
|
&GRAPHENE_RECT_INIT(visible_rect.x, visible_rect.y, CHECK_SIZE, CHECK_SIZE));
|
|
|
|
gtk_snapshot_append_color (snapshot,
|
|
|
|
&(GdkRGBA) { CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT, 1.0 },
|
|
|
|
&GRAPHENE_RECT_INIT(visible_rect.x + CHECK_SIZE, visible_rect.y + CHECK_SIZE, CHECK_SIZE, CHECK_SIZE));
|
|
|
|
gtk_snapshot_pop (snapshot);
|
2011-02-09 14:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2014-01-08 13:02:17 +00:00
|
|
|
GtkWidget *window, *textview, *sw, *button, *button2;
|
2011-02-09 14:41:39 +00:00
|
|
|
GtkTextBuffer *buffer;
|
2014-01-08 13:02:17 +00:00
|
|
|
GtkTextChildAnchor *anchor;
|
2011-02-09 14:41:39 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2011-02-09 14:41:39 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2014-07-28 19:19:31 +00:00
|
|
|
textview = g_object_new (my_text_view_get_type (), NULL);
|
2011-02-09 14:41:39 +00:00
|
|
|
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
|
2014-01-08 13:02:17 +00:00
|
|
|
button = gtk_button_new_with_label ("Fixed Child");
|
|
|
|
button2 = gtk_button_new_with_label ("Flowed Child");
|
|
|
|
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
2011-02-09 14:41:39 +00:00
|
|
|
|
2014-01-08 13:02:17 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 400, 400);
|
2011-02-14 06:27:42 +00:00
|
|
|
|
2011-02-09 14:41:39 +00:00
|
|
|
create_tags (buffer);
|
2014-01-08 13:02:17 +00:00
|
|
|
anchor = insert_text (buffer);
|
|
|
|
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), sw);
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), textview);
|
2019-10-04 02:21:45 +00:00
|
|
|
gtk_text_view_add_overlay (GTK_TEXT_VIEW (textview),
|
|
|
|
button,
|
|
|
|
50, 150);
|
2014-01-08 13:02:17 +00:00
|
|
|
|
|
|
|
gtk_text_view_add_child_at_anchor (GTK_TEXT_VIEW (textview),
|
|
|
|
button2, anchor);
|
2011-02-09 14:41:39 +00:00
|
|
|
|
|
|
|
gtk_widget_show (window);
|
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (TRUE)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2011-02-09 14:41:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|