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
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())
339 m_llist
->DeleteLines(1);
341 case 'h': // like backspace
342 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
345 m_llist
->DeleteToBeginOfLine();
348 m_llist
->DeleteToEndOfLine();
353 #ifdef WXLAYOUT_DEBUG
355 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
363 else if( event
.AltDown() && ! event
.ControlDown() )
369 m_llist
->DeleteWord();
376 else if ( ! event
.AltDown() && ! event
.ControlDown())
381 if(event
.ShiftDown())
387 case WXK_BACK
: // backspace
388 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
392 m_llist
->WrapLine(m_WrapMargin
);
393 m_llist
->LineBreak();
396 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
397 && (keyCode
< 256 && keyCode
>= 32)
402 if(m_WrapMargin
> 0 && isspace(keyCode
))
403 m_llist
->WrapLine(m_WrapMargin
);
404 m_llist
->Insert(tmp
);
415 if(event
.ShiftDown())
416 m_llist
->ContinueSelection();
419 m_llist
->EndSelection();
425 wxRect r
= *m_llist
->GetUpdateRect();
430 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
432 if(event
.KeyCode() == WXK_SHIFT
&& m_llist
->IsSelecting())
434 m_llist
->EndSelection();
442 wxLayoutWindow::ScrollToCursor(void)
444 wxClientDC
dc( this );
447 int x0
,y0
,x1
,y1
, dx
, dy
;
449 // Calculate where the top of the visible area is:
451 GetScrollPixelsPerUnit(&dx
, &dy
);
454 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0
, y0
));
456 // Get the size of the visible window:
457 GetClientSize(&x1
,&y1
);
460 // As we have the values anyway, use them to avoid unnecessary
461 // scrollbar updates.
462 if(x1
> m_maxx
) m_maxx
= x1
;
463 if(y1
> m_maxy
) m_maxy
= y1
;
464 /* Make sure that the scrollbars are at a position so that the
465 cursor is visible if we are editing. */
466 /** Scroll so that cursor is visible! */
467 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
468 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
469 if(cc
.x
< x0
|| cc
.y
< y0
470 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
473 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
474 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
475 Scroll(nx
/dx
,ny
/dy
); // new view start
477 m_ScrollToCursor
= false; // avoid recursion
482 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
484 wxRect region
= GetUpdateRegion().GetBox();
485 InternalPaint(& region
);
489 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
492 InternalPaint(updateRect
);
494 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
499 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
501 wxPaintDC
dc( this );
504 int x0
,y0
,x1
,y1
, dx
, dy
;
506 // Calculate where the top of the visible area is:
508 GetScrollPixelsPerUnit(&dx
, &dy
);
511 // Get the size of the visible window:
512 GetClientSize(&x1
,&y1
);
515 // As we have the values anyway, use them to avoid unnecessary
516 // scrollbar updates.
517 if(x1
> m_maxx
) m_maxx
= x1
;
518 if(y1
> m_maxy
) m_maxy
= y1
;
523 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
524 updateRect
->x
, updateRect
->y
,
525 updateRect
->x
+updateRect
->width
,
526 updateRect
->y
+updateRect
->height
));
533 /* Check whether the window has grown, if so, we need to reallocate
534 the bitmap to be larger. */
535 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
537 wxASSERT(m_bitmapSize
.x
> 0);
538 wxASSERT(m_bitmapSize
.y
> 0);
540 m_memDC
->SelectObject(wxNullBitmap
);
542 m_bitmapSize
= wxPoint(x1
,y1
);
543 m_bitmap
= new wxBitmap(x1
,y1
);
544 m_memDC
->SelectObject(*m_bitmap
);
547 m_memDC
->SetDeviceOrigin(0,0);
548 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(),wxSOLID
));
549 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),
551 m_memDC
->SetLogicalFunction(wxCOPY
);
553 /* Either fill the background with the background bitmap, or clear
559 w
= m_BGbitmap
->GetWidth(),
560 h
= m_BGbitmap
->GetHeight();
561 for(y
= 0; y
< y1
; y
+=h
)
562 for(x
= 0; x
< x1
; x
+=w
)
563 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
564 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
568 // clear the background: (must not be done if we use the update rectangle!)
569 m_memDC
->SetBackgroundMode(wxSOLID
);
570 m_memDC
->DrawRectangle(0,0,x1
, y1
);
574 /* This is the important bit: we tell the list to draw itself: */
578 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
579 updateRect
->x
, updateRect
->y
,
580 updateRect
->x
+updateRect
->width
,
581 updateRect
->y
+updateRect
->height
));
585 // Device origins on the memDC are suspect, we translate manually
586 // with the translate parameter of Draw().
587 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
588 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
590 // We start calculating a new update rect before drawing the
591 // cursor, so that the cursor coordinates get included in the next
592 // update rectangle (although they are drawn on the memDC, this is
593 // needed to erase it):
594 m_llist
->InvalidateUpdateRect();
595 if(m_CursorVisibility
== 1)
596 m_llist
->DrawCursor(*m_memDC
,
597 m_HaveFocus
&& IsEditable(), // draw a thick
598 // cursor for editable windows with focus
601 // Now copy everything to the screen:
603 // This somehow doesn't work, but even the following bit with the
604 // whole rect at once is still a bit broken I think.
605 wxRegionIterator
ri ( GetUpdateRegion() );
609 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
610 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
611 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
612 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
618 // FIXME: Trying to copy only the changed parts, but it does not seem
620 // x0 = updateRect->x; y0 = updateRect->y;
621 // if(updateRect->height < y1)
622 // y1 = updateRect->height;
623 // y1 += WXLO_YOFFSET; //FIXME might not be needed
624 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
628 m_ScrollToCursor
= false;
629 if(m_StatusBar
&& m_StatusFieldCursor
!= -1)
632 label
.Printf(_("L:%d C:%d"), m_llist
->GetCursorPos().x
+1, m_llist
->GetCursorPos().y
+1);
633 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
637 // change the range and position of scrollbars
639 wxLayoutWindow::ResizeScrollbars(bool exact
)
641 wxPoint max
= m_llist
->GetSize();
643 WXLO_DEBUG(("ResizeScrollbars: GetSize: %ld, %ld", (long int)max
.x
,
645 if(max
.x
> m_maxx
|| max
.y
> m_maxy
646 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
651 // add an extra bit to the sizes to avoid future updates
652 max
.x
= max
.x
+WXLO_ROFFSET
;
653 max
.y
= max
.y
+WXLO_BOFFSET
;
655 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
656 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
657 m_maxx
= max
.x
; m_maxy
= max
.y
;
662 wxLayoutWindow::Paste(void)
665 if (wxTheClipboard
->Open())
667 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
668 wxLayoutDataObject wxldo
;
669 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
671 wxTheClipboard
->GetData(&wxldo
);
674 //FIXME: missing functionality m_llist->Insert(wxldo.GetList());
679 wxTextDataObject data
;
680 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
682 wxTheClipboard
->GetData(&data
);
683 wxString text
= data
.GetText();
684 wxLayoutImportText( m_llist
, text
);
687 wxTheClipboard
->Close();
690 /* My attempt to get the primary selection, but it does not
692 if(text
.Length() == 0)
694 wxTextCtrl
tmp_tctrl(this,-1);
696 text
+= tmp_tctrl
.GetValue();
702 wxLayoutWindow::Copy(bool invalidate
)
704 // Calling GetSelection() will automatically do an EndSelection()
705 // on the list, but we need to take a note of it, too:
709 m_llist
->EndSelection();
712 wxLayoutDataObject wldo
;
713 wxLayoutList
*llist
= m_llist
->GetSelection(&wldo
, invalidate
);
716 // Export selection as text:
718 wxLayoutExportObject
*export
;
719 wxLayoutExportStatus
status(llist
);
720 while((export
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
722 if(export
->type
== WXLO_EXPORT_TEXT
)
723 text
<< *(export
->content
.text
);
728 // The exporter always appends a newline, so we chop it off if it
731 size_t len
= text
.Length();
732 if(len
> 2 && text
[len
-2] == '\r') // Windows
733 text
= text
.Mid(0,len
-2);
734 else if(len
> 1 && text
[len
-1] == '\n')
735 text
= text
.Mid(0,len
-1);
739 if (wxTheClipboard
->Open())
741 wxTextDataObject
*data
= new wxTextDataObject( text
);
742 bool rc
= wxTheClipboard
->SetData( data
);
743 #if wxUSE_PRIVATE_CLIPBOARD_FORMAT
744 rc
|= wxTheClipboard
->AddData( &wldo
);
746 wxTheClipboard
->Close();
753 wxLayoutWindow::Cut(void)
755 if(Copy(false)) // do not invalidate selection after copy
757 m_llist
->DeleteSelection();
764 wxLayoutWindow::Find(const wxString
&needle
,
769 if(fromWhere
== NULL
)
770 found
= m_llist
->FindText(needle
, m_llist
->GetCursorPos());
772 found
= m_llist
->FindText(needle
, *fromWhere
);
780 m_llist
->MoveCursorTo(found
);
788 wxLayoutWindow::MakeFormatMenu()
790 wxMenu
*m
= new wxMenu(_("Layout Menu"));
792 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
793 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
794 m
->AppendSeparator();
795 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
796 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
797 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
798 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
799 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
800 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
801 m
->AppendSeparator();
802 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
803 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
804 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
808 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
810 switch (event
.GetId())
812 case WXLOWIN_MENU_LARGER
:
813 m_llist
->SetFontLarger(); break;
814 case WXLOWIN_MENU_SMALLER
:
815 m_llist
->SetFontSmaller(); break;
816 case WXLOWIN_MENU_UNDERLINE_ON
:
817 m_llist
->SetFontUnderline(true); break;
818 case WXLOWIN_MENU_UNDERLINE_OFF
:
819 m_llist
->SetFontUnderline(false); break;
820 case WXLOWIN_MENU_BOLD_ON
:
821 m_llist
->SetFontWeight(wxBOLD
); break;
822 case WXLOWIN_MENU_BOLD_OFF
:
823 m_llist
->SetFontWeight(wxNORMAL
); break;
824 case WXLOWIN_MENU_ITALICS_ON
:
825 m_llist
->SetFontStyle(wxITALIC
); break;
826 case WXLOWIN_MENU_ITALICS_OFF
:
827 m_llist
->SetFontStyle(wxNORMAL
); break;
828 case WXLOWIN_MENU_ROMAN
:
829 m_llist
->SetFontFamily(wxROMAN
); break;
830 case WXLOWIN_MENU_TYPEWRITER
:
831 m_llist
->SetFontFamily(wxFIXED
); break;
832 case WXLOWIN_MENU_SANSSERIF
:
833 m_llist
->SetFontFamily(wxSWISS
); break;
838 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
841 //FIXME: need argument DoPaint(); // to repaint the cursor
845 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
848 //FIXME: need argument DoPaint(); // to repaint the cursor