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