]> Pileus Git - ~andy/gtk/blobdiff - tests/testtextview.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testtextview.c
index 2a7701f5b376d5feb2091d3216a01eca70b0e75f..2a495395407211dc83d5bc6fbe64c4e68eed41c9 100644 (file)
@@ -18,11 +18,14 @@ create_tags (GtkTextBuffer *buffer)
                               "scale", PANGO_SCALE_X_LARGE, NULL);
 
   gtk_text_buffer_create_tag (buffer, "semi_blue_foreground",
-                              "foreground", "rgba(0,0,255,0.5)", NULL);
+                              "foreground", "rgba(0,0,255,0.7)", NULL);
 
   gtk_text_buffer_create_tag (buffer, "semi_red_background",
                               "background", "rgba(255,0,0,0.5)", NULL);
 
+  gtk_text_buffer_create_tag (buffer, "semi_orange_paragraph_background",
+                              "paragraph-background", "rgba(255,165,0,0.5)", NULL);
+
   gtk_text_buffer_create_tag (buffer, "word_wrap",
                               "wrap_mode", GTK_WRAP_WORD, NULL);
 }
@@ -31,8 +34,9 @@ create_tags (GtkTextBuffer *buffer)
 static void
 insert_text (GtkTextBuffer *buffer)
 {
-  GtkTextIter iter;
-  GtkTextIter start, end;
+  GtkTextIter  iter;
+  GtkTextIter  start, end;
+  GtkTextMark *para_start;
 
   /* get start of buffer; each insertion will revalidate the
    * iterator to point to just after the inserted text.
@@ -59,7 +63,38 @@ insert_text (GtkTextBuffer *buffer)
                                            "semi_red_background",
                                            "x-large",
                                            NULL);
-  gtk_text_buffer_insert (buffer, &iter, ".", -1);
+  gtk_text_buffer_insert (buffer, &iter, ".\n\n", -1);
+
+  /* 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);
+
+  gtk_text_buffer_get_bounds (buffer, &start, &end);
+
+  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);
 
   /* Apply word_wrap tag to whole buffer */
   gtk_text_buffer_get_bounds (buffer, &start, &end);
@@ -67,56 +102,50 @@ insert_text (GtkTextBuffer *buffer)
 }
 
 
+/* 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)
+
 static cairo_pattern_t *
-get_pattern (void)
+get_checkered (void)
 {
-  static cairo_pattern_t *static_pattern = NULL;
+  /* need to respect pixman's stride being a multiple of 4 */
+  static unsigned char data[8] = { 0xFF, 0x00, 0x00, 0x00,
+                                   0x00, 0xFF, 0x00, 0x00 };
+  static cairo_surface_t *checkered = NULL;
+  cairo_pattern_t *pattern;
 
-  if (!static_pattern)
+  if (checkered == NULL)
     {
-      cairo_surface_t *surface = 
-       cairo_image_surface_create_from_png ("gradient1.png");
-
-      if (surface)
-       {
-         static_pattern = cairo_pattern_create_for_surface (surface);
-         cairo_pattern_set_extend (static_pattern, CAIRO_EXTEND_REFLECT);
-       }
-      else 
-       g_warning ("Failed to create surface for gradient1.png\n");
+      checkered = cairo_image_surface_create_for_data (data,
+                                                       CAIRO_FORMAT_A8,
+                                                       2, 2, 4);
     }
-  return static_pattern;
+
+  pattern = cairo_pattern_create_for_surface (checkered);
+  cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
+  cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
+
+  return pattern;
 }
 
 static void
 draw_background (GtkWidget *widget, cairo_t *cr)
 {
-  GtkAllocation allocation;
   cairo_pattern_t *pat;
-  
-  gtk_widget_get_allocation (widget, &allocation);
 
   cairo_save (cr);
 
-#if 0
-  pat = cairo_pattern_create_linear (0.0, 0.0,  30.0, 30.0);
-  cairo_pattern_add_color_stop_rgba (pat, 1, 0, 0, 0, 1);
-  cairo_pattern_add_color_stop_rgba (pat, 0, 1, 1, 1, 1);
-  cairo_pattern_set_extend (pat, CAIRO_EXTEND_REPEAT);
-  cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
-  cairo_set_source (cr, pat);
-  cairo_fill (cr);
-  cairo_pattern_destroy (pat);
+  cairo_set_source_rgb (cr, CHECK_DARK, CHECK_DARK, CHECK_DARK);
+  cairo_paint (cr);
 
-#else
+  cairo_set_source_rgb (cr, CHECK_LIGHT, CHECK_LIGHT, CHECK_LIGHT);
+  cairo_scale (cr, CHECK_SIZE, CHECK_SIZE);
 
-  if (get_pattern ())
-    {
-      cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
-      cairo_set_source (cr, get_pattern ());
-      cairo_fill (cr);
-    }
-#endif
+  pat = get_checkered ();
+  cairo_mask (cr, pat);
+  cairo_pattern_destroy (pat);
 
   cairo_restore (cr);
 }
@@ -133,6 +162,8 @@ main (int argc, char **argv)
   textview = gtk_text_view_new ();
   buffer   = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));
 
+  gtk_window_set_default_size (GTK_WINDOW (window), 400, -1);
+
   create_tags (buffer);
   insert_text (buffer);