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