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"
27 # include "gui/wxlwindow.h"
28 # include "gui/wxlparser.h"
37 # include "wxlwindow.h"
38 # include "wxlparser.h"
41 #include <wx/clipbrd.h>
42 #include <wx/textctrl.h>
43 #include <wx/dataobj.h>
47 /// offsets to put a nice frame around text
48 #define WXLO_XOFFSET 4
49 #define WXLO_YOFFSET 4
51 /// offset to the right and bottom for when to redraw scrollbars
52 #define WXLO_ROFFSET 20
53 #define WXLO_BOFFSET 20
55 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
56 EVT_PAINT (wxLayoutWindow::OnPaint
)
57 EVT_CHAR (wxLayoutWindow::OnChar
)
58 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
59 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
60 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
61 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
62 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
63 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
66 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
67 : wxScrolledWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
68 wxHSCROLL
| wxVSCROLL
| wxBORDER
)
72 m_doSendEvents
= false;
73 m_ViewStartX
= 0; m_ViewStartY
= 0;
75 m_PopupMenu
= MakeFormatMenu();
76 m_memDC
= new wxMemoryDC
;
77 m_bitmap
= new wxBitmap(4,4);
78 m_bitmapSize
= wxPoint(4,4);
79 m_llist
= new wxLayoutList();
81 m_ScrollToCursor
= false;
83 wxPoint max
= m_llist
->GetSize();
84 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
85 EnableScrolling(true,true);
86 m_maxx
= max
.x
; m_maxy
= max
.y
;
88 SetCursor(wxCURSOR_IBEAM
);
92 wxLayoutWindow::~wxLayoutWindow()
94 delete m_memDC
; // deletes bitmap automatically (?)
98 SetBackgroundBitmap(NULL
);
103 wxLayoutWindow::MSWGetDlgCode()
105 // if we don't return this, we won't get OnChar() events for TABs and ENTER
106 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
111 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
113 wxPaintDC
dc( this );
119 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
120 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
122 findPos
.x
-= WXLO_XOFFSET
;
123 findPos
.y
-= WXLO_YOFFSET
;
125 if(findPos
.x
< 0) findPos
.x
= 0;
126 if(findPos
.y
< 0) findPos
.y
= 0;
128 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
129 #ifdef WXLAYOUT_DEBUG
130 wxLogDebug("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
131 event
.GetX(), event
.GetY(), findPos
.x
, findPos
.y
);
135 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
, &cursorPos
);
137 #ifdef WXLAYOUT_DEBUG
139 wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
142 wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
145 // always move cursor to mouse click:
146 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
148 m_llist
->MoveCursorTo(cursorPos
);
149 m_ScrollToCursor
= true; //FIXME: needed? DoPaint(m_llist->GetUpdateRect());
151 if(!m_doSendEvents
) // nothing to do
154 // only do the menu if activated, editable and not on a clickable object
155 if(eventId
== WXLOWIN_MENU_RCLICK
157 && (! obj
|| (obj
&& obj
->GetUserData() == NULL
))
160 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
163 // find the object at this position
166 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
167 commandEvent
.SetEventObject( this );
168 commandEvent
.SetClientData((char *)obj
);
169 GetEventHandler()->ProcessEvent(commandEvent
);
174 * Some simple keyboard handling.
177 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
179 if(!IsEditable()) // do nothing
185 long keyCode
= event
.KeyCode();
186 if(event
.ShiftDown())
191 m_llist
->EndSelection();
194 /* First, handle control keys */
195 if(event
.ControlDown() && ! event
.AltDown())
204 m_llist
->DeleteLines(1);
206 case 'h': // like backspace
207 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
210 m_llist
->DeleteToBeginOfLine();
213 m_llist
->DeleteToEndOfLine();
215 #ifdef WXLAYOUT_DEBUG
217 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
225 else if( event
.AltDown() && ! event
.ControlDown() )
231 m_llist
->DeleteWord();
238 else if ( ! event
.AltDown() && ! event
.ControlDown())
243 if(m_Selecting
) m_llist
->StartSelection();
244 m_llist
->MoveCursorHorizontally(1);
247 if(m_Selecting
) m_llist
->StartSelection();
248 m_llist
->MoveCursorHorizontally(-1);
251 if(m_Selecting
) m_llist
->StartSelection();
252 m_llist
->MoveCursorVertically(-1);
255 if(m_Selecting
) m_llist
->StartSelection();
256 m_llist
->MoveCursorVertically(1);
259 if(m_Selecting
) m_llist
->StartSelection();
260 m_llist
->MoveCursorVertically(-20);
263 if(m_Selecting
) m_llist
->StartSelection();
264 m_llist
->MoveCursorVertically(20);
267 if(m_Selecting
) m_llist
->StartSelection();
268 m_llist
->MoveCursorToBeginOfLine();
271 if(m_Selecting
) m_llist
->StartSelection();
272 m_llist
->MoveCursorToEndOfLine();
277 case WXK_BACK
: // backspace
278 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
282 m_llist
->WrapLine(m_WrapMargin
);
283 m_llist
->LineBreak();
286 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
287 && (keyCode
< 256 && keyCode
>= 32)
292 if(m_WrapMargin
> 0 && isspace(keyCode
))
293 m_llist
->WrapLine(m_WrapMargin
);
294 m_llist
->Insert(tmp
);
301 m_ScrollToCursor
= true;
302 //DoPaint(true); // paint and scroll to cursor
303 wxRect r
= *m_llist
->GetUpdateRect();
304 r
.x
-= WXLO_XOFFSET
; r
.y
-= WXLO_YOFFSET
;
309 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
311 wxRect region
= GetUpdateRegion().GetBox();
312 InternalPaint(& region
);
316 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
319 InternalPaint(updateRect
);
321 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
326 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
328 wxPaintDC
dc( this );
331 int x0
,y0
,x1
,y1
, dx
, dy
;
333 // Calculate where the top of the visible area is:
335 GetScrollPixelsPerUnit(&dx
, &dy
);
338 // Get the size of the visible window:
339 GetClientSize(&x1
,&y1
);
342 // As we have the values anyway, use them to avoid unnecessary
343 // scrollbar updates.
344 if(x1
> m_maxx
) m_maxx
= x1
;
345 if(y1
> m_maxy
) m_maxy
= y1
;
348 m_llist
->InvalidateUpdateRect();
349 const wxRect
*r
= m_llist
->GetUpdateRect();
350 wxLogDebug("Update rect before calling Layout: %ld,%ld / %ld,%ld",
351 r
->x
, r
->y
, r
->x
+r
->width
, r
->y
+r
->height
);
354 //FIXME: we should never need to call Layout at all because the
355 // list does it automatically.
356 // Maybe we need to change the scrollbar sizes or positions,
357 // so layout the list and check:
360 wxLogDebug("Update rect after calling Layout: %ld,%ld / %ld,%ld",
361 r
->x
, r
->y
, r
->x
+r
->width
, r
->y
+r
->height
);
362 // this is needed even when only the cursor moved
363 m_llist
->Layout(dc
,y0
+y1
);
364 wxLogDebug("Update rect after calling Layout again: %ld,%ld / %ld,%ld",
365 r
->x
, r
->y
, r
->x
+r
->width
, r
->y
+r
->height
);
371 /* Make sure that the scrollbars are at a position so that the
372 cursor is visible if we are editing. */
373 /** Scroll so that cursor is visible! */
374 wxLogDebug("m_ScrollToCursor = %d", (int) m_ScrollToCursor
);
375 if(IsEditable() && m_ScrollToCursor
)
377 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
378 if(cc
.x
< x0
|| cc
.y
< y0
379 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
382 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
383 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
384 Scroll(nx
/dx
,ny
/dy
); // new view start
389 /* Check whether the window has grown, if so, we need to reallocate
390 the bitmap to be larger. */
391 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
393 wxASSERT(m_bitmapSize
.x
> 0);
394 wxASSERT(m_bitmapSize
.y
> 0);
396 m_memDC
->SelectObject(wxNullBitmap
);
398 m_bitmapSize
= wxPoint(x1
,y1
);
399 m_bitmap
= new wxBitmap(x1
,y1
);
400 m_memDC
->SelectObject(*m_bitmap
);
402 // Device origins on the memDC are suspect, we translate manually
403 // with the translate parameter of Draw().
404 m_memDC
->SetDeviceOrigin(0,0);
405 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
406 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(), wxSOLID
));
407 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),0,wxTRANSPARENT
));
408 m_memDC
->SetLogicalFunction(wxCOPY
);
413 w
= m_BGbitmap
->GetWidth(),
414 h
= m_BGbitmap
->GetHeight();
415 for(y
= 0; y
< y1
; y
+=h
)
416 for(x
= 0; x
< x1
; x
+=w
)
417 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
420 m_memDC
->DrawRectangle(0,0,x1
, y1
);
422 // The offsets give the window a tiny border on the left and top, looks nice.
423 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
424 m_llist
->Draw(*m_memDC
,offset
);
426 m_llist
->DrawCursor(*m_memDC
,m_HaveFocus
,offset
);
428 // Now copy everything to the screen:
431 // 1. the update region as calculated by the list is wrong
432 // 2. we get wrong values here
433 // 3. how about the offset?
434 wxRegionIterator
ri ( GetUpdateRegion() );
438 wxLogDebug("UpdateRegion: %ld,%ld, %ld,%ld",
439 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH());
440 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
441 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
446 // If there are no update rectangles, we got called to reflect
447 // a change in the list. Currently there is no mechanism to
448 // easily find out which bits need updating, so we update
449 // all. The wxLayoutList could handle this, creating a list or
450 // at least one large rectangle of changes. FIXME
451 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
454 m_ScrollToCursor
= false;
457 // change the range and position of scroll bars
459 wxLayoutWindow::ResizeScrollbars(bool exact
)
461 wxPoint max
= m_llist
->GetSize();
463 if(max
.x
> m_maxx
|| max
.y
> m_maxy
464 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
469 // add an extra bit to the sizes to avoid future updates
470 max
.x
= max
.x
+WXLO_ROFFSET
;
471 max
.y
= max
.y
+WXLO_BOFFSET
;
473 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
474 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
475 m_maxx
= max
.x
; m_maxy
= max
.y
;
480 wxLayoutWindow::Paste(void)
484 if (wxTheClipboard
->Open())
486 wxTextDataObject data
;
487 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
489 wxTheClipboard
->GetData(&data
);
490 text
+= data
.GetText();
492 wxTheClipboard
->Close();
495 /* wxGTK's sophisticated multi-format copy/paste is not supported
496 by 99% of the X11 clients available. If there was no selection,
497 do the dumb thing, too:
500 /* Unfortunately, this little hack doesn't work. So I'll go back to
502 if(text
.Length() == 0)
504 wxTextCtrl
tmp_tctrl(this,-1);
506 text
+= tmp_tctrl
.GetValue();
511 wxLayoutImportText( m_llist
, text
);
516 wxLayoutWindow::MakeFormatMenu()
518 wxMenu
*m
= new wxMenu(_("Layout Menu"));
520 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
521 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
522 m
->AppendSeparator();
523 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
524 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
525 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
526 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
527 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
528 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
529 m
->AppendSeparator();
530 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
531 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
532 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
536 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
538 switch (event
.GetId())
540 case WXLOWIN_MENU_LARGER
:
541 m_llist
->SetFontLarger(); break;
542 case WXLOWIN_MENU_SMALLER
:
543 m_llist
->SetFontSmaller(); break;
544 case WXLOWIN_MENU_UNDERLINE_ON
:
545 m_llist
->SetFontUnderline(true); break;
546 case WXLOWIN_MENU_UNDERLINE_OFF
:
547 m_llist
->SetFontUnderline(false); break;
548 case WXLOWIN_MENU_BOLD_ON
:
549 m_llist
->SetFontWeight(wxBOLD
); break;
550 case WXLOWIN_MENU_BOLD_OFF
:
551 m_llist
->SetFontWeight(wxNORMAL
); break;
552 case WXLOWIN_MENU_ITALICS_ON
:
553 m_llist
->SetFontStyle(wxITALIC
); break;
554 case WXLOWIN_MENU_ITALICS_OFF
:
555 m_llist
->SetFontStyle(wxNORMAL
); break;
556 case WXLOWIN_MENU_ROMAN
:
557 m_llist
->SetFontFamily(wxROMAN
); break;
558 case WXLOWIN_MENU_TYPEWRITER
:
559 m_llist
->SetFontFamily(wxFIXED
); break;
560 case WXLOWIN_MENU_SANSSERIF
:
561 m_llist
->SetFontFamily(wxSWISS
); break;
566 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
569 //FIXME DoPaint(); // to repaint the cursor
573 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
576 //FIXME DoPaint(); // to repaint the cursor