1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
4 * (C) 1998, 1999 by Karsten Ballüder (Ballueder@usa.net) *
7 *******************************************************************/
10 # pragma implementation "wxlwindow.h"
13 #include "wx/wxprec.h"
23 # include "gui/wxMenuDefs.h"
24 # include "gui/wxMApp.h"
26 # include "gui/wxlwindow.h"
27 # include "gui/wxlparser.h"
36 # include "wxlwindow.h"
37 # include "wxlparser.h"
40 #include <wx/clipbrd.h>
41 #include <wx/textctrl.h>
42 #include <wx/dataobj.h>
47 # define WXLO_DEBUG(x) wxLogDebug x
49 # define WXLO_DEBUG(x)
52 /// offsets to put a nice frame around text
53 #define WXLO_XOFFSET 4
54 #define WXLO_YOFFSET 4
56 /// offset to the right and bottom for when to redraw scrollbars
57 #define WXLO_ROFFSET 20
58 #define WXLO_BOFFSET 20
60 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
61 EVT_PAINT (wxLayoutWindow::OnPaint
)
62 EVT_CHAR (wxLayoutWindow::OnChar
)
63 EVT_KEY_UP (wxLayoutWindow::OnKeyUp
)
64 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
65 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
66 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
67 EVT_MOTION (wxLayoutWindow::OnMouseMove
)
68 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
69 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
70 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
73 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
74 : wxScrolledWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
75 wxHSCROLL
| wxVSCROLL
| wxBORDER
)
79 m_doSendEvents
= false;
80 m_ViewStartX
= 0; m_ViewStartY
= 0;
82 m_PopupMenu
= MakeFormatMenu();
83 m_memDC
= new wxMemoryDC
;
84 m_bitmap
= new wxBitmap(4,4);
85 m_bitmapSize
= wxPoint(4,4);
86 m_llist
= new wxLayoutList();
88 m_ScrollToCursor
= false;
90 wxPoint max
= m_llist
->GetSize();
91 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
92 EnableScrolling(true,true);
93 m_maxx
= max
.x
; m_maxy
= max
.y
;
95 SetCursor(wxCURSOR_IBEAM
);
99 wxLayoutWindow::~wxLayoutWindow()
101 delete m_memDC
; // deletes bitmap automatically (?)
105 SetBackgroundBitmap(NULL
);
110 wxLayoutWindow::MSWGetDlgCode()
112 // if we don't return this, we won't get OnChar() events for TABs and ENTER
113 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
118 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
120 wxPaintDC
dc( this );
126 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
127 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
129 findPos
.x
-= WXLO_XOFFSET
;
130 findPos
.y
-= WXLO_YOFFSET
;
132 if(findPos
.x
< 0) findPos
.x
= 0;
133 if(findPos
.y
< 0) findPos
.y
= 0;
135 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
139 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
142 //has the mouse only been moved?
143 if(eventId
== WXLOWIN_MENU_MOUSEMOVE
)
145 // found is only true if we are really over an object, not just
147 if(found
&& obj
&& obj
->GetUserData() != NULL
)
150 SetCursor(wxCURSOR_HAND
);
156 SetCursor(wxCURSOR_IBEAM
);
157 m_HandCursor
= FALSE
;
162 // always move cursor to mouse click:
163 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
165 m_llist
->MoveCursorTo(cursorPos
);
169 if(!m_doSendEvents
) // nothing to do
172 // only do the menu if activated, editable and not on a clickable object
173 if(eventId
== WXLOWIN_MENU_RCLICK
175 && (! obj
|| (obj
&& obj
->GetUserData() == NULL
))
178 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
181 // find the object at this position
184 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
185 commandEvent
.SetEventObject( this );
186 commandEvent
.SetClientData((char *)obj
);
187 GetEventHandler()->ProcessEvent(commandEvent
);
192 * Some simple keyboard handling.
195 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
197 #ifdef WXLAYOUT_DEBUG
198 if(event
.KeyCode() == WXK_F1
)
205 long keyCode
= event
.KeyCode();
206 if(m_Selecting
&& ! event
.ShiftDown())
208 m_llist
->EndSelection();
212 if(! m_Selecting
&& event
.ShiftDown())
225 m_llist
->StartSelection();
232 /* These two nested switches work like this:
233 The first one processes all non-editing keycodes, to move the
234 cursor, etc. It's default will process all keycodes causing
235 modifications to the buffer, but only if editing is allowed.
240 m_llist
->MoveCursorHorizontally(1);
243 m_llist
->MoveCursorHorizontally(-1);
246 m_llist
->MoveCursorVertically(-1);
249 m_llist
->MoveCursorVertically(1);
252 m_llist
->MoveCursorVertically(-20);
255 m_llist
->MoveCursorVertically(20);
258 m_llist
->MoveCursorToBeginOfLine();
261 m_llist
->MoveCursorToEndOfLine();
264 if(event
.ControlDown())
270 /* First, handle control keys */
271 if(event
.ControlDown() && ! event
.AltDown())
280 m_llist
->DeleteLines(1);
282 case 'h': // like backspace
283 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
286 m_llist
->DeleteToBeginOfLine();
289 m_llist
->DeleteToEndOfLine();
294 #ifdef WXLAYOUT_DEBUG
296 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
304 else if( event
.AltDown() && ! event
.ControlDown() )
310 m_llist
->DeleteWord();
317 else if ( ! event
.AltDown() && ! event
.ControlDown())
322 if(event
.ShiftDown())
328 case WXK_BACK
: // backspace
329 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
333 m_llist
->WrapLine(m_WrapMargin
);
334 m_llist
->LineBreak();
337 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
338 && (keyCode
< 256 && keyCode
>= 32)
343 if(m_WrapMargin
> 0 && isspace(keyCode
))
344 m_llist
->WrapLine(m_WrapMargin
);
345 m_llist
->Insert(tmp
);
355 wxRect r
= *m_llist
->GetUpdateRect();
360 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
362 if(event
.KeyCode() == WXK_SHIFT
&& m_llist
->IsSelecting())
363 m_llist
->EndSelection();
369 wxLayoutWindow::ScrollToCursor(void)
371 wxClientDC
dc( this );
374 int x0
,y0
,x1
,y1
, dx
, dy
;
376 // Calculate where the top of the visible area is:
378 GetScrollPixelsPerUnit(&dx
, &dy
);
381 // Get the size of the visible window:
382 GetClientSize(&x1
,&y1
);
385 // As we have the values anyway, use them to avoid unnecessary
386 // scrollbar updates.
387 if(x1
> m_maxx
) m_maxx
= x1
;
388 if(y1
> m_maxy
) m_maxy
= y1
;
389 /* Make sure that the scrollbars are at a position so that the
390 cursor is visible if we are editing. */
391 /** Scroll so that cursor is visible! */
392 WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor
));
393 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
394 if(cc
.x
< x0
|| cc
.y
< y0
395 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
398 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
399 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
400 Scroll(nx
/dx
,ny
/dy
); // new view start
402 m_ScrollToCursor
= false; // avoid recursion
407 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
409 wxRect region
= GetUpdateRegion().GetBox();
410 InternalPaint(& region
);
414 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
417 InternalPaint(updateRect
);
419 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
424 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
426 wxPaintDC
dc( this );
429 int x0
,y0
,x1
,y1
, dx
, dy
;
431 // Calculate where the top of the visible area is:
433 GetScrollPixelsPerUnit(&dx
, &dy
);
436 // Get the size of the visible window:
437 GetClientSize(&x1
,&y1
);
440 // As we have the values anyway, use them to avoid unnecessary
441 // scrollbar updates.
442 if(x1
> m_maxx
) m_maxx
= x1
;
443 if(y1
> m_maxy
) m_maxy
= y1
;
445 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
446 updateRect
->x
, updateRect
->y
,
447 updateRect
->x
+updateRect
->width
,
448 updateRect
->y
+updateRect
->height
));
453 /* Check whether the window has grown, if so, we need to reallocate
454 the bitmap to be larger. */
455 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
457 wxASSERT(m_bitmapSize
.x
> 0);
458 wxASSERT(m_bitmapSize
.y
> 0);
460 m_memDC
->SelectObject(wxNullBitmap
);
462 m_bitmapSize
= wxPoint(x1
,y1
);
463 m_bitmap
= new wxBitmap(x1
,y1
);
464 m_memDC
->SelectObject(*m_bitmap
);
467 m_memDC
->SetDeviceOrigin(0,0);
468 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(),wxSOLID
));
469 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),
471 m_memDC
->SetLogicalFunction(wxCOPY
);
473 /* Either fill the background with the background bitmap, or clear
479 w
= m_BGbitmap
->GetWidth(),
480 h
= m_BGbitmap
->GetHeight();
481 for(y
= 0; y
< y1
; y
+=h
)
482 for(x
= 0; x
< x1
; x
+=w
)
483 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
484 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
488 // clear the background: (must not be done if we use the update rectangle!)
489 m_memDC
->SetBackgroundMode(wxSOLID
);
490 m_memDC
->DrawRectangle(0,0,x1
, y1
);
494 /* This is the important bit: we tell the list to draw itself: */
495 WXLO_DEBUG(("Update rect: %ld,%ld / %ld,%ld",
496 updateRect
->x
, updateRect
->y
,
497 updateRect
->x
+updateRect
->width
,
498 updateRect
->y
+updateRect
->height
));
500 // Device origins on the memDC are suspect, we translate manually
501 // with the translate parameter of Draw().
502 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
503 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
505 // We start calculating a new update rect before drawing the
506 // cursor, so that the cursor coordinates get included in the next
507 // update rectangle (although they are drawn on the memDC, this is
508 // needed to erase it):
509 m_llist
->InvalidateUpdateRect();
510 m_llist
->DrawCursor(*m_memDC
,
511 m_HaveFocus
&& IsEditable(), // draw a thick
512 // cursor for editable windows with focus
515 // Now copy everything to the screen:
517 // This somehow doesn't work, but even the following bit with the
518 // whole rect at once is still a bit broken I think.
519 wxRegionIterator
ri ( GetUpdateRegion() );
523 WXLO_DEBUG(("UpdateRegion: %ld,%ld, %ld,%ld",
524 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH()));
525 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
526 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
532 // FIXME: Trying to copy only the changed parts, but it does not seem
534 // x0 = updateRect->x; y0 = updateRect->y;
535 // if(updateRect->height < y1)
536 // y1 = updateRect->height;
537 // y1 += WXLO_YOFFSET; //FIXME might not be needed
538 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
542 m_ScrollToCursor
= false;
545 // change the range and position of scrollbars
547 wxLayoutWindow::ResizeScrollbars(bool exact
)
549 wxPoint max
= m_llist
->GetSize();
551 if(max
.x
> m_maxx
|| max
.y
> m_maxy
552 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
557 // add an extra bit to the sizes to avoid future updates
558 max
.x
= max
.x
+WXLO_ROFFSET
;
559 max
.y
= max
.y
+WXLO_BOFFSET
;
561 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
562 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
563 m_maxx
= max
.x
; m_maxy
= max
.y
;
568 wxLayoutWindow::Paste(void)
572 if (wxTheClipboard
->Open())
574 wxTextDataObject data
;
575 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
577 wxTheClipboard
->GetData(&data
);
578 text
+= data
.GetText();
580 wxTheClipboard
->Close();
583 /* My attempt to get the primary selection, but it does not
585 if(text
.Length() == 0)
587 wxTextCtrl
tmp_tctrl(this,-1);
589 text
+= tmp_tctrl
.GetValue();
592 wxLayoutImportText( m_llist
, text
);
596 wxLayoutWindow::Copy(void)
598 wxLayoutList
*llist
= m_llist
->GetSelection();
603 wxLayoutExportObject
*export
;
604 wxLayoutExportStatus
status(llist
);
605 while((export
= wxLayoutExport( &status
, WXLO_EXPORT_AS_TEXT
)) != NULL
)
607 if(export
->type
== WXLO_EXPORT_TEXT
)
608 text
<< *(export
->content
.text
);
614 if (wxTheClipboard
->Open())
616 wxTextDataObject
*data
= new wxTextDataObject( text
);
617 bool rc
= wxTheClipboard
->SetData( data
);
618 wxTheClipboard
->Close();
625 wxLayoutWindow::MakeFormatMenu()
627 wxMenu
*m
= new wxMenu(_("Layout Menu"));
629 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
630 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
631 m
->AppendSeparator();
632 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
633 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
634 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
635 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
636 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
637 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
638 m
->AppendSeparator();
639 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
640 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
641 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
645 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
647 switch (event
.GetId())
649 case WXLOWIN_MENU_LARGER
:
650 m_llist
->SetFontLarger(); break;
651 case WXLOWIN_MENU_SMALLER
:
652 m_llist
->SetFontSmaller(); break;
653 case WXLOWIN_MENU_UNDERLINE_ON
:
654 m_llist
->SetFontUnderline(true); break;
655 case WXLOWIN_MENU_UNDERLINE_OFF
:
656 m_llist
->SetFontUnderline(false); break;
657 case WXLOWIN_MENU_BOLD_ON
:
658 m_llist
->SetFontWeight(wxBOLD
); break;
659 case WXLOWIN_MENU_BOLD_OFF
:
660 m_llist
->SetFontWeight(wxNORMAL
); break;
661 case WXLOWIN_MENU_ITALICS_ON
:
662 m_llist
->SetFontStyle(wxITALIC
); break;
663 case WXLOWIN_MENU_ITALICS_OFF
:
664 m_llist
->SetFontStyle(wxNORMAL
); break;
665 case WXLOWIN_MENU_ROMAN
:
666 m_llist
->SetFontFamily(wxROMAN
); break;
667 case WXLOWIN_MENU_TYPEWRITER
:
668 m_llist
->SetFontFamily(wxFIXED
); break;
669 case WXLOWIN_MENU_SANSSERIF
:
670 m_llist
->SetFontFamily(wxSWISS
); break;
675 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
678 //FIXME DoPaint(); // to repaint the cursor
682 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
685 //FIXME DoPaint(); // to repaint the cursor