source: trunk/UTILITAIRE/text_box.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

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