2011-02-15 16:19:07 +00:00
|
|
|
// Copyright (c) 2011 Novell, Inc.
|
2002-08-03 22:24:37 +00:00
|
|
|
//
|
2011-02-15 16:19:07 +00:00
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
using Gtk;
|
|
|
|
using Gdk;
|
|
|
|
using System;
|
2002-08-03 22:24:37 +00:00
|
|
|
|
|
|
|
namespace GtkSamples {
|
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
public class ScribbleArea : DrawingArea {
|
2002-08-03 22:24:37 +00:00
|
|
|
|
|
|
|
public static int Main (string[] args)
|
|
|
|
{
|
|
|
|
Application.Init ();
|
2011-02-15 16:19:07 +00:00
|
|
|
Gtk.Window win = new Gtk.Window ("Scribble");
|
|
|
|
win.DeleteEvent += delegate { Application.Quit (); };
|
|
|
|
win.BorderWidth = 8;
|
|
|
|
Frame frm = new Frame (null);
|
|
|
|
frm.ShadowType = ShadowType.In;
|
|
|
|
frm.Add (new ScribbleArea ());
|
|
|
|
win.Add (frm);
|
2002-08-03 22:24:37 +00:00
|
|
|
win.ShowAll ();
|
|
|
|
Application.Run ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
Cairo.Surface surface;
|
|
|
|
|
|
|
|
public ScribbleArea ()
|
2002-08-03 22:24:37 +00:00
|
|
|
{
|
2011-02-15 16:19:07 +00:00
|
|
|
SetSizeRequest (200, 200);
|
|
|
|
Events |= EventMask.ButtonPressMask | EventMask.PointerMotionMask | EventMask.PointerMotionHintMask;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
void ClearSurface ()
|
2002-08-03 22:24:37 +00:00
|
|
|
{
|
2011-02-15 16:19:07 +00:00
|
|
|
using (Cairo.Context ctx = new Cairo.Context (surface)) {
|
|
|
|
ctx.SetSourceRGB (1, 1, 1);
|
|
|
|
ctx.Paint ();
|
|
|
|
}
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
2003-03-15 21:02:25 +00:00
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
void DrawBrush (double x, double y)
|
|
|
|
{
|
|
|
|
using (Cairo.Context ctx = new Cairo.Context (surface)) {
|
|
|
|
ctx.Rectangle ((int) x - 3, (int) y - 3, 6, 6);
|
|
|
|
ctx.Fill ();
|
|
|
|
}
|
2002-08-03 22:24:37 +00:00
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
QueueDrawArea ((int) x - 3, (int) y - 3, 6, 6);
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
2011-02-15 16:19:07 +00:00
|
|
|
|
|
|
|
protected override bool OnButtonPressEvent (EventButton ev)
|
2002-08-03 22:24:37 +00:00
|
|
|
{
|
2011-02-15 16:19:07 +00:00
|
|
|
if (surface == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (ev.Button) {
|
|
|
|
case 1:
|
|
|
|
DrawBrush (ev.X, ev.Y);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
ClearSurface ();
|
|
|
|
QueueDraw ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
2011-02-15 16:19:07 +00:00
|
|
|
|
|
|
|
protected override bool OnConfigureEvent (EventConfigure ev)
|
2002-08-03 22:24:37 +00:00
|
|
|
{
|
2013-12-01 15:06:50 +00:00
|
|
|
if (surface != null) {
|
|
|
|
surface.Dispose ();
|
|
|
|
}
|
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
surface = ev.Window.CreateSimilarSurface (Cairo.Content.Color, AllocatedWidth, AllocatedHeight);
|
|
|
|
ClearSurface ();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnDrawn (Cairo.Context ctx)
|
|
|
|
{
|
|
|
|
ctx.SetSourceSurface (surface, 0, 0);
|
|
|
|
ctx.Paint ();
|
|
|
|
return false;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
protected override bool OnMotionNotifyEvent (EventMotion ev)
|
2002-08-03 22:24:37 +00:00
|
|
|
{
|
2011-02-15 16:19:07 +00:00
|
|
|
if (surface == null)
|
|
|
|
return false;
|
|
|
|
|
2002-08-03 22:24:37 +00:00
|
|
|
int x, y;
|
|
|
|
Gdk.ModifierType state;
|
2011-02-15 16:19:07 +00:00
|
|
|
ev.Window.GetPointer (out x, out y, out state);
|
|
|
|
if ((state & Gdk.ModifierType.Button1Mask) != 0)
|
|
|
|
DrawBrush (x, y);
|
2002-08-03 22:24:37 +00:00
|
|
|
|
2011-02-15 16:19:07 +00:00
|
|
|
return true;
|
2002-08-03 22:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|