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