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"
23 # include "gui/wxMenuDefs.h"
24 # include "gui/wxMApp.h"
26 # include "gui/wxlwindow.h"
27 # include "gui/wxlparser.h"
36 # include "wxlwindow.h"
37 # include "wxlparser.h"
40 #include <wx/clipbrd.h>
41 #include <wx/textctrl.h>
42 #include <wx/dataobj.h>
47 # define WXLO_DEBUG(x) wxLogDebug x
49 # define WXLO_DEBUG(x)
52 /// offsets to put a nice frame around text
53 #define WXLO_XOFFSET 4
54 #define WXLO_YOFFSET 4
56 /// offset to the right and bottom for when to redraw scrollbars
57 #define WXLO_ROFFSET 20
58 #define WXLO_BOFFSET 20
60 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
61 EVT_PAINT (wxLayoutWindow::OnPaint
)
62 EVT_CHAR (wxLayoutWindow::OnChar
)
63 EVT_KEY_UP (wxLayoutWindow::OnKeyUp
)
64 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
65 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
66 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
67 EVT_MOTION (wxLayoutWindow::OnMouseMove
)
68 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
69 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
70 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
73 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
74 : wxScrolledWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
75 wxHSCROLL
| wxVSCROLL
| wxBORDER
)
78 SetStatusBar(NULL
); // don't use statusbar
80 m_doSendEvents
= false;
81 m_ViewStartX
= 0; m_ViewStartY
= 0;
83 m_PopupMenu
= MakeFormatMenu();
84 m_memDC
= new wxMemoryDC
;
85 m_bitmap
= new wxBitmap(4,4);
86 m_bitmapSize
= wxPoint(4,4);
87 m_llist
= new wxLayoutList();
89 m_ScrollToCursor
= false;
91 wxPoint max
= m_llist
->GetSize();
92 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
93 EnableScrolling(true,true);
94 m_maxx
= max
.x
; m_maxy
= max
.y
;
96 SetCursorVisibility(-1);
97 SetCursor(wxCURSOR_IBEAM
);
101 wxLayoutWindow::~wxLayoutWindow()
103 delete m_memDC
; // deletes bitmap automatically (?)
107 SetBackgroundBitmap(NULL
);
111 wxLayoutWindow::Clear(int family
,
119 GetLayoutList()->Clear(family
,size
,style
,weight
,underline
,fg
,bg
);
120 SetBackgroundColour(GetLayoutList()->GetDefaults()->GetBGColour());
121 ResizeScrollbars(true);
126 r
.x
= r
.y
= 0; GetSize(&w
,&h
);
134 wxLayoutWindow::MSWGetDlgCode()
136 // if we don't return this, we won't get OnChar() events for TABs and ENTER
137 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
142 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
144 wxPaintDC
dc( this );
149 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
150 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
152 findPos
.x
-= WXLO_XOFFSET
;
153 findPos
.y
-= WXLO_YOFFSET
;
155 if(findPos
.x
< 0) findPos
.x
= 0;
156 if(findPos
.y
< 0) findPos
.y
= 0;
158 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
162 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
164 wxLayoutObject::UserData
*u
= obj
? obj
->GetUserData() : NULL
;
166 //has the mouse only been moved?
167 if(eventId
== WXLOWIN_MENU_MOUSEMOVE
)
169 // found is only true if we are really over an object, not just
171 if(found
&& u
&& ! m_Selecting
)
174 SetCursor(wxCURSOR_HAND
);
176 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
178 const wxString
&label
= u
->GetLabel();
180 m_StatusBar
->SetStatusText(label
,
187 SetCursor(wxCURSOR_IBEAM
);
188 m_HandCursor
= FALSE
;
189 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
190 m_StatusBar
->SetStatusText("", m_StatusFieldLabel
);
192 if(event
.LeftIsDown())
196 m_llist
->StartSelection();
202 m_llist
->ContinueSelection(cursorPos
);
206 if(m_Selecting
&& ! event
.LeftIsDown())
208 m_llist
->EndSelection(cursorPos
);
216 // always move cursor to mouse click:
217 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
219 m_llist
->MoveCursorTo(cursorPos
);
220 if(m_CursorVisibility
== -1)
221 m_CursorVisibility
= 1;
223 DoPaint(FALSE
); // DoPaint suppresses flicker under GTK
226 if(!m_doSendEvents
) // nothing to do
232 // only do the menu if activated, editable and not on a clickable object
233 if(eventId
== WXLOWIN_MENU_RCLICK
235 && (! obj
|| u
== NULL
))
237 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
243 // find the object at this position
246 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
247 commandEvent
.SetEventObject( this );
248 commandEvent
.SetClientData((char *)obj
);
249 GetEventHandler()->ProcessEvent(commandEvent
);
254 * Some simple keyboard handling.
257 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
259 int keyCode
= event
.KeyCode();
261 #ifdef WXLAYOUT_DEBUG
262 if(keyCode
== WXK_F1
)
269 if(! m_Selecting
&& event
.ShiftDown())
282 m_llist
->StartSelection();
289 // If needed, make cursor visible:
290 if(m_CursorVisibility
== -1)
291 m_CursorVisibility
= 1;
293 /* These two nested switches work like this:
294 The first one processes all non-editing keycodes, to move the
295 cursor, etc. It's default will process all keycodes causing
296 modifications to the buffer, but only if editing is allowed.
301 m_llist
->MoveCursorHorizontally(1);
304 m_llist
->MoveCursorHorizontally(-1);
307 m_llist
->MoveCursorVertically(-1);
310 m_llist
->MoveCursorVertically(1);
313 m_llist
->MoveCursorVertically(-20);
316 m_llist
->MoveCursorVertically(20);
319 m_llist
->MoveCursorToBeginOfLine();
322 m_llist
->MoveCursorToEndOfLine();
325 if(keyCode
== 'c' && event
.ControlDown())
329 /* First, handle control keys */
330 if(event
.ControlDown() && ! event
.AltDown())
342 m_llist
->DeleteLines(1);
344 case 'h': // like backspace
345 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
348 m_llist
->DeleteToBeginOfLine();
351 m_llist
->DeleteToEndOfLine();
362 #ifdef WXLAYOUT_DEBUG
364 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
372 else if( event
.AltDown() && ! event
.ControlDown() )
378 m_llist
->DeleteWord();
385 else if ( ! event
.AltDown() && ! event
.ControlDown())
390 if(event
.ShiftDown())
394 if(event
.ShiftDown())
399 case WXK_BACK
: // backspace
400 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
404 m_llist
->WrapLine(m_WrapMargin
);
405 m_llist
->LineBreak();
408 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
409 && (keyCode
< 256 && keyCode
>= 32)
414 if(m_WrapMargin
> 0 && isspace(keyCode
))
415 m_llist
->WrapLine(m_WrapMargin
);
416 m_llist
->Insert(tmp
);
427 if(event
.ShiftDown())
428 m_llist
->ContinueSelection();
431 m_llist
->EndSelection();
437 wxRect r
= *m_llist
->GetUpdateRect();
442 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
444 if(event
.KeyCode() == WXK_SHIFT
&& m_llist
->IsSelecting())
446 m_llist
->EndSelection();
454 wxLayoutWindow::ScrollToCursor(void)
456 wxClientDC
dc( this );
459 int x0
,y0
,x1
,y1
, dx
, dy
;
461 // Calculate where the top of the visible area is:
463 GetScrollPixelsPerUnit(&dx
, &dy
);
466 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0
, y0
));
468 // Get the size of the visible window:
469 GetClientSize(&x1
,&y1
);
472 // As we have the values anyway, use them to avoid unnecessary
473 // scrollbar updates.
474 if(x1
> m_maxx
) m_maxx
= x1
;
475 if(y1
> m_maxy
) m_maxy
= y1
;
476 /* Make sure that the scrollbars are at a position so that the
477 cursor is visible if we are editing. */
478 /** Scroll so that cursor is visible! */
479 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
480 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
481 if(cc
.x
< x0
|| cc
.y
< y0
482 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
485 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
486 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
487 Scroll(nx
/dx
,ny
/dy
); // new view start
489 m_ScrollToCursor
= false; // avoid recursion
494 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
496 wxRect region
= GetUpdateRegion().GetBox();
497 InternalPaint(& region
);
501 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
504 InternalPaint(updateRect
);
506 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
511 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
513 wxPaintDC
dc( this );
516 int x0
,y0
,x1
,y1
, dx
, dy
;
518 // Calculate where the top of the visible area is:
520 GetScrollPixelsPerUnit(&dx
, &dy
);
523 // Get the size of the visible window:
524 GetClientSize(&x1
,&y1
);
527 // As we have the values anyway, use them to avoid unnecessary
528 // scrollbar updates.
529 if(x1
> m_maxx
) m_maxx
= x1
;
530 if(y1
> m_maxy
) m_maxy
= y1
;
535 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
536 updateRect
->x
, updateRect
->y
,
537 updateRect
->x
+updateRect
->width
,
538 updateRect
->y
+updateRect
->height
));
545 /* Check whether the window has grown, if so, we need to reallocate
546 the bitmap to be larger. */
547 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
549 wxASSERT(m_bitmapSize
.x
> 0);
550 wxASSERT(m_bitmapSize
.y
> 0);
552 m_memDC
->SelectObject(wxNullBitmap
);
554 m_bitmapSize
= wxPoint(x1
,y1
);
555 m_bitmap
= new wxBitmap(x1
,y1
);
556 m_memDC
->SelectObject(*m_bitmap
);
559 m_memDC
->SetDeviceOrigin(0,0);
560 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(),wxSOLID
));
561 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),
563 m_memDC
->SetLogicalFunction(wxCOPY
);
565 /* Either fill the background with the background bitmap, or clear
571 w
= m_BGbitmap
->GetWidth(),
572 h
= m_BGbitmap
->GetHeight();
573 for(y
= 0; y
< y1
; y
+=h
)
574 for(x
= 0; x
< x1
; x
+=w
)
575 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
576 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
580 // clear the background: (must not be done if we use the update rectangle!)
581 m_memDC
->SetBackgroundMode(wxSOLID
);
582 m_memDC
->DrawRectangle(0,0,x1
, y1
);
586 /* This is the important bit: we tell the list to draw itself: */
590 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
591 updateRect
->x
, updateRect
->y
,
592 updateRect
->x
+updateRect
->width
,
593 updateRect
->y
+updateRect
->height
));
597 // Device origins on the memDC are suspect, we translate manually
598 // with the translate parameter of Draw().
599 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
600 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
602 // We start calculating a new update rect before drawing the
603 // cursor, so that the cursor coordinates get included in the next
604 // update rectangle (although they are drawn on the memDC, this is
605 // needed to erase it):
606 m_llist
->InvalidateUpdateRect();
607 if(m_CursorVisibility
== 1)
608 m_llist
->DrawCursor(*m_memDC
,
609 m_HaveFocus
&& IsEditable(), // draw a thick
610 // cursor for editable windows with focus
613 // Now copy everything to the screen:
615 // This somehow doesn't work, but even the following bit with the
616 // whole rect at once is still a bit broken I think.
617 wxRegionIterator
ri ( GetUpdateRegion() );
621 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
622 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
623 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
624 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
630 // FIXME: Trying to copy only the changed parts, but it does not seem
632 // x0 = updateRect->x; y0 = updateRect->y;
633 // if(updateRect->height < y1)
634 // y1 = updateRect->height;
635 // y1 += WXLO_YOFFSET; //FIXME might not be needed
636 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
640 m_ScrollToCursor
= false;
641 if(m_StatusBar
&& m_StatusFieldCursor
!= -1)
644 label
.Printf(_("L:%d C:%d"), m_llist
->GetCursorPos().x
+1, m_llist
->GetCursorPos().y
+1);
645 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
649 // change the range and position of scrollbars
651 wxLayoutWindow::ResizeScrollbars(bool exact
)
653 wxPoint max
= m_llist
->GetSize();
655 WXLO_DEBUG(("ResizeScrollbars: GetSize: %ld, %ld", (long int)max
.x
,
657 if(max
.x
> m_maxx
|| max
.y
> m_maxy
658 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
663 // add an extra bit to the sizes to avoid future updates
664 max
.x
= max
.x
+WXLO_ROFFSET
;
665 max
.y
= max
.y
+WXLO_BOFFSET
;
667 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
668 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
669 m_maxx
= max
.x
; m_maxy
= max
.y
;
674 wxLayoutWindow::Paste(void)
677 if (wxTheClipboard
->Open())
679 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
680 wxLayoutDataObject wxldo
;
681 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
683 wxTheClipboard
->GetData(&wxldo
);
686 //FIXME: missing functionality m_llist->Insert(wxldo.GetList());
691 wxTextDataObject data
;
692 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
694 wxTheClipboard
->GetData(&data
);
695 wxString text
= data
.GetText();
696 wxLayoutImportText( m_llist
, text
);
699 wxTheClipboard
->Close();
702 /* My attempt to get the primary selection, but it does not
704 if(text
.Length() == 0)
706 wxTextCtrl
tmp_tctrl(this,-1);
708 text
+= tmp_tctrl
.GetValue();
714 wxLayoutWindow::Copy(bool invalidate
)
716 // Calling GetSelection() will automatically do an EndSelection()
717 // on the list, but we need to take a note of it, too:
721 m_llist
->EndSelection();
724 wxLayoutDataObject wldo
;
725 wxLayoutList
*llist
= m_llist
->GetSelection(&wldo
, invalidate
);
728 // Export selection as text:
730 wxLayoutExportObject
*export
;
731 wxLayoutExportStatus
status(llist
);
732 while((export
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
734 if(export
->type
== WXLO_EXPORT_TEXT
)
735 text
<< *(export
->content
.text
);
740 // The exporter always appends a newline, so we chop it off if it
743 size_t len
= text
.Length();
744 if(len
> 2 && text
[len
-2] == '\r') // Windows
745 text
= text
.Mid(0,len
-2);
746 else if(len
> 1 && text
[len
-1] == '\n')
747 text
= text
.Mid(0,len
-1);
751 if (wxTheClipboard
->Open())
753 wxTextDataObject
*data
= new wxTextDataObject( text
);
754 bool rc
= wxTheClipboard
->SetData( data
);
755 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
756 rc
|= wxTheClipboard
->AddData( &wldo
);
758 wxTheClipboard
->Close();
765 wxLayoutWindow::Cut(void)
767 if(Copy(false)) // do not invalidate selection after copy
769 m_llist
->DeleteSelection();
776 wxLayoutWindow::Find(const wxString
&needle
,
781 if(fromWhere
== NULL
)
782 found
= m_llist
->FindText(needle
, m_llist
->GetCursorPos());
784 found
= m_llist
->FindText(needle
, *fromWhere
);
792 m_llist
->MoveCursorTo(found
);
800 wxLayoutWindow::MakeFormatMenu()
802 wxMenu
*m
= new wxMenu(_("Layout Menu"));
804 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
805 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
806 m
->AppendSeparator();
807 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
808 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
809 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
810 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
811 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
812 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
813 m
->AppendSeparator();
814 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
815 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
816 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
820 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
822 switch (event
.GetId())
824 case WXLOWIN_MENU_LARGER
:
825 m_llist
->SetFontLarger(); break;
826 case WXLOWIN_MENU_SMALLER
:
827 m_llist
->SetFontSmaller(); break;
828 case WXLOWIN_MENU_UNDERLINE_ON
:
829 m_llist
->SetFontUnderline(true); break;
830 case WXLOWIN_MENU_UNDERLINE_OFF
:
831 m_llist
->SetFontUnderline(false); break;
832 case WXLOWIN_MENU_BOLD_ON
:
833 m_llist
->SetFontWeight(wxBOLD
); break;
834 case WXLOWIN_MENU_BOLD_OFF
:
835 m_llist
->SetFontWeight(wxNORMAL
); break;
836 case WXLOWIN_MENU_ITALICS_ON
:
837 m_llist
->SetFontStyle(wxITALIC
); break;
838 case WXLOWIN_MENU_ITALICS_OFF
:
839 m_llist
->SetFontStyle(wxNORMAL
); break;
840 case WXLOWIN_MENU_ROMAN
:
841 m_llist
->SetFontFamily(wxROMAN
); break;
842 case WXLOWIN_MENU_TYPEWRITER
:
843 m_llist
->SetFontFamily(wxFIXED
); break;
844 case WXLOWIN_MENU_SANSSERIF
:
845 m_llist
->SetFontFamily(wxSWISS
); break;
850 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
853 //FIXME: need argument DoPaint(); // to repaint the cursor
857 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
860 //FIXME: need argument DoPaint(); // to repaint the cursor