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