1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
4 * (C) 1998, 1999 by Karsten Ballüder (karsten@phy.hw.ac.uk) *
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
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
64 # define WXLO_DEBUG(x) wxLogDebug x
66 # define WXLO_DEBUG(x)
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 /// offsets to put a nice frame around text
74 #define WXLO_XOFFSET 4
75 #define WXLO_YOFFSET 4
77 /// offset to the right and bottom for when to redraw scrollbars
78 #define WXLO_ROFFSET 20
79 #define WXLO_BOFFSET 20
81 /// scroll margins when selecting with the mouse
82 #define WXLO_SCROLLMARGIN_X 10
83 #define WXLO_SCROLLMARGIN_Y 10
85 /// the size of one scrollbar page in pixels
86 static const int X_SCROLL_PAGE
= 10;
87 static const int Y_SCROLL_PAGE
= 20;
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
96 EVT_SIZE (wxLayoutWindow::OnSize
)
98 EVT_PAINT (wxLayoutWindow::OnPaint
)
100 EVT_CHAR (wxLayoutWindow::OnChar
)
101 EVT_KEY_UP (wxLayoutWindow::OnKeyUp
)
103 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseDown
)
104 EVT_LEFT_UP(wxLayoutWindow::OnLeftMouseUp
)
105 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
106 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
107 EVT_MIDDLE_DOWN(wxLayoutWindow::OnMiddleMouseDown
)
108 EVT_MOTION (wxLayoutWindow::OnMouseMove
)
110 EVT_UPDATE_UI(WXLOWIN_MENU_UNDERLINE
, wxLayoutWindow::OnUpdateMenuUnderline
)
111 EVT_UPDATE_UI(WXLOWIN_MENU_BOLD
, wxLayoutWindow::OnUpdateMenuBold
)
112 EVT_UPDATE_UI(WXLOWIN_MENU_ITALICS
, wxLayoutWindow::OnUpdateMenuItalic
)
113 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
115 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
116 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
118 // EVT_IDLE(wxLayoutWindow::ResizeScrollbars)
121 // ----------------------------------------------------------------------------
122 // function prototypes
123 // ----------------------------------------------------------------------------
125 /// returns TRUE if keyCode is one of arrows/home/end/page{up|down} keys
126 static bool IsDirectionKey(long keyCode
);
128 // ============================================================================
130 // ============================================================================
132 #ifndef wxWANTS_CHARS
133 # define wxWANTS_CHARS 0
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
141 : wxScrolledWindow(parent
, -1,
142 wxDefaultPosition
, wxDefaultSize
,
143 wxHSCROLL
| wxVSCROLL
|
148 SetStatusBar(NULL
); // don't use statusbar
150 m_doSendEvents
= false;
151 m_ViewStartX
= 0; m_ViewStartY
= 0;
152 m_DoPopupMenu
= true;
153 m_PopupMenu
= MakeFormatMenu();
154 m_memDC
= new wxMemoryDC
;
155 m_bitmap
= new wxBitmap(4,4);
156 m_bitmapSize
= wxPoint(4,4);
157 m_llist
= new wxLayoutList();
159 m_ScrollToCursor
= false;
161 m_FocusFollowMode
= false;
165 // no scrollbars initially
167 m_hasVScrollbar
= false;
171 #ifdef WXLAYOUT_USE_CARET
172 // FIXME cursor size shouldn't be hardcoded
173 wxCaret
*caret
= new wxCaret(this, 2, 20);
175 m_llist
->SetCaret(caret
);
176 #endif // WXLAYOUT_USE_CARET
179 m_HandCursor
= FALSE
;
180 m_CursorVisibility
= -1;
181 SetCursor(wxCURSOR_IBEAM
);
184 // at least under Windows, this should be the default behaviour
185 m_AutoDeleteSelection
= TRUE
;
188 wxLayoutWindow::~wxLayoutWindow()
190 delete m_memDC
; // deletes bitmap automatically (?)
194 SetBackgroundBitmap(NULL
);
198 wxLayoutWindow::Clear(int family
,
206 GetLayoutList()->Clear(family
,size
,style
,weight
,underline
,fg
,bg
);
207 SetBackgroundColour(GetLayoutList()->GetDefaultStyleInfo().GetBGColour());
208 wxScrolledWindow::Clear();
209 ResizeScrollbars(true);
213 m_CursorVisibility
= 1;
215 #ifdef WXLAYOUT_USE_CARET
216 if ( m_CursorVisibility
== 1 )
218 #endif // WXLAYOUT_USE_CARET
220 RequestUpdate((wxRect
*)NULL
);
223 void wxLayoutWindow::Refresh(bool eraseBackground
, const wxRect
*rect
)
225 wxScrolledWindow::Refresh(eraseBackground
, rect
);
229 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
231 wxClientDC
dc( this );
233 if ( eventId
!= WXLOWIN_MENU_MOUSEMOVE
241 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
242 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
244 findPos
.x
-= WXLO_XOFFSET
;
245 findPos
.y
-= WXLO_YOFFSET
;
252 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
254 // Scroll the window if the mouse is at the end of it:
255 if(m_Selecting
&& eventId
== WXLOWIN_MENU_MOUSEMOVE
)
257 //WXLO_DEBUG(("selecting at : %d/%d", (int) event.GetX(), (int)event.GetY()));
259 ViewStart(&left
, &top
);
260 wxSize size
= GetClientSize();
263 if(event
.GetX() < WXLO_SCROLLMARGIN_X
)
264 xdelta
= -(WXLO_SCROLLMARGIN_X
-event
.GetX());
265 else if(event
.GetX() > size
.x
-WXLO_SCROLLMARGIN_X
)
266 xdelta
= event
.GetX()-size
.x
+WXLO_SCROLLMARGIN_X
;
269 if(event
.GetY() < WXLO_SCROLLMARGIN_Y
)
270 ydelta
= -(WXLO_SCROLLMARGIN_Y
-event
.GetY());
271 else if(event
.GetY() > size
.y
-WXLO_SCROLLMARGIN_Y
)
272 ydelta
= event
.GetY()-size
.y
+WXLO_SCROLLMARGIN_Y
;
276 //WXLO_DEBUG(("xdelta: %d", (int) xdelta));
277 if(xdelta
!= 0 || ydelta
!= 0)
279 top
+= ydelta
; if(top
< 0) top
= 0;
280 left
+= xdelta
; if(left
< 0) left
= 0;
287 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
289 wxLayoutObject::UserData
*u
= obj
? obj
->GetUserData() : NULL
;
291 // has the mouse only been moved?
294 case WXLOWIN_MENU_MOUSEMOVE
:
296 // this variables is used to only erase the message in the status
297 // bar if we had put it there previously - otherwise empting status
298 // bar might be undesirable
299 static bool s_hasPutMessageInStatusBar
= false;
301 // found is only true if we are really over an object, not just
303 if(found
&& u
&& ! m_Selecting
)
306 SetCursor(wxCURSOR_HAND
);
308 if(m_StatusBar
&& m_StatusFieldLabel
!= -1)
310 const wxString
&label
= u
->GetLabel();
313 m_StatusBar
->SetStatusText(label
,
315 s_hasPutMessageInStatusBar
= true;
322 SetCursor(wxCURSOR_IBEAM
);
323 m_HandCursor
= FALSE
;
324 if( m_StatusBar
&& m_StatusFieldLabel
!= -1 &&
325 s_hasPutMessageInStatusBar
)
327 m_StatusBar
->SetStatusText("", m_StatusFieldLabel
);
333 if ( event
.LeftIsDown() )
335 // m_Selecting might not be set if the button got pressed
336 // outside this window, so check for it:
339 m_llist
->ContinueSelection(cursorPos
, m_ClickPosition
);
340 RequestUpdate(); // TODO: we don't have to redraw everything!
351 case WXLOWIN_MENU_LDOWN
:
353 // always move cursor to mouse click:
354 m_llist
->MoveCursorTo(cursorPos
);
356 // clicking a mouse removes the selection
357 if ( m_llist
->HasSelection() )
359 m_llist
->DiscardSelection();
361 RequestUpdate(); // TODO: we don't have to redraw everything!
364 // Calculate where the top of the visible area is:
368 GetScrollPixelsPerUnit(&dx
, &dy
);
371 wxPoint
offset(-x0
+WXLO_XOFFSET
, -y0
+WXLO_YOFFSET
);
373 if(m_CursorVisibility
== -1)
374 m_CursorVisibility
= 1;
375 #ifdef WXLAYOUT_USE_CARET
376 if ( m_CursorVisibility
== 1 )
378 #endif // WXLAYOUT_USE_CARET
380 if(m_CursorVisibility
)
382 // draw a thick cursor for editable windows with focus
383 m_llist
->DrawCursor(dc
, m_HaveFocus
&& IsEditable(), offset
);
387 RequestUpdate(); // RequestUpdate suppresses flicker under GTK
391 m_llist
->StartSelection(wxPoint(-1, -1), m_ClickPosition
);
396 case WXLOWIN_MENU_LUP
:
399 // end selection at the cursor position corresponding to the
400 // current mouse position, but don´t move cursor there.
401 m_llist
->EndSelection(cursorPos
,m_ClickPosition
);
404 RequestUpdate(); // TODO: we don't have to redraw everything!
408 case WXLOWIN_MENU_MDOWN
:
412 case WXLOWIN_MENU_DBLCLICK
:
413 // select a word under cursor
414 m_llist
->MoveCursorTo(cursorPos
);
415 m_llist
->MoveCursorWord(-1);
416 m_llist
->StartSelection();
417 m_llist
->MoveCursorWord(1, false);
418 m_llist
->EndSelection();
420 RequestUpdate(); // TODO: we don't have to redraw everything!
424 // notify about mouse events?
427 // only do the menu if activated, editable and not on a clickable object
428 if(eventId
== WXLOWIN_MENU_RCLICK
430 && (! obj
|| u
== NULL
))
432 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
437 // find the object at this position
440 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
441 commandEvent
.SetEventObject( this );
442 commandEvent
.SetClientData((char *)obj
);
443 GetEventHandler()->ProcessEvent(commandEvent
);
450 // ----------------------------------------------------------------------------
451 // keyboard handling.
452 // ----------------------------------------------------------------------------
455 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
457 int keyCode
= event
.KeyCode();
458 bool ctrlDown
= event
.ControlDown();
460 #ifdef WXLAYOUT_DEBUG
461 if(keyCode
== WXK_F1
)
468 // Force m_Selecting to be false if shift is no longer
469 // pressed. OnKeyUp() cannot catch all Shift-Up events.
470 if(m_Selecting
&& !event
.ShiftDown())
473 m_llist
->EndSelection();
474 m_llist
->DiscardSelection(); //FIXME: correct?
477 // If we deleted the selection here, we must not execute the
478 // deletion in Delete/Backspace handling.
479 bool deletedSelection
= false;
480 // pressing any non-arrow key optionally replaces the selection:
481 if(m_AutoDeleteSelection
483 && m_llist
->HasSelection()
484 && ! IsDirectionKey(keyCode
)
485 && ! (event
.AltDown() || ctrlDown
)
488 m_llist
->DeleteSelection();
489 deletedSelection
= true;
493 // <Shift>+<arrow> starts selection
494 if ( IsDirectionKey(keyCode
) )
496 // just continue the old selection
497 if ( m_Selecting
&& event
.ShiftDown() )
498 m_llist
->ContinueSelection();
501 m_llist
->DiscardSelection();
503 if( event
.ShiftDown() )
506 m_llist
->StartSelection();
511 // If needed, make cursor visible:
512 if(m_CursorVisibility
== -1)
513 m_CursorVisibility
= 1;
515 /* These two nested switches work like this:
516 The first one processes all non-editing keycodes, to move the
517 cursor, etc. It's default will process all keycodes causing
518 modifications to the buffer, but only if editing is allowed.
524 m_llist
->MoveCursorWord(1);
526 m_llist
->MoveCursorHorizontally(1);
530 m_llist
->MoveCursorWord(-1);
532 m_llist
->MoveCursorHorizontally(-1);
535 m_llist
->MoveCursorVertically(-1);
538 m_llist
->MoveCursorVertically(1);
541 m_llist
->MoveCursorVertically(-Y_SCROLL_PAGE
);
544 m_llist
->MoveCursorVertically(Y_SCROLL_PAGE
);
548 m_llist
->MoveCursorTo(wxPoint(0, 0));
550 m_llist
->MoveCursorToBeginOfLine();
554 m_llist
->MoveCursorToEnd();
556 m_llist
->MoveCursorToEndOfLine();
560 if(ctrlDown
&& ! IsEditable())
564 // this should work even in read-only mode
570 case 't': // search again
574 // we don't handle it, maybe an accelerator?
578 else if( IsEditable() )
580 /* First, handle control keys */
581 if(ctrlDown
&& ! event
.AltDown())
583 if(keyCode
>= 'A' && keyCode
<= 'Z')
584 keyCode
= tolower(keyCode
);
591 if(! deletedSelection
)
593 m_llist
->DeleteWord();
598 if(! deletedSelection
) // already done
605 m_llist
->DeleteLines(1);
608 case 'h': // like backspace
609 if(m_llist
->MoveCursorHorizontally(-1))
618 case 't': // search again
622 m_llist
->DeleteToBeginOfLine();
626 m_llist
->DeleteToEndOfLine();
638 #ifdef WXLAYOUT_DEBUG
640 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
644 // we don't handle it, maybe an accelerator?
649 else if( event
.AltDown() && ! event
.ControlDown() )
655 m_llist
->DeleteWord();
659 // we don't handle it, maybe an accelerator?
664 else if ( ! event
.AltDown() && ! event
.ControlDown())
669 if(event
.ShiftDown())
673 if(event
.ShiftDown())
676 if(! deletedSelection
)
682 case WXK_BACK
: // backspace
683 if(! deletedSelection
)
684 if(m_llist
->MoveCursorHorizontally(-1))
692 m_llist
->WrapLine(m_WrapMargin
);
693 m_llist
->LineBreak();
698 if ( !event
.ShiftDown() )
700 // TODO should be configurable
701 static const int tabSize
= 8;
703 CoordType x
= m_llist
->GetCursorPos().x
;
704 size_t numSpaces
= tabSize
- x
% tabSize
;
705 m_llist
->Insert(wxString(' ', numSpaces
));
711 if((!(event
.ControlDown() || event
.AltDown()
713 && (keyCode
< 256 && keyCode
>= 32)
716 if(m_WrapMargin
> 0 && isspace(keyCode
))
718 bool wrapped
= m_llist
->WrapLine(m_WrapMargin
);
719 // don´t insert space as first thing in line
721 if(! wrapped
|| m_llist
->GetCursorPos().x
!= 0)
722 m_llist
->Insert((char)keyCode
);
725 m_llist
->Insert((char)keyCode
);
729 // we don't handle it, maybe an accelerator?
736 // we don't handle it, maybe an accelerator?
742 // continue selection to the current (new) cursor position
743 m_llist
->ContinueSelection();
746 // refresh the screen
747 RequestUpdate(m_llist
->GetUpdateRect());
751 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
753 if ( event
.KeyCode() == WXK_SHIFT
&& m_Selecting
)
755 m_llist
->EndSelection();
764 wxLayoutWindow::ScrollToCursor(void)
766 //is always needed to make sure we know where the cursor is
768 RequestUpdate(m_llist
->GetUpdateRect());
770 int x0
,y0
,x1
,y1
, dx
, dy
;
772 // Calculate where the top of the visible area is:
774 GetScrollPixelsPerUnit(&dx
, &dy
);
777 WXLO_DEBUG(("ScrollToCursor: ViewStart is %d/%d", x0
, y0
));
779 // Get the size of the visible window:
780 GetClientSize(&x1
, &y1
);
782 // Make sure that the scrollbars are at a position so that the cursor is
783 // visible if we are editing
784 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
785 wxPoint cc
= m_llist
->GetCursorScreenPos();
787 // the cursor should be completely visible in both directions
788 wxPoint
cs(m_llist
->GetCursorSize());
791 if ( cc
.x
< x0
|| cc
.x
>= x0
+ x1
- cs
.x
)
798 if ( cc
.y
< y0
|| cc
.y
>= y0
+ y1
- cs
.y
)
805 if ( nx
!= -1 || ny
!= -1 )
807 // set new view start
808 Scroll(nx
== -1 ? -1 : (nx
+dx
-1)/dx
, ny
== -1 ? -1 : (ny
+dy
-1)/dy
);
810 m_ScrollToCursor
= false;
816 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
))
818 wxRect region
= GetUpdateRegion().GetBox();
819 InternalPaint(®ion
);
823 wxLayoutWindow::RequestUpdate(const wxRect
*updateRect
)
826 // Calling Refresh() causes bad flicker under wxGTK!!!
827 InternalPaint(updateRect
);
829 // shouldn't specify the update rectangle if it doesn't include all the
830 // changed locations - otherwise, they won't be repainted at all because
831 // the system clips the display to the update rect
832 Refresh(FALSE
); //, updateRect);
837 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
839 wxPaintDC
dc( this );
842 #ifdef WXLAYOUT_USE_CARET
843 // hide the caret before drawing anything
845 #endif // WXLAYOUT_USE_CARET
847 int x0
,y0
,x1
,y1
, dx
, dy
;
849 // Calculate where the top of the visible area is:
851 GetScrollPixelsPerUnit(&dx
, &dy
);
854 // Get the size of the visible window:
855 GetClientSize(&x1
,&y1
);
861 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
862 updateRect
->x
, updateRect
->y
,
863 updateRect
->x
+updateRect
->width
,
864 updateRect
->y
+updateRect
->height
));
870 /* Check whether the window has grown, if so, we need to reallocate
871 the bitmap to be larger. */
872 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
874 wxASSERT(m_bitmapSize
.x
> 0);
875 wxASSERT(m_bitmapSize
.y
> 0);
877 m_memDC
->SelectObject(wxNullBitmap
);
879 m_bitmapSize
= wxPoint(x1
,y1
);
880 m_bitmap
= new wxBitmap(x1
,y1
);
881 m_memDC
->SelectObject(*m_bitmap
);
884 m_memDC
->SetDeviceOrigin(0,0);
885 m_memDC
->SetBackground(wxBrush(m_llist
->GetDefaultStyleInfo().GetBGColour(),wxSOLID
));
886 m_memDC
->SetPen(wxPen(m_llist
->GetDefaultStyleInfo().GetBGColour(),
888 m_memDC
->SetLogicalFunction(wxCOPY
);
891 // fill the background with the background bitmap
896 w
= m_BGbitmap
->GetWidth(),
897 h
= m_BGbitmap
->GetHeight();
898 for(y
= 0; y
< y1
; y
+=h
)
899 for(x
= 0; x
< x1
; x
+=w
)
900 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
901 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
904 // This is the important bit: we tell the list to draw itself
908 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
909 updateRect
->x
, updateRect
->y
,
910 updateRect
->x
+updateRect
->width
,
911 updateRect
->y
+updateRect
->height
));
915 // Device origins on the memDC are suspect, we translate manually
916 // with the translate parameter of Draw().
917 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
918 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
920 // We start calculating a new update rect before drawing the
921 // cursor, so that the cursor coordinates get included in the next
922 // update rectangle (although they are drawn on the memDC, this is
923 // needed to erase it):
924 m_llist
->InvalidateUpdateRect();
925 if(m_CursorVisibility
!= 0)
927 // draw a thick cursor for editable windows with focus
928 m_llist
->DrawCursor(*m_memDC
,
929 m_HaveFocus
&& IsEditable(),
933 // Now copy everything to the screen:
935 // This somehow doesn't work, but even the following bit with the
936 // whole rect at once is still a bit broken I think.
937 wxRegionIterator
ri ( GetUpdateRegion() );
941 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
942 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
943 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
944 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
950 // FIXME: Trying to copy only the changed parts, but it does not seem
952 // x0 = updateRect->x; y0 = updateRect->y;
953 // if(updateRect->height < y1)
954 // y1 = updateRect->height;
955 // y1 += WXLO_YOFFSET; //FIXME might not be needed
956 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
959 #ifdef WXLAYOUT_USE_CARET
960 // show the caret back after everything is redrawn
962 #endif // WXLAYOUT_USE_CARET
966 if ( m_StatusBar
&& m_StatusFieldCursor
!= -1 )
968 static wxPoint
s_oldCursorPos(-1, -1);
970 wxPoint
pos(m_llist
->GetCursorPos());
972 // avoid unnecessary status bar refreshes
973 if ( pos
!= s_oldCursorPos
)
975 s_oldCursorPos
= pos
;
978 label
.Printf(_("Ln:%d Col:%d"), pos
.y
+ 1, pos
.x
+ 1);
979 m_StatusBar
->SetStatusText(label
, m_StatusFieldCursor
);
985 wxLayoutWindow::OnSize(wxSizeEvent
&event
)
994 Change the range and position of scrollbars. Has evolved into a
995 generic Update function which will at some time later cause a repaint
1000 wxLayoutWindow::ResizeScrollbars(bool exact
)
1006 wxClientDC
dc( this );
1008 // m_llist->ForceTotalLayout();
1009 m_llist
->Layout(dc
);
1012 wxPoint max
= m_llist
->GetSize();
1013 wxSize size
= GetClientSize();
1015 WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
1016 (long int)max
.x
, (long int) max
.y
));
1018 // in the absence of scrollbars we should compare with the client size
1019 if ( !m_hasHScrollbar
)
1020 m_maxx
= size
.x
;// - WXLO_ROFFSET;
1021 if ( !m_hasVScrollbar
)
1022 m_maxy
= size
.y
;// - WXLO_BOFFSET;
1024 // check if the text hasn't become too big
1025 // TODO why do we set both at once? they're independent...
1026 if( max
.x
> m_maxx
- WXLO_ROFFSET
1027 || max
.y
> m_maxy
- WXLO_BOFFSET
1028 || max
.x
< m_maxx
- X_SCROLL_PAGE
1029 || max
.y
< m_maxy
- Y_SCROLL_PAGE
1032 // text became too large
1035 // add an extra bit to the sizes to avoid future updates
1036 max
.x
+= WXLO_ROFFSET
;
1037 max
.y
+= WXLO_BOFFSET
;
1041 if(max
.x
< X_SCROLL_PAGE
)
1043 SetScrollbars(0,-1,0,-1,0,-1,true);
1044 m_hasHScrollbar
= FALSE
;
1047 if(max
.y
< Y_SCROLL_PAGE
)
1049 SetScrollbars(-1,0,-1,0,-1,0,true);
1050 m_hasVScrollbar
= FALSE
;
1055 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
1056 SetScrollbars(X_SCROLL_PAGE
,
1058 max
.x
/ X_SCROLL_PAGE
+ 1,
1059 max
.y
/ Y_SCROLL_PAGE
+ 1,
1060 m_ViewStartX
, m_ViewStartY
,
1063 m_hasVScrollbar
= true;
1066 m_maxx
= max
.x
+ X_SCROLL_PAGE
;
1067 m_maxy
= max
.y
+ Y_SCROLL_PAGE
;
1071 // ----------------------------------------------------------------------------
1073 // clipboard operations
1075 // ----------------------------------------------------------------------------
1078 wxLayoutWindow::Paste(bool usePrivate
, bool primary
)
1080 // this only has an effect under X11:
1081 wxTheClipboard
->UsePrimarySelection(primary
);
1083 if (wxTheClipboard
->Open())
1087 wxLayoutDataObject wxldo
;
1088 if (wxTheClipboard
->IsSupported( wxldo
.GetFormat() ))
1090 if(wxTheClipboard
->GetData(wxldo
))
1092 wxTheClipboard
->Close();
1093 wxString str
= wxldo
.GetLayoutData();
1101 wxTextDataObject data
;
1102 if (wxTheClipboard
->IsSupported( data
.GetFormat() )
1103 && wxTheClipboard
->GetData(data
) )
1105 wxTheClipboard
->Close();
1106 wxString text
= data
.GetText();
1107 wxLayoutImportText( m_llist
, text
);
1113 // if everything failed we can still try the primary:
1114 wxTheClipboard
->Close();
1115 if(! primary
) // not tried before
1117 wxTheClipboard
->UsePrimarySelection();
1118 if (wxTheClipboard
->Open())
1120 wxTextDataObject data
;
1121 if (wxTheClipboard
->IsSupported( data
.GetFormat() )
1122 && wxTheClipboard
->GetData(data
) )
1124 wxString text
= data
.GetText();
1125 wxLayoutImportText( m_llist
, text
);
1129 wxTheClipboard
->Close();
1135 wxLayoutWindow::Copy(bool invalidate
, bool privateFormat
, bool primary
)
1137 // Calling GetSelection() will automatically do an EndSelection()
1138 // on the list, but we need to take a note of it, too:
1141 m_Selecting
= false;
1142 m_llist
->EndSelection();
1145 wxLayoutDataObject
*wldo
= new wxLayoutDataObject
;
1146 wxLayoutList
*llist
= m_llist
->GetSelection(wldo
, invalidate
);
1149 // Export selection as text:
1151 wxLayoutExportObject
*exp
;
1152 wxLayoutExportStatus
status(llist
);
1153 while((exp
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
1155 if(exp
->type
== WXLO_EXPORT_TEXT
)
1156 text
<< *(exp
->content
.text
);
1161 // The exporter always appends a newline, so we chop it off if it
1164 size_t len
= text
.Length();
1165 if(len
> 2 && text
[len
-2] == '\r') // Windows
1166 text
= text
.Mid(0,len
-2);
1167 else if(len
> 1 && text
[len
-1] == '\n')
1168 text
= text
.Mid(0,len
-1);
1172 if(! primary
) // always copy as text-only to primary selection
1174 wxTheClipboard
->UsePrimarySelection();
1175 if (wxTheClipboard
->Open())
1177 wxTextDataObject
*data
= new wxTextDataObject( text
);
1178 wxTheClipboard
->SetData( data
);
1179 wxTheClipboard
->Close();
1184 wxTheClipboard
->UsePrimarySelection(primary
);
1185 if (wxTheClipboard
->Open())
1187 wxTextDataObject
*data
= new wxTextDataObject( text
);
1190 rc
= wxTheClipboard
->SetData( data
);
1192 rc
|= wxTheClipboard
->SetData( wldo
);
1193 wxTheClipboard
->Close();
1203 wxLayoutWindow::Cut(bool privateFormat
, bool usePrimary
)
1205 if(Copy(false, privateFormat
, usePrimary
)) // do not invalidate selection after copy
1207 m_llist
->DeleteSelection();
1215 // ----------------------------------------------------------------------------
1217 // ----------------------------------------------------------------------------
1220 wxLayoutWindow::Find(const wxString
&needle
,
1221 wxPoint
* fromWhere
,
1222 const wxString
&configPath
)
1227 if(needle
.Length() == 0)
1229 if( ! MInputBox(&m_FindString
,
1234 || strutil_isempty(m_FindString
))
1238 m_FindString
= needle
;
1240 if(fromWhere
== NULL
)
1241 found
= m_llist
->FindText(m_FindString
, m_llist
->GetCursorPos());
1243 found
= m_llist
->FindText(m_FindString
, *fromWhere
);
1251 m_llist
->MoveCursorTo(found
);
1262 wxLayoutWindow::FindAgain(void)
1264 bool rc
= Find(m_FindString
);
1268 // ----------------------------------------------------------------------------
1270 // ----------------------------------------------------------------------------
1273 wxLayoutWindow::MakeFormatMenu()
1275 wxMenu
*m
= new wxMenu(_("Layout Menu"));
1277 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
1278 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
1279 m
->AppendSeparator();
1280 m
->Append(WXLOWIN_MENU_UNDERLINE
, _("&Underline"),_("Underline mode."), true);
1281 m
->Append(WXLOWIN_MENU_BOLD
, _("&Bold"),_("Bold mode."), true);
1282 m
->Append(WXLOWIN_MENU_ITALICS
, _("&Italics"),_("Italics mode."), true);
1283 m
->AppendSeparator();
1284 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
1285 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
1286 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
1291 void wxLayoutWindow::OnUpdateMenuUnderline(wxUpdateUIEvent
& event
)
1293 event
.Check(m_llist
->IsFontUnderlined());
1296 void wxLayoutWindow::OnUpdateMenuBold(wxUpdateUIEvent
& event
)
1298 event
.Check(m_llist
->IsFontBold());
1301 void wxLayoutWindow::OnUpdateMenuItalic(wxUpdateUIEvent
& event
)
1303 event
.Check(m_llist
->IsFontItalic());
1306 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
1308 switch (event
.GetId())
1310 case WXLOWIN_MENU_LARGER
:
1311 m_llist
->SetFontLarger(); RequestUpdate(); break;
1312 case WXLOWIN_MENU_SMALLER
:
1313 m_llist
->SetFontSmaller(); RequestUpdate(); break;
1314 case WXLOWIN_MENU_UNDERLINE
:
1315 m_llist
->ToggleFontUnderline(); RequestUpdate(); break;
1316 case WXLOWIN_MENU_BOLD
:
1317 m_llist
->ToggleFontWeight(); RequestUpdate(); break;
1318 case WXLOWIN_MENU_ITALICS
:
1319 m_llist
->ToggleFontItalics(); RequestUpdate(); break;
1320 case WXLOWIN_MENU_ROMAN
:
1321 m_llist
->SetFontFamily(wxROMAN
); RequestUpdate(); break;
1322 case WXLOWIN_MENU_TYPEWRITER
:
1323 m_llist
->SetFontFamily(wxFIXED
); RequestUpdate(); break;
1324 case WXLOWIN_MENU_SANSSERIF
:
1325 m_llist
->SetFontFamily(wxSWISS
); RequestUpdate(); break;
1329 // ----------------------------------------------------------------------------
1331 // ----------------------------------------------------------------------------
1334 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
1338 RequestUpdate(); // cursor must change
1342 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
1344 m_HaveFocus
= false;
1346 RequestUpdate();// cursor must change
1349 // ----------------------------------------------------------------------------
1350 // private functions
1351 // ----------------------------------------------------------------------------
1353 static bool IsDirectionKey(long keyCode
)