1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
4 * (C) 1998-2000 by Karsten Ballüder (ballueder@gmx.net) *
7 *******************************************************************/
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
18 # pragma implementation "wxlwindow.h"
21 #include "wx/wxprec.h"
32 # include "gui/wxMenuDefs.h"
33 # include "gui/wxMApp.h"
35 # include "gui/wxlwindow.h"
36 # include "gui/wxlparser.h"
38 # include "MDialogs.h"
42 # include <wx/msw/private.h>
45 # include "wxlwindow.h"
46 # include "wxlparser.h"
49 #include <wx/clipbrd.h>
50 #include <wx/textctrl.h>
51 #include <wx/dataobj.h>
53 #ifdef WXLAYOUT_USE_CARET
54 # include <wx/caret.h>
55 #endif // WXLAYOUT_USE_CARET
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
65 # define WXLO_DEBUG(x) wxLogDebug x
67 # define WXLO_DEBUG(x)
70 // for profiling in debug mode:
71 WXLO_TIMER_DEFINE(UpdateTimer
);
72 WXLO_TIMER_DEFINE(BlitTimer
);
73 WXLO_TIMER_DEFINE(LayoutTimer
);
74 WXLO_TIMER_DEFINE(TmpTimer
);
75 WXLO_TIMER_DEFINE(DrawTimer
);
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 /// offsets to put a nice frame around text
82 #define WXLO_XOFFSET 4
83 #define WXLO_YOFFSET 4
85 /// offset to the right and bottom for when to redraw scrollbars
86 #define WXLO_ROFFSET 20
87 #define WXLO_BOFFSET 20
89 /// scroll margins when selecting with the mouse
90 #define WXLO_SCROLLMARGIN_X 10
91 #define WXLO_SCROLLMARGIN_Y 10
93 /// the size of one scrollbar page in pixels
94 static const int X_SCROLL_PAGE
= 10;
95 static const int Y_SCROLL_PAGE
= 20;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
104 EVT_SIZE (wxLayoutWindow::OnSize
)
106 EVT_PAINT (wxLayoutWindow::OnPaint
)
108 EVT_CHAR (wxLayoutWindow::OnChar
)
109 EVT_KEY_UP (wxLayoutWindow::OnKeyUp
)
111 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseDown
)
112 EVT_LEFT_UP(wxLayoutWindow::OnLeftMouseUp
)
113 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
114 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
115 EVT_MIDDLE_DOWN(wxLayoutWindow::OnMiddleMouseDown
)
116 EVT_MOTION (wxLayoutWindow::OnMouseMove
)
118 EVT_UPDATE_UI(WXLOWIN_MENU_UNDERLINE
, wxLayoutWindow::OnUpdateMenuUnderline
)
119 EVT_UPDATE_UI(WXLOWIN_MENU_BOLD
, wxLayoutWindow::OnUpdateMenuBold
)
120 EVT_UPDATE_UI(WXLOWIN_MENU_ITALICS
, wxLayoutWindow::OnUpdateMenuItalic
)
121 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
123 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
124 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
126 // EVT_IDLE(wxLayoutWindow::ResizeScrollbars)
129 // ----------------------------------------------------------------------------
130 // function prototypes
131 // ----------------------------------------------------------------------------
133 /// returns TRUE if keyCode is one of arrows/home/end/page{up|down} keys
134 static bool IsDirectionKey(long keyCode
);
136 // ============================================================================
138 // ============================================================================
140 #ifndef wxWANTS_CHARS
141 # define wxWANTS_CHARS 0
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
149 : wxScrolledWindow(parent
, -1,
150 wxDefaultPosition
, wxDefaultSize
,
151 wxHSCROLL
| wxVSCROLL
|
156 SetStatusBar(NULL
); // don't use statusbar
158 m_doSendEvents
= false;
159 m_ViewStartX
= 0; m_ViewStartY
= 0;
160 m_DoPopupMenu
= true;
161 m_PopupMenu
= MakeFormatMenu();
162 m_memDC
= new wxMemoryDC
;
163 m_bitmap
= new wxBitmap(4,4);
164 m_bitmapSize
= wxPoint(4,4);
165 m_llist
= new wxLayoutList();
167 m_ScrollToCursor
= false;
169 m_FocusFollowMode
= false;
174 // no scrollbars initially
176 m_hasVScrollbar
= false;
180 #ifdef WXLAYOUT_USE_CARET
181 // FIXME cursor size shouldn't be hardcoded
182 wxCaret
*caret
= new wxCaret(this, 2, 20);
184 m_llist
->SetCaret(caret
);
185 #endif // WXLAYOUT_USE_CARET
188 m_HandCursor
= FALSE
;
189 m_CursorVisibility
= -1;
190 SetCursor(wxCURSOR_IBEAM
);
193 // at least under Windows, this should be the default behaviour
194 m_AutoDeleteSelection
= TRUE
;
197 wxLayoutWindow::~wxLayoutWindow()
199 delete m_memDC
; // deletes bitmap automatically (?)
203 SetBackgroundBitmap(NULL
);
207 wxLayoutWindow::Clear(int family
,
215 GetLayoutList()->Clear(family
,size
,style
,weight
,underline
,fg
,bg
);
216 SetBackgroundColour(GetLayoutList()->GetDefaultStyleInfo().GetBGColour());
217 wxScrolledWindow::Clear();
218 ResizeScrollbars(true);
222 m_CursorVisibility
= 1;
224 #ifdef WXLAYOUT_USE_CARET
225 if ( m_CursorVisibility
== 1 )
227 #endif // WXLAYOUT_USE_CARET
229 RequestUpdate((wxRect
*)NULL
);
232 void wxLayoutWindow::Refresh(bool eraseBackground
, const wxRect
*rect
)
234 wxScrolledWindow::Refresh(eraseBackground
, rect
);
238 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
240 wxClientDC
dc( this );
242 if ( (eventId
!= WXLOWIN_MENU_MOUSEMOVE
246 ) && (wxWindow::FindFocus() != this)
251 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
252 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
254 findPos
.x
-= WXLO_XOFFSET
;
255 findPos
.y
-= WXLO_YOFFSET
;
262 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
264 // Scroll the window if the mouse is at the end of it:
265 if(m_Selecting
&& eventId
== WXLOWIN_MENU_MOUSEMOVE
)
267 //WXLO_DEBUG(("selecting at : %d/%d", (int) event.GetX(), (int)event.GetY()));
269 GetViewStart(&left
, &top
);
270 wxSize size
= GetClientSize();
273 if(event
.GetX() < WXLO_SCROLLMARGIN_X
)
274 xdelta
= -(WXLO_SCROLLMARGIN_X
-event
.GetX());
275 else if(event
.GetX() > size
.x
-WXLO_SCROLLMARGIN_X
)
276 xdelta
= event
.GetX()-size
.x
+WXLO_SCROLLMARGIN_X
;
279 if(event
.GetY() < WXLO_SCROLLMARGIN_Y
)
280 ydelta
= -(WXLO_SCROLLMARGIN_Y
-event
.GetY());
281 else if(event
.GetY() > size
.y
-WXLO_SCROLLMARGIN_Y
)
282 ydelta
= event
.GetY()-size
.y
+WXLO_SCROLLMARGIN_Y
;
286 //WXLO_DEBUG(("xdelta: %d", (int) xdelta));
287 if(xdelta
!= 0 || ydelta
!= 0)
289 top
+= ydelta
; if(top
< 0) top
= 0;
290 left
+= xdelta
; if(left
< 0) left
= 0;
297 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
299 wxLayoutObject::UserData
*u
= obj
? obj
->GetUserData() : NULL
;
301 // has the mouse only been moved?
304 case WXLOWIN_MENU_MOUSEMOVE
:
306 // this variables is used to only erase the message in the status
307 // bar if we had put it there previously - otherwise empting status
308 // bar might be undesirable
309 static bool s_hasPutMessageInStatusBar
= false;
311 // found is only true if we are really over an object, not just
313 if(found
&& u
&& ! m_Selecting
)
316 SetCursor(wxCURSOR_HAND
);
318 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
320 const wxString
&label
= u
->GetLabel();
323 m_StatusBar
->SetStatusText(label
,
325 s_hasPutMessageInStatusBar
= true;
332 SetCursor(wxCURSOR_IBEAM
);
333 m_HandCursor
= FALSE
;
334 if( m_StatusBar
&& m_StatusFieldLabel
!= -1 &&
335 s_hasPutMessageInStatusBar
)
337 m_StatusBar
->SetStatusText("", m_StatusFieldLabel
);
343 if ( event
.LeftIsDown() )
345 // m_Selecting might not be set if the button got pressed
346 // outside this window, so check for it:
349 m_llist
->ContinueSelection(cursorPos
, m_ClickPosition
);
350 RequestUpdate(); // TODO: we don't have to redraw everything!
361 case WXLOWIN_MENU_LDOWN
:
363 // always move cursor to mouse click:
364 m_llist
->MoveCursorTo(cursorPos
);
366 // clicking a mouse removes the selection
367 if ( m_llist
->HasSelection() )
369 m_llist
->DiscardSelection();
371 RequestUpdate(); // TODO: we don't have to redraw everything!
374 // Calculate where the top of the visible area is:
376 GetViewStart(&x0
,&y0
);
378 GetScrollPixelsPerUnit(&dx
, &dy
);
381 wxPoint
offset(-x0
+WXLO_XOFFSET
, -y0
+WXLO_YOFFSET
);
383 if(m_CursorVisibility
== -1)
384 m_CursorVisibility
= 1;
385 #ifdef WXLAYOUT_USE_CARET
386 if ( m_CursorVisibility
== 1 )
388 #endif // WXLAYOUT_USE_CARET
390 if(m_CursorVisibility
)
392 // draw a thick cursor for editable windows with focus
393 m_llist
->DrawCursor(dc
, m_HaveFocus
&& IsEditable(), offset
);
397 RequestUpdate(); // RequestUpdate suppresses flicker under GTK
401 m_llist
->StartSelection(wxPoint(-1, -1), m_ClickPosition
);
406 case WXLOWIN_MENU_LUP
:
409 // end selection at the cursor position corresponding to the
410 // current mouse position, but don´t move cursor there.
411 m_llist
->EndSelection(cursorPos
,m_ClickPosition
);
414 RequestUpdate(); // TODO: we don't have to redraw everything!
418 case WXLOWIN_MENU_MDOWN
:
422 case WXLOWIN_MENU_DBLCLICK
:
423 // select a word under cursor
424 m_llist
->MoveCursorTo(cursorPos
);
425 m_llist
->MoveCursorWord(-1);
426 m_llist
->StartSelection();
427 m_llist
->MoveCursorWord(1, false);
428 m_llist
->EndSelection();
430 RequestUpdate(); // TODO: we don't have to redraw everything!
434 // notify about mouse events?
437 // only do the menu if activated, editable and not on a clickable object
438 if(eventId
== WXLOWIN_MENU_RCLICK
440 && (! obj
|| u
== NULL
))
442 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
447 // find the object at this position
450 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
451 commandEvent
.SetEventObject( this );
452 commandEvent
.SetClientData((char *)obj
);
453 GetEventHandler()->ProcessEvent(commandEvent
);
460 // ----------------------------------------------------------------------------
461 // keyboard handling.
462 // ----------------------------------------------------------------------------
465 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
467 int keyCode
= event
.KeyCode();
468 bool ctrlDown
= event
.ControlDown();
470 #ifdef WXLAYOUT_DEBUG
471 if(keyCode
== WXK_F1
)
478 // Force m_Selecting to be false if shift is no longer
479 // pressed. OnKeyUp() cannot catch all Shift-Up events.
480 if(m_Selecting
&& !event
.ShiftDown())
483 m_llist
->EndSelection();
484 m_llist
->DiscardSelection(); //FIXME: correct?
487 // If we deleted the selection here, we must not execute the
488 // deletion in Delete/Backspace handling.
489 bool deletedSelection
= false;
490 // pressing any non-arrow key optionally replaces the selection:
491 if(m_AutoDeleteSelection
494 && m_llist
->HasSelection()
495 && ! IsDirectionKey(keyCode
)
496 && ! (event
.AltDown() || ctrlDown
)
499 m_llist
->DeleteSelection();
500 deletedSelection
= true;
504 // <Shift>+<arrow> starts selection
505 if ( IsDirectionKey(keyCode
) )
507 // just continue the old selection
508 if ( m_Selecting
&& event
.ShiftDown() )
509 m_llist
->ContinueSelection();
512 m_llist
->DiscardSelection();
514 if( event
.ShiftDown() )
517 m_llist
->StartSelection();
522 // If needed, make cursor visible:
523 if(m_CursorVisibility
== -1)
524 m_CursorVisibility
= 1;
526 /* These two nested switches work like this:
527 The first one processes all non-editing keycodes, to move the
528 cursor, etc. It's default will process all keycodes causing
529 modifications to the buffer, but only if editing is allowed.
535 m_llist
->MoveCursorWord(1);
537 m_llist
->MoveCursorHorizontally(1);
541 m_llist
->MoveCursorWord(-1);
543 m_llist
->MoveCursorHorizontally(-1);
546 m_llist
->MoveCursorVertically(-1);
549 m_llist
->MoveCursorVertically(1);
552 m_llist
->MoveCursorVertically(-Y_SCROLL_PAGE
);
555 m_llist
->MoveCursorVertically(Y_SCROLL_PAGE
);
559 m_llist
->MoveCursorTo(wxPoint(0, 0));
561 m_llist
->MoveCursorToBeginOfLine();
565 m_llist
->MoveCursorToEnd();
567 m_llist
->MoveCursorToEndOfLine();
571 if(ctrlDown
&& ! IsEditable())
575 // this should work even in read-only mode
581 case 't': // search again
585 // we don't handle it, maybe an accelerator?
589 else if( IsEditable() )
591 /* First, handle control keys */
592 if(ctrlDown
&& ! event
.AltDown())
594 if(keyCode
>= 'A' && keyCode
<= 'Z')
595 keyCode
= tolower(keyCode
);
602 if(! deletedSelection
)
604 m_llist
->DeleteWord();
609 if(! deletedSelection
) // already done
616 m_llist
->DeleteLines(1);
619 case 'h': // like backspace
620 if(m_llist
->MoveCursorHorizontally(-1))
629 case 't': // search again
633 m_llist
->DeleteToBeginOfLine();
637 m_llist
->DeleteToEndOfLine();
651 m_llist
->WrapLine(m_WrapMargin
);
655 m_llist
->WrapAll(m_WrapMargin
);
657 #ifdef WXLAYOUT_DEBUG
659 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
666 // we don't handle it, maybe an accelerator?
671 else if( event
.AltDown() && ! event
.ControlDown() )
677 m_llist
->DeleteWord();
681 // we don't handle it, maybe an accelerator?
686 else if ( ! event
.AltDown() && ! event
.ControlDown())
691 if(event
.ShiftDown())
695 if(event
.ShiftDown())
698 if(! deletedSelection
)
704 case WXK_BACK
: // backspace
705 if(! deletedSelection
)
706 if(m_llist
->MoveCursorHorizontally(-1))
715 && m_llist
->GetCursorPos().x
> m_WrapMargin
)
716 m_llist
->WrapLine(m_WrapMargin
);
717 m_llist
->LineBreak();
722 if ( !event
.ShiftDown() )
724 // TODO should be configurable
725 static const int tabSize
= 8;
727 CoordType x
= m_llist
->GetCursorPos().x
;
728 size_t numSpaces
= tabSize
- x
% tabSize
;
729 m_llist
->Insert(wxString(' ', numSpaces
));
735 if((!(event
.ControlDown() || event
.AltDown()
737 && (keyCode
< 256 && keyCode
>= 32)
742 && m_llist
->GetCursorPos().x
> m_WrapMargin
744 m_llist
->WrapLine(m_WrapMargin
);
745 m_llist
->Insert((char)keyCode
);
749 // we don't handle it, maybe an accelerator?
756 // we don't handle it, maybe an accelerator?
762 // continue selection to the current (new) cursor position
763 m_llist
->ContinueSelection();
766 // refresh the screen
767 RequestUpdate(m_llist
->GetUpdateRect());
771 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
773 if ( event
.KeyCode() == WXK_SHIFT
&& m_Selecting
)
775 m_llist
->EndSelection();
784 wxLayoutWindow::ScrollToCursor(void)
786 //is always needed to make sure we know where the cursor is
788 //RequestUpdate(m_llist->GetUpdateRect());
793 int x0
,y0
,x1
,y1
, dx
, dy
;
795 // Calculate where the top of the visible area is:
796 GetViewStart(&x0
,&y0
);
797 GetScrollPixelsPerUnit(&dx
, &dy
);
800 WXLO_DEBUG(("ScrollToCursor: GetViewStart is %d/%d", x0
, y0
));
802 // Get the size of the visible window:
803 GetClientSize(&x1
, &y1
);
805 // Make sure that the scrollbars are at a position so that the cursor is
806 // visible if we are editing
807 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
808 wxPoint cc
= m_llist
->GetCursorScreenPos();
810 // the cursor should be completely visible in both directions
811 wxPoint
cs(m_llist
->GetCursorSize());
814 if ( cc
.x
< x0
|| cc
.x
>= x0
+ x1
- cs
.x
)
821 if ( cc
.y
< y0
|| cc
.y
>= y0
+ y1
- cs
.y
)
828 if( nx
!= -1 || ny
!= -1 )
830 // set new view start
831 Scroll(nx
== -1 ? -1 : (nx
+dx
-1)/dx
, ny
== -1 ? -1 : (ny
+dy
-1)/dy
);
833 m_ScrollToCursor
= false;
839 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
))
841 wxRect region
= GetUpdateRegion().GetBox();
842 InternalPaint(®ion
);
846 wxLayoutWindow::RequestUpdate(const wxRect
*updateRect
)
849 // Calling Refresh() causes bad flicker under wxGTK!!!
850 InternalPaint(updateRect
);
852 // shouldn't specify the update rectangle if it doesn't include all the
853 // changed locations - otherwise, they won't be repainted at all because
854 // the system clips the display to the update rect
855 Refresh(FALSE
); //, updateRect);
860 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
863 wxPaintDC
dc( this );
866 #ifdef WXLAYOUT_USE_CARET
867 // hide the caret before drawing anything
869 #endif // WXLAYOUT_USE_CARET
871 int x0
,y0
,x1
,y1
, dx
, dy
;
873 // Calculate where the top of the visible area is:
874 GetViewStart(&x0
,&y0
);
875 GetScrollPixelsPerUnit(&dx
, &dy
);
878 // Get the size of the visible window:
879 GetClientSize(&x1
,&y1
);
885 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
886 updateRect
->x
, updateRect
->y
,
887 updateRect
->x
+updateRect
->width
,
888 updateRect
->y
+updateRect
->height
));
891 ResizeScrollbars(true);
893 WXLO_TIMER_START(TmpTimer
);
894 /* Check whether the window has grown, if so, we need to reallocate
895 the bitmap to be larger. */
896 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
898 wxASSERT(m_bitmapSize
.x
> 0);
899 wxASSERT(m_bitmapSize
.y
> 0);
901 m_memDC
->SelectObject(wxNullBitmap
);
903 m_bitmapSize
= wxPoint(x1
,y1
);
904 m_bitmap
= new wxBitmap(x1
,y1
);
905 m_memDC
->SelectObject(*m_bitmap
);
908 m_memDC
->SetDeviceOrigin(0,0);
909 m_memDC
->SetBackground(wxBrush(m_llist
->GetDefaultStyleInfo().GetBGColour(),wxSOLID
));
910 m_memDC
->SetPen(wxPen(m_llist
->GetDefaultStyleInfo().GetBGColour(),
912 m_memDC
->SetLogicalFunction(wxCOPY
);
914 WXLO_TIMER_STOP(TmpTimer
);
916 // fill the background with the background bitmap
921 w
= m_BGbitmap
->GetWidth(),
922 h
= m_BGbitmap
->GetHeight();
923 for(y
= 0; y
< y1
; y
+=h
)
924 for(x
= 0; x
< x1
; x
+=w
)
925 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
926 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
929 // This is the important bit: we tell the list to draw itself
933 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
934 updateRect
->x
, updateRect
->y
,
935 updateRect
->x
+updateRect
->width
,
936 updateRect
->y
+updateRect
->height
));
940 // Device origins on the memDC are suspect, we translate manually
941 // with the translate parameter of Draw().
942 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
943 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
945 // We start calculating a new update rect before drawing the
946 // cursor, so that the cursor coordinates get included in the next
947 // update rectangle (although they are drawn on the memDC, this is
948 // needed to erase it):
949 m_llist
->InvalidateUpdateRect();
950 if(m_CursorVisibility
== 1)
952 // draw a thick cursor for editable windows with focus
953 m_llist
->DrawCursor(*m_memDC
,
954 m_HaveFocus
&& IsEditable(),
958 WXLO_TIMER_START(BlitTimer
);
959 // Now copy everything to the screen:
961 // This somehow doesn't work, but even the following bit with the
962 // whole rect at once is still a bit broken I think.
963 wxRegionIterator
ri ( GetUpdateRegion() );
967 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
968 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
969 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
970 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
976 // FIXME: Trying to copy only the changed parts, but it does not seem
978 // x0 = updateRect->x; y0 = updateRect->y;
979 // if(updateRect->height < y1)
980 // y1 = updateRect->height;
981 // y1 += WXLO_YOFFSET; //FIXME might not be needed
982 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
984 WXLO_TIMER_STOP(BlitTimer
);
987 #ifdef WXLAYOUT_USE_CARET
988 // show the caret back after everything is redrawn
990 #endif // WXLAYOUT_USE_CARET
994 if ( m_StatusBar
&& m_StatusFieldCursor
!= -1 )
996 static wxPoint
s_oldCursorPos(-1, -1);
998 wxPoint
pos(m_llist
->GetCursorPos());
1000 // avoid unnecessary status bar refreshes
1001 if ( pos
!= s_oldCursorPos
)
1003 s_oldCursorPos
= pos
;
1006 label
.Printf(_("Ln:%d Col:%d"), pos
.y
+ 1, pos
.x
+ 1);
1007 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
1011 WXLO_TIMER_PRINT(LayoutTimer
);
1012 WXLO_TIMER_PRINT(BlitTimer
);
1013 WXLO_TIMER_PRINT(TmpTimer
);
1017 wxLayoutWindow::OnSize(wxSizeEvent
&event
)
1026 Change the range and position of scrollbars. Has evolved into a
1027 generic Update function which will at some time later cause a repaint
1032 wxLayoutWindow::ResizeScrollbars(bool exact
)
1034 wxClientDC
dc( this );
1036 // m_llist->ForceTotalLayout();
1040 // we are laying out just the minimum, but always up to the
1041 // cursor line, so the cursor position is updated.
1042 m_llist
->Layout(dc
, 0);
1045 WXLO_TIMER_START(LayoutTimer
);
1046 m_llist
->Layout(dc
, -1);
1047 WXLO_TIMER_STOP(LayoutTimer
);
1050 wxPoint max
= m_llist
->GetSize();
1051 wxSize size
= GetClientSize();
1053 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
1054 (long int)max
.x
, (long int) max
.y
));
1056 // in the absence of scrollbars we should compare with the client size
1057 if ( !m_hasHScrollbar
)
1058 m_maxx
= size
.x
;// - WXLO_ROFFSET;
1059 if ( !m_hasVScrollbar
)
1060 m_maxy
= size
.y
;// - WXLO_BOFFSET;
1062 // check if the text hasn't become too big
1063 // TODO why do we set both at once? they're independent...
1064 if( max
.x
> m_maxx
- WXLO_ROFFSET
1065 || max
.y
> m_maxy
- WXLO_BOFFSET
1066 || (max
.x
< m_maxx
- X_SCROLL_PAGE
)
1067 || (max
.y
< m_maxy
- Y_SCROLL_PAGE
)
1070 // text became too large
1073 // add an extra bit to the sizes to avoid future updates
1074 max
.x
+= WXLO_ROFFSET
;
1075 max
.y
+= WXLO_BOFFSET
;
1079 if(max
.x
< X_SCROLL_PAGE
&& m_hasHScrollbar
)
1081 SetScrollbars(0,-1,0,-1,0,-1,true);
1082 m_hasHScrollbar
= FALSE
;
1085 if(max
.y
< Y_SCROLL_PAGE
&& m_hasVScrollbar
)
1087 SetScrollbars(-1,0,-1,0,-1,0,true);
1088 m_hasVScrollbar
= FALSE
;
1092 // (max.x > X_SCROLL_PAGE || max.y > Y_SCROLL_PAGE)
1093 (max
.x
> size
.x
- X_SCROLL_PAGE
|| max
.y
> size
.y
- Y_SCROLL_PAGE
)
1096 GetViewStart(&m_ViewStartX
, &m_ViewStartY
);
1097 SetScrollbars(X_SCROLL_PAGE
,
1099 max
.x
/ X_SCROLL_PAGE
+ 2,
1100 max
.y
/ Y_SCROLL_PAGE
+ 2,
1105 m_hasVScrollbar
= true;
1106 // ScrollToCursor();
1109 m_maxx
= max
.x
+ X_SCROLL_PAGE
;
1110 m_maxy
= max
.y
+ Y_SCROLL_PAGE
;
1114 // ----------------------------------------------------------------------------
1116 // clipboard operations
1118 // ----------------------------------------------------------------------------
1121 wxLayoutWindow::Paste(bool usePrivate
, bool primary
)
1123 // this only has an effect under X11:
1124 wxTheClipboard
->UsePrimarySelection(primary
);
1126 if (wxTheClipboard
->Open())
1130 wxLayoutDataObject wxldo
;
1131 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
1133 if(wxTheClipboard
->GetData(wxldo
))
1135 wxTheClipboard
->Close();
1136 wxString str
= wxldo
.GetLayoutData();
1144 wxTextDataObject data
;
1145 if (wxTheClipboard
->IsSupported( data
.GetFormat() )
1146 && wxTheClipboard
->GetData(data
) )
1148 wxTheClipboard
->Close();
1149 wxString text
= data
.GetText();
1150 wxLayoutImportText( m_llist
, text
);
1156 // if everything failed we can still try the primary:
1157 wxTheClipboard
->Close();
1158 if(! primary
) // not tried before
1160 wxTheClipboard
->UsePrimarySelection();
1161 if (wxTheClipboard
->Open())
1163 wxTextDataObject data
;
1164 if (wxTheClipboard
->IsSupported( data
.GetFormat() )
1165 && wxTheClipboard
->GetData(data
) )
1167 wxString text
= data
.GetText();
1168 wxLayoutImportText( m_llist
, text
);
1172 wxTheClipboard
->Close();
1178 wxLayoutWindow::Copy(bool invalidate
, bool privateFormat
, bool primary
)
1180 // Calling GetSelection() will automatically do an EndSelection()
1181 // on the list, but we need to take a note of it, too:
1184 m_Selecting
= false;
1185 m_llist
->EndSelection();
1188 wxLayoutDataObject
*wldo
= new wxLayoutDataObject
;
1189 wxLayoutList
*llist
= m_llist
->GetSelection(wldo
, invalidate
);
1192 // Export selection as text:
1194 wxLayoutExportObject
*exp
;
1195 wxLayoutExportStatus
status(llist
);
1196 while((exp
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
1198 if(exp
->type
== WXLO_EXPORT_TEXT
)
1199 text
<< *(exp
->content
.text
);
1204 // The exporter always appends a newline, so we chop it off if it
1207 size_t len
= text
.Length();
1208 if(len
> 2 && text
[len
-2] == '\r') // Windows
1209 text
= text
.Mid(0,len
-2);
1210 else if(len
> 1 && text
[len
-1] == '\n')
1211 text
= text
.Mid(0,len
-1);
1215 if(! primary
) // always copy as text-only to primary selection
1217 wxTheClipboard
->UsePrimarySelection();
1218 if (wxTheClipboard
->Open())
1220 wxTextDataObject
*data
= new wxTextDataObject( text
);
1221 wxTheClipboard
->SetData( data
);
1222 wxTheClipboard
->Close();
1227 wxTheClipboard
->UsePrimarySelection(primary
);
1228 if (wxTheClipboard
->Open())
1230 wxTextDataObject
*data
= new wxTextDataObject( text
);
1233 rc
= wxTheClipboard
->SetData( data
);
1235 rc
|= wxTheClipboard
->SetData( wldo
);
1236 wxTheClipboard
->Close();
1246 wxLayoutWindow::Cut(bool privateFormat
, bool usePrimary
)
1248 if(Copy(false, privateFormat
, usePrimary
)) // do not invalidate selection after copy
1250 m_llist
->DeleteSelection();
1258 // ----------------------------------------------------------------------------
1260 // ----------------------------------------------------------------------------
1263 wxLayoutWindow::Find(const wxString
&needle
,
1264 wxPoint
* fromWhere
,
1265 const wxString
&configPath
)
1270 if(needle
.Length() == 0)
1272 if( ! MInputBox(&m_FindString
,
1277 || strutil_isempty(m_FindString
))
1281 m_FindString
= needle
;
1283 if(fromWhere
== NULL
)
1284 found
= m_llist
->FindText(m_FindString
, m_llist
->GetCursorPos());
1286 found
= m_llist
->FindText(m_FindString
, *fromWhere
);
1294 m_llist
->MoveCursorTo(found
);
1305 wxLayoutWindow::FindAgain(void)
1307 bool rc
= Find(m_FindString
);
1311 // ----------------------------------------------------------------------------
1313 // ----------------------------------------------------------------------------
1316 wxLayoutWindow::MakeFormatMenu()
1318 wxMenu
*m
= new wxMenu(_("Layout Menu"));
1320 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
1321 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
1322 m
->AppendSeparator();
1323 m
->Append(WXLOWIN_MENU_UNDERLINE
, _("&Underline"),_("Underline mode."), true);
1324 m
->Append(WXLOWIN_MENU_BOLD
, _("&Bold"),_("Bold mode."), true);
1325 m
->Append(WXLOWIN_MENU_ITALICS
, _("&Italics"),_("Italics mode."), true);
1326 m
->AppendSeparator();
1327 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
1328 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
1329 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
1334 void wxLayoutWindow::OnUpdateMenuUnderline(wxUpdateUIEvent
& event
)
1336 event
.Check(m_llist
->IsFontUnderlined());
1339 void wxLayoutWindow::OnUpdateMenuBold(wxUpdateUIEvent
& event
)
1341 event
.Check(m_llist
->IsFontBold());
1344 void wxLayoutWindow::OnUpdateMenuItalic(wxUpdateUIEvent
& event
)
1346 event
.Check(m_llist
->IsFontItalic());
1349 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
1351 switch (event
.GetId())
1353 case WXLOWIN_MENU_LARGER
:
1354 m_llist
->SetFontLarger(); RequestUpdate(); break;
1355 case WXLOWIN_MENU_SMALLER
:
1356 m_llist
->SetFontSmaller(); RequestUpdate(); break;
1357 case WXLOWIN_MENU_UNDERLINE
:
1358 m_llist
->ToggleFontUnderline(); RequestUpdate(); break;
1359 case WXLOWIN_MENU_BOLD
:
1360 m_llist
->ToggleFontWeight(); RequestUpdate(); break;
1361 case WXLOWIN_MENU_ITALICS
:
1362 m_llist
->ToggleFontItalics(); RequestUpdate(); break;
1363 case WXLOWIN_MENU_ROMAN
:
1364 m_llist
->SetFontFamily(wxROMAN
); RequestUpdate(); break;
1365 case WXLOWIN_MENU_TYPEWRITER
:
1366 m_llist
->SetFontFamily(wxFIXED
); RequestUpdate(); break;
1367 case WXLOWIN_MENU_SANSSERIF
:
1368 m_llist
->SetFontFamily(wxSWISS
); RequestUpdate(); break;
1372 // ----------------------------------------------------------------------------
1374 // ----------------------------------------------------------------------------
1377 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
1381 RequestUpdate(); // cursor must change
1385 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
1387 m_HaveFocus
= false;
1389 RequestUpdate();// cursor must change
1392 // ----------------------------------------------------------------------------
1393 // private functions
1394 // ----------------------------------------------------------------------------
1396 static bool IsDirectionKey(long keyCode
)