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
| wxBORDER
)
81 SetStatusBar(NULL
); // don't use statusbar
83 m_doSendEvents
= false;
84 m_ViewStartX
= 0; m_ViewStartY
= 0;
86 m_PopupMenu
= MakeFormatMenu();
87 m_memDC
= new wxMemoryDC
;
88 m_bitmap
= new wxBitmap(4,4);
89 m_bitmapSize
= wxPoint(4,4);
90 m_llist
= new wxLayoutList();
93 m_ScrollToCursor
= false;
95 wxPoint max
= m_llist
->GetSize();
96 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
97 EnableScrolling(true,true);
98 m_maxx
= max
.x
; m_maxy
= max
.y
;
101 #ifdef WXLAYOUT_USE_CARET
102 // FIXME cursor size shouldn't be hardcoded
103 wxCaret
*caret
= new wxCaret(this, 2, 20);
105 m_llist
->SetCaret(caret
);
107 #endif // WXLAYOUT_USE_CARET
109 SetCursorVisibility(-1);
110 SetCursor(wxCURSOR_IBEAM
);
114 wxLayoutWindow::~wxLayoutWindow()
116 delete m_memDC
; // deletes bitmap automatically (?)
120 SetBackgroundBitmap(NULL
);
124 wxLayoutWindow::Clear(int family
,
132 GetLayoutList()->Clear(family
,size
,style
,weight
,underline
,fg
,bg
);
133 SetBackgroundColour(GetLayoutList()->GetDefaults()->GetBGColour());
134 ResizeScrollbars(true);
138 DoPaint((wxRect
*)NULL
);
143 wxLayoutWindow::MSWGetDlgCode()
145 // if we don't return this, we won't get OnChar() events for TABs and ENTER
146 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
151 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
153 wxClientDC
dc( this );
155 if ( eventId
!= WXLOWIN_MENU_MOUSEMOVE
)
157 // moving the mouse in a window shouldn't give it the focus!
162 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
163 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
165 findPos
.x
-= WXLO_XOFFSET
;
166 findPos
.y
-= WXLO_YOFFSET
;
168 if(findPos
.x
< 0) findPos
.x
= 0;
169 if(findPos
.y
< 0) findPos
.y
= 0;
171 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
175 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
177 wxLayoutObject::UserData
*u
= obj
? obj
->GetUserData() : NULL
;
179 //has the mouse only been moved?
180 if(eventId
== WXLOWIN_MENU_MOUSEMOVE
)
182 // found is only true if we are really over an object, not just
184 if(found
&& u
&& ! m_Selecting
)
187 SetCursor(wxCURSOR_HAND
);
189 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
191 const wxString
&label
= u
->GetLabel();
193 m_StatusBar
->SetStatusText(label
,
200 SetCursor(wxCURSOR_IBEAM
);
201 m_HandCursor
= FALSE
;
202 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
203 m_StatusBar
->SetStatusText("", m_StatusFieldLabel
);
205 if(event
.LeftIsDown())
209 m_llist
->StartSelection();
215 m_llist
->ContinueSelection(cursorPos
);
219 if(m_Selecting
&& ! event
.LeftIsDown())
221 m_llist
->EndSelection(cursorPos
);
229 // always move cursor to mouse click:
230 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
232 m_llist
->MoveCursorTo(cursorPos
);
233 if(m_CursorVisibility
== -1)
234 m_CursorVisibility
= 1;
236 DoPaint(FALSE
); // DoPaint suppresses flicker under GTK
239 if(!m_doSendEvents
) // nothing to do
245 // only do the menu if activated, editable and not on a clickable object
246 if(eventId
== WXLOWIN_MENU_RCLICK
248 && (! obj
|| u
== NULL
))
250 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
256 // find the object at this position
259 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
260 commandEvent
.SetEventObject( this );
261 commandEvent
.SetClientData((char *)obj
);
262 GetEventHandler()->ProcessEvent(commandEvent
);
267 * Some simple keyboard handling.
270 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
272 int keyCode
= event
.KeyCode();
274 #ifdef WXLAYOUT_DEBUG
275 if(keyCode
== WXK_F1
)
282 if(! m_Selecting
&& event
.ShiftDown())
295 m_llist
->StartSelection();
302 // If needed, make cursor visible:
303 if(m_CursorVisibility
== -1)
304 m_CursorVisibility
= 1;
306 /* These two nested switches work like this:
307 The first one processes all non-editing keycodes, to move the
308 cursor, etc. It's default will process all keycodes causing
309 modifications to the buffer, but only if editing is allowed.
314 m_llist
->MoveCursorHorizontally(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);
332 m_llist
->MoveCursorToBeginOfLine();
335 m_llist
->MoveCursorToEndOfLine();
338 if(keyCode
== 'c' && event
.ControlDown())
340 // this should work even in read-only mode
345 /* First, handle control keys */
346 if(event
.ControlDown() && ! event
.AltDown())
358 m_llist
->DeleteLines(1);
360 case 'h': // like backspace
361 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
364 m_llist
->DeleteToBeginOfLine();
367 m_llist
->DeleteToEndOfLine();
375 #ifdef WXLAYOUT_DEBUG
377 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
385 else if( event
.AltDown() && ! event
.ControlDown() )
391 m_llist
->DeleteWord();
398 else if ( ! event
.AltDown() && ! event
.ControlDown())
403 if(event
.ShiftDown())
407 if(event
.ShiftDown())
412 case WXK_BACK
: // backspace
413 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
417 m_llist
->WrapLine(m_WrapMargin
);
418 m_llist
->LineBreak();
421 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
422 && (keyCode
< 256 && keyCode
>= 32)
427 if(m_WrapMargin
> 0 && isspace(keyCode
))
428 m_llist
->WrapLine(m_WrapMargin
);
429 m_llist
->Insert(tmp
);
440 if(event
.ShiftDown())
441 m_llist
->ContinueSelection();
444 m_llist
->EndSelection();
450 wxRect r
= *m_llist
->GetUpdateRect();
455 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
457 if(event
.KeyCode() == WXK_SHIFT
&& m_llist
->IsSelecting())
459 m_llist
->EndSelection();
467 wxLayoutWindow::ScrollToCursor(void)
469 wxClientDC
dc( this );
472 int x0
,y0
,x1
,y1
, dx
, dy
;
474 // Calculate where the top of the visible area is:
476 GetScrollPixelsPerUnit(&dx
, &dy
);
479 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0
, y0
));
481 // Get the size of the visible window:
482 GetClientSize(&x1
,&y1
);
485 // As we have the values anyway, use them to avoid unnecessary
486 // scrollbar updates.
487 if(x1
> m_maxx
) m_maxx
= x1
;
488 if(y1
> m_maxy
) m_maxy
= y1
;
489 /* Make sure that the scrollbars are at a position so that the
490 cursor is visible if we are editing. */
491 /** Scroll so that cursor is visible! */
492 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
493 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
494 if(cc
.x
< x0
|| cc
.y
< y0
495 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
498 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
499 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
500 Scroll(nx
/dx
,ny
/dy
); // new view start
502 m_ScrollToCursor
= false; // avoid recursion
507 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
509 wxRect region
= GetUpdateRegion().GetBox();
510 InternalPaint(& region
);
514 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
517 InternalPaint(updateRect
);
519 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
521 if ( !::UpdateWindow(GetHwnd()) )
522 wxLogLastError("UpdateWindow");
527 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
529 wxPaintDC
dc( this );
532 #ifdef WXLAYOUT_USE_CARET
533 // hide the caret before drawing anything
535 #endif // WXLAYOUT_USE_CARET
537 int x0
,y0
,x1
,y1
, dx
, dy
;
539 // Calculate where the top of the visible area is:
541 GetScrollPixelsPerUnit(&dx
, &dy
);
544 // Get the size of the visible window:
545 GetClientSize(&x1
,&y1
);
548 // As we have the values anyway, use them to avoid unnecessary
549 // scrollbar updates.
550 if(x1
> m_maxx
) m_maxx
= x1
;
551 if(y1
> m_maxy
) m_maxy
= y1
;
556 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
557 updateRect
->x
, updateRect
->y
,
558 updateRect
->x
+updateRect
->width
,
559 updateRect
->y
+updateRect
->height
));
566 /* Check whether the window has grown, if so, we need to reallocate
567 the bitmap to be larger. */
568 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
570 wxASSERT(m_bitmapSize
.x
> 0);
571 wxASSERT(m_bitmapSize
.y
> 0);
573 m_memDC
->SelectObject(wxNullBitmap
);
575 m_bitmapSize
= wxPoint(x1
,y1
);
576 m_bitmap
= new wxBitmap(x1
,y1
);
577 m_memDC
->SelectObject(*m_bitmap
);
580 m_memDC
->SetDeviceOrigin(0,0);
581 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(),wxSOLID
));
582 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),
584 m_memDC
->SetLogicalFunction(wxCOPY
);
586 /* Either fill the background with the background bitmap, or clear
592 w
= m_BGbitmap
->GetWidth(),
593 h
= m_BGbitmap
->GetHeight();
594 for(y
= 0; y
< y1
; y
+=h
)
595 for(x
= 0; x
< x1
; x
+=w
)
596 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
597 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
601 // clear the background: (must not be done if we use the update rectangle!)
602 m_memDC
->SetBackgroundMode(wxSOLID
);
603 m_memDC
->DrawRectangle(0,0,x1
, y1
);
607 /* This is the important bit: we tell the list to draw itself: */
611 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
612 updateRect
->x
, updateRect
->y
,
613 updateRect
->x
+updateRect
->width
,
614 updateRect
->y
+updateRect
->height
));
618 // Device origins on the memDC are suspect, we translate manually
619 // with the translate parameter of Draw().
620 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
621 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
623 // We start calculating a new update rect before drawing the
624 // cursor, so that the cursor coordinates get included in the next
625 // update rectangle (although they are drawn on the memDC, this is
626 // needed to erase it):
627 m_llist
->InvalidateUpdateRect();
628 if(m_CursorVisibility
!= 0)
629 m_llist
->DrawCursor(*m_memDC
,
630 m_HaveFocus
&& IsEditable(), // draw a thick
631 // cursor for editable windows with focus
634 // Now copy everything to the screen:
636 // This somehow doesn't work, but even the following bit with the
637 // whole rect at once is still a bit broken I think.
638 wxRegionIterator
ri ( GetUpdateRegion() );
642 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
643 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
644 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
645 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
651 // FIXME: Trying to copy only the changed parts, but it does not seem
653 // x0 = updateRect->x; y0 = updateRect->y;
654 // if(updateRect->height < y1)
655 // y1 = updateRect->height;
656 // y1 += WXLO_YOFFSET; //FIXME might not be needed
657 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
660 #ifdef WXLAYOUT_USE_CARET
661 // show the caret back after everything is redrawn
663 #endif // WXLAYOUT_USE_CARET
666 m_ScrollToCursor
= false;
667 if(m_StatusBar
&& m_StatusFieldCursor
!= -1)
670 label
.Printf(_("Ln:%d Col:%d"),
671 m_llist
->GetCursorPos().y
+1,
672 m_llist
->GetCursorPos().x
+1);
673 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
677 // change the range and position of scrollbars
679 wxLayoutWindow::ResizeScrollbars(bool exact
)
681 wxPoint max
= m_llist
->GetSize();
683 WXLO_DEBUG(("ResizeScrollbars: GetSize: %ld, %ld", (long int)max
.x
,
685 if(max
.x
> m_maxx
|| max
.y
> m_maxy
686 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
691 // add an extra bit to the sizes to avoid future updates
692 max
.x
= max
.x
+WXLO_ROFFSET
;
693 max
.y
= max
.y
+WXLO_BOFFSET
;
695 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
696 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
697 m_maxx
= max
.x
; m_maxy
= max
.y
;
702 wxLayoutWindow::Paste(void)
705 if (wxTheClipboard
->Open())
707 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
708 wxLayoutDataObject wxldo
;
709 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
711 wxTheClipboard
->GetData(&wxldo
);
714 //FIXME: missing functionality m_llist->Insert(wxldo.GetList());
719 wxTextDataObject data
;
720 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
722 wxTheClipboard
->GetData(&data
);
723 wxString text
= data
.GetText();
724 wxLayoutImportText( m_llist
, text
);
727 wxTheClipboard
->Close();
731 /* My attempt to get the primary selection, but it does not
733 if(text
.Length() == 0)
735 wxTextCtrl
tmp_tctrl(this,-1);
737 text
+= tmp_tctrl
.GetValue();
743 wxLayoutWindow::Copy(bool invalidate
)
745 // Calling GetSelection() will automatically do an EndSelection()
746 // on the list, but we need to take a note of it, too:
750 m_llist
->EndSelection();
753 wxLayoutDataObject wldo
;
754 wxLayoutList
*llist
= m_llist
->GetSelection(&wldo
, invalidate
);
757 // Export selection as text:
759 wxLayoutExportObject
*export
;
760 wxLayoutExportStatus
status(llist
);
761 while((export
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
763 if(export
->type
== WXLO_EXPORT_TEXT
)
764 text
<< *(export
->content
.text
);
769 // The exporter always appends a newline, so we chop it off if it
772 size_t len
= text
.Length();
773 if(len
> 2 && text
[len
-2] == '\r') // Windows
774 text
= text
.Mid(0,len
-2);
775 else if(len
> 1 && text
[len
-1] == '\n')
776 text
= text
.Mid(0,len
-1);
780 if (wxTheClipboard
->Open())
782 wxTextDataObject
*data
= new wxTextDataObject( text
);
783 bool rc
= wxTheClipboard
->SetData( data
);
784 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
785 rc
|= wxTheClipboard
->AddData( &wldo
);
787 wxTheClipboard
->Close();
795 wxLayoutWindow::Cut(void)
797 if(Copy(false)) // do not invalidate selection after copy
799 m_llist
->DeleteSelection();
806 wxLayoutWindow::Find(const wxString
&needle
,
811 if(fromWhere
== NULL
)
812 found
= m_llist
->FindText(needle
, m_llist
->GetCursorPos());
814 found
= m_llist
->FindText(needle
, *fromWhere
);
822 m_llist
->MoveCursorTo(found
);
830 wxLayoutWindow::MakeFormatMenu()
832 wxMenu
*m
= new wxMenu(_("Layout Menu"));
834 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
835 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
836 m
->AppendSeparator();
837 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
838 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
839 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
840 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
841 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
842 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
843 m
->AppendSeparator();
844 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
845 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
846 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
850 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
852 switch (event
.GetId())
854 case WXLOWIN_MENU_LARGER
:
855 m_llist
->SetFontLarger(); break;
856 case WXLOWIN_MENU_SMALLER
:
857 m_llist
->SetFontSmaller(); break;
858 case WXLOWIN_MENU_UNDERLINE_ON
:
859 m_llist
->SetFontUnderline(true); break;
860 case WXLOWIN_MENU_UNDERLINE_OFF
:
861 m_llist
->SetFontUnderline(false); break;
862 case WXLOWIN_MENU_BOLD_ON
:
863 m_llist
->SetFontWeight(wxBOLD
); break;
864 case WXLOWIN_MENU_BOLD_OFF
:
865 m_llist
->SetFontWeight(wxNORMAL
); break;
866 case WXLOWIN_MENU_ITALICS_ON
:
867 m_llist
->SetFontStyle(wxITALIC
); break;
868 case WXLOWIN_MENU_ITALICS_OFF
:
869 m_llist
->SetFontStyle(wxNORMAL
); break;
870 case WXLOWIN_MENU_ROMAN
:
871 m_llist
->SetFontFamily(wxROMAN
); break;
872 case WXLOWIN_MENU_TYPEWRITER
:
873 m_llist
->SetFontFamily(wxFIXED
); break;
874 case WXLOWIN_MENU_SANSSERIF
:
875 m_llist
->SetFontFamily(wxSWISS
); break;
880 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
887 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)