]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlwindow.cpp
3780f8ccabd2ba0e448fd33c8883418c3ea79e99
[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 event.Skip();
736 }
737
738 void
739 wxLayoutWindow::OnKeyUp(wxKeyEvent& event)
740 {
741 if ( event.KeyCode() == WXK_SHIFT && m_Selecting )
742 {
743 m_llist->EndSelection();
744 m_Selecting = false;
745 }
746
747 event.Skip();
748 }
749
750
751 void
752 wxLayoutWindow::ScrollToCursor(void)
753 {
754 //is always needed to make sure we know where the cursor is
755 //if(IsDirty())
756 RequestUpdate(m_llist->GetUpdateRect());
757
758 int x0,y0,x1,y1, dx, dy;
759
760 // Calculate where the top of the visible area is:
761 ViewStart(&x0,&y0);
762 GetScrollPixelsPerUnit(&dx, &dy);
763 x0 *= dx; y0 *= dy;
764
765 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0, y0));
766
767 // Get the size of the visible window:
768 GetClientSize(&x1, &y1);
769
770 // Make sure that the scrollbars are at a position so that the cursor is
771 // visible if we are editing
772 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor));
773 wxPoint cc = m_llist->GetCursorScreenPos();
774
775 // the cursor should be completely visible in both directions
776 wxPoint cs(m_llist->GetCursorSize());
777 int nx = -1,
778 ny = -1;
779 if ( cc.x < x0 || cc.x >= x0 + x1 - cs.x )
780 {
781 nx = cc.x - x1/2;
782 if ( nx < 0 )
783 nx = 0;
784 }
785
786 if ( cc.y < y0 || cc.y >= y0 + y1 - cs.y )
787 {
788 ny = cc.y - y1/2;
789 if ( ny < 0)
790 ny = 0;
791 }
792
793 if ( nx != -1 || ny != -1 )
794 {
795 // set new view start
796 Scroll(nx == -1 ? -1 : (nx+dx-1)/dx, ny == -1 ? -1 : (ny+dy-1)/dy);
797 // avoid recursion
798 m_ScrollToCursor = false;
799 RequestUpdate();
800 }
801 }
802
803 void
804 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event))
805 {
806 wxRect region = GetUpdateRegion().GetBox();
807 InternalPaint(&region);
808 }
809
810 void
811 wxLayoutWindow::RequestUpdate(const wxRect *updateRect)
812 {
813 #ifdef __WXGTK__
814 // Calling Refresh() causes bad flicker under wxGTK!!!
815 InternalPaint(updateRect);
816 #else
817 // shouldn't specify the update rectangle if it doesn't include all the
818 // changed locations - otherwise, they won't be repainted at all because
819 // the system clips the display to the update rect
820 Refresh(FALSE); //, updateRect);
821 #endif
822 }
823
824 void
825 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
826 {
827 wxPaintDC dc( this );
828 PrepareDC( dc );
829
830 #ifdef WXLAYOUT_USE_CARET
831 // hide the caret before drawing anything
832 GetCaret()->Hide();
833 #endif // WXLAYOUT_USE_CARET
834
835 int x0,y0,x1,y1, dx, dy;
836
837 // Calculate where the top of the visible area is:
838 ViewStart(&x0,&y0);
839 GetScrollPixelsPerUnit(&dx, &dy);
840 x0 *= dx; y0 *= dy;
841
842 // Get the size of the visible window:
843 GetClientSize(&x1,&y1);
844 wxASSERT(x1 >= 0);
845 wxASSERT(y1 >= 0);
846
847 if(updateRect)
848 {
849 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
850 updateRect->x, updateRect->y,
851 updateRect->x+updateRect->width,
852 updateRect->y+updateRect->height));
853 }
854 if(IsDirty())
855 {
856 WXLO_DEBUG(("InternalPaint, isdirty, list size: %ld,%ld",
857 (unsigned long) m_llist->GetSize().x,
858 (unsigned long) m_llist->GetSize().y));
859 // m_llist->ForceTotalLayout();
860 m_llist->Layout(dc);
861 WXLO_DEBUG(("InternalPaint, isdirty, list size after layout: %ld,%ld",
862 (unsigned long) m_llist->GetSize().x,
863 (unsigned long) m_llist->GetSize().y));
864 ResizeScrollbars();
865 ResetDirty();
866 }
867
868 /* Check whether the window has grown, if so, we need to reallocate
869 the bitmap to be larger. */
870 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
871 {
872 wxASSERT(m_bitmapSize.x > 0);
873 wxASSERT(m_bitmapSize.y > 0);
874
875 m_memDC->SelectObject(wxNullBitmap);
876 delete m_bitmap;
877 m_bitmapSize = wxPoint(x1,y1);
878 m_bitmap = new wxBitmap(x1,y1);
879 m_memDC->SelectObject(*m_bitmap);
880 }
881
882 m_memDC->SetDeviceOrigin(0,0);
883 m_memDC->SetBackground(wxBrush(m_llist->GetDefaultStyleInfo().GetBGColour(),wxSOLID));
884 m_memDC->SetPen(wxPen(m_llist->GetDefaultStyleInfo().GetBGColour(),
885 0,wxTRANSPARENT));
886 m_memDC->SetLogicalFunction(wxCOPY);
887 m_memDC->Clear();
888
889 // fill the background with the background bitmap
890 if(m_BGbitmap)
891 {
892 CoordType
893 y, x,
894 w = m_BGbitmap->GetWidth(),
895 h = m_BGbitmap->GetHeight();
896 for(y = 0; y < y1; y+=h)
897 for(x = 0; x < x1; x+=w)
898 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
899 m_memDC->SetBackgroundMode(wxTRANSPARENT);
900 }
901
902 // This is the important bit: we tell the list to draw itself
903 #if WXLO_DEBUG_URECT
904 if(updateRect)
905 {
906 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
907 updateRect->x, updateRect->y,
908 updateRect->x+updateRect->width,
909 updateRect->y+updateRect->height));
910 }
911 #endif
912
913 // Device origins on the memDC are suspect, we translate manually
914 // with the translate parameter of Draw().
915 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
916 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
917
918 // We start calculating a new update rect before drawing the
919 // cursor, so that the cursor coordinates get included in the next
920 // update rectangle (although they are drawn on the memDC, this is
921 // needed to erase it):
922 m_llist->InvalidateUpdateRect();
923 if(m_CursorVisibility != 0)
924 {
925 // draw a thick cursor for editable windows with focus
926 m_llist->DrawCursor(*m_memDC,
927 m_HaveFocus && IsEditable(),
928 offset);
929 }
930
931 // Now copy everything to the screen:
932 #if 0
933 // This somehow doesn't work, but even the following bit with the
934 // whole rect at once is still a bit broken I think.
935 wxRegionIterator ri ( GetUpdateRegion() );
936 if(ri)
937 while(ri)
938 {
939 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
940 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH()));
941 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
942 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
943 ri++;
944 }
945 else
946 #endif
947 {
948 // FIXME: Trying to copy only the changed parts, but it does not seem
949 // to work:
950 // x0 = updateRect->x; y0 = updateRect->y;
951 // if(updateRect->height < y1)
952 // y1 = updateRect->height;
953 // y1 += WXLO_YOFFSET; //FIXME might not be needed
954 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
955 }
956
957 #ifdef WXLAYOUT_USE_CARET
958 // show the caret back after everything is redrawn
959 GetCaret()->Show();
960 #endif // WXLAYOUT_USE_CARET
961
962 ResetDirty();
963
964 if ( m_StatusBar && m_StatusFieldCursor != -1 )
965 {
966 static wxPoint s_oldCursorPos(-1, -1);
967
968 wxPoint pos(m_llist->GetCursorPos());
969
970 // avoid unnecessary status bar refreshes
971 if ( pos != s_oldCursorPos )
972 {
973 s_oldCursorPos = pos;
974
975 wxString label;
976 label.Printf(_("Ln:%d Col:%d"), pos.y + 1, pos.x + 1);
977 m_StatusBar->SetStatusText(label, m_StatusFieldCursor);
978 }
979 }
980 }
981
982 void
983 wxLayoutWindow::OnSize(wxSizeEvent &event)
984 {
985 if ( m_llist )
986 ResizeScrollbars();
987
988 event.Skip();
989 }
990
991 // change the range and position of scrollbars
992 void
993 wxLayoutWindow::ResizeScrollbars(bool exact)
994 {
995
996 if(IsDirty())
997 {
998 wxClientDC dc( this );
999 PrepareDC( dc );
1000 // m_llist->ForceTotalLayout();
1001 m_llist->Layout(dc);
1002 ResetDirty();
1003 RequestUpdate();
1004 }
1005
1006 wxPoint max = m_llist->GetSize();
1007 wxSize size = GetClientSize();
1008
1009 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
1010 (long int)max.x, (long int) max.y));
1011
1012 // in the absence of scrollbars we should compare with the client size
1013 if ( !m_hasHScrollbar )
1014 m_maxx = size.x;// - WXLO_ROFFSET;
1015 if ( !m_hasVScrollbar )
1016 m_maxy = size.y;// - WXLO_BOFFSET;
1017
1018 // check if the text hasn't become too big
1019 // TODO why do we set both at once? they're independent...
1020 if( max.x > m_maxx - WXLO_ROFFSET || max.y > m_maxy - WXLO_BOFFSET || exact )
1021 {
1022 // text became too large
1023 if ( !exact )
1024 {
1025 // add an extra bit to the sizes to avoid future updates
1026 max.x += WXLO_ROFFSET;
1027 max.y += WXLO_BOFFSET;
1028 }
1029
1030 ViewStart(&m_ViewStartX, &m_ViewStartY);
1031 SetScrollbars(X_SCROLL_PAGE, Y_SCROLL_PAGE,
1032 max.x / X_SCROLL_PAGE + 1, max.y / Y_SCROLL_PAGE + 1,
1033 m_ViewStartX, m_ViewStartY,
1034 true);
1035
1036 m_hasHScrollbar =
1037 m_hasVScrollbar = true;
1038
1039 m_maxx = max.x + X_SCROLL_PAGE;
1040 m_maxy = max.y + Y_SCROLL_PAGE;
1041 }
1042 #if 0
1043 //FIXME: this code is pretty broken, producing "arithmetic
1044 //exception" crashes (div by 0??)
1045 else
1046 {
1047 // check if the window hasn't become too big, thus making the scrollbars
1048 // unnecessary
1049 if ( !exact )
1050 {
1051 // add an extra bit to the sizes to avoid future updates
1052 max.x -= WXLO_ROFFSET;
1053 max.y -= WXLO_BOFFSET;
1054 }
1055
1056 if ( m_hasHScrollbar && (max.x < m_maxx) )
1057 {
1058 // remove the horizontal scrollbar
1059 SetScrollbars(0, -1, 0, -1, 0, -1, true);
1060 m_hasHScrollbar = false;
1061 }
1062
1063 if ( m_hasVScrollbar && (max.y < m_maxy) )
1064 {
1065 // remove the vertical scrollbar
1066 SetScrollbars(-1, 0, -1, 0, -1, 0, true);
1067 m_hasVScrollbar = false;
1068 }
1069 }
1070 #endif
1071 }
1072
1073 // ----------------------------------------------------------------------------
1074 // clipboard operations
1075 //
1076 // ----------------------------------------------------------------------------
1077
1078 void
1079 wxLayoutWindow::Paste(bool primary)
1080 {
1081 // Read some text
1082 if (wxTheClipboard->Open())
1083 {
1084 if(primary)
1085 wxTheClipboard->UsePrimarySelection();
1086
1087 wxLayoutDataObject wxldo;
1088 if (wxTheClipboard->IsSupported( wxldo.GetFormat() ))
1089 {
1090 wxTheClipboard->GetData(wxldo);
1091
1092 // now we can access the data we had put into wxLayoutDataObject in
1093 // wxLayoutList::GetSelection by calling its GetLayoutData() - the
1094 // trouble is that I don't know what to do with it! (VZ)
1095 }
1096 else
1097 {
1098 wxTextDataObject data;
1099 if (wxTheClipboard->IsSupported( data.GetFormat() ))
1100 {
1101 wxTheClipboard->GetData(data);
1102 wxString text = data.GetText();
1103 wxLayoutImportText( m_llist, text);
1104 SetDirty();
1105 }
1106 }
1107 wxTheClipboard->Close();
1108 }
1109 }
1110
1111 bool
1112 wxLayoutWindow::Copy(bool invalidate)
1113 {
1114 // Calling GetSelection() will automatically do an EndSelection()
1115 // on the list, but we need to take a note of it, too:
1116 if(m_Selecting)
1117 {
1118 m_Selecting = false;
1119 m_llist->EndSelection();
1120 }
1121
1122 wxLayoutDataObject *wldo = new wxLayoutDataObject;
1123 wxLayoutList *llist = m_llist->GetSelection(wldo, invalidate);
1124 if(! llist)
1125 return FALSE;
1126 // Export selection as text:
1127 wxString text;
1128 wxLayoutExportObject *exp;
1129 wxLayoutExportStatus status(llist);
1130 while((exp = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
1131 {
1132 if(exp->type == WXLO_EXPORT_TEXT)
1133 text << *(exp->content.text);
1134 delete exp;
1135 }
1136 delete llist;
1137
1138 // The exporter always appends a newline, so we chop it off if it
1139 // is there:
1140 {
1141 size_t len = text.Length();
1142 if(len > 2 && text[len-2] == '\r') // Windows
1143 text = text.Mid(0,len-2);
1144 else if(len > 1 && text[len-1] == '\n')
1145 text = text.Mid(0,len-1);
1146 }
1147
1148
1149 if (wxTheClipboard->Open())
1150 {
1151 wxDataObjectComposite *dobj = new wxDataObjectComposite;
1152 dobj->Add(new wxTextDataObject(text));
1153 dobj->Add(wldo);
1154
1155 bool rc = wxTheClipboard->SetData(dobj);
1156
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 }