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

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

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

  • 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;               gaurantee 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 your keyword
39;
40; @keyword BOX
41; activate to show the box on graphics window.
42;
43; @history  Paul Ricchiazzi                            7Jul93
44;           Institute for Computational Earth System Science
45;           University of California, Santa Barbara
46;
47; @version $Id$
48;
49;-
50PRO text_box,text,pos=pos,fg_color=fg_color,bg_color=bg_color,$
51               center=center,right=right,box=box,vert_space=vert_space, _EXTRA = ex
52;
53;
54  compile_opt idl2, strictarrsubs
55;
56        ON_ERROR, 2
57;
58;  Check the number of parameters.
59;
60justify=-1
61if keyword_set(right) ne 0 then justify=1
62if keyword_set(center) ne 0 then justify=0
63if keyword_set(vert_space) eq 0 then vert_space= 1.5
64IF n_elements(text) eq 0 then message, 'must specify text'
65nnx=!x.window*!d.x_vsize
66nny=!y.window*!d.y_vsize
67nnx=[0., 1.]*!d.x_vsize
68nny=[0., 1.]*!d.y_vsize
69
70
71if n_elements(pos) eq 0 then begin
72
73  box_cursor,xx1,yy1,nx,ny
74  xx2=xx1+nx
75  yy2=yy1+ny
76  pos=[(xx1-nnx[0])/(nnx[1]-nnx[0]),(yy1-nny[0])/(nny[1]-nny[0]),$
77       (xx2-nnx[0])/(nnx[1]-nnx[0]),(yy2-nny[0])/(nny[1]-nny[0])]
78  posstring=string(form='(a,4(f5.2,a))',$
79           ',pos=[',pos[0],',',pos[1],',',pos[2],',',pos[3],']')
80  print,strcompress(posstring,/remove_all)
81
82   
83endif else begin
84 
85  xx1 = nnx[0]+pos[0]*(nnx[1]-nnx[0])
86  xx2 = nnx[0]+pos[2]*(nnx[1]-nnx[0])
87  yy1 = nny[0]+pos[1]*(nny[1]-nnx[0])
88  yy2 = nny[0]+pos[3]*(nny[1]-nnx[0])
89
90endelse
91;
92;  calculate the height and width of the box in characters.
93;
94  width  = (xx2 - xx1) / !d.x_ch_size
95  height = (yy2 - yy1) / !d.y_ch_size
96;
97;  decompose the message into words.
98;
99  words = str_sep(text,' ')
100; print,f='(20a)',words
101  nwords=n_elements(words)
102  wordlen=lenstr(words)*!d.x_vsize
103  blanklen=lenstr(' ')*!d.x_vsize
104  maxcharsize=(xx2-xx1)/(4*blanklen+max(wordlen))
105  charsize=1 
106  lpnt=intarr(nwords)
107  nomore=0
108  ntries=0
109  repeat begin
110    ntries=ntries+1
111    if ntries gt 20 then message,'Can not fit message into box'
112    ychsiz=vert_space*!d.y_ch_size*charsize
113    wlen=wordlen*charsize
114    blen=blanklen*charsize
115    n_lines=fix((yy2-yy1)/ychsiz)-1
116    sum=0
117    ilines=0
118;   print,f='(8a8)','charsz','i','ilines','n_lines','lpnt','wlen','sum','xwdth'
119    for i=0,nwords-1 do begin
120      sum=sum+wlen[i]+blen
121      if sum+3*blen gt xx2-xx1 then begin
122        ilines=ilines+1
123        sum=wlen[i]+blen
124      endif
125      lpnt[i]=ilines       
126     
127;      print,f='(f8.2,4i8,3f8.2)',charsize,i,ilines,n_lines,lpnt[i],$
128;                 wlen[i]+blen,sum+3*blen,xx2-xx1
129    endfor       
130    case 1 of
131      ilines+1 lt n_lines: if charsize*1.1 gt maxcharsize then $
132          vert_space=(yy2-yy1)/((n_lines-1)*!d.y_ch_size*charsize) $
133          else charsize=charsize*1.1
134      ilines+1 eq n_lines: nomore=1
135      ilines+1 gt n_lines: charsize=charsize*.9
136    endcase
137endrep until nomore
138
139lines=strarr(n_lines)
140maxlen=0
141
142for i=0,n_lines-1 do begin
143  ii=where(lpnt eq i,nc)
144  maxlen=(total(wlen[ii])+nc*blen)>maxlen
145  lines[i]=string(f='(200a)',words[ii]+' ')
146; print,i,words[ii]
147; print,i,lines[i]
148endfor
149
150;
151  align=.5*(1+justify)
152 
153  case justify of
154    -1:xx = xx1+.5*((xx2-xx1)-maxlen)
155     0:xx = 0.5*(xx1 + xx2)
156     1:xx = xx2-.5*((xx2-xx1)-maxlen)
157  endcase
158
159  dy=!d.y_ch_size*charsize*vert_space
160  yy=yy2-0.5*dy
161
162  xbox=[xx1,xx2,xx2,xx1,xx1]
163  ybox=[yy1,yy1,yy2,yy2,yy1]
164  if n_elements(bg_color) ne 0 then begin
165    if !p.color eq 0 and bg_color eq 0 then bgc=255 else bgc=bg_color
166    polyfill,xbox,ybox,color=bgc,/device
167  endif
168
169  if n_elements(fg_color) eq 0 then color = 0 else color=fg_color
170
171  for i_line = 0,n_lines-1 do begin
172    yy = yy-dy
173;   print,xx,yy,lines[i_line],charsize
174    xyouts, xx, yy, lines[i_line], /device, charsize=charsize, $
175      alignment=align, color=color, font=-1, _extra = ex
176  endfor
177  if keyword_set(box) then plots,xbox,ybox,color=color,/device
178;
179return
180end
181
Note: See TracBrowser for help on using the repository browser.