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
745 m_llist
->WrapLine(m_WrapMargin
);
748 m_llist
->Insert((wxChar
)keyCode
);
752 // we don't handle it, maybe an accelerator?
759 // we don't handle it, maybe an accelerator?
765 // continue selection to the current (new) cursor position
766 m_llist
->ContinueSelection();
769 // refresh the screen
770 RequestUpdate(m_llist
->GetUpdateRect());
774 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
776 if ( event
.KeyCode() == WXK_SHIFT
&& m_Selecting
)
778 m_llist
->EndSelection();
787 wxLayoutWindow::ScrollToCursor(void)
789 //is always needed to make sure we know where the cursor is
791 //RequestUpdate(m_llist->GetUpdateRect());
796 int x0
,y0
,x1
,y1
, dx
, dy
;
798 // Calculate where the top of the visible area is:
799 GetViewStart(&x0
,&y0
);
800 GetScrollPixelsPerUnit(&dx
, &dy
);
803 WXLO_DEBUG(("ScrollToCursor: GetViewStart is %d/%d", x0
, y0
));
805 // Get the size of the visible window:
806 GetClientSize(&x1
, &y1
);
808 // Make sure that the scrollbars are at a position so that the cursor is
809 // visible if we are editing
810 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
811 wxPoint cc
= m_llist
->GetCursorScreenPos();
813 // the cursor should be completely visible in both directions
814 wxPoint
cs(m_llist
->GetCursorSize());
817 if ( cc
.x
< x0
|| cc
.x
>= x0
+ x1
- cs
.x
)
824 if ( cc
.y
< y0
|| cc
.y
>= y0
+ y1
- cs
.y
)
831 if( nx
!= -1 || ny
!= -1 )
833 // set new view start
834 Scroll(nx
== -1 ? -1 : (nx
+dx
-1)/dx
, ny
== -1 ? -1 : (ny
+dy
-1)/dy
);
836 m_ScrollToCursor
= false;
842 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
))
844 wxRect region
= GetUpdateRegion().GetBox();
845 InternalPaint(®ion
);
849 wxLayoutWindow::RequestUpdate(const wxRect
*updateRect
)
852 // Calling Refresh() causes bad flicker under wxGTK!!!
853 InternalPaint(updateRect
);
855 // shouldn't specify the update rectangle if it doesn't include all the
856 // changed locations - otherwise, they won't be repainted at all because
857 // the system clips the display to the update rect
858 Refresh(FALSE
); //, updateRect);
863 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
866 wxPaintDC
dc( this );
869 #ifdef WXLAYOUT_USE_CARET
870 // hide the caret before drawing anything
872 #endif // WXLAYOUT_USE_CARET
874 int x0
,y0
,x1
,y1
, dx
, dy
;
876 // Calculate where the top of the visible area is:
877 GetViewStart(&x0
,&y0
);
878 GetScrollPixelsPerUnit(&dx
, &dy
);
881 // Get the size of the visible window:
882 GetClientSize(&x1
,&y1
);
888 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
889 updateRect
->x
, updateRect
->y
,
890 updateRect
->x
+updateRect
->width
,
891 updateRect
->y
+updateRect
->height
));
894 ResizeScrollbars(true);
896 WXLO_TIMER_START(TmpTimer
);
897 /* Check whether the window has grown, if so, we need to reallocate
898 the bitmap to be larger. */
899 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
901 wxASSERT(m_bitmapSize
.x
> 0);
902 wxASSERT(m_bitmapSize
.y
> 0);
904 m_memDC
->SelectObject(wxNullBitmap
);
906 m_bitmapSize
= wxPoint(x1
,y1
);
907 m_bitmap
= new wxBitmap(x1
,y1
);
908 m_memDC
->SelectObject(*m_bitmap
);
911 m_memDC
->SetDeviceOrigin(0,0);
912 m_memDC
->SetBackground(wxBrush(m_llist
->GetDefaultStyleInfo().GetBGColour(),wxSOLID
));
913 m_memDC
->SetPen(wxPen(m_llist
->GetDefaultStyleInfo().GetBGColour(),
915 m_memDC
->SetLogicalFunction(wxCOPY
);
917 WXLO_TIMER_STOP(TmpTimer
);
919 // fill the background with the background bitmap
924 w
= m_BGbitmap
->GetWidth(),
925 h
= m_BGbitmap
->GetHeight();
926 for(y
= 0; y
< y1
; y
+=h
)
927 for(x
= 0; x
< x1
; x
+=w
)
928 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
929 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
932 // This is the important bit: we tell the list to draw itself
936 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
937 updateRect
->x
, updateRect
->y
,
938 updateRect
->x
+updateRect
->width
,
939 updateRect
->y
+updateRect
->height
));
943 // Device origins on the memDC are suspect, we translate manually
944 // with the translate parameter of Draw().
945 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
946 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
948 // We start calculating a new update rect before drawing the
949 // cursor, so that the cursor coordinates get included in the next
950 // update rectangle (although they are drawn on the memDC, this is
951 // needed to erase it):
952 m_llist
->InvalidateUpdateRect();
953 if(m_CursorVisibility
== 1)
955 // draw a thick cursor for editable windows with focus
956 m_llist
->DrawCursor(*m_memDC
,
957 m_HaveFocus
&& IsEditable(),
961 WXLO_TIMER_START(BlitTimer
);
962 // Now copy everything to the screen:
964 // This somehow doesn't work, but even the following bit with the
965 // whole rect at once is still a bit broken I think.
966 wxRegionIterator
ri ( GetUpdateRegion() );
970 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
971 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
972 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
973 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
979 // FIXME: Trying to copy only the changed parts, but it does not seem
981 // x0 = updateRect->x; y0 = updateRect->y;
982 // if(updateRect->height < y1)
983 // y1 = updateRect->height;
984 // y1 += WXLO_YOFFSET; //FIXME might not be needed
985 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
987 WXLO_TIMER_STOP(BlitTimer
);
990 #ifdef WXLAYOUT_USE_CARET
991 // show the caret back after everything is redrawn
993 #endif // WXLAYOUT_USE_CARET
997 if ( m_StatusBar
&& m_StatusFieldCursor
!= -1 )
999 static wxPoint
s_oldCursorPos(-1, -1);
1001 wxPoint
pos(m_llist
->GetCursorPos());
1003 // avoid unnecessary status bar refreshes
1004 if ( pos
!= s_oldCursorPos
)
1006 s_oldCursorPos
= pos
;
1009 label
.Printf(_("Ln:%d Col:%d"), pos
.y
+ 1, pos
.x
+ 1);
1010 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
1014 WXLO_TIMER_PRINT(LayoutTimer
);
1015 WXLO_TIMER_PRINT(BlitTimer
);
1016 WXLO_TIMER_PRINT(TmpTimer
);
1020 wxLayoutWindow::OnSize(wxSizeEvent
&event
)
1029 Change the range and position of scrollbars. Has evolved into a
1030 generic Update function which will at some time later cause a repaint
1035 wxLayoutWindow::ResizeScrollbars(bool exact
)
1037 wxClientDC
dc( this );
1039 // m_llist->ForceTotalLayout();
1043 // we are laying out just the minimum, but always up to the
1044 // cursor line, so the cursor position is updated.
1045 m_llist
->Layout(dc
, 0);
1048 WXLO_TIMER_START(LayoutTimer
);
1049 m_llist
->Layout(dc
, -1);
1050 WXLO_TIMER_STOP(LayoutTimer
);
1053 wxPoint max
= m_llist
->GetSize();
1054 wxSize size
= GetClientSize();
1056 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
1057 (long int)max
.x
, (long int) max
.y
));
1059 // in the absence of scrollbars we should compare with the client size
1060 if ( !m_hasHScrollbar
)
1061 m_maxx
= size
.x
;// - WXLO_ROFFSET;
1062 if ( !m_hasVScrollbar
)
1063 m_maxy
= size
.y
;// - WXLO_BOFFSET;
1065 // check if the text hasn't become too big
1066 // TODO why do we set both at once? they're independent...
1067 if( max
.x
> m_maxx
- WXLO_ROFFSET
1068 || max
.y
> m_maxy
- WXLO_BOFFSET
1069 || (max
.x
< m_maxx
- X_SCROLL_PAGE
)
1070 || (max
.y
< m_maxy
- Y_SCROLL_PAGE
)
1073 // text became too large
1076 // add an extra bit to the sizes to avoid future updates
1077 max
.x
+= WXLO_ROFFSET
;
1078 max
.y
+= WXLO_BOFFSET
;
1082 if(max
.x
< X_SCROLL_PAGE
&& m_hasHScrollbar
)
1084 SetScrollbars(0,-1,0,-1,0,-1,true);
1085 m_hasHScrollbar
= FALSE
;
1088 if(max
.y
< Y_SCROLL_PAGE
&& m_hasVScrollbar
)
1090 SetScrollbars(-1,0,-1,0,-1,0,true);
1091 m_hasVScrollbar
= FALSE
;
1095 // (max.x > X_SCROLL_PAGE || max.y > Y_SCROLL_PAGE)
1096 (max
.x
> size
.x
- X_SCROLL_PAGE
|| max
.y
> size
.y
- Y_SCROLL_PAGE
)
1099 GetViewStart(&m_ViewStartX
, &m_ViewStartY
);
1100 SetScrollbars(X_SCROLL_PAGE
,
1102 max
.x
/ X_SCROLL_PAGE
+ 2,
1103 max
.y
/ Y_SCROLL_PAGE
+ 2,
1108 m_hasVScrollbar
= true;
1109 // ScrollToCursor();
1112 m_maxx
= max
.x
+ X_SCROLL_PAGE
;
1113 m_maxy
= max
.y
+ Y_SCROLL_PAGE
;
1117 // ----------------------------------------------------------------------------
1119 // clipboard operations
1121 // ----------------------------------------------------------------------------
1124 wxLayoutWindow::Paste(bool usePrivate
, bool primary
)
1126 // this only has an effect under X11:
1127 wxTheClipboard
->UsePrimarySelection(primary
);
1129 if (wxTheClipboard
->Open())
1133 wxLayoutDataObject wxldo
;
1134 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
1136 if(wxTheClipboard
->GetData(wxldo
))
1138 wxTheClipboard
->Close();
1139 wxString str
= wxldo
.GetLayoutData();
1147 wxTextDataObject data
;
1148 if (wxTheClipboard
->IsSupported( data
.GetFormat() )
1149 && wxTheClipboard
->GetData(data
) )
1151 wxTheClipboard
->Close();
1152 wxString text
= data
.GetText();
1153 wxLayoutImportText( m_llist
, text
);
1159 // if everything failed we can still try the primary:
1160 wxTheClipboard
->Close();
1161 if(! primary
) // not tried before
1163 wxTheClipboard
->UsePrimarySelection();
1164 if (wxTheClipboard
->Open())
1166 wxTextDataObject data
;
1167 if (wxTheClipboard
->IsSupported( data
.GetFormat() )
1168 && wxTheClipboard
->GetData(data
) )
1170 wxString text
= data
.GetText();
1171 wxLayoutImportText( m_llist
, text
);
1175 wxTheClipboard
->Close();
1181 wxLayoutWindow::Copy(bool invalidate
, bool privateFormat
, bool primary
)
1183 // Calling GetSelection() will automatically do an EndSelection()
1184 // on the list, but we need to take a note of it, too:
1187 m_Selecting
= false;
1188 m_llist
->EndSelection();
1191 wxLayoutDataObject
*wldo
= new wxLayoutDataObject
;
1192 wxLayoutList
*llist
= m_llist
->GetSelection(wldo
, invalidate
);
1195 // Export selection as text:
1197 wxLayoutExportObject
*exp
;
1198 wxLayoutExportStatus
status(llist
);
1199 while((exp
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
1201 if(exp
->type
== WXLO_EXPORT_TEXT
)
1202 text
<< *(exp
->content
.text
);
1207 // The exporter always appends a newline, so we chop it off if it
1210 size_t len
= text
.Length();
1211 if(len
> 2 && text
[len
-2] == '\r') // Windows
1212 text
= text
.Mid(0,len
-2);
1213 else if(len
> 1 && text
[len
-1] == '\n')
1214 text
= text
.Mid(0,len
-1);
1218 if(! primary
) // always copy as text-only to primary selection
1220 wxTheClipboard
->UsePrimarySelection();
1221 if (wxTheClipboard
->Open())
1223 wxTextDataObject
*data
= new wxTextDataObject( text
);
1224 wxTheClipboard
->SetData( data
);
1225 wxTheClipboard
->Close();
1230 wxTheClipboard
->UsePrimarySelection(primary
);
1231 if (wxTheClipboard
->Open())
1233 wxTextDataObject
*data
= new wxTextDataObject( text
);
1236 rc
= wxTheClipboard
->SetData( data
);
1238 rc
|= wxTheClipboard
->SetData( wldo
);
1239 wxTheClipboard
->Close();
1249 wxLayoutWindow::Cut(bool privateFormat
, bool usePrimary
)
1251 if(Copy(false, privateFormat
, usePrimary
)) // do not invalidate selection after copy
1253 m_llist
->DeleteSelection();
1261 // ----------------------------------------------------------------------------
1263 // ----------------------------------------------------------------------------
1266 wxLayoutWindow::Find(const wxString
&needle
,
1267 wxPoint
* fromWhere
,
1268 const wxString
&configPath
)
1273 if(needle
.Length() == 0)
1275 if( ! MInputBox(&m_FindString
,
1280 || strutil_isempty(m_FindString
))
1284 m_FindString
= needle
;
1286 if(fromWhere
== NULL
)
1287 found
= m_llist
->FindText(m_FindString
, m_llist
->GetCursorPos());
1289 found
= m_llist
->FindText(m_FindString
, *fromWhere
);
1297 m_llist
->MoveCursorTo(found
);
1308 wxLayoutWindow::FindAgain(void)
1310 bool rc
= Find(m_FindString
);
1314 // ----------------------------------------------------------------------------
1316 // ----------------------------------------------------------------------------
1319 wxLayoutWindow::MakeFormatMenu()
1321 wxMenu
*m
= new wxMenu(_("Layout Menu"));
1323 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
1324 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
1325 m
->AppendSeparator();
1326 m
->Append(WXLOWIN_MENU_UNDERLINE
, _("&Underline"),_("Underline mode."), true);
1327 m
->Append(WXLOWIN_MENU_BOLD
, _("&Bold"),_("Bold mode."), true);
1328 m
->Append(WXLOWIN_MENU_ITALICS
, _("&Italics"),_("Italics mode."), true);
1329 m
->AppendSeparator();
1330 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
1331 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
1332 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
1337 void wxLayoutWindow::OnUpdateMenuUnderline(wxUpdateUIEvent
& event
)
1339 event
.Check(m_llist
->IsFontUnderlined());
1342 void wxLayoutWindow::OnUpdateMenuBold(wxUpdateUIEvent
& event
)
1344 event
.Check(m_llist
->IsFontBold());
1347 void wxLayoutWindow::OnUpdateMenuItalic(wxUpdateUIEvent
& event
)
1349 event
.Check(m_llist
->IsFontItalic());
1352 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
1354 switch (event
.GetId())
1356 case WXLOWIN_MENU_LARGER
:
1357 m_llist
->SetFontLarger(); RequestUpdate(); break;
1358 case WXLOWIN_MENU_SMALLER
:
1359 m_llist
->SetFontSmaller(); RequestUpdate(); break;
1360 case WXLOWIN_MENU_UNDERLINE
:
1361 m_llist
->ToggleFontUnderline(); RequestUpdate(); break;
1362 case WXLOWIN_MENU_BOLD
:
1363 m_llist
->ToggleFontWeight(); RequestUpdate(); break;
1364 case WXLOWIN_MENU_ITALICS
:
1365 m_llist
->ToggleFontItalics(); RequestUpdate(); break;
1366 case WXLOWIN_MENU_ROMAN
:
1367 m_llist
->SetFontFamily(wxROMAN
); RequestUpdate(); break;
1368 case WXLOWIN_MENU_TYPEWRITER
:
1369 m_llist
->SetFontFamily(wxFIXED
); RequestUpdate(); break;
1370 case WXLOWIN_MENU_SANSSERIF
:
1371 m_llist
->SetFontFamily(wxSWISS
); RequestUpdate(); break;
1375 // ----------------------------------------------------------------------------
1377 // ----------------------------------------------------------------------------
1380 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
1384 RequestUpdate(); // cursor must change
1388 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
1390 m_HaveFocus
= false;
1392 RequestUpdate();// cursor must change
1395 // ----------------------------------------------------------------------------
1396 // private functions
1397 // ----------------------------------------------------------------------------
1399 static bool IsDirectionKey(long keyCode
)