]>
git.saurik.com Git - wxWidgets.git/blob - samples/caret/caret.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCaret sample
4 // Author: Robert Roebling
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include <wx/wxprec.h>
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all <standard< wxWindows headers
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
32 // the application icon
33 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
34 #include "mondrian.xpm"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // Define a new application type, each program should derive a class from wxApp
42 class MyApp
: public wxApp
45 // override base class virtuals
46 // ----------------------------
48 // this one is called on application startup and is a good place for the app
49 // initialization (doing it here and not in the ctor allows to have an error
50 // return: if OnInit() returns false, the application terminates)
51 virtual bool OnInit();
54 // MyCanvas is a canvas on which you can type
55 class MyCanvas
: public wxScrolledWindow
59 MyCanvas( wxWindow
*parent
);
62 wxChar
& CharAt(int x
, int y
) { return *(m_text
+ x
+ m_xChars
* y
); }
66 void MoveCaret(int x
, int y
);
69 void Home() { m_xCaret
= 0; }
70 void End() { m_xCaret
= m_xChars
- 1; }
71 void FirstLine() { m_yCaret
= 0; }
72 void LastLine() { m_yCaret
= m_yChars
- 1; }
73 void PrevChar() { if ( !m_xCaret
-- ) { End(); PrevLine(); } }
74 void NextChar() { if ( ++m_xCaret
== m_xChars
) { Home(); NextLine(); } }
75 void PrevLine() { if ( !m_yCaret
-- ) LastLine(); }
76 void NextLine() { if ( ++m_yCaret
== m_yChars
) FirstLine(); }
79 void OnPaint( wxPaintEvent
&event
);
80 void OnSize( wxSizeEvent
&event
);
81 void OnChar( wxKeyEvent
&event
);
84 // move the caret to m_xCaret, m_yCaret
89 // the margin around the text (looks nicer)
90 int m_xMargin
, m_yMargin
;
92 // size (in pixels) of one character
93 long m_widthChar
, m_heightChar
;
95 // position (in text coords) of the caret
96 int m_xCaret
, m_yCaret
;
98 // the size (in text coords) of the window
99 int m_xChars
, m_yChars
;
104 DECLARE_DYNAMIC_CLASS(MyCanvas
)
105 DECLARE_EVENT_TABLE()
109 // Define a new frame type: this is going to be our main frame
110 class MyFrame
: public wxFrame
114 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
116 // event handlers (these functions should _not_ be virtual)
117 void OnQuit(wxCommandEvent
& event
);
118 void OnAbout(wxCommandEvent
& event
);
119 void OnSetBlinkTime(wxCommandEvent
& event
);
120 void OnCaretMove(wxCommandEvent
& event
);
125 // any class wishing to process wxWindows events must use this macro
126 DECLARE_EVENT_TABLE()
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 // IDs for the controls and the menu commands
142 // controls start here (the numbers are, of course, arbitrary)
146 // ----------------------------------------------------------------------------
147 // event tables and other macros for wxWindows
148 // ----------------------------------------------------------------------------
150 // the event tables connect the wxWindows events with the functions (event
151 // handlers) which process them. It can be also done at run-time, but for the
152 // simple menu events like this the static method is much simpler.
153 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
154 EVT_MENU(Caret_Quit
, MyFrame::OnQuit
)
155 EVT_MENU(Caret_About
, MyFrame::OnAbout
)
156 EVT_MENU(Caret_SetBlinkTime
, MyFrame::OnSetBlinkTime
)
157 EVT_MENU(Caret_Move
, MyFrame::OnCaretMove
)
160 // Create a new application object: this macro will allow wxWindows to create
161 // the application object during program execution (it's better than using a
162 // static object for many reasons) and also declares the accessor function
163 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
167 // ============================================================================
169 // ============================================================================
171 // ----------------------------------------------------------------------------
172 // the application class
173 // ----------------------------------------------------------------------------
175 // `Main program' equivalent: the program execution "starts" here
178 // create and show the main application window
179 MyFrame
*frame
= new MyFrame("Caret wxWindows sample",
180 wxPoint(50, 50), wxSize(450, 340));
184 // success: wxApp::OnRun() will be called which will enter the main message
185 // loop and the application will run. If we returned FALSE here, the
186 // application would exit immediately.
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
195 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
196 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
198 // set the frame icon
199 SetIcon(wxICON(mondrian
));
202 wxMenu
*menuFile
= new wxMenu
;
204 menuFile
->Append(Caret_SetBlinkTime
, "&Blink time...\tCtrl-B");
205 menuFile
->Append(Caret_Move
, "&Move caret\tCtrl-C");
206 menuFile
->AppendSeparator();
207 menuFile
->Append(Caret_About
, "&About...\tCtrl-A", "Show about dialog");
208 menuFile
->AppendSeparator();
209 menuFile
->Append(Caret_Quit
, "E&xit\tAlt-X", "Quit this program");
211 // now append the freshly created menu to the menu bar...
212 wxMenuBar
*menuBar
= new wxMenuBar
;
213 menuBar
->Append(menuFile
, "&File");
215 // ... and attach this menu bar to the frame
218 m_canvas
= new MyCanvas(this);
220 // create a status bar just for fun (by default with 1 pane only)
222 SetStatusText("Welcome to wxWindows!");
228 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
230 // TRUE is to force the frame to close
234 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
236 wxMessageBox(_T("The caret wxWindows sample.\n© 1999 Vadim Zeitlin"),
237 _T("About Caret"), wxOK
| wxICON_INFORMATION
, this);
240 void MyFrame::OnCaretMove(wxCommandEvent
& WXUNUSED(event
))
242 m_canvas
->MoveCaret(10, 10);
245 void MyFrame::OnSetBlinkTime(wxCommandEvent
& WXUNUSED(event
))
247 long blinkTime
= wxGetNumberFromUser
249 _T("The caret blink time is the time between two blinks"),
250 _T("Time in milliseconds:"),
251 _T("wxCaret sample"),
252 wxCaret::GetBlinkTime(), 0, 10000,
255 if ( blinkTime
!= -1 )
257 wxCaret::SetBlinkTime((int)blinkTime
);
258 m_canvas
->CreateCaret();
259 wxLogStatus(this, _T("Blink time set to %ld milliseconds."), blinkTime
);
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
269 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
270 EVT_PAINT(MyCanvas::OnPaint
)
271 EVT_SIZE(MyCanvas::OnSize
)
272 EVT_CHAR(MyCanvas::OnChar
)
275 MyCanvas::MyCanvas( wxWindow
*parent
)
276 : wxScrolledWindow( parent
, -1,
277 wxDefaultPosition
, wxDefaultSize
,
280 m_text
= (wxChar
*)NULL
;
282 SetBackgroundColour(*wxWHITE
);
284 m_font
= wxFont(12, wxFONTFAMILY_TELETYPE
,
285 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
287 m_xCaret
= m_yCaret
=
288 m_xChars
= m_yChars
= 0;
290 m_xMargin
= m_yMargin
= 5;
295 MyCanvas::~MyCanvas()
300 void MyCanvas::CreateCaret()
304 m_heightChar
= dc
.GetCharHeight();
305 m_widthChar
= dc
.GetCharWidth();
307 wxCaret
*caret
= new wxCaret(this, m_widthChar
, m_heightChar
);
310 caret
->Move(m_xMargin
, m_yMargin
);
314 void MyCanvas::MoveCaret(int x
, int y
)
322 void MyCanvas::DoMoveCaret()
324 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret
, m_yCaret
);
326 GetCaret()->Move(m_xMargin
+ m_xCaret
* m_widthChar
,
327 m_yMargin
+ m_yCaret
* m_heightChar
);
330 void MyCanvas::OnSize( wxSizeEvent
&event
)
332 m_xChars
= (event
.GetSize().x
- 2*m_xMargin
) / m_widthChar
;
333 m_yChars
= (event
.GetSize().y
- 2*m_yMargin
) / m_heightChar
;
340 m_text
= (wxChar
*)calloc(m_xChars
* m_yChars
, sizeof(wxChar
));
342 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
344 if ( frame
&& frame
->GetStatusBar() )
347 msg
.Printf(_T("Panel size is (%d, %d)"), m_xChars
, m_yChars
);
349 frame
->SetStatusText(msg
, 1);
355 // NB: this method is horrible inefficient especially because the caret
356 // needs to be redrawn often and in this case we only have to redraw
357 // the caret location and not the entire window - in a real program we
358 // would use GetUpdateRegion() and iterate over rectangles it contains
359 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
361 wxCaretSuspend
cs(this);
362 wxPaintDC
dc( this );
366 dc
.SetFont( m_font
);
368 for ( int y
= 0; y
< m_yChars
; y
++ )
372 for ( int x
= 0; x
< m_xChars
; x
++ )
374 wxChar ch
= CharAt(x
, y
);
380 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
384 void MyCanvas::OnChar( wxKeyEvent
&event
)
386 switch ( event
.KeyCode() )
418 if ( !event
.AltDown() && wxIsprint(event
.KeyCode()) )
420 wxChar ch
= (wxChar
)event
.KeyCode();
421 CharAt(m_xCaret
, m_yCaret
) = ch
;
423 wxCaretSuspend
cs(this);
426 dc
.SetBackgroundMode(wxSOLID
); // overwrite old value
427 dc
.DrawText(ch
, m_xMargin
+ m_xCaret
* m_widthChar
,
428 m_yMargin
+ m_yCaret
* m_heightChar
);