summaryrefslogtreecommitdiffstats
path: root/qtsharp/src/examples/samples/scribblewindow.cs
blob: 70ed3c0f5b31e4ae69db8a461d249c4d961040f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// scribblewindow.cs - Qt# scribblewindow
//
// Author: Adam Treat <manyoso@yahoo.com>
// (c) 2002 Adam Treat
// Licensed under the terms of the GNU GPL

namespace QtSamples {

	using Qt;
	using System;

	[DeclareQtSignal ("colorChanged(TTQColor)")]
	[DeclareQtSignal ("load(TTQString)")]
	[DeclareQtSignal ("save(TTQString)")]
	public class ScribbleWindow : TTQMainWindow {

		private TTQMenuBar menubar;
		private TTQPopupMenu filemenu;
		private TTQPopupMenu aboutmenu;
		private TTQScrollView scrollview;
		public ScribbleArea scribblearea;

		enum Color {Black, Red, Blue, Green, Yellow};

		public static int Main (String[] args)
		{
			TTQApplication app = new TTQApplication (args);
			ScribbleWindow demo = new ScribbleWindow ();
			demo.SetGeometry (50, 500, 400, 400);
			app.SetMainWidget (demo);
			demo.SetCaption ("Qt# 0.7!");
			demo.Show ();
			return app.Exec ();
		}

		ScribbleWindow () : base (null, null)
		{
			filemenu = new TTQPopupMenu (null, "filemenu");
			filemenu.InsertItem ("&Load", this, TQT_SLOT ("SlotLoad()") );
			filemenu.InsertItem ("&Save", this, TQT_SLOT ("SlotSave()") );
			filemenu.InsertSeparator ();
			filemenu.InsertItem ("&Quit", qApp, TQT_SLOT ("quit()"));

			aboutmenu = new TTQPopupMenu (null, "helpmenu");
			aboutmenu.InsertItem ("&About Qt-Sharp", this, TQT_SLOT ("SlotAboutQtSharp()"));
			aboutmenu.InsertItem ("&About Qt", this, TQT_SLOT ("SlotAboutQt()"));

			menubar = new TTQMenuBar (this, "");
			menubar.InsertItem ("&File", filemenu);
			menubar.InsertItem ("&Color", this, TQT_SLOT("SlotColorChooser()"));
			menubar.InsertItem ("&About", aboutmenu);

			scrollview = new TTQScrollView (this);
			scrollview.SetGeometry (0, menubar.Height (), Width (), Height () - menubar.Height ());
			scribblearea = new ScribbleArea (this);
			scribblearea.SetGeometry (0, 0, 1000, 1000);
			scrollview.AddChild (scribblearea);
			this.SetCentralWidget (scrollview);
			SetMaximumSize (Width (), Height () - menubar.Height ());

			TTQObject.Connect (this, TQT_SIGNAL ("colorChanged(TTQColor)"),
				scribblearea, TQT_SLOT ("SlotSetColor(TTQColor)") );
			TTQObject.Connect (this, TQT_SIGNAL ("load(TTQString)"),
				scribblearea, TQT_SLOT ("PerformLoad(TTQString)") );
			TTQObject.Connect (this, TQT_SIGNAL ("save(TTQString)"),
				scribblearea, TQT_SLOT ("PerformSave(TTQString)") );
		}

		public void SlotLoad ()
		{
			string filename = TTQFileDialog.GetOpenFileName (".", "*.bmp", this,
				null, "Load File", TTQString.Null, true);
			
			if ( filename != null )
				Emit ("load(TTQString)", (TTQString) filename);
		}

		public void SlotSave ()
		{
			string filename = TTQFileDialog.GetSaveFileName (".", "*.bmp", this,
				null, "Save File", TTQString.Null, true);

			if ( filename != null )
			{
				if ( ! filename.ToLower().EndsWith(".bmp") )
					filename += ".bmp";
				Emit ("save(TTQString)", (TTQString) filename);
			}
		}

		public void SlotAboutQtSharp ()
		{
			TTQMessageBox.Information (this, "About Qt# 0.7",
											"A Qt (http://www.trolltech.com) to C# language binding. \n" +
											"Qt# is compatible with Mono (http://go-mono.org) and\n" +
											"Portable.NET (http://www.southern-storm.com.au/portable_net.html)\n" +
											"(c) 2002 Adam Treat. Licensed under the terms of the GNU GPL.\n"
											);
		}

		public void SlotAboutQt ()
		{
			TTQMessageBox.AboutQt (this, "About Qt");
		}

		public void SlotColorChooser ()
		{
			TTQColor chosenColor = TQColorDialog.GetColor();
			if (chosenColor.IsValid())
				Emit ("colorChanged(TTQColor)", chosenColor);
		}

		public class ScribbleArea : TTQFrame {
			private TTQPoint last;
			private TTQPixmap buffer;
			private TTQColor currentcolor = new TTQColor("Black");
			private TTQPopupMenu popupmenu;

			public ScribbleArea (TTQWidget parent) : base (parent)
			{
				buffer = new TTQPixmap ();
				last = new TTQPoint ();
				SetBackgroundMode (Qt.BackgroundMode.NoBackground);

				popupmenu = new TTQPopupMenu();
				popupmenu.InsertItem ("&Clear", this, TQT_SLOT ("SlotClearArea()") );

				mouseMoveEvent += new MouseMoveEvent (MouseMoved);
				mousePressEvent += new MousePressEvent (MousePressed);
				paintEvent += new PaintEvent (PerformPaint);
				resizeEvent += new ResizeEvent (PerformResize);
			}

			public void PerformLoad (TTQString filename)
			{
 				if ( ! buffer.Load(filename)  )
 					TTQMessageBox.Warning (null, "Load error", "Could not load file");
 				Repaint();
			}

			public void PerformSave (TTQString filename)
			{
				if ( ! buffer.Save (filename, "BMP") )
					TTQMessageBox.Warning( null, "Save error", "Could not save file");
			}

			public void SlotClearArea ()
			{
				buffer.Fill( new TTQColor ("white") );
				BitBlt (this, 0, 0, buffer, 0, 0, -1, -1, Qt.RasterOp.CopyROP, false);
			}


			public void SlotSetColor (TTQColor color)
			{
				currentcolor = color;
			}

			// Note, Dispose() is called on TQPoints here to increase performance
			// of the UI. Otherwise, the GC would let dead TTQPoint instances pile
			// up and delete them all at once, causing the UI to pause while it frees
			// memory. (This happens because the GC runs in the same thread as the
			// application.)

			protected void MousePressed (TTQMouseEvent e)
			{
				if (e.Button() == ButtonState.RightButton )
					popupmenu.Exec (TTQCursor.Pos ());
				else {
					last.Dispose ();
					last = e.Pos();
				}
			}

			protected void MouseMoved (TTQMouseEvent e)
			{
				TTQPainter windowPainter = new TTQPainter ();
				TTQPainter bufferPainter = new TTQPainter ();

				windowPainter.Begin (this);
				bufferPainter.Begin (buffer);

				windowPainter.SetPen (currentcolor);
				bufferPainter.SetPen (currentcolor);

				windowPainter.DrawLine (last, e.Pos());
				bufferPainter.DrawLine (last, e.Pos());

				windowPainter.End ();
				bufferPainter.End ();

				last.Dispose ();
 				last = e.Pos ();
			}

			protected void PerformPaint (TTQPaintEvent e)
			{
				BitBlt(this, 0, 0, buffer,
					0, 0, -1, -1, RasterOp.CopyROP, false);
			}

			protected void PerformResize (TTQResizeEvent e)
			{
				TTQPixmap save = new TTQPixmap (buffer);
				buffer.Resize (e.Size());
				buffer.Fill (new TTQColor("white"));
				BitBlt (buffer, 0, 0, save,
					0, 0, -1, -1, RasterOp.CopyROP, false);
			}
		}
	}
}