(***********************************************************************
   This file is part of GTKpas, an OOP wrapper for GTK+ written
   for Pascal.

   Copyright (C) 2000 by Johann Glaser <Johann.Glaser@gmx.at>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program in the file COPYING; if not, write to the 
   Free Software Foundation, Inc., 
   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
***********************************************************************)
Program RulersExample;
{$MODE OBJFPC}
Uses GLib,GDK,GTK,GTKpas;

Const XSIZE = 600;
      YSIZE = 400;

Type CRulersWin = Class(CWindow)
                HRule     : CHRuler;
                VRule     : CVRuler;
                Function  DeleteEventImpl(aEvent:PGdkEventAny):Gint; virtual; override;
                Procedure Destroy_; virtual; override;
                Function  DoMotionNotifyEvent(Sender:CWidget;Data:GPointer;P1:PGdkEventMotion):GInt;
              End;

Var Kit       : CMain;

Function CRulersWin.DeleteEventImpl(aEvent:PGdkEventAny):Gint;
Begin
  inherited DeleteEventImpl(aEvent);
  DeleteEventImpl := 0;
End;
Procedure CRulersWin.Destroy_;
Begin
  inherited Destroy_;
  Kit.Quit;
End;
Function CRulersWin.DoMotionNotifyEvent(Sender:CWidget;Data:GPointer;P1:PGdkEventMotion):GInt;
Begin
  (* As the mouse moves across the drawing area, a motion_notify_event
   * is passed to the appropriate event handler for the ruler. *)
  DoMotionNotifyEvent := HRule.DoMotionNotifyEvent(Sender,Data,P1);
  DoMotionNotifyEvent := VRule.DoMotionNotifyEvent(Sender,Data,P1);
End;

Var Window    : CRulersWin;
    Table     : CTable;
    Area      : CDrawingArea;

(* The main routine *)
Begin
  (* Initialize GTK and create the main window *)
  Kit := CMain.Create(@ArgC,@ArgV,False);

  Window := CRulersWin.Create(GTK_WINDOW_TOPLEVEL);
  Window.BorderWidth := 10;

  (* Create a Table for placing the ruler and the drawing Area *)
  Table := CTable.Create(3,2,FALSE);
  Window.Add(Table);

  Area := CDrawingArea.Create;
  Area.Size(XSIZE,YSIZE);
  Table.Attach(Area, 1, 2, 1, 2,
               GTK_EXPAND or GTK_FILL, GTK_FILL,0,0);
  Area.Events := GDK_POINTER_MOTION_MASK or GDK_POINTER_MOTION_HINT_MASK;

  (* The horizontal ruler goes on top. *)
  Window.HRule := CHRuler.Create;
  Window.HRule.Metric := GTK_PIXELS;
  Window.HRule.SetRange(7,13,0,20);
  Table.Attach(Window.HRule,1,2,0,1,GTK_EXPAND or GTK_SHRINK or GTK_FILL,GTK_FILL,0,0);
  
  (* The vertical ruler goes on the left. *)
  Window.VRule := CVRuler.Create;
  Window.VRule.Metric := GTK_PIXELS;
  Window.VRule.SetRange(0,YSIZE,10,YSIZE);
  Table.Attach(Window.VRule,0,1,1,2,GTK_FILL,GTK_EXPAND or GTK_SHRINK or GTK_FILL,0,0);

  (* Set up the handler for the motion_notify_event. As there can only be
   * one OnMotionNotifyEvent for the drawing area, we build a wrapper
   * function *)
  Area.OnMotionNotifyEvent := @Window.DoMotionNotifyEvent;

  (* Now show everything *)
  Area.Show;
  Window.HRule.Show;
  Window.VRule.Show;
  Table.Show;
  Window.Show;
  
  Kit.Run;
End.

