GtkSharp/sample/CanvasExample.cs
Dan Winship 62258ca7c3 * generator/CallbackGen.cs:
* generator/CodeGenerator.cs: 
        * generator/ManagedCallString.cs:
        * generator/Property.cs: Remove unused vars

        * generator/Method.cs (GetHashCode): have to implement this since
        we're overriding Equals.

        * generator/CallbackGen.cs: print a message when generating a
        broken struct-returning callback. (Currently affects
        GtkSharp.TextSegSplitFuncNative and
        GtkSharp.TextSegCleanupFuncNative)

        * gdk/glue/device.c: 
        * gdk/glue/dragcontext.c: Add missing prototypes

        * gtk/Gtk.metadata: Mark SeparatorToolItem.Draw "new". Re-rename
        CheckMenuItem.Toggled to EmitToggled rather than Toggle, since
        that's a better description of what it does.

        * gtk/CheckMenuItem.custom: implement a "Toggle" method that does
        what the documentation claims it does.

        * gtk/NodeStore.cs: remove unused var

        * gnome/Gnome.metadata: mark DateEdit.Flags, Dialog.Default, and
        PropertyBox.State "new". Hide GnomePixmapEntry.GnomeEntry and
        GnomePixmapEntry.GtkEntry since they do exactly the same thing as
        the methods of the same names inherited from GnomeFileEntry.

        * gnome/glue/canvas-proxy.c:
        * gnome/glue/canvas-proxy.h:
        * gnome/glue/canvas-proxy-marshal.c:
        * gnome/glue/canvas-proxy-marshal.h:
        * gnome/glue/canvas-proxy-marshal.list: Remove unused code

        * gnome/glue/Makefile.am (libgnomesharpglue_2_la_SOURCES): update

        * panelapplet/PanelApplet.metadata: mark PanelApplet.Flags "new"

        * sample/CanvasExample.cs: 
        * sample/CustomCellRenderer.cs: 
        * sample/CustomNotebook.cs: 
        * sample/DrawingSample.cs:
        * sample/Fifteen.cs: 
        * sample/GladeTest.cs: 
        * sample/GtkDemo/DemoHyperText.cs: 
        * sample/GtkDemo/DemoPixbuf.cs: 
        * sample/ScribbleXInput.cs: remove unused vars, use
        GLib.Timeout.Add rather than the deprecated Gtk.Timeout.Add

svn path=/trunk/gtk-sharp/; revision=38043
2004-12-21 18:46:42 +00:00

168 lines
3.8 KiB
C#

// Canvas.cs - port of standard GNOME canvas demo
//
// Author: Rachel Hestilow <hestilow@ximian.com>
//
// (c) 2002 Rachel Hestilow
namespace GtkSamples {
using Gnome;
using Gtk;
using Gdk;
using System;
public class CanvasExample {
private int width = 400, height = 400;
private double remember_x = 0.0, remember_y = 0.0;
private Canvas canvas;
private Random random = new Random ();
public CanvasExample () {
Gtk.Window win = new Gtk.Window ("Canvas example");
win.DeleteEvent += new DeleteEventHandler (Window_Delete);
VBox vbox = new VBox (false, 0);
win.Add (vbox);
vbox.PackStart (new Label ("Drag - move object.\n" +
"Double click - change color\n" +
"Right click - delete object"),
false, false, 0);
canvas = new Canvas ();
canvas.SetSizeRequest (width, height);
canvas.SetScrollRegion (0.0, 0.0, (double) width, (double) height);
vbox.PackStart (canvas, false, false, 0);
HBox hbox = new HBox (false, 0);
vbox.PackStart (hbox, false, false, 0);
Button add_button = new Button ("Add an object");
add_button.Clicked += new EventHandler (AddObject);
hbox.PackStart (add_button, false, false, 0);
Button quit_button = new Button ("Quit");
quit_button.Clicked += new EventHandler (Quit);
hbox.PackStart (quit_button, false, false, 0);
win.ShowAll ();
}
void Swap (ref double a, ref double b) {
double tmp = a;
a = b;
b = tmp;
}
void AddObject (object obj, EventArgs args)
{
double x1 = random.Next (width);
double y1 = random.Next (height);
double x2 = random.Next (width);
double y2 = random.Next (height);
if (x1 > x2)
Swap (ref x1, ref x2);
if (y1 > y2)
Swap (ref y1, ref y2);
if ((x2 - x1) < 10)
x2 += 10;
if ((y2 - y1) < 10)
y2 += 10;
CanvasRE item = null;
if (random.Next (2) > 0)
item = new CanvasRect (canvas.Root ());
else
item = new CanvasEllipse (canvas.Root ());
item.X1 = x1;
item.Y1 = y1;
item.X2 = x2;
item.Y2 = y2;
item.FillColor = "white";
item.OutlineColor = "black";
item.WidthUnits = 1.0;
item.CanvasEvent += new Gnome.CanvasEventHandler (Item_Event);
}
void ChangeItemColor (CanvasRE item)
{
string[] colors = new string[] {"red", "yellow", "green", "cyan", "blue", "magenta"};
item.FillColor = colors[random.Next (colors.Length)];
}
void Item_Event (object obj, Gnome.CanvasEventArgs args) {
EventButton ev = new EventButton (args.Event.Handle);
CanvasRE item = (CanvasRE) obj;
switch (ev.Type) {
case EventType.ButtonPress:
if (ev.Button == 1) {
remember_x = ev.X;
remember_y = ev.Y;
args.RetVal = true;
return;
} else if (ev.Button == 3) {
item.Destroy ();
args.RetVal = true;
return;
}
break;
case EventType.TwoButtonPress:
ChangeItemColor (item);
args.RetVal = true;
return;
case EventType.MotionNotify:
Gdk.ModifierType state = (Gdk.ModifierType) ev.State;
if ((state & Gdk.ModifierType.Button1Mask) != 0) {
double new_x = ev.X, new_y = ev.Y;
item.Move (new_x - remember_x, new_y - remember_y);
remember_x = new_x;
remember_y = new_y;
args.RetVal = true;
return;
}
break;
case EventType.EnterNotify:
item.WidthUnits = 3.0;
args.RetVal = true;
return;
case EventType.LeaveNotify:
item.WidthUnits = 1.0;
args.RetVal = true;
return;
}
args.RetVal = false;
return;
}
void Quit (object obj, EventArgs args)
{
Application.Quit ();
}
void Window_Delete (object obj, DeleteEventArgs args)
{
Application.Quit ();
args.RetVal = true;
}
public static int Main (string[] args)
{
Application.Init ();
new CanvasExample ();
Application.Run ();
return 0;
}
}
}