GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Text Editor created using Gnocl, the Tcl/Gtk Bindings

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> Project Showcase
Author Message
wjgiddings
GTK+ Geek


Joined: 06 Feb 2009
Posts: 71
Location: The Midlands, England

PostPosted: Sat Feb 07, 2009 11:45 am    Post subject: Text Editor created using Gnocl, the Tcl/Gtk Bindings Reply with quote

Hi there.

Here's a version of the ubiquitous text editor created in Gnocl. For many newcomer to scripting, Tcl/Tk may not be the 'coolest' dynamic language on the block, but its surely is one of the most mature! Unlike other bindings, Gnocl doesn't try to 'wrap' library functions, but offers powerful, easy to use commands to access widget functionality.

Get your copy of Gnocl from Sourceforge.

http://sourceforge.net/projects/gnocl/

William J Giddings
Gnocl Maintainer




Code: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#---------------
 # GnoclEdit.tcl
 #
 # Simple, plain text editor using gnocl Tcl-Gtk bindings
 # Created by William J Giddings
 # 29-October-2007
 #---------------

 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" "$@"

 package require Gnocl

 # array to contain some global variables
 set GE(text)     ""           ;# name of editor widget
 set GE(status)   ""           ;# name of status bar widget
 set GE(filename) "untitled"   ;# filename
 set GE(basefont) "Courier"    ;# besefont for text display
 set GE(fontsize) "12"         ;# default font size in pixels

 # array to contain tooltip strings
 set TT(new)      "Clear workspace and begin a new document."
 set TT(open)     "Clear existing workspace and open an existing file for editing."
 set TT(save)     "Save the current, named file to disk."
 set TT(saveas)   "Save an untitled file to disk or save a copy with a new name."
 set TT(quit)     "Discard current document and close the GnoclEdit program."
 set TT(print)    "Print the contents of the edit buffer."
 set TT(cut)      "Cut the active selection to the clipboard."
 set TT(copy)     "Copy the active selection to the clipboard."
 set TT(paste)    "Paste the current clipboard into the insertion point or replace selection."
 set TT(delete)   "Delete the current selection from the document."
 set TT(selall)   "Select all text within the current document."

 #
 # build the application user interface
 #
 proc main {} {
   # Step 1) create menus
   # a) file
   set menu [gnocl::menu]
   $menu add [gnocl::menuItem -text "%#New" -tooltip $::TT(new) \
      -onClicked fileNew]
   $menu add [gnocl::menuItem -text "%#Open" -tooltip $::TT(open) \
      -onClicked fileOpen]
   $menu add [gnocl::menuItem -text "%#Save" -tooltip $::TT(save) \
      -onClicked fileSave]
   $menu add [gnocl::menuItem -text "%#SaveAs" -tooltip $::TT(saveas) \
      -onClicked fileSaveAs]
   $menu add [gnocl::menuSeparator]
   $menu add [gnocl::menuItem -text "%#Print" -tooltip $::TT(print) \
      -onClicked print]
   $menu add [gnocl::menuSeparator]
   $menu add [gnocl::menuItem -text "%#Quit"  -tooltip $::TT(quit) \
      -onClicked exit ]

   set file [gnocl::menuItem -text "%__File" -submenu $menu]

   # b) edit
   set edit [gnocl::menu]
   $edit add [gnocl::menuItem -text "%#Cut" -tooltip $::TT(cut) \
      -onClicked cut ]
   $edit add [gnocl::menuItem -text "%#Copy" -tooltip $::TT(copy) \
      -onClicked copy ]
   $edit add [gnocl::menuItem -text "%#Paste" -tooltip $::TT(paste) \
      -onClicked paste ]
   $edit add [gnocl::menuSeparator]
   $edit add [gnocl::menuItem -text "%#Delete" -tooltip $::TT(delete) \
      -onClicked delete ]
   $edit add [gnocl::menuSeparator]
   $edit add [gnocl::menuItem -text "%_Select _All" -tooltip $::TT(delete) \
      -onClicked selectAll -accelerator "<Ctrl>A" ]

   set edit [gnocl::menuItem -text "%__Edit" -submenu $edit]

   # c) about
   set menu [gnocl::menu]
   $menu add [gnocl::menuItem -text "%__About" \
      -tooltip "Show about dialog" \
      -onClicked about ]
   set help [gnocl::menuItem -text "%__Help" -submenu $menu]

   # Step 2) create toolbar
   set toolBar [gnocl::toolBar -style both]
   $toolBar add item -text "%#New" -tooltip $::TT(new) \
      -onClicked fileNew
   $toolBar add item -text "%#Open" -tooltip $::TT(open) \
      -onClicked fileOpen
   $toolBar add item -text "%#Save" -tooltip $::TT(save) \
      -onClicked fileSave
   $toolBar add space
   $toolBar add item -text "%#Print" -tooltip $::TT(print) \
              -onClicked print
   $toolBar add space
   $toolBar add item -text "%#Cut" -tooltip $::TT(cut) \
      -onClicked cut
   $toolBar add item -text "%#Copy" -tooltip $::TT(copy) \
      -onClicked copy
   $toolBar add item -text "%#Paste" -tooltip $::TT(paste) \
      -onClicked paste

   # Step 3) create text edit area, and default formatting tag
   # No need to add scrollbars or input popumenu, these will be added automatically
   set text [gnocl::text \
                   -onButtonPress {
                           puts %b
                           if {%b == "3" } { textPopup }  \
                   }]
   $text tag create default -fontFamily $::GE(basefont) -fontSize $::GE(fontsize)

   # Step 4) create status bar
   set status [gnocl::statusBar]

   # Step 5) create Container and toplevel window, add containter to window
   set box [gnocl::box -orientation vertical -borderWidth 0 -spacing 0]
   set win [gnocl::window -child $box -title "GnoclEdit" -onDelete exit]

   # Step 6) add elements to the container
   $box add [gnocl::menuBar -children [list $file $edit $help]]
   $box add $toolBar
   $box add $text -fill {1 1} -expand 1
   $box add $status

   # Step 7) update globals
   set ::GE(text) $text
   set ::GE(status) $status

   # Step 8) default status
   $::GE(status) push "File: $::GE(filename)"

   # enter the main input loop
   gnocl::mainLoop
 }

 #---------------
 # update the status bar, remind us of the filename!
 #---------------
 proc statusUpdate {str} { $::GE(status) push "File: $str" }

 #---------------
 # file menu procs, they speak for themselves
 #---------------
 proc fileNew {} {
   $::GE(text) erase {0 0} end
   set ::GE(filename) "untitled"
   statusUpdate "untitled"
 }

 proc fileOpen {} {
   set file [gnocl::fileChooser \
      -action open \
      -title "Please choose a file to edit.."]

   if { $file != "" } {
      set fp [open $file "r"]
      set data [read $fp]
      close $fp
      $::GE(text) erase {0 0} end
      $::GE(text) insert end $data -tags default
      statusUpdate $file
      set ::GE(filename) $file
   }
 }

 proc fileSaveAs {} {
   set file [gnocl::fileChooser \
      -action save \
      -title "Please choose a file to save.."]

   if { $file != "" } {
      set fp [open $file "w"]
      set data [$::GE(text) get {0 0} end-1]
      puts $fp $data
      close $fp
      statusUpdate $file
      set ::GE(filename) $file
   }
 }

 proc fileSave {} {

   if {$::GE(filename) != "untitled" } {
      set fp [open $::GE(filename) "w"]
      set data [$::GE(text) get {0 0} end-1]
      puts $fp $data
      close $fp
      return
   } else {
      fileSaveAs
   }

 }

 proc print {} {
    set fp [open __print.tmp__ "w"]
    set data [$::GE(text) get {0 0} end-1]
    puts $fp $data
    close $fp
    exec lpr __print.tmp__ &
 }

 # Basically rebuilds the menubar submenu
 proc textPopup {} {
   # make text popup menu
   set editPopup [gnocl::menu -tearoff 0]
   $editPopup add [gnocl::menuItem -text "%#Cut" -tooltip $::TT(cut) \
      -onClicked cut ]
   $editPopup add [gnocl::menuItem -text "%#Copy" -tooltip $::TT(copy) \
      -onClicked copy ]
   $editPopup add [gnocl::menuItem -text "%#Paste" -tooltip $::TT(paste) \
      -onClicked paste ]
   $editPopup add [gnocl::menuSeparator]
   $editPopup add [gnocl::menuItem -text "%#Delete" -tooltip $::TT(delete) \
      -onClicked delete ]
   $editPopup add [gnocl::menuSeparator]
   $editPopup add [gnocl::menuItem -text "%_Select _All" -tooltip $::TT(delete) \
      -onClicked selectAll -accelerator "<Ctrl>A" ]
   $editPopup popup
 }

 #---------------
 # clipboard operations
 #---------------
 proc cut {} {
   gnocl::clipboard clear
   gnocl::clipboard setText [$::GE(text) get selectionStart selectionEnd ]
   $::GE(text) erase selectionStart selectionEnd
 }

 proc paste {} {
    $::GE(text) erase selectionStart selectionEnd
    $::GE(text) insert cursor [gnocl::clipboard getText] -tags default
 }

 proc copy {} { gnocl::clipboard setText [$::GE(text) get selectionStart selectionEnd ] }

 proc delete {} { $::GE(text) erase selectionStart selectionEnd }

 proc selectAll {} { $::GE(text) select {0 0} end-1 }

 #---------------
 # about box
 #---------------
 proc about {} { gnocl::dialog -type info -text "GnoclEdit by\nWilliam J Giddings\n29-Oct-2007" }

 # RUN THE APP!
 main
[img][/img]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> Project Showcase All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP