]>
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
);
66 // any class wishing to process wxWindows events must use this macro
70 // MyCanvas is a canvas on which you can type
71 class MyCanvas
: public wxScrolledWindow
75 MyCanvas( wxWindow
*parent
);
78 wxChar
& CharAt(int x
, int y
) { return *(m_text
+ x
+ m_xChars
* y
); }
81 void Home() { m_xCaret
= 0; }
82 void End() { m_xCaret
= m_xChars
- 1; }
83 void FirstLine() { m_yCaret
= 0; }
84 void LastLine() { m_yCaret
= m_yChars
- 1; }
85 void PrevChar() { if ( !m_xCaret
-- ) { End(); PrevLine(); } }
86 void NextChar() { if ( ++m_xCaret
== m_xChars
) { Home(); NextLine(); } }
87 void PrevLine() { if ( !m_yCaret
-- ) LastLine(); }
88 void NextLine() { if ( ++m_yCaret
== m_yChars
) FirstLine(); }
91 void OnPaint( wxPaintEvent
&event
);
92 void OnSize( wxSizeEvent
&event
);
93 void OnChar( wxKeyEvent
&event
);
98 // the margin around the text (looks nicer)
99 int m_xMargin
, m_yMargin
;
101 // size (in pixels) of one character
102 long m_widthChar
, m_heightChar
;
104 // position (in text coords) of the caret
105 int m_xCaret
, m_yCaret
;
107 // the size (in text coords) of the window
108 int m_xChars
, m_yChars
;
113 DECLARE_DYNAMIC_CLASS(MyCanvas
)
114 DECLARE_EVENT_TABLE()
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 // 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
)
146 // Create a new application object: this macro will allow wxWindows to create
147 // the application object during program execution (it's better than using a
148 // static object for many reasons) and also declares the accessor function
149 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
153 // ============================================================================
155 // ============================================================================
157 // ----------------------------------------------------------------------------
158 // the application class
159 // ----------------------------------------------------------------------------
161 // `Main program' equivalent: the program execution "starts" here
164 // Create the main application window
165 MyFrame
*frame
= new MyFrame("Caret wxWindows sample",
166 wxPoint(50, 50), wxSize(450, 340));
168 // Show it and tell the application that it's our main window
169 // @@@ what does it do exactly, in fact? is it necessary here?
173 // success: wxApp::OnRun() will be called which will enter the main message
174 // loop and the application will run. If we returned FALSE here, the
175 // application would exit immediately.
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
184 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
185 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
187 // set the frame icon
188 SetIcon(wxICON(mondrian
));
191 wxMenu
*menuFile
= new wxMenu
;
193 menuFile
->Append(Caret_About
, "&About...\tCtrl-A", "Show about dialog");
194 menuFile
->AppendSeparator();
195 menuFile
->Append(Caret_Quit
, "E&xit\tAlt-X", "Quit this program");
197 // now append the freshly created menu to the menu bar...
198 wxMenuBar
*menuBar
= new wxMenuBar
;
199 menuBar
->Append(menuFile
, "&File");
201 // ... and attach this menu bar to the frame
204 (void) new MyCanvas( this );
206 // create a status bar just for fun (by default with 1 pane only)
208 SetStatusText("Welcome to wxWindows!");
214 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
216 // TRUE is to force the frame to close
220 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
222 wxMessageBox(_T("This is the about dialog of caret sample."), "About Caret", wxOK
| wxICON_INFORMATION
, this);
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
232 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
233 EVT_PAINT(MyCanvas::OnPaint
)
234 EVT_SIZE(MyCanvas::OnSize
)
235 EVT_CHAR(MyCanvas::OnChar
)
238 MyCanvas::MyCanvas( wxWindow
*parent
)
239 : wxScrolledWindow( parent
, -1,
240 wxDefaultPosition
, wxDefaultSize
,
243 m_text
= (wxChar
*)NULL
;
245 SetBackgroundColour(* wxWHITE
);
247 m_font
= *wxNORMAL_FONT
;
250 dc
.SetFont( m_font
);
251 m_heightChar
= dc
.GetCharHeight();
252 m_widthChar
= dc
.GetCharWidth();
254 wxCaret
*caret
= new wxCaret(this, m_widthChar
, m_heightChar
);
257 m_xCaret
= m_yCaret
=
258 m_xChars
= m_yChars
= 0;
260 m_xMargin
= m_yMargin
= 5;
261 caret
->Move(m_xMargin
, m_yMargin
);
265 MyCanvas::~MyCanvas()
270 void MyCanvas::OnSize( wxSizeEvent
&event
)
272 m_xChars
= (event
.GetSize().x
- 2*m_xMargin
) / m_widthChar
;
273 m_yChars
= (event
.GetSize().y
- 2*m_yMargin
) / m_heightChar
;
280 m_text
= (wxChar
*)calloc(m_xChars
* m_yChars
, sizeof(wxChar
));
282 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
284 if ( frame
&& frame
->GetStatusBar() )
287 msg
.Printf(_T("Panel size is (%d, %d)"), m_xChars
, m_yChars
);
289 frame
->SetStatusText(msg
, 1);
295 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
297 wxPaintDC
dc( this );
300 dc
.SetFont( m_font
);
302 for ( int y
= 0; y
< m_yChars
; y
++ )
306 for ( int x
= 0; x
< m_xChars
; x
++ )
308 wxChar ch
= CharAt(x
, y
);
314 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
318 void MyCanvas::OnChar( wxKeyEvent
&event
)
320 switch ( event
.KeyCode() )
352 if ( !event
.AltDown() && wxIsprint(event
.KeyCode()) )
354 CharAt(m_xCaret
, m_yCaret
) = (wxChar
)event
.KeyCode();
366 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret
, m_yCaret
);
368 GetCaret()->Move(m_xMargin
+ m_xCaret
* m_widthChar
,
369 m_yMargin
+ m_yCaret
* m_heightChar
);