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