OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [tags/] [start/] [insight/] [itcl/] [iwidgets3.0.0/] [generic/] [hyperhelp.itk] - Blame information for rev 1765

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
#
2
# Hyperhelp
3
# ----------------------------------------------------------------------
4
# Implements a help facility using html formatted hypertext files.
5
#
6
# ----------------------------------------------------------------------
7
#  AUTHOR: Kris Raney                   EMAIL: kraney@spd.dsccc.com
8
#
9
#  @(#) $Id: hyperhelp.itk,v 1.1.1.1 2002-01-16 10:24:50 markom Exp $
10
# ----------------------------------------------------------------------
11
#            Copyright (c) 1996 DSC Technologies Corporation
12
# ======================================================================
13
# Permission to use, copy, modify, distribute and license this software
14
# and its documentation for any purpose, and without fee or written
15
# agreement with DSC, is hereby granted, provided that the above copyright
16
# notice appears in all copies and that both the copyright notice and
17
# warranty disclaimer below appear in supporting documentation, and that
18
# the names of DSC Technologies Corporation or DSC Communications
19
# Corporation not be used in advertising or publicity pertaining to the
20
# software without specific, written prior permission.
21
#
22
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
23
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
24
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
25
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE,
26
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL
27
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
30
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31
# SOFTWARE.
32
# ======================================================================
33
 
34
#
35
# Acknowledgements:
36
#
37
# Special thanks go to Sam Shen(SLShen@lbl.gov), as this code is based on his
38
# help.tcl code from tk inspect.
39
 
40
#
41
# Default resources.
42
#
43
option add *Hyperhelp.width 575 widgetDefault
44
option add *Hyperhelp.height 450 widgetDefault
45
option add *Hyperhelp.modality none widgetDefault
46
option add *Hyperhelp.vscrollMode static widgetDefault
47
option add *Hyperhelp.hscrollMode static widgetDefault
48
option add *Hyperhelp.maxHistory 20 widgetDefault
49
 
50
#
51
# Usual options.
52
#
53
itk::usual Hyperhelp {
54
    keep -activebackground -activerelief -background -borderwidth -cursor \
55
         -foreground -highlightcolor -highlightthickness \
56
         -selectbackground -selectborderwidth -selectforeground \
57
         -textbackground
58
}
59
 
60
# ------------------------------------------------------------------
61
#                          HYPERHELP
62
# ------------------------------------------------------------------
63
class iwidgets::Hyperhelp {
64
    inherit iwidgets::Shell
65
 
66
    constructor {args} {}
67
 
68
    itk_option define -topics topics Topics {}
69
    itk_option define -helpdir helpdir Directory .
70
    itk_option define -title title Title "Help"
71
    itk_option define -closecmd closeCmd CloseCmd {}
72
    itk_option define -maxhistory maxHistory MaxHistory 20
73
 
74
    public variable beforelink {}
75
    public variable afterlink {}
76
 
77
    public method showtopic {topic}
78
    public method followlink {link}
79
    public method forward {}
80
    public method back {}
81
    public method updatefeedback {n}
82
 
83
    protected method _readtopic {file {anchorpoint {}}}
84
    protected method _pageforward {}
85
    protected method _pageback {}
86
    protected method _lineforward {}
87
    protected method _lineback {}
88
    protected method _fill_go_menu {}
89
 
90
    protected variable _history {}      ;# History list of viewed pages
91
    protected variable _history_ndx -1  ;# current position in history list
92
    protected variable _history_len 0   ;# length of history list
93
    protected variable _histdir -1      ;# direction in history we just came
94
                                        ;# from
95
    protected variable _len 0           ;# length of text to be rendered
96
    protected variable _file {}         ;# current topic
97
 
98
    private variable _remaining 0       ;# remaining text to be rendered
99
    private variable _rendering 0       ;# flag - in process of rendering
100
}
101
 
102
#
103
# Provide a lowercased access method for the Scrolledlistbox class.
104
#
105
proc ::iwidgets::hyperhelp {pathName args} {
106
    uplevel ::iwidgets::Hyperhelp $pathName $args
107
}
108
 
109
# ------------------------------------------------------------------
110
#                        CONSTRUCTOR
111
# ------------------------------------------------------------------
112
body iwidgets::Hyperhelp::constructor {args} {
113
    itk_option remove iwidgets::Shell::padx iwidgets::Shell::pady
114
 
115
    #
116
    # Create a pulldown menu
117
    #
118
    itk_component add -private menubar {
119
      frame $itk_interior.menu -relief raised -bd 2
120
    } {
121
      keep -background -cursor
122
    }
123
    pack $itk_component(menubar) -side top -fill x
124
 
125
    itk_component add -private topicmb {
126
      menubutton $itk_component(menubar).topicmb -text "Topics" \
127
           -menu $itk_component(menubar).topicmb.topicmenu \
128
           -underline 0 -padx 8 -pady 2
129
    } {
130
      keep -background -cursor -font -foreground \
131
              -activebackground -activeforeground
132
    }
133
    pack $itk_component(topicmb) -side left
134
 
135
    itk_component add -private topicmenu {
136
      menu $itk_component(topicmb).topicmenu -tearoff no
137
    } {
138
      keep -background -cursor -font -foreground \
139
              -activebackground -activeforeground
140
    }
141
 
142
    itk_component add -private navmb {
143
      menubutton $itk_component(menubar).navmb -text "Navigate" \
144
          -menu $itk_component(menubar).navmb.navmenu \
145
          -underline 0 -padx 8 -pady 2
146
    } {
147
      keep -background -cursor -font -foreground \
148
             -activebackground -activeforeground
149
    }
150
    pack $itk_component(navmb) -side left
151
 
152
    itk_component add -private navmenu {
153
      menu $itk_component(navmb).navmenu -tearoff no
154
    } {
155
      keep -background -cursor -font -foreground \
156
              -activebackground -activeforeground
157
    }
158
    set m $itk_component(navmenu)
159
    $m add command -label "Forward" -underline 0 -state disabled \
160
         -command [code $this forward] -accelerator f
161
    $m add command -label "Back" -underline 0 -state disabled \
162
         -command [code $this back] -accelerator b
163
    $m add cascade -label "Go" -underline 0 -menu $m.go
164
 
165
    itk_component add -private navgo {
166
      menu $itk_component(navmenu).go -postcommand [code $this _fill_go_menu]
167
    } {
168
      keep -background -cursor -font -foreground \
169
              -activebackground -activeforeground
170
    }
171
 
172
    #
173
    # Create a scrolledhtml object to display help pages
174
    #
175
    itk_component add scrtxt {
176
      iwidgets::scrolledhtml $itk_interior.scrtxt \
177
           -linkcommand "$this followlink" -feedback "$this updatefeedback"
178
    } {
179
        keep    -hscrollmode -vscrollmode -background -textbackground \
180
                -fontname -fontsize -fixedfont -link \
181
                -linkhighlight -borderwidth -cursor -sbwidth -scrollmargin \
182
                -width -height -foreground -highlightcolor -visibleitems \
183
                -highlightthickness -padx -pady -activerelief \
184
                -relief -selectbackground -selectborderwidth \
185
                -selectforeground -setgrid -wrap -unknownimage
186
    }
187
    pack $itk_component(scrtxt) -fill both -expand yes
188
 
189
    #
190
    # Bind shortcut keys
191
    #
192
    bind $itk_component(hull)  [code $this forward]
193
    bind $itk_component(hull)  [code $this back]
194
    bind $itk_component(hull)  [code $this forward]
195
    bind $itk_component(hull)  [code $this back]
196
    bind $itk_component(hull)  [code $this _pageforward]
197
    bind $itk_component(hull)  [code $this _pageforward]
198
    bind $itk_component(hull)  [code $this _pageback]
199
    bind $itk_component(hull)  [code $this _pageback]
200
    bind $itk_component(hull)  [code $this _pageback]
201
    bind $itk_component(hull)  [code $this _lineforward]
202
    bind $itk_component(hull)  [code $this _lineback]
203
 
204
    wm title $itk_component(hull) "Help"
205
 
206
    eval itk_initialize $args
207
    if {[lsearch -exact $args -closecmd] == -1} {
208
      configure -closecmd [code $this deactivate]
209
    }
210
}
211
 
212
# ------------------------------------------------------------------
213
#                             OPTIONS
214
# ------------------------------------------------------------------
215
 
216
# ------------------------------------------------------------------
217
# OPTION: -topics
218
#
219
# Specifies the topics to display on the menu. For each topic, there should
220
# be a file named /.html
221
# ------------------------------------------------------------------
222
configbody iwidgets::Hyperhelp::topics {
223
    set m $itk_component(topicmenu)
224
    $m delete 0 last
225
    foreach topic $itk_option(-topics) {
226
      if {[lindex $topic 1] == {} } {
227
        $m add radiobutton -variable topic \
228
          -value $topic \
229
          -label $topic \
230
          -command [list $this showtopic $topic]
231
      } else {
232
        if {[string index [file dirname [lindex $topic 1]] 0] != "/" && \
233
            [string index [file dirname [lindex $topic 1]] 0] != "~"} {
234
          set link $itk_option(-helpdir)/[lindex $topic 1]
235
        } else {
236
          set link [lindex $topic 1]
237
        }
238
        $m add radiobutton -variable topic \
239
          -value [lindex $topic 0] \
240
          -label [lindex $topic 0] \
241
          -command [list $this followlink $link]
242
      }
243
    }
244
    $m add separator
245
    $m add command -label "Close Help" -underline 0 \
246
      -command $itk_option(-closecmd)
247
}
248
 
249
# ------------------------------------------------------------------
250
# OPTION: -title
251
#
252
# Specify the window title.
253
# ------------------------------------------------------------------
254
configbody iwidgets::Hyperhelp::title {
255
    wm title $itk_component(hull) $itk_option(-title)
256
}
257
 
258
# ------------------------------------------------------------------
259
# OPTION: -helpdir
260
#
261
# Set location of help files
262
# ------------------------------------------------------------------
263
configbody iwidgets::Hyperhelp::helpdir {
264
    if {[string index [file dirname $itk_option(-helpdir)] 0] != "/" && \
265
        [string index [file dirname $itk_option(-helpdir)] 0] != "~"} {
266
      configure -helpdir [pwd]/$itk_option(-helpdir)
267
    } else {
268
      set _history {}
269
      set _history_len 0
270
      set _history_ndx -1
271
      $itk_component(navmenu) entryconfig 0 -state disabled
272
      $itk_component(navmenu) entryconfig 1 -state disabled
273
      configure -topics $itk_option(-topics)
274
   }
275
}
276
 
277
# ------------------------------------------------------------------
278
# OPTION: -closecmd
279
#
280
# Specify the command to execute when close is selected from the menu
281
# ------------------------------------------------------------------
282
configbody iwidgets::Hyperhelp::closecmd {
283
  $itk_component(topicmenu) entryconfigure last -command $itk_option(-closecmd)
284
}
285
 
286
# ------------------------------------------------------------------
287
#                            METHODS
288
# ------------------------------------------------------------------
289
 
290
# ------------------------------------------------------------------
291
# METHOD: showtopic topic
292
#
293
# render text of help topic . The text is expected to be found in
294
# /.html
295
# ------------------------------------------------------------------
296
body iwidgets::Hyperhelp::showtopic {topic} {
297
  if ![regexp {(.*)#(.*)} $topic dummy topicname anchorpart] {
298
    set topicname $topic
299
    set anchorpart {}
300
  }
301
  if {$topicname == ""} {
302
    set topicname $_file
303
    set filepath $_file
304
  } else {
305
    set filepath $itk_option(-helpdir)/$topicname.html
306
  }
307
  if {[incr _history_ndx] < $itk_option(-maxhistory)} {
308
    set _history [lrange $_history 0 [expr $_history_ndx - 1]]
309
    set _history_len [expr $_history_ndx + 1]
310
  } else {
311
    incr _history_ndx -1
312
    set _history [lrange $_history 1 $_history_ndx]
313
    set _history_len [expr $_history_ndx + 1]
314
  }
315
  lappend _history [list $topicname $filepath $anchorpart]
316
  _readtopic $filepath $anchorpart
317
}
318
 
319
# ------------------------------------------------------------------
320
# METHOD: followlink link
321
#
322
# Callback for click on a link. Shows new topic.
323
# ------------------------------------------------------------------
324
body iwidgets::Hyperhelp::followlink {link} {
325
  if {[string compare $beforelink ""] != 0} {
326
    eval $beforelink $link
327
  }
328
  if ![regexp {(.*)#(.*)} $link dummy filepart anchorpart] {
329
    set filepart $link
330
    set anchorpart {}
331
  }
332
  if {$filepart != "" && [string index [file dirname $filepart] 0] != "/" && \
333
      [string index [file dirname $filepart] 0] != "~"} {
334
    set filepart [$itk_component(scrtxt) pwd]/$filepart
335
    set hfile $filepart
336
  } else {
337
    set hfile $_file
338
  }
339
  incr _history_ndx
340
  set _history [lrange $_history 0 [expr $_history_ndx - 1]]
341
  set _history_len [expr $_history_ndx + 1]
342
  lappend _history [list [file rootname [file tail $hfile]] $hfile $anchorpart]
343
  set ret [_readtopic $filepart $anchorpart]
344
  if {[string compare $afterlink ""] != 0} {
345
    eval $afterlink $link
346
  }
347
  return $ret
348
}
349
 
350
# ------------------------------------------------------------------
351
# METHOD: forward
352
#
353
# Show topic one forward in history list
354
# ------------------------------------------------------------------
355
body iwidgets::Hyperhelp::forward {} {
356
    if {$_rendering || ($_history_ndx+1) >= $_history_len} return
357
    incr _history_ndx
358
    eval _readtopic [lrange [lindex $_history $_history_ndx] 1 end]
359
}
360
 
361
# ------------------------------------------------------------------
362
# METHOD: back
363
#
364
# Show topic one back in history list
365
# ------------------------------------------------------------------
366
body iwidgets::Hyperhelp::back {} {
367
    if {$_rendering || $_history_ndx <= 0} return
368
    incr _history_ndx -1
369
    set _histdir 1
370
    eval _readtopic [lrange [lindex $_history $_history_ndx] 1 end]
371
}
372
 
373
# ------------------------------------------------------------------
374
# METHOD: updatefeedback remaining
375
#
376
# Callback from text to update feedback widget
377
# ------------------------------------------------------------------
378
body iwidgets::Hyperhelp::updatefeedback {n} {
379
    if {($_remaining - $n) > .1*$_len} {
380
      [$itk_interior.feedbackshell childsite].helpfeedback step [expr $_remaining - $n]
381
      update idletasks
382
      set _remaining $n
383
    }
384
}
385
 
386
# ------------------------------------------------------------------
387
# PRIVATE METHOD: _readtopic
388
#
389
# Read in file, render it in text area, and jump to anchorpoint
390
# ------------------------------------------------------------------
391
body iwidgets::Hyperhelp::_readtopic {file {anchorpoint {}}} {
392
  if {$file != ""} {
393
    if {[string compare $file $_file] != 0} {
394
      if {[catch {set f [open $file r]} err]} {
395
        incr _history_ndx $_histdir
396
        set _history_len [expr $_history_ndx + 1]
397
        set _histdir -1
398
        set m $itk_component(navmenu)
399
        if {($_history_ndx+1) < $_history_len} {
400
          $m entryconfig 0 -state normal
401
        } else {
402
          $m entryconfig 0 -state disabled
403
        }
404
        if {$_history_ndx > 0} {
405
          $m entryconfig 1 -state normal
406
        } else {
407
          $m entryconfig 1 -state disabled
408
        }
409
        error $err
410
      }
411
      set _file $file
412
      set txt [read $f]
413
      iwidgets::shell $itk_interior.feedbackshell -title "Rendering HTML" -padx 1 -pady 1
414
      iwidgets::Feedback [$itk_interior.feedbackshell childsite].helpfeedback \
415
          -steps [set _len [string length $txt]] \
416
          -labeltext "Rendering HTML" -labelpos n
417
      pack [$itk_interior.feedbackshell childsite].helpfeedback
418
      $itk_interior.feedbackshell center $itk_interior
419
      $itk_interior.feedbackshell activate
420
      set _remaining $_len
421
      set _rendering 1
422
      if [catch {$itk_component(scrtxt) render $txt [file dirname $file]} err] {
423
          if [regexp "
" $err] {
424
            $itk_component(scrtxt) render "$err"
425
          } else {
426
            $itk_component(scrtxt) render "
$err
"
427
          }
428
      }
429
      wm title $itk_component(hull) "Help: $file"
430
      delete object [$itk_interior.feedbackshell childsite].helpfeedback
431
      delete object $itk_interior.feedbackshell
432
      set _rendering 0
433
    }
434
  }
435
  set m $itk_component(navmenu)
436
  if {($_history_ndx+1) < $_history_len} {
437
    $m entryconfig 0 -state normal
438
  } else {
439
    $m entryconfig 0 -state disabled
440
  }
441
  if {$_history_ndx > 0} {
442
    $m entryconfig 1 -state normal
443
  } else {
444
    $m entryconfig 1 -state disabled
445
  }
446
  if {$anchorpoint != "{}"} {
447
    $itk_component(scrtxt) import -link #$anchorpoint
448
  } else {
449
    $itk_component(scrtxt) import -link #
450
  }
451
  set _histdir -1
452
}
453
 
454
# ------------------------------------------------------------------
455
# PRIVATE METHOD: _fill_go_menu
456
#
457
# update go submenu with current history
458
# ------------------------------------------------------------------
459
body iwidgets::Hyperhelp::_fill_go_menu {} {
460
    set m $itk_component(navgo)
461
    catch {$m delete 0 last}
462
    for {set i [expr $_history_len - 1]} {$i >= 0} {incr i -1} {
463
      set topic [lindex [lindex $_history $i] 0]
464
      set filepath [lindex [lindex $_history $i] 1]
465
      set anchor [lindex [lindex $_history $i] 2]
466
      $m add command -label $topic \
467
         -command [list $this followlink $filepath#$anchor]
468
    }
469
}
470
 
471
# ------------------------------------------------------------------
472
# PRIVATE METHOD: _pageforward
473
#
474
# Callback for page forward shortcut key
475
# ------------------------------------------------------------------
476
body iwidgets::Hyperhelp::_pageforward {} {
477
    $itk_component(scrtxt) yview scroll 1 pages
478
}
479
 
480
# ------------------------------------------------------------------
481
# PRIVATE METHOD: _pageback
482
#
483
# Callback for page back shortcut key
484
# ------------------------------------------------------------------
485
body iwidgets::Hyperhelp::_pageback {} {
486
    $itk_component(scrtxt) yview scroll -1 pages
487
}
488
 
489
# ------------------------------------------------------------------
490
# PRIVATE METHOD: _lineforward
491
#
492
# Callback for line forward shortcut key
493
# ------------------------------------------------------------------
494
body iwidgets::Hyperhelp::_lineforward {} {
495
    $itk_component(scrtxt) yview scroll 1 units
496
}
497
 
498
# ------------------------------------------------------------------
499
# PRIVATE METHOD: _lineback
500
#
501
# Callback for line back shortcut key
502
# ------------------------------------------------------------------
503
body iwidgets::Hyperhelp::_lineback {} {
504
    $itk_component(scrtxt) yview scroll -1 units
505
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.