]>
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__)
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 // Define a new frame type: this is going to be our main frame
55 class MyFrame
: public wxFrame
59 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
61 // event handlers (these functions should _not_ be virtual)
62 void OnQuit(wxCommandEvent
& event
);
63 void OnAbout(wxCommandEvent
& event
);
64 void OnSetBlinkTime(wxCommandEvent
& event
);
67 // any class wishing to process wxWindows events must use this macro
71 // MyCanvas is a canvas on which you can type
72 class MyCanvas
: public wxScrolledWindow
76 MyCanvas( wxWindow
*parent
);
79 wxChar
& CharAt(int x
, int y
) { return *(m_text
+ x
+ m_xChars
* y
); }
82 void Home() { m_xCaret
= 0; }
83 void End() { m_xCaret
= m_xChars
- 1; }
84 void FirstLine() { m_yCaret
= 0; }
85 void LastLine() { m_yCaret
= m_yChars
- 1; }
86 void PrevChar() { if ( !m_xCaret
-- ) { End(); PrevLine(); } }
87 void NextChar() { if ( ++m_xCaret
== m_xChars
) { Home(); NextLine(); } }
88 void PrevLine() { if ( !m_yCaret
-- ) LastLine(); }
89 void NextLine() { if ( ++m_yCaret
== m_yChars
) FirstLine(); }
92 void OnPaint( wxPaintEvent
&event
);
93 void OnSize( wxSizeEvent
&event
);
94 void OnChar( wxKeyEvent
&event
);
99 // the margin around the text (looks nicer)
100 int m_xMargin
, m_yMargin
;
102 // size (in pixels) of one character
103 long m_widthChar
, m_heightChar
;
105 // position (in text coords) of the caret
106 int m_xCaret
, m_yCaret
;
108 // the size (in text coords) of the window
109 int m_xChars
, m_yChars
;
114 DECLARE_DYNAMIC_CLASS(MyCanvas
)
115 DECLARE_EVENT_TABLE()
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 // IDs for the controls and the menu commands
130 // controls start here (the numbers are, of course, arbitrary)
134 // ----------------------------------------------------------------------------
135 // event tables and other macros for wxWindows
136 // ----------------------------------------------------------------------------
138 // the event tables connect the wxWindows events with the functions (event
139 // handlers) which process them. It can be also done at run-time, but for the
140 // simple menu events like this the static method is much simpler.
141 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
142 EVT_MENU(Caret_Quit
, MyFrame::OnQuit
)
143 EVT_MENU(Caret_About
, MyFrame::OnAbout
)
144 EVT_MENU(Caret_SetBlinkTime
, MyFrame::OnSetBlinkTime
)
147 // Create a new application object: this macro will allow wxWindows to create
148 // the application object during program execution (it's better than using a
149 // static object for many reasons) and also declares the accessor function
150 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
159 // the application class
160 // ----------------------------------------------------------------------------
162 // `Main program' equivalent: the program execution "starts" here
165 // Create the main application window
166 MyFrame
*frame
= new MyFrame("Caret wxWindows sample",
167 wxPoint(50, 50), wxSize(450, 340));
169 // Show it and tell the application that it's our main window
170 // @@@ what does it do exactly, in fact? is it necessary here?
174 // success: wxApp::OnRun() will be called which will enter the main message
175 // loop and the application will run. If we returned FALSE here, the
176 // application would exit immediately.
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
185 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
186 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
188 // set the frame icon
189 SetIcon(wxICON(mondrian
));
192 wxMenu
*menuFile
= new wxMenu
;
194 menuFile
->Append(Caret_SetBlinkTime
, "&Blink time...\tCtrl-B");
195 menuFile
->AppendSeparator();
196 menuFile
->Append(Caret_About
, "&About...\tCtrl-A", "Show about dialog");
197 menuFile
->AppendSeparator();
198 menuFile
->Append(Caret_Quit
, "E&xit\tAlt-X", "Quit this program");
200 // now append the freshly created menu to the menu bar...
201 wxMenuBar
*menuBar
= new wxMenuBar
;
202 menuBar
->Append(menuFile
, "&File");
204 // ... and attach this menu bar to the frame
207 (void) new MyCanvas( this );
209 // create a status bar just for fun (by default with 1 pane only)
211 SetStatusText("Welcome to wxWindows!");
217 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
219 // TRUE is to force the frame to close
223 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
225 wxMessageBox(_T("The caret wxWindows sample.\n© 1999 Vadim Zeitlin"),
226 _T("About Caret"), wxOK
| wxICON_INFORMATION
, this);
229 void MyFrame::OnSetBlinkTime(wxCommandEvent
& WXUNUSED(event
))
231 long blinkTime
= wxGetNumberFromUser
233 _T("The caret blink time is the time between two blinks"),
234 _T("Time in milliseconds:"),
235 _T("wxCaret sample"),
236 wxCaret::GetBlinkTime(), 0, 10000,
239 if ( blinkTime
!= -1 )
241 wxCaret::SetBlinkTime((int)blinkTime
);
242 wxLogStatus(this, _T("Blink time set to %ld milliseconds."), blinkTime
);
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
252 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
253 EVT_PAINT(MyCanvas::OnPaint
)
254 EVT_SIZE(MyCanvas::OnSize
)
255 EVT_CHAR(MyCanvas::OnChar
)
258 MyCanvas::MyCanvas( wxWindow
*parent
)
259 : wxScrolledWindow( parent
, -1,
260 wxDefaultPosition
, wxDefaultSize
,
263 m_text
= (wxChar
*)NULL
;
265 SetBackgroundColour(* wxWHITE
);
267 m_font
= *wxNORMAL_FONT
;
270 dc
.SetFont( m_font
);
271 m_heightChar
= dc
.GetCharHeight();
272 m_widthChar
= dc
.GetCharWidth();
274 wxCaret
*caret
= new wxCaret(this, m_widthChar
, m_heightChar
);
277 m_xCaret
= m_yCaret
=
278 m_xChars
= m_yChars
= 0;
280 m_xMargin
= m_yMargin
= 5;
281 caret
->Move(m_xMargin
, m_yMargin
);
285 MyCanvas::~MyCanvas()
290 void MyCanvas::OnSize( wxSizeEvent
&event
)
292 m_xChars
= (event
.GetSize().x
- 2*m_xMargin
) / m_widthChar
;
293 m_yChars
= (event
.GetSize().y
- 2*m_yMargin
) / m_heightChar
;
300 m_text
= (wxChar
*)calloc(m_xChars
* m_yChars
, sizeof(wxChar
));
302 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
304 if ( frame
&& frame
->GetStatusBar() )
307 msg
.Printf(_T("Panel size is (%d, %d)"), m_xChars
, m_yChars
);
309 frame
->SetStatusText(msg
, 1);
315 // NB: this method is horrible inefficient especially because the caret
316 // needs to be redrawn often and in this case we only have to redraw
317 // the caret location and not the entire window - in a real program we
318 // would use GetUpdateRegion() and iterate over rectangles it contains
319 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
321 wxPaintDC
dc( this );
325 dc
.SetFont( m_font
);
327 for ( int y
= 0; y
< m_yChars
; y
++ )
331 for ( int x
= 0; x
< m_xChars
; x
++ )
333 wxChar ch
= CharAt(x
, y
);
339 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
343 void MyCanvas::OnChar( wxKeyEvent
&event
)
345 switch ( event
.KeyCode() )
377 if ( !event
.AltDown() && wxIsprint(event
.KeyCode()) )
379 CharAt(m_xCaret
, m_yCaret
) = (wxChar
)event
.KeyCode();
391 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret
, m_yCaret
);
393 GetCaret()->Move(m_xMargin
+ m_xCaret
* m_widthChar
,
394 m_yMargin
+ m_yCaret
* m_heightChar
);