]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlwindow.cpp
98239d518464e10a6ba38d567b3a8857e7c86527
[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 #ifdef __WXGTK__
637 InternalPaint(updateRect);
638 #else // Causes bad flicker under wxGTK!!!
639 Refresh(FALSE); //, updateRect);
640
641 if ( !::UpdateWindow(GetHwnd()) )
642 wxLogLastError("UpdateWindow");
643 #endif
644 }
645
646 void
647 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
648 {
649 wxPaintDC dc( this );
650 PrepareDC( dc );
651
652 #ifdef WXLAYOUT_USE_CARET
653 // hide the caret before drawing anything
654 GetCaret()->Hide();
655 #endif // WXLAYOUT_USE_CARET
656
657 int x0,y0,x1,y1, dx, dy;
658
659 // Calculate where the top of the visible area is:
660 ViewStart(&x0,&y0);
661 GetScrollPixelsPerUnit(&dx, &dy);
662 x0 *= dx; y0 *= dy;
663
664 // Get the size of the visible window:
665 GetClientSize(&x1,&y1);
666 wxASSERT(x1 >= 0);
667 wxASSERT(y1 >= 0);
668
669 if(updateRect)
670 {
671 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
672 updateRect->x, updateRect->y,
673 updateRect->x+updateRect->width,
674 updateRect->y+updateRect->height));
675 }
676 if(IsDirty())
677 {
678 m_llist->Layout(dc);
679 ResizeScrollbars();
680 }
681 /* Check whether the window has grown, if so, we need to reallocate
682 the bitmap to be larger. */
683 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
684 {
685 wxASSERT(m_bitmapSize.x > 0);
686 wxASSERT(m_bitmapSize.y > 0);
687
688 m_memDC->SelectObject(wxNullBitmap);
689 delete m_bitmap;
690 m_bitmapSize = wxPoint(x1,y1);
691 m_bitmap = new wxBitmap(x1,y1);
692 m_memDC->SelectObject(*m_bitmap);
693 }
694
695 m_memDC->SetDeviceOrigin(0,0);
696 m_memDC->SetBrush(wxBrush(m_llist->GetDefaultStyleInfo().GetBGColour(),wxSOLID));
697 m_memDC->SetPen(wxPen(m_llist->GetDefaultStyleInfo().GetBGColour(),
698 0,wxTRANSPARENT));
699 m_memDC->SetLogicalFunction(wxCOPY);
700 m_memDC->Clear();
701
702 // fill the background with the background bitmap
703 if(m_BGbitmap)
704 {
705 CoordType
706 y, x,
707 w = m_BGbitmap->GetWidth(),
708 h = m_BGbitmap->GetHeight();
709 for(y = 0; y < y1; y+=h)
710 for(x = 0; x < x1; x+=w)
711 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
712 m_memDC->SetBackgroundMode(wxTRANSPARENT);
713 }
714
715 // This is the important bit: we tell the list to draw itself
716 #if WXLO_DEBUG_URECT
717 if(updateRect)
718 {
719 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
720 updateRect->x, updateRect->y,
721 updateRect->x+updateRect->width,
722 updateRect->y+updateRect->height));
723 }
724 #endif
725
726 // Device origins on the memDC are suspect, we translate manually
727 // with the translate parameter of Draw().
728 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
729 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
730
731 // We start calculating a new update rect before drawing the
732 // cursor, so that the cursor coordinates get included in the next
733 // update rectangle (although they are drawn on the memDC, this is
734 // needed to erase it):
735 m_llist->InvalidateUpdateRect();
736 if(m_CursorVisibility != 0)
737 {
738 // draw a thick cursor for editable windows with focus
739 m_llist->DrawCursor(*m_memDC,
740 m_HaveFocus && IsEditable(),
741 offset);
742 }
743
744 // Now copy everything to the screen:
745 #if 0
746 // This somehow doesn't work, but even the following bit with the
747 // whole rect at once is still a bit broken I think.
748 wxRegionIterator ri ( GetUpdateRegion() );
749 if(ri)
750 while(ri)
751 {
752 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
753 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH()));
754 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
755 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
756 ri++;
757 }
758 else
759 #endif
760 {
761 // FIXME: Trying to copy only the changed parts, but it does not seem
762 // to work:
763 // x0 = updateRect->x; y0 = updateRect->y;
764 // if(updateRect->height < y1)
765 // y1 = updateRect->height;
766 // y1 += WXLO_YOFFSET; //FIXME might not be needed
767 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
768 }
769
770 #ifdef WXLAYOUT_USE_CARET
771 // show the caret back after everything is redrawn
772 m_caret->Show();
773 #endif // WXLAYOUT_USE_CARET
774
775 ResetDirty();
776 m_ScrollToCursor = false;
777
778 if ( m_StatusBar && m_StatusFieldCursor != -1 )
779 {
780 static wxPoint s_oldCursorPos(-1, -1);
781
782 wxPoint pos(m_llist->GetCursorPos());
783
784 // avoid unnecessary status bar refreshes
785 if ( pos != s_oldCursorPos )
786 {
787 s_oldCursorPos = pos;
788
789 wxString label;
790 label.Printf(_("Ln:%d Col:%d"), pos.y + 1, pos.x + 1);
791 m_StatusBar->SetStatusText(label, m_StatusFieldCursor);
792 }
793 }
794 }
795
796 // change the range and position of scrollbars
797 void
798 wxLayoutWindow::ResizeScrollbars(bool exact)
799 {
800 wxPoint max = m_llist->GetSize();
801
802 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
803 (long int)max.x, (long int) max.y));
804
805 if( max.x > m_maxx - WXLO_ROFFSET || max.y > m_maxy - WXLO_BOFFSET || exact )
806 {
807 if ( !exact )
808 {
809 // add an extra bit to the sizes to avoid future updates
810 max.x += WXLO_ROFFSET;
811 max.y += WXLO_BOFFSET;
812 }
813
814 ViewStart(&m_ViewStartX, &m_ViewStartY);
815 SetScrollbars(X_SCROLL_PAGE, Y_SCROLL_PAGE,
816 max.x / X_SCROLL_PAGE + 1, max.y / Y_SCROLL_PAGE + 1,
817 m_ViewStartX, m_ViewStartY,
818 true);
819
820 m_maxx = max.x + X_SCROLL_PAGE;
821 m_maxy = max.y + Y_SCROLL_PAGE;
822 }
823 }
824
825 void
826 wxLayoutWindow::Paste(void)
827 {
828 // Read some text
829 if (wxTheClipboard->Open())
830 {
831 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
832 wxLayoutDataObject wxldo;
833 if (wxTheClipboard->IsSupported( wxldo.GetFormat() ))
834 {
835 wxTheClipboard->GetData(&wxldo);
836 {
837 }
838 //FIXME: missing functionality m_llist->Insert(wxldo.GetList());
839 }
840 else
841 #endif
842 {
843 wxTextDataObject data;
844 if (wxTheClipboard->IsSupported( data.GetFormat() ))
845 {
846 wxTheClipboard->GetData(&data);
847 wxString text = data.GetText();
848 wxLayoutImportText( m_llist, text);
849 }
850 }
851 wxTheClipboard->Close();
852 }
853
854 #if 0
855 /* My attempt to get the primary selection, but it does not
856 work. :-( */
857 if(text.Length() == 0)
858 {
859 wxTextCtrl tmp_tctrl(this,-1);
860 tmp_tctrl.Paste();
861 text += tmp_tctrl.GetValue();
862 }
863 #endif
864 }
865
866 bool
867 wxLayoutWindow::Copy(bool invalidate)
868 {
869 // Calling GetSelection() will automatically do an EndSelection()
870 // on the list, but we need to take a note of it, too:
871 if(m_Selecting)
872 {
873 m_Selecting = false;
874 m_llist->EndSelection();
875 }
876
877 wxLayoutDataObject wldo;
878 wxLayoutList *llist = m_llist->GetSelection(&wldo, invalidate);
879 if(! llist)
880 return FALSE;
881 // Export selection as text:
882 wxString text;
883 wxLayoutExportObject *export;
884 wxLayoutExportStatus status(llist);
885 while((export = wxLayoutExport( &status, WXLO_EXPORT_AS_TEXT)) != NULL)
886 {
887 if(export->type == WXLO_EXPORT_TEXT)
888 text << *(export->content.text);
889 delete export;
890 }
891 delete llist;
892
893 // The exporter always appends a newline, so we chop it off if it
894 // is there:
895 {
896 size_t len = text.Length();
897 if(len > 2 && text[len-2] == '\r') // Windows
898 text = text.Mid(0,len-2);
899 else if(len > 1 && text[len-1] == '\n')
900 text = text.Mid(0,len-1);
901 }
902
903
904 if (wxTheClipboard->Open())
905 {
906 wxTextDataObject *data = new wxTextDataObject( text );
907 bool rc = wxTheClipboard->SetData( data );
908 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
909 rc |= wxTheClipboard->AddData( &wldo );
910 #endif
911 wxTheClipboard->Close();
912 return rc;
913 }
914
915 return FALSE;
916 }
917
918 bool
919 wxLayoutWindow::Cut(void)
920 {
921 if(Copy(false)) // do not invalidate selection after copy
922 {
923 m_llist->DeleteSelection();
924 return TRUE;
925 }
926 else
927 return FALSE;
928 }
929 bool
930 wxLayoutWindow::Find(const wxString &needle,
931 wxPoint * fromWhere)
932 {
933 wxPoint found;
934
935 if(fromWhere == NULL)
936 found = m_llist->FindText(needle, m_llist->GetCursorPos());
937 else
938 found = m_llist->FindText(needle, *fromWhere);
939 if(found.x != -1)
940 {
941 if(fromWhere)
942 {
943 *fromWhere = found;
944 fromWhere->x ++;
945 }
946 m_llist->MoveCursorTo(found);
947 ScrollToCursor();
948 return true;
949 }
950 return false;
951 }
952
953 wxMenu *
954 wxLayoutWindow::MakeFormatMenu()
955 {
956 wxMenu *m = new wxMenu(_("Layout Menu"));
957
958 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
959 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
960 m->AppendSeparator();
961 m->Append(WXLOWIN_MENU_UNDERLINE, _("&Underline"),_("Underline mode."), true);
962 m->Append(WXLOWIN_MENU_BOLD, _("&Bold"),_("Bold mode."), true);
963 m->Append(WXLOWIN_MENU_ITALICS, _("&Italics"),_("Italics mode."), true);
964 m->AppendSeparator();
965 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
966 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
967 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
968
969 return m;
970 }
971
972 void wxLayoutWindow::OnUpdateMenuUnderline(wxUpdateUIEvent& event)
973 {
974 event.Check(m_llist->IsFontUnderlined());
975 }
976
977 void wxLayoutWindow::OnUpdateMenuBold(wxUpdateUIEvent& event)
978 {
979 event.Check(m_llist->IsFontBold());
980 }
981
982 void wxLayoutWindow::OnUpdateMenuItalic(wxUpdateUIEvent& event)
983 {
984 event.Check(m_llist->IsFontItalic());
985 }
986
987 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
988 {
989 switch (event.GetId())
990 {
991 case WXLOWIN_MENU_LARGER:
992 m_llist->SetFontLarger();
993 break;
994 case WXLOWIN_MENU_SMALLER:
995 m_llist->SetFontSmaller();
996 break;
997
998 case WXLOWIN_MENU_UNDERLINE:
999 m_llist->ToggleFontUnderline();
1000 break;
1001 case WXLOWIN_MENU_BOLD:
1002 m_llist->ToggleFontWeight();
1003 break;
1004 case WXLOWIN_MENU_ITALICS:
1005 m_llist->ToggleFontItalics();
1006 break;
1007
1008 case WXLOWIN_MENU_ROMAN:
1009 m_llist->SetFontFamily(wxROMAN); break;
1010 case WXLOWIN_MENU_TYPEWRITER:
1011 m_llist->SetFontFamily(wxFIXED); break;
1012 case WXLOWIN_MENU_SANSSERIF:
1013 m_llist->SetFontFamily(wxSWISS); break;
1014 }
1015 }
1016
1017 void
1018 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
1019 {
1020 m_HaveFocus = true;
1021 ev.Skip();
1022 }
1023
1024 void
1025 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
1026 {
1027 m_HaveFocus = false;
1028 ev.Skip();
1029 }