]>
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(Minimal_Quit
, MyFrame::OnQuit
)
143 EVT_MENU(Minimal_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("Minimal wxWindows App",
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(Minimal_About
, "&About...\tCtrl-A", "Show about dialog");
194 menuFile
->AppendSeparator();
195 menuFile
->Append(Minimal_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
))
223 msg
.Printf( _T("This is the about dialog of minimal sample.\n")
227 #endif // wxBETA_NUMBER
231 #endif // wxBETA_NUMBER
234 wxMessageBox(msg
, "About Minimal", wxOK
| wxICON_INFORMATION
, this);
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
244 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
245 EVT_PAINT(MyCanvas::OnPaint
)
246 EVT_SIZE(MyCanvas::OnSize
)
247 EVT_CHAR(MyCanvas::OnChar
)
250 MyCanvas::MyCanvas( wxWindow
*parent
)
251 : wxScrolledWindow( parent
, -1,
252 wxDefaultPosition
, wxDefaultSize
,
255 m_text
= (wxChar
*)NULL
;
257 SetBackgroundColour(* wxWHITE
);
259 m_font
= *wxNORMAL_FONT
;
262 dc
.SetFont( m_font
);
263 m_heightChar
= dc
.GetCharHeight();
264 m_widthChar
= dc
.GetCharWidth();
266 wxCaret
*caret
= new wxCaret(this, m_widthChar
, m_heightChar
);
269 m_xCaret
= m_yCaret
=
270 m_xChars
= m_yChars
= 0;
272 m_xMargin
= m_yMargin
= 5;
273 caret
->Move(m_xMargin
, m_yMargin
);
277 MyCanvas::~MyCanvas()
282 void MyCanvas::OnSize( wxSizeEvent
&event
)
284 m_xChars
= (event
.GetSize().x
- 2*m_xMargin
) / m_widthChar
;
285 m_yChars
= (event
.GetSize().y
- 2*m_yMargin
) / m_heightChar
;
292 m_text
= (wxChar
*)calloc(m_xChars
* m_yChars
, sizeof(wxChar
));
294 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
296 if ( frame
&& frame
->GetStatusBar() )
299 msg
.Printf(_T("Panel size is (%d, %d)"), m_xChars
, m_yChars
);
301 frame
->SetStatusText(msg
, 1);
307 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
309 wxPaintDC
dc( this );
312 dc
.SetFont( m_font
);
314 for ( int y
= 0; y
< m_yChars
; y
++ )
318 for ( int x
= 0; x
< m_xChars
; x
++ )
320 wxChar ch
= CharAt(x
, y
);
326 dc
.DrawText( line
, m_xMargin
, m_yMargin
+ y
* m_heightChar
);
330 void MyCanvas::OnChar( wxKeyEvent
&event
)
332 switch ( event
.KeyCode() )
364 if ( wxIsprint(event
.KeyCode()) )
366 CharAt(m_xCaret
, m_yCaret
) = (wxChar
)event
.KeyCode();
376 wxLogStatus(_T("Caret is at (%d, %d)"), m_xCaret
, m_yCaret
);
378 GetCaret()->Move(m_xMargin
+ m_xCaret
* m_widthChar
,
379 m_yMargin
+ m_yCaret
* m_heightChar
);