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

Last change on this file since 231 was 231, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

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