1: // the most improtant code in frame.h and frame.cpp
2: /***************************************************************
3: * Name: tmp1Main.h
4: * Purpose: Defines Application Frame
5: * Author: grass in moon
6: * Created: 2012-07-06
7: * Copyright: grass in moon (http://www.cppblog.com/grass-and-moon/)
8: * License:
9: **************************************************************/
10:
11: #ifndef TMP1MAIN_H
12: #define TMP1MAIN_H
13:
14: #ifndef WX_PRECOMP
15: #include <wx/wx.h>
16: #endif
17:
18: #include "tmp1App.h"
19:
20: class tmp1Frame: public wxFrame
21: {
22: public:
23: tmp1Frame(wxFrame *frame, const wxString& title);
24: ~tmp1Frame();
25:
26: // for painting
27: void PaintNow();
28: void render(wxDC& dc);
29:
30: private:
31: enum
32: {
33: idMenuQuit = 1000,
34: idMenuAbout,
35: idStartLoop,
36: idStopLoop
37: };
38:
39: // mark loop on or off
40: bool render_loop_on;
41:
42: void OnClose(wxCloseEvent& event);
43: void OnQuit(wxCommandEvent& event);
44: void OnAbout(wxCommandEvent& event);
45: void OnStartLoop(wxCommandEvent& event);
46: void OnStopLoop(wxCommandEvent& event);
47:
48: // deel with wxIdleEvent
49: void OnIdle(wxIdleEvent& event);
50:
51: DECLARE_EVENT_TABLE()
52: };
53:
54:
55: #endif // TMP1MAIN_H
56:
57: /***************************************************************
58: * Name: tmp1Main.cpp
59: * Purpose: Code for Application Frame
60: * Author: grass in moon
61: * Created: 2012-07-06
62: * Copyright: grass in moon (http://www.cppblog.com/grass-and-moon/)
63: * License:
64: **************************************************************/
65:
66: #ifdef WX_PRECOMP
67: #include "wx_pch.h"
68: #endif
69:
70: #ifdef __BORLANDC__
71: #pragma hdrstop
72: #endif //__BORLANDC__
73:
74: #include "tmp1Main.h"
75:
76: //helper functions
77: enum wxbuildinfoformat
78: {
79: short_f, long_f
80: };
81:
82: wxString wxbuildinfo(wxbuildinfoformat format)
83: {
84: wxString wxbuild(wxVERSION_STRING);
85:
86: if (format == long_f )
87: {
88: #if defined(__WXMSW__)
89: wxbuild << _T("-Windows");
90: #elif defined(__WXMAC__)
91: wxbuild << _T("-Mac");
92: #elif defined(__UNIX__)
93: wxbuild << _T("-Linux");
94: #endif
95:
96: #if wxUSE_UNICODE
97: wxbuild << _T("-Unicode build");
98: #else
99: wxbuild << _T("-ANSI build");
100: #endif // wxUSE_UNICODE
101: }
102:
103: return wxbuild;
104: }
105:
106: BEGIN_EVENT_TABLE(tmp1Frame, wxFrame)
107: EVT_CLOSE(tmp1Frame::OnClose)
108: EVT_MENU(idMenuQuit, tmp1Frame::OnQuit)
109: EVT_MENU(idMenuAbout, tmp1Frame::OnAbout)
110: EVT_MENU(idStartLoop, tmp1Frame::OnStartLoop)
111: EVT_MENU(idStopLoop, tmp1Frame::OnStopLoop)
112: EVT_IDLE(tmp1Frame::OnIdle)
113: END_EVENT_TABLE()
114:
115: tmp1Frame::tmp1Frame(wxFrame *frame, const wxString& title)
116: : wxFrame(frame, -1, title)
117: {
118: #if wxUSE_MENUS
119: // create a menu bar
120: wxMenuBar* mbar = new wxMenuBar();
121: wxMenu* fileMenu = new wxMenu(_T(""));
122: fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
123: fileMenu->Append(idStartLoop, _("St&artLoop")); // for starting draw text
124: fileMenu->Append(idStopLoop, _("Sto&pLoop")); // for stoping draw text
125: mbar->Append(fileMenu, _("&File"));
126:
127: wxMenu* helpMenu = new wxMenu(_T(""));
128: helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
129: mbar->Append(helpMenu, _("&Help"));
130:
131: SetMenuBar(mbar);
132: #endif // wxUSE_MENUS
133:
134: #if wxUSE_STATUSBAR
135: // create a status bar with some information about the used wxWidgets version
136: CreateStatusBar(2);
137: SetStatusText(_("Hello Code::Blocks user!"),0);
138: SetStatusText(wxbuildinfo(short_f), 1);
139: #endif // wxUSE_STATUSBAR
140:
141: render_loop_on = false;
142: }
143:
144:
145: tmp1Frame::~tmp1Frame()
146: {
147: }
148:
149: void tmp1Frame::OnClose(wxCloseEvent &event)
150: {
151: Destroy();
152: }
153:
154: void tmp1Frame::OnQuit(wxCommandEvent &event)
155: {
156: Destroy();
157: }
158:
159: void tmp1Frame::OnAbout(wxCommandEvent &event)
160: {
161: wxString msg = wxbuildinfo(long_f);
162: wxMessageBox(msg, _("Welcome to..."));
163: }
164:
165: // set render_loop_on true, make OnIdle can draw text
166: void tmp1Frame::OnStartLoop(wxCommandEvent& event)
167: {
168: render_loop_on = true;
169: }
170:
171: // set render_loop_on true, make OnIdle can skip draw text
172: void tmp1Frame::OnStopLoop(wxCommandEvent& event)
173: {
174: render_loop_on = false;
175: }
176:
177: // paint depend on render_loop_on is true or off
178: void tmp1Frame::OnIdle(wxIdleEvent& event)
179: {
180: if (render_loop_on)
181: {
182: PaintNow();
183: event.RequestMore();
184: }
185: }
186:
187: void tmp1Frame::PaintNow()
188: {
189: wxClientDC dc(this);
190: render(dc);
191: }
192:
193: // draw text on screen
194: void tmp1Frame::render(wxDC& dc)
195: {
196: static int y = 0;
197: static int y_speed = 2;
198:
199: y += y_speed;
200: if (y < 0) y_speed = 2;
201: if (y > 200) y_speed = -2;
202:
203: dc.SetBackground(*wxWHITE_BRUSH);
204: dc.Clear();
205: dc.DrawText(wxT("Testing"),40,y);
206: }
207: