1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
4 * (C) 1998, 1999 by Karsten Ballüder (Ballueder@usa.net) *
7 *******************************************************************/
10 # pragma implementation "wxlwindow.h"
13 #include "wx/wxprec.h"
24 # include "gui/wxMenuDefs.h"
25 # include "gui/wxMApp.h"
27 # include "gui/wxlwindow.h"
28 # include "gui/wxlparser.h"
31 # include <wx/msw/private.h>
34 # include "wxlwindow.h"
35 # include "wxlparser.h"
38 #include <wx/clipbrd.h>
39 #include <wx/textctrl.h>
40 #include <wx/dataobj.h>
42 #ifdef WXLAYOUT_USE_CARET
43 # include <wx/caret.h>
44 #endif // WXLAYOUT_USE_CARET
49 # define WXLO_DEBUG(x) wxLogDebug x
51 # define WXLO_DEBUG(x)
54 /// offsets to put a nice frame around text
55 #define WXLO_XOFFSET 4
56 #define WXLO_YOFFSET 4
58 /// offset to the right and bottom for when to redraw scrollbars
59 #define WXLO_ROFFSET 20
60 #define WXLO_BOFFSET 20
62 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
63 EVT_PAINT (wxLayoutWindow::OnPaint
)
64 EVT_CHAR (wxLayoutWindow::OnChar
)
65 EVT_KEY_UP (wxLayoutWindow::OnKeyUp
)
66 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
67 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
68 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
69 EVT_MOTION (wxLayoutWindow::OnMouseMove
)
70 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
71 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
72 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
75 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
76 : wxScrolledWindow(parent
, -1,
77 wxDefaultPosition
, wxDefaultSize
,
78 wxHSCROLL
| wxVSCROLL
|
83 SetStatusBar(NULL
); // don't use statusbar
85 m_doSendEvents
= false;
86 m_ViewStartX
= 0; m_ViewStartY
= 0;
88 m_PopupMenu
= MakeFormatMenu();
89 m_memDC
= new wxMemoryDC
;
90 m_bitmap
= new wxBitmap(4,4);
91 m_bitmapSize
= wxPoint(4,4);
92 m_llist
= new wxLayoutList();
95 m_ScrollToCursor
= false;
97 wxPoint max
= m_llist
->GetSize();
98 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
99 EnableScrolling(true,true);
100 m_maxx
= max
.x
; m_maxy
= max
.y
;
103 #ifdef WXLAYOUT_USE_CARET
104 // FIXME cursor size shouldn't be hardcoded
105 wxCaret
*caret
= new wxCaret(this, 2, 20);
107 m_llist
->SetCaret(caret
);
109 #endif // WXLAYOUT_USE_CARET
111 SetCursorVisibility(-1);
112 SetCursor(wxCURSOR_IBEAM
);
116 wxLayoutWindow::~wxLayoutWindow()
118 delete m_memDC
; // deletes bitmap automatically (?)
122 SetBackgroundBitmap(NULL
);
126 wxLayoutWindow::Clear(int family
,
134 GetLayoutList()->Clear(family
,size
,style
,weight
,underline
,fg
,bg
);
135 SetBackgroundColour(GetLayoutList()->GetDefaults()->GetBGColour());
136 ResizeScrollbars(true);
140 DoPaint((wxRect
*)NULL
);
144 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
146 wxClientDC
dc( this );
148 if ( eventId
!= WXLOWIN_MENU_MOUSEMOVE
)
150 // moving the mouse in a window shouldn't give it the focus!
155 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
156 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
158 findPos
.x
-= WXLO_XOFFSET
;
159 findPos
.y
-= WXLO_YOFFSET
;
161 if(findPos
.x
< 0) findPos
.x
= 0;
162 if(findPos
.y
< 0) findPos
.y
= 0;
164 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
168 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
170 wxLayoutObject::UserData
*u
= obj
? obj
->GetUserData() : NULL
;
172 //has the mouse only been moved?
173 if(eventId
== WXLOWIN_MENU_MOUSEMOVE
)
175 // found is only true if we are really over an object, not just
177 if(found
&& u
&& ! m_Selecting
)
180 SetCursor(wxCURSOR_HAND
);
182 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
184 const wxString
&label
= u
->GetLabel();
186 m_StatusBar
->SetStatusText(label
,
193 SetCursor(wxCURSOR_IBEAM
);
194 m_HandCursor
= FALSE
;
195 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
196 m_StatusBar
->SetStatusText("", m_StatusFieldLabel
);
198 if(event
.LeftIsDown())
202 m_llist
->StartSelection();
208 m_llist
->ContinueSelection(cursorPos
);
212 if(m_Selecting
&& ! event
.LeftIsDown())
214 m_llist
->EndSelection(cursorPos
);
222 // always move cursor to mouse click:
223 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
225 m_llist
->MoveCursorTo(cursorPos
);
226 if(m_CursorVisibility
== -1)
227 m_CursorVisibility
= 1;
229 DoPaint(FALSE
); // DoPaint suppresses flicker under GTK
232 if(!m_doSendEvents
) // nothing to do
238 // only do the menu if activated, editable and not on a clickable object
239 if(eventId
== WXLOWIN_MENU_RCLICK
241 && (! obj
|| u
== NULL
))
243 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
249 // find the object at this position
252 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
253 commandEvent
.SetEventObject( this );
254 commandEvent
.SetClientData((char *)obj
);
255 GetEventHandler()->ProcessEvent(commandEvent
);
260 * Some simple keyboard handling.
263 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
265 int keyCode
= event
.KeyCode();
267 #ifdef WXLAYOUT_DEBUG
268 if(keyCode
== WXK_F1
)
275 if(! m_Selecting
&& event
.ShiftDown())
288 m_llist
->StartSelection();
295 // If needed, make cursor visible:
296 if(m_CursorVisibility
== -1)
297 m_CursorVisibility
= 1;
299 /* These two nested switches work like this:
300 The first one processes all non-editing keycodes, to move the
301 cursor, etc. It's default will process all keycodes causing
302 modifications to the buffer, but only if editing is allowed.
304 bool ctrlDown
= event
.ControlDown();
309 m_llist
->MoveCursorWord(1);
311 m_llist
->MoveCursorHorizontally(1);
315 m_llist
->MoveCursorWord(-1);
317 m_llist
->MoveCursorHorizontally(-1);
320 m_llist
->MoveCursorVertically(-1);
323 m_llist
->MoveCursorVertically(1);
326 m_llist
->MoveCursorVertically(-20);
329 m_llist
->MoveCursorVertically(20);
333 m_llist
->MoveCursorTo(wxPoint(0, 0));
335 m_llist
->MoveCursorToBeginOfLine();
340 // TODO VZ: how to move the cursor to the last line of the text?
341 m_llist
->MoveCursorVertically(1000);
343 m_llist
->MoveCursorToEndOfLine();
346 if(keyCode
== 'c' && ctrlDown
)
348 // this should work even in read-only mode
351 else if( IsEditable() )
353 /* First, handle control keys */
354 if(event
.ControlDown() && ! event
.AltDown())
366 m_llist
->DeleteLines(1);
368 case 'h': // like backspace
369 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
372 m_llist
->DeleteToBeginOfLine();
375 m_llist
->DeleteToEndOfLine();
383 #ifdef WXLAYOUT_DEBUG
385 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
393 else if( event
.AltDown() && ! event
.ControlDown() )
399 m_llist
->DeleteWord();
406 else if ( ! event
.AltDown() && ! event
.ControlDown())
411 if(event
.ShiftDown())
415 if(event
.ShiftDown())
420 case WXK_BACK
: // backspace
421 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
425 m_llist
->WrapLine(m_WrapMargin
);
426 m_llist
->LineBreak();
429 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
430 && (keyCode
< 256 && keyCode
>= 32)
435 if(m_WrapMargin
> 0 && isspace(keyCode
))
436 m_llist
->WrapLine(m_WrapMargin
);
437 m_llist
->Insert(tmp
);
448 if(event
.ShiftDown())
449 m_llist
->ContinueSelection();
452 m_llist
->EndSelection();
458 wxRect r
= *m_llist
->GetUpdateRect();
463 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
465 if(event
.KeyCode() == WXK_SHIFT
&& m_llist
->IsSelecting())
467 m_llist
->EndSelection();
475 wxLayoutWindow::ScrollToCursor(void)
477 wxClientDC
dc( this );
480 int x0
,y0
,x1
,y1
, dx
, dy
;
482 // Calculate where the top of the visible area is:
484 GetScrollPixelsPerUnit(&dx
, &dy
);
487 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0
, y0
));
489 // Get the size of the visible window:
490 GetClientSize(&x1
,&y1
);
493 // As we have the values anyway, use them to avoid unnecessary
494 // scrollbar updates.
495 if(x1
> m_maxx
) m_maxx
= x1
;
496 if(y1
> m_maxy
) m_maxy
= y1
;
497 /* Make sure that the scrollbars are at a position so that the
498 cursor is visible if we are editing. */
499 /** Scroll so that cursor is visible! */
500 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
501 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
502 if(cc
.x
< x0
|| cc
.y
< y0
503 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
506 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
507 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
508 Scroll(nx
/dx
,ny
/dy
); // new view start
510 m_ScrollToCursor
= false; // avoid recursion
515 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
517 wxRect region
= GetUpdateRegion().GetBox();
518 InternalPaint(& region
);
522 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
525 InternalPaint(updateRect
);
527 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
529 if ( !::UpdateWindow(GetHwnd()) )
530 wxLogLastError("UpdateWindow");
535 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
537 wxPaintDC
dc( this );
540 #ifdef WXLAYOUT_USE_CARET
541 // hide the caret before drawing anything
543 #endif // WXLAYOUT_USE_CARET
545 int x0
,y0
,x1
,y1
, dx
, dy
;
547 // Calculate where the top of the visible area is:
549 GetScrollPixelsPerUnit(&dx
, &dy
);
552 // Get the size of the visible window:
553 GetClientSize(&x1
,&y1
);
556 // As we have the values anyway, use them to avoid unnecessary
557 // scrollbar updates.
558 if(x1
> m_maxx
) m_maxx
= x1
;
559 if(y1
> m_maxy
) m_maxy
= y1
;
564 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
565 updateRect
->x
, updateRect
->y
,
566 updateRect
->x
+updateRect
->width
,
567 updateRect
->y
+updateRect
->height
));
574 /* Check whether the window has grown, if so, we need to reallocate
575 the bitmap to be larger. */
576 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
578 wxASSERT(m_bitmapSize
.x
> 0);
579 wxASSERT(m_bitmapSize
.y
> 0);
581 m_memDC
->SelectObject(wxNullBitmap
);
583 m_bitmapSize
= wxPoint(x1
,y1
);
584 m_bitmap
= new wxBitmap(x1
,y1
);
585 m_memDC
->SelectObject(*m_bitmap
);
588 m_memDC
->SetDeviceOrigin(0,0);
589 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(),wxSOLID
));
590 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),
592 m_memDC
->SetLogicalFunction(wxCOPY
);
594 /* Either fill the background with the background bitmap, or clear
600 w
= m_BGbitmap
->GetWidth(),
601 h
= m_BGbitmap
->GetHeight();
602 for(y
= 0; y
< y1
; y
+=h
)
603 for(x
= 0; x
< x1
; x
+=w
)
604 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
605 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
609 // clear the background: (must not be done if we use the update rectangle!)
610 m_memDC
->SetBackgroundMode(wxSOLID
);
611 m_memDC
->DrawRectangle(0,0,x1
, y1
);
615 /* This is the important bit: we tell the list to draw itself: */
619 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
620 updateRect
->x
, updateRect
->y
,
621 updateRect
->x
+updateRect
->width
,
622 updateRect
->y
+updateRect
->height
));
626 // Device origins on the memDC are suspect, we translate manually
627 // with the translate parameter of Draw().
628 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
629 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
631 // We start calculating a new update rect before drawing the
632 // cursor, so that the cursor coordinates get included in the next
633 // update rectangle (although they are drawn on the memDC, this is
634 // needed to erase it):
635 m_llist
->InvalidateUpdateRect();
636 if(m_CursorVisibility
!= 0)
637 m_llist
->DrawCursor(*m_memDC
,
638 m_HaveFocus
&& IsEditable(), // draw a thick
639 // cursor for editable windows with focus
642 // Now copy everything to the screen:
644 // This somehow doesn't work, but even the following bit with the
645 // whole rect at once is still a bit broken I think.
646 wxRegionIterator
ri ( GetUpdateRegion() );
650 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
651 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
652 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
653 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
659 // FIXME: Trying to copy only the changed parts, but it does not seem
661 // x0 = updateRect->x; y0 = updateRect->y;
662 // if(updateRect->height < y1)
663 // y1 = updateRect->height;
664 // y1 += WXLO_YOFFSET; //FIXME might not be needed
665 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
668 #ifdef WXLAYOUT_USE_CARET
669 // show the caret back after everything is redrawn
671 #endif // WXLAYOUT_USE_CARET
674 m_ScrollToCursor
= false;
675 if(m_StatusBar
&& m_StatusFieldCursor
!= -1)
678 label
.Printf(_("Ln:%d Col:%d"),
679 m_llist
->GetCursorPos().y
+1,
680 m_llist
->GetCursorPos().x
+1);
681 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
685 // change the range and position of scrollbars
687 wxLayoutWindow::ResizeScrollbars(bool exact
)
689 wxPoint max
= m_llist
->GetSize();
691 WXLO_DEBUG(("ResizeScrollbars: GetSize: %ld, %ld", (long int)max
.x
,
693 if(max
.x
> m_maxx
|| max
.y
> m_maxy
694 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
699 // add an extra bit to the sizes to avoid future updates
700 max
.x
= max
.x
+WXLO_ROFFSET
;
701 max
.y
= max
.y
+WXLO_BOFFSET
;
703 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
704 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
705 m_maxx
= max
.x
; m_maxy
= max
.y
;
710 wxLayoutWindow::Paste(void)
713 if (wxTheClipboard
->Open())
715 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
716 wxLayoutDataObject wxldo
;
717 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
719 wxTheClipboard
->GetData(&wxldo
);
722 //FIXME: missing functionality m_llist->Insert(wxldo.GetList());
727 wxTextDataObject data
;
728 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
730 wxTheClipboard
->GetData(&data
);
731 wxString text
= data
.GetText();
732 wxLayoutImportText( m_llist
, text
);
735 wxTheClipboard
->Close();
739 /* My attempt to get the primary selection, but it does not
741 if(text
.Length() == 0)
743 wxTextCtrl
tmp_tctrl(this,-1);
745 text
+= tmp_tctrl
.GetValue();
751 wxLayoutWindow::Copy(bool invalidate
)
753 // Calling GetSelection() will automatically do an EndSelection()
754 // on the list, but we need to take a note of it, too:
758 m_llist
->EndSelection();
761 wxLayoutDataObject wldo
;
762 wxLayoutList
*llist
= m_llist
->GetSelection(&wldo
, invalidate
);
765 // Export selection as text:
767 wxLayoutExportObject
*export
;
768 wxLayoutExportStatus
status(llist
);
769 while((export
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
771 if(export
->type
== WXLO_EXPORT_TEXT
)
772 text
<< *(export
->content
.text
);
777 // The exporter always appends a newline, so we chop it off if it
780 size_t len
= text
.Length();
781 if(len
> 2 && text
[len
-2] == '\r') // Windows
782 text
= text
.Mid(0,len
-2);
783 else if(len
> 1 && text
[len
-1] == '\n')
784 text
= text
.Mid(0,len
-1);
788 if (wxTheClipboard
->Open())
790 wxTextDataObject
*data
= new wxTextDataObject( text
);
791 bool rc
= wxTheClipboard
->SetData( data
);
792 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
793 rc
|= wxTheClipboard
->AddData( &wldo
);
795 wxTheClipboard
->Close();
803 wxLayoutWindow::Cut(void)
805 if(Copy(false)) // do not invalidate selection after copy
807 m_llist
->DeleteSelection();
814 wxLayoutWindow::Find(const wxString
&needle
,
819 if(fromWhere
== NULL
)
820 found
= m_llist
->FindText(needle
, m_llist
->GetCursorPos());
822 found
= m_llist
->FindText(needle
, *fromWhere
);
830 m_llist
->MoveCursorTo(found
);
838 wxLayoutWindow::MakeFormatMenu()
840 wxMenu
*m
= new wxMenu(_("Layout Menu"));
842 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
843 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
844 m
->AppendSeparator();
845 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
846 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
847 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
848 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
849 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
850 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
851 m
->AppendSeparator();
852 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
853 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
854 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
858 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
860 switch (event
.GetId())
862 case WXLOWIN_MENU_LARGER
:
863 m_llist
->SetFontLarger(); break;
864 case WXLOWIN_MENU_SMALLER
:
865 m_llist
->SetFontSmaller(); break;
866 case WXLOWIN_MENU_UNDERLINE_ON
:
867 m_llist
->SetFontUnderline(true); break;
868 case WXLOWIN_MENU_UNDERLINE_OFF
:
869 m_llist
->SetFontUnderline(false); break;
870 case WXLOWIN_MENU_BOLD_ON
:
871 m_llist
->SetFontWeight(wxBOLD
); break;
872 case WXLOWIN_MENU_BOLD_OFF
:
873 m_llist
->SetFontWeight(wxNORMAL
); break;
874 case WXLOWIN_MENU_ITALICS_ON
:
875 m_llist
->SetFontStyle(wxITALIC
); break;
876 case WXLOWIN_MENU_ITALICS_OFF
:
877 m_llist
->SetFontStyle(wxNORMAL
); break;
878 case WXLOWIN_MENU_ROMAN
:
879 m_llist
->SetFontFamily(wxROMAN
); break;
880 case WXLOWIN_MENU_TYPEWRITER
:
881 m_llist
->SetFontFamily(wxFIXED
); break;
882 case WXLOWIN_MENU_SANSSERIF
:
883 m_llist
->SetFontFamily(wxSWISS
); break;
888 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
895 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)