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>
46 /// offsets to put a nice frame around text
47 #define WXLO_XOFFSET 4
48 #define WXLO_YOFFSET 4
50 /// offset to the right and bottom for when to redraw scrollbars
51 #define WXLO_ROFFSET 20
52 #define WXLO_BOFFSET 20
54 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
55 EVT_PAINT (wxLayoutWindow::OnPaint
)
56 EVT_CHAR (wxLayoutWindow::OnChar
)
57 EVT_KEY_UP (wxLayoutWindow::OnKeyUp
)
58 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
59 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
60 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
61 EVT_MOTION (wxLayoutWindow::OnMouseMove
)
62 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
63 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
64 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
67 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
68 : wxScrolledWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
69 wxHSCROLL
| wxVSCROLL
| wxBORDER
)
73 m_doSendEvents
= false;
74 m_ViewStartX
= 0; m_ViewStartY
= 0;
76 m_PopupMenu
= MakeFormatMenu();
77 m_memDC
= new wxMemoryDC
;
78 m_bitmap
= new wxBitmap(4,4);
79 m_bitmapSize
= wxPoint(4,4);
80 m_llist
= new wxLayoutList();
82 m_ScrollToCursor
= false;
84 wxPoint max
= m_llist
->GetSize();
85 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
86 EnableScrolling(true,true);
87 m_maxx
= max
.x
; m_maxy
= max
.y
;
89 SetCursor(wxCURSOR_IBEAM
);
93 wxLayoutWindow::~wxLayoutWindow()
95 delete m_memDC
; // deletes bitmap automatically (?)
99 SetBackgroundBitmap(NULL
);
104 wxLayoutWindow::MSWGetDlgCode()
106 // if we don't return this, we won't get OnChar() events for TABs and ENTER
107 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
112 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
114 wxPaintDC
dc( this );
120 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
121 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
123 findPos
.x
-= WXLO_XOFFSET
;
124 findPos
.y
-= WXLO_YOFFSET
;
126 if(findPos
.x
< 0) findPos
.x
= 0;
127 if(findPos
.y
< 0) findPos
.y
= 0;
129 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
130 #ifdef WXLAYOUT_DEBUG
131 // wxLogDebug("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
132 // event.GetX(), event.GetY(), findPos.x, findPos.y);
137 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
,
140 #ifdef WXLAYOUT_DEBUG
142 // wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
145 // wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
148 //has the mouse only been moved?
149 if(eventId
== WXLOWIN_MENU_MOUSEMOVE
)
151 // found is only true if we are really over an object, not just
153 if(found
&& obj
&& obj
->GetUserData() != NULL
)
156 SetCursor(wxCURSOR_HAND
);
162 SetCursor(wxCURSOR_IBEAM
);
163 m_HandCursor
= FALSE
;
168 // always move cursor to mouse click:
169 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
171 m_llist
->MoveCursorTo(cursorPos
);
172 m_ScrollToCursor
= true; //FIXME: needed? DoPaint(m_llist->GetUpdateRect());
174 if(!m_doSendEvents
) // nothing to do
177 // only do the menu if activated, editable and not on a clickable object
178 if(eventId
== WXLOWIN_MENU_RCLICK
180 && (! obj
|| (obj
&& obj
->GetUserData() == NULL
))
183 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
186 // find the object at this position
189 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
190 commandEvent
.SetEventObject( this );
191 commandEvent
.SetClientData((char *)obj
);
192 GetEventHandler()->ProcessEvent(commandEvent
);
197 * Some simple keyboard handling.
200 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
202 #ifdef WXLAYOUT_DEBUG
203 if(event
.KeyCode() == WXK_F1
)
210 long keyCode
= event
.KeyCode();
211 if(m_Selecting
&& ! event
.ShiftDown())
213 m_llist
->EndSelection();
217 if(! m_Selecting
&& event
.ShiftDown())
230 m_llist
->StartSelection();
237 if(!IsEditable()) // do nothing
242 m_llist
->MoveCursorVertically(-1);
245 m_llist
->MoveCursorVertically(1);
248 m_llist
->MoveCursorVertically(-20);
251 m_llist
->MoveCursorVertically(20);
259 /* First, handle control keys */
260 if(event
.ControlDown() && ! event
.AltDown())
269 m_llist
->DeleteLines(1);
271 case 'h': // like backspace
272 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
275 m_llist
->DeleteToBeginOfLine();
278 m_llist
->DeleteToEndOfLine();
280 #ifdef WXLAYOUT_DEBUG
282 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
290 else if( event
.AltDown() && ! event
.ControlDown() )
296 m_llist
->DeleteWord();
303 else if ( ! event
.AltDown() && ! event
.ControlDown())
308 m_llist
->MoveCursorHorizontally(1);
311 m_llist
->MoveCursorHorizontally(-1);
314 m_llist
->MoveCursorVertically(-1);
317 m_llist
->MoveCursorVertically(1);
320 m_llist
->MoveCursorVertically(-20);
323 m_llist
->MoveCursorVertically(20);
326 m_llist
->MoveCursorToBeginOfLine();
329 m_llist
->MoveCursorToEndOfLine();
334 case WXK_BACK
: // backspace
335 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
339 m_llist
->WrapLine(m_WrapMargin
);
340 m_llist
->LineBreak();
343 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
344 && (keyCode
< 256 && keyCode
>= 32)
349 if(m_WrapMargin
> 0 && isspace(keyCode
))
350 m_llist
->WrapLine(m_WrapMargin
);
351 m_llist
->Insert(tmp
);
358 m_ScrollToCursor
= true;
359 //DoPaint(true); // paint and scroll to cursor
360 wxRect r
= *m_llist
->GetUpdateRect();
365 wxLayoutWindow::OnKeyUp(wxKeyEvent
& event
)
367 if(event
.KeyCode() == WXK_SHIFT
&& m_llist
->IsSelecting())
368 m_llist
->EndSelection();
373 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
375 wxRect region
= GetUpdateRegion().GetBox();
376 InternalPaint(& region
);
380 wxLayoutWindow::DoPaint(const wxRect
*updateRect
)
383 InternalPaint(updateRect
);
385 Refresh(FALSE
, updateRect
); // Causes bad flicker under wxGTK!!!
390 wxLayoutWindow::InternalPaint(const wxRect
*updateRect
)
392 wxPaintDC
dc( this );
395 int x0
,y0
,x1
,y1
, dx
, dy
;
397 // Calculate where the top of the visible area is:
399 GetScrollPixelsPerUnit(&dx
, &dy
);
402 // Get the size of the visible window:
403 GetClientSize(&x1
,&y1
);
406 // As we have the values anyway, use them to avoid unnecessary
407 // scrollbar updates.
408 if(x1
> m_maxx
) m_maxx
= x1
;
409 if(y1
> m_maxy
) m_maxy
= y1
;
412 //m_llist->InvalidateUpdateRect();
413 //const wxRect *r = m_llist->GetUpdateRect();
414 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
415 updateRect
->x
, updateRect
->y
, updateRect
->x
+updateRect
->width
, updateRect
->y
+updateRect
->height
);
418 //FIXME: we should never need to call Layout at all because the
419 // list does it automatically.
420 // Maybe we need to change the scrollbar sizes or positions,
421 // so layout the list and check:
424 wxLogDebug("Update rect after calling Layout: %ld,%ld / %ld,%ld",
425 r
->x
, r
->y
, r
->x
+r
->width
, r
->y
+r
->height
);
426 // this is needed even when only the cursor moved
427 m_llist
->Layout(dc
,y0
+y1
);
428 wxLogDebug("Update rect after calling Layout again: %ld,%ld / %ld,%ld",
429 r
->x
, r
->y
, r
->x
+r
->width
, r
->y
+r
->height
);
435 /* Make sure that the scrollbars are at a position so that the
436 cursor is visible if we are editing. */
437 /** Scroll so that cursor is visible! */
438 wxLogDebug("m_ScrollToCursor = %d", (int) m_ScrollToCursor
);
439 if(IsEditable() && m_ScrollToCursor
)
441 wxPoint cc
= m_llist
->GetCursorScreenPos(*m_memDC
);
442 if(cc
.x
< x0
|| cc
.y
< y0
443 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
446 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
447 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
448 Scroll(nx
/dx
,ny
/dy
); // new view start
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 // Device origins on the memDC are suspect, we translate manually
468 // with the translate parameter of Draw().
469 m_memDC
->SetDeviceOrigin(0,0);
470 m_memDC
->SetBrush(wxBrush(m_llist
->GetDefaults()->GetBGColour(), wxSOLID
));
471 m_memDC
->SetPen(wxPen(m_llist
->GetDefaults()->GetBGColour(),0,wxTRANSPARENT
));
472 m_memDC
->SetLogicalFunction(wxCOPY
);
477 w
= m_BGbitmap
->GetWidth(),
478 h
= m_BGbitmap
->GetHeight();
479 for(y
= 0; y
< y1
; y
+=h
)
480 for(x
= 0; x
< x1
; x
+=w
)
481 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
482 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
486 m_memDC
->SetBackgroundMode(wxSOLID
);
487 m_memDC
->DrawRectangle(0,0,x1
, y1
);
491 /* This is the important bit: we tell the list to draw itself: */
492 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
493 updateRect
->x
, updateRect
->y
, updateRect
->x
+updateRect
->width
, updateRect
->y
+updateRect
->height
);
495 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
496 m_llist
->Draw(*m_memDC
,offset
, y0
, y0
+y1
);
498 // Now copy everything to the screen:
501 // 1. the update region as calculated by the list is wrong
502 // 2. we get wrong values here
503 // 3. how about the offset?
504 wxRegionIterator
ri ( GetUpdateRegion() );
508 wxLogDebug("UpdateRegion: %ld,%ld, %ld,%ld",
509 ri
.GetX(),ri
.GetY(),ri
.GetW(),ri
.GetH());
510 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
511 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
517 // FIXME: Trying to copy only the changed parts, but it does not seem
519 // x0 = updateRect->x; y0 = updateRect->y;
520 // if(updateRect->height < y1)
521 // y1 = updateRect->height;
522 // y1 += WXLO_YOFFSET; //FIXME might not be needed
523 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
525 //FIXME: we need to make sure we blit draw the cursor!
526 // How about drawing it directly to the screen?
528 //m_llist->DrawCursor(*m_memDC,m_HaveFocus,offset);
529 m_llist
->DrawCursor(dc
,m_HaveFocus
, wxPoint(WXLO_XOFFSET
,WXLO_YOFFSET
)); //direct to screen
532 m_ScrollToCursor
= false;
533 m_llist
->InvalidateUpdateRect();
536 // change the range and position of scroll bars
538 wxLayoutWindow::ResizeScrollbars(bool exact
)
540 wxPoint max
= m_llist
->GetSize();
542 if(max
.x
> m_maxx
|| max
.y
> m_maxy
543 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
548 // add an extra bit to the sizes to avoid future updates
549 max
.x
= max
.x
+WXLO_ROFFSET
;
550 max
.y
= max
.y
+WXLO_BOFFSET
;
552 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
553 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
554 m_maxx
= max
.x
; m_maxy
= max
.y
;
559 wxLayoutWindow::Paste(void)
563 if (wxTheClipboard
->Open())
565 wxTextDataObject data
;
566 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
568 wxTheClipboard
->GetData(&data
);
569 text
+= data
.GetText();
571 wxTheClipboard
->Close();
574 /* Unfortunately, this little hack doesn't work. So I'll go back to
576 if(text
.Length() == 0)
578 wxTextCtrl
tmp_tctrl(this,-1);
580 text
+= tmp_tctrl
.GetValue();
583 wxLayoutImportText( m_llist
, text
);
588 wxLayoutWindow::MakeFormatMenu()
590 wxMenu
*m
= new wxMenu(_("Layout Menu"));
592 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
593 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
594 m
->AppendSeparator();
595 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
596 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
597 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
598 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
599 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
600 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
601 m
->AppendSeparator();
602 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
603 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
604 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
608 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
610 switch (event
.GetId())
612 case WXLOWIN_MENU_LARGER
:
613 m_llist
->SetFontLarger(); break;
614 case WXLOWIN_MENU_SMALLER
:
615 m_llist
->SetFontSmaller(); break;
616 case WXLOWIN_MENU_UNDERLINE_ON
:
617 m_llist
->SetFontUnderline(true); break;
618 case WXLOWIN_MENU_UNDERLINE_OFF
:
619 m_llist
->SetFontUnderline(false); break;
620 case WXLOWIN_MENU_BOLD_ON
:
621 m_llist
->SetFontWeight(wxBOLD
); break;
622 case WXLOWIN_MENU_BOLD_OFF
:
623 m_llist
->SetFontWeight(wxNORMAL
); break;
624 case WXLOWIN_MENU_ITALICS_ON
:
625 m_llist
->SetFontStyle(wxITALIC
); break;
626 case WXLOWIN_MENU_ITALICS_OFF
:
627 m_llist
->SetFontStyle(wxNORMAL
); break;
628 case WXLOWIN_MENU_ROMAN
:
629 m_llist
->SetFontFamily(wxROMAN
); break;
630 case WXLOWIN_MENU_TYPEWRITER
:
631 m_llist
->SetFontFamily(wxFIXED
); break;
632 case WXLOWIN_MENU_SANSSERIF
:
633 m_llist
->SetFontFamily(wxSWISS
); break;
638 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
641 //FIXME DoPaint(); // to repaint the cursor
645 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
648 //FIXME DoPaint(); // to repaint the cursor