]>
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< wxWidgets headers
27 #include "wx/numdlg.h"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // the application icon
34 #if !defined(__WXMSW__) && !defined(__WXPM__)
35 #include "../sample.xpm"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // Define a new application type, each program should derive a class from wxApp
43 class MyApp
: public wxApp
46 // override base class virtuals
47 // ----------------------------
49 // this one is called on application startup and is a good place for the app
50 // initialization (doing it here and not in the ctor allows to have an error
51 // return: if OnInit() returns false, the application terminates)
52 virtual bool OnInit();
55 // MyCanvas is a canvas on which you can type
56 class MyCanvas
: public wxScrolledWindow
60 MyCanvas( wxWindow
*parent
);
63 wxChar
& CharAt(int x
, int y
) { return *(m_text
+ x
+ m_xChars
* y
); }
66 void SetFontSize(int fontSize
);
68 void MoveCaret(int x
, int y
);
71 void Home() { m_xCaret
= 0; }
72 void End() { m_xCaret
= m_xChars
- 1; }
73 void FirstLine() { m_yCaret
= 0; }
74 void LastLine() { m_yCaret
= m_yChars
- 1; }
75 void PrevChar() { if ( !m_xCaret
-- ) { End(); PrevLine(); } }
76 void NextChar() { if ( ++m_xCaret
== m_xChars
) { Home(); NextLine(); } }
77 void PrevLine() { if ( !m_yCaret
-- ) LastLine(); }
78 void NextLine() { if ( ++m_yCaret
== m_yChars
) FirstLine(); }
81 void OnPaint( wxPaintEvent
&event
);
82 void OnSize( wxSizeEvent
&event
);
83 void OnChar( wxKeyEvent
&event
);
86 // move the caret to m_xCaret, m_yCaret
89 // update the geometry
94 // the margin around the text (looks nicer)
95 int m_xMargin
, m_yMargin
;
97 // size (in pixels) of one character
98 long m_widthChar
, m_heightChar
;
100 // position (in text coords) of the caret
101 int m_xCaret
, m_yCaret
;
103 // the size (in text coords) of the window
104 int m_xChars
, m_yChars
;
109 DECLARE_DYNAMIC_CLASS(MyCanvas
)
110 DECLARE_EVENT_TABLE()
114 // Define a new frame type: this is going to be our main frame
115 class MyFrame
: public wxFrame
119 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
121 // event handlers (these functions should _not_ be virtual)
122 void OnQuit(wxCommandEvent
& event
);
123 void OnAbout(wxCommandEvent
& event
);
124 void OnSetBlinkTime(wxCommandEvent
& event
);
125 void OnSetFontSize(wxCommandEvent
& event
);
126 void OnCaretMove(wxCommandEvent
& event
);
131 // any class wishing to process wxWidgets events must use this macro
132 DECLARE_EVENT_TABLE()
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 // IDs for the controls and the menu commands
149 // controls start here (the numbers are, of course, arbitrary)
153 // ----------------------------------------------------------------------------
154 // event tables and other macros for wxWidgets
155 // ----------------------------------------------------------------------------
157 // the event tables connect the wxWidgets events with the functions (event
158 // handlers) which process them. It can be also done at run-time, but for the
159 // simple menu events like this the static method is much simpler.
160 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
161 EVT_MENU(Caret_Quit
, MyFrame::OnQuit
)
162 EVT_MENU(Caret_About
, MyFrame::OnAbout
)
163 EVT_MENU(Caret_SetBlinkTime
, MyFrame::OnSetBlinkTime
)
164 EVT_MENU(Caret_SetFontSize
, MyFrame::OnSetFontSize
)
165 EVT_MENU(Caret_Move
, MyFrame::OnCaretMove
)
168 // Create a new application object: this macro will allow wxWidgets to create
169 // the application object during program execution (it's better than using a
170 // static object for many reasons) and also declares the accessor function
171 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
175 // ============================================================================
177 // ============================================================================
179 // ----------------------------------------------------------------------------
180 // the application class
181 // ----------------------------------------------------------------------------
183 // `Main program' equivalent: the program execution "starts" here
186 if ( !wxApp::OnInit() )
189 // create and show the main application window
190 MyFrame
*frame
= new MyFrame(wxT("Caret wxWidgets sample"),
191 wxPoint(50, 50), wxSize(450, 340));
195 // success: wxApp::OnRun() will be called which will enter the main message
196 // loop and the application will run. If we returned false here, the
197 // application would exit immediately.
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
206 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
207 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
209 // set the frame icon
210 SetIcon(wxICON(sample
));
213 wxMenu
*menuFile
= new wxMenu
;
215 menuFile
->Append(Caret_SetBlinkTime
, wxT("&Blink time...\tCtrl-B"));
216 menuFile
->Append(Caret_SetFontSize
, wxT("&Font size...\tCtrl-S"));
217 menuFile
->Append(Caret_Move
, wxT("&Move caret\tCtrl-C"));
218 menuFile
->AppendSeparator();
219 menuFile
->Append(Caret_About
, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
220 menuFile
->AppendSeparator();
221 menuFile
->Append(Caret_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
223 // now append the freshly created menu to the menu bar...
224 wxMenuBar
*menuBar
= new wxMenuBar
;
225 menuBar
->Append(menuFile
, wxT("&File"));
227 // ... and attach this menu bar to the frame
230 m_canvas
= new MyCanvas(this);
233 // create a status bar just for fun (by default with 1 pane only)
235 SetStatusText(wxT("Welcome to wxWidgets!"));
236 #endif // wxUSE_STATUSBAR
242 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
244 // true is to force the frame to close
248 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
250 wxMessageBox(wxT("The caret wxWidgets sample.\n(c) 1999 Vadim Zeitlin"),
251 wxT("About Caret"), wxOK
| wxICON_INFORMATION
, this);
254 void MyFrame::OnCaretMove(wxCommandEvent
& WXUNUSED(event
))
256 m_canvas
->MoveCaret(10, 10);
259 void MyFrame::OnSetBlinkTime(wxCommandEvent
& WXUNUSED(event
))
261 long blinkTime
= wxGetNumberFromUser
263 wxT("The caret blink time is the time between two blinks"),
264 wxT("Time in milliseconds:"),
265 wxT("wxCaret sample"),
266 wxCaret::GetBlinkTime(), 0, 10000,
269 if ( blinkTime
!= -1 )
271 wxCaret::SetBlinkTime((int)blinkTime
);
272 m_canvas
->CreateCaret();
273 wxLogStatus(this, wxT("Blink time set to %ld milliseconds."), blinkTime
);
277 void MyFrame::OnSetFontSize(wxCommandEvent
& WXUNUSED(event
))
279 long fontSize
= wxGetNumberFromUser
281 wxT("The font size also determines the caret size so\nthis demonstrates resizing the caret."),
282 wxT("Font size (in points):"),
283 wxT("wxCaret sample"),
288 if ( fontSize
!= -1 )
290 m_canvas
->SetFontSize((int)fontSize
);
294 // ----------------------------------------------------------------------------
296 // ----------------------------------------------------------------------------
298 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
300 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
301 EVT_PAINT(MyCanvas::OnPaint
)
302 EVT_SIZE(MyCanvas::OnSize
)
303 EVT_CHAR(MyCanvas::OnChar
)
306 MyCanvas::MyCanvas( wxWindow
*parent
)
307 : wxScrolledWindow( parent
, wxID_ANY
,
308 wxDefaultPosition
, wxDefaultSize
,
311 m_text
= (wxChar
*)NULL
;
313 SetBackgroundColour(*wxWHITE
);
317 m_xCaret
= m_yCaret
=
318 m_xChars
= m_yChars
= 0;
320 m_xMargin
= m_yMargin
= 5;
325 MyCanvas::~MyCanvas()
330 void MyCanvas::CreateCaret()
332 wxCaret
*caret
= new wxCaret(this, m_widthChar
, m_heightChar
);
335 caret
->Move(m_xMargin
, m_yMargin
);
339 void MyCanvas::SetFontSize(int fontSize
)
341 m_font
= wxFont(fontSize
, wxFONTFAMILY_TELETYPE
,
342 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
346 m_heightChar
= dc
.GetCharHeight();
347 m_widthChar
= dc
.GetCharWidth();
349 wxCaret
*caret
= GetCaret();
352 caret
->SetSize(m_widthChar
, m_heightChar
);
358 void MyCanvas::MoveCaret(int x
, int y
)
366 void MyCanvas::DoMoveCaret()
368 wxLogStatus(wxT("Caret is at (%d, %d)"), m_xCaret
, m_yCaret
);
370 GetCaret()->Move(m_xMargin
+ m_xCaret
* m_widthChar
,
371 m_yMargin
+ m_yCaret
* m_heightChar
);
374 void MyCanvas::OnSize(wxSizeEvent
& event
)
381 void MyCanvas::ChangeSize()
383 wxSize size
= GetClientSize();
384 m_xChars
= (size
.x
- 2*m_xMargin
) / m_widthChar
;
385 m_yChars
= (size
.y
- 2*m_yMargin
) / m_heightChar
;
392 m_text
= (wxChar
*)calloc(m_xChars
* m_yChars
, sizeof(wxChar
));
395 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
397 if ( frame
&& frame
->GetStatusBar() )
400 msg
.Printf(wxT("Panel size is (%d, %d)"), m_xChars
, m_yChars
);
401 frame
->SetStatusText(msg
, 1);
403 #endif // wxUSE_STATUSBAR
406 // NB: this method is horrible inefficient especially because the caret
407 // needs to be redrawn often and in this case we only have to redraw
408 // the caret location and not the entire window - in a real program we
409 // would use GetUpdateRegion() and iterate over rectangles it contains
410 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
412 wxCaretSuspend
cs(this);
413 wxPaintDC
dc( this );
417 dc
.SetFont( m_font
);
419 for ( int y
= 0; y
< m_yChars
; y
++ )
423 for ( int x
= 0; x
< m_xChars
; x
++ )
425 wxChar ch
= CharAt(x
, y
);
429 dc
.DrawText(ch
, m_xMargin
+ x
* m_widthChar
,
430 m_yMargin
+ y
* m_heightChar
);
437 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
442 void MyCanvas::OnChar( wxKeyEvent
&event
)
444 switch ( event
.GetKeyCode() )
476 if ( !event
.AltDown() && wxIsprint(event
.GetKeyCode()) )
478 wxChar ch
= (wxChar
)event
.GetKeyCode();
479 CharAt(m_xCaret
, m_yCaret
) = ch
;
481 wxCaretSuspend
cs(this);
484 dc
.SetBackgroundMode(wxSOLID
); // overwrite old value
485 dc
.DrawText(ch
, m_xMargin
+ m_xCaret
* m_widthChar
,
486 m_yMargin
+ m_yCaret
* m_heightChar
);