widgetproxy_windows.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2025 The tk9.0-go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tk9_0 // import "modernc.org/tk9.0"
  5. import (
  6. "fmt"
  7. "unsafe"
  8. "golang.org/x/sys/windows"
  9. )
  10. func (proxy *widgetProxy) eventDispatcher(clientData, in uintptr, argc int32, argv uintptr) uintptr {
  11. // Expect at least arguments for the path and the operation.
  12. if argc < 2 {
  13. setResult(fmt.Sprintf("WidgetProxy eventDispatcher internal error: argc=%v", argc))
  14. return tcl_error
  15. }
  16. argsPointers := unsafe.Slice((*uintptr)(unsafe.Pointer(argv)), argc)
  17. argsPointers = argsPointers[1:] // skip path
  18. args := make([]string, len(argsPointers))
  19. for i := 0; i < len(argsPointers); i++ {
  20. args[i] = goString(argsPointers[i])
  21. }
  22. operation := args[0]
  23. if callback, ok := proxy.operations[operation]; ok {
  24. // Dispatch to the operation's registered callback.
  25. callback(args)
  26. } else {
  27. // Process as normal.
  28. proxy.EvalWrapped(args)
  29. }
  30. return tcl_ok
  31. }
  32. // Create a new Tcl command whose name is the widget's pathname, and
  33. // whose action is to dispatch on the operation passed to the widget:
  34. func (proxy *widgetProxy) registerEventDispatcher() {
  35. runCmdProxy := windows.NewCallback(proxy.eventDispatcher)
  36. if proxy.commandName, Error = cString(proxy.window.String()); Error != nil {
  37. return
  38. }
  39. cmd, _, _ := createCommandProc.Call(interp, proxy.commandName, runCmdProxy, 0, 0)
  40. if cmd == 0 {
  41. fail(fmt.Errorf("registering widget proxy event dispatcher proxy failed: %v", getObjResultProc))
  42. return
  43. }
  44. }
  45. func (proxy *widgetProxy) unregisterEventDispatcher() {
  46. defer func() {
  47. allocator.UintptrFree(proxy.commandName)
  48. proxy.commandName = 0 // nil
  49. }()
  50. _, _, _ = deleteCommandProc.Call(interp, proxy.commandName)
  51. }