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