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