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 wxChar
& 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
);
110 // the margin around the text (looks nicer)
111 int m_xMargin
, m_yMargin
;
113 // size (in pixels) of one character
114 long m_widthChar
, m_heightChar
;
116 // position (in text coords) of the caret
117 int m_xCaret
, m_yCaret
;
119 // the size (in text coords) of the window
120 int m_xChars
, m_yChars
;
125 DECLARE_DYNAMIC_CLASS(MyCanvas
)
126 DECLARE_EVENT_TABLE()
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 // IDs for the controls and the menu commands
142 // controls start here (the numbers are, of course, arbitrary)
146 // ----------------------------------------------------------------------------
147 // event tables and other macros for wxWindows
148 // ----------------------------------------------------------------------------
150 // the event tables connect the wxWindows events with the functions (event
151 // handlers) which process them. It can be also done at run-time, but for the
152 // simple menu events like this the static method is much simpler.
153 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
154 EVT_MENU(Minimal_Quit
, MyFrame
::OnQuit
)
155 EVT_MENU(Minimal_About
, MyFrame
::OnAbout
)
158 // Create a new application object: this macro will allow wxWindows to create
159 // the application object during program execution (it's better than using a
160 // static object for many reasons) and also declares the accessor function
161 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
165 // ============================================================================
167 // ============================================================================
169 // ----------------------------------------------------------------------------
170 // the application class
171 // ----------------------------------------------------------------------------
173 // `Main program' equivalent: the program execution "starts" here
176 // Create the main application window
177 MyFrame
*frame
= new MyFrame("Minimal wxWindows App",
178 wxPoint(50, 50), wxSize(450, 340));
180 // Show it and tell the application that it's our main window
181 // @@@ what does it do exactly, in fact? is it necessary here?
185 // success: wxApp::OnRun() will be called which will enter the main message
186 // loop and the application will run. If we returned FALSE here, the
187 // application would exit immediately.
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
196 MyFrame
::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
197 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
199 // set the frame icon
200 SetIcon(wxICON(mondrian
));
203 wxMenu
*menuFile
= new wxMenu
;
205 menuFile
->Append(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
206 menuFile
->AppendSeparator();
207 menuFile
->Append(Minimal_Quit
, "E&xit\tAlt-X", "Quit this program");
209 // now append the freshly created menu to the menu bar...
210 wxMenuBar
*menuBar
= new wxMenuBar
;
211 menuBar
->Append(menuFile
, "&File");
213 // ... and attach this menu bar to the frame
216 (void) new MyCanvas( this );
218 // create a status bar just for fun (by default with 1 pane only)
220 SetStatusText("Welcome to wxWindows!");
226 void MyFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
228 // TRUE is to force the frame to close
232 void MyFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
235 msg
.Printf( _T("This is the about dialog of minimal sample.\n")
239 #endif // wxBETA_NUMBER
243 #endif // wxBETA_NUMBER
246 wxMessageBox(msg
, "About Minimal", wxOK
| wxICON_INFORMATION
, this);
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
254 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
256 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
257 EVT_PAINT(MyCanvas
::OnPaint
)
258 EVT_SIZE(MyCanvas
::OnSize
)
259 EVT_CHAR(MyCanvas
::OnChar
)
262 MyCanvas
::MyCanvas( wxWindow
*parent
)
263 : wxScrolledWindow( parent
, -1,
264 wxDefaultPosition
, wxDefaultSize
,
267 m_text
= (wxChar
*)NULL
;
269 SetBackgroundColour(* wxWHITE
);
271 m_font
= *wxNORMAL_FONT
;
274 dc
.SetFont( m_font
);
275 m_heightChar
= dc
.GetCharHeight();
276 m_widthChar
= dc
.GetCharWidth();
278 wxCaret
*caret
= new wxCaret(this, m_widthChar
, m_heightChar
);
281 m_xCaret
= m_yCaret
=
282 m_xChars
= m_yChars
= 0;
284 m_xMargin
= m_yMargin
= 5;
285 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
= (wxChar
*)calloc(m_xChars
* m_yChars
, sizeof(wxChar
));
306 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
308 if ( frame
&& frame
->GetStatusBar() )
311 msg
.Printf(_T("Panel size is (%d, %d)"), m_xChars
, m_yChars
);
313 frame
->SetStatusText(msg
, 1);
319 void MyCanvas
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
321 wxPaintDC
dc( this );
324 dc
.SetFont( m_font
);
326 for ( int y
= 0; y
< m_yChars
; y
++ )
330 for ( int x
= 0; x
< m_xChars
; x
++ )
332 wxChar ch
= CharAt(x
, y
);
338 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
342 void MyCanvas
::OnChar( wxKeyEvent
&event
)
344 switch ( event
.KeyCode() )
376 if ( wxIsprint(event
.KeyCode()) )
378 CharAt(m_xCaret
, m_yCaret
) = (wxChar
)event
.KeyCode();
388 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret
, m_yCaret
);
390 GetCaret()->Move(m_xMargin
+ m_xCaret
* m_widthChar
,
391 m_yMargin
+ m_yCaret
* m_heightChar
);