WeixiangYan/CodeTransOcean huggingface dataset:
Task: Detect division by zero







Python: def div_check(x, y):
  try:
    x / y
  except ZeroDivisionError:
    return True
  else:
    return False





Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;

procedure Divide_By_Zero is
   Fnum : Float := 1.0;
   Fdenom : Float := 0.0;
   Fresult : Float;
   Inum : Integer := 1;
   Idenom : Integer := 0;
   Iresult : Integer;
begin
   begin
      Put("Integer divide by zero: ");
      Iresult := Inum / Idenom;
      Put(Item => Iresult);
   exception
      when Constraint_Error =>
         Put("Division by zero detected.");
   end;
   New_Line;
   Put("Floating point divide by zero: ");
   Fresult := Fnum / Fdenom;
   if Fresult > Float'Last or Fresult < Float'First then
      Put("Division by zero detected (infinite value).");
   else
      Put(Item => Fresult, Aft => 9, Exp => 0);
   end if;
   New_Line;
end Divide_By_Zero;
Task: Detect division by zero








VB: Option Explicit

Sub Main()
Dim Div
    If CatchDivideByZero(152, 0, Div) Then Debug.Print Div Else Debug.Print "Error"
    If CatchDivideByZero(152, 10, Div) Then Debug.Print Div Else Debug.Print "Error"
End Sub

Function CatchDivideByZero(Num, Den, Div) As Boolean
    On Error Resume Next
    Div = Num / Den
    If Err = 0 Then CatchDivideByZero = True
    On Error GoTo 0
End Function




Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;

procedure Divide_By_Zero is
   Fnum : Float := 1.0;
   Fdenom : Float := 0.0;
   Fresult : Float;
   Inum : Integer := 1;
   Idenom : Integer := 0;
   Iresult : Integer;
begin
   begin
      Put("Integer divide by zero: ");
      Iresult := Inum / Idenom;
      Put(Item => Iresult);
   exception
      when Constraint_Error =>
         Put("Division by zero detected.");
   end;
   New_Line;
   Put("Floating point divide by zero: ");
   Fresult := Fnum / Fdenom;
   if Fresult > Float'Last or Fresult < Float'First then
      Put("Division by zero detected (infinite value).");
   else
      Put(Item => Fresult, Aft => 9, Exp => 0);
   end if;
   New_Line;
end Divide_By_Zero;
Task: Detect division by zero

C: #include <limits.h>	
#include <setjmp.h>	
#include <stdio.h>	
#include <stdlib.h>	
#include <signal.h>	

static sigjmp_buf fpe_env;


static void
fpe_handler(int signal, siginfo_t *w, void *a)
{
	siglongjmp(fpe_env, w->si_code);
	
}


void
try_division(int x, int y)
{
	struct sigaction act, old;
	int code;
	
	volatile int result;

	
	code = sigsetjmp(fpe_env, 1);
	if (code == 0) {
		
		act.sa_sigaction = fpe_handler;
		sigemptyset(&act.sa_mask);
		act.sa_flags = SA_SIGINFO;
		if (sigaction(SIGFPE, &act, &old) < 0) {
			perror("sigaction");
			exit(1);
		}

		
		result = x / y;

		
		if (sigaction(SIGFPE, &old, NULL) < 0) {
			perror("sigaction");
			exit(1);
		}

		printf("%d / %d is %d\n", x, y, result);
	} else {
		
		if (sigaction(SIGFPE, &old, NULL) < 0) {
			perror("sigaction");
			exit(1);
		}

		
		switch (code) {
		case FPE_INTDIV: 
		case FPE_FLTDIV: 
			printf("%d / %d: caught division by zero!\n", x, y);
			break;
		default:
			printf("%d / %d: caught mysterious error!\n", x, y);
			break;
		}
	}
}


int
main()
{
	try_division(-44, 0);
	try_division(-44, 5);
	try_division(0, 5);
	try_division(0, 0);
	try_division(INT_MIN, -1);
	return 0;
}
Task: Detect division by zero



C#: using System;

namespace RosettaCode {
    class Program {
        static void Main(string[] args) {
            int x = 1;
            int y = 0;
            try {
               int z = x / y;
            } catch (DivideByZeroException e) {
                Console.WriteLine(e);
            }
            
        }
    }
}
Task: Detect division by zero


C++: #include<iostream>
#include<csignal> 
#include<cstdlib>

using namespace std;

void fpe_handler(int signal)
{
    cerr << "Floating Point Exception: division by zero" << endl;
    exit(signal);
}

int main()
{
    
    signal(SIGFPE, fpe_handler);

    int a = 1;
    int b = 0;
    cout << a/b << endl;

    return 0;
}
Task: Detect division by zero





Java: public static boolean infinity(double numer, double denom){
	return Double.isInfinite(numer/denom);
}
Task: Detect division by zero







Python: def div_check(x, y):
  try:
    x / y
  except ZeroDivisionError:
    return True
  else:
    return False
Task: Detect division by zero








VB: Option Explicit

Sub Main()
Dim Div
    If CatchDivideByZero(152, 0, Div) Then Debug.Print Div Else Debug.Print "Error"
    If CatchDivideByZero(152, 10, Div) Then Debug.Print Div Else Debug.Print "Error"
End Sub

Function CatchDivideByZero(Num, Den, Div) As Boolean
    On Error Resume Next
    Div = Num / Den
    If Err = 0 Then CatchDivideByZero = True
    On Error GoTo 0
End Function
Task: Detect division by zero




Go: package main

import "fmt"

func divCheck(x, y int) (q int, ok bool) {
    defer func() {
        recover()
    }()
    q = x / y
    return q, true
}

func main() {
    fmt.Println(divCheck(3, 2))
    fmt.Println(divCheck(3, 0))
}
Task: Detect division by zero



C#: using System;

namespace RosettaCode {
    class Program {
        static void Main(string[] args) {
            int x = 1;
            int y = 0;
            try {
               int z = x / y;
            } catch (DivideByZeroException e) {
                Console.WriteLine(e);
            }
            
        }
    }
}








Fortran: program  rosetta_divbyzero
   implicit none
   integer, parameter :: rdp = kind(1.d0)
   real(rdp) :: normal,zero

   normal = 1.d0
   zero = 0.d0

   call div_by_zero_check(normal,zero)

 contains

   subroutine  div_by_zero_check(x,y)
      use, intrinsic  :: ieee_exceptions
      use, intrinsic  :: ieee_arithmetic
      implicit none
      real(rdp), intent(in) :: x,y

      real(rdp) :: check
      type(ieee_status_type) :: status_value
      logical :: flag
      flag = .false.
      
      call ieee_get_status(status_value)
      
      call ieee_set_flag(ieee_divide_by_zero,.false.)
      write(*,*)"Inf supported? ",ieee_support_inf(check)

      
      check = x/y
      write(*,*)"Is check finite?",ieee_is_finite(check), check

      call ieee_get_flag(ieee_divide_by_zero, flag)
      if (flag) write(*,*)"Warning

      
      call ieee_set_status(status_value)

   end subroutine div_by_zero_check

end program rosetta_divbyzero
Task: Detect division by zero


C++: #include<iostream>
#include<csignal> 
#include<cstdlib>

using namespace std;

void fpe_handler(int signal)
{
    cerr << "Floating Point Exception: division by zero" << endl;
    exit(signal);
}

int main()
{
    
    signal(SIGFPE, fpe_handler);

    int a = 1;
    int b = 0;
    cout << a/b << endl;

    return 0;
}









Fortran: program  rosetta_divbyzero
   implicit none
   integer, parameter :: rdp = kind(1.d0)
   real(rdp) :: normal,zero

   normal = 1.d0
   zero = 0.d0

   call div_by_zero_check(normal,zero)

 contains

   subroutine  div_by_zero_check(x,y)
      use, intrinsic  :: ieee_exceptions
      use, intrinsic  :: ieee_arithmetic
      implicit none
      real(rdp), intent(in) :: x,y

      real(rdp) :: check
      type(ieee_status_type) :: status_value
      logical :: flag
      flag = .false.
      
      call ieee_get_status(status_value)
      
      call ieee_set_flag(ieee_divide_by_zero,.false.)
      write(*,*)"Inf supported? ",ieee_support_inf(check)

      
      check = x/y
      write(*,*)"Is check finite?",ieee_is_finite(check), check

      call ieee_get_flag(ieee_divide_by_zero, flag)
      if (flag) write(*,*)"Warning

      
      call ieee_set_status(status_value)

   end subroutine div_by_zero_check

end program rosetta_divbyzero
Task: Detect division by zero

C: #include <limits.h>	
#include <setjmp.h>	
#include <stdio.h>	
#include <stdlib.h>	
#include <signal.h>	

static sigjmp_buf fpe_env;


static void
fpe_handler(int signal, siginfo_t *w, void *a)
{
	siglongjmp(fpe_env, w->si_code);
	
}


void
try_division(int x, int y)
{
	struct sigaction act, old;
	int code;
	
	volatile int result;

	
	code = sigsetjmp(fpe_env, 1);
	if (code == 0) {
		
		act.sa_sigaction = fpe_handler;
		sigemptyset(&act.sa_mask);
		act.sa_flags = SA_SIGINFO;
		if (sigaction(SIGFPE, &act, &old) < 0) {
			perror("sigaction");
			exit(1);
		}

		
		result = x / y;

		
		if (sigaction(SIGFPE, &old, NULL) < 0) {
			perror("sigaction");
			exit(1);
		}

		printf("%d / %d is %d\n", x, y, result);
	} else {
		
		if (sigaction(SIGFPE, &old, NULL) < 0) {
			perror("sigaction");
			exit(1);
		}

		
		switch (code) {
		case FPE_INTDIV: 
		case FPE_FLTDIV: 
			printf("%d / %d: caught division by zero!\n", x, y);
			break;
		default:
			printf("%d / %d: caught mysterious error!\n", x, y);
			break;
		}
	}
}


int
main()
{
	try_division(-44, 0);
	try_division(-44, 5);
	try_division(0, 5);
	try_division(0, 0);
	try_division(INT_MIN, -1);
	return 0;
}










Fortran: program  rosetta_divbyzero
   implicit none
   integer, parameter :: rdp = kind(1.d0)
   real(rdp) :: normal,zero

   normal = 1.d0
   zero = 0.d0

   call div_by_zero_check(normal,zero)

 contains

   subroutine  div_by_zero_check(x,y)
      use, intrinsic  :: ieee_exceptions
      use, intrinsic  :: ieee_arithmetic
      implicit none
      real(rdp), intent(in) :: x,y

      real(rdp) :: check
      type(ieee_status_type) :: status_value
      logical :: flag
      flag = .false.
      
      call ieee_get_status(status_value)
      
      call ieee_set_flag(ieee_divide_by_zero,.false.)
      write(*,*)"Inf supported? ",ieee_support_inf(check)

      
      check = x/y
      write(*,*)"Is check finite?",ieee_is_finite(check), check

      call ieee_get_flag(ieee_divide_by_zero, flag)
      if (flag) write(*,*)"Warning

      
      call ieee_set_status(status_value)

   end subroutine div_by_zero_check

end program rosetta_divbyzero
Task: Detect division by zero





Java: public static boolean infinity(double numer, double denom){
	return Double.isInfinite(numer/denom);
}






Fortran: program  rosetta_divbyzero
   implicit none
   integer, parameter :: rdp = kind(1.d0)
   real(rdp) :: normal,zero

   normal = 1.d0
   zero = 0.d0

   call div_by_zero_check(normal,zero)

 contains

   subroutine  div_by_zero_check(x,y)
      use, intrinsic  :: ieee_exceptions
      use, intrinsic  :: ieee_arithmetic
      implicit none
      real(rdp), intent(in) :: x,y

      real(rdp) :: check
      type(ieee_status_type) :: status_value
      logical :: flag
      flag = .false.
      
      call ieee_get_status(status_value)
      
      call ieee_set_flag(ieee_divide_by_zero,.false.)
      write(*,*)"Inf supported? ",ieee_support_inf(check)

      
      check = x/y
      write(*,*)"Is check finite?",ieee_is_finite(check), check

      call ieee_get_flag(ieee_divide_by_zero, flag)
      if (flag) write(*,*)"Warning

      
      call ieee_set_status(status_value)

   end subroutine div_by_zero_check

end program rosetta_divbyzero
Task: Detect division by zero







Python: def div_check(x, y):
  try:
    x / y
  except ZeroDivisionError:
    return True
  else:
    return False




Fortran: program  rosetta_divbyzero
   implicit none
   integer, parameter :: rdp = kind(1.d0)
   real(rdp) :: normal,zero

   normal = 1.d0
   zero = 0.d0

   call div_by_zero_check(normal,zero)

 contains

   subroutine  div_by_zero_check(x,y)
      use, intrinsic  :: ieee_exceptions
      use, intrinsic  :: ieee_arithmetic
      implicit none
      real(rdp), intent(in) :: x,y

      real(rdp) :: check
      type(ieee_status_type) :: status_value
      logical :: flag
      flag = .false.
      
      call ieee_get_status(status_value)
      
      call ieee_set_flag(ieee_divide_by_zero,.false.)
      write(*,*)"Inf supported? ",ieee_support_inf(check)

      
      check = x/y
      write(*,*)"Is check finite?",ieee_is_finite(check), check

      call ieee_get_flag(ieee_divide_by_zero, flag)
      if (flag) write(*,*)"Warning

      
      call ieee_set_status(status_value)

   end subroutine div_by_zero_check

end program rosetta_divbyzero
Task: Detect division by zero








VB: Option Explicit

Sub Main()
Dim Div
    If CatchDivideByZero(152, 0, Div) Then Debug.Print Div Else Debug.Print "Error"
    If CatchDivideByZero(152, 10, Div) Then Debug.Print Div Else Debug.Print "Error"
End Sub

Function CatchDivideByZero(Num, Den, Div) As Boolean
    On Error Resume Next
    Div = Num / Den
    If Err = 0 Then CatchDivideByZero = True
    On Error GoTo 0
End Function



Fortran: program  rosetta_divbyzero
   implicit none
   integer, parameter :: rdp = kind(1.d0)
   real(rdp) :: normal,zero

   normal = 1.d0
   zero = 0.d0

   call div_by_zero_check(normal,zero)

 contains

   subroutine  div_by_zero_check(x,y)
      use, intrinsic  :: ieee_exceptions
      use, intrinsic  :: ieee_arithmetic
      implicit none
      real(rdp), intent(in) :: x,y

      real(rdp) :: check
      type(ieee_status_type) :: status_value
      logical :: flag
      flag = .false.
      
      call ieee_get_status(status_value)
      
      call ieee_set_flag(ieee_divide_by_zero,.false.)
      write(*,*)"Inf supported? ",ieee_support_inf(check)

      
      check = x/y
      write(*,*)"Is check finite?",ieee_is_finite(check), check

      call ieee_get_flag(ieee_divide_by_zero, flag)
      if (flag) write(*,*)"Warning

      
      call ieee_set_status(status_value)

   end subroutine div_by_zero_check

end program rosetta_divbyzero
Task: Detect division by zero






PHP: function div_check($x, $y) {
  @trigger_error(''); // a dummy to detect when error didn't occur
  @($x / $y);
  $e = error_get_last();
  return $e['message'] != '';
}





Fortran: program  rosetta_divbyzero
   implicit none
   integer, parameter :: rdp = kind(1.d0)
   real(rdp) :: normal,zero

   normal = 1.d0
   zero = 0.d0

   call div_by_zero_check(normal,zero)

 contains

   subroutine  div_by_zero_check(x,y)
      use, intrinsic  :: ieee_exceptions
      use, intrinsic  :: ieee_arithmetic
      implicit none
      real(rdp), intent(in) :: x,y

      real(rdp) :: check
      type(ieee_status_type) :: status_value
      logical :: flag
      flag = .false.
      
      call ieee_get_status(status_value)
      
      call ieee_set_flag(ieee_divide_by_zero,.false.)
      write(*,*)"Inf supported? ",ieee_support_inf(check)

      
      check = x/y
      write(*,*)"Is check finite?",ieee_is_finite(check), check

      call ieee_get_flag(ieee_divide_by_zero, flag)
      if (flag) write(*,*)"Warning

      
      call ieee_set_status(status_value)

   end subroutine div_by_zero_check

end program rosetta_divbyzero
Task: Detect division by zero

C: #include <limits.h>	
#include <setjmp.h>	
#include <stdio.h>	
#include <stdlib.h>	
#include <signal.h>	

static sigjmp_buf fpe_env;


static void
fpe_handler(int signal, siginfo_t *w, void *a)
{
	siglongjmp(fpe_env, w->si_code);
	
}


void
try_division(int x, int y)
{
	struct sigaction act, old;
	int code;
	
	volatile int result;

	
	code = sigsetjmp(fpe_env, 1);
	if (code == 0) {
		
		act.sa_sigaction = fpe_handler;
		sigemptyset(&act.sa_mask);
		act.sa_flags = SA_SIGINFO;
		if (sigaction(SIGFPE, &act, &old) < 0) {
			perror("sigaction");
			exit(1);
		}

		
		result = x / y;

		
		if (sigaction(SIGFPE, &old, NULL) < 0) {
			perror("sigaction");
			exit(1);
		}

		printf("%d / %d is %d\n", x, y, result);
	} else {
		
		if (sigaction(SIGFPE, &old, NULL) < 0) {
			perror("sigaction");
			exit(1);
		}

		
		switch (code) {
		case FPE_INTDIV: 
		case FPE_FLTDIV: 
			printf("%d / %d: caught division by zero!\n", x, y);
			break;
		default:
			printf("%d / %d: caught mysterious error!\n", x, y);
			break;
		}
	}
}


int
main()
{
	try_division(-44, 0);
	try_division(-44, 5);
	try_division(0, 5);
	try_division(0, 0);
	try_division(INT_MIN, -1);
	return 0;
}









COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Detect division by zero



C#: using System;

namespace RosettaCode {
    class Program {
        static void Main(string[] args) {
            int x = 1;
            int y = 0;
            try {
               int z = x / y;
            } catch (DivideByZeroException e) {
                Console.WriteLine(e);
            }
            
        }
    }
}







COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Detect division by zero


C++: #include<iostream>
#include<csignal> 
#include<cstdlib>

using namespace std;

void fpe_handler(int signal)
{
    cerr << "Floating Point Exception: division by zero" << endl;
    exit(signal);
}

int main()
{
    
    signal(SIGFPE, fpe_handler);

    int a = 1;
    int b = 0;
    cout << a/b << endl;

    return 0;
}








COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Detect division by zero





Java: public static boolean infinity(double numer, double denom){
	return Double.isInfinite(numer/denom);
}





COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Detect division by zero







Python: def div_check(x, y):
  try:
    x / y
  except ZeroDivisionError:
    return True
  else:
    return False



COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Detect division by zero








VB: Option Explicit

Sub Main()
Dim Div
    If CatchDivideByZero(152, 0, Div) Then Debug.Print Div Else Debug.Print "Error"
    If CatchDivideByZero(152, 10, Div) Then Debug.Print Div Else Debug.Print "Error"
End Sub

Function CatchDivideByZero(Num, Den, Div) As Boolean
    On Error Resume Next
    Div = Num / Den
    If Err = 0 Then CatchDivideByZero = True
    On Error GoTo 0
End Function


COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Detect division by zero




Go: package main

import "fmt"

func divCheck(x, y int) (q int, ok bool) {
    defer func() {
        recover()
    }()
    q = x / y
    return q, true
}

func main() {
    fmt.Println(divCheck(3, 2))
    fmt.Println(divCheck(3, 0))
}






COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Detect division by zero






PHP: function div_check($x, $y) {
  @trigger_error(''); // a dummy to detect when error didn't occur
  @($x / $y);
  $e = error_get_last();
  return $e['message'] != '';
}
Task: Detect division by zero






PHP: function div_check($x, $y) {
  @trigger_error(''); // a dummy to detect when error didn't occur
  @($x / $y);
  $e = error_get_last();
  return $e['message'] != '';
}






Ada: 

with Ada.Text_Io; use Ada.Text_Io;
with Ada.Float_Text_Io; use Ada.Float_Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;

procedure Divide_By_Zero is
   Fnum : Float := 1.0;
   Fdenom : Float := 0.0;
   Fresult : Float;
   Inum : Integer := 1;
   Idenom : Integer := 0;
   Iresult : Integer;
begin
   begin
      Put("Integer divide by zero: ");
      Iresult := Inum / Idenom;
      Put(Item => Iresult);
   exception
      when Constraint_Error =>
         Put("Division by zero detected.");
   end;
   New_Line;
   Put("Floating point divide by zero: ");
   Fresult := Fnum / Fdenom;
   if Fresult > Float'Last or Fresult < Float'First then
      Put("Division by zero detected (infinite value).");
   else
      Put(Item => Fresult, Aft => 9, Exp => 0);
   end if;
   New_Line;
end Divide_By_Zero;
Task: Detect division by zero






PHP: function div_check($x, $y) {
  @trigger_error(''); // a dummy to detect when error didn't occur
  @($x / $y);
  $e = error_get_last();
  return $e['message'] != '';
}




COBOL: DIVIDE foo BY bar GIVING foobar
    ON SIZE ERROR
        DISPLAY "Division by zero detected!"
END-DIVIDE
Task: Mouse position



C#: using System;
using System.Windows.Forms;
static class Program
{
    [STAThread]
    static void Main()
    {
        Console.WriteLine(Control.MousePosition.X);
        Console.WriteLine(Control.MousePosition.Y);
    }
}









Ada: with GLib;        use GLib;
with Gtk.Button;  use Gtk.Button;
with Gtk.Label;   use Gtk.Label;
with Gtk.Window;  use Gtk.Window;
with Gtk.Widget;  use Gtk.Widget;
with Gtk.Table;   use Gtk.Table;
 
with Gtk.Handlers;
with Gtk.Main;
 
procedure Tell_Mouse is
   Window : Gtk_Window;
   Grid   : Gtk_Table;
   Button : Gtk_Button;
   Label  : Gtk_Label;
 
   package Handlers is new Gtk.Handlers.Callback (Gtk_Widget_Record);
   package Return_Handlers is
      new Gtk.Handlers.Return_Callback (Gtk_Widget_Record, Boolean);
 
   function Delete_Event (Widget : access Gtk_Widget_Record'Class)
      return Boolean is
   begin
      return False;
   end Delete_Event;
 
   procedure Destroy (Widget : access Gtk_Widget_Record'Class) is
   begin
      Gtk.Main.Main_Quit;
   end Destroy;
 
   procedure Clicked (Widget : access Gtk_Widget_Record'Class) is
      X, Y : GInt;
   begin
      Get_Pointer (Window, X, Y);
      Set_Text (Label, "At" & GInt'Image (X) & GInt'Image (Y));
   end Clicked;
 
begin
   Gtk.Main.Init;
   Gtk.Window.Gtk_New (Window);
   Gtk_New (Grid, 1, 2, False);
   Add (Window, Grid);
   Gtk_New (Label);
   Attach (Grid, Label, 0, 1, 0, 1);
   Gtk_New (Button, "Click me");
   Attach (Grid, Button, 0, 1, 1, 2);
   Return_Handlers.Connect
   (  Window,
      "delete_event",
      Return_Handlers.To_Marshaller (Delete_Event'Access)
   );
   Handlers.Connect
   (  Window,
      "destroy",
      Handlers.To_Marshaller (Destroy'Access)
   );
   Handlers.Connect
   (  Button,
      "clicked",
      Handlers.To_Marshaller (Clicked'Access)
   );
   Show_All (Grid);
   Show (Window);
 
   Gtk.Main.Main;
end Tell_Mouse;
Task: Mouse position

C: #include <stdio.h>
#include <X11/Xlib.h>

int main()
{
  Display *d;
  Window inwin;      
  Window inchildwin; 
  int rootx, rooty; 
  int childx, childy;  
  Atom atom_type_prop; 
  int actual_format;   
  unsigned int mask;   
  unsigned long n_items, bytes_after_ret;
  Window *props; 

  
  d = XOpenDisplay(NULL); 

  
  (void)XGetWindowProperty(d, DefaultRootWindow(d),
			   XInternAtom(d, "_NET_ACTIVE_WINDOW", True),
			   0, 1, False, AnyPropertyType,
			   &atom_type_prop, &actual_format,
			   &n_items, &bytes_after_ret, (unsigned char**)&props);
			  
  XQueryPointer(d, props[0], &inwin,  &inchildwin,
		&rootx, &rooty, &childx, &childy, &mask);
  printf("relative to active window: %d,%d\n", childx, childy);

  XFree(props);           
  (void)XCloseDisplay(d); 
  return 0;
}











Ada: with GLib;        use GLib;
with Gtk.Button;  use Gtk.Button;
with Gtk.Label;   use Gtk.Label;
with Gtk.Window;  use Gtk.Window;
with Gtk.Widget;  use Gtk.Widget;
with Gtk.Table;   use Gtk.Table;
 
with Gtk.Handlers;
with Gtk.Main;
 
procedure Tell_Mouse is
   Window : Gtk_Window;
   Grid   : Gtk_Table;
   Button : Gtk_Button;
   Label  : Gtk_Label;
 
   package Handlers is new Gtk.Handlers.Callback (Gtk_Widget_Record);
   package Return_Handlers is
      new Gtk.Handlers.Return_Callback (Gtk_Widget_Record, Boolean);
 
   function Delete_Event (Widget : access Gtk_Widget_Record'Class)
      return Boolean is
   begin
      return False;
   end Delete_Event;
 
   procedure Destroy (Widget : access Gtk_Widget_Record'Class) is
   begin
      Gtk.Main.Main_Quit;
   end Destroy;
 
   procedure Clicked (Widget : access Gtk_Widget_Record'Class) is
      X, Y : GInt;
   begin
      Get_Pointer (Window, X, Y);
      Set_Text (Label, "At" & GInt'Image (X) & GInt'Image (Y));
   end Clicked;
 
begin
   Gtk.Main.Init;
   Gtk.Window.Gtk_New (Window);
   Gtk_New (Grid, 1, 2, False);
   Add (Window, Grid);
   Gtk_New (Label);
   Attach (Grid, Label, 0, 1, 0, 1);
   Gtk_New (Button, "Click me");
   Attach (Grid, Button, 0, 1, 1, 2);
   Return_Handlers.Connect
   (  Window,
      "delete_event",
      Return_Handlers.To_Marshaller (Delete_Event'Access)
   );
   Handlers.Connect
   (  Window,
      "destroy",
      Handlers.To_Marshaller (Destroy'Access)
   );
   Handlers.Connect
   (  Button,
      "clicked",
      Handlers.To_Marshaller (Clicked'Access)
   );
   Show_All (Grid);
   Show (Window);
 
   Gtk.Main.Main;
end Tell_Mouse;