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