File: TGraphScale.cpp

package info (click to toggle)
gentle 1.9+cvs20100605+dfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 12,224 kB
  • ctags: 6,214
  • sloc: cpp: 41,569; ansic: 3,978; sh: 1,420; makefile: 243
file content (247 lines) | stat: -rw-r--r-- 6,731 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "TGraph.h"

//******************************************************** TGraphScale
 
TGraphScale::TGraphScale ( float _min , float _max , bool _horizontal , bool _left , wxString _name , wxString _unit , 
				wxColour _col , wxString _type )
	{
 	min = _min ;
   max = _max ;
 	top = max ;
 	bottom = min ;
 	horizontal = _horizontal ;
 	left = _left ;
 	col = *wxBLACK ;
 	name = _name ;
 	unit = _unit ;
 	type = _type ;
 	col = _col ;
 	outline = wxRect ( -1 , -1 , 0 , 0 ) ;
 	selected = false ;
 	show_mark = false ;
	}
	
int TGraphScale::GetWidth ()
	{
 	return horizontal ? 30 : 50 ;
	}
	
wxRect TGraphScale::CalcInternalRect ( wxRect &r )
	{
	wxRect ir = r ;
	int w = GetWidth() ;
	if ( horizontal )
		{
   		if ( left ) // Top
   			{
		    r.y += w ;
   			}
      	else // Bottom
       		{
   		    ir.y += ir.height - w ;
         	}        
      	ir.height = w ;
      	r.height -= w ;
		}
 	else
  		{
   		if ( left ) // Left
   			{
		    r.x += w ;
   			}
      	else // Right
       		{
   		    ir.x += ir.width - w ;
         	}        
      	ir.width = w ;
      	r.width -= w ;
    	}        
   	return ir ;
	}    

void TGraphScale::drawit ( wxDC &dc , wxRect &r , wxRect &inner )
	{
   	wxRect ir = CalcInternalRect ( r ) ;
   	int border = 2 ;
   	
   	if ( horizontal )
   		{
	    if ( ir.x < inner.x ) ir.x = inner.x ;
	    while ( ir.GetRight() > inner.GetRight() ) ir.width-- ;
	    outline = ir ;
	    ir.height -= border*2 ;
	    ir.y += border ;
   		}
    else
    	{
	    if ( ir.y < inner.y ) ir.y = inner.y ;
	    while ( ir.GetBottom() > inner.GetBottom() ) ir.height-- ;
	    outline = ir ;
	    ir.width -= border*2 ;
	    ir.x += border ;
     	}        
   	
   	// Selected? Draw nice background!
   	if ( selected )
   		{
	    dc.SetPen ( *MYPEN ( TGraphDisplay::prettyColor ) ) ;
	    dc.SetBrush ( *MYBRUSH ( TGraphDisplay::prettyColor ) ) ;
	    dc.DrawRectangle ( outline ) ;
	    dc.SetBrush ( *wxTRANSPARENT_BRUSH ) ;
   		}   
   	
   	// Calculate major and minor units
   	float multiplier = 10 ; //( unit == _T("s") ) ? 6 : 10 ;
   	float factor = 1 ;
   	while ( factor <= max ) factor *= multiplier ;
   	float f2 = top - bottom ;
   	while ( factor >= f2 ) factor /= multiplier ;
   	
   	float major = factor ;
   	float minor = major / 5 ;
   	
   	// Draw scale
   	dc.SetFont ( *MYFONT ( 8 , wxMODERN , wxNORMAL , wxNORMAL ) ) ;
   	dc.SetTextForeground ( col ) ;
   	dc.SetPen ( *MYPEN ( col ) ) ;
   	
   	float p = 0 ;
   	while ( p > bottom ) p -= minor ;
   	while ( p < bottom ) p += minor ;
   	if ( p != bottom ) p -= minor ;
   	while ( p <= top )
   		{
			wxString label ;
/*			if ( unit == _T("s") ) label = wxString::Format ( _T("%d:%2d") , p / 60 , ((int)p) % 60 ) ;
			else */label = wxString::Format ( _T("%f") , p ) ;
			DrawMark ( dc , p , ir , label , ( p - ((int)(p/major))*major ) == 0 ) ;
			p += minor ;
   		}    
	
	// Draw "backline" and name/unit
	wxString text = name ;
	if ( !unit.IsEmpty() ) text += _T(" [") + unit + _T("]") ;
   	int tw , th ;
   	dc.GetTextExtent ( text , &tw , &th ) ;
	if ( horizontal )
		{
  		if ( left )
    		{
          	dc.DrawLine ( ir.x , ir.y , ir.GetRight() , ir.y ) ;
          	dc.DrawText ( text , ir.x , ir.y ) ;
          	}   	
  		else
    		{
     		dc.DrawLine ( ir.x , ir.GetBottom() , ir.GetRight() , ir.GetBottom() ) ;
          	dc.DrawText ( text , ir.x , ir.GetBottom() - th ) ;
     		}  		
  		if ( show_mark )
  			{
	    	int c = GetRealCoord ( GetVirtualCoordinate ( mark , inner ) , inner ) ;
	    	dc.SetPen ( *wxRED_PEN ) ;
	    	dc.DrawLine ( c , ir.GetTop() , c , ir.GetBottom() ) ;
  			}    
		}		
	else
		{
		if ( left )
  			{
         dc.DrawLine ( ir.x , ir.y , ir.x , ir.GetBottom() ) ;
         dc.DrawRotatedText ( text , ir.x + 2 , ir.GetBottom() , 90 ) ;
         }   	
		else
  			{
    		dc.DrawLine ( ir.GetRight() , ir.y , ir.GetRight() , ir.GetBottom() ) ;
         dc.DrawRotatedText ( text , ir.GetRight()-th , ir.GetBottom() , 90 ) ;
      	}
  		if ( show_mark )
  			{
	    	int c = GetRealCoord ( GetVirtualCoordinate ( mark , inner ) , inner ) ;
	    	dc.SetPen ( *wxRED_PEN ) ;
	    	dc.DrawLine ( ir.GetLeft() , c , ir.GetRight() , c ) ;
  			}    
  		}
	last_inner = inner ;
	show_mark = false ;
	}    

void TGraphScale::DrawMark ( wxDC &dc , float p , wxRect &ir , wxString text , bool big )
	{
	int x , y , z ;
 	if ( horizontal )
 		{
    	x = GetRealCoord ( p , ir ) ;
    	y = ir.y + ir.height / 2 ;
    	z = big ? 0 : ir.height / 4 ;
    	if ( x < ir.x || x > ir.GetRight() ) return ;
 		}
 	else
  		{
    	x = ir.x + ir.width / 2 ;
    	y = GetRealCoord ( p , ir ) ;
    	z = big ? 0 : ir.width / 4 ;
    	if ( y < ir.y || y > ir.GetBottom() ) return ;
    	}        
   
	while ( text.Last() == '0' ) text = text.Left ( text.length() - 1 ) ;
   while ( text.Last() == '.' ) text = text.Left ( text.length() - 1 ) ;
   	
   int tw , th ;
   dc.GetTextExtent ( text , &tw , &th ) ;
   if ( horizontal )
   	{
	    if ( left ) dc.DrawLine ( x , y-z , x , ir.y ) ;
	    else dc.DrawLine ( x , y+z , x , ir.GetBottom() ) ;
	    x -= tw / 2 ;
	    y = left ? ir.GetBottom() - th : ir.y ;
   	}
   else
    	{
	    if ( left ) dc.DrawLine ( ir.x , y , x-z , y ) ;
	    else dc.DrawLine ( x+z , y , ir.GetRight() , y ) ;
	    y -= th / 2 ;
	    x = left ? ir.GetRight() - tw : ir.x ;
     	}        

   if ( big ) dc.DrawText ( text , x , y ) ;
	}
    
int TGraphScale::GetRealCoord ( float f , wxRect &inner )
	{
	float m2 = horizontal ? inner.width : inner.height ; // The pixel "width"
	float f2 = top - bottom ; // The internal "width"
	float ret = m2 * ( f - bottom ) / f2 ;
	if ( horizontal ) ret += (float ) inner.x ;
	else ret = (float) ( inner.GetBottom() ) - ret ;
 	return (int) ret ;
	}    

float TGraphScale::GetVirtualCoordinate ( int i , wxRect &inner )
	{
 	i -= horizontal ? inner.x : inner.y ;
 	float i2 = i ;
	float m2 = horizontal ? inner.width : inner.height ;
	float f2 = top - bottom ; // The internal "width"
	f2 = f2 * i2 / m2 ;
	if ( horizontal ) f2 += bottom ;
	else f2 = top - f2 ;
	return f2 ;
 	}    	

void TGraphScale::Drag ( int delta )
	{
	float df = GetVirtualCoordinate ( delta , last_inner ) ;
	df -= GetVirtualCoordinate ( 0 , last_inner ) ;
	top += df ;
	bottom += df ;
	if ( top > max )
 		{
   		bottom -= top - max ;
        top = max ;
        }    
    if ( bottom < min )
    	{
	    top += min - bottom ;
	    bottom = min ;
    	}    
	}