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"
37 # include "wxlwindow.h"
38 # include "wxlparser.h"
41 #include <wx/clipbrd.h>
42 #include <wx/textctrl.h>
43 #include <wx/dataobj.h>
45 #ifdef WXLAYOUT_USE_CARET
46 # include <wx/caret.h>
47 #endif // WXLAYOUT_USE_CARET
52 # define WXLO_DEBUG(x) wxLogDebug x
54 # define WXLO_DEBUG(x)
57 /// offsets to put a nice frame around text
58 #define WXLO_XOFFSET 4
59 #define WXLO_YOFFSET 4
61 /// offset to the right and bottom for when to redraw scrollbars
62 #define WXLO_ROFFSET 20
63 #define WXLO_BOFFSET 20
65 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
66 EVT_PAINT (wxLayoutWindow::OnPaint
)
67 EVT_CHAR (wxLayoutWindow::OnChar
)
68 EVT_KEY_UP (wxLayoutWindow::OnKeyUp
)
69 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
70 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
71 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
72 EVT_MOTION (wxLayoutWindow::OnMouseMove
)
73 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
74 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
75 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
78 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
79 : wxScrolledWindow(parent
, -1,
80 wxDefaultPosition
, wxDefaultSize
,
81 wxHSCROLL
| wxVSCROLL
| wxBORDER
)
84 SetStatusBar(NULL
); // don't use statusbar
86 m_doSendEvents
= false;
87 m_ViewStartX
= 0; m_ViewStartY
= 0;
89 m_PopupMenu
= MakeFormatMenu();
90 m_memDC
= new wxMemoryDC
;
91 m_bitmap
= new wxBitmap(4,4);
92 m_bitmapSize
= wxPoint(4,4);
93 m_llist
= new wxLayoutList();
96 m_ScrollToCursor
= false;
98 wxPoint max
= m_llist
->GetSize();
99 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
100 EnableScrolling(true,true);
101 m_maxx
= max
.x
; m_maxy
= max
.y
;
104 #ifdef WXLAYOUT_USE_CARET
105 // FIXME cursor size shouldn't be hardcoded
106 wxCaret
*caret
= new wxCaret(this, 2, 20);
108 m_llist
->SetCaret(caret
);
110 #endif // WXLAYOUT_USE_CARET
112 SetCursorVisibility(-1);
113 SetCursor(wxCURSOR_IBEAM
);
117 wxLayoutWindow::~wxLayoutWindow()
119 delete m_memDC
; // deletes bitmap automatically (?)
123 SetBackgroundBitmap(NULL
);
127 wxLayoutWindow::Clear(int family
,
135 GetLayoutList()->Clear(family
,size
,style
,weight
,underline
,fg
,bg
);
136 SetBackgroundColour(GetLayoutList()->GetDefaults()->GetBGColour());
137 ResizeScrollbars(true);
141 DoPaint((wxRect
*)NULL
);
146 wxLayoutWindow::MSWGetDlgCode()
148 // if we don't return this, we won't get OnChar() events for TABs and ENTER
149 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
154 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
156 wxPaintDC
dc( this );
161 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
162 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
164 findPos
.x
-= WXLO_XOFFSET
;
165 findPos
.y
-= WXLO_YOFFSET
;
167 if(findPos
.x
< 0) findPos
.x
= 0;
168 if(findPos
.y
< 0) findPos
.y
= 0;
170 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
174 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
176 wxLayoutObject::UserData
*u
= obj
? obj
->GetUserData() : NULL
;
178 //has the mouse only been moved?
179 if(eventId
== WXLOWIN_MENU_MOUSEMOVE
)
181 // found is only true if we are really over an object, not just
183 if(found
&& u
&& ! m_Selecting
)
186 SetCursor(wxCURSOR_HAND
);
188 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
190 const wxString
&label
= u
->GetLabel();
192 m_StatusBar
->SetStatusText(label
,
199 SetCursor(wxCURSOR_IBEAM
);
200 m_HandCursor
= FALSE
;
201 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
202 m_StatusBar
->SetStatusText("", m_StatusFieldLabel
);
204 if(event
.LeftIsDown())
208 m_llist
->StartSelection();
214 m_llist
->ContinueSelection(cursorPos
);
218 if(m_Selecting
&& ! event
.LeftIsDown())
220 m_llist
->EndSelection(cursorPos
);
228 // always move cursor to mouse click:
229 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
231 m_llist
->MoveCursorTo(cursorPos
);
232 if(m_CursorVisibility
== -1)
233 m_CursorVisibility
= 1;
235 DoPaint(FALSE
); // DoPaint suppresses flicker under GTK
238 if(!m_doSendEvents
) // nothing to do
244 // only do the menu if activated, editable and not on a clickable object
245 if(eventId
== WXLOWIN_MENU_RCLICK
247 && (! obj
|| u
== NULL
))
249 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
255 // find the object at this position
258 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
259 commandEvent
.SetEventObject( this );
260 commandEvent
.SetClientData((char *)obj
);
261 GetEventHandler()->ProcessEvent(commandEvent
);
266 * Some simple keyboard handling.
269 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
271 int keyCode
= event
.KeyCode();
273 #ifdef WXLAYOUT_DEBUG
274 if(keyCode
== WXK_F1
)
281 if(! m_Selecting
&& event
.ShiftDown())
294 m_llist
->StartSelection();
301 // If needed, make cursor visible:
302 if(m_CursorVisibility
== -1)
303 m_CursorVisibility
= 1;
305 /* These two nested switches work like this:
306 The first one processes all non-editing keycodes, to move the
307 cursor, etc. It's default will process all keycodes causing
308 modifications to the buffer, but only if editing is allowed.
313 m_llist
->MoveCursorHorizontally(1);
316 m_llist
->MoveCursorHorizontally(-1);
319 m_llist
->MoveCursorVertically(-1);
322 m_llist
->MoveCursorVertically(1);
325 m_llist
->MoveCursorVertically(-20);
328 m_llist
->MoveCursorVertically(20);
331 m_llist
->MoveCursorToBeginOfLine();
334 m_llist
->MoveCursorToEndOfLine();
337 if(keyCode
== 'c' && event
.ControlDown())
339 // this should work even in read-only mode
344 /* First, handle control keys */
345 if(event
.ControlDown() && ! event
.AltDown())
357 m_llist
->DeleteLines(1);
359 case 'h': // like backspace
360 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
363 m_llist
->DeleteToBeginOfLine();
366 m_llist
->DeleteToEndOfLine();
374 #ifdef WXLAYOUT_DEBUG
376 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
384 else if( event
.AltDown() && ! event
.ControlDown() )
390 m_llist
->DeleteWord();
397 else if ( ! event
.AltDown() && ! event
.ControlDown())
402 if(event
.ShiftDown())
406 if(event
.ShiftDown())
411 case WXK_BACK
: // backspace
412 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
416 m_llist
->WrapLine(m_WrapMargin
);
417 m_llist
->LineBreak();
420 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
421 && (keyCode
< 256 && keyCode
>= 32)
426 if(m_WrapMargin
> 0 && isspace(keyCode
))
427 m_llist
->WrapLine(m_WrapMargin
);
428 m_llist
->Insert(tmp
);
439 if(event
.ShiftDown())
440 m_llist
->ContinueSelection();
443 m_llist
->EndSelection();
449 wxRect r
= *m_llist
->GetUpdateRect();
454 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
456 if(event
.KeyCode() == WXK_SHIFT
&& m_llist
->IsSelecting())
458 m_llist
->EndSelection();
466 wxLayoutWindow::ScrollToCursor(void)
468 wxClientDC
dc( this );
471 int x0
,y0
,x1
,y1
, dx
, dy
;
473 // Calculate where the top of the visible area is:
475 GetScrollPixelsPerUnit(&dx
, &dy
);
478 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0
, y0
));
480 // Get the size of the visible window:
481 GetClientSize(&x1
,&y1
);
484 // As we have the values anyway, use them to avoid unnecessary
485 // scrollbar updates.
486 if(x1
> m_maxx
) m_maxx
= x1
;
487 if(y1
> m_maxy
) m_maxy
= y1
;
488 /* Make sure that the scrollbars are at a position so that the
489 cursor is visible if we are editing. */
490 /** Scroll so that cursor is visible! */
491 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
492 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
493 if(cc
.x
< x0
|| cc
.y
< y0
494 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
497 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
498 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
499 Scroll(nx
/dx
,ny
/dy
); // new view start
501 m_ScrollToCursor
= false; // avoid recursion
506 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
508 wxRect region
= GetUpdateRegion().GetBox();
509 InternalPaint(& region
);
513 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
516 InternalPaint(updateRect
);
518 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
523 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
525 wxPaintDC
dc( this );
528 #ifdef WXLAYOUT_USE_CARET
529 // hide the caret before drawing anything
531 #endif // WXLAYOUT_USE_CARET
533 int x0
,y0
,x1
,y1
, dx
, dy
;
535 // Calculate where the top of the visible area is:
537 GetScrollPixelsPerUnit(&dx
, &dy
);
540 // Get the size of the visible window:
541 GetClientSize(&x1
,&y1
);
544 // As we have the values anyway, use them to avoid unnecessary
545 // scrollbar updates.
546 if(x1
> m_maxx
) m_maxx
= x1
;
547 if(y1
> m_maxy
) m_maxy
= y1
;
552 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
553 updateRect
->x
, updateRect
->y
,
554 updateRect
->x
+updateRect
->width
,
555 updateRect
->y
+updateRect
->height
));
562 /* Check whether the window has grown, if so, we need to reallocate
563 the bitmap to be larger. */
564 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
566 wxASSERT(m_bitmapSize
.x
> 0);
567 wxASSERT(m_bitmapSize
.y
> 0);
569 m_memDC
->SelectObject(wxNullBitmap
);
571 m_bitmapSize
= wxPoint(x1
,y1
);
572 m_bitmap
= new wxBitmap(x1
,y1
);
573 m_memDC
->SelectObject(*m_bitmap
);
576 m_memDC
->SetDeviceOrigin(0,0);
577 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(),wxSOLID
));
578 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),
580 m_memDC
->SetLogicalFunction(wxCOPY
);
582 /* Either fill the background with the background bitmap, or clear
588 w
= m_BGbitmap
->GetWidth(),
589 h
= m_BGbitmap
->GetHeight();
590 for(y
= 0; y
< y1
; y
+=h
)
591 for(x
= 0; x
< x1
; x
+=w
)
592 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
593 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
597 // clear the background: (must not be done if we use the update rectangle!)
598 m_memDC
->SetBackgroundMode(wxSOLID
);
599 m_memDC
->DrawRectangle(0,0,x1
, y1
);
603 /* This is the important bit: we tell the list to draw itself: */
607 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
608 updateRect
->x
, updateRect
->y
,
609 updateRect
->x
+updateRect
->width
,
610 updateRect
->y
+updateRect
->height
));
614 // Device origins on the memDC are suspect, we translate manually
615 // with the translate parameter of Draw().
616 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
617 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
619 // We start calculating a new update rect before drawing the
620 // cursor, so that the cursor coordinates get included in the next
621 // update rectangle (although they are drawn on the memDC, this is
622 // needed to erase it):
623 m_llist
->InvalidateUpdateRect();
624 if(m_CursorVisibility
!= 0)
625 m_llist
->DrawCursor(*m_memDC
,
626 m_HaveFocus
&& IsEditable(), // draw a thick
627 // cursor for editable windows with focus
630 // Now copy everything to the screen:
632 // This somehow doesn't work, but even the following bit with the
633 // whole rect at once is still a bit broken I think.
634 wxRegionIterator
ri ( GetUpdateRegion() );
638 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
639 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
640 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
641 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
647 // FIXME: Trying to copy only the changed parts, but it does not seem
649 // x0 = updateRect->x; y0 = updateRect->y;
650 // if(updateRect->height < y1)
651 // y1 = updateRect->height;
652 // y1 += WXLO_YOFFSET; //FIXME might not be needed
653 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
656 #ifdef WXLAYOUT_USE_CARET
657 // show the caret back after everything is redrawn
659 #endif // WXLAYOUT_USE_CARET
662 m_ScrollToCursor
= false;
663 if(m_StatusBar
&& m_StatusFieldCursor
!= -1)
666 label
.Printf(_("Ln:%d Col:%d"),
667 m_llist
->GetCursorPos().y
+1,
668 m_llist
->GetCursorPos().x
+1);
669 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
673 // change the range and position of scrollbars
675 wxLayoutWindow::ResizeScrollbars(bool exact
)
677 wxPoint max
= m_llist
->GetSize();
679 WXLO_DEBUG(("ResizeScrollbars: GetSize: %ld, %ld", (long int)max
.x
,
681 if(max
.x
> m_maxx
|| max
.y
> m_maxy
682 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
687 // add an extra bit to the sizes to avoid future updates
688 max
.x
= max
.x
+WXLO_ROFFSET
;
689 max
.y
= max
.y
+WXLO_BOFFSET
;
691 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
692 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
693 m_maxx
= max
.x
; m_maxy
= max
.y
;
698 wxLayoutWindow::Paste(void)
701 if (wxTheClipboard
->Open())
703 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
704 wxLayoutDataObject wxldo
;
705 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
707 wxTheClipboard
->GetData(&wxldo
);
710 //FIXME: missing functionality m_llist->Insert(wxldo.GetList());
715 wxTextDataObject data
;
716 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
718 wxTheClipboard
->GetData(&data
);
719 wxString text
= data
.GetText();
720 wxLayoutImportText( m_llist
, text
);
723 wxTheClipboard
->Close();
727 /* My attempt to get the primary selection, but it does not
729 if(text
.Length() == 0)
731 wxTextCtrl
tmp_tctrl(this,-1);
733 text
+= tmp_tctrl
.GetValue();
739 wxLayoutWindow::Copy(bool invalidate
)
741 // Calling GetSelection() will automatically do an EndSelection()
742 // on the list, but we need to take a note of it, too:
746 m_llist
->EndSelection();
749 wxLayoutDataObject wldo
;
750 wxLayoutList
*llist
= m_llist
->GetSelection(&wldo
, invalidate
);
753 // Export selection as text:
755 wxLayoutExportObject
*export
;
756 wxLayoutExportStatus
status(llist
);
757 while((export
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
759 if(export
->type
== WXLO_EXPORT_TEXT
)
760 text
<< *(export
->content
.text
);
765 // The exporter always appends a newline, so we chop it off if it
768 size_t len
= text
.Length();
769 if(len
> 2 && text
[len
-2] == '\r') // Windows
770 text
= text
.Mid(0,len
-2);
771 else if(len
> 1 && text
[len
-1] == '\n')
772 text
= text
.Mid(0,len
-1);
776 if (wxTheClipboard
->Open())
778 wxTextDataObject
*data
= new wxTextDataObject( text
);
779 bool rc
= wxTheClipboard
->SetData( data
);
780 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
781 rc
|= wxTheClipboard
->AddData( &wldo
);
783 wxTheClipboard
->Close();
791 wxLayoutWindow::Cut(void)
793 if(Copy(false)) // do not invalidate selection after copy
795 m_llist
->DeleteSelection();
802 wxLayoutWindow::Find(const wxString
&needle
,
807 if(fromWhere
== NULL
)
808 found
= m_llist
->FindText(needle
, m_llist
->GetCursorPos());
810 found
= m_llist
->FindText(needle
, *fromWhere
);
818 m_llist
->MoveCursorTo(found
);
826 wxLayoutWindow::MakeFormatMenu()
828 wxMenu
*m
= new wxMenu(_("Layout Menu"));
830 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
831 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
832 m
->AppendSeparator();
833 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
834 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
835 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
836 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
837 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
838 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
839 m
->AppendSeparator();
840 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
841 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
842 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
846 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
848 switch (event
.GetId())
850 case WXLOWIN_MENU_LARGER
:
851 m_llist
->SetFontLarger(); break;
852 case WXLOWIN_MENU_SMALLER
:
853 m_llist
->SetFontSmaller(); break;
854 case WXLOWIN_MENU_UNDERLINE_ON
:
855 m_llist
->SetFontUnderline(true); break;
856 case WXLOWIN_MENU_UNDERLINE_OFF
:
857 m_llist
->SetFontUnderline(false); break;
858 case WXLOWIN_MENU_BOLD_ON
:
859 m_llist
->SetFontWeight(wxBOLD
); break;
860 case WXLOWIN_MENU_BOLD_OFF
:
861 m_llist
->SetFontWeight(wxNORMAL
); break;
862 case WXLOWIN_MENU_ITALICS_ON
:
863 m_llist
->SetFontStyle(wxITALIC
); break;
864 case WXLOWIN_MENU_ITALICS_OFF
:
865 m_llist
->SetFontStyle(wxNORMAL
); break;
866 case WXLOWIN_MENU_ROMAN
:
867 m_llist
->SetFontFamily(wxROMAN
); break;
868 case WXLOWIN_MENU_TYPEWRITER
:
869 m_llist
->SetFontFamily(wxFIXED
); break;
870 case WXLOWIN_MENU_SANSSERIF
:
871 m_llist
->SetFontFamily(wxSWISS
); break;
876 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
879 //FIXME: need argument DoPaint(); // to repaint the cursor
883 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
886 //FIXME: need argument DoPaint(); // to repaint the cursor