dialog.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2018 visualfc. All rights reserved.
  2. package tk
  3. import (
  4. "fmt"
  5. )
  6. //tk_chooseColor — pops up a dialog box for the user to select a color.
  7. func ChooseColor(parent Widget, title string, initcolor string) (string, error) {
  8. script := fmt.Sprintf("tk_chooseColor")
  9. if parent != nil {
  10. script += " -parent " + parent.Id()
  11. }
  12. if initcolor != "" {
  13. script += " -initialcolor " + initcolor
  14. }
  15. if title != "" {
  16. setObjText("atk_tmp_title", title)
  17. script += " -title $atk_tmp_title"
  18. }
  19. return evalAsString(script)
  20. }
  21. //tk_chooseDirectory — pops up a dialog box for the user to select a directory.
  22. func ChooseDirectory(parent Widget, title string, initialdir string, mustexist bool) (string, error) {
  23. script := fmt.Sprintf("tk_chooseDirectory")
  24. if parent != nil {
  25. script += " -parent " + parent.Id()
  26. }
  27. if initialdir != "" {
  28. setObjText("atk_tmp_initialdir", initialdir)
  29. script += " -initialdir $atk_tmp_initialdir"
  30. }
  31. if mustexist {
  32. script += " -mustexist true"
  33. }
  34. if title != "" {
  35. setObjText("atk_tmp_title", title)
  36. script += " -title $atk_tmp_title"
  37. }
  38. return evalAsString(script)
  39. }
  40. type FileType struct {
  41. Info string
  42. Ext string
  43. }
  44. func (v FileType) String() string {
  45. return fmt.Sprintf("{%v} {%v}", v.Info, v.Ext)
  46. }
  47. //tk_getOpenFile, tk_getSaveFile — pop up a dialog box for the user to select a file to open or save.
  48. func GetOpenFile(parent Widget, title string, filetypes []FileType, initialdir string, initialfile string) (string, error) {
  49. script := fmt.Sprintf("tk_getOpenFile")
  50. if parent != nil {
  51. script += " -parent " + parent.Id()
  52. }
  53. if filetypes != nil {
  54. var info []string
  55. for _, v := range filetypes {
  56. info = append(info, v.String())
  57. }
  58. setObjTextList("atk_tmp_filetypes", info)
  59. script += " -filetypes $atk_tmp_filetypes"
  60. }
  61. if initialdir != "" {
  62. setObjText("atk_tmp_initialdir", initialdir)
  63. script += " -initialdir $atk_tmp_initialdir"
  64. }
  65. if initialfile != "" {
  66. setObjText("atk_tmp_initialfile", initialfile)
  67. script += " -initialfile $atk_tmp_initialfile"
  68. }
  69. if title != "" {
  70. setObjText("atk_tmp_title", title)
  71. script += " -title $atk_tmp_title"
  72. }
  73. return evalAsString(script)
  74. }
  75. func GetOpenMultipleFile(parent Widget, title string, filetypes []FileType, initialdir string, initialfile string) ([]string, error) {
  76. script := fmt.Sprintf("tk_getOpenFile")
  77. if parent != nil {
  78. script += " -parent " + parent.Id()
  79. }
  80. if filetypes != nil {
  81. var info []string
  82. for _, v := range filetypes {
  83. info = append(info, v.String())
  84. }
  85. setObjTextList("atk_tmp_filetypes", info)
  86. script += " -filetypes $atk_tmp_filetypes"
  87. }
  88. if initialdir != "" {
  89. setObjText("atk_tmp_initialdir", initialdir)
  90. script += " -initialdir $atk_tmp_initialdir"
  91. }
  92. if initialfile != "" {
  93. setObjText("atk_tmp_initialfile", initialfile)
  94. script += " -initialfile $atk_tmp_initialfile"
  95. }
  96. if title != "" {
  97. setObjText("atk_tmp_title", title)
  98. script += " -title $atk_tmp_title"
  99. }
  100. script += " -multiple true"
  101. return evalAsStringList(script)
  102. }
  103. //tk_getOpenFile, tk_getSaveFile — pop up a dialog box for the user to select a file to open or save.
  104. func GetSaveFile(parent Widget, title string, confirmoverwrite bool, defaultextension string, filetypes []FileType, initialdir string, initialfile string) (string, error) {
  105. script := fmt.Sprintf("tk_getSaveFile")
  106. if parent != nil {
  107. script += " -parent " + parent.Id()
  108. }
  109. if mainInterp.SupportTk86() {
  110. script += " -confirmoverwrite " + fmt.Sprint(confirmoverwrite)
  111. }
  112. if defaultextension != "" {
  113. setObjText("atk_tmp_defaultextension", defaultextension)
  114. script += " -defaultextension $atk_tmp_defaultextension"
  115. }
  116. if filetypes != nil {
  117. var info []string
  118. for _, v := range filetypes {
  119. info = append(info, v.String())
  120. }
  121. setObjTextList("atk_tmp_filetypes", info)
  122. script += " -filetypes $atk_tmp_filetypes"
  123. }
  124. if initialdir != "" {
  125. setObjText("atk_tmp_initialdir", initialdir)
  126. script += " -initialdir $atk_tmp_initialdir"
  127. }
  128. if initialfile != "" {
  129. setObjText("atk_tmp_initialfile", initialfile)
  130. script += " -initialfile $atk_tmp_initialfile"
  131. }
  132. if title != "" {
  133. setObjText("atk_tmp_title", title)
  134. script += " -title $atk_tmp_title"
  135. }
  136. return evalAsString(script)
  137. }
  138. type MessageBoxIcon int
  139. const (
  140. MessageBoxIconNone MessageBoxIcon = iota
  141. MessageBoxIconError
  142. MessageBoxIconInfo
  143. MessageBoxIconQuestion
  144. MessageBoxIconWarning
  145. )
  146. var (
  147. messageBoxIconName = []string{"", "error", "info", "question", "warning"}
  148. )
  149. func (v MessageBoxIcon) String() string {
  150. if v >= 0 && int(v) < len(messageBoxIconName) {
  151. return messageBoxIconName[v]
  152. }
  153. return ""
  154. }
  155. type MessageBoxType int
  156. const (
  157. MessageBoxTypeOk MessageBoxType = iota
  158. MessageBoxTypeOkCancel
  159. MessageBoxTypeAbortRetryIgnore
  160. MessageBoxTypeRetryCancel
  161. MessageBoxTypeYesNo
  162. MessageBoxTypeYesNoCancel
  163. )
  164. var (
  165. messageBoxTypeName = []string{"ok", "okcancel", "abortretryignore", "retrycancel", "yesno", "yesnocancel"}
  166. )
  167. func (v MessageBoxType) String() string {
  168. if v >= 0 && int(v) < len(messageBoxTypeName) {
  169. return messageBoxTypeName[v]
  170. }
  171. return ""
  172. }
  173. //tk_messageBox — pops up a message window and waits for user response.
  174. func MessageBox(parent Widget, title string, message string, detail string, defaultbutton string, icon MessageBoxIcon, typ MessageBoxType) (string, error) {
  175. script := fmt.Sprintf("tk_messageBox")
  176. if parent != nil {
  177. script += " -parent " + parent.Id()
  178. }
  179. if defaultbutton != "" {
  180. setObjText("atk_tmp_defaultbutton", defaultbutton)
  181. script += " -default $atk_tmp_defaultbutton"
  182. }
  183. if message != "" {
  184. setObjText("atk_tmp_message", message)
  185. script += " -message $atk_tmp_message"
  186. }
  187. sicon := icon.String()
  188. if sicon != "" {
  189. script += " -icon " + sicon
  190. }
  191. styp := typ.String()
  192. if styp != "" {
  193. script += " -type " + styp
  194. }
  195. if detail != "" {
  196. setObjText("atk_tmp_detail", detail)
  197. script += " -detail $atk_tmp_detail"
  198. }
  199. if title != "" {
  200. setObjText("atk_tmp_title", title)
  201. script += " -title $atk_tmp_title"
  202. }
  203. return evalAsString(script)
  204. }