]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
1c4564756317a63cd1986ad132747d4e16cc9d47
[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 "wx/wxprec.h"
14 #ifdef __BORLANDC__
15 # pragma hdrstop
16 #endif
17
18
19 //#include "Mpch.h"
20 #ifdef M_BASEDIR
21 # ifndef USE_PCH
22 # include "Mcommon.h"
23 # include "gui/wxMenuDefs.h"
24 # include "gui/wxMApp.h"
25 # endif // USE_PCH
26 # include "gui/wxlwindow.h"
27 # include "gui/wxlparser.h"
28 #else
29 # ifdef __WXMSW__
30 # include <windows.h>
31 # undef FindWindow
32 # undef GetCharWidth
33 # undef StartDoc
34 # endif
35
36 # include "wxlwindow.h"
37 # include "wxlparser.h"
38 #endif
39
40 #include <wx/clipbrd.h>
41 #include <wx/textctrl.h>
42 #include <wx/dataobj.h>
43
44 #include <ctype.h>
45
46 /// offsets to put a nice frame around text
47 #define WXLO_XOFFSET 4
48 #define WXLO_YOFFSET 4
49
50 /// offset to the right and bottom for when to redraw scrollbars
51 #define WXLO_ROFFSET 20
52 #define WXLO_BOFFSET 20
53
54 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
55 EVT_PAINT (wxLayoutWindow::OnPaint)
56 EVT_CHAR (wxLayoutWindow::OnChar)
57 EVT_KEY_UP (wxLayoutWindow::OnKeyUp)
58 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick)
59 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick)
60 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick)
61 EVT_MOTION (wxLayoutWindow::OnMouseMove)
62 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST, WXLOWIN_MENU_LAST, wxLayoutWindow::OnMenu)
63 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus)
64 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus)
65 END_EVENT_TABLE()
66
67 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
68 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
69 wxHSCROLL | wxVSCROLL | wxBORDER)
70
71 {
72 m_Editable = false;
73 m_doSendEvents = false;
74 m_ViewStartX = 0; m_ViewStartY = 0;
75 m_DoPopupMenu = true;
76 m_PopupMenu = MakeFormatMenu();
77 m_memDC = new wxMemoryDC;
78 m_bitmap = new wxBitmap(4,4);
79 m_bitmapSize = wxPoint(4,4);
80 m_llist = new wxLayoutList();
81 m_BGbitmap = NULL;
82 m_ScrollToCursor = false;
83 SetWrapMargin(0);
84 wxPoint max = m_llist->GetSize();
85 SetScrollbars(10, 20 /*lineHeight*/, max.x/10+1, max.y/20+1);
86 EnableScrolling(true,true);
87 m_maxx = max.x; m_maxy = max.y;
88 m_Selecting = false;
89 SetCursor(wxCURSOR_IBEAM);
90 SetDirty();
91 }
92
93 wxLayoutWindow::~wxLayoutWindow()
94 {
95 delete m_memDC; // deletes bitmap automatically (?)
96 delete m_bitmap;
97 delete m_llist;
98 delete m_PopupMenu;
99 SetBackgroundBitmap(NULL);
100 }
101
102 #ifdef __WXMSW__
103 long
104 wxLayoutWindow::MSWGetDlgCode()
105 {
106 // if we don't return this, we won't get OnChar() events for TABs and ENTER
107 return DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_WANTMESSAGE;
108 }
109 #endif //MSW
110
111 void
112 wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
113 {
114 wxPaintDC dc( this );
115 PrepareDC( dc );
116 SetFocus();
117
118
119 wxPoint findPos;
120 findPos.x = dc.DeviceToLogicalX(event.GetX());
121 findPos.y = dc.DeviceToLogicalY(event.GetY());
122
123 findPos.x -= WXLO_XOFFSET;
124 findPos.y -= WXLO_YOFFSET;
125
126 if(findPos.x < 0) findPos.x = 0;
127 if(findPos.y < 0) findPos.y = 0;
128
129 m_ClickPosition = wxPoint(event.GetX(), event.GetY());
130 #ifdef WXLAYOUT_DEBUG
131 // wxLogDebug("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
132 // event.GetX(), event.GetY(), findPos.x, findPos.y);
133 #endif
134
135 wxPoint cursorPos;
136 bool found;
137 wxLayoutObject *obj = m_llist->FindObjectScreen(dc, findPos,
138 &cursorPos, &found);
139
140 #ifdef WXLAYOUT_DEBUG
141 // if(obj)
142 // wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
143 // obj->GetType());
144 // else
145 // wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
146 #endif
147
148 //has the mouse only been moved?
149 if(eventId == WXLOWIN_MENU_MOUSEMOVE)
150 {
151 // found is only true if we are really over an object, not just
152 // behind it
153 if(found && obj && obj->GetUserData() != NULL)
154 {
155 if(!m_HandCursor)
156 SetCursor(wxCURSOR_HAND);
157 m_HandCursor = TRUE;
158 }
159 else
160 {
161 if(m_HandCursor)
162 SetCursor(wxCURSOR_IBEAM);
163 m_HandCursor = FALSE;
164 }
165 return;
166 }
167
168 // always move cursor to mouse click:
169 if(obj && eventId == WXLOWIN_MENU_LCLICK)
170 {
171 m_llist->MoveCursorTo(cursorPos);
172 m_ScrollToCursor = true; //FIXME: needed? DoPaint(m_llist->GetUpdateRect());
173 }
174 if(!m_doSendEvents) // nothing to do
175 return;
176
177 // only do the menu if activated, editable and not on a clickable object
178 if(eventId == WXLOWIN_MENU_RCLICK
179 && IsEditable()
180 && (! obj || (obj && obj->GetUserData() == NULL))
181 )
182 {
183 PopupMenu(m_PopupMenu, m_ClickPosition.x, m_ClickPosition.y);
184 return;
185 }
186 // find the object at this position
187 if(obj)
188 {
189 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
190 commandEvent.SetEventObject( this );
191 commandEvent.SetClientData((char *)obj);
192 GetEventHandler()->ProcessEvent(commandEvent);
193 }
194 }
195
196 /*
197 * Some simple keyboard handling.
198 */
199 void
200 wxLayoutWindow::OnChar(wxKeyEvent& event)
201 {
202 #ifdef WXLAYOUT_DEBUG
203 if(event.KeyCode() == WXK_F1)
204 {
205 m_llist->Debug();
206 return;
207 }
208 #endif
209
210 if(!IsEditable()) // do nothing
211 {
212 event.Skip();
213 return;
214 }
215
216 long keyCode = event.KeyCode();
217 if(event.ShiftDown())
218 m_Selecting = true;
219 else
220 {
221 if(m_Selecting)
222 m_llist->EndSelection();
223 m_Selecting = false;
224 }
225 /* First, handle control keys */
226 if(event.ControlDown() && ! event.AltDown())
227 {
228 switch(keyCode)
229 {
230 case WXK_DELETE :
231 case 'd':
232 m_llist->Delete(1);
233 break;
234 case 'y':
235 m_llist->DeleteLines(1);
236 break;
237 case 'h': // like backspace
238 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
239 break;
240 case 'u':
241 m_llist->DeleteToBeginOfLine();
242 break;
243 case 'k':
244 m_llist->DeleteToEndOfLine();
245 break;
246 #ifdef WXLAYOUT_DEBUG
247 case WXK_F1:
248 m_llist->SetFont(-1,-1,-1,-1,true); // underlined
249 break;
250 #endif
251 default:
252 ;
253 }
254 }
255 // ALT only:
256 else if( event.AltDown() && ! event.ControlDown() )
257 {
258 switch(keyCode)
259 {
260 case WXK_DELETE:
261 case 'd':
262 m_llist->DeleteWord();
263 break;
264 default:
265 ;
266 }
267 }
268 // no control keys:
269 else if ( ! event.AltDown() && ! event.ControlDown())
270 {
271 switch(keyCode)
272 {
273 case WXK_RIGHT:
274 if(m_Selecting) m_llist->StartSelection();
275 m_llist->MoveCursorHorizontally(1);
276 break;
277 case WXK_LEFT:
278 if(m_Selecting) m_llist->StartSelection();
279 m_llist->MoveCursorHorizontally(-1);
280 break;
281 case WXK_UP:
282 if(m_Selecting) m_llist->StartSelection();
283 m_llist->MoveCursorVertically(-1);
284 break;
285 case WXK_DOWN:
286 if(m_Selecting) m_llist->StartSelection();
287 m_llist->MoveCursorVertically(1);
288 break;
289 case WXK_PRIOR:
290 if(m_Selecting) m_llist->StartSelection();
291 m_llist->MoveCursorVertically(-20);
292 break;
293 case WXK_NEXT:
294 if(m_Selecting) m_llist->StartSelection();
295 m_llist->MoveCursorVertically(20);
296 break;
297 case WXK_HOME:
298 if(m_Selecting) m_llist->StartSelection();
299 m_llist->MoveCursorToBeginOfLine();
300 break;
301 case WXK_END:
302 if(m_Selecting) m_llist->StartSelection();
303 m_llist->MoveCursorToEndOfLine();
304 break;
305 case WXK_DELETE :
306 m_llist->Delete(1);
307 break;
308 case WXK_BACK: // backspace
309 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
310 break;
311 case WXK_RETURN:
312 if(m_WrapMargin > 0)
313 m_llist->WrapLine(m_WrapMargin);
314 m_llist->LineBreak();
315 break;
316 default:
317 if((!(event.ControlDown() || event.AltDown() || event.MetaDown()))
318 && (keyCode < 256 && keyCode >= 32)
319 )
320 {
321 wxString tmp;
322 tmp += keyCode;
323 if(m_WrapMargin > 0 && isspace(keyCode))
324 m_llist->WrapLine(m_WrapMargin);
325 m_llist->Insert(tmp);
326 }
327 break;
328 }
329 }
330 SetDirty();
331 SetModified();
332 m_ScrollToCursor = true;
333 //DoPaint(true); // paint and scroll to cursor
334 wxRect r = *m_llist->GetUpdateRect();
335 Refresh( FALSE, &r);
336 }
337
338 void
339 wxLayoutWindow::OnKeyUp(wxKeyEvent& event)
340 {
341 if(event.KeyCode() == WXK_SHIFT && m_llist->IsSelecting())
342 m_llist->EndSelection();
343 event.Skip();
344 }
345
346 void
347 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)) // or: OnDraw(wxDC& dc)
348 {
349 wxRect region = GetUpdateRegion().GetBox();
350 InternalPaint(& region);
351 }
352
353 void
354 wxLayoutWindow::DoPaint(const wxRect *updateRect)
355 {
356 #ifdef __WXGTK__
357 InternalPaint(updateRect);
358 #else
359 Refresh(FALSE, updateRect); // Causes bad flicker under wxGTK!!!
360 #endif
361 }
362
363 void
364 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
365 {
366 wxPaintDC dc( this );
367 PrepareDC( dc );
368
369 int x0,y0,x1,y1, dx, dy;
370
371 // Calculate where the top of the visible area is:
372 ViewStart(&x0,&y0);
373 GetScrollPixelsPerUnit(&dx, &dy);
374 x0 *= dx; y0 *= dy;
375
376 // Get the size of the visible window:
377 GetClientSize(&x1,&y1);
378 wxASSERT(x1 > 0);
379 wxASSERT(y1 > 0);
380 // As we have the values anyway, use them to avoid unnecessary
381 // scrollbar updates.
382 if(x1 > m_maxx) m_maxx = x1;
383 if(y1 > m_maxy) m_maxy = y1;
384
385
386 //m_llist->InvalidateUpdateRect();
387 //const wxRect *r = m_llist->GetUpdateRect();
388 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
389 updateRect->x, updateRect->y, updateRect->x+updateRect->width, updateRect->y+updateRect->height);
390
391 #if 0
392 //FIXME: we should never need to call Layout at all because the
393 // list does it automatically.
394 // Maybe we need to change the scrollbar sizes or positions,
395 // so layout the list and check:
396 if(IsDirty())
397 m_llist->Layout(dc);
398 wxLogDebug("Update rect after calling Layout: %ld,%ld / %ld,%ld",
399 r->x, r->y, r->x+r->width, r->y+r->height);
400 // this is needed even when only the cursor moved
401 m_llist->Layout(dc,y0+y1);
402 wxLogDebug("Update rect after calling Layout again: %ld,%ld / %ld,%ld",
403 r->x, r->y, r->x+r->width, r->y+r->height);
404 #endif
405
406 if(IsDirty())
407 ResizeScrollbars();
408
409 /* Make sure that the scrollbars are at a position so that the
410 cursor is visible if we are editing. */
411 /** Scroll so that cursor is visible! */
412 wxLogDebug("m_ScrollToCursor = %d", (int) m_ScrollToCursor);
413 if(IsEditable() && m_ScrollToCursor)
414 {
415 wxPoint cc = m_llist->GetCursorScreenPos(*m_memDC);
416 if(cc.x < x0 || cc.y < y0
417 || cc.x >= x0+(9*x1)/10 || cc.y >= y0+(9*y1/10)) // (9*x)/10 == 90%
418 {
419 int nx, ny;
420 nx = cc.x - x1/2; if(nx < 0) nx = 0;
421 ny = cc.y - y1/2; if(ny < 0) ny = 0;
422 Scroll(nx/dx,ny/dy); // new view start
423 x0 = nx; y0 = ny;
424 }
425 }
426
427 /* Check whether the window has grown, if so, we need to reallocate
428 the bitmap to be larger. */
429 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
430 {
431 wxASSERT(m_bitmapSize.x > 0);
432 wxASSERT(m_bitmapSize.y > 0);
433
434 m_memDC->SelectObject(wxNullBitmap);
435 delete m_bitmap;
436 m_bitmapSize = wxPoint(x1,y1);
437 m_bitmap = new wxBitmap(x1,y1);
438 m_memDC->SelectObject(*m_bitmap);
439 }
440
441 // Device origins on the memDC are suspect, we translate manually
442 // with the translate parameter of Draw().
443 m_memDC->SetDeviceOrigin(0,0);
444 m_memDC->SetBackgroundMode(wxTRANSPARENT);
445 m_memDC->SetBrush(wxBrush(m_llist->GetDefaults()->GetBGColour(), wxSOLID));
446 m_memDC->SetPen(wxPen(m_llist->GetDefaults()->GetBGColour(),0,wxTRANSPARENT));
447 m_memDC->SetLogicalFunction(wxCOPY);
448 if(m_BGbitmap)
449 {
450 CoordType
451 y, x,
452 w = m_BGbitmap->GetWidth(),
453 h = m_BGbitmap->GetHeight();
454 for(y = 0; y < y1; y+=h)
455 for(x = 0; x < x1; x+=w)
456 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
457 }
458 else
459 m_memDC->DrawRectangle(0,0,x1, y1);
460
461
462
463 /* This is the important bit: we tell the list to draw itself: */
464 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
465 updateRect->x, updateRect->y, updateRect->x+updateRect->width, updateRect->y+updateRect->height);
466
467 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
468 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
469
470 // Now copy everything to the screen:
471 #if 0
472 //FIXME:
473 // 1. the update region as calculated by the list is wrong
474 // 2. we get wrong values here
475 // 3. how about the offset?
476 wxRegionIterator ri ( GetUpdateRegion() );
477 if(ri)
478 while(ri)
479 {
480 wxLogDebug("UpdateRegion: %ld,%ld, %ld,%ld",
481 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH());
482 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
483 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
484 ri++;
485 }
486 else
487 #endif
488 {
489 // FIXME: Trying to copy only the changed parts, but it does not seem
490 // to work:
491 // x0 = updateRect->x; y0 = updateRect->y;
492 // if(updateRect->height < y1)
493 // y1 = updateRect->height;
494 // y1 += WXLO_YOFFSET; //FIXME might not be needed
495 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
496 }
497 //FIXME: we need to make sure we blit draw the cursor!
498 // How about drawing it directly to the screen?
499 if(IsEditable())
500 //m_llist->DrawCursor(*m_memDC,m_HaveFocus,offset);
501 m_llist->DrawCursor(dc,m_HaveFocus, wxPoint(WXLO_XOFFSET,WXLO_YOFFSET)); //direct to screen
502
503 ResetDirty();
504 m_ScrollToCursor = false;
505 m_llist->InvalidateUpdateRect();
506 }
507
508 // change the range and position of scroll bars
509 void
510 wxLayoutWindow::ResizeScrollbars(bool exact)
511 {
512 wxPoint max = m_llist->GetSize();
513
514 if(max.x > m_maxx || max.y > m_maxy
515 || max.x > m_maxx-WXLO_ROFFSET || max.y > m_maxy-WXLO_BOFFSET
516 || exact)
517 {
518 if(! exact)
519 {
520 // add an extra bit to the sizes to avoid future updates
521 max.x = max.x+WXLO_ROFFSET;
522 max.y = max.y+WXLO_BOFFSET;
523 }
524 ViewStart(&m_ViewStartX, &m_ViewStartY);
525 SetScrollbars(10, 20, max.x/10+1,max.y/20+1,m_ViewStartX,m_ViewStartY,true);
526 m_maxx = max.x; m_maxy = max.y;
527 }
528 }
529
530 void
531 wxLayoutWindow::Paste(void)
532 {
533 wxString text;
534 // Read some text
535 if (wxTheClipboard->Open())
536 {
537 wxTextDataObject data;
538 if (wxTheClipboard->IsSupported( data.GetFormat() ))
539 {
540 wxTheClipboard->GetData(&data);
541 text += data.GetText();
542 }
543 wxTheClipboard->Close();
544 }
545 #if 0
546 /* Unfortunately, this little hack doesn't work. So I'll go back to
547 pure X11. */
548 if(text.Length() == 0)
549 {
550 wxTextCtrl tmp_tctrl(this,-1);
551 tmp_tctrl.Paste();
552 text += tmp_tctrl.GetValue();
553 }
554 #endif
555 wxLayoutImportText( m_llist, text);
556 }
557
558
559 wxMenu *
560 wxLayoutWindow::MakeFormatMenu()
561 {
562 wxMenu *m = new wxMenu(_("Layout Menu"));
563
564 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
565 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
566 m->AppendSeparator();
567 m->Append(WXLOWIN_MENU_UNDERLINE_ON, _("&Underline on"),_("Activate underline mode."), false);
568 m->Append(WXLOWIN_MENU_UNDERLINE_OFF,_("&Underline off"),_("Deactivate underline mode."), false);
569 m->Append(WXLOWIN_MENU_BOLD_ON ,_("&Bold on"),_("Activate bold mode."), false);
570 m->Append(WXLOWIN_MENU_BOLD_OFF ,_("&Bold off"),_("Deactivate bold mode."), false);
571 m->Append(WXLOWIN_MENU_ITALICS_ON ,_("&Italics on"),_("Activate italics mode."), false);
572 m->Append(WXLOWIN_MENU_ITALICS_OFF ,_("&Italics off"),_("Deactivate italics mode."), false);
573 m->AppendSeparator();
574 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
575 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
576 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
577 return m;
578 }
579
580 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
581 {
582 switch (event.GetId())
583 {
584 case WXLOWIN_MENU_LARGER:
585 m_llist->SetFontLarger(); break;
586 case WXLOWIN_MENU_SMALLER:
587 m_llist->SetFontSmaller(); break;
588 case WXLOWIN_MENU_UNDERLINE_ON:
589 m_llist->SetFontUnderline(true); break;
590 case WXLOWIN_MENU_UNDERLINE_OFF:
591 m_llist->SetFontUnderline(false); break;
592 case WXLOWIN_MENU_BOLD_ON:
593 m_llist->SetFontWeight(wxBOLD); break;
594 case WXLOWIN_MENU_BOLD_OFF:
595 m_llist->SetFontWeight(wxNORMAL); break;
596 case WXLOWIN_MENU_ITALICS_ON:
597 m_llist->SetFontStyle(wxITALIC); break;
598 case WXLOWIN_MENU_ITALICS_OFF:
599 m_llist->SetFontStyle(wxNORMAL); break;
600 case WXLOWIN_MENU_ROMAN:
601 m_llist->SetFontFamily(wxROMAN); break;
602 case WXLOWIN_MENU_TYPEWRITER:
603 m_llist->SetFontFamily(wxFIXED); break;
604 case WXLOWIN_MENU_SANSSERIF:
605 m_llist->SetFontFamily(wxSWISS); break;
606 }
607 }
608
609 void
610 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
611 {
612 m_HaveFocus = true;
613 //FIXME DoPaint(); // to repaint the cursor
614 }
615
616 void
617 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
618 {
619 m_HaveFocus = false;
620 //FIXME DoPaint(); // to repaint the cursor
621 }