Re: Drawing Area issues ( continued, with example )





Hi Torsten,

This is something that I have had some trouble with at the app level. In GTK3, how drawing is done, has changed a bit between minor versions in GTK3. In order to get CSS, transparency, OpenGL and modern drawing techniques working well,,, things have changed. Here is an article about container windows. I have always considered containers not to have windows to draw on but that may not be true anymore.

https://blog.gtk.org/2017/05/24/container-secrets/

Some widgets might use the background window. For example a label or even a plug inside a socket. You can always try a GtkEventBox if you need a window to draw on behind a widget if it doesn't have it's own window.

A GtkDrawingArea has it's own window to draw on. If you put a drawing area over the main window and draw on both with transparency you can see this. How GTK draws the layout using cairo and the compositor might be a different argument though. Cairo contexts are the same.

The following has two transparent windows that you can draw on. Check out the context and gdkwindow pointer values.

Tested on Ubuntu 16.04 and GTK3.18. If you have GTK3.22 there will probably be a warning about is_composited() but it should still work. More changes.

Eric

#!/usr/bin/perl
use strict;
use diagnostics;
use warnings;
use Gtk3 '-init';
use Glib qw(TRUE FALSE);

my $window = Gtk3::Window->new();
$window->signal_connect('delete_event' => sub { Gtk3->main_quit; });
$window->set_default_size(400, 400);
$window->set_border_width(10);
$window->set_title("Layers");
$window->set_app_paintable(TRUE);
if ($window->is_composited())
      {
        my $screen = $window->get_screen(); 
        my $visual=$screen->get_rgba_visual();
        $window->set_visual($visual);
      }
    else
      {
        print "Couldn't set transparency.\n"
      }
$window->signal_connect('draw' => \&draw_window);

my $da = Gtk3::DrawingArea->new();
$da->set_vexpand(TRUE);
$da->set_hexpand(TRUE);
$da->signal_connect('draw' => \&draw_da);

$window->add($da);

$window->show_all();
Gtk3->main;

sub draw_da
{
  my ($widget, $cr, $data) = @_;
  print "da context " . $cr . "\n" . " da GdkWindow " . $widget->get_window() . "\n";
  my $width = $widget->get_allocated_width();
  my $height = $widget->get_allocated_height();
 
  #Drawing area tranparent background.
  $cr->set_source_rgba(0, 0, 0, 0);
  $cr->paint;

  #Draw a line.
  $cr->set_source_rgba(0, 1, 1, 0.5);
  $cr->set_line_width(40);
 
  $cr->move_to(0, $height / 2);
  $cr->line_to($width, $height / 2);
  $cr->stroke();
 
  return FALSE;
}
sub draw_window
{
  my ($widget, $cr, $data) = @_;
  print "window context " . $cr . "\n" . " window GdkWindow " . $widget->get_window() . "\n";
  my $width = $widget->get_allocated_width();
  my $height = $widget->get_allocated_height();
 
  #Window semi-tranparent background.
  $cr->set_source_rgba(0, 0, 1, 0.6);
  $cr->paint;

  #Draw a line.
  $cr->set_source_rgba(1, 1, 0, 0.5);
  $cr->set_line_width(40); 
  $cr->move_to($width / 2, 0);
  $cr->line_to($width / 2, $height);
  $cr->stroke();
 
  return FALSE;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]