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