sedit.stk 4.53 KB
Newer Older
eg's avatar
eg committed
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
;;;;
;;;; sedit.stk	-- A small editor written in STklos
;;;; 
;;;; Copyright  2000-2002 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;; 
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
;;;; USA.
;;;; 
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 10-Nov-2000 15:14 (eg)
;;;; Last file update:  8-Apr-2002 18:36 (eg)
;;;;

(require "gtklos")

;; Change the image-path to allow execution of the demo without installation
(set-image-path! (cons "./images" (image-path)))


(define *current-file* #f)	;; Name of the current file	
(define *text*	       #f)	;; The text widget 

;;;;
;;;; Our error handler. This handler pops a window instead of displaying the 
;;;; message on the standard error port.
;;;;

(define (graphical-handler k l m)
  (make-simple-dialog :text  (string-append "\n\n" m "\n\n")
		      :title "Error ..." :image "stopsign"
		      :texts '("OK")))

;;;;
;;;; I/O Operations
;;;;

(define (file-edit f)
  (set! *current-file* f)
  (text-read-file *text* f))

(define (open-a-file . _)
  (with-handler graphical-handler
    (let ((f  (apply  make-file-selector-dialog 
		      :title "Open File ..."
		      (if *current-file*  (list :value *current-file*) '()))))
      (when f
	(file-edit f)))))

(define (save-as . _)
  (with-handler graphical-handler
    (let ((f (apply make-file-selector-dialog 
		    :title "Save as ..."
		    (if *current-file* (list :value *current-file*) '()))))
      (when f
	(if (and (file-exists? f) (not (file-is-regular? f)))
	    (error "bad file ~S" f)
	    (begin
	      (set! *current-file* f)
	      (text-write-file *text* *current-file*)))))))
	  
(define (save-file . _)
  (unless *current-file* (save-as _))
  (text-write-file *text* *current-file*))

;;;;
;;;; Copy and Paste
;;;;

(define (cut-sel   . l) (text-cut-clipboard *text*))
(define (copy-sel  . l) (text-copy-clipboard *text*))
(define (paste-sel . l) (text-paste-clipboard *text*))

;;;;
;;;; Misc
;;;;

(define (quit-editor . _)
  (exit 0))

(define (about-editor _)
  (make-simple-dialog :title "Information ..."
		      :text (string-append 
			     "\n"
			     "This is a simple demo of an editor written\n"
			     "using STklos (version " (version) ")\n")
		      :texts '("Ok")))

;;;;
;;;;	M A K E - E D I T O R
;;;;
(define (make-editor file)
  (let* ((top   (make <window> :title "STklos Demo Editor"))
	 (menus (make <menu-bar> :parent top
		      :items `(("File" 
				  ("Open"    	 :action ,open-a-file)
				  ("Save"    	 :action ,save-file)
				  ("Save As ..." :action ,save-as)
				  (""       	 :type   :separator)
				  ("Quit"   	 :action ,quit-editor))
			       ("Edit"
				  ("Cut"   	 :action ,cut-sel)
				("Copy"  	 :action ,copy-sel)
				("Paste" 	 :action ,paste-sel))
			       ()
			       ("Help"
				  ("About"	:action ,about-editor)))))
	 (hand  (make <handle-window> :border-width 2 :parent top))
	 (tb    (make <toolbar>  :style "icons" :relief "none" :space-style "line"
		      :parent hand
		      :items 
		      `((:file "tb_fileopen.xpm" :command ,open-a-file 
			       :tooltip "Open File")
			(:file "tb_edit.xpm" :command ,save-file
			       :tooltip "Save File")
			()
			(:file "tb_exit.xpm" :command ,quit-editor
			       :tooltip "Quit")
			()
			(:file "tb_cut.xpm" :command ,cut-sel
			       :tooltip "Cut")
			(:file "tb_copy.xpm" :command ,copy-sel 
			       :tooltip "Copy")
			(:file "tb_paste.xpm" :command ,paste-sel
			       :tooltip "Paste"))))
	 (txt	 (make <text> :width 500 :height 400)))
    ;; Place now txt in its container and tell it that it expands and fills..
    (container-add! top txt :fill #t :expand #t)
    (set! *text* txt)

    ;; If a file was specified, try to edit it
    (when file
      (file-edit file))))

(define (main args)
  ;; If we have just one argument, use it as an initial file. Otherwise start
  ;; with an empty buffer
  (make-editor (if (= (length args) 2)
		   (cadr args)
		   #f)))