Re: Scribble Example



Thanks again. I've implemented the exact features in my application last
night ;-)

Now i know to draw on a canvas. Do you have any ideas on how i can draw
on a foreign window?
First of all, i know how to integrate a foreign window so i get a
gdk-window. (with use of xwininfo i get the xid etc.)
As a gdk window is a drawable i should be able to draw to it right? But
i have not really a clue how i should do it. 

Here is my current try:

#! /usr/bin/perl
use strict;
use Gtk2;
use Gnome2;
use Gnome2::Wnck;
use Glib qw/TRUE FALSE/;

Gtk2->init;

my $pixmap = undef;

my $screen = Gnome2::Wnck::Screen->get_default;
$screen->force_update;
my @windows = $screen->get_windows;
my $window;
my $counter = 0;
my $selwidget = Gnome2::Wnck::Selector->new;
foreach(@windows){
        $window = Gtk2::Gdk::Window->foreign_new ($_->get_xid);

        if (defined($window)){
        #~ $_->make_above;
        #~ $_->maximize_vertically;
        #~ $_->move_to_workspace( $screen->get_active_workspace);               
        #~ Gtk2::Gdk->flush;    
        #~ sleep 1;
        #~ $counter++;
        #~ my ($pwidth, $pheight) = $window->get_size();
        #~ my $new_pixbuf = Gtk2::Gdk::Pixbuf->new('GDK_COLORSPACE_RGB', TRUE,
8, $pwidth, $pheight);   
        #~ $new_pixbuf->get_from_drawable ($window, undef, 0, 0, 0, 0, $pwidth,
$pheight); 
        my $window_name = $_->get_name;
        $window_name =~ s/\//\_/g;
        #~ $new_pixbuf->save ("screen_".$window_name, 'png') if
defined($new_pixbuf);  

        print "Window-ID: $_ - Window-Name = ".$window_name."\n"; 
 
        my $area = new Gtk2::DrawingArea;
        $area->set_events ([qw/exposure-mask
                                                leave-notify-mask
                                                button-press-mask
                                                pointer-motion-mask
                                                pointer-motion-hint-mask/]);    
        $area->signal_connect (expose_event => \&expose_event);
        $area->signal_connect (configure_event => \&configure_event);
        $area->signal_connect (motion_notify_event => \&motion_notify_event);
        $area->signal_connect (key_press_event => \&key_press_event);   
        my ($xp, $yp, $widthp, $heightp) = $_->get_geometry;
        $area->size($window->get_size);
        $area->show;
                
        }
}

# Create a new backing pixmap of the appropriate size
sub configure_event {
  my $widget = shift; # GtkWidget         *widget
  my $event  = shift; # GdkEventConfigure *event

  $pixmap = Gtk2::Gdk::Pixmap->new ($widget->window,
                                    $widget->allocation->width,
                                    $widget->allocation->height,
                                    -1);
  $pixmap->draw_rectangle ($widget->style->white_gc,
                           TRUE,
                           0, 0,
                           $widget->allocation->width,
                           $widget->allocation->height);

  return TRUE;
}

# Redraw the screen from the backing pixmap
sub expose_event {
  my $widget = shift; # GtkWidget      *widget
  my $event  = shift; # GdkEventExpose *event

  $widget->window->draw_drawable (
                     $widget->style->fg_gc($widget->state),
                     $pixmap,
                     $event->area->x, $event->area->y,
                     $event->area->x, $event->area->y,
                     $event->area->width, $event->area->height);

  return FALSE;
}

# Draw a rectangle on the screen
sub draw_brush {
  my ($widget, $x, $y) = @_;

  # this is not a real GdkRectangle structure; we don't actually need
one.
  my @update_rect;
  $update_rect[0] = $x - 5;
  $update_rect[1] = $y - 5;
  $update_rect[2] = 10;
  $update_rect[3] = 10;
  $pixmap->draw_rectangle ($widget->style->black_gc,
                           TRUE, @update_rect);
  
  $widget->queue_draw_area (@update_rect);
}

sub button_press_event {
  my $widget = shift;   # GtkWidget      *widget
  my $event = shift;    # GdkEventButton *event

  if ($event->button == 1 && defined $pixmap) {
    draw_brush ($widget, $event->coords);
  }
  return TRUE;
}

sub motion_notify_event {
  my $widget = shift; # GtkWidget *widget
  my $event  = shift; # GdkEventMotion *event

  my ($x, $y, $state);


  if ($event->is_hint) {
    (undef, $x, $y, $state) = $event->window->get_pointer;
  } else {
    $x = $event->x;
    $y = $event->y;
    $state = $event->state;
  }

  if ($state >= "button1-mask" && defined $pixmap) {
    draw_brush ($widget, $x, $y);
  }
  
  return TRUE;
}


Gtk2->main; 

___________________END___________________________________

Am Donnerstag, den 10.07.2008, 07:36 -0400 schrieb zentara: 
On Wed, 09 Jul 2008 15:01:30 +0200
Mario Kemper <mario kemper googlemail com> wrote:

I am playing around with muppet's scribble example and it's working like

I'm having some trouble with the maillist, but I saw in the archives that
you liked my example. There is even a better one at

http://perlmonks.org?node_id=696556 

It allows choosing color, bruch size and even an imge underlay.

zentara





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