source: trunk/SRC/ToBeReviewed/UTILITAIRE/text_box.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

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