]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlwindow.cpp
final sweep over docs - replace & with \&
[wxWidgets.git] / samples / richedit / wxlwindow.cpp
1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
3 * *
4 * (C) 1998-2000 by Karsten Ballüder (ballueder@gmx.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
38 # include "MDialogs.h"
39 # include "strutil.h"
40 #else
41 # ifdef __WXMSW__
42 # include "wx/msw/private.h"
43 # endif
44
45 # include "wxlwindow.h"
46 # include "wxlparser.h"
47 #endif
48
49 #include "wx/clipbrd.h"
50 #include "wx/textctrl.h"
51 #include "wx/dataobj.h"
52
53 #ifdef WXLAYOUT_USE_CARET
54 # include "wx/caret.h"
55 #endif // WXLAYOUT_USE_CARET
56
57 #include <ctype.h>
58
59
60 // ----------------------------------------------------------------------------
61 // macros
62 // ----------------------------------------------------------------------------
63
64 #ifdef DEBUG
65 # define WXLO_DEBUG(x) wxLogDebug x
66 #else
67 # define WXLO_DEBUG(x)
68 #endif
69
70 // for profiling in debug mode:
71 WXLO_TIMER_DEFINE(UpdateTimer);
72 WXLO_TIMER_DEFINE(BlitTimer);
73 WXLO_TIMER_DEFINE(LayoutTimer);
74 WXLO_TIMER_DEFINE(TmpTimer);
75 WXLO_TIMER_DEFINE(DrawTimer);
76
77 // ----------------------------------------------------------------------------
78 // constants
79 // ----------------------------------------------------------------------------
80
81 /// offsets to put a nice frame around text
82 #define WXLO_XOFFSET 4
83 #define WXLO_YOFFSET 4
84
85 /// offset to the right and bottom for when to redraw scrollbars
86 #define WXLO_ROFFSET 20
87 #define WXLO_BOFFSET 20
88
89 /// scroll margins when selecting with the mouse
90 #define WXLO_SCROLLMARGIN_X 10
91 #define WXLO_SCROLLMARGIN_Y 10
92
93 /// the size of one scrollbar page in pixels
94 static const int X_SCROLL_PAGE = 10;
95 static const int Y_SCROLL_PAGE = 20;
96
97
98
99 // ----------------------------------------------------------------------------
100 // event tables
101 // ----------------------------------------------------------------------------
102
103 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
104 EVT_SIZE (wxLayoutWindow::OnSize)
105
106 EVT_PAINT (wxLayoutWindow::OnPaint)
107
108 EVT_CHAR (wxLayoutWindow::OnChar)
109 EVT_KEY_UP (wxLayoutWindow::OnKeyUp)
110
111 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseDown)
112 EVT_LEFT_UP(wxLayoutWindow::OnLeftMouseUp)
113 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick)
114 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick)
115 EVT_MIDDLE_DOWN(wxLayoutWindow::OnMiddleMouseDown)
116 EVT_MOTION (wxLayoutWindow::OnMouseMove)
117
118 EVT_UPDATE_UI(WXLOWIN_MENU_UNDERLINE, wxLayoutWindow::OnUpdateMenuUnderline)
119 EVT_UPDATE_UI(WXLOWIN_MENU_BOLD, wxLayoutWindow::OnUpdateMenuBold)
120 EVT_UPDATE_UI(WXLOWIN_MENU_ITALICS, wxLayoutWindow::OnUpdateMenuItalic)
121 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST, WXLOWIN_MENU_LAST, wxLayoutWindow::OnMenu)
122
123 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus)
124 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus)
125
126 // EVT_IDLE(wxLayoutWindow::ResizeScrollbars)
127 END_EVENT_TABLE()
128
129 // ----------------------------------------------------------------------------
130 // function prototypes
131 // ----------------------------------------------------------------------------
132
133 /// returns true if keyCode is one of arrows/home/end/page{up|down} keys
134 static bool IsDirectionKey(long keyCode);
135
136 // ============================================================================
137 // implementation
138 // ============================================================================
139
140 #ifndef wxWANTS_CHARS
141 # define wxWANTS_CHARS 0
142 #endif
143
144 // ----------------------------------------------------------------------------
145 // wxLayoutWindow
146 // ----------------------------------------------------------------------------
147
148 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
149 : wxScrolledWindow(parent, wxID_ANY,
150 wxDefaultPosition, wxDefaultSize,
151 wxHSCROLL | wxVSCROLL |
152 wxBORDER |
153 wxWANTS_CHARS),
154 m_llist(NULL)
155 {
156 #if wxUSE_STATUSBAR
157 SetStatusBar(NULL); // don't use statusbar
158 #endif // wxUSE_STATUSBAR
159 m_Editable = false;
160 m_doSendEvents = false;
161 m_ViewStartX = 0; m_ViewStartY = 0;
162 m_DoPopupMenu = true;
163 m_PopupMenu = MakeFormatMenu();
164 m_memDC = new wxMemoryDC;
165 m_bitmap = new wxBitmap(4,4);
166 m_bitmapSize = wxPoint(4,4);
167 m_llist = new wxLayoutList();
168 m_BGbitmap = NULL;
169 m_ScrollToCursor = false;
170 #ifndef __WXMSW__
171 m_FocusFollowMode = false;
172 #endif
173 SetWordWrap(false);
174 SetWrapMargin(0);
175
176 // no scrollbars initially
177 m_hasHScrollbar =
178 m_hasVScrollbar = false;
179
180 m_Selecting = false;
181
182 #ifdef WXLAYOUT_USE_CARET
183 // FIXME cursor size shouldn't be hardcoded
184 wxCaret *caret = new wxCaret(this, 2, 20);
185 SetCaret(caret);
186 m_llist->SetCaret(caret);
187 #endif // WXLAYOUT_USE_CARET
188
189 m_HaveFocus = false;
190 m_HandCursor = false;
191 m_CursorVisibility = -1;
192 SetCursor(wxCURSOR_IBEAM);
193 SetDirty();
194
195 // at least under Windows, this should be the default behaviour
196 m_AutoDeleteSelection = true;
197 }
198
199 wxLayoutWindow::~wxLayoutWindow()
200 {
201 delete m_memDC; // deletes bitmap automatically (?)
202 delete m_bitmap;
203 delete m_llist;
204 delete m_PopupMenu;
205 SetBackgroundBitmap(NULL);
206 }
207
208 void
209 wxLayoutWindow::Clear(int family,
210 int size,
211 int style,
212 int weight,
213 int underline,
214 wxColour *fg,
215 wxColour *bg)
216 {
217 GetLayoutList()->Clear(family,size,style,weight,underline,fg,bg);
218 SetBackgroundColour(GetLayoutList()->GetDefaultStyleInfo().GetBGColour());
219 //wxScrolledWindow::Clear();
220 ResizeScrollbars(true);
221 SetDirty();
222 SetModified(false);
223 if ( m_Editable )
224 m_CursorVisibility = 1;
225
226 #ifdef WXLAYOUT_USE_CARET
227 if ( m_CursorVisibility == 1 )
228 GetCaret()->Show();
229 #endif // WXLAYOUT_USE_CARET
230
231 RequestUpdate((wxRect *)NULL);
232 }
233
234 void wxLayoutWindow::Refresh(bool eraseBackground, const wxRect *rect)
235 {
236 wxScrolledWindow::Refresh(eraseBackground, rect);
237 }
238
239 void
240 wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
241 {
242 wxClientDC dc( this );
243 PrepareDC( dc );
244 if ( (eventId != WXLOWIN_MENU_MOUSEMOVE
245 #ifndef __WXMSW__
246 || m_FocusFollowMode
247 #endif
248 )
249 && (wxWindow::FindFocus() != this) )
250 {
251 SetFocus();
252 }
253
254 wxPoint findPos;
255 findPos.x = dc.DeviceToLogicalX(event.GetX());
256 findPos.y = dc.DeviceToLogicalY(event.GetY());
257
258 findPos.x -= WXLO_XOFFSET;
259 findPos.y -= WXLO_YOFFSET;
260
261 if(findPos.x < 0)
262 findPos.x = 0;
263
264 if(findPos.y < 0)
265 findPos.y = 0;
266
267 m_ClickPosition = wxPoint(event.GetX(), event.GetY());
268
269 // Scroll the window if the mouse is at the end of it:
270 if(m_Selecting && eventId == WXLOWIN_MENU_MOUSEMOVE)
271 {
272 //WXLO_DEBUG(("selecting at : %d/%d", (int) event.GetX(), (int)event.GetY()));
273 int left, top;
274 GetViewStart(&left, &top);
275 wxSize size = GetClientSize();
276 int xdelta, ydelta;
277
278 if(event.GetX() < WXLO_SCROLLMARGIN_X)
279 xdelta = -(WXLO_SCROLLMARGIN_X-event.GetX());
280 else if(event.GetX() > size.x-WXLO_SCROLLMARGIN_X)
281 xdelta = event.GetX()-size.x+WXLO_SCROLLMARGIN_X;
282 else
283 xdelta = 0;
284
285 if(event.GetY() < WXLO_SCROLLMARGIN_Y)
286 ydelta = -(WXLO_SCROLLMARGIN_Y-event.GetY());
287 else if(event.GetY() > size.y-WXLO_SCROLLMARGIN_Y)
288 ydelta = event.GetY()-size.y+WXLO_SCROLLMARGIN_Y;
289 else
290 ydelta = 0;
291
292 //WXLO_DEBUG(("xdelta: %d", (int) xdelta));
293 if(xdelta != 0 || ydelta != 0)
294 {
295 top += ydelta; if(top < 0) top = 0;
296 left += xdelta; if(left < 0) left = 0;
297 Scroll(left, top);
298 }
299 }
300
301 wxPoint cursorPos;
302 bool found;
303 wxLayoutObject *obj = m_llist->FindObjectScreen(dc, findPos,
304 &cursorPos, &found);
305 wxLayoutObject::UserData *u = obj ? obj->GetUserData() : NULL;
306
307 // has the mouse only been moved?
308 switch ( eventId )
309 {
310 case WXLOWIN_MENU_MOUSEMOVE:
311 {
312 // this variables is used to only erase the message in the status
313 // bar if we had put it there previously - otherwise empting status
314 // bar might be undesirable
315 #if wxUSE_STATUSBAR
316 static bool s_hasPutMessageInStatusBar = false;
317 #endif // wxUSE_STATUSBAR
318
319 // found is only true if we are really over an object, not just
320 // behind it
321 if(found && u && ! m_Selecting)
322 {
323 if(!m_HandCursor)
324 SetCursor(wxCURSOR_HAND);
325 m_HandCursor = true;
326 #if wxUSE_STATUSBAR
327 if(m_StatusBar && m_StatusFieldLabel != -1)
328 {
329 const wxString &label = u->GetLabel();
330 if(label.Length())
331 {
332 m_StatusBar->SetStatusText(label,m_StatusFieldLabel);
333 s_hasPutMessageInStatusBar = true;
334 }
335 }
336 #endif // wxUSE_STATUSBAR
337 }
338 else
339 {
340 if(m_HandCursor)
341 SetCursor(wxCURSOR_IBEAM);
342 m_HandCursor = false;
343 #if wxUSE_STATUSBAR
344 if( m_StatusBar && m_StatusFieldLabel != -1 &&
345 s_hasPutMessageInStatusBar )
346 {
347 m_StatusBar->SetStatusText(wxEmptyString, m_StatusFieldLabel);
348 }
349 #endif // wxUSE_STATUSBAR
350 }
351 }
352
353 // selecting?
354 if ( event.LeftIsDown() )
355 {
356 // m_Selecting might not be set if the button got pressed
357 // outside this window, so check for it:
358 if( m_Selecting )
359 {
360 m_llist->ContinueSelection(cursorPos, m_ClickPosition);
361 RequestUpdate(); // TODO: we don't have to redraw everything!
362 }
363 }
364
365 if ( u )
366 {
367 u->DecRef();
368 u = NULL;
369 }
370 break;
371
372 case WXLOWIN_MENU_LDOWN:
373 {
374 // always move cursor to mouse click:
375 m_llist->MoveCursorTo(cursorPos);
376
377 // clicking a mouse removes the selection
378 if ( m_llist->HasSelection() )
379 {
380 m_llist->DiscardSelection();
381 m_Selecting = false;
382 RequestUpdate(); // TODO: we don't have to redraw everything!
383 }
384
385 // Calculate where the top of the visible area is:
386 int x0, y0;
387 GetViewStart(&x0,&y0);
388 int dx, dy;
389 GetScrollPixelsPerUnit(&dx, &dy);
390 x0 *= dx; y0 *= dy;
391
392 wxPoint offset(-x0+WXLO_XOFFSET, -y0+WXLO_YOFFSET);
393
394 if(m_CursorVisibility == -1)
395 m_CursorVisibility = 1;
396
397 #ifdef WXLAYOUT_USE_CARET
398 if ( m_CursorVisibility == 1 )
399 GetCaret()->Show();
400 #endif // WXLAYOUT_USE_CARET
401
402 if(m_CursorVisibility)
403 {
404 // draw a thick cursor for editable windows with focus
405 m_llist->DrawCursor(dc, m_HaveFocus && IsEditable(), offset);
406 }
407
408 #ifdef __WXGTK__
409 RequestUpdate(); // RequestUpdate suppresses flicker under GTK
410 #endif // wxGTK
411
412 // start selection
413 m_llist->StartSelection(wxPoint(-1, -1), m_ClickPosition);
414 m_Selecting = true;
415 }
416 break;
417
418 case WXLOWIN_MENU_LUP:
419 if ( m_Selecting )
420 {
421 // end selection at the cursor position corresponding to the
422 // current mouse position, but don´t move cursor there.
423 m_llist->EndSelection(cursorPos,m_ClickPosition);
424 m_Selecting = false;
425
426 RequestUpdate(); // TODO: we don't have to redraw everything!
427 }
428 break;
429
430 case WXLOWIN_MENU_MDOWN:
431 Paste(true);
432 break;
433
434 case WXLOWIN_MENU_DBLCLICK:
435 // select a word under cursor
436 m_llist->MoveCursorTo(cursorPos);
437 m_llist->MoveCursorWord(-1);
438 m_llist->StartSelection();
439 m_llist->MoveCursorWord(1, false);
440 m_llist->EndSelection();
441 m_Selecting = false;
442 RequestUpdate(); // TODO: we don't have to redraw everything!
443 break;
444 }
445
446 // notify about mouse events?
447 if( m_doSendEvents )
448 {
449 // only do the menu if activated, editable and not on a clickable object
450 if(eventId == WXLOWIN_MENU_RCLICK
451 && IsEditable()
452 && (! obj || u == NULL))
453 {
454 PopupMenu(m_PopupMenu, m_ClickPosition.x, m_ClickPosition.y);
455 if(u) u->DecRef();
456 return;
457 }
458
459 // find the object at this position
460 if(obj)
461 {
462 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
463 commandEvent.SetEventObject( this );
464 commandEvent.SetClientData((char *)obj);
465 GetEventHandler()->ProcessEvent(commandEvent);
466 }
467 }
468
469 if( u ) u->DecRef();
470 }
471
472 // ----------------------------------------------------------------------------
473 // keyboard handling.
474 // ----------------------------------------------------------------------------
475
476 void
477 wxLayoutWindow::OnChar(wxKeyEvent& event)
478 {
479 int keyCode = event.GetKeyCode();
480 bool ctrlDown = event.ControlDown();
481
482 #ifdef WXLAYOUT_DEBUG
483 if(keyCode == WXK_F1)
484 {
485 m_llist->Debug();
486 return;
487 }
488 #endif
489
490 // Force m_Selecting to be false if shift is no longer
491 // pressed. OnKeyUp() cannot catch all Shift-Up events.
492 if(m_Selecting && !event.ShiftDown())
493 {
494 m_Selecting = false;
495 m_llist->EndSelection();
496 m_llist->DiscardSelection(); //FIXME: correct?
497 }
498
499 // If we deleted the selection here, we must not execute the
500 // deletion in Delete/Backspace handling.
501 bool deletedSelection = false;
502 // pressing any non-arrow key optionally replaces the selection:
503 if(m_AutoDeleteSelection
504 && IsEditable()
505 && !m_Selecting
506 && m_llist->HasSelection()
507 && ! IsDirectionKey(keyCode)
508 && ! (event.AltDown() || ctrlDown) )
509 {
510 m_llist->DeleteSelection();
511 deletedSelection = true;
512 SetDirty();
513 }
514
515 // <Shift>+<arrow> starts selection
516 if ( IsDirectionKey(keyCode) )
517 {
518 // just continue the old selection
519 if ( m_Selecting && event.ShiftDown() )
520 {
521 m_llist->ContinueSelection();
522 }
523 else
524 {
525 m_llist->DiscardSelection();
526 m_Selecting = false;
527 if( event.ShiftDown() )
528 {
529 m_Selecting = true;
530 m_llist->StartSelection();
531 }
532 }
533 }
534
535 // If needed, make cursor visible:
536 if(m_CursorVisibility == -1)
537 m_CursorVisibility = 1;
538
539 /* These two nested switches work like this:
540 The first one processes all non-editing keycodes, to move the
541 cursor, etc. It's default will process all keycodes causing
542 modifications to the buffer, but only if editing is allowed.
543 */
544 switch(keyCode)
545 {
546
547 case WXK_RIGHT:
548 if ( ctrlDown )
549 m_llist->MoveCursorWord(1);
550 else
551 m_llist->MoveCursorHorizontally(1);
552 break;
553
554 case WXK_LEFT:
555 if ( ctrlDown )
556 m_llist->MoveCursorWord(-1);
557 else
558 m_llist->MoveCursorHorizontally(-1);
559
560 break;
561
562 case WXK_UP:
563 m_llist->MoveCursorVertically(-1);
564 break;
565
566 case WXK_DOWN:
567 m_llist->MoveCursorVertically(1);
568 break;
569
570 case WXK_PRIOR:
571 m_llist->MoveCursorVertically(-Y_SCROLL_PAGE);
572 break;
573
574 case WXK_NEXT:
575 m_llist->MoveCursorVertically(Y_SCROLL_PAGE);
576 break;
577
578 case WXK_HOME:
579 if ( ctrlDown )
580 m_llist->MoveCursorTo(wxPoint(0, 0));
581 else
582 m_llist->MoveCursorToBeginOfLine();
583 break;
584
585 case WXK_END:
586 if ( ctrlDown )
587 m_llist->MoveCursorToEnd();
588 else
589 m_llist->MoveCursorToEndOfLine();
590 break;
591
592 default:
593
594 if(ctrlDown && ! IsEditable())
595 {
596 switch(keyCode)
597 {
598
599 case 'c':
600 // this should work even in read-only mode
601 Copy(true, true);
602 break;
603
604 case 's': // search
605 Find(wxEmptyString);
606 break;
607
608 case 't': // search again
609 FindAgain();
610 break;
611
612 default:
613 // we don't handle it, maybe an accelerator?
614 event.Skip();
615 ;
616 }
617 }
618 else if( IsEditable() )
619 {
620 /* First, handle control keys */
621 if(ctrlDown && ! event.AltDown())
622 {
623 if(keyCode >= 'A' && keyCode <= 'Z')
624 keyCode = tolower(keyCode);
625
626 switch(keyCode)
627 {
628
629 case WXK_INSERT:
630 Copy();
631 break;
632
633 case WXK_DELETE :
634 if(! deletedSelection)
635 {
636 m_llist->DeleteWord();
637 SetDirty();
638 }
639 break;
640
641 case 'd':
642 if(! deletedSelection) // already done
643 {
644 m_llist->Delete(1);
645 SetDirty();
646 }
647 break;
648
649 case 'y':
650 m_llist->DeleteLines(1);
651 SetDirty();
652 break;
653
654 case 'h': // like backspace
655 if(m_llist->MoveCursorHorizontally(-1))
656 {
657 m_llist->Delete(1);
658 SetDirty();
659 }
660 break;
661
662 case 's': // search
663 Find(wxEmptyString);
664 break;
665
666 case 't': // search again
667 FindAgain();
668 break;
669
670 case 'u':
671 m_llist->DeleteToBeginOfLine();
672 SetDirty();
673 break;
674
675 case 'k':
676 m_llist->DeleteToEndOfLine();
677 SetDirty();
678 break;
679
680 case 'c':
681 Copy(true, true);
682 break;
683
684 case 'v':
685 Paste(true);
686 break;
687
688 case 'x':
689 Cut();
690 break;
691
692 case 'w':
693 if(m_WrapMargin > 0)
694 m_llist->WrapLine(m_WrapMargin);
695 break;
696
697 case 'q':
698 if(m_WrapMargin > 0)
699 m_llist->WrapAll(m_WrapMargin);
700 break;
701
702 #ifdef WXLAYOUT_DEBUG
703 case WXK_F1:
704 m_llist->SetFont(-1,-1,-1,-1,true); // underlined
705 break;
706
707 case 'l':
708 Refresh(true);
709 break;
710 #endif
711
712 default:
713 // we don't handle it, maybe an accelerator?
714 event.Skip();
715 }
716 }
717 // ALT only:
718 else if( event.AltDown() && ! event.ControlDown() )
719 {
720 switch(keyCode)
721 {
722 case WXK_DELETE:
723 case 'd':
724 m_llist->DeleteWord();
725 SetDirty();
726 break;
727
728 default:
729 // we don't handle it, maybe an accelerator?
730 event.Skip();
731 }
732 }
733 // no control keys:
734 else if ( ! event.AltDown() && ! event.ControlDown())
735 {
736 switch(keyCode)
737 {
738 case WXK_INSERT:
739 if(event.ShiftDown())
740 Paste();
741 break;
742
743 case WXK_DELETE :
744 if(event.ShiftDown())
745 {
746 Cut();
747 }
748 else if(! deletedSelection)
749 {
750 m_llist->Delete(1);
751 SetDirty();
752 }
753 break;
754
755 case WXK_BACK: // backspace
756 if(! deletedSelection)
757 {
758 if(m_llist->MoveCursorHorizontally(-1))
759 {
760 m_llist->Delete(1);
761 SetDirty();
762 }
763 }
764 break;
765
766 case WXK_RETURN:
767 if (m_DoWordWrap &&
768 m_WrapMargin > 0
769 && m_llist->GetCursorPos().x > m_WrapMargin )
770 {
771 m_llist->WrapLine(m_WrapMargin);
772 }
773
774 m_llist->LineBreak();
775 SetDirty();
776 break;
777
778 case WXK_TAB:
779 if ( !event.ShiftDown() )
780 {
781 // TODO should be configurable
782 static const int tabSize = 8;
783
784 CoordType x = m_llist->GetCursorPos().x;
785 size_t numSpaces = tabSize - x % tabSize;
786 m_llist->Insert(wxString(' ', numSpaces));
787 SetDirty();
788 }
789 break;
790
791 default:
792 if ( ( !(event.ControlDown() || event.AltDown()) )
793 && (keyCode < 256 && keyCode >= 32) )
794 {
795 if ( m_DoWordWrap
796 && m_WrapMargin > 0
797 && m_llist->GetCursorPos().x > m_WrapMargin
798 && isspace(keyCode) )
799 {
800 m_llist->WrapLine(m_WrapMargin);
801 }
802
803 m_llist->Insert((wxChar)keyCode);
804 SetDirty();
805 }
806 else
807 {
808 // we don't handle it, maybe an accelerator?
809 event.Skip();
810 }
811 break;
812 }
813
814 }
815 }// if(IsEditable())
816 else
817 {
818 // we don't handle it, maybe an accelerator?
819 event.Skip();
820 }
821 }// first switch()
822
823 if ( m_Selecting )
824 {
825 // continue selection to the current (new) cursor position
826 m_llist->ContinueSelection();
827 }
828
829 ScrollToCursor();
830 // refresh the screen
831 RequestUpdate(m_llist->GetUpdateRect());
832 }
833
834 void
835 wxLayoutWindow::OnKeyUp(wxKeyEvent& event)
836 {
837 if ( event.GetKeyCode() == WXK_SHIFT && m_Selecting )
838 {
839 m_llist->EndSelection();
840 m_Selecting = false;
841 }
842
843 event.Skip();
844 }
845
846
847 void
848 wxLayoutWindow::ScrollToCursor()
849 {
850 //is always needed to make sure we know where the cursor is
851 //if(IsDirty())
852 //RequestUpdate(m_llist->GetUpdateRect());
853
854
855 ResizeScrollbars();
856
857 int x0,y0,x1,y1, dx, dy;
858
859 // Calculate where the top of the visible area is:
860 GetViewStart(&x0,&y0);
861 GetScrollPixelsPerUnit(&dx, &dy);
862 x0 *= dx; y0 *= dy;
863
864 WXLO_DEBUG(("ScrollToCursor: GetViewStart is %d/%d", x0, y0));
865
866 // Get the size of the visible window:
867 GetClientSize(&x1, &y1);
868
869 // Make sure that the scrollbars are at a position so that the cursor is
870 // visible if we are editing
871 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor));
872 wxPoint cc = m_llist->GetCursorScreenPos();
873
874 // the cursor should be completely visible in both directions
875 wxPoint cs(m_llist->GetCursorSize());
876 int nx = -1,
877 ny = -1;
878
879 if ( cc.x < x0 || cc.x >= x0 + x1 - cs.x )
880 {
881 nx = cc.x - x1/2;
882 if ( nx < 0 )
883 nx = 0;
884 }
885
886 if ( cc.y < y0 || cc.y >= y0 + y1 - cs.y )
887 {
888 ny = cc.y - y1/2;
889 if ( ny < 0)
890 ny = 0;
891 }
892
893 if( nx != -1 || ny != -1 )
894 {
895 // set new view start
896 Scroll(nx == -1 ? -1 : (nx+dx-1)/dx, ny == -1 ? -1 : (ny+dy-1)/dy);
897 // avoid recursion
898 m_ScrollToCursor = false;
899 RequestUpdate();
900 }
901 }
902
903 void
904 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event))
905 {
906 wxRect region = GetUpdateRegion().GetBox();
907 InternalPaint(&region);
908 }
909
910 void
911 wxLayoutWindow::RequestUpdate(const wxRect *
912 #ifdef __WXGTK__
913 updateRect
914 #else
915 WXUNUSED(updateRect)
916 #endif
917 )
918 {
919 #ifdef __WXGTK__
920 // Calling Refresh() causes bad flicker under wxGTK!!!
921 InternalPaint(updateRect);
922 #else
923 // shouldn't specify the update rectangle if it doesn't include all the
924 // changed locations - otherwise, they won't be repainted at all because
925 // the system clips the display to the update rect
926 Refresh(false); //, updateRect);
927 #endif
928 }
929
930 void
931 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
932 {
933
934 wxPaintDC dc( this );
935 PrepareDC( dc );
936
937 #ifdef WXLAYOUT_USE_CARET
938 // hide the caret before drawing anything
939 GetCaret()->Hide();
940 #endif // WXLAYOUT_USE_CARET
941
942 int x0,y0,x1,y1, dx, dy;
943
944 // Calculate where the top of the visible area is:
945 GetViewStart(&x0,&y0);
946 GetScrollPixelsPerUnit(&dx, &dy);
947 x0 *= dx; y0 *= dy;
948
949 // Get the size of the visible window:
950 GetClientSize(&x1,&y1);
951 wxASSERT(x1 >= 0);
952 wxASSERT(y1 >= 0);
953
954 if(updateRect)
955 {
956 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
957 updateRect->x, updateRect->y,
958 updateRect->x+updateRect->width,
959 updateRect->y+updateRect->height));
960 }
961
962 ResizeScrollbars(true);
963
964 WXLO_TIMER_START(TmpTimer);
965 /* Check whether the window has grown, if so, we need to reallocate
966 the bitmap to be larger. */
967 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
968 {
969 wxASSERT(m_bitmapSize.x > 0);
970 wxASSERT(m_bitmapSize.y > 0);
971
972 m_memDC->SelectObject(wxNullBitmap);
973 delete m_bitmap;
974 m_bitmapSize = wxPoint(x1,y1);
975 m_bitmap = new wxBitmap(x1,y1);
976 m_memDC->SelectObject(*m_bitmap);
977 }
978
979 m_memDC->SetDeviceOrigin(0,0);
980 m_memDC->SetBackground(wxBrush(m_llist->GetDefaultStyleInfo().GetBGColour(),wxSOLID));
981 m_memDC->SetPen(wxPen(m_llist->GetDefaultStyleInfo().GetBGColour(),
982 0,wxTRANSPARENT));
983 m_memDC->SetLogicalFunction(wxCOPY);
984 m_memDC->Clear();
985 WXLO_TIMER_STOP(TmpTimer);
986
987 // fill the background with the background bitmap
988 if(m_BGbitmap)
989 {
990 CoordType
991 y, x,
992 w = m_BGbitmap->GetWidth(),
993 h = m_BGbitmap->GetHeight();
994 for(y = 0; y < y1; y+=h)
995 {
996 for(x = 0; x < x1; x+=w)
997 {
998 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
999 }
1000 }
1001
1002 m_memDC->SetBackgroundMode(wxTRANSPARENT);
1003 }
1004
1005 // This is the important bit: we tell the list to draw itself
1006 #if WXLO_DEBUG_URECT
1007 if(updateRect)
1008 {
1009 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
1010 updateRect->x, updateRect->y,
1011 updateRect->x+updateRect->width,
1012 updateRect->y+updateRect->height));
1013 }
1014 #endif
1015
1016 // Device origins on the memDC are suspect, we translate manually
1017 // with the translate parameter of Draw().
1018 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
1019 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
1020
1021 // We start calculating a new update rect before drawing the
1022 // cursor, so that the cursor coordinates get included in the next
1023 // update rectangle (although they are drawn on the memDC, this is
1024 // needed to erase it):
1025 m_llist->InvalidateUpdateRect();
1026 if(m_CursorVisibility == 1)
1027 {
1028 // draw a thick cursor for editable windows with focus
1029 m_llist->DrawCursor(*m_memDC,
1030 m_HaveFocus && IsEditable(),
1031 offset);
1032 }
1033
1034 WXLO_TIMER_START(BlitTimer);
1035 // Now copy everything to the screen:
1036 #if 0
1037 // This somehow doesn't work, but even the following bit with the
1038 // whole rect at once is still a bit broken I think.
1039 wxRegionIterator ri ( GetUpdateRegion() );
1040 if(ri)
1041 while(ri)
1042 {
1043 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
1044 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH()));
1045
1046 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
1047 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,false);
1048 ri++;
1049 }
1050 else
1051 #endif
1052 {
1053 // FIXME: Trying to copy only the changed parts, but it does not seem
1054 // to work:
1055 // x0 = updateRect->x; y0 = updateRect->y;
1056 // if(updateRect->height < y1)
1057 // y1 = updateRect->height;
1058 // y1 += WXLO_YOFFSET; //FIXME might not be needed
1059 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,false);
1060 }
1061
1062 WXLO_TIMER_STOP(BlitTimer);
1063
1064
1065 #ifdef WXLAYOUT_USE_CARET
1066 // show the caret back after everything is redrawn
1067 GetCaret()->Show();
1068 #endif // WXLAYOUT_USE_CARET
1069
1070 ResetDirty();
1071
1072 #if wxUSE_STATUSBAR
1073 if ( m_StatusBar && m_StatusFieldCursor != -1 )
1074 {
1075 static wxPoint s_oldCursorPos(-1, -1);
1076
1077 wxPoint pos(m_llist->GetCursorPos());
1078
1079 // avoid unnecessary status bar refreshes
1080 if ( pos != s_oldCursorPos )
1081 {
1082 s_oldCursorPos = pos;
1083
1084 wxString label;
1085 label.Printf(_("Ln:%d Col:%d"), pos.y + 1, pos.x + 1);
1086 m_StatusBar->SetStatusText(label, m_StatusFieldCursor);
1087 }
1088 }
1089 #endif // wxUSE_STATUSBAR
1090
1091 WXLO_TIMER_PRINT(LayoutTimer);
1092 WXLO_TIMER_PRINT(BlitTimer);
1093 WXLO_TIMER_PRINT(TmpTimer);
1094 }
1095
1096 void
1097 wxLayoutWindow::OnSize(wxSizeEvent &event)
1098 {
1099 if ( m_llist )
1100 ResizeScrollbars();
1101
1102 event.Skip();
1103 }
1104
1105 /*
1106 Change the range and position of scrollbars. Has evolved into a
1107 generic Update function which will at some time later cause a repaint
1108 as needed.
1109 */
1110
1111 void
1112 wxLayoutWindow::ResizeScrollbars(bool exact)
1113 {
1114 wxClientDC dc( this );
1115 PrepareDC( dc );
1116 // m_llist->ForceTotalLayout();
1117
1118 if(! IsDirty())
1119 {
1120 // we are laying out just the minimum, but always up to the
1121 // cursor line, so the cursor position is updated.
1122 m_llist->Layout(dc, 0);
1123 return;
1124 }
1125
1126 WXLO_TIMER_START(LayoutTimer);
1127 m_llist->Layout(dc, -1);
1128 WXLO_TIMER_STOP(LayoutTimer);
1129 ResetDirty();
1130
1131 wxPoint max = m_llist->GetSize();
1132 wxSize size = GetClientSize();
1133
1134 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
1135 (long int)max.x, (long int) max.y));
1136
1137 // in the absence of scrollbars we should compare with the client size
1138 if ( !m_hasHScrollbar )
1139 m_maxx = size.x;// - WXLO_ROFFSET;
1140
1141 if ( !m_hasVScrollbar )
1142 m_maxy = size.y;// - WXLO_BOFFSET;
1143
1144 // check if the text hasn't become too big
1145 // TODO why do we set both at once? they're independent...
1146 if( max.x > m_maxx - WXLO_ROFFSET
1147 || max.y > m_maxy - WXLO_BOFFSET
1148 || (max.x < m_maxx - X_SCROLL_PAGE)
1149 || (max.y < m_maxy - Y_SCROLL_PAGE)
1150 || exact )
1151 {
1152 // text became too large
1153 if ( !exact )
1154 {
1155 // add an extra bit to the sizes to avoid future updates
1156 max.x += WXLO_ROFFSET;
1157 max.y += WXLO_BOFFSET;
1158 }
1159
1160 bool done = false;
1161
1162 if(max.x < X_SCROLL_PAGE && m_hasHScrollbar)
1163 {
1164 SetScrollbars(0,-1,0,-1,0,-1,true);
1165 m_hasHScrollbar = false;
1166 done = true;
1167 }
1168
1169 if(max.y < Y_SCROLL_PAGE && m_hasVScrollbar)
1170 {
1171 SetScrollbars(-1,0,-1,0,-1,0,true);
1172 m_hasVScrollbar = false;
1173 done = true;
1174 }
1175
1176 if (! done &&
1177 // (max.x > X_SCROLL_PAGE || max.y > Y_SCROLL_PAGE)
1178 (max.x > size.x - X_SCROLL_PAGE|| max.y > size.y - Y_SCROLL_PAGE) )
1179 {
1180 GetViewStart(&m_ViewStartX, &m_ViewStartY);
1181
1182 SetScrollbars(X_SCROLL_PAGE,
1183 Y_SCROLL_PAGE,
1184 max.x / X_SCROLL_PAGE + 2,
1185 max.y / Y_SCROLL_PAGE + 2,
1186 m_ViewStartX,
1187 m_ViewStartY,
1188 true);
1189
1190 m_hasHScrollbar =
1191 m_hasVScrollbar = true;
1192 // ScrollToCursor();
1193 }
1194
1195 m_maxx = max.x + X_SCROLL_PAGE;
1196 m_maxy = max.y + Y_SCROLL_PAGE;
1197 }
1198 }
1199
1200 // ----------------------------------------------------------------------------
1201 //
1202 // clipboard operations
1203 //
1204 // ----------------------------------------------------------------------------
1205
1206 void
1207 wxLayoutWindow::Paste(bool usePrivate, bool primary)
1208 {
1209 // this only has an effect under X11:
1210 wxTheClipboard->UsePrimarySelection(primary);
1211 // Read some text
1212 if (wxTheClipboard->Open())
1213 {
1214 if(usePrivate)
1215 {
1216 wxLayoutDataObject wxldo;
1217 if (wxTheClipboard->IsSupported( wxldo.GetFormat() ))
1218 {
1219 if(wxTheClipboard->GetData(wxldo))
1220 {
1221 wxTheClipboard->Close();
1222 wxString str = wxldo.GetLayoutData();
1223 m_llist->Read(str);
1224 SetDirty();
1225 RequestUpdate();
1226 return;
1227 }
1228 }
1229 }
1230
1231 wxTextDataObject data;
1232 if (wxTheClipboard->IsSupported( data.GetFormat() )
1233 && wxTheClipboard->GetData(data) )
1234 {
1235 wxTheClipboard->Close();
1236 wxString text = data.GetText();
1237 wxLayoutImportText( m_llist, text);
1238 SetDirty();
1239 RequestUpdate();
1240 return;
1241 }
1242 }
1243 // if everything failed we can still try the primary:
1244 wxTheClipboard->Close();
1245 if(! primary) // not tried before
1246 {
1247 wxTheClipboard->UsePrimarySelection();
1248 if (wxTheClipboard->Open())
1249 {
1250 wxTextDataObject data;
1251 if (wxTheClipboard->IsSupported( data.GetFormat() )
1252 && wxTheClipboard->GetData(data) )
1253 {
1254 wxString text = data.GetText();
1255 wxLayoutImportText( m_llist, text);
1256 SetDirty();
1257 RequestUpdate();
1258 }
1259 wxTheClipboard->Close();
1260 }
1261 }
1262 }
1263
1264 bool
1265 wxLayoutWindow::Copy(bool invalidate, bool privateFormat, bool primary)
1266 {
1267 // Calling GetSelection() will automatically do an EndSelection()
1268 // on the list, but we need to take a note of it, too:
1269 if(m_Selecting)
1270 {
1271 m_Selecting = false;
1272 m_llist->EndSelection();
1273 }
1274
1275 wxLayoutDataObject *wldo = new wxLayoutDataObject;
1276 wxLayoutList *llist = m_llist->GetSelection(wldo, invalidate);
1277 if(! llist)
1278 return false;
1279 // Export selection as text:
1280 wxString text;
1281 wxLayoutExportObject *exp;
1282 wxLayoutExportStatus status(llist);
1283 while((exp = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
1284 {
1285 if(exp->type == WXLO_EXPORT_TEXT)
1286 text << *(exp->content.text);
1287
1288 delete exp;
1289 }
1290
1291 delete llist;
1292
1293 // The exporter always appends a newline, so we chop it off if it
1294 // is there:
1295 {
1296 size_t len = text.Length();
1297 if(len > 2 && text[len-2] == '\r') // Windows
1298 text = text.Mid(0,len-2);
1299 else if(len > 1 && text[len-1] == '\n')
1300 text = text.Mid(0,len-1);
1301 }
1302
1303 #if 0
1304 if(! primary) // always copy as text-only to primary selection
1305 {
1306 wxTheClipboard->UsePrimarySelection();
1307 if (wxTheClipboard->Open())
1308 {
1309 wxTextDataObject *data = new wxTextDataObject( text );
1310 wxTheClipboard->SetData( data );
1311 wxTheClipboard->Close();
1312 }
1313 }
1314 #endif
1315
1316 wxTheClipboard->UsePrimarySelection(primary);
1317 if (wxTheClipboard->Open())
1318 {
1319 wxTextDataObject *data = new wxTextDataObject( text );
1320 bool rc = wxTheClipboard->SetData( data );
1321
1322 if(privateFormat)
1323 rc |= wxTheClipboard->SetData( wldo );
1324
1325 wxTheClipboard->Close();
1326 return rc;
1327 }
1328 else
1329 {
1330 delete wldo;
1331 }
1332
1333 return false;
1334 }
1335
1336 bool
1337 wxLayoutWindow::Cut(bool privateFormat, bool usePrimary)
1338 {
1339 if(Copy(false, privateFormat, usePrimary)) // do not invalidate selection after copy
1340 {
1341 m_llist->DeleteSelection();
1342 SetDirty();
1343 return true;
1344 }
1345 else
1346 {
1347 return false;
1348 }
1349 }
1350
1351 // ----------------------------------------------------------------------------
1352 // searching
1353 // ----------------------------------------------------------------------------
1354
1355 bool
1356 wxLayoutWindow::Find(
1357 #ifdef M_BASEDIR
1358 const wxString &needle,
1359 wxPoint * fromWhere,
1360 const wxString &configPath
1361 #else
1362 const wxString & WXUNUSED(needle),
1363 wxPoint * WXUNUSED(fromWhere),
1364 const wxString & WXUNUSED(configPath)
1365 #endif
1366 )
1367 {
1368 #ifdef M_BASEDIR
1369 wxPoint found;
1370
1371 if(needle.Length() == 0)
1372 {
1373 if( ! MInputBox(&m_FindString,
1374 _("Find text"),
1375 _(" Find:"),
1376 this,
1377 configPath, "")
1378 || strutil_isempty(m_FindString))
1379 {
1380 return true;
1381 }
1382 }
1383 else
1384 {
1385 m_FindString = needle;
1386 }
1387
1388 if(fromWhere == NULL)
1389 found = m_llist->FindText(m_FindString, m_llist->GetCursorPos());
1390 else
1391 found = m_llist->FindText(m_FindString, *fromWhere);
1392
1393 if(found.x != -1)
1394 {
1395 if(fromWhere)
1396 {
1397 *fromWhere = found;
1398 fromWhere->x ++;
1399 }
1400
1401 m_llist->MoveCursorTo(found);
1402 ScrollToCursor();
1403 RequestUpdate();
1404
1405 return true;
1406 }
1407 #endif
1408
1409 return false;
1410 }
1411
1412
1413 bool
1414 wxLayoutWindow::FindAgain()
1415 {
1416 bool rc = Find(m_FindString);
1417 return rc;
1418 }
1419
1420 // ----------------------------------------------------------------------------
1421 // popup menu stuff
1422 // ----------------------------------------------------------------------------
1423
1424 wxMenu *
1425 wxLayoutWindow::MakeFormatMenu()
1426 {
1427 wxMenu *m = new wxMenu(_("Layout Menu"));
1428
1429 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."));
1430 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."));
1431 m->AppendSeparator();
1432 m->Append(WXLOWIN_MENU_UNDERLINE, _("&Underline"),_("Underline mode."), wxITEM_CHECK);
1433 m->Append(WXLOWIN_MENU_BOLD, _("&Bold"),_("Bold mode."), wxITEM_CHECK);
1434 m->Append(WXLOWIN_MENU_ITALICS, _("&Italics"),_("Italics mode."), wxITEM_CHECK);
1435 m->AppendSeparator();
1436 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."));
1437 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."));
1438 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."));
1439
1440 return m;
1441 }
1442
1443 void wxLayoutWindow::OnUpdateMenuUnderline(wxUpdateUIEvent& event)
1444 {
1445 event.Check(m_llist->IsFontUnderlined());
1446 }
1447
1448 void wxLayoutWindow::OnUpdateMenuBold(wxUpdateUIEvent& event)
1449 {
1450 event.Check(m_llist->IsFontBold());
1451 }
1452
1453 void wxLayoutWindow::OnUpdateMenuItalic(wxUpdateUIEvent& event)
1454 {
1455 event.Check(m_llist->IsFontItalic());
1456 }
1457
1458 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
1459 {
1460 switch (event.GetId())
1461 {
1462 case WXLOWIN_MENU_LARGER:
1463 m_llist->SetFontLarger(); RequestUpdate(); break;
1464
1465 case WXLOWIN_MENU_SMALLER:
1466 m_llist->SetFontSmaller(); RequestUpdate(); break;
1467
1468 case WXLOWIN_MENU_UNDERLINE:
1469 m_llist->ToggleFontUnderline(); RequestUpdate(); break;
1470
1471 case WXLOWIN_MENU_BOLD:
1472 m_llist->ToggleFontWeight(); RequestUpdate(); break;
1473
1474 case WXLOWIN_MENU_ITALICS:
1475 m_llist->ToggleFontItalics(); RequestUpdate(); break;
1476
1477 case WXLOWIN_MENU_ROMAN:
1478 m_llist->SetFontFamily(wxROMAN); RequestUpdate(); break;
1479
1480 case WXLOWIN_MENU_TYPEWRITER:
1481 m_llist->SetFontFamily(wxFIXED); RequestUpdate(); break;
1482
1483 case WXLOWIN_MENU_SANSSERIF:
1484 m_llist->SetFontFamily(wxSWISS); RequestUpdate(); break;
1485 }
1486 }
1487
1488 // ----------------------------------------------------------------------------
1489 // focus
1490 // ----------------------------------------------------------------------------
1491
1492 void
1493 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
1494 {
1495 m_HaveFocus = true;
1496 ev.Skip();
1497 RequestUpdate(); // cursor must change
1498 }
1499
1500 void
1501 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
1502 {
1503 m_HaveFocus = false;
1504 ev.Skip();
1505 RequestUpdate();// cursor must change
1506 }
1507
1508 // ----------------------------------------------------------------------------
1509 // private functions
1510 // ----------------------------------------------------------------------------
1511
1512 static bool IsDirectionKey(long keyCode)
1513 {
1514 switch(keyCode)
1515 {
1516 case WXK_UP:
1517 case WXK_DOWN:
1518 case WXK_RIGHT:
1519 case WXK_LEFT:
1520 case WXK_PRIOR:
1521 case WXK_NEXT:
1522 case WXK_HOME:
1523 case WXK_END:
1524 return true;
1525
1526 default:
1527 return false;
1528 }
1529 }
1530