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