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