source: trunk/SRC/Utilities/text_box.pro @ 133

Last change on this file since 133 was 133, checked in by navarro, 18 years ago

english and nicer header (1)

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1;+
2;
3; @file_comments
4; This procedure writes a short text message within a box-shaped
5; area in a graphics window.  The message may be split at word
6; boundaries into several lines, and the character size and
7 ;orientation may be adjusted for the text to fit within the box.
8;   
9; @param TEXT {in}{required} ASCII text string containing the message.
10;
11; @keyword pos  4 element vector specifying the box position and size
12;               pos[0],pos[1] specify the lower left corner coordinate
13;               pos[2],pos[3] specify the upper right corner coordinate
14;               data window normalized coordinates are use
15;
16; @keyword fg_color color of box and legend titles (default=0)
17;
18; @keyword bg_color background color. Setting BG_COLOR erases the area
19;               covered by the text box (filling it with color BG_COLOR)
20;               prior to writing the text.  If both BG_COLOR and !p.color
21;               are zero then the background color is reset to 255 to
22;               gaurantee a readability.
23;               
24; @keyword right if set, right justify text
25;
26; @keyword center if set, center the text
27;
28; @keyword vert_space {default=1.5}vertical spacing of lines in units of character height
29;
30; @keyword _EXTRA used to pass your keyword
31;
32; @keyword box activate to show the box on graphics window.
33;
34; @history  Paul Ricchiazzi                            7Jul93
35;           Institute for Computational Earth System Science
36;           University of California, Santa Barbara
37;
38; @version $Id$
39;
40;-
41PRO text_box,text,pos=pos,fg_color=fg_color,bg_color=bg_color,$
42               center=center,right=right,box=box,vert_space=vert_space, _EXTRA = ex
43;
44;
45  compile_opt idl2, strictarrsubs
46;
47        ON_ERROR, 2
48;
49;  Check the number of parameters.
50;
51justify=-1
52if keyword_set(right) ne 0 then justify=1
53if keyword_set(center) ne 0 then justify=0
54if keyword_set(vert_space) eq 0 then vert_space= 1.5
55IF n_elements(text) eq 0 then message, 'must specify text'
56nnx=!x.window*!d.x_vsize
57nny=!y.window*!d.y_vsize
58nnx=[0., 1.]*!d.x_vsize
59nny=[0., 1.]*!d.y_vsize
60
61
62if n_elements(pos) eq 0 then begin
63
64  box_cursor,xx1,yy1,nx,ny
65  xx2=xx1+nx
66  yy2=yy1+ny
67  pos=[(xx1-nnx[0])/(nnx[1]-nnx[0]),(yy1-nny[0])/(nny[1]-nny[0]),$
68       (xx2-nnx[0])/(nnx[1]-nnx[0]),(yy2-nny[0])/(nny[1]-nny[0])]
69  posstring=string(form='(a,4(f5.2,a))',$
70           ',pos=[',pos[0],',',pos[1],',',pos[2],',',pos[3],']')
71  print,strcompress(posstring,/remove_all)
72
73   
74endif else begin
75 
76  xx1 = nnx[0]+pos[0]*(nnx[1]-nnx[0])
77  xx2 = nnx[0]+pos[2]*(nnx[1]-nnx[0])
78  yy1 = nny[0]+pos[1]*(nny[1]-nnx[0])
79  yy2 = nny[0]+pos[3]*(nny[1]-nnx[0])
80
81endelse
82;
83;  calculate the height and width of the box in characters.
84;
85  width  = (xx2 - xx1) / !d.x_ch_size
86  height = (yy2 - yy1) / !d.y_ch_size
87;
88;  decompose the message into words.
89;
90  words = str_sep(text,' ')
91; print,f='(20a)',words
92  nwords=n_elements(words)
93  wordlen=lenstr(words)*!d.x_vsize
94  blanklen=lenstr(' ')*!d.x_vsize
95  maxcharsize=(xx2-xx1)/(4*blanklen+max(wordlen))
96  charsize=1 
97  lpnt=intarr(nwords)
98  nomore=0
99  ntries=0
100  repeat begin
101    ntries=ntries+1
102    if ntries gt 20 then message,'Can not fit message into box'
103    ychsiz=vert_space*!d.y_ch_size*charsize
104    wlen=wordlen*charsize
105    blen=blanklen*charsize
106    n_lines=fix((yy2-yy1)/ychsiz)-1
107    sum=0
108    ilines=0
109;   print,f='(8a8)','charsz','i','ilines','n_lines','lpnt','wlen','sum','xwdth'
110    for i=0,nwords-1 do begin
111      sum=sum+wlen[i]+blen
112      if sum+3*blen gt xx2-xx1 then begin
113        ilines=ilines+1
114        sum=wlen[i]+blen
115      endif
116      lpnt[i]=ilines       
117     
118;      print,f='(f8.2,4i8,3f8.2)',charsize,i,ilines,n_lines,lpnt[i],$
119;                 wlen[i]+blen,sum+3*blen,xx2-xx1
120    endfor       
121    case 1 of
122      ilines+1 lt n_lines: if charsize*1.1 gt maxcharsize then $
123          vert_space=(yy2-yy1)/((n_lines-1)*!d.y_ch_size*charsize) $
124          else charsize=charsize*1.1
125      ilines+1 eq n_lines: nomore=1
126      ilines+1 gt n_lines: charsize=charsize*.9
127    endcase
128endrep until nomore
129
130lines=strarr(n_lines)
131maxlen=0
132
133for i=0,n_lines-1 do begin
134  ii=where(lpnt eq i,nc)
135  maxlen=(total(wlen[ii])+nc*blen)>maxlen
136  lines[i]=string(f='(200a)',words[ii]+' ')
137; print,i,words[ii]
138; print,i,lines[i]
139endfor
140
141;
142  align=.5*(1+justify)
143 
144  case justify of
145    -1:xx = xx1+.5*((xx2-xx1)-maxlen)
146     0:xx = 0.5*(xx1 + xx2)
147     1:xx = xx2-.5*((xx2-xx1)-maxlen)
148  endcase
149
150  dy=!d.y_ch_size*charsize*vert_space
151  yy=yy2-0.5*dy
152
153  xbox=[xx1,xx2,xx2,xx1,xx1]
154  ybox=[yy1,yy1,yy2,yy2,yy1]
155  if n_elements(bg_color) ne 0 then begin
156    if !p.color eq 0 and bg_color eq 0 then bgc=255 else bgc=bg_color
157    polyfill,xbox,ybox,color=bgc,/device
158  endif
159
160  if n_elements(fg_color) eq 0 then color = 0 else color=fg_color
161
162  for i_line = 0,n_lines-1 do begin
163    yy = yy-dy
164;   print,xx,yy,lines[i_line],charsize
165    xyouts, xx, yy, lines[i_line], /device, charsize=charsize, $
166      alignment=align, color=color, font=-1, _extra = ex
167  endfor
168  if keyword_set(box) then plots,xbox,ybox,color=color,/device
169;
170return
171end
172
Note: See TracBrowser for help on using the repository browser.