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