actions.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2013 The Walk 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 main
  5. import (
  6. "log"
  7. )
  8. import (
  9. "github.com/lxn/walk"
  10. . "github.com/lxn/walk/declarative"
  11. )
  12. var isSpecialMode = walk.NewMutableCondition()
  13. type MyMainWindow struct {
  14. *walk.MainWindow
  15. }
  16. func main() {
  17. MustRegisterCondition("isSpecialMode", isSpecialMode)
  18. mw := new(MyMainWindow)
  19. var openAction, showAboutBoxAction *walk.Action
  20. var recentMenu *walk.Menu
  21. var toggleSpecialModePB *walk.PushButton
  22. if err := (MainWindow{
  23. AssignTo: &mw.MainWindow,
  24. Title: "Walk Actions Example",
  25. MenuItems: []MenuItem{
  26. Menu{
  27. Text: "&File",
  28. Items: []MenuItem{
  29. Action{
  30. AssignTo: &openAction,
  31. Text: "&Open",
  32. Image: "../img/open.png",
  33. Enabled: Bind("enabledCB.Checked"),
  34. Visible: Bind("openVisibleCB.Checked"),
  35. Shortcut: Shortcut{walk.ModControl, walk.KeyO},
  36. OnTriggered: mw.openAction_Triggered,
  37. },
  38. Menu{
  39. AssignTo: &recentMenu,
  40. Text: "Recent",
  41. },
  42. Separator{},
  43. Action{
  44. Text: "E&xit",
  45. OnTriggered: func() { mw.Close() },
  46. },
  47. },
  48. },
  49. Menu{
  50. Text: "&Help",
  51. Items: []MenuItem{
  52. Action{
  53. AssignTo: &showAboutBoxAction,
  54. Text: "About",
  55. OnTriggered: mw.showAboutBoxAction_Triggered,
  56. },
  57. },
  58. },
  59. },
  60. ToolBar: ToolBar{
  61. ButtonStyle: ToolBarButtonImageBeforeText,
  62. Items: []MenuItem{
  63. ActionRef{&openAction},
  64. Menu{
  65. Text: "New A",
  66. Image: "../img/document-new.png",
  67. Items: []MenuItem{
  68. Action{
  69. Text: "A",
  70. OnTriggered: mw.newAction_Triggered,
  71. },
  72. Action{
  73. Text: "B",
  74. OnTriggered: mw.newAction_Triggered,
  75. },
  76. Action{
  77. Text: "C",
  78. OnTriggered: mw.newAction_Triggered,
  79. },
  80. },
  81. OnTriggered: mw.newAction_Triggered,
  82. },
  83. Separator{},
  84. Menu{
  85. Text: "View",
  86. Image: "../img/document-properties.png",
  87. Items: []MenuItem{
  88. Action{
  89. Text: "X",
  90. OnTriggered: mw.changeViewAction_Triggered,
  91. },
  92. Action{
  93. Text: "Y",
  94. OnTriggered: mw.changeViewAction_Triggered,
  95. },
  96. Action{
  97. Text: "Z",
  98. OnTriggered: mw.changeViewAction_Triggered,
  99. },
  100. },
  101. },
  102. Separator{},
  103. Action{
  104. Text: "Special",
  105. Image: "../img/system-shutdown.png",
  106. Enabled: Bind("isSpecialMode && enabledCB.Checked"),
  107. OnTriggered: mw.specialAction_Triggered,
  108. },
  109. },
  110. },
  111. ContextMenuItems: []MenuItem{
  112. ActionRef{&showAboutBoxAction},
  113. },
  114. MinSize: Size{300, 200},
  115. Layout: VBox{},
  116. Children: []Widget{
  117. CheckBox{
  118. Name: "enabledCB",
  119. Text: "Open / Special Enabled",
  120. Checked: true,
  121. },
  122. CheckBox{
  123. Name: "openVisibleCB",
  124. Text: "Open Visible",
  125. Checked: true,
  126. },
  127. PushButton{
  128. AssignTo: &toggleSpecialModePB,
  129. Text: "Enable Special Mode",
  130. OnClicked: func() {
  131. isSpecialMode.SetSatisfied(!isSpecialMode.Satisfied())
  132. if isSpecialMode.Satisfied() {
  133. toggleSpecialModePB.SetText("Disable Special Mode")
  134. } else {
  135. toggleSpecialModePB.SetText("Enable Special Mode")
  136. }
  137. },
  138. },
  139. },
  140. }.Create()); err != nil {
  141. log.Fatal(err)
  142. }
  143. addRecentFileActions := func(texts ...string) {
  144. for _, text := range texts {
  145. a := walk.NewAction()
  146. a.SetText(text)
  147. a.Triggered().Attach(mw.openAction_Triggered)
  148. recentMenu.Actions().Add(a)
  149. }
  150. }
  151. addRecentFileActions("Foo", "Bar", "Baz")
  152. mw.Run()
  153. }
  154. func (mw *MyMainWindow) openAction_Triggered() {
  155. walk.MsgBox(mw, "Open", "Pretend to open a file...", walk.MsgBoxIconInformation)
  156. }
  157. func (mw *MyMainWindow) newAction_Triggered() {
  158. walk.MsgBox(mw, "New", "Newing something up... or not.", walk.MsgBoxIconInformation)
  159. }
  160. func (mw *MyMainWindow) changeViewAction_Triggered() {
  161. walk.MsgBox(mw, "Change View", "By now you may have guessed it. Nothing changed.", walk.MsgBoxIconInformation)
  162. }
  163. func (mw *MyMainWindow) showAboutBoxAction_Triggered() {
  164. walk.MsgBox(mw, "About", "Walk Actions Example", walk.MsgBoxIconInformation)
  165. }
  166. func (mw *MyMainWindow) specialAction_Triggered() {
  167. walk.MsgBox(mw, "Special", "Nothing to see here.", walk.MsgBoxIconInformation)
  168. }