00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 using System;
00014 using System.Drawing;
00015 using System.Runtime.InteropServices;
00016
00017 using wx;
00018
00022 namespace wx.SampleDnd
00023 {
00024 public class DNDText : TextDropTarget
00025 {
00026 private ListBox myOwner;
00027
00028
00029
00030 public DNDText(ListBox myOwner)
00031 : base()
00032 {
00033 this.myOwner = myOwner;
00034 }
00035
00036
00037
00038 public override bool OnDropText(int x, int y, string text)
00039 {
00040 myOwner.Append(text);
00041 return true;
00042 }
00043 }
00044
00045
00046
00047 public class DNDFile : FileDropTarget
00048 {
00049 private ListBox myOwner;
00050
00051
00052
00053 public DNDFile(ListBox myOwner)
00054 : base()
00055 {
00056 this.myOwner = myOwner;
00057 }
00058
00059
00060
00061 public override bool OnDropFiles(int x, int y, string[] filenames)
00062 {
00063 string str = filenames.Length + " files dropped";
00064 myOwner.Append(str);
00065
00066 for (int i = 0; i < filenames.Length; i++)
00067 {
00068 myOwner.Append(filenames[i]);
00069 }
00070
00071 return true;
00072 }
00073 }
00074
00075
00076
00077 public class DndFrame : Frame
00078 {
00079 enum Cmd
00080 {
00081 Menu_Drag, Menu_DragMoveDef, Menu_DragMoveAllow, Menu_About, Menu_Quit,
00082 Menu_Clear, Menu_Help, Menu_Copy, Menu_Paste
00083 }
00084
00085 private ListBox m_ctrlText = null;
00086 private ListBox m_ctrlFile = null;
00087
00088 private TextCtrl m_ctrlLog = null;
00089
00090 private bool m_moveByDefault = false;
00091 private bool m_moveAllow = true;
00092
00093 public string m_strText;
00094
00095
00096
00097 public DndFrame(string title, Point pos, Size size)
00098 : base(title, pos, size)
00099 {
00100
00101
00102 Icon = new wx.Icon("../Samples/Dnd/mondrian.png");
00103
00104 CreateStatusBar();
00105 StatusText = "Welcome to the Dnd Sample!";
00106
00107
00108
00109 Menu fileMenu = new Menu();
00110 fileMenu.Append((int)Cmd.Menu_Drag, "&Test drag...");
00111 fileMenu.AppendCheckItem((int)Cmd.Menu_DragMoveDef, "&Move by default", "");
00112 fileMenu.AppendCheckItem((int)Cmd.Menu_DragMoveAllow, "&Allow moving", "");
00113 fileMenu.AppendSeparator();
00114 fileMenu.Append((int)Cmd.Menu_Quit, "E&xit\tCtrl-Q");
00115
00116 Menu logMenu = new Menu();
00117 logMenu.Append((int)Cmd.Menu_Clear, "Clear\tCtrl-L");
00118
00119 Menu helpMenu = new Menu();
00120 helpMenu.Append((int)Cmd.Menu_Help, "&Help");
00121 helpMenu.AppendSeparator();
00122 helpMenu.Append((int)Cmd.Menu_About, "&About");
00123
00124 Menu clipMenu = new Menu();
00125 clipMenu.Append((int)Cmd.Menu_Copy, "&Copy text\tCtrl-C");
00126 clipMenu.Append((int)Cmd.Menu_Paste, "&Paste text\tCtrl-V");
00127
00128 wx.MenuBar menuBar = new wx.MenuBar();
00129 menuBar.Append(fileMenu, "&File");
00130 menuBar.Append(logMenu, "&Log");
00131 menuBar.Append(clipMenu, "&Clipboard");
00132 menuBar.Append(helpMenu, "&Help");
00133
00134 MenuBar = menuBar;
00135
00136 Size asize = new Size(300, 200);
00137 Size bsize = new Size(600, 100);
00138
00139 string[] strFile = { "Drop files here!" };
00140 string[] strText = { "Drop text on me" };
00141
00142 m_ctrlFile = new ListBox(this, -1, wxDefaultPosition, asize, strFile, wx.WindowStyles.LB_HSCROLL | wx.WindowStyles.LB_ALWAYS_SB);
00143
00144 m_ctrlText = new ListBox(this, -1, wxDefaultPosition, asize, strText, wx.WindowStyles.LB_HSCROLL | wx.WindowStyles.LB_ALWAYS_SB);
00145
00146 m_ctrlLog = new TextCtrl(this, -1, "", wxDefaultPosition, bsize, wx.WindowStyles.TE_MULTILINE | wx.WindowStyles.TE_READONLY | wx.WindowStyles.BORDER_SUNKEN);
00147
00148 Log.SetActiveTarget(m_ctrlLog);
00149 Log.AddTraceMask("focus");
00150
00151 m_ctrlFile.DropTarget = new DNDFile(m_ctrlFile);
00152 m_ctrlText.DropTarget = new DNDText(m_ctrlText);
00153
00154 BoxSizer main_sizer = new BoxSizer(Orientation.wxVERTICAL);
00155 BoxSizer h_sizer = new BoxSizer(Orientation.wxHORIZONTAL);
00156
00157 h_sizer.Add(m_ctrlFile, 1, wx.SizerFlag.wxALL, 5);
00158 h_sizer.Add(m_ctrlText, 1, wx.SizerFlag.wxALL, 5);
00159 main_sizer.Add(h_sizer, 0, wx.SizerFlag.wxALL, 0);
00160 main_sizer.Add(m_ctrlLog, 1, wx.SizerFlag.wxALL, 5);
00161 main_sizer.Add(new BoxSizer(Orientation.wxHORIZONTAL), 1, wx.SizerFlag.wxALL, 5);
00162
00163 AutoLayout = true;
00164 SetSizer(main_sizer, true);
00165
00166 main_sizer.Fit(this);
00167 main_sizer.SetSizeHints(this);
00168
00169 menuBar.Check((int)Cmd.Menu_DragMoveAllow, true);
00170
00171
00172
00173 EVT_MENU((int)Cmd.Menu_Quit, new EventListener(OnQuit));
00174 EVT_MENU((int)Cmd.Menu_About, new EventListener(OnAbout));
00175 EVT_MENU((int)Cmd.Menu_Drag, new EventListener(OnDrag));
00176 EVT_MENU((int)Cmd.Menu_DragMoveDef, new EventListener(OnDragMoveByDefault));
00177 EVT_MENU((int)Cmd.Menu_DragMoveAllow, new EventListener(OnDragMoveAllow));
00178 EVT_MENU((int)Cmd.Menu_Help, new EventListener(OnHelp));
00179 EVT_MENU((int)Cmd.Menu_Clear, new EventListener(OnLogClear));
00180 EVT_MENU((int)Cmd.Menu_Copy, new EventListener(OnCopy));
00181 EVT_MENU((int)Cmd.Menu_Paste, new EventListener(OnPaste));
00182
00183 EVT_SIZE(new EventListener(OnSize));
00184
00185 EVT_PAINT(new EventListener(OnPaint));
00186
00187 EVT_UPDATE_UI((int)Cmd.Menu_DragMoveDef, new EventListener(OnUpdateUIMoveByDefault));
00188 EVT_UPDATE_UI((int)Cmd.Menu_Paste, new EventListener(OnUpdateUIPasteText));
00189
00190 EVT_LEFT_DOWN(new EventListener(OnLeftDown));
00191 EVT_RIGHT_DOWN(new EventListener(OnRightDown));
00192
00193 Log.LogMessage("DnD sample started..");
00194
00195 m_strText = "wx.NET drag & drop works :-)";
00196 }
00197
00198
00199
00200 public void OnQuit(object sender, wx.Event e)
00201 {
00202 Close();
00203 }
00204
00205
00206
00207
00208 public void OnAbout(object sender, wx.Event e)
00209 {
00210 string msg = "This is the About dialog of the dnd sample.";
00211 wx.MessageDialog.ShowModal(this, msg, "About Dnd", wx.WindowStyles.DIALOG_OK | wx.WindowStyles.ICON_INFORMATION);
00212 }
00213
00214
00215
00216 public void OnSize(object sender, Event e)
00217 {
00218 Refresh();
00219
00220 e.Skip();
00221 }
00222
00223
00224
00225 public void OnPaint(object sender, Event e)
00226 {
00227 Size cs = ClientSize;
00228
00229 PaintDC dc = new PaintDC(this);
00230
00231 dc.Font = new Font(24, FontFamily.wxDECORATIVE, FontStyle.wxNORMAL, FontWeight.wxNORMAL, false, "charter");
00232 dc.DrawText("Drag text from here!", 100, cs.Height - 50);
00233
00234 dc.Dispose();
00235 }
00236
00237
00238
00239 public void OnUpdateUIMoveByDefault(object sender, Event e)
00240 {
00241 UpdateUIEvent ue = (UpdateUIEvent)e;
00242 ue.Enabled = m_moveAllow;
00243 }
00244
00245
00246
00247 public void OnUpdateUIPasteText(object sender, Event e)
00248 {
00249 UpdateUIEvent ue = (UpdateUIEvent)e;
00250 ue.Enabled = CTheClipboard.TheClipboard.IsSupported(new DataFormat(DataFormatId.wxDF_TEXT));
00251 }
00252
00253
00254
00255 public void OnDrag(object sender, Event e)
00256 {
00257 string strText = new GetTextFromUser(
00258 "After you enter text in this dialog, press any mouse\n" +
00259 "button in the bottom (empty) part of the frame and \n" +
00260 "drag it anywhere - you will be in fact dragging the\n" +
00261 "text object containing this text",
00262 "Please enter some text", m_strText, this);
00263
00264 if (strText.Length > 0)
00265 {
00266 m_strText = null;
00267 m_strText = strText;
00268 }
00269 }
00270
00271
00272
00273 public void OnDragMoveByDefault(object sender, Event e)
00274 {
00275 CommandEvent ce = (CommandEvent)e;
00276 m_moveByDefault = ce.IsChecked;
00277 }
00278
00279
00280
00281 public void OnDragMoveAllow(object sender, Event e)
00282 {
00283 CommandEvent ce = (CommandEvent)e;
00284 m_moveAllow = ce.IsChecked;
00285 }
00286
00287
00288
00289 public void OnHelp(object sender, Event e)
00290 {
00291 MessageDialog md = new MessageDialog(this,
00292 "This small program demonstrates drag & drop support in wx.NET. The program window\n" +
00293 "consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n" +
00294 "going on inside. The top part is split into 2 listboxes, the left one accepts files\n" +
00295 "and the right one accepts text.\n" +
00296 "\n" +
00297 "To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n" +
00298 "the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n" +
00299 "Also, try dragging some files (you can select several at once) from Windows Explorer (or \n" +
00300 "File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n" +
00301 "work with files) and see what changes.\n" +
00302 "\n" +
00303 "To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n" +
00304 "it to wordpad or any other droptarget accepting text (and of course you can just drag it\n" +
00305 "to the right pane). Due to a lot of trace messages, the cursor might take some time to \n" +
00306 "change, don't release the mouse button until it does. You can change the string being\n" +
00307 "dragged in in \"File|Test drag...\" dialog.\n",
00308 "wxDnD Help");
00309
00310 md.ShowModal();
00311 }
00312
00313
00314
00315 public void OnLogClear(object sender, Event e)
00316 {
00317 m_ctrlLog.Clear();
00318 m_ctrlText.Clear();
00319 m_ctrlFile.Clear();
00320 }
00321
00322
00323
00324 public void OnLeftDown(object sender, Event e)
00325 {
00326 if (m_strText.Length > 0)
00327 {
00328 TextDataObject textData = new TextDataObject(m_strText);
00329
00330 if (textData == null)
00331 {
00332 return;
00333 }
00334
00335 DropSource source = new DropSource(textData, this);
00336
00337 Drag flags = Drag.wxDrag_CopyOnly;
00338 if (m_moveByDefault)
00339 flags |= Drag.wxDrag_DefaultMove;
00340 else if (m_moveAllow)
00341 flags |= Drag.wxDrag_AllowMove;
00342
00343 string result = "";
00344
00345 switch (source.DoDragDrop(flags))
00346 {
00347 case DragResult.wxDragError: result = "Error!"; break;
00348 case DragResult.wxDragNone: result = "Nothing"; break;
00349 case DragResult.wxDragCopy: result = "Copied"; break;
00350 case DragResult.wxDragMove: result = "Moved"; break;
00351 case DragResult.wxDragCancel: result = "Cancelled"; break;
00352 default: result = "Huh?"; break;
00353 }
00354
00355 StatusText = "Drag result: " + result;
00356 }
00357 }
00358
00359
00360
00361 public void OnRightDown(object sender, Event e)
00362 {
00363 MouseEvent me = (MouseEvent)e;
00364
00365 Menu menu = new Menu("Dnd sample menu");
00366
00367 menu.Append((int)Cmd.Menu_Drag, "&Test drag");
00368 menu.AppendSeparator();
00369 menu.Append((int)Cmd.Menu_About, "&About");
00370
00371 PopupMenu(menu, me.Position);
00372 }
00373
00374
00375
00376 public void OnCopy(object sender, Event e)
00377 {
00378 if (!CTheClipboard.TheClipboard.Open())
00379 {
00380 Log.LogError("Can't open clipboard.");
00381 return;
00382 }
00383
00384 if (!CTheClipboard.TheClipboard.AddData(new TextDataObject(m_strText)))
00385 {
00386 Log.LogError("Can't copy data to the clipboard");
00387 }
00388 else
00389 {
00390 Log.LogMessage("Text '{0}' put on the clipboard", m_strText);
00391 }
00392
00393 CTheClipboard.TheClipboard.Close();
00394 }
00395
00396
00397
00398 public void OnPaste(object sender, Event e)
00399 {
00400 if (!CTheClipboard.TheClipboard.Open())
00401 {
00402 Log.LogError("Can't open clipboard.");
00403 return;
00404 }
00405
00406 if (!CTheClipboard.TheClipboard.IsSupported(new DataFormat(DataFormatId.wxDF_TEXT)))
00407 {
00408 Log.LogWarning("No text data on clipboard");
00409 CTheClipboard.TheClipboard.Close();
00410 return;
00411 }
00412
00413 TextDataObject text = new TextDataObject();
00414 if (!CTheClipboard.TheClipboard.GetData(text))
00415 {
00416 Log.LogError("Can't paste data from the clipboard");
00417 }
00418 else
00419 {
00420 Log.LogMessage("Text '{0}' pasted from the clipboard", text.Text);
00421 }
00422
00423 CTheClipboard.TheClipboard.Close();
00424 }
00425 }
00426
00427
00428
00429 public class Dnd : wx.App
00430 {
00431
00432
00433 public override bool OnInit()
00434 {
00435 DndFrame frame = new DndFrame("Dnd wxWidgets App", new Point(10, 100), new Size(650, 340));
00436 frame.Show(true);
00437
00438 return true;
00439 }
00440
00441
00442
00443 [STAThread]
00444 static void Main()
00445 {
00446 Dnd app = new Dnd();
00447 app.Run();
00448 }
00449
00450
00451 }
00452 }