]>
git.saurik.com Git - wxWidgets.git/blob - samples/caret/caret.cpp
e075f5a4bf7185b3b63000d3c10da77f64fee7b2
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 // ----------------------------------------------------------------------------
32 // the application icon
33 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
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
); }
65 void SetFontSize(int fontSize
);
67 void MoveCaret(int x
, int y
);
70 void Home() { m_xCaret
= 0; }
71 void End() { m_xCaret
= m_xChars
- 1; }
72 void FirstLine() { m_yCaret
= 0; }
73 void LastLine() { m_yCaret
= m_yChars
- 1; }
74 void PrevChar() { if ( !m_xCaret
-- ) { End(); PrevLine(); } }
75 void NextChar() { if ( ++m_xCaret
== m_xChars
) { Home(); NextLine(); } }
76 void PrevLine() { if ( !m_yCaret
-- ) LastLine(); }
77 void NextLine() { if ( ++m_yCaret
== m_yChars
) FirstLine(); }
80 void OnPaint( wxPaintEvent
&event
);
81 void OnSize( wxSizeEvent
&event
);
82 void OnChar( wxKeyEvent
&event
);
85 // move the caret to m_xCaret, m_yCaret
88 // update the geometry
93 // the margin around the text (looks nicer)
94 int m_xMargin
, m_yMargin
;
96 // size (in pixels) of one character
97 long m_widthChar
, m_heightChar
;
99 // position (in text coords) of the caret
100 int m_xCaret
, m_yCaret
;
102 // the size (in text coords) of the window
103 int m_xChars
, m_yChars
;
108 DECLARE_DYNAMIC_CLASS(MyCanvas
)
109 DECLARE_EVENT_TABLE()
113 // Define a new frame type: this is going to be our main frame
114 class MyFrame
: public wxFrame
118 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
120 // event handlers (these functions should _not_ be virtual)
121 void OnQuit(wxCommandEvent
& event
);
122 void OnAbout(wxCommandEvent
& event
);
123 void OnSetBlinkTime(wxCommandEvent
& event
);
124 void OnSetFontSize(wxCommandEvent
& event
);
125 void OnCaretMove(wxCommandEvent
& event
);
130 // any class wishing to process wxWidgets events must use this macro
131 DECLARE_EVENT_TABLE()
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 // IDs for the controls and the menu commands
148 // controls start here (the numbers are, of course, arbitrary)
152 // ----------------------------------------------------------------------------
153 // event tables and other macros for wxWidgets
154 // ----------------------------------------------------------------------------
156 // the event tables connect the wxWidgets events with the functions (event
157 // handlers) which process them. It can be also done at run-time, but for the
158 // simple menu events like this the static method is much simpler.
159 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
160 EVT_MENU(Caret_Quit
, MyFrame::OnQuit
)
161 EVT_MENU(Caret_About
, MyFrame::OnAbout
)
162 EVT_MENU(Caret_SetBlinkTime
, MyFrame::OnSetBlinkTime
)
163 EVT_MENU(Caret_SetFontSize
, MyFrame::OnSetFontSize
)
164 EVT_MENU(Caret_Move
, MyFrame::OnCaretMove
)
167 // Create a new application object: this macro will allow wxWidgets to create
168 // the application object during program execution (it's better than using a
169 // static object for many reasons) and also declares the accessor function
170 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
174 // ============================================================================
176 // ============================================================================
178 // ----------------------------------------------------------------------------
179 // the application class
180 // ----------------------------------------------------------------------------
182 // `Main program' equivalent: the program execution "starts" here
185 // create and show the main application window
186 MyFrame
*frame
= new MyFrame(_T("Caret wxWidgets sample"),
187 wxPoint(50, 50), wxSize(450, 340));
191 // success: wxApp::OnRun() will be called which will enter the main message
192 // loop and the application will run. If we returned false here, the
193 // application would exit immediately.
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
202 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
203 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
205 // set the frame icon
206 SetIcon(wxICON(mondrian
));
209 wxMenu
*menuFile
= new wxMenu
;
211 menuFile
->Append(Caret_SetBlinkTime
, _T("&Blink time...\tCtrl-B"));
212 menuFile
->Append(Caret_SetFontSize
, _T("&Font size...\tCtrl-S"));
213 menuFile
->Append(Caret_Move
, _T("&Move caret\tCtrl-C"));
214 menuFile
->AppendSeparator();
215 menuFile
->Append(Caret_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
216 menuFile
->AppendSeparator();
217 menuFile
->Append(Caret_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
219 // now append the freshly created menu to the menu bar...
220 wxMenuBar
*menuBar
= new wxMenuBar
;
221 menuBar
->Append(menuFile
, _T("&File"));
223 // ... and attach this menu bar to the frame
226 m_canvas
= new MyCanvas(this);
229 // create a status bar just for fun (by default with 1 pane only)
231 SetStatusText(_T("Welcome to wxWidgets!"));
232 #endif // wxUSE_STATUSBAR
238 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
240 // true is to force the frame to close
244 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
246 wxMessageBox(_T("The caret wxWidgets sample.\n© 1999 Vadim Zeitlin"),
247 _T("About Caret"), wxOK
| wxICON_INFORMATION
, this);
250 void MyFrame::OnCaretMove(wxCommandEvent
& WXUNUSED(event
))
252 m_canvas
->MoveCaret(10, 10);
255 void MyFrame::OnSetBlinkTime(wxCommandEvent
& WXUNUSED(event
))
257 long blinkTime
= wxGetNumberFromUser
259 _T("The caret blink time is the time between two blinks"),
260 _T("Time in milliseconds:"),
261 _T("wxCaret sample"),
262 wxCaret::GetBlinkTime(), 0, 10000,
265 if ( blinkTime
!= -1 )
267 wxCaret::SetBlinkTime((int)blinkTime
);
268 m_canvas
->CreateCaret();
269 wxLogStatus(this, _T("Blink time set to %ld milliseconds."), blinkTime
);
273 void MyFrame::OnSetFontSize(wxCommandEvent
& WXUNUSED(event
))
275 long fontSize
= wxGetNumberFromUser
277 _T("The font size also determines the caret size so\nthis demonstrates resizing the caret."),
278 _T("Font size (in points):"),
279 _T("wxCaret sample"),
284 if ( fontSize
!= -1 )
286 m_canvas
->SetFontSize((int)fontSize
);
290 // ----------------------------------------------------------------------------
292 // ----------------------------------------------------------------------------
294 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
296 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
297 EVT_PAINT(MyCanvas::OnPaint
)
298 EVT_SIZE(MyCanvas::OnSize
)
299 EVT_CHAR(MyCanvas::OnChar
)
302 MyCanvas::MyCanvas( wxWindow
*parent
)
303 : wxScrolledWindow( parent
, wxID_ANY
,
304 wxDefaultPosition
, wxDefaultSize
,
307 m_text
= (wxChar
*)NULL
;
309 SetBackgroundColour(*wxWHITE
);
313 m_xCaret
= m_yCaret
=
314 m_xChars
= m_yChars
= 0;
316 m_xMargin
= m_yMargin
= 5;
321 MyCanvas::~MyCanvas()
326 void MyCanvas::CreateCaret()
328 wxCaret
*caret
= new wxCaret(this, m_widthChar
, m_heightChar
);
331 caret
->Move(m_xMargin
, m_yMargin
);
335 void MyCanvas::SetFontSize(int fontSize
)
337 m_font
= wxFont(fontSize
, wxFONTFAMILY_TELETYPE
,
338 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
);
342 m_heightChar
= dc
.GetCharHeight();
343 m_widthChar
= dc
.GetCharWidth();
345 wxCaret
*caret
= GetCaret();
348 caret
->SetSize(m_widthChar
, m_heightChar
);
354 void MyCanvas::MoveCaret(int x
, int y
)
362 void MyCanvas::DoMoveCaret()
364 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret
, m_yCaret
);
366 GetCaret()->Move(m_xMargin
+ m_xCaret
* m_widthChar
,
367 m_yMargin
+ m_yCaret
* m_heightChar
);
370 void MyCanvas::OnSize(wxSizeEvent
& event
)
377 void MyCanvas::ChangeSize()
379 wxSize size
= GetClientSize();
380 m_xChars
= (size
.x
- 2*m_xMargin
) / m_widthChar
;
381 m_yChars
= (size
.y
- 2*m_yMargin
) / m_heightChar
;
388 m_text
= (wxChar
*)calloc(m_xChars
* m_yChars
, sizeof(wxChar
));
391 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
393 if ( frame
&& frame
->GetStatusBar() )
396 msg
.Printf(_T("Panel size is (%d, %d)"), m_xChars
, m_yChars
);
397 frame
->SetStatusText(msg
, 1);
399 #endif // wxUSE_STATUSBAR
402 // NB: this method is horrible inefficient especially because the caret
403 // needs to be redrawn often and in this case we only have to redraw
404 // the caret location and not the entire window - in a real program we
405 // would use GetUpdateRegion() and iterate over rectangles it contains
406 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
408 wxCaretSuspend
cs(this);
409 wxPaintDC
dc( this );
413 dc
.SetFont( m_font
);
415 for ( int y
= 0; y
< m_yChars
; y
++ )
419 for ( int x
= 0; x
< m_xChars
; x
++ )
421 wxChar ch
= CharAt(x
, y
);
427 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
431 void MyCanvas::OnChar( wxKeyEvent
&event
)
433 switch ( event
.GetKeyCode() )
465 if ( !event
.AltDown() && wxIsprint(event
.GetKeyCode()) )
467 wxChar ch
= (wxChar
)event
.GetKeyCode();
468 CharAt(m_xCaret
, m_yCaret
) = ch
;
470 wxCaretSuspend
cs(this);
473 dc
.SetBackgroundMode(wxSOLID
); // overwrite old value
474 dc
.DrawText(ch
, m_xMargin
+ m_xCaret
* m_widthChar
,
475 m_yMargin
+ m_yCaret
* m_heightChar
);