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