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