khchen / wNim

Nim's Windows GUI Framework
MIT License
327 stars 17 forks source link

How to disable drag and drop of controls #53

Closed EasiestSoft closed 4 years ago

EasiestSoft commented 4 years ago

In a multi-threaded program, I want to revoke the drag and drop of the control when entering a thread and defer: ctrl.setDropTarget() to enable it again

khchen commented 4 years ago

setDropTarget() can only works in the thread that creates the window. However, you can send a message to inform the window of what to do, for example:

import threadpool, strutils, os
import wNim/[wApp, wFrame, wButton, wTextCtrl, wDataObject]

const
  wEvent_DisableDragDrop = wEvent_App + 1
  wEvent_EnableDragDrop = wEvent_App + 2

var
  app = App()
  frame = Frame(title="Enable Drag and Drop")
  textctrl = TextCtrl(frame, value="drop something here...", style=wTeMultiLine)
  button = Button(frame, label="Disable 10 Seconds")

frame.wEvent_DragEnter do (event: wEvent):
  if event.dataObject.isText():
    event.effect = wDragCopy

frame.wEvent_Drop do (event: wEvent):
  if event.dataObject.isText():
    textctrl.value = event.dataObject.text.replace("\n", "\r\n")

  else:
    event.effect = wDragNone

frame.wEvent_DisableDragDrop do ():
  frame.setDropTarget(false)
  frame.title = "Disable Drag and Drop"

frame.wEvent_EnableDragDrop do ():
  frame.setDropTarget(true)
  frame.title = "Enable Drag and Drop"

frame.wEvent_Size do ():
  frame.autolayout """
    H: |[textctrl,button]|
    V: |[textctrl][button(30)]|
  """

proc thread(win: wWindow) {.thread.} =
  win.queueMessage(wEvent_DisableDragDrop, 0, 0)
  for i in 1..10:
    os.sleep(1000)
  win.queueMessage(wEvent_EnableDragDrop, 0, 0)

button.wEvent_Button do ():
  spawn thread(frame)

frame.setDropTarget(true)
frame.center()
frame.show()
app.mainLoop()
EasiestSoft commented 4 years ago

You are great

khchen commented 4 years ago

For communication between window in different threads, I posted more example at wNim's wiki page. https://github.com/khchen/wNim/wiki/Create-and-Communicate-Window-in-Child-Thread