PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/toolbar.tcl

https://gitlab.com/OpenSourceMirror/tkbuilder
TCL | 450 lines | 315 code | 71 blank | 64 comment | 43 complexity | 4df32325a7e7e508aefe4680be55ff57 MD5 | raw file
  1. # -------------------------------------------------------------
  2. # toolbar.tcl
  3. # This file is part of the tkBuilder distribution
  4. # Copyright (C) 1999, 2000 Frank Schnekenburger
  5. # Distributed under the terms of the GNU General Public License
  6. #
  7. # Modified by andrasy on 10/22/06 for :
  8. # - tcl 8.4.13
  9. # - BWidget 1.7
  10. # - msgcat calls added for translation
  11. # - combobox from Bryan Oakley replaced by ::ComboBox of BWidget
  12. # -------------------------------------------------------------
  13. #
  14. # This file was built using tkBuilder
  15. #
  16. namespace eval Toolbar {
  17. variable _height_ 20
  18. variable _width_ 23
  19. # constrained dimensions of buttons
  20. variable _relation_ {}
  21. # current relation used for insertion; one of SIBLING or CHILD
  22. variable _butRelation_
  23. # array used for names of sibling and child buttons
  24. # indexed on SIBLING and CHILD
  25. variable _fraRelation_
  26. # array used for names of frames containing sibling and child buttons
  27. # indexed on SIBLING and CHILD
  28. variable _frame_
  29. # array used to register name of frames used for widgets, canvas objects, and menu entries
  30. # indexed on widget, canvas, menu, special
  31. variable _currentToolbarFrame_ widget
  32. # indicates which of 'widget' or 'special' is currently displayed
  33. # in the widget toolbar, to control toggling among widgets and special
  34. # objects
  35. variable _frameMainToolbar_
  36. # main frame to hold main toolbar
  37. variable _frameToolbar_
  38. # main frame to hold entire widget toolbar
  39. variable _frameMainPane_
  40. # frame above which the main toolbar is packed in the display
  41. variable _toolbar_ widget
  42. # current toolbar type: one of widget, special, canvas or menu
  43. variable _toolTip_ {} ; # name of toplevel window used for tool tip
  44. variable _toolTipActive_ 0 ; # 1 if delay not required before displaying tooltip
  45. variable _afterID_ ; # id of pending 'after' command for tool tip
  46. variable _background_
  47. # background colour needed for relation buttons
  48. proc CreateMainToolbar { fraMainToolbar fraMainPane } {
  49. variable _frameMainToolbar_ $fraMainToolbar
  50. variable _frameMainPane_ $fraMainPane ; # remember frame to pack before
  51. variable _height_
  52. variable _width_
  53. variable _butGeometry_
  54. # ensure tool tip is deactivated after leaving the toolbar, to
  55. # force the delay next time we enter
  56. bind $_frameMainToolbar_ <Leave> "set Toolbar::_toolTipActive_ 0"
  57. # bindings on icon button binding tag
  58. bind IconButton <ButtonPress-1> "focus %W; Toolbar::HideToolTip"
  59. bind IconButton <Leave> "%W configure -relief flat; Toolbar::HideToolTip"
  60. # make frame for separator line
  61. set fraSeparator [frame $fraMainToolbar.fraSeparator -bd 3 -height 2 -relief sunken]
  62. pack $fraSeparator -side top -anchor nw -fill x -pady 1
  63. # make frame to hold icons
  64. set fraIcons [frame $fraMainToolbar.fraIcons -bd 0]
  65. pack $fraIcons -side top -anchor nw
  66. foreach { icon tooltip command } {
  67. open "[mc {Open Project}]" File::OpenFile
  68. save "[mc {Save Project}]" File::SaveFile
  69. saveTcl "[mc {Save Tcl Files}]" Project::SaveProjectTclFiles
  70. separator {} {} } {
  71. if { $icon == "separator" } {
  72. AddSeparator $fraIcons left
  73. } else {
  74. AddIcon $fraIcons left $icon $tooltip $command
  75. }
  76. }
  77. foreach man {pack grid place} {
  78. # make confining frame for button
  79. set fraBut [frame $fraIcons.fra_$man -height $_height_ -width $_width_]
  80. pack $fraBut -side left
  81. pack propagate $fraBut 0
  82. set b [button $fraBut.button -image [Icon::GetIconImage $man] \
  83. -relief flat -bd 1 -takefocus 0]
  84. pack $b
  85. bind $b <ButtonPress-1> "Toolbar::HideToolTip
  86. Option::SetOption geometryManager $man
  87. Toolbar::UpdateGeometryManager"
  88. set _butGeometry_($man) $b
  89. }
  90. UpdateGeometryManager
  91. # 1.0.2a: 04/19/00: bug fix: added WidgetTree::CopyWidgetToSiblings and Descendants
  92. foreach { icon tooltip command } {
  93. separator {} {}
  94. duplicateWidget "[mc {Duplicate Widget}]" WidgetTree::DuplicateWidget
  95. duplicateBranch "[mc {Duplicate Branch}]" WidgetTree::DuplicateBranch
  96. copyToSiblings "[mc {Copy To Siblings}]" WidgetTree::CopyWidgetToSiblings
  97. copyToDescendants "[mc {Copy To Descendants}]" WidgetTree::CopyWidgetToDescendants
  98. deleteWidget "[mc {Delete Widget}]" WidgetTree::DeleteWidgetNode
  99. deleteBranch "[mc {Delete Branch}]" WidgetTree::DeleteWidgetBranch
  100. separator {} {}
  101. runProject "[mc {Run Project}]" "Build::BuildBranch RUN_PROJECT"
  102. runBranch "[mc {Run Branch}]" "Build::BuildBranch RUN_BRANCH"
  103. runLast "[mc {Run Last}]" "Build::BuildBranch RUN_LAST"
  104. stop "[mc {Stop}] " Build::StopApplication
  105. separator {} {}
  106. view "[mc {View Branch}]" "Build::BuildBranch VIEW_TCL" } {
  107. if { $icon == "separator" } {
  108. AddSeparator $fraIcons left
  109. } else {
  110. AddIcon $fraIcons left $icon $tooltip $command
  111. }
  112. }
  113. }
  114. proc ShowMainToolbar { } {
  115. variable _frameMainToolbar_
  116. variable _frameMainPane_
  117. if { [Option::GetOption mainToolbarShow] } {
  118. pack $_frameMainToolbar_ -side top -anchor nw -fill x -before $_frameMainPane_
  119. } else {
  120. pack forget $_frameMainToolbar_
  121. }
  122. }
  123. proc UpdateGeometryManager { } {
  124. variable _butGeometry_
  125. set geometryManager [Option::GetOption geometryManager]
  126. foreach { man tip } { pack Pack grid Grid place Place } {
  127. set b $_butGeometry_($man)
  128. if { $man == $geometryManager } {
  129. $_butGeometry_($man) configure -relief sunken
  130. bind $b <Enter> "Toolbar::ShowToolTip $tip \[winfo rootx %W] \[winfo rooty %W] "
  131. bind $b <Leave> "Toolbar::HideToolTip"
  132. } else {
  133. $_butGeometry_($man) configure -relief flat
  134. bind $b <Enter> "Toolbar::ShowToolTip $tip \[winfo rootx %W] \[winfo rooty %W]
  135. $_butGeometry_($man) configure -relief raised"
  136. bind $b <Leave> "Toolbar::HideToolTip
  137. $_butGeometry_($man) configure -relief flat"
  138. }
  139. }
  140. }
  141. proc CreateToolbar { fraToolbar fraTree } {
  142. variable _frameToolbar_ $fraToolbar
  143. variable _frameTree_ $fraTree ; # remember frame used to pack before
  144. variable _frame_
  145. variable _butRelation_
  146. variable _fraRelation_
  147. variable _height_
  148. variable _width_
  149. variable _background_
  150. # ensure tool tip is deactivated after leaving the toolbar, to
  151. # force the delay next time we enter
  152. bind $_frameToolbar_ <Leave> "set Toolbar::_toolTipActive_ 0"
  153. set _background_ [Utility::GetBackgroundColour]
  154. set _fraRelation_(SIBLING) [frame $fraToolbar.fraSibling -background red -height $_height_ -width $_width_]
  155. pack $fraToolbar.fraSibling -padx 1 -pady 1
  156. pack propagate $_fraRelation_(SIBLING) 0
  157. set _butRelation_(SIBLING) [button $fraToolbar.fraSibling.button -command {Toolbar::UpdateRelation SIBLING
  158. Toolbar::UpdateToolbar 0} -image "[Icon::GetIconImage sibling]" -takefocus 0]
  159. pack $fraToolbar.fraSibling.button -padx 1 -pady 1
  160. bind $fraToolbar.fraSibling.button <Enter> {Toolbar::ShowToolTip "[mc {Insert As Sibling}]" [winfo rootx %W] [winfo rooty %W]}
  161. bind $fraToolbar.fraSibling.button <Leave> Toolbar::HideToolTip
  162. set _fraRelation_(CHILD) [frame $fraToolbar.fraChild -background red -height $_height_ -width $_width_]
  163. pack $fraToolbar.fraChild -padx 1 -pady 1
  164. pack propagate $_fraRelation_(CHILD) 0
  165. set _butRelation_(CHILD) [button $fraToolbar.fraChild.button -command {Toolbar::UpdateRelation CHILD
  166. Toolbar::UpdateToolbar 0} -image "[Icon::GetIconImage child]" -takefocus 0]
  167. pack $fraToolbar.fraChild.button -padx 1 -pady 1
  168. bind $fraToolbar.fraChild.button <Enter> {Toolbar::ShowToolTip "[mc {Insert As Child}]" [winfo rootx %W] [winfo rooty %W]}
  169. bind $fraToolbar.fraChild.button <Leave> Toolbar::HideToolTip
  170. AddSeparator $fraToolbar top
  171. # make frame and buttons for widgets
  172. set _frame_(widget) [frame $fraToolbar.widgets -bd 0]
  173. # icon at top to switch to special objects
  174. AddIcon $_frame_(widget) top specialObjects "[mc {Special Objects}]" \
  175. { Toolbar::SwapToolbarFrames special }
  176. AddSeparator $_frame_(widget) top
  177. foreach widget [Widget::GetWidgetList] {
  178. AddIcon $_frame_(widget) top $widget $widget \
  179. "WidgetTree::AddWidget $widget \[Toolbar::GetRelation]"
  180. }
  181. pack $_frame_(widget)
  182. # make frame and buttons for special objects
  183. set _frame_(special) [frame $fraToolbar.special -bd 0]
  184. # icon at top to switch to widgets
  185. AddIcon $_frame_(special) top widgets "[mc {Widgets}]" \
  186. { Toolbar::SwapToolbarFrames widget }
  187. AddSeparator $_frame_(special) top
  188. foreach object [Special::GetSpecialObjectList] {
  189. AddIcon $_frame_(special) top $object $object \
  190. "WidgetTree::AddWidget $object \[Toolbar::GetRelation]"
  191. }
  192. # add icon to allow inserting main/root toplevel window
  193. AddSeparator $_frame_(special) top
  194. AddIcon $_frame_(special) top toplevel [Project::GetRootName] \
  195. "Special::AddSpecialObject \"[Project::GetRootName]\" \[Toolbar::GetRelation] "
  196. # make frame and buttons for canvas objects
  197. set _frame_(canvas) [frame $fraToolbar.canvas -bd 0]
  198. foreach object [Canvas::GetCanvasObjectList] {
  199. AddIcon $_frame_(canvas) top $object $object \
  200. "WidgetTree::AddWidget $object \[Toolbar::GetRelation]"
  201. }
  202. # make frame and buttons for menu entries
  203. set _frame_(menu) [frame $fraToolbar.menu -bd 0]
  204. foreach entry [MenuEntry::GetMenuEntryList] {
  205. AddIcon $_frame_(menu) top $entry $entry \
  206. "WidgetTree::AddWidget $entry \[Toolbar::GetRelation]"
  207. }
  208. }
  209. proc SwapToolbarFrames newFrame {
  210. variable _frame_
  211. variable _toolbar_
  212. variable _currentToolbarFrame_
  213. pack forget $_frame_($_currentToolbarFrame_)
  214. pack $_frame_($newFrame)
  215. set _currentToolbarFrame_ $newFrame
  216. set _toolbar_ $newFrame
  217. }
  218. proc ShowToolbar { } {
  219. variable _frameToolbar_
  220. variable _frameTree_
  221. if { [Option::GetOption widgetToolbarShow] } {
  222. pack $_frameToolbar_ -side left -anchor nw -before $_frameTree_
  223. } else {
  224. pack forget $_frameToolbar_
  225. }
  226. }
  227. proc UpdateRelation relation {
  228. variable _relation_
  229. variable _butRelation_
  230. variable _fraRelation_
  231. variable _background_
  232. if { $relation != $_relation_ } {
  233. set _relation_ $relation
  234. if { $_relation_ == "SIBLING" } {
  235. $_butRelation_(SIBLING) configure -relief sunken
  236. $_butRelation_(CHILD) configure -relief raised
  237. $_fraRelation_(SIBLING) configure -bg black
  238. $_fraRelation_(CHILD) configure -bg $_background_
  239. } elseif { $_relation_ == "CHILD" } {
  240. $_butRelation_(SIBLING) configure -relief raised
  241. $_butRelation_(CHILD) configure -relief sunken
  242. $_fraRelation_(SIBLING) configure -bg $_background_
  243. $_fraRelation_(CHILD) configure -bg black
  244. }
  245. }
  246. }
  247. proc UpdateToolbar { {tryDefaultRelation 1} } {
  248. variable _frame_
  249. variable _toolbar_
  250. variable _relation_
  251. variable _currentToolbarFrame_
  252. set currentNode [Tree::GetCurrentNode]
  253. set objectType [Tree::GetNodeItem widgetType $currentNode]
  254. if { [Canvas::WidgetIsCanvasObject $objectType] ||
  255. [MenuEntry::WidgetIsMenuEntry $objectType] } {
  256. UpdateRelation SIBLING
  257. SetButtonState disabled
  258. } else {
  259. # generally, use the default relation if option is set, unless this
  260. # is called by the sib or child toolbar button, indicating user
  261. # may want to override default
  262. if { $tryDefaultRelation } {
  263. if { [Option::GetOption useDefaultRelation] } {
  264. UpdateRelation [Main::GetDefaultRelation $objectType]
  265. }
  266. }
  267. SetButtonState normal
  268. }
  269. if { $_relation_ == "CHILD" } {
  270. if { $objectType == "canvas" } {
  271. set toolbar canvas
  272. } elseif { $objectType == "menu" } {
  273. set toolbar menu
  274. } else {
  275. # set toolbar widget
  276. set toolbar $_currentToolbarFrame_ ; # widget or special
  277. }
  278. }
  279. if { $_relation_ == "SIBLING" } {
  280. if { [Canvas::WidgetIsCanvasObject $objectType] } {
  281. set toolbar canvas
  282. } elseif { [MenuEntry::WidgetIsMenuEntry $objectType] } {
  283. set toolbar menu
  284. } else {
  285. # set toolbar widget
  286. set toolbar $_currentToolbarFrame_ ; # widget or special
  287. }
  288. }
  289. # check if need to change the toolbar
  290. if { $toolbar != $_toolbar_ } {
  291. pack forget $_frame_($_toolbar_)
  292. pack $_frame_($toolbar)
  293. set _toolbar_ $toolbar
  294. }
  295. }
  296. proc GetRelation { } {
  297. # returns the current setting for relation; one of SIBLING of CHILD
  298. variable _relation_
  299. return $_relation_
  300. }
  301. proc SetButtonState state {
  302. variable _butRelation_
  303. $_butRelation_(SIBLING) configure -state $state
  304. $_butRelation_(CHILD) configure -state $state
  305. }
  306. proc ShowToolTip { widget X Y } {
  307. # creates the toplevel window used to display the tool tip
  308. variable _toolTip_
  309. variable _toolTipActive_
  310. variable _afterID_
  311. if { ![Option::GetOption toolTipsShow] } {
  312. return
  313. }
  314. # make tool tip toplevel each time, to simplify geometry management
  315. set _toolTip_ [toplevel .toolTip -bg yellow]
  316. wm withdraw $_toolTip_
  317. wm overrideredirect $_toolTip_ 1
  318. wm geometry $_toolTip_ +[expr $X + 21]+[expr $Y + 20]
  319. set fraWidget [frame $_toolTip_.fraWidget -bd 1 -relief raised]
  320. pack $fraWidget
  321. pack [label $fraWidget.labWidget -bg yellow -text $widget]
  322. # display tool tip immediately if its active (i.e., its already
  323. # being display for neighbouring widget), otherwise after a delay
  324. if { $_toolTipActive_ } {
  325. wm deiconify $_toolTip_
  326. } else {
  327. set _afterID_ [after 1000 Toolbar::DelayToolTip]
  328. }
  329. }
  330. proc HideToolTip { } {
  331. # destroys tool tip window, and cancels possibly pending after command
  332. variable _toolTip_
  333. catch { destroy $_toolTip_ }
  334. CancelToolTip ; # cancel pending after command
  335. }
  336. proc DelayToolTip { } {
  337. # command called by 'after', to display tool tip
  338. variable _toolTip_
  339. variable _toolTipActive_
  340. wm deiconify $_toolTip_
  341. set _toolTipActive_ 1
  342. }
  343. proc CancelToolTip { } {
  344. # cancels pending tooltip command
  345. variable _afterID_
  346. catch { after cancel $_afterID_ }
  347. }
  348. proc AddIcon { frame side icon tooltip command } {
  349. variable _height_
  350. variable _width_
  351. # make frame to limit icon button size
  352. set fraBut [frame $frame.fra_$icon -height $_height_ -width $_width_]
  353. pack $fraBut -side $side
  354. pack propagate $fraBut 0
  355. # make the icon button
  356. set but [button $fraBut.button -image [Icon::GetIconImage $icon] \
  357. -relief flat -bd 1 -takefocus 0 -command $command]
  358. pack $but
  359. bindtags $but [list $but Button IconButton . all]
  360. bind $but <Enter> "%W configure -relief raised
  361. Toolbar::ShowToolTip \"$tooltip\" \[winfo rootx %W] \[winfo rooty %W]"
  362. }
  363. proc AddSeparator { frame side } {
  364. # get unique name for this separator
  365. set i 0
  366. while { [winfo exists $frame.fraSeparator$i] } {
  367. incr i
  368. }
  369. if { $side == "left" || $side == "right" } {
  370. set fraSeparator [frame $frame.fraSeparator$i -width 2 \
  371. -relief sunken -bd 3]
  372. pack $fraSeparator -side $side -fill y -padx 2 -pady 2
  373. } else {
  374. set fraSeparator [frame $frame.fraSeparator$i -height 2 \
  375. -relief sunken -bd 3]
  376. pack $fraSeparator -side $side -fill x -padx 2 -pady 1
  377. }
  378. }
  379. }