]>
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 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "caret.cpp"
21 #pragma interface "caret.cpp"
24 // For compilers that support precompilation, includes "wx/wx.h".
25 #include <wx/wxprec.h>
31 // for all others, include the necessary headers (this file is usually all you
32 // need because it includes almost all <standard< wxWindows headers
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
44 // the application icon
45 #if defined(__WXGTK__) || defined(__WXMOTIF__)
46 #include "mondrian.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // Define a new application type, each program should derive a class from wxApp
54 class MyApp
: public wxApp
57 // override base class virtuals
58 // ----------------------------
60 // this one is called on application startup and is a good place for the app
61 // initialization (doing it here and not in the ctor allows to have an error
62 // return: if OnInit() returns false, the application terminates)
63 virtual bool OnInit();
66 // Define a new frame type: this is going to be our main frame
67 class MyFrame
: public wxFrame
71 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
73 // event handlers (these functions should _not_ be virtual)
74 void OnQuit(wxCommandEvent
& event
);
75 void OnAbout(wxCommandEvent
& event
);
78 // any class wishing to process wxWindows events must use this macro
82 // MyCanvas is a canvas on which you can type
83 class MyCanvas
: public wxScrolledWindow
87 MyCanvas( wxWindow
*parent
);
90 char& CharAt(int x
, int y
) { return *(m_text
+ x
+ m_xChars
* y
); }
93 void Home() { m_xCaret
= 0; }
94 void End() { m_xCaret
= m_xChars
- 1; }
95 void FirstLine() { m_yCaret
= 0; }
96 void LastLine() { m_yCaret
= m_yChars
- 1; }
97 void PrevChar() { if ( !m_xCaret
-- ) { End(); PrevLine(); } }
98 void NextChar() { if ( ++m_xCaret
== m_xChars
) { Home(); NextLine(); } }
99 void PrevLine() { if ( !m_yCaret
-- ) LastLine(); }
100 void NextLine() { if ( ++m_yCaret
== m_yChars
) FirstLine(); }
103 void OnPaint( wxPaintEvent
&event
);
104 void OnSize( wxSizeEvent
&event
);
105 void OnChar( wxKeyEvent
&event
);
111 // the margin around the text (looks nicer)
112 int m_xMargin
, m_yMargin
;
114 // size (in pixels) of one character
115 long m_widthChar
, m_heightChar
;
117 // position (in text coords) of the caret
118 int m_xCaret
, m_yCaret
;
120 // the size (in text coords) of the window
121 int m_xChars
, m_yChars
;
126 DECLARE_DYNAMIC_CLASS(MyCanvas
)
127 DECLARE_EVENT_TABLE()
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 // IDs for the controls and the menu commands
143 // controls start here (the numbers are, of course, arbitrary)
147 // ----------------------------------------------------------------------------
148 // event tables and other macros for wxWindows
149 // ----------------------------------------------------------------------------
151 // the event tables connect the wxWindows events with the functions (event
152 // handlers) which process them. It can be also done at run-time, but for the
153 // simple menu events like this the static method is much simpler.
154 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
155 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
156 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
159 // Create a new application object: this macro will allow wxWindows to create
160 // the application object during program execution (it's better than using a
161 // static object for many reasons) and also declares the accessor function
162 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
166 // ============================================================================
168 // ============================================================================
170 // ----------------------------------------------------------------------------
171 // the application class
172 // ----------------------------------------------------------------------------
174 // `Main program' equivalent: the program execution "starts" here
177 // Create the main application window
178 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
179 wxPoint(50, 50), wxSize(450, 340));
181 // Show it and tell the application that it's our main window
182 // @@@ what does it do exactly, in fact? is it necessary here?
186 // success: wxApp::OnRun() will be called which will enter the main message
187 // loop and the application will run. If we returned FALSE here, the
188 // application would exit immediately.
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
197 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
198 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
200 // set the frame icon
201 SetIcon(wxICON(mondrian
));
204 wxMenu
*menuFile
= new wxMenu
;
206 menuFile
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
207 menuFile
->AppendSeparator();
208 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
210 // now append the freshly created menu to the menu bar...
211 wxMenuBar
*menuBar
= new wxMenuBar
;
212 menuBar
->Append(menuFile
, "&File");
214 // ... and attach this menu bar to the frame
217 (void) new MyCanvas( this );
219 // create a status bar just for fun (by default with 1 pane only)
221 SetStatusText("Welcome to wxWindows!");
227 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
229 // TRUE is to force the frame to close
233 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
236 msg
.Printf( _T("This is the about dialog of minimal sample.\n")
240 #endif // wxBETA_NUMBER
244 #endif // wxBETA_NUMBER
247 wxMessageBox(msg
, "About Minimal", wxOK
| wxICON_INFORMATION
, this);
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
257 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
258 EVT_PAINT(MyCanvas::OnPaint
)
259 EVT_SIZE(MyCanvas::OnSize
)
260 EVT_CHAR(MyCanvas::OnChar
)
263 MyCanvas::MyCanvas( wxWindow
*parent
)
264 : wxScrolledWindow( parent
, -1,
265 wxDefaultPosition
, wxDefaultSize
,
268 m_text
= (char *)NULL
;
270 SetBackgroundColour(* wxWHITE
);
272 m_font
= *wxNORMAL_FONT
;
275 dc
.SetFont( m_font
);
276 m_heightChar
= dc
.GetCharHeight();
277 m_widthChar
= dc
.GetCharWidth();
279 m_caret
.Create( this, m_widthChar
, m_heightChar
);
281 m_xCaret
= m_yCaret
=
282 m_xChars
= m_yChars
= 0;
284 m_xMargin
= m_yMargin
= 5;
285 m_caret
.Move(m_xMargin
, m_yMargin
);
289 MyCanvas::~MyCanvas()
294 void MyCanvas::OnSize( wxSizeEvent
&event
)
296 m_xChars
= (event
.GetSize().x
- 2*m_xMargin
) / m_widthChar
;
297 m_yChars
= (event
.GetSize().y
- 2*m_yMargin
) / m_heightChar
;
304 m_text
= (char *)calloc(m_xChars
* m_yChars
, sizeof(char));
307 msg
.Printf("Panel size is (%d, %d)", m_xChars
, m_yChars
);
309 ((wxFrame
*)GetParent())->SetStatusText(msg
, 1);
314 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
316 wxPaintDC
dc( this );
319 dc
.SetFont( m_font
);
321 for ( int y
= 0; y
< m_yChars
; y
++ )
325 for ( int x
= 0; x
< m_xChars
; x
++ )
327 char ch
= CharAt(x
, y
);
333 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
337 void MyCanvas::OnChar( wxKeyEvent
&event
)
339 switch ( event
.KeyCode() )
371 if ( isprint(event
.KeyCode()) )
373 CharAt(m_xCaret
, m_yCaret
) = (char)event
.KeyCode();
383 wxLogStatus("Caret is at (%d, %d)", m_xCaret
, m_yCaret
);
385 m_caret
.Move(m_xMargin
+ m_xCaret
* m_widthChar
,
386 m_yMargin
+ m_yCaret
* m_heightChar
);