home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / catdo_35.zip / wordview < prev   
Text File  |  1999-12-17  |  6KB  |  203 lines

  1. #!/usr/bin/wish4.2
  2.  
  3. set font 8x13
  4.  
  5. # Find options (All this can be tuned from dialog)
  6. set FindMode -exact ;# no -regexp for novices
  7. set FindDir -forwards ;# Why not -backwards
  8. set FindCase -nocase ;# Leave it empty if you want to be case sensitive
  9. foreach i {file edit search  help} {
  10. option add *$i.highlightBackground MidnightBlue 
  11. option add *$i.highlightThickness 0
  12. option add *$i.borderWidth 0
  13. }
  14. option add *m.activeBackground MidnightBlue 80 
  15. option add *m.activeForeground white 80 
  16. option add *m.activeBorderWidth 0 80
  17. menubutton .file -text File -menu .file.m -underline 0
  18. set m [menu .file.m]
  19. $m add command -label Open... -command load_file -accelerator Ctrl-O
  20. $m add command -label "Save As..." -command write_file -accelerator Ctrl-S
  21. $m add separator
  22. $m add command -label Quit -command exit -accelerator Alt-F4
  23. menubutton .edit -text Edit -menu .edit.m -underline 0 
  24. set m [menu .edit.m -postcommand EditEnable]
  25. $m add command -label Copy -command CopySel -accelerator Ctrl-C
  26. $m add separator
  27. $m add command -label "Select All" -accelerator Ctrl-A -command \
  28.  {.text tag add sel 0.0 end}
  29. menubutton .search -text Find -menu .search.m -underline 1 
  30. set m [menu .search.m -postcommand EnableSearch]
  31. $m add command -label "Find..." -command FindDialog -accelerator Ctrl-F
  32. $m add command -label "Find Again" -accelerator F3 -command DoFind
  33. menubutton .help -text Help -menu .help.m -underline 0
  34. set m [menu .help.m]
  35. $m add command -label "About..." -command AboutDialog
  36. text .text -width 80 -height 25 -font $font -xscrollcommand ".xs set" \
  37.     -yscrollcommand ".ys set" -background white -font $font -wrap word \
  38.     -selectforeground white -selectbackground black -spacing3 2m 
  39. .text tag configure sel -relief flat -borderwidth 0
  40. .text tag configure doc -lmargin1 0.2i -lmargin2 0
  41. scrollbar .ys -orient vert -command ".text yview"
  42. scrollbar .xs -orient horiz -command ".text xview"
  43. bind .text <F3> { if [info exists FindPattern] DoFind}
  44. bind .text <Control-O> load_file
  45. bind .text <Control-o> load_file
  46. bind .text <Control-S> {write_file}
  47. bind .text <Control-s> {write_file}
  48. bind .text <Control-F> FindDialog
  49. bind .text <Control-f> FindDialog
  50. grid .file .edit .search  x .help -sticky w
  51. grid .text - - -  - .ys
  52. grid .xs - - -  -
  53. grid .text -sticky news
  54. grid .xs -sticky we
  55. grid .ys -sticky ns
  56. grid columnconfigure . 0 -weight 0
  57. grid columnconfigure . 1 -weight 0
  58. grid columnconfigure . 2 -weight 0
  59. grid columnconfigure . 3 -weight 1
  60. grid columnconfigure . 4 -weight 0
  61. grid columnconfigure . 5 -weight 0
  62. grid rowconfigure . 0 -weight 0
  63. grid rowconfigure . 1 -weight 1
  64. grid rowconfigure . 2 -weight 0
  65.  
  66. proc load_file {{name {}}} {
  67. global filename
  68. if ![string length $name] {set name [tk_getOpenFile -filetypes {
  69. {{Msword files} .doc}
  70. {{All files} *}} ]}
  71. if ![string length $name] return
  72. if ![file readable $name] {
  73.   return -code error "Cannot open file $name"
  74. }
  75. set f [open "|[file dirname [info script]]/catdoc -w $name" r]
  76. .text configure -state normal
  77. .text delete 0.0 end
  78. .text insert 0.0 [read $f] doc
  79. .text mark set insert 1.0
  80. .text configure -state disabled
  81. .text see 1.0
  82. if [catch {close $f} msg] {
  83.  tk_messageBox -icon error -title error -message $msg -type ok
  84.  return
  85. }
  86. set filename $name
  87. }
  88. proc write_file {{name {}}} {
  89.     global filename 
  90.     if ![string length $name] {
  91.        set name [tk_getSaveFile -filetypes {
  92.       {{Text files} .txt}
  93.       {{LaTeX files} .tex}}]
  94.     }
  95.     if ![string length $name] return
  96.     if {[file extension $name]==".tex"} {
  97.        exec catdoc -t $filename > $name
  98.     } else {
  99.        exec catdoc $filename > $name
  100.     }
  101. }
  102. # -postcommand for Edit menu
  103. proc EditEnable {} {
  104. if [llength [.text tag ranges sel]] {
  105.   .edit.m entryconfigure Copy -state normal
  106.   .edit.m entryconfigure Delete -state normal
  107. } else {
  108.   .edit.m entryconfigure Copy -state disabled
  109. }
  110. }
  111. proc CopySel {} {
  112. clipboard clear
  113. clipboard append -- [.text get sel.first sel.last]
  114. }
  115. proc FindDialog {} {
  116. make_transient .find "Find" 
  117. frame .find.top
  118. label .find.top.l -text "Find"
  119. entry .find.top.e -width 30 -textvar FindPattern
  120. bind .find.top.e <Key-Return> ".find.b.find invoke"
  121. pack .find.top.l .find.top.e -side left
  122. FindOptionFrame
  123. frame .find.b
  124. button .find.b.find -text "Search" -command DoFind
  125. button .find.b.close -text "Close" -command "destroy .find"
  126. pack .find.b.find .find.b.close -side left -padx 20
  127. pack .find.top -pady 5 -anchor w -padx 10
  128. pack .find.opt -pady 10
  129. pack .find.b
  130. focus .find.top.e
  131. }
  132. proc EnableSearch {} {
  133. global FindPattern ReplaceString
  134. if ![info exists FindPattern] {
  135.   .search.m entryconfigure "Find Again" -state disabled
  136. } else {
  137.   .search.m entryconfigure "Find Again" -state normal
  138. }
  139. }
  140. proc make_transient {wpath title} {
  141. set x [expr [winfo rootx .]+[winfo width .]/3]
  142. set y [expr [winfo rooty .]+[winfo height .]/3]
  143. catch {destroy $wpath}
  144. toplevel $wpath
  145. wm transient $wpath .
  146. wm positionfrom $wpath program
  147. wm geometry $wpath +$x+$y
  148. wm title  $wpath $title
  149. }
  150. proc FindOptionFrame {} {
  151. frame .find.opt
  152. checkbutton .find.opt.dir -variable FindDir -onvalue -backwards\
  153.    -offvalue -forwards  -text Backward
  154. checkbutton .find.opt.regex -variable FindMode -onvalue\
  155.       -regex -offvalue -exact  -text RegExp
  156. checkbutton .find.opt.case -variable FindCase -onvalue -nocase -offvalue {}\
  157.   -text "Ignore case"
  158. pack .find.opt.dir .find.opt.regex .find.opt.case -side left
  159. }
  160. proc DoFind {{quiet 0}} {
  161. global FindPattern FindMode FindDir FindCase
  162. if ![string length $FindPattern] {return 0}
  163. if {$FindMode=="-backwords"} {  
  164.     set stopindex 0.0
  165. } else {
  166.   set stopindex end
  167. set index [eval ".text search $FindCase $FindMode $FindDir -- \
  168.   [list $FindPattern] insert $stopindex"] 
  169. if ![string length $index] {
  170.   if !$quiet {
  171.    tk_messageBox -type ok -title "Not found" -message "Pattern not found"
  172.   }
  173.  return 0
  174. } else {
  175. .text tag remove sel 0.0 end
  176. if {$FindMode=="-exact"} {
  177. .text tag add sel $index "$index + [string length $FindPattern] chars"
  178. } else {
  179. eval "regexp $FindCase --" [list $FindPattern [.text get "$index linestart"\
  180.    "$index lineend"] match]
  181. .text tag add sel $index "$index + [string length $match] chars"
  182. }
  183. .text mark set insert sel.last 
  184. .text see $index
  185. .text see insert
  186. focus .text
  187. return 1
  188. }
  189. }
  190. proc AboutDialog {} {
  191. make_transient .about "About Notepad"
  192. message .about.m -aspect 250 -text "MS-Word viewer for UNIX
  193. Copyright (c) by Softweyr, 1997
  194. Project GUI Light" -justify center
  195. button .about.ok -text Ok -command {destroy .about}
  196. pack .about.m .about.ok
  197. }
  198. if [llength $argv] {
  199. load_file $argv
  200. }
  201. focus .text
  202.