2002-08-03 22:24:37 +00:00
|
|
|
// Scribble.cs - port of Gtk+ scribble demo
|
|
|
|
//
|
|
|
|
// Author: Rachel Hestilow <hestilow@ximian.com>
|
|
|
|
//
|
|
|
|
// (c) 2002 Rachel Hestilow
|
|
|
|
|
|
|
|
namespace GtkSamples {
|
|
|
|
|
|
|
|
using Gtk;
|
|
|
|
using Gdk;
|
|
|
|
using GtkSharp;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
public class Scribble {
|
|
|
|
private static Gtk.DrawingArea darea;
|
|
|
|
private static Gdk.Pixmap pixmap = null;
|
|
|
|
|
|
|
|
public static int Main (string[] args)
|
|
|
|
{
|
|
|
|
Application.Init ();
|
|
|
|
Gtk.Window win = new Gtk.Window ("Scribble demo");
|
|
|
|
win.DeleteEvent += new DeleteEventHandler (Window_Delete);
|
|
|
|
|
|
|
|
darea = new Gtk.DrawingArea ();
|
|
|
|
darea.SetSizeRequest (200, 200);
|
|
|
|
win.Add (darea);
|
|
|
|
|
|
|
|
darea.ExposeEvent += new ExposeEventHandler (ExposeEvent);
|
|
|
|
darea.ConfigureEvent += new ConfigureEventHandler (ConfigureEvent);
|
|
|
|
darea.MotionNotifyEvent += new MotionNotifyEventHandler (MotionNotifyEvent);
|
|
|
|
darea.ButtonPressEvent += new ButtonPressEventHandler (ButtonPressEvent);
|
2002-08-04 04:23:13 +00:00
|
|
|
darea.Events = (int)EventMask.ExposureMask |
|
2002-09-16 11:12:01 +00:00
|
|
|
(int)EventMask.LeaveNotifyMask |
|
|
|
|
(int)EventMask.ButtonPressMask |
|
|
|
|
(int)EventMask.PointerMotionMask |
|
|
|
|
(int)EventMask.PointerMotionHintMask;
|
2002-08-03 22:24:37 +00:00
|
|
|
|
|
|
|
win.ShowAll ();
|
|
|
|
Application.Run ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Window_Delete (object obj, DeleteEventArgs args)
|
|
|
|
{
|
|
|
|
Application.Quit ();
|
2003-11-20 01:10:46 +00:00
|
|
|
args.RetVal = true;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ExposeEvent (object obj, ExposeEventArgs args)
|
|
|
|
{
|
|
|
|
Gdk.EventExpose ev = args.Event;
|
|
|
|
Gdk.Window window = ev.window;
|
|
|
|
// FIXME: mcs bug
|
|
|
|
Gdk.Rectangle area = ev.area;
|
|
|
|
// FIXME: array marshalling not done yet so no FG */
|
|
|
|
window.DrawDrawable (darea.Style.BlackGC,
|
|
|
|
pixmap,
|
|
|
|
area.x, area.y,
|
|
|
|
area.x, area.y,
|
|
|
|
area.width, area.height);
|
|
|
|
|
2003-11-20 01:10:46 +00:00
|
|
|
args.RetVal = false;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ConfigureEvent (object obj, ConfigureEventArgs args)
|
|
|
|
{
|
|
|
|
Gdk.EventConfigure ev = args.Event;
|
|
|
|
Gdk.Window window = ev.window;
|
|
|
|
Gdk.Rectangle allocation = darea.Allocation;
|
2003-03-15 21:02:25 +00:00
|
|
|
|
|
|
|
Console.WriteLine ("Darea=[{0}]" , darea);
|
2002-08-03 22:24:37 +00:00
|
|
|
pixmap = new Gdk.Pixmap (window,
|
|
|
|
allocation.width,
|
|
|
|
allocation.height,
|
|
|
|
-1);
|
2003-03-15 21:02:25 +00:00
|
|
|
Console.WriteLine ("Darea.Style={0}", darea.Style);
|
2003-07-15 05:52:09 +00:00
|
|
|
pixmap.DrawRectangle (darea.Style.WhiteGC, true, 0, 0,
|
2002-08-03 22:24:37 +00:00
|
|
|
allocation.width, allocation.height);
|
|
|
|
|
2003-11-20 01:10:46 +00:00
|
|
|
args.RetVal = true;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void DrawBrush (double x, double y)
|
|
|
|
{
|
|
|
|
Gdk.Rectangle update_rect = new Gdk.Rectangle ();
|
|
|
|
update_rect.x = (int) x - 5;
|
|
|
|
update_rect.y = (int) y - 5;
|
|
|
|
update_rect.width = 10;
|
|
|
|
update_rect.height = 10;
|
|
|
|
|
2003-07-15 05:52:09 +00:00
|
|
|
pixmap.DrawRectangle (darea.Style.BlackGC, true,
|
2002-08-03 22:24:37 +00:00
|
|
|
update_rect.x, update_rect.y,
|
|
|
|
update_rect.width, update_rect.height);
|
|
|
|
darea.QueueDrawArea (update_rect.x, update_rect.y,
|
|
|
|
update_rect.width, update_rect.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ButtonPressEvent (object obj, ButtonPressEventArgs args)
|
|
|
|
{
|
|
|
|
Gdk.EventButton ev = args.Event;
|
|
|
|
if (ev.button == 1 && pixmap != null)
|
|
|
|
DrawBrush (ev.x, ev.y);
|
|
|
|
|
2003-11-20 01:10:46 +00:00
|
|
|
args.RetVal = true;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void MotionNotifyEvent (object obj, MotionNotifyEventArgs args)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
Gdk.ModifierType state;
|
|
|
|
Gdk.EventMotion ev = args.Event;
|
|
|
|
Gdk.Window window = ev.window;
|
|
|
|
|
2002-11-06 05:44:49 +00:00
|
|
|
if (ev.is_hint != 0) {
|
2003-04-28 12:41:35 +00:00
|
|
|
Gdk.ModifierType s;
|
2002-11-06 05:44:49 +00:00
|
|
|
window.GetPointer (out x, out y, out s);
|
2003-04-28 12:41:35 +00:00
|
|
|
state = s;
|
2002-11-06 05:44:49 +00:00
|
|
|
} else {
|
2002-08-03 22:24:37 +00:00
|
|
|
x = (int) ev.x;
|
|
|
|
y = (int) ev.y;
|
|
|
|
state = (Gdk.ModifierType) ev.state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((state & Gdk.ModifierType.Button1Mask) != 0 && pixmap != null)
|
|
|
|
DrawBrush (x, y);
|
|
|
|
|
2003-11-20 01:10:46 +00:00
|
|
|
args.RetVal = true;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|