]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlwindow.cpp
improved selection-autodeletion, but backspace handling not perfect
[wxWidgets.git] / samples / richedit / 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 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #ifdef __GNUG__
18 # pragma implementation "wxlwindow.h"
19 #endif
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 # pragma hdrstop
25 #endif
26
27 #include "Mpch.h"
28
29 #ifdef M_BASEDIR
30 # ifndef USE_PCH
31 # include "Mcommon.h"
32 # include "gui/wxMenuDefs.h"
33 # include "gui/wxMApp.h"
34 # endif // USE_PCH
35 # include "gui/wxlwindow.h"
36 # include "gui/wxlparser.h"
37 #else
38 # ifdef __WXMSW__
39 # include <wx/msw/private.h>
40 # endif
41
42 # include "wxlwindow.h"
43 # include "wxlparser.h"
44 #endif
45
46 #include <wx/clipbrd.h>
47 #include <wx/textctrl.h>
48 #include <wx/dataobj.h>
49
50 #ifdef WXLAYOUT_USE_CARET
51 # include <wx/caret.h>
52 #endif // WXLAYOUT_USE_CARET
53
54 #include <ctype.h>
55
56 // ----------------------------------------------------------------------------
57 // macros
58 // ----------------------------------------------------------------------------
59
60 #ifdef WXLAYOUT_DEBUG
61 # define WXLO_DEBUG(x) wxLogDebug x
62 #else
63 # define WXLO_DEBUG(x)
64 #endif
65
66 // ----------------------------------------------------------------------------
67 // constants
68 // ----------------------------------------------------------------------------
69
70 /// offsets to put a nice frame around text
71 #define WXLO_XOFFSET 4
72 #define WXLO_YOFFSET 4
73
74 /// offset to the right and bottom for when to redraw scrollbars
75 #define WXLO_ROFFSET 20
76 #define WXLO_BOFFSET 20
77
78 /// the size of one scrollbar page in pixels
79 static const int X_SCROLL_PAGE = 10;
80 static const int Y_SCROLL_PAGE = 20;
81
82 // ----------------------------------------------------------------------------
83 // event tables
84 // ----------------------------------------------------------------------------
85
86 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
87 EVT_SIZE (wxLayoutWindow::OnSize)
88
89 EVT_PAINT (wxLayoutWindow::OnPaint)
90
91 EVT_CHAR (wxLayoutWindow::OnChar)
92 EVT_KEY_UP (wxLayoutWindow::OnKeyUp)
93
94 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseDown)
95 EVT_LEFT_UP(wxLayoutWindow::OnLeftMouseUp)
96 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick)
97 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick)
98 EVT_MIDDLE_DOWN(wxLayoutWindow::OnMiddleMouseDown)
99 EVT_MOTION (wxLayoutWindow::OnMouseMove)
100
101 EVT_UPDATE_UI(WXLOWIN_MENU_UNDERLINE, wxLayoutWindow::OnUpdateMenuUnderline)
102 EVT_UPDATE_UI(WXLOWIN_MENU_BOLD, wxLayoutWindow::OnUpdateMenuBold)
103 EVT_UPDATE_UI(WXLOWIN_MENU_ITALICS, wxLayoutWindow::OnUpdateMenuItalic)
104 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST, WXLOWIN_MENU_LAST, wxLayoutWindow::OnMenu)
105
106 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus)
107 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus)
108 END_EVENT_TABLE()
109
110 // ----------------------------------------------------------------------------
111 // function prototypes
112 // ----------------------------------------------------------------------------
113
114 /// returns TRUE if keyCode is one of arrows/home/end/page{up|down} keys
115 static bool IsDirectionKey(long keyCode);
116
117 // ============================================================================
118 // implementation
119 // ============================================================================
120
121 /* LEAVE IT HERE UNTIL WXGTK WORKS AGAIN!!! */
122 #ifdef __WXGTK__
123 /// allows me to compare to wxPoints
124 static bool operator != (wxPoint const &p1, wxPoint const &p2)
125 {
126 return p1.x != p2.x || p1.y != p2.y;
127 }
128 #endif // __WXGTK__
129
130 #ifndef wxWANTS_CHARS
131 #define wxWANTS_CHARS 0
132 #endif
133
134 // ----------------------------------------------------------------------------
135 // wxLayoutWindow
136 // ----------------------------------------------------------------------------
137
138 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
139 : wxScrolledWindow(parent, -1,
140 wxDefaultPosition, wxDefaultSize,
141 wxHSCROLL | wxVSCROLL |
142 wxBORDER |
143 wxWANTS_CHARS),
144 m_llist(NULL)
145 {
146 SetStatusBar(NULL); // don't use statusbar
147 m_Editable = false;
148 m_doSendEvents = false;
149 m_ViewStartX = 0; m_ViewStartY = 0;
150 m_DoPopupMenu = true;
151 m_PopupMenu = MakeFormatMenu();
152 m_memDC = new wxMemoryDC;
153 m_bitmap = new wxBitmap(4,4);
154 m_bitmapSize = wxPoint(4,4);
155 m_llist = new wxLayoutList();
156 #ifdef __WXMSW__
157 SetAutoDeleteSelection(true);
158 #else
159 SetAutoDeleteSelection(false);
160 #endif
161 m_BGbitmap = NULL;
162 m_ScrollToCursor = false;
163 SetWrapMargin(0);
164
165 // no scrollbars initially
166 m_hasHScrollbar =
167 m_hasVScrollbar = false;
168
169 m_Selecting = false;
170
171 #ifdef WXLAYOUT_USE_CARET
172 // FIXME cursor size shouldn't be hardcoded
173 wxCaret *caret = new wxCaret(this, 2, 20);
174 SetCaret(caret);
175 m_llist->SetCaret(caret);
176 caret->Show();
177 #endif // WXLAYOUT_USE_CARET
178
179 m_HandCursor = FALSE;
180 m_CursorVisibility = -1;
181 SetCursor(wxCURSOR_IBEAM);
182 SetDirty();
183 }
184
185 wxLayoutWindow::~wxLayoutWindow()
186 {
187 delete m_memDC; // deletes bitmap automatically (?)
188 delete m_bitmap;
189 delete m_llist;
190 delete m_PopupMenu;
191 SetBackgroundBitmap(NULL);
192 }
193
194 void
195 wxLayoutWindow::Clear(int family,
196 int size,
197 int style,
198 int weight,
199 int underline,
200 wxColour *fg,
201 wxColour *bg)
202 {
203 GetLayoutList()->Clear(family,size,style,weight,underline,fg,bg);
204 SetBackgroundColour(GetLayoutList()->GetDefaultStyleInfo().GetBGColour());
205 wxScrolledWindow::Clear();
206 ResizeScrollbars(true);
207 SetDirty();
208 SetModified(false);
209 wxScrolledWindow::Clear();
210 DoPaint((wxRect *)NULL);
211 }
212
213 void wxLayoutWindow::Refresh(bool eraseBackground, const wxRect *rect)
214 {
215 wxScrolledWindow::Refresh(eraseBackground, rect);
216
217 ResizeScrollbars();
218 ScrollToCursor();
219 }
220
221 void
222 wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
223 {
224 wxClientDC dc( this );
225 PrepareDC( dc );
226 if ( eventId != WXLOWIN_MENU_MOUSEMOVE )
227 {
228 // moving the mouse in a window shouldn't give it the focus!
229 SetFocus();
230 }
231
232 wxPoint findPos;
233 findPos.x = dc.DeviceToLogicalX(event.GetX());
234 findPos.y = dc.DeviceToLogicalY(event.GetY());
235
236 findPos.x -= WXLO_XOFFSET;
237 findPos.y -= WXLO_YOFFSET;
238
239 if(findPos.x < 0)
240 findPos.x = 0;
241 if(findPos.y < 0)
242 findPos.y = 0;
243
244 m_ClickPosition = wxPoint(event.GetX(), event.GetY());
245
246 wxPoint cursorPos;
247 bool found;
248 wxLayoutObject *obj = m_llist->FindObjectScreen(dc, findPos,
249 &cursorPos, &found);
250 wxLayoutObject::UserData *u = obj ? obj->GetUserData() : NULL;
251
252 // has the mouse only been moved?
253 switch ( eventId )
254 {
255 case WXLOWIN_MENU_MOUSEMOVE:
256 // found is only true if we are really over an object, not just
257 // behind it
258 if(found && u && ! m_Selecting)
259 {
260 if(!m_HandCursor)
261 SetCursor(wxCURSOR_HAND);
262 m_HandCursor = TRUE;
263 if(m_StatusBar && m_StatusFieldLabel != -1)
264 {
265 const wxString &label = u->GetLabel();
266 if(label.Length())
267 m_StatusBar->SetStatusText(label,
268 m_StatusFieldLabel);
269 }
270 }
271 else
272 {
273 if(m_HandCursor)
274 SetCursor(wxCURSOR_IBEAM);
275 m_HandCursor = FALSE;
276 if(m_StatusBar && m_StatusFieldLabel != -1)
277 m_StatusBar->SetStatusText("", m_StatusFieldLabel);
278 }
279
280 // selecting?
281 if ( event.LeftIsDown() )
282 {
283 wxASSERT_MSG( m_Selecting, "should be set in OnMouseLeftDown" );
284
285 m_llist->ContinueSelection(cursorPos, m_ClickPosition);
286 DoPaint(); // TODO: we don't have to redraw everything!
287 }
288
289 if ( u )
290 {
291 u->DecRef();
292 u = NULL;
293 }
294 break;
295
296 case WXLOWIN_MENU_LDOWN:
297 {
298 // always move cursor to mouse click:
299 if ( obj )
300 {
301 // we have found the real position
302 m_llist->MoveCursorTo(cursorPos);
303 }
304 else
305 {
306 // click beyond the end of the text
307 m_llist->MoveCursorToEnd();
308 }
309
310 // clicking a mouse removes the selection
311 if ( m_llist->HasSelection() )
312 {
313 m_llist->DiscardSelection();
314 DoPaint(); // TODO: we don't have to redraw everything!
315 }
316
317 // Calculate where the top of the visible area is:
318 int x0, y0;
319 ViewStart(&x0,&y0);
320 int dx, dy;
321 GetScrollPixelsPerUnit(&dx, &dy);
322 x0 *= dx; y0 *= dy;
323
324 wxPoint offset(-x0+WXLO_XOFFSET, -y0+WXLO_YOFFSET);
325
326 if(m_CursorVisibility == -1)
327 m_CursorVisibility = 1;
328
329 if(m_CursorVisibility != 0)
330 {
331 // draw a thick cursor for editable windows with focus
332 m_llist->DrawCursor(dc, m_HaveFocus && IsEditable(), offset);
333 }
334
335 #ifdef __WXGTK__
336 DoPaint(); // DoPaint suppresses flicker under GTK
337 #endif // wxGTK
338
339 // start selection
340 m_llist->StartSelection(wxPoint(-1, -1), m_ClickPosition);
341 m_Selecting = true;
342 }
343 break;
344
345 case WXLOWIN_MENU_LUP:
346 if ( m_Selecting )
347 {
348 m_llist->EndSelection();
349 m_Selecting = false;
350
351 DoPaint(); // TODO: we don't have to redraw everything!
352 }
353 break;
354
355 case WXLOWIN_MENU_MDOWN:
356 Paste(TRUE);
357 break;
358
359 case WXLOWIN_MENU_DBLCLICK:
360 // select a word under cursor
361 m_llist->MoveCursorTo(cursorPos);
362 m_llist->MoveCursorWord(-1);
363 m_llist->StartSelection();
364 m_llist->MoveCursorWord(1, false);
365 m_llist->EndSelection();
366
367 DoPaint(); // TODO: we don't have to redraw everything!
368 break;
369 }
370
371 // notify about mouse events?
372 if( m_doSendEvents )
373 {
374 // only do the menu if activated, editable and not on a clickable object
375 if(eventId == WXLOWIN_MENU_RCLICK
376 && IsEditable()
377 && (! obj || u == NULL))
378 {
379 PopupMenu(m_PopupMenu, m_ClickPosition.x, m_ClickPosition.y);
380 if(u) u->DecRef();
381 return;
382 }
383
384 // find the object at this position
385 if(obj)
386 {
387 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
388 commandEvent.SetEventObject( this );
389 commandEvent.SetClientData((char *)obj);
390 GetEventHandler()->ProcessEvent(commandEvent);
391 }
392 }
393
394 if( u )
395 u->DecRef();
396 }
397
398 // ----------------------------------------------------------------------------
399 // keyboard handling.
400 // ----------------------------------------------------------------------------
401
402 void
403 wxLayoutWindow::OnChar(wxKeyEvent& event)
404 {
405 int keyCode = event.KeyCode();
406 bool ctrlDown = event.ControlDown();
407
408 #ifdef WXLAYOUT_DEBUG
409 if(keyCode == WXK_F1)
410 {
411 m_llist->Debug();
412 return;
413 }
414 #endif
415
416 // Force m_Selecting to be false if shift is no longer
417 // pressed. OnKeyUp() cannot catch all Shift-Up events.
418 if(!event.ShiftDown())
419 m_Selecting = false;
420
421 // If we deleted the selection here, we must not execute the
422 // deletion in Delete/Backspace handling.
423 bool deletedSelection = false;
424 // pressing any non-arrow key optionally replaces the selection:
425 if(m_AutoDeleteSelection
426 && !m_Selecting
427 && m_llist->HasSelection()
428 && ! IsDirectionKey(keyCode)
429 && ! (event.AltDown() || ctrlDown)
430 )
431 {
432 m_llist->DeleteSelection();
433 deletedSelection = true;
434 }
435
436 // <Shift>+<arrow> starts selection
437 if ( IsDirectionKey(keyCode) )
438 {
439 if ( !m_Selecting )
440 {
441 m_Selecting = true;
442 m_llist->StartSelection();
443 }
444 else
445 {
446 // just continue the old selection
447 if(! event.ShiftDown() )
448 m_llist->DiscardSelection();
449 }
450 }
451
452 // If needed, make cursor visible:
453 if(m_CursorVisibility == -1)
454 m_CursorVisibility = 1;
455
456 /* These two nested switches work like this:
457 The first one processes all non-editing keycodes, to move the
458 cursor, etc. It's default will process all keycodes causing
459 modifications to the buffer, but only if editing is allowed.
460 */
461 switch(keyCode)
462 {
463 case WXK_RIGHT:
464 if ( ctrlDown )
465 m_llist->MoveCursorWord(1);
466 else
467 m_llist->MoveCursorHorizontally(1);
468 break;
469 case WXK_LEFT:
470 if ( ctrlDown )
471 m_llist->MoveCursorWord(-1);
472 else
473 m_llist->MoveCursorHorizontally(-1);
474 break;
475 case WXK_UP:
476 m_llist->MoveCursorVertically(-1);
477 break;
478 case WXK_DOWN:
479 m_llist->MoveCursorVertically(1);
480 break;
481 case WXK_PRIOR:
482 m_llist->MoveCursorVertically(-Y_SCROLL_PAGE);
483 break;
484 case WXK_NEXT:
485 m_llist->MoveCursorVertically(Y_SCROLL_PAGE);
486 break;
487 case WXK_HOME:
488 if ( ctrlDown )
489 m_llist->MoveCursorTo(wxPoint(0, 0));
490 else
491 m_llist->MoveCursorToBeginOfLine();
492 break;
493 case WXK_END:
494 if ( ctrlDown )
495 m_llist->MoveCursorToEnd();
496 else
497 m_llist->MoveCursorToEndOfLine();
498 break;
499
500 default:
501 if(keyCode == 'c' && ctrlDown)
502 {
503 // this should work even in read-only mode
504 Copy();
505 }
506 else if( IsEditable() )
507 {
508 /* First, handle control keys */
509 if(ctrlDown && ! event.AltDown())
510 {
511 switch(keyCode)
512 {
513 case WXK_INSERT:
514 Copy();
515 break;
516 case WXK_DELETE :
517 case 'd':
518 if(! deletedSelection) // already done
519 m_llist->Delete(1);
520 break;
521 case 'y':
522 m_llist->DeleteLines(1);
523 break;
524 case 'h': // like backspace
525 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
526 break;
527 case 'u':
528 m_llist->DeleteToBeginOfLine();
529 break;
530 case 'k':
531 m_llist->DeleteToEndOfLine();
532 break;
533 case 'v':
534 Paste();
535 break;
536 case 'x':
537 Cut();
538 break;
539 #ifdef WXLAYOUT_DEBUG
540 case WXK_F1:
541 m_llist->SetFont(-1,-1,-1,-1,true); // underlined
542 break;
543 #endif
544 default:
545 ;
546 }
547 }
548 // ALT only:
549 else if( event.AltDown() && ! event.ControlDown() )
550 {
551 switch(keyCode)
552 {
553 case WXK_DELETE:
554 case 'd':
555 m_llist->DeleteWord();
556 break;
557 default:
558 ;
559 }
560 }
561 // no control keys:
562 else if ( ! event.AltDown() && ! event.ControlDown())
563 {
564 switch(keyCode)
565 {
566 case WXK_INSERT:
567 if(event.ShiftDown())
568 Paste();
569 break;
570 case WXK_DELETE :
571 if(event.ShiftDown())
572 Cut();
573 else
574 if(! deletedSelection)
575 m_llist->Delete(1);
576 break;
577 case WXK_BACK: // backspace
578 if(! deletedSelection)
579 if(m_llist->MoveCursorHorizontally(-1))
580 m_llist->Delete(1);
581 break;
582 case WXK_RETURN:
583 if(m_WrapMargin > 0)
584 m_llist->WrapLine(m_WrapMargin);
585 m_llist->LineBreak();
586 break;
587
588 case WXK_TAB:
589 {
590 // TODO should be configurable
591 static const int tabSize = 8;
592
593 CoordType x = m_llist->GetCursorPos().x;
594 size_t numSpaces = tabSize - x % tabSize;
595 m_llist->Insert(wxString(' ', numSpaces));
596 }
597 break;
598
599 default:
600 if((!(event.ControlDown() || event.AltDown() || event.MetaDown()))
601 && (keyCode < 256 && keyCode >= 32)
602 )
603 {
604 if(m_WrapMargin > 0 && isspace(keyCode))
605 m_llist->WrapLine(m_WrapMargin);
606 m_llist->Insert((char)keyCode);
607 }
608 break;
609 }
610 }
611 SetDirty();
612 SetModified();
613 }// if(IsEditable())
614 }// first switch()
615
616 if ( m_Selecting )
617 {
618 // continue selection to the current (new) cursor position
619 m_llist->ContinueSelection();
620 }
621
622 // we must call ResizeScrollbars() before ScrollToCursor(), otherwise the
623 // ne cursor position might be outside the current scrolllbar range
624 ResizeScrollbars();
625 ScrollToCursor();
626
627 // refresh the screen
628 DoPaint(m_llist->GetUpdateRect());
629 }
630
631 void
632 wxLayoutWindow::OnKeyUp(wxKeyEvent& event)
633 {
634 if ( event.KeyCode() == WXK_SHIFT && m_Selecting )
635 {
636 m_llist->EndSelection();
637 m_Selecting = false;
638 }
639
640 event.Skip();
641 }
642
643
644 void
645 wxLayoutWindow::ScrollToCursor(void)
646 {
647 wxClientDC dc( this );
648 PrepareDC( dc );
649
650 int x0,y0,x1,y1, dx, dy;
651
652 // Calculate where the top of the visible area is:
653 ViewStart(&x0,&y0);
654 GetScrollPixelsPerUnit(&dx, &dy);
655 x0 *= dx; y0 *= dy;
656
657 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0, y0));
658
659 // Get the size of the visible window:
660 GetClientSize(&x1, &y1);
661
662 // update the cursor screen position
663 m_llist->Layout(dc);
664
665 // Make sure that the scrollbars are at a position so that the cursor is
666 // visible if we are editing
667 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor));
668 wxPoint cc = m_llist->GetCursorScreenPos(dc);
669
670 // the cursor should be completely visible in both directions
671 wxPoint cs(m_llist->GetCursorSize());
672 int nx = -1,
673 ny = -1;
674 if ( cc.x < x0 || cc.x >= x0 + x1 - cs.x )
675 {
676 nx = cc.x - x1/2;
677 if ( nx < 0 )
678 nx = 0;
679 }
680
681 if ( cc.y < y0 || cc.y >= y0 + y1 - cs.y )
682 {
683 ny = cc.y - y1/2;
684 if ( ny < 0)
685 ny = 0;
686 }
687
688 if ( nx != -1 || ny != -1 )
689 {
690 // set new view start
691 Scroll(nx == -1 ? -1 : (nx+dx-1)/dx, ny == -1 ? -1 : (ny+dy-1)/dy);
692
693 // avoid recursion
694 m_ScrollToCursor = false;
695 }
696 }
697
698 void
699 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event))
700 {
701 wxRect region = GetUpdateRegion().GetBox();
702 InternalPaint(&region);
703 }
704
705 void
706 wxLayoutWindow::DoPaint(const wxRect *updateRect)
707 {
708 #ifdef __WXGTK__
709 InternalPaint(updateRect);
710 #else // Causes bad flicker under wxGTK!!!
711 Refresh(FALSE); //, updateRect);
712
713 if ( !::UpdateWindow(GetHwnd()) )
714 wxLogLastError("UpdateWindow");
715 #endif
716 }
717
718 void
719 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
720 {
721 wxPaintDC dc( this );
722 PrepareDC( dc );
723
724 #ifdef WXLAYOUT_USE_CARET
725 // hide the caret before drawing anything
726 GetCaret()->Hide();
727 #endif // WXLAYOUT_USE_CARET
728
729 int x0,y0,x1,y1, dx, dy;
730
731 // Calculate where the top of the visible area is:
732 ViewStart(&x0,&y0);
733 GetScrollPixelsPerUnit(&dx, &dy);
734 x0 *= dx; y0 *= dy;
735
736 // Get the size of the visible window:
737 GetClientSize(&x1,&y1);
738 wxASSERT(x1 >= 0);
739 wxASSERT(y1 >= 0);
740
741 if(updateRect)
742 {
743 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
744 updateRect->x, updateRect->y,
745 updateRect->x+updateRect->width,
746 updateRect->y+updateRect->height));
747 }
748 if(IsDirty())
749 {
750 m_llist->Layout(dc);
751 ResizeScrollbars();
752 }
753 /* Check whether the window has grown, if so, we need to reallocate
754 the bitmap to be larger. */
755 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
756 {
757 wxASSERT(m_bitmapSize.x > 0);
758 wxASSERT(m_bitmapSize.y > 0);
759
760 m_memDC->SelectObject(wxNullBitmap);
761 delete m_bitmap;
762 m_bitmapSize = wxPoint(x1,y1);
763 m_bitmap = new wxBitmap(x1,y1);
764 m_memDC->SelectObject(*m_bitmap);
765 }
766
767 m_memDC->SetDeviceOrigin(0,0);
768 m_memDC->SetBrush(wxBrush(m_llist->GetDefaultStyleInfo().GetBGColour(),wxSOLID));
769 m_memDC->SetPen(wxPen(m_llist->GetDefaultStyleInfo().GetBGColour(),
770 0,wxTRANSPARENT));
771 m_memDC->SetLogicalFunction(wxCOPY);
772 m_memDC->Clear();
773
774 // fill the background with the background bitmap
775 if(m_BGbitmap)
776 {
777 CoordType
778 y, x,
779 w = m_BGbitmap->GetWidth(),
780 h = m_BGbitmap->GetHeight();
781 for(y = 0; y < y1; y+=h)
782 for(x = 0; x < x1; x+=w)
783 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
784 m_memDC->SetBackgroundMode(wxTRANSPARENT);
785 }
786
787 // This is the important bit: we tell the list to draw itself
788 #if WXLO_DEBUG_URECT
789 if(updateRect)
790 {
791 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
792 updateRect->x, updateRect->y,
793 updateRect->x+updateRect->width,
794 updateRect->y+updateRect->height));
795 }
796 #endif
797
798 // Device origins on the memDC are suspect, we translate manually
799 // with the translate parameter of Draw().
800 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
801 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
802
803 // We start calculating a new update rect before drawing the
804 // cursor, so that the cursor coordinates get included in the next
805 // update rectangle (although they are drawn on the memDC, this is
806 // needed to erase it):
807 m_llist->InvalidateUpdateRect();
808 if(m_CursorVisibility != 0)
809 {
810 // draw a thick cursor for editable windows with focus
811 m_llist->DrawCursor(*m_memDC,
812 m_HaveFocus && IsEditable(),
813 offset);
814 }
815
816 // Now copy everything to the screen:
817 #if 0
818 // This somehow doesn't work, but even the following bit with the
819 // whole rect at once is still a bit broken I think.
820 wxRegionIterator ri ( GetUpdateRegion() );
821 if(ri)
822 while(ri)
823 {
824 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
825 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH()));
826 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
827 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
828 ri++;
829 }
830 else
831 #endif
832 {
833 // FIXME: Trying to copy only the changed parts, but it does not seem
834 // to work:
835 // x0 = updateRect->x; y0 = updateRect->y;
836 // if(updateRect->height < y1)
837 // y1 = updateRect->height;
838 // y1 += WXLO_YOFFSET; //FIXME might not be needed
839 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
840 }
841
842 #ifdef WXLAYOUT_USE_CARET
843 // show the caret back after everything is redrawn
844 m_caret->Show();
845 #endif // WXLAYOUT_USE_CARET
846
847 ResetDirty();
848 m_ScrollToCursor = false;
849
850 if ( m_StatusBar && m_StatusFieldCursor != -1 )
851 {
852 static wxPoint s_oldCursorPos(-1, -1);
853
854 wxPoint pos(m_llist->GetCursorPos());
855
856 // avoid unnecessary status bar refreshes
857 if ( pos != s_oldCursorPos )
858 {
859 s_oldCursorPos = pos;
860
861 wxString label;
862 label.Printf(_("Ln:%d Col:%d"), pos.y + 1, pos.x + 1);
863 m_StatusBar->SetStatusText(label, m_StatusFieldCursor);
864 }
865 }
866 }
867
868 void
869 wxLayoutWindow::OnSize(wxSizeEvent &event)
870 {
871 if ( m_llist )
872 {
873 ResizeScrollbars();
874 }
875
876 event.Skip();
877 }
878
879 // change the range and position of scrollbars
880 void
881 wxLayoutWindow::ResizeScrollbars(bool exact)
882 {
883 wxPoint max = m_llist->GetSize();
884 wxSize size = GetClientSize();
885
886 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
887 (long int)max.x, (long int) max.y));
888
889 // in the absence of scrollbars we should compare with the client size
890 if ( !m_hasHScrollbar )
891 m_maxx = size.x - WXLO_ROFFSET;
892 if ( !m_hasVScrollbar )
893 m_maxy = size.y - WXLO_BOFFSET;
894
895 // check if the text hasn't become too big
896 // TODO why do we set both at once? they're independent...
897 if( max.x > m_maxx - WXLO_ROFFSET || max.y > m_maxy - WXLO_BOFFSET || exact )
898 {
899 // text became too large
900 if ( !exact )
901 {
902 // add an extra bit to the sizes to avoid future updates
903 max.x += WXLO_ROFFSET;
904 max.y += WXLO_BOFFSET;
905 }
906
907 ViewStart(&m_ViewStartX, &m_ViewStartY);
908 SetScrollbars(X_SCROLL_PAGE, Y_SCROLL_PAGE,
909 max.x / X_SCROLL_PAGE + 1, max.y / Y_SCROLL_PAGE + 1,
910 m_ViewStartX, m_ViewStartY,
911 true);
912
913 m_hasHScrollbar =
914 m_hasVScrollbar = true;
915
916 m_maxx = max.x + X_SCROLL_PAGE;
917 m_maxy = max.y + Y_SCROLL_PAGE;
918 }
919 #if 0
920 else
921 {
922 // check if the window hasn't become too big, thus making the scrollbars
923 // unnecessary
924 if ( m_hasHScrollbar && (max.x < size.x) )
925 {
926 // remove the horizontal scrollbar
927 SetScrollbars(0, -1, 0, -1, 0, -1, true);
928 m_hasHScrollbar = false;
929 }
930
931 if ( m_hasVScrollbar && (max.y < size.y) )
932 {
933 // remove the vertical scrollbar
934 SetScrollbars(-1, 0, -1, 0, -1, 0, true);
935 m_hasVScrollbar = false;
936 }
937 }
938 #endif
939 }
940
941 // ----------------------------------------------------------------------------
942 // clipboard operations
943 //
944 // ----------------------------------------------------------------------------
945
946 void
947 wxLayoutWindow::Paste(bool primary)
948 {
949 // Read some text
950 if (wxTheClipboard->Open())
951 {
952 #if __WXGTK__
953 if(primary)
954 wxTheClipboard->UsePrimarySelection();
955 #endif
956 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
957 wxLayoutDataObject wxldo;
958 if (wxTheClipboard->IsSupported( wxldo.GetFormat() ))
959 {
960 wxTheClipboard->GetData(&wxldo);
961 {
962 }
963 //FIXME: missing functionality m_llist->Insert(wxldo.GetList());
964 }
965 else
966 #endif
967 {
968 wxTextDataObject data;
969 if (wxTheClipboard->IsSupported( data.GetFormat() ))
970 {
971 wxTheClipboard->GetData(&data);
972 wxString text = data.GetText();
973 wxLayoutImportText( m_llist, text);
974 }
975 }
976 wxTheClipboard->Close();
977 }
978 }
979
980 bool
981 wxLayoutWindow::Copy(bool invalidate)
982 {
983 // Calling GetSelection() will automatically do an EndSelection()
984 // on the list, but we need to take a note of it, too:
985 if(m_Selecting)
986 {
987 m_Selecting = false;
988 m_llist->EndSelection();
989 }
990
991 wxLayoutDataObject wldo;
992 wxLayoutList *llist = m_llist->GetSelection(&wldo, invalidate);
993 if(! llist)
994 return FALSE;
995 // Export selection as text:
996 wxString text;
997 wxLayoutExportObject *export;
998 wxLayoutExportStatus status(llist);
999 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
1000 {
1001 if(export->type == WXLO_EXPORT_TEXT)
1002 text << *(export->content.text);
1003 delete export;
1004 }
1005 delete llist;
1006
1007 // The exporter always appends a newline, so we chop it off if it
1008 // is there:
1009 {
1010 size_t len = text.Length();
1011 if(len > 2 && text[len-2] == '\r') // Windows
1012 text = text.Mid(0,len-2);
1013 else if(len > 1 && text[len-1] == '\n')
1014 text = text.Mid(0,len-1);
1015 }
1016
1017
1018 if (wxTheClipboard->Open())
1019 {
1020 wxTextDataObject *data = new wxTextDataObject( text );
1021 bool rc = wxTheClipboard->SetData( data );
1022 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
1023 rc |= wxTheClipboard->AddData( &wldo );
1024 #endif
1025 wxTheClipboard->Close();
1026 return rc;
1027 }
1028
1029 return FALSE;
1030 }
1031
1032 bool
1033 wxLayoutWindow::Cut(void)
1034 {
1035 if(Copy(false)) // do not invalidate selection after copy
1036 {
1037 m_llist->DeleteSelection();
1038 return TRUE;
1039 }
1040 else
1041 return FALSE;
1042 }
1043
1044 // ----------------------------------------------------------------------------
1045 // searching
1046 // ----------------------------------------------------------------------------
1047
1048 bool
1049 wxLayoutWindow::Find(const wxString &needle,
1050 wxPoint * fromWhere)
1051 {
1052 wxPoint found;
1053
1054 if(fromWhere == NULL)
1055 found = m_llist->FindText(needle, m_llist->GetCursorPos());
1056 else
1057 found = m_llist->FindText(needle, *fromWhere);
1058 if(found.x != -1)
1059 {
1060 if(fromWhere)
1061 {
1062 *fromWhere = found;
1063 fromWhere->x ++;
1064 }
1065 m_llist->MoveCursorTo(found);
1066 ScrollToCursor();
1067 return true;
1068 }
1069 return false;
1070 }
1071
1072 // ----------------------------------------------------------------------------
1073 // popup menu stuff
1074 // ----------------------------------------------------------------------------
1075
1076 wxMenu *
1077 wxLayoutWindow::MakeFormatMenu()
1078 {
1079 wxMenu *m = new wxMenu(_("Layout Menu"));
1080
1081 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
1082 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
1083 m->AppendSeparator();
1084 m->Append(WXLOWIN_MENU_UNDERLINE, _("&Underline"),_("Underline mode."), true);
1085 m->Append(WXLOWIN_MENU_BOLD, _("&Bold"),_("Bold mode."), true);
1086 m->Append(WXLOWIN_MENU_ITALICS, _("&Italics"),_("Italics mode."), true);
1087 m->AppendSeparator();
1088 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
1089 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
1090 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
1091
1092 return m;
1093 }
1094
1095 void wxLayoutWindow::OnUpdateMenuUnderline(wxUpdateUIEvent& event)
1096 {
1097 event.Check(m_llist->IsFontUnderlined());
1098 }
1099
1100 void wxLayoutWindow::OnUpdateMenuBold(wxUpdateUIEvent& event)
1101 {
1102 event.Check(m_llist->IsFontBold());
1103 }
1104
1105 void wxLayoutWindow::OnUpdateMenuItalic(wxUpdateUIEvent& event)
1106 {
1107 event.Check(m_llist->IsFontItalic());
1108 }
1109
1110 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
1111 {
1112 switch (event.GetId())
1113 {
1114 case WXLOWIN_MENU_LARGER:
1115 m_llist->SetFontLarger(); Refresh(FALSE); break;
1116 case WXLOWIN_MENU_SMALLER:
1117 m_llist->SetFontSmaller(); Refresh(FALSE); break;
1118 case WXLOWIN_MENU_UNDERLINE:
1119 m_llist->ToggleFontUnderline(); Refresh(FALSE); break;
1120 case WXLOWIN_MENU_BOLD:
1121 m_llist->ToggleFontWeight(); Refresh(FALSE); break;
1122 case WXLOWIN_MENU_ITALICS:
1123 m_llist->ToggleFontItalics(); Refresh(FALSE); break;
1124 case WXLOWIN_MENU_ROMAN:
1125 m_llist->SetFontFamily(wxROMAN); Refresh(FALSE); break;
1126 case WXLOWIN_MENU_TYPEWRITER:
1127 m_llist->SetFontFamily(wxFIXED); Refresh(FALSE); break;
1128 case WXLOWIN_MENU_SANSSERIF:
1129 m_llist->SetFontFamily(wxSWISS); Refresh(FALSE); break;
1130 }
1131 }
1132
1133 // ----------------------------------------------------------------------------
1134 // focus
1135 // ----------------------------------------------------------------------------
1136
1137 void
1138 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
1139 {
1140 m_HaveFocus = true;
1141 ev.Skip();
1142 }
1143
1144 void
1145 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
1146 {
1147 m_HaveFocus = false;
1148 ev.Skip();
1149 }
1150
1151 // ----------------------------------------------------------------------------
1152 // private functions
1153 // ----------------------------------------------------------------------------
1154
1155 static bool IsDirectionKey(long keyCode)
1156 {
1157 switch(keyCode)
1158 {
1159 case WXK_UP:
1160 case WXK_DOWN:
1161 case WXK_RIGHT:
1162 case WXK_LEFT:
1163 case WXK_PRIOR:
1164 case WXK_NEXT:
1165 case WXK_HOME:
1166 case WXK_END:
1167 return true;
1168
1169 default:
1170 return false;
1171 }
1172 }