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