1 /***************************************************************************
2 * lefttreeitemdata.cpp
3 *
4 * Wed Sep 6 22:19:52 2006
5 * Copyright 2006 liubin,China
6 * Email multiget@gmail.com
7 ****************************************************************************/
8
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24 #include "meterctrl.h"
25 #include "mgapp.h"
26
27 #include "common.h"
28 #include <iostream>
29
30 using namespace std;
31
32 DEFINE_EVENT_TYPE( wxID_TIMER2 )
33
DEFINE_EVENT_TYPE(wxID_30K)34 DEFINE_EVENT_TYPE( wxID_30K )
35 DEFINE_EVENT_TYPE( wxID_90K )
36 DEFINE_EVENT_TYPE( wxID_300K )
37 DEFINE_EVENT_TYPE( wxID_900K )
38 DEFINE_EVENT_TYPE( wxID_3M )
39 DEFINE_EVENT_TYPE( wxID_9M )
40
41 BEGIN_EVENT_TABLE( CMeterCtrl, wxWindow )
42 EVT_PAINT( CMeterCtrl::OnPaint )
43 EVT_SIZE( CMeterCtrl::OnSize )
44 EVT_TIMER( wxID_TIMER2, CMeterCtrl::OnTimer )
45 EVT_RIGHT_UP( CMeterCtrl::OnMouseRightUp )
46 EVT_MOTION( CMeterCtrl::OnMouseMove )
47 EVT_MENU( wxID_30K, CMeterCtrl::On30K )
48 EVT_MENU( wxID_90K, CMeterCtrl::On90K )
49 EVT_MENU( wxID_300K, CMeterCtrl::On300K )
50 EVT_MENU( wxID_900K, CMeterCtrl::On900K )
51 EVT_MENU( wxID_3M, CMeterCtrl::On3M )
52 EVT_MENU( wxID_9M, CMeterCtrl::On9M )
53 END_EVENT_TABLE()
54
55 CMeterCtrl::CMeterCtrl( wxWindow* parent )
56 : wxWindow( parent, wxID_ANY )
57 {
58 SetBackgroundColour( wxColour( 0, 0, 0 ) );
59 m_nMaxSpeed = 300; //300K为上界
60 m_SpeedList.push_back( 0 );
61 m_timer.SetOwner( this, wxID_TIMER2 );
62 m_fade = 0;
63 m_tip = wxGetApp().GetWxStr( _S_100_TONGJI );
64 }
65
SetTip(string tip)66 void CMeterCtrl::SetTip( string tip )
67 {
68 m_tip = wxString( tip.c_str(), wxConvLocal );
69 m_fade = 0;
70
71 if ( m_tip.empty() && m_timer.IsRunning() )
72 {
73 m_timer.Stop();
74 }
75
76 wxPaintEvent refresh;
77 wxPostEvent( this, refresh );
78 }
79
PutSpeedData(int speed)80 void CMeterCtrl::PutSpeedData( int speed )
81 {
82
83 if ( m_SpeedList.size() < 1000 )
84 {
85 m_SpeedList.push_front( speed );
86 }
87 else
88 {
89 m_SpeedList.pop_back();
90 m_SpeedList.push_front( speed );
91 }
92 #ifdef WIN32
93 Refresh();
94 #else
95 wxPaintEvent refresh;
96 wxPostEvent( this, refresh );
97 #endif
98 }
99
OnSize(wxSizeEvent & event)100 void CMeterCtrl::OnSize( wxSizeEvent& event )
101 {
102 event.Skip();
103 }
104
OnPaint(wxPaintEvent & event)105 void CMeterCtrl::OnPaint( wxPaintEvent& event )
106 {
107
108 wxCoord w, h;
109 GetSize( &w, &h );
110
111 wxBitmap bmpBuf( w, 30 );
112 wxMemoryDC MemDC;
113 MemDC.SelectObject( bmpBuf );
114 MemDC.SetBackground( wxBrush( wxColour( 0, 0, 0 ) ) );
115 MemDC.Clear();
116
117 MemDC.SetPen( *( wxGREEN_PEN ) );
118
119 list<int>::iterator it;
120
121 int count = 0;
122 int barheight;
123
124 for ( it = m_SpeedList.begin(); it != m_SpeedList.end(); it++ )
125 {
126 barheight = ( *it ) * 28 / m_nMaxSpeed / 1024;
127
128 if ( barheight > 28 )
129 barheight = 28;
130
131 MemDC.DrawLine( count * 2, 28, count * 2, 28 - barheight );
132
133 MemDC.DrawLine( count * 2 + 1, 28, count * 2 + 1, 28 - barheight );
134
135 count++;
136
137 }
138
139
140 MemDC.SetPen( *( wxGREY_PEN ) );
141
142 MemDC.DrawLine( 0, 10, 2048, 10 );
143 MemDC.DrawLine( 0, 20, 2048, 20 );
144
145
146 if ( m_tip.Length() > 0 )
147 {
148 int gray = 255 - m_fade * 10;
149 MemDC.SetTextForeground( wxColour( gray, gray, gray ) );
150 MemDC.SetTextBackground( wxColour( 0, 0, 0 ) );
151 int tw, th, x = 0, y = 0;
152 GetTextExtent( m_tip, &tw, &th );
153
154 if ( tw < w )
155 x = ( w - tw ) / 2;
156 else
157 x = 0;
158
159 if ( th < h )
160 y = ( h - th ) / 2;
161 else
162 y = 0;
163
164 MemDC.DrawText( m_tip, x, y );
165 }
166
167
168 wxPaintDC dc( this );
169 dc.Blit( 0, 0, w, h, &MemDC, 0, 0 );
170 }
171
172
OnTimer(wxTimerEvent & event)173 void CMeterCtrl::OnTimer( wxTimerEvent& event )
174 {
175 m_fade++;
176
177 if ( m_fade > 25 )
178 {
179 m_timer.Stop();
180 m_tip.clear();
181 m_fade = 0;
182 }
183 #ifdef WIN32
184 Refresh();
185 #else
186 wxPaintEvent refresh;
187 wxPostEvent( this, refresh );
188 #endif
189 }
190
On30K(wxCommandEvent & event)191 void CMeterCtrl::On30K( wxCommandEvent& event )
192 {
193 m_nMaxSpeed = 30;
194 wxPaintEvent refresh;
195 wxPostEvent( this, refresh );
196 }
197
On90K(wxCommandEvent & event)198 void CMeterCtrl::On90K( wxCommandEvent& event )
199 {
200 m_nMaxSpeed = 90;
201 wxPaintEvent refresh;
202 wxPostEvent( this, refresh );
203 }
204
On300K(wxCommandEvent & event)205 void CMeterCtrl::On300K( wxCommandEvent& event )
206 {
207 m_nMaxSpeed = 300;
208 wxPaintEvent refresh;
209 wxPostEvent( this, refresh );
210 }
211
On900K(wxCommandEvent & event)212 void CMeterCtrl::On900K( wxCommandEvent& event )
213 {
214 m_nMaxSpeed = 900;
215 wxPaintEvent refresh;
216 wxPostEvent( this, refresh );
217
218 }
219
On3M(wxCommandEvent & event)220 void CMeterCtrl::On3M( wxCommandEvent& event )
221 {
222 m_nMaxSpeed = 3 * 1024;
223 wxPaintEvent refresh;
224 wxPostEvent( this, refresh );
225 }
226
On9M(wxCommandEvent & event)227 void CMeterCtrl::On9M( wxCommandEvent& event )
228 {
229 m_nMaxSpeed = 9 * 1024;
230 wxPaintEvent refresh;
231 wxPostEvent( this, refresh );
232 }
233
OnMouseMove(wxMouseEvent & event)234 void CMeterCtrl::OnMouseMove( wxMouseEvent& event )
235 {
236 if ( m_tip.empty() )
237 return ;
238 else if ( !m_timer.IsRunning() )
239 m_timer.Start( 100 );
240 }
241
242 //pop menu
OnMouseRightUp(wxMouseEvent & event)243 void CMeterCtrl::OnMouseRightUp( wxMouseEvent& event )
244 {
245 wxPoint pos = GetPosition();
246 //menu begin
247 wxMenu *rootPopMenu = new wxMenu( wxGetApp().GetWxStr( _S_METER_TOPSPEED ) );
248
249 wxMenuItem* _30K = rootPopMenu->AppendCheckItem( wxID_30K, wxT( "30K/S" ) );
250 wxMenuItem* _90K = rootPopMenu->AppendCheckItem( wxID_90K, wxT( "90K/S" ) );
251 wxMenuItem* _300K = rootPopMenu->AppendCheckItem( wxID_300K, wxT( "300K/S" ) );
252 wxMenuItem* _900K = rootPopMenu->AppendCheckItem( wxID_900K, wxT( "900/S" ) );
253 wxMenuItem* _3M = rootPopMenu->AppendCheckItem( wxID_3M, wxT( "3M/S" ) );
254 wxMenuItem* _9M = rootPopMenu->AppendCheckItem( wxID_9M, wxT( "9M/S" ) );
255
256 switch ( m_nMaxSpeed )
257 {
258
259 case 30:
260 _30K->Check();
261 break;
262
263 case 90:
264 _90K->Check();
265 break;
266
267 case 300:
268 _300K->Check();
269 break;
270
271 case 900:
272 _900K->Check();
273 break;
274
275 case 3*1024:
276 _3M->Check();
277 break;
278
279 case 9*1024:
280 _9M->Check();
281 break;
282
283 default:
284 break;
285 }
286
287 PopupMenu( rootPopMenu );
288 delete rootPopMenu;
289 }
290
291