]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
828ac67a71fc0d2f76c1e17c0db2b90ec93e210d
[wxWidgets.git] / user / wxLayout / wxlwindow.cpp
1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8
9 #ifdef __GNUG__
10 # pragma implementation "wxlwindow.h"
11 #endif
12
13 //#include "Mpch.h"
14
15 #ifdef M_BASEDIR
16 # ifndef USE_PCH
17 # include "Mcommon.h"
18 # include "gui/wxMenuDefs.h"
19 # endif // USE_PCH
20 # include "gui/wxlwindow.h"
21 #else
22 # ifdef __WXMSW__
23 # include <windows.h>
24
25 # undef FindWindow
26 # undef GetCharWidth
27 # undef StartDoc
28 # endif
29
30 # include "wxlwindow.h"
31 # define TRACEMESSAGE(x)
32 #endif
33 # define WXL_VAR(x) cerr << #x " = " << x ;
34
35 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
36 EVT_PAINT (wxLayoutWindow::OnPaint)
37 EVT_CHAR (wxLayoutWindow::OnChar)
38
39 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick)
40 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick)
41 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick)
42 END_EVENT_TABLE()
43
44 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
45 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
46 wxHSCROLL | wxVSCROLL | wxBORDER)
47
48 {
49 m_ScrollbarsSet = false;
50 m_doSendEvents = false;
51 m_ViewStartX = 0; m_ViewStartY = 0;
52
53
54 CoordType
55 max_x, max_y, lineHeight;
56 m_llist.GetSize(&max_x, &max_y, &lineHeight);
57 SetScrollbars(10, lineHeight, max_x/10+1, max_y/lineHeight+1);
58 EnableScrolling(true,true);
59 }
60
61 #ifdef __WXMSW__
62 long
63 wxLayoutWindow::MSWGetDlgCode()
64 {
65 // if we don't return this, we won't get OnChar() events
66 return DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_WANTMESSAGE;
67 }
68 #endif //MSW
69
70 void
71 wxLayoutWindow::Update(void)
72 {
73 wxClientDC dc(this);
74 PrepareDC(dc);
75 if(IsDirty())
76 {
77 DoPaint(dc);
78 UpdateScrollbars();
79 ResetDirty();
80 }
81 m_llist.DrawCursor(dc);
82 }
83
84 void
85 wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
86 {
87 if(!m_doSendEvents) // nothing to do
88 return;
89
90 wxPaintDC dc( this );
91 PrepareDC( dc );
92 SetFocus();
93
94 wxPoint findPos;
95 findPos.x = dc.DeviceToLogicalX(event.GetX());
96 findPos.y = dc.DeviceToLogicalY(event.GetY());
97
98 TRACEMESSAGE(("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
99 event.GetX(), event.GetY(), findPos.x, findPos.y));
100
101 // find the object at this position
102 wxLayoutObjectBase *obj = m_llist.Find(findPos);
103 if(obj)
104 {
105 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, eventId);
106 commandEvent.SetEventObject( this );
107 commandEvent.SetClientData((char *)obj);
108 GetEventHandler()->ProcessEvent(commandEvent);
109 }
110 }
111
112 /*
113 * some simple keyboard handling
114 */
115 void
116 wxLayoutWindow::OnChar(wxKeyEvent& event)
117 {
118 if(! m_llist.IsEditable()) // do nothing
119 {
120 event.Skip();
121 return;
122 }
123
124 long keyCode = event.KeyCode();
125 wxPoint p;
126 CoordType help;
127
128 switch(event.KeyCode())
129 {
130 case WXK_RIGHT:
131 m_llist.MoveCursor(1);
132 break;
133 case WXK_LEFT:
134 m_llist.MoveCursor(-1);
135 break;
136 case WXK_UP:
137 m_llist.MoveCursor(0,-1);
138 break;
139 case WXK_DOWN:
140 m_llist.MoveCursor(0,1);
141 break;
142 case WXK_PRIOR:
143 m_llist.MoveCursor(0,-20);
144 break;
145 case WXK_NEXT:
146 m_llist.MoveCursor(0,20);
147 break;
148 case WXK_HOME:
149 p = m_llist.GetCursor();
150 p.x = 0;
151 m_llist.SetCursor(p);
152 break;
153 case WXK_END:
154 p = m_llist.GetCursor();
155 p.x = m_llist.GetLineLength(m_llist.FindCurrentObject(NULL));
156 m_llist.SetCursor(p);
157 break;
158 case WXK_DELETE :
159 if(event.ControlDown()) // delete to end of line
160 {
161 help = m_llist.GetLineLength(
162 m_llist.FindCurrentObject(NULL))
163 - m_llist.GetCursor().x;
164 m_llist.Delete(help ? help : 1);
165 }
166 else
167 m_llist.Delete(1);
168 break;
169 case WXK_BACK: // backspace
170 if(m_llist.MoveCursor(-1)) {
171 m_llist.Delete(1);
172 }
173 break;
174 case WXK_RETURN:
175 m_llist.LineBreak();
176 break;
177
178 #ifdef WXLAYOUT_DEBUG
179 case WXK_F1:
180 m_llist.Debug();
181 break;
182 #endif
183
184 default:
185 if(keyCode < 256 && keyCode >= 32)
186 {
187 String tmp;
188 tmp += keyCode;
189 m_llist.Insert(tmp);
190 }
191 break;
192 }
193
194 /** Scroll so that cursor is visible! */
195 int x0,y0,x1,y1,ux,uy;
196 ViewStart(&x0,&y0);
197 GetScrollPixelsPerUnit(&ux,&uy);
198 x0*=ux; y0*=uy;
199 GetClientSize(&x1,&y1);
200
201 wxPoint cc = m_llist.GetCursorCoords();
202 int nx = x0, ny = y0;
203 // when within 10% of borders, scroll to center
204 if(cc.y > y0+(9*y1)/10)
205 ny = cc.y - y1/5;
206 else if (cc.y < y0+y1/10)
207 {
208 ny = cc.y-y1/2;
209 if(ny < 0) ny = 0;
210 }
211 if(cc.x > x0+(9*x1)/10)
212 nx = cc.x - x1/5;
213 else if (cc.x < x0+x1/10)
214 {
215 nx = cc.x-x1/2;
216 if(nx < 0) nx = 0;
217 }
218 Scroll(nx,ny);
219
220 Update();
221 }
222
223 void
224 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)) // or: OnDraw(wxDC& dc)
225 {
226 wxPaintDC dc( this );
227 PrepareDC( dc );
228
229 DoPaint(dc);
230
231 // wxGTK: wxMemoryDC broken?
232 #if 0
233 int x0,y0,x1,y1;
234 ViewStart(&x0,&y0);
235 GetSize(&x1,&y1);
236 WXL_VAR(x0); WXL_VAR(y0);
237 WXL_VAR(x1); WXL_VAR(y1);
238
239 wxMemoryDC(memdc);
240 wxBitmap bm(x1,y1);
241 memdc.SelectObject(bm);
242
243 // make temporary copy and edit this
244 memdc.SetDeviceOrigin(x0,y0);
245 memdc.Blit(x0,y0,x1,y1,&dc,x0,y0,wxCOPY,FALSE);
246 DoPaint(memdc);
247 // blit it back
248 dc.Blit(x0,y0,x1,y1,&memdc,x0,y0,wxCOPY,FALSE);
249 #endif
250
251 }
252
253 // does the actual painting
254 void
255 wxLayoutWindow::DoPaint(wxDC &dc)
256 {
257 m_llist.EraseAndDraw(dc);
258 m_llist.DrawCursor(dc);
259 // FIXME: not strictly correct, this does only work for changes behind
260 // the cursor position, not complete redraws
261
262 if(! m_ScrollbarsSet)
263 {
264 m_ScrollbarsSet = true; // avoid recursion
265 UpdateScrollbars();
266 }
267 }
268
269 void
270 wxLayoutWindow::UpdateScrollbars(void)
271 {
272 CoordType
273 max_x, max_y, lineHeight;
274
275 ViewStart(&m_ViewStartX, &m_ViewStartY);
276 m_llist.GetSize(&max_x, &max_y, &lineHeight);
277 SetScrollbars(10, lineHeight, max_x/10+1, max_y/lineHeight+1,m_ViewStartX,m_ViewStartY,true);
278 //EnableScrolling(true,true);
279 //Scroll(m_ViewStartX, m_ViewStartY);
280 }
281
282 void
283 wxLayoutWindow::Print(void)
284 {
285 wxPostScriptDC dc("layout.ps",true,this);
286 if (dc.Ok() && dc.StartDoc((char *)_("Printing message...")))
287 {
288 //dc.SetUserScale(1.0, 1.0);
289 m_llist.Draw(dc);
290 dc.EndDoc();
291 }
292 }