]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlwindow.cpp
implemented DoGetSize() for wxSlider (patch from Dimitri)
[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 GetViewStart(&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 GetViewStart(&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 {
745 m_llist->WrapLine(m_WrapMargin);
746 }
747
748 m_llist->Insert((wxChar)keyCode);
749 SetDirty();
750 }
751 else
752 // we don't handle it, maybe an accelerator?
753 event.Skip();
754 break;
755 }
756 }
757 }// if(IsEditable())
758 else
759 // we don't handle it, maybe an accelerator?
760 event.Skip();
761 }// first switch()
762
763 if ( m_Selecting )
764 {
765 // continue selection to the current (new) cursor position
766 m_llist->ContinueSelection();
767 }
768 ScrollToCursor();
769 // refresh the screen
770 RequestUpdate(m_llist->GetUpdateRect());
771 }
772
773 void
774 wxLayoutWindow::OnKeyUp(wxKeyEvent& event)
775 {
776 if ( event.KeyCode() == WXK_SHIFT && m_Selecting )
777 {
778 m_llist->EndSelection();
779 m_Selecting = false;
780 }
781
782 event.Skip();
783 }
784
785
786 void
787 wxLayoutWindow::ScrollToCursor(void)
788 {
789 //is always needed to make sure we know where the cursor is
790 //if(IsDirty())
791 //RequestUpdate(m_llist->GetUpdateRect());
792
793
794 ResizeScrollbars();
795
796 int x0,y0,x1,y1, dx, dy;
797
798 // Calculate where the top of the visible area is:
799 GetViewStart(&x0,&y0);
800 GetScrollPixelsPerUnit(&dx, &dy);
801 x0 *= dx; y0 *= dy;
802
803 WXLO_DEBUG(("ScrollToCursor: GetViewStart is %d/%d", x0, y0));
804
805 // Get the size of the visible window:
806 GetClientSize(&x1, &y1);
807
808 // Make sure that the scrollbars are at a position so that the cursor is
809 // visible if we are editing
810 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor));
811 wxPoint cc = m_llist->GetCursorScreenPos();
812
813 // the cursor should be completely visible in both directions
814 wxPoint cs(m_llist->GetCursorSize());
815 int nx = -1,
816 ny = -1;
817 if ( cc.x < x0 || cc.x >= x0 + x1 - cs.x )
818 {
819 nx = cc.x - x1/2;
820 if ( nx < 0 )
821 nx = 0;
822 }
823
824 if ( cc.y < y0 || cc.y >= y0 + y1 - cs.y )
825 {
826 ny = cc.y - y1/2;
827 if ( ny < 0)
828 ny = 0;
829 }
830
831 if( nx != -1 || ny != -1 )
832 {
833 // set new view start
834 Scroll(nx == -1 ? -1 : (nx+dx-1)/dx, ny == -1 ? -1 : (ny+dy-1)/dy);
835 // avoid recursion
836 m_ScrollToCursor = false;
837 RequestUpdate();
838 }
839 }
840
841 void
842 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event))
843 {
844 wxRect region = GetUpdateRegion().GetBox();
845 InternalPaint(&region);
846 }
847
848 void
849 wxLayoutWindow::RequestUpdate(const wxRect *updateRect)
850 {
851 #ifdef __WXGTK__
852 // Calling Refresh() causes bad flicker under wxGTK!!!
853 InternalPaint(updateRect);
854 #else
855 // shouldn't specify the update rectangle if it doesn't include all the
856 // changed locations - otherwise, they won't be repainted at all because
857 // the system clips the display to the update rect
858 Refresh(FALSE); //, updateRect);
859 #endif
860 }
861
862 void
863 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
864 {
865
866 wxPaintDC dc( this );
867 PrepareDC( dc );
868
869 #ifdef WXLAYOUT_USE_CARET
870 // hide the caret before drawing anything
871 GetCaret()->Hide();
872 #endif // WXLAYOUT_USE_CARET
873
874 int x0,y0,x1,y1, dx, dy;
875
876 // Calculate where the top of the visible area is:
877 GetViewStart(&x0,&y0);
878 GetScrollPixelsPerUnit(&dx, &dy);
879 x0 *= dx; y0 *= dy;
880
881 // Get the size of the visible window:
882 GetClientSize(&x1,&y1);
883 wxASSERT(x1 >= 0);
884 wxASSERT(y1 >= 0);
885
886 if(updateRect)
887 {
888 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
889 updateRect->x, updateRect->y,
890 updateRect->x+updateRect->width,
891 updateRect->y+updateRect->height));
892 }
893
894 ResizeScrollbars(true);
895
896 WXLO_TIMER_START(TmpTimer);
897 /* Check whether the window has grown, if so, we need to reallocate
898 the bitmap to be larger. */
899 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
900 {
901 wxASSERT(m_bitmapSize.x > 0);
902 wxASSERT(m_bitmapSize.y > 0);
903
904 m_memDC->SelectObject(wxNullBitmap);
905 delete m_bitmap;
906 m_bitmapSize = wxPoint(x1,y1);
907 m_bitmap = new wxBitmap(x1,y1);
908 m_memDC->SelectObject(*m_bitmap);
909 }
910
911 m_memDC->SetDeviceOrigin(0,0);
912 m_memDC->SetBackground(wxBrush(m_llist->GetDefaultStyleInfo().GetBGColour(),wxSOLID));
913 m_memDC->SetPen(wxPen(m_llist->GetDefaultStyleInfo().GetBGColour(),
914 0,wxTRANSPARENT));
915 m_memDC->SetLogicalFunction(wxCOPY);
916 m_memDC->Clear();
917 WXLO_TIMER_STOP(TmpTimer);
918
919 // fill the background with the background bitmap
920 if(m_BGbitmap)
921 {
922 CoordType
923 y, x,
924 w = m_BGbitmap->GetWidth(),
925 h = m_BGbitmap->GetHeight();
926 for(y = 0; y < y1; y+=h)
927 for(x = 0; x < x1; x+=w)
928 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
929 m_memDC->SetBackgroundMode(wxTRANSPARENT);
930 }
931
932 // This is the important bit: we tell the list to draw itself
933 #if WXLO_DEBUG_URECT
934 if(updateRect)
935 {
936 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
937 updateRect->x, updateRect->y,
938 updateRect->x+updateRect->width,
939 updateRect->y+updateRect->height));
940 }
941 #endif
942
943 // Device origins on the memDC are suspect, we translate manually
944 // with the translate parameter of Draw().
945 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
946 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
947
948 // We start calculating a new update rect before drawing the
949 // cursor, so that the cursor coordinates get included in the next
950 // update rectangle (although they are drawn on the memDC, this is
951 // needed to erase it):
952 m_llist->InvalidateUpdateRect();
953 if(m_CursorVisibility == 1)
954 {
955 // draw a thick cursor for editable windows with focus
956 m_llist->DrawCursor(*m_memDC,
957 m_HaveFocus && IsEditable(),
958 offset);
959 }
960
961 WXLO_TIMER_START(BlitTimer);
962 // Now copy everything to the screen:
963 #if 0
964 // This somehow doesn't work, but even the following bit with the
965 // whole rect at once is still a bit broken I think.
966 wxRegionIterator ri ( GetUpdateRegion() );
967 if(ri)
968 while(ri)
969 {
970 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
971 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH()));
972 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
973 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
974 ri++;
975 }
976 else
977 #endif
978 {
979 // FIXME: Trying to copy only the changed parts, but it does not seem
980 // to work:
981 // x0 = updateRect->x; y0 = updateRect->y;
982 // if(updateRect->height < y1)
983 // y1 = updateRect->height;
984 // y1 += WXLO_YOFFSET; //FIXME might not be needed
985 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
986 }
987 WXLO_TIMER_STOP(BlitTimer);
988
989
990 #ifdef WXLAYOUT_USE_CARET
991 // show the caret back after everything is redrawn
992 GetCaret()->Show();
993 #endif // WXLAYOUT_USE_CARET
994
995 ResetDirty();
996
997 if ( m_StatusBar && m_StatusFieldCursor != -1 )
998 {
999 static wxPoint s_oldCursorPos(-1, -1);
1000
1001 wxPoint pos(m_llist->GetCursorPos());
1002
1003 // avoid unnecessary status bar refreshes
1004 if ( pos != s_oldCursorPos )
1005 {
1006 s_oldCursorPos = pos;
1007
1008 wxString label;
1009 label.Printf(_("Ln:%d Col:%d"), pos.y + 1, pos.x + 1);
1010 m_StatusBar->SetStatusText(label, m_StatusFieldCursor);
1011 }
1012 }
1013
1014 WXLO_TIMER_PRINT(LayoutTimer);
1015 WXLO_TIMER_PRINT(BlitTimer);
1016 WXLO_TIMER_PRINT(TmpTimer);
1017 }
1018
1019 void
1020 wxLayoutWindow::OnSize(wxSizeEvent &event)
1021 {
1022 if ( m_llist )
1023 ResizeScrollbars();
1024
1025 event.Skip();
1026 }
1027
1028 /*
1029 Change the range and position of scrollbars. Has evolved into a
1030 generic Update function which will at some time later cause a repaint
1031 as needed.
1032 */
1033
1034 void
1035 wxLayoutWindow::ResizeScrollbars(bool exact)
1036 {
1037 wxClientDC dc( this );
1038 PrepareDC( dc );
1039 // m_llist->ForceTotalLayout();
1040
1041 if(! IsDirty())
1042 {
1043 // we are laying out just the minimum, but always up to the
1044 // cursor line, so the cursor position is updated.
1045 m_llist->Layout(dc, 0);
1046 return;
1047 }
1048 WXLO_TIMER_START(LayoutTimer);
1049 m_llist->Layout(dc, -1);
1050 WXLO_TIMER_STOP(LayoutTimer);
1051 ResetDirty();
1052
1053 wxPoint max = m_llist->GetSize();
1054 wxSize size = GetClientSize();
1055
1056 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
1057 (long int)max.x, (long int) max.y));
1058
1059 // in the absence of scrollbars we should compare with the client size
1060 if ( !m_hasHScrollbar )
1061 m_maxx = size.x;// - WXLO_ROFFSET;
1062 if ( !m_hasVScrollbar )
1063 m_maxy = size.y;// - WXLO_BOFFSET;
1064
1065 // check if the text hasn't become too big
1066 // TODO why do we set both at once? they're independent...
1067 if( max.x > m_maxx - WXLO_ROFFSET
1068 || max.y > m_maxy - WXLO_BOFFSET
1069 || (max.x < m_maxx - X_SCROLL_PAGE)
1070 || (max.y < m_maxy - Y_SCROLL_PAGE)
1071 || exact )
1072 {
1073 // text became too large
1074 if ( !exact )
1075 {
1076 // add an extra bit to the sizes to avoid future updates
1077 max.x += WXLO_ROFFSET;
1078 max.y += WXLO_BOFFSET;
1079 }
1080
1081 bool done = FALSE;
1082 if(max.x < X_SCROLL_PAGE && m_hasHScrollbar)
1083 {
1084 SetScrollbars(0,-1,0,-1,0,-1,true);
1085 m_hasHScrollbar = FALSE;
1086 done = TRUE;
1087 }
1088 if(max.y < Y_SCROLL_PAGE && m_hasVScrollbar)
1089 {
1090 SetScrollbars(-1,0,-1,0,-1,0,true);
1091 m_hasVScrollbar = FALSE;
1092 done = TRUE;
1093 }
1094 if(! done &&
1095 // (max.x > X_SCROLL_PAGE || max.y > Y_SCROLL_PAGE)
1096 (max.x > size.x - X_SCROLL_PAGE|| max.y > size.y - Y_SCROLL_PAGE)
1097 )
1098 {
1099 GetViewStart(&m_ViewStartX, &m_ViewStartY);
1100 SetScrollbars(X_SCROLL_PAGE,
1101 Y_SCROLL_PAGE,
1102 max.x / X_SCROLL_PAGE + 2,
1103 max.y / Y_SCROLL_PAGE + 2,
1104 m_ViewStartX,
1105 m_ViewStartY,
1106 true);
1107 m_hasHScrollbar =
1108 m_hasVScrollbar = true;
1109 // ScrollToCursor();
1110 }
1111
1112 m_maxx = max.x + X_SCROLL_PAGE;
1113 m_maxy = max.y + Y_SCROLL_PAGE;
1114 }
1115 }
1116
1117 // ----------------------------------------------------------------------------
1118 //
1119 // clipboard operations
1120 //
1121 // ----------------------------------------------------------------------------
1122
1123 void
1124 wxLayoutWindow::Paste(bool usePrivate, bool primary)
1125 {
1126 // this only has an effect under X11:
1127 wxTheClipboard->UsePrimarySelection(primary);
1128 // Read some text
1129 if (wxTheClipboard->Open())
1130 {
1131 if(usePrivate)
1132 {
1133 wxLayoutDataObject wxldo;
1134 if (wxTheClipboard->IsSupported( wxldo.GetFormat() ))
1135 {
1136 if(wxTheClipboard->GetData(wxldo))
1137 {
1138 wxTheClipboard->Close();
1139 wxString str = wxldo.GetLayoutData();
1140 m_llist->Read(str);
1141 SetDirty();
1142 RequestUpdate();
1143 return;
1144 }
1145 }
1146 }
1147 wxTextDataObject data;
1148 if (wxTheClipboard->IsSupported( data.GetFormat() )
1149 && wxTheClipboard->GetData(data) )
1150 {
1151 wxTheClipboard->Close();
1152 wxString text = data.GetText();
1153 wxLayoutImportText( m_llist, text);
1154 SetDirty();
1155 RequestUpdate();
1156 return;
1157 }
1158 }
1159 // if everything failed we can still try the primary:
1160 wxTheClipboard->Close();
1161 if(! primary) // not tried before
1162 {
1163 wxTheClipboard->UsePrimarySelection();
1164 if (wxTheClipboard->Open())
1165 {
1166 wxTextDataObject data;
1167 if (wxTheClipboard->IsSupported( data.GetFormat() )
1168 && wxTheClipboard->GetData(data) )
1169 {
1170 wxString text = data.GetText();
1171 wxLayoutImportText( m_llist, text);
1172 SetDirty();
1173 RequestUpdate();
1174 }
1175 wxTheClipboard->Close();
1176 }
1177 }
1178 }
1179
1180 bool
1181 wxLayoutWindow::Copy(bool invalidate, bool privateFormat, bool primary)
1182 {
1183 // Calling GetSelection() will automatically do an EndSelection()
1184 // on the list, but we need to take a note of it, too:
1185 if(m_Selecting)
1186 {
1187 m_Selecting = false;
1188 m_llist->EndSelection();
1189 }
1190
1191 wxLayoutDataObject *wldo = new wxLayoutDataObject;
1192 wxLayoutList *llist = m_llist->GetSelection(wldo, invalidate);
1193 if(! llist)
1194 return FALSE;
1195 // Export selection as text:
1196 wxString text;
1197 wxLayoutExportObject *exp;
1198 wxLayoutExportStatus status(llist);
1199 while((exp = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
1200 {
1201 if(exp->type == WXLO_EXPORT_TEXT)
1202 text << *(exp->content.text);
1203 delete exp;
1204 }
1205 delete llist;
1206
1207 // The exporter always appends a newline, so we chop it off if it
1208 // is there:
1209 {
1210 size_t len = text.Length();
1211 if(len > 2 && text[len-2] == '\r') // Windows
1212 text = text.Mid(0,len-2);
1213 else if(len > 1 && text[len-1] == '\n')
1214 text = text.Mid(0,len-1);
1215 }
1216
1217 #if 0
1218 if(! primary) // always copy as text-only to primary selection
1219 {
1220 wxTheClipboard->UsePrimarySelection();
1221 if (wxTheClipboard->Open())
1222 {
1223 wxTextDataObject *data = new wxTextDataObject( text );
1224 wxTheClipboard->SetData( data );
1225 wxTheClipboard->Close();
1226 }
1227 }
1228 #endif
1229
1230 wxTheClipboard->UsePrimarySelection(primary);
1231 if (wxTheClipboard->Open())
1232 {
1233 wxTextDataObject *data = new wxTextDataObject( text );
1234 bool rc;
1235
1236 rc = wxTheClipboard->SetData( data );
1237 if(privateFormat)
1238 rc |= wxTheClipboard->SetData( wldo );
1239 wxTheClipboard->Close();
1240 return rc;
1241 }
1242 else
1243 delete wldo;
1244
1245 return FALSE;
1246 }
1247
1248 bool
1249 wxLayoutWindow::Cut(bool privateFormat, bool usePrimary)
1250 {
1251 if(Copy(false, privateFormat, usePrimary)) // do not invalidate selection after copy
1252 {
1253 m_llist->DeleteSelection();
1254 SetDirty();
1255 return TRUE;
1256 }
1257 else
1258 return FALSE;
1259 }
1260
1261 // ----------------------------------------------------------------------------
1262 // searching
1263 // ----------------------------------------------------------------------------
1264
1265 bool
1266 wxLayoutWindow::Find(const wxString &needle,
1267 wxPoint * fromWhere,
1268 const wxString &configPath)
1269 {
1270 #ifdef M_BASEDIR
1271 wxPoint found;
1272
1273 if(needle.Length() == 0)
1274 {
1275 if( ! MInputBox(&m_FindString,
1276 _("Find text"),
1277 _(" Find:"),
1278 this,
1279 configPath, "")
1280 || strutil_isempty(m_FindString))
1281 return true;
1282 }
1283 else
1284 m_FindString = needle;
1285
1286 if(fromWhere == NULL)
1287 found = m_llist->FindText(m_FindString, m_llist->GetCursorPos());
1288 else
1289 found = m_llist->FindText(m_FindString, *fromWhere);
1290 if(found.x != -1)
1291 {
1292 if(fromWhere)
1293 {
1294 *fromWhere = found;
1295 fromWhere->x ++;
1296 }
1297 m_llist->MoveCursorTo(found);
1298 ScrollToCursor();
1299 RequestUpdate();
1300 return true;
1301 }
1302 #endif
1303 return false;
1304 }
1305
1306
1307 bool
1308 wxLayoutWindow::FindAgain(void)
1309 {
1310 bool rc = Find(m_FindString);
1311 return rc;
1312 }
1313
1314 // ----------------------------------------------------------------------------
1315 // popup menu stuff
1316 // ----------------------------------------------------------------------------
1317
1318 wxMenu *
1319 wxLayoutWindow::MakeFormatMenu()
1320 {
1321 wxMenu *m = new wxMenu(_("Layout Menu"));
1322
1323 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
1324 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
1325 m->AppendSeparator();
1326 m->Append(WXLOWIN_MENU_UNDERLINE, _("&Underline"),_("Underline mode."), true);
1327 m->Append(WXLOWIN_MENU_BOLD, _("&Bold"),_("Bold mode."), true);
1328 m->Append(WXLOWIN_MENU_ITALICS, _("&Italics"),_("Italics mode."), true);
1329 m->AppendSeparator();
1330 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
1331 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
1332 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
1333
1334 return m;
1335 }
1336
1337 void wxLayoutWindow::OnUpdateMenuUnderline(wxUpdateUIEvent& event)
1338 {
1339 event.Check(m_llist->IsFontUnderlined());
1340 }
1341
1342 void wxLayoutWindow::OnUpdateMenuBold(wxUpdateUIEvent& event)
1343 {
1344 event.Check(m_llist->IsFontBold());
1345 }
1346
1347 void wxLayoutWindow::OnUpdateMenuItalic(wxUpdateUIEvent& event)
1348 {
1349 event.Check(m_llist->IsFontItalic());
1350 }
1351
1352 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
1353 {
1354 switch (event.GetId())
1355 {
1356 case WXLOWIN_MENU_LARGER:
1357 m_llist->SetFontLarger(); RequestUpdate(); break;
1358 case WXLOWIN_MENU_SMALLER:
1359 m_llist->SetFontSmaller(); RequestUpdate(); break;
1360 case WXLOWIN_MENU_UNDERLINE:
1361 m_llist->ToggleFontUnderline(); RequestUpdate(); break;
1362 case WXLOWIN_MENU_BOLD:
1363 m_llist->ToggleFontWeight(); RequestUpdate(); break;
1364 case WXLOWIN_MENU_ITALICS:
1365 m_llist->ToggleFontItalics(); RequestUpdate(); break;
1366 case WXLOWIN_MENU_ROMAN:
1367 m_llist->SetFontFamily(wxROMAN); RequestUpdate(); break;
1368 case WXLOWIN_MENU_TYPEWRITER:
1369 m_llist->SetFontFamily(wxFIXED); RequestUpdate(); break;
1370 case WXLOWIN_MENU_SANSSERIF:
1371 m_llist->SetFontFamily(wxSWISS); RequestUpdate(); break;
1372 }
1373 }
1374
1375 // ----------------------------------------------------------------------------
1376 // focus
1377 // ----------------------------------------------------------------------------
1378
1379 void
1380 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
1381 {
1382 m_HaveFocus = true;
1383 ev.Skip();
1384 RequestUpdate(); // cursor must change
1385 }
1386
1387 void
1388 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
1389 {
1390 m_HaveFocus = false;
1391 ev.Skip();
1392 RequestUpdate();// cursor must change
1393 }
1394
1395 // ----------------------------------------------------------------------------
1396 // private functions
1397 // ----------------------------------------------------------------------------
1398
1399 static bool IsDirectionKey(long keyCode)
1400 {
1401 switch(keyCode)
1402 {
1403 case WXK_UP:
1404 case WXK_DOWN:
1405 case WXK_RIGHT:
1406 case WXK_LEFT:
1407 case WXK_PRIOR:
1408 case WXK_NEXT:
1409 case WXK_HOME:
1410 case WXK_END:
1411 return true;
1412
1413 default:
1414 return false;
1415 }
1416 }