]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
wxTextFile::Close() implemented
[wxWidgets.git] / user / wxLayout / wxlwindow.cpp
1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
3 * *
4 * (C) 1998, 1999 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 # include "gui/wxMApp.h"
20 # endif // USE_PCH
21 # include "gui/wxlwindow.h"
22 #else
23 # ifdef __WXMSW__
24 # include <windows.h>
25 # undef FindWindow
26 # undef GetCharWidth
27 # undef StartDoc
28 # endif
29 # include "wxlwindow.h"
30 # include "wxlparser.h"
31 #endif
32
33 #include <ctype.h>
34 #include <wx/clipbrd.h>
35
36 #define WXLO_XOFFSET 4
37 #define WXLO_YOFFSET 4
38
39 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
40 EVT_PAINT (wxLayoutWindow::OnPaint)
41 EVT_CHAR (wxLayoutWindow::OnChar)
42 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick)
43 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick)
44 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick)
45 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST, WXLOWIN_MENU_LAST, wxLayoutWindow::OnMenu)
46 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus)
47 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus)
48 END_EVENT_TABLE()
49
50 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
51 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
52 wxHSCROLL | wxVSCROLL | wxBORDER)
53
54 {
55 m_Editable = false;
56 m_doSendEvents = false;
57 m_ViewStartX = 0; m_ViewStartY = 0;
58 m_DoPopupMenu = true;
59 m_PopupMenu = MakeFormatMenu();
60 m_memDC = new wxMemoryDC;
61 m_bitmap = new wxBitmap(4,4);
62 m_bitmapSize = wxPoint(4,4);
63 m_llist = new wxLayoutList();
64 SetWrapMargin(0);
65 wxPoint max = m_llist->GetSize();
66 SetScrollbars(10, 20 /*lineHeight*/, max.x/10+1, max.y/20+1);
67 EnableScrolling(true,true);
68 m_maxx = max.x; m_maxy = max.y;
69 SetCursor(wxCURSOR_IBEAM);
70 SetDirty();
71 }
72
73 wxLayoutWindow::~wxLayoutWindow()
74 {
75 delete m_memDC; // deletes bitmap automatically (?)
76 delete m_bitmap;
77 delete m_llist;
78 delete m_PopupMenu;
79 }
80
81 #ifdef __WXMSW__
82 long
83 wxLayoutWindow::MSWGetDlgCode()
84 {
85 // if we don't return this, we won't get OnChar() events for TABs and ENTER
86 return DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_WANTMESSAGE;
87 }
88 #endif //MSW
89
90 void
91 wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
92 {
93 wxPaintDC dc( this );
94 PrepareDC( dc );
95 SetFocus();
96
97 wxPoint findPos;
98 findPos.x = dc.DeviceToLogicalX(event.GetX());
99 findPos.y = dc.DeviceToLogicalY(event.GetY());
100
101 findPos.x -= WXLO_XOFFSET;
102 findPos.y -= WXLO_YOFFSET;
103
104 if(findPos.x < 0) findPos.x = 0;
105 if(findPos.y < 0) findPos.y = 0;
106
107 #ifdef WXLAYOUT_DEBUG
108 wxLogDebug("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
109 event.GetX(), event.GetY(), findPos.x, findPos.y);
110 #endif
111
112 m_ClickPosition = findPos;
113 wxPoint cursorPos;
114 wxLayoutObject *obj = m_llist->FindObjectScreen(dc, findPos, &cursorPos);
115
116 #ifdef WXLAYOUT_DEBUG
117 if(obj)
118 wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
119 obj->GetType());
120 else
121 wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
122 #endif
123
124 // always move cursor to mouse click:
125 if(obj && eventId == WXLOWIN_MENU_LCLICK)
126 {
127 m_llist->MoveCursorTo(cursorPos);
128 DoPaint(false);
129 }
130 if(!m_doSendEvents) // nothing to do
131 return;
132
133 // only do the menu if activated, editable and not on a clickable object
134 if(eventId == WXLOWIN_MENU_RCLICK
135 && IsEditable()
136 && (! obj || (obj && obj->GetUserData() == NULL))
137 )
138 {
139 PopupMenu(m_PopupMenu, event.GetX(), event.GetY());
140 return;
141 }
142 // find the object at this position
143 if(obj)
144 {
145 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
146 commandEvent.SetEventObject( this );
147 commandEvent.SetClientData((char *)obj);
148 GetEventHandler()->ProcessEvent(commandEvent);
149 }
150 }
151
152 /*
153 * Some simple keyboard handling.
154 */
155 void
156 wxLayoutWindow::OnChar(wxKeyEvent& event)
157 {
158 if(!IsEditable()) // do nothing
159 {
160 event.Skip();
161 return;
162 }
163
164 long keyCode = event.KeyCode();
165
166 /* First, handle control keys */
167 if(event.ControlDown() && ! event.AltDown())
168 {
169 switch(event.KeyCode())
170 {
171 case WXK_DELETE :
172 case 'd':
173 m_llist->Delete(1);
174 break;
175 case 'y':
176 m_llist->DeleteLines(1);
177 break;
178 case 'h': // like backspace
179 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
180 break;
181 case 'u':
182 m_llist->DeleteToBeginOfLine();
183 break;
184 case 'k':
185 m_llist->DeleteToEndOfLine();
186 break;
187 #ifdef WXLAYOUT_DEBUG
188 case WXK_F1:
189 m_llist->SetFont(-1,-1,-1,-1,true); // underlined
190 break;
191 #endif
192 default:
193 ;
194 }
195 }
196 // ALT only:
197 else if( event.AltDown() && ! event.ControlDown() )
198 {
199 switch(event.KeyCode())
200 {
201 case WXK_DELETE:
202 case 'd':
203 m_llist->DeleteWord();
204 break;
205 default:
206 ;
207 }
208 }
209 // no control keys:
210 else if ( ! event.AltDown() && ! event.ControlDown())
211 {
212 switch(event.KeyCode())
213 {
214 case WXK_RIGHT:
215 m_llist->MoveCursorHorizontally(1);
216 break;
217 case WXK_LEFT:
218 m_llist->MoveCursorHorizontally(-1);
219 break;
220 case WXK_UP:
221 m_llist->MoveCursorVertically(-1);
222 break;
223 case WXK_DOWN:
224 m_llist->MoveCursorVertically(1);
225 break;
226 case WXK_PRIOR:
227 m_llist->MoveCursorVertically(-20);
228 break;
229 case WXK_NEXT:
230 m_llist->MoveCursorVertically(20);
231 break;
232 case WXK_HOME:
233 m_llist->MoveCursorToBeginOfLine();
234 break;
235 case WXK_END:
236 m_llist->MoveCursorToEndOfLine();
237 break;
238 case WXK_DELETE :
239 m_llist->Delete(1);
240 break;
241 case WXK_BACK: // backspace
242 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
243 break;
244 case WXK_RETURN:
245 if(m_WrapMargin > 0)
246 m_llist->WrapLine(m_WrapMargin);
247 m_llist->LineBreak();
248 break;
249 default:
250 if((!(event.ControlDown() || event.AltDown() || event.MetaDown()))
251 && (keyCode < 256 && keyCode >= 32)
252 )
253 {
254 wxString tmp;
255 tmp += keyCode;
256 if(m_WrapMargin > 0 && isspace(keyCode))
257 m_llist->WrapLine(m_WrapMargin);
258 m_llist->Insert(tmp);
259 }
260 break;
261 }
262 }
263 SetDirty();
264 DoPaint(true); // paint and scroll to cursor
265 }
266
267 void
268 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)) // or: OnDraw(wxDC& dc)
269 {
270 m_ScrollToCursor = false;
271 InternalPaint();
272 }
273
274 void
275 wxLayoutWindow::DoPaint(bool scrollToCursor)
276 {
277 m_ScrollToCursor = scrollToCursor;
278 #ifdef __WXGTK__
279 InternalPaint();
280 #else
281 Refresh();
282 #endif
283 }
284
285 void
286 wxLayoutWindow::InternalPaint(void)
287 {
288 wxPaintDC dc( this );
289 PrepareDC( dc );
290
291 int x0,y0,x1,y1, dx, dy;
292
293 // Calculate where the top of the visible area is:
294 ViewStart(&x0,&y0);
295 GetScrollPixelsPerUnit(&dx, &dy);
296 x0 *= dx; y0 *= dy;
297
298 // Get the size of the visible window:
299 GetClientSize(&x1,&y1);
300 wxASSERT(x1 > 0);
301
302 wxASSERT(y1 > 0);
303
304 // Maybe we need to change the scrollbar sizes or positions,
305 // so layout the list and check:
306 if(IsDirty())
307 m_llist->Layout(dc);
308 // this is needed even when only the cursor moved
309 m_llist->Layout(dc,y0+y1);
310
311 if(IsDirty())
312 ResizeScrollbars();
313
314 /* Make sure that the scrollbars are at a position so that the
315 cursor is visible if we are editing. */
316 /** Scroll so that cursor is visible! */
317 if(IsEditable() && m_ScrollToCursor)
318 {
319 wxPoint cc = m_llist->GetCursorScreenPos();
320 if(cc.x < x0 || cc.y < y0
321 || cc.x >= x0+(9*x1)/10 || cc.y >= y0+(9*y1/10)) // (9*x)/10 == 90%
322 {
323 int nx, ny;
324 nx = cc.x - x1/2; if(nx < 0) nx = 0;
325 ny = cc.y - y1/2; if(ny < 0) ny = 0;
326 Scroll(nx/dx,ny/dy); // new view start
327 x0 = nx; y0 = ny;
328 }
329 }
330
331 /* Check whether the window has grown, if so, we need to reallocate
332 the bitmap to be larger. */
333 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
334 {
335 wxASSERT(m_bitmapSize.x > 0);
336 wxASSERT(m_bitmapSize.y > 0);
337
338 m_memDC->SelectObject(wxNullBitmap);
339 delete m_bitmap;
340 m_bitmapSize = wxPoint(x1,y1);
341 m_bitmap = new wxBitmap(x1,y1);
342 m_memDC->SelectObject(*m_bitmap);
343 }
344 // Device origins on the memDC are suspect, we translate manually
345 // with the translate parameter of Draw().
346 m_memDC->SetDeviceOrigin(0,0);
347 m_memDC->Clear();
348
349 // The offsets give the window a tiny border on the left and top, looks nice.
350 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
351 m_llist->Draw(*m_memDC,offset);
352 if(IsEditable())
353 m_llist->DrawCursor(*m_memDC,m_HaveFocus,offset);
354 // Now copy everything to the screen:
355 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
356
357
358 ResetDirty();
359 }
360
361 // change the range and position of scroll bars
362 void
363 wxLayoutWindow::ResizeScrollbars(bool exact)
364 {
365 wxPoint max = m_llist->GetSize();
366
367 if(max.x > m_maxx || max.y > m_maxy
368 || max.x < (7*m_maxx)/10 || max.y << (7*m_maxy)/10
369 || exact)
370 {
371 if(! exact) // add an extra 20% to the sizes to avoid future updates
372 {
373 max.x = (12*max.x)/10; // 12/20 = 120%
374 max.y = (12*max.y)/10;
375 }
376 ViewStart(&m_ViewStartX, &m_ViewStartY);
377 SetScrollbars(10, 20, max.x/10+1,max.y/20+1,m_ViewStartX,m_ViewStartY,true);
378 m_maxx = max.x; m_maxy = max.y;
379 }
380 }
381
382 void
383 wxLayoutWindow::Paste(void)
384 {
385 // Read some text
386 if (wxTheClipboard->Open())
387 {
388 wxTextDataObject data;
389 if (wxTheClipboard->IsSupported(wxDF_TEXT))
390 {
391 wxTheClipboard->GetData(&data);
392 wxLayoutImportText( m_llist, data.GetText());
393 }
394 wxTheClipboard->Close();
395 }
396 }
397
398
399 wxMenu *
400 wxLayoutWindow::MakeFormatMenu()
401 {
402 wxMenu *m = new wxMenu(_("Layout Menu"));
403
404 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
405 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
406 m->AppendSeparator();
407 m->Append(WXLOWIN_MENU_UNDERLINE_ON, _("&Underline on"),_("Activate underline mode."), false);
408 m->Append(WXLOWIN_MENU_UNDERLINE_OFF,_("&Underline off"),_("Deactivate underline mode."), false);
409 m->Append(WXLOWIN_MENU_BOLD_ON ,_("&Bold on"),_("Activate bold mode."), false);
410 m->Append(WXLOWIN_MENU_BOLD_OFF ,_("&Bold off"),_("Deactivate bold mode."), false);
411 m->Append(WXLOWIN_MENU_ITALICS_ON ,_("&Italics on"),_("Activate italics mode."), false);
412 m->Append(WXLOWIN_MENU_ITALICS_OFF ,_("&Italics off"),_("Deactivate italics mode."), false);
413 m->AppendSeparator();
414 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
415 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
416 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
417 return m;
418 }
419
420 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
421 {
422 switch (event.GetId())
423 {
424 case WXLOWIN_MENU_LARGER:
425 m_llist->SetFontLarger(); break;
426 case WXLOWIN_MENU_SMALLER:
427 m_llist->SetFontSmaller(); break;
428 case WXLOWIN_MENU_UNDERLINE_ON:
429 m_llist->SetFontUnderline(true); break;
430 case WXLOWIN_MENU_UNDERLINE_OFF:
431 m_llist->SetFontUnderline(false); break;
432 case WXLOWIN_MENU_BOLD_ON:
433 m_llist->SetFontWeight(wxBOLD); break;
434 case WXLOWIN_MENU_BOLD_OFF:
435 m_llist->SetFontWeight(wxNORMAL); break;
436 case WXLOWIN_MENU_ITALICS_ON:
437 m_llist->SetFontStyle(wxITALIC); break;
438 case WXLOWIN_MENU_ITALICS_OFF:
439 m_llist->SetFontStyle(wxNORMAL); break;
440 case WXLOWIN_MENU_ROMAN:
441 m_llist->SetFontFamily(wxROMAN); break;
442 case WXLOWIN_MENU_TYPEWRITER:
443 m_llist->SetFontFamily(wxFIXED); break;
444 case WXLOWIN_MENU_SANSSERIF:
445 m_llist->SetFontFamily(wxSWISS); break;
446 }
447 }
448
449 void
450 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
451 {
452 m_HaveFocus = true;
453 DoPaint(); // to repaint the cursor
454 }
455
456 void
457 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
458 {
459 m_HaveFocus = false;
460 DoPaint(); // to repaint the cursor
461 }