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"
18 # include "gui/wxMenuDefs.h"
19 # include "gui/wxMApp.h"
22 # include "gui/wxlwindow.h"
23 # include "gui/wxlparser.h"
32 # include "wxlwindow.h"
33 # include "wxlparser.h"
36 #include <wx/clipbrd.h>
37 #include <wx/dataobj.h>
41 /// offsets to put a nice frame around text
42 #define WXLO_XOFFSET 4
43 #define WXLO_YOFFSET 4
45 /// offset to the right and bottom for when to redraw scrollbars
46 #define WXLO_ROFFSET 20
47 #define WXLO_BOFFSET 20
49 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
50 EVT_PAINT (wxLayoutWindow::OnPaint
)
51 EVT_CHAR (wxLayoutWindow::OnChar
)
52 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
53 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
54 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
55 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
56 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
57 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
60 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
61 : wxScrolledWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
62 wxHSCROLL
| wxVSCROLL
| wxBORDER
)
66 m_doSendEvents
= false;
67 m_ViewStartX
= 0; m_ViewStartY
= 0;
69 m_PopupMenu
= MakeFormatMenu();
70 m_memDC
= new wxMemoryDC
;
71 m_bitmap
= new wxBitmap(4,4);
72 m_bitmapSize
= wxPoint(4,4);
73 m_llist
= new wxLayoutList();
76 wxPoint max
= m_llist
->GetSize();
77 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
78 EnableScrolling(true,true);
79 m_maxx
= max
.x
; m_maxy
= max
.y
;
80 SetCursor(wxCURSOR_IBEAM
);
84 wxLayoutWindow::~wxLayoutWindow()
86 delete m_memDC
; // deletes bitmap automatically (?)
90 SetBackgroundBitmap(NULL
);
95 wxLayoutWindow::MSWGetDlgCode()
97 // if we don't return this, we won't get OnChar() events for TABs and ENTER
98 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
103 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
105 wxPaintDC
dc( this );
111 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
112 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
114 findPos
.x
-= WXLO_XOFFSET
;
115 findPos
.y
-= WXLO_YOFFSET
;
117 if(findPos
.x
< 0) findPos
.x
= 0;
118 if(findPos
.y
< 0) findPos
.y
= 0;
120 m_ClickPosition
= wxPoint(event
.GetX(), event
.GetY());
121 #ifdef WXLAYOUT_DEBUG
122 wxLogDebug("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
123 event
.GetX(), event
.GetY(), findPos
.x
, findPos
.y
);
127 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
, &cursorPos
);
129 #ifdef WXLAYOUT_DEBUG
131 wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
134 wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
137 // always move cursor to mouse click:
138 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
140 m_llist
->MoveCursorTo(cursorPos
);
143 if(!m_doSendEvents
) // nothing to do
146 // only do the menu if activated, editable and not on a clickable object
147 if(eventId
== WXLOWIN_MENU_RCLICK
149 && (! obj
|| (obj
&& obj
->GetUserData() == NULL
))
152 PopupMenu(m_PopupMenu
, m_ClickPosition
.x
, m_ClickPosition
.y
);
155 // find the object at this position
158 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
159 commandEvent
.SetEventObject( this );
160 commandEvent
.SetClientData((char *)obj
);
161 GetEventHandler()->ProcessEvent(commandEvent
);
166 * Some simple keyboard handling.
169 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
171 if(!IsEditable()) // do nothing
177 long keyCode
= event
.KeyCode();
179 /* First, handle control keys */
180 if(event
.ControlDown() && ! event
.AltDown())
182 switch(event
.KeyCode())
189 m_llist
->DeleteLines(1);
191 case 'h': // like backspace
192 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
195 m_llist
->DeleteToBeginOfLine();
198 m_llist
->DeleteToEndOfLine();
200 #ifdef WXLAYOUT_DEBUG
202 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
210 else if( event
.AltDown() && ! event
.ControlDown() )
212 switch(event
.KeyCode())
216 m_llist
->DeleteWord();
223 else if ( ! event
.AltDown() && ! event
.ControlDown())
225 switch(event
.KeyCode())
228 m_llist
->MoveCursorHorizontally(1);
231 m_llist
->MoveCursorHorizontally(-1);
234 m_llist
->MoveCursorVertically(-1);
237 m_llist
->MoveCursorVertically(1);
240 m_llist
->MoveCursorVertically(-20);
243 m_llist
->MoveCursorVertically(20);
246 m_llist
->MoveCursorToBeginOfLine();
249 m_llist
->MoveCursorToEndOfLine();
254 case WXK_BACK
: // backspace
255 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
259 m_llist
->WrapLine(m_WrapMargin
);
260 m_llist
->LineBreak();
263 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
264 && (keyCode
< 256 && keyCode
>= 32)
269 if(m_WrapMargin
> 0 && isspace(keyCode
))
270 m_llist
->WrapLine(m_WrapMargin
);
271 m_llist
->Insert(tmp
);
278 DoPaint(true); // paint and scroll to cursor
282 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
284 m_ScrollToCursor
= false;
289 wxLayoutWindow::DoPaint(bool scrollToCursor
)
291 m_ScrollToCursor
= scrollToCursor
;
295 Refresh(FALSE
); // Causes bad flicker under wxGTK!!!
300 wxLayoutWindow::InternalPaint(void)
302 wxPaintDC
dc( this );
305 int x0
,y0
,x1
,y1
, dx
, dy
;
307 // Calculate where the top of the visible area is:
309 GetScrollPixelsPerUnit(&dx
, &dy
);
312 // Get the size of the visible window:
313 GetClientSize(&x1
,&y1
);
316 // As we have the values anyway, use them to avoid unnecessary
317 // scrollbar updates.
318 if(x1
> m_maxx
) m_maxx
= x1
;
319 if(y1
> m_maxy
) m_maxy
= y1
;
321 // Maybe we need to change the scrollbar sizes or positions,
322 // so layout the list and check:
325 // this is needed even when only the cursor moved
326 m_llist
->Layout(dc
,y0
+y1
);
331 /* Make sure that the scrollbars are at a position so that the
332 cursor is visible if we are editing. */
333 /** Scroll so that cursor is visible! */
334 if(IsEditable() && m_ScrollToCursor
)
336 wxPoint cc
= m_llist
->GetCursorScreenPos();
337 if(cc
.x
< x0
|| cc
.y
< y0
338 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
341 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
342 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
343 Scroll(nx
/dx
,ny
/dy
); // new view start
348 /* Check whether the window has grown, if so, we need to reallocate
349 the bitmap to be larger. */
350 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
352 wxASSERT(m_bitmapSize
.x
> 0);
353 wxASSERT(m_bitmapSize
.y
> 0);
355 m_memDC
->SelectObject(wxNullBitmap
);
357 m_bitmapSize
= wxPoint(x1
,y1
);
358 m_bitmap
= new wxBitmap(x1
,y1
);
359 m_memDC
->SelectObject(*m_bitmap
);
361 // Device origins on the memDC are suspect, we translate manually
362 // with the translate parameter of Draw().
363 m_memDC
->SetDeviceOrigin(0,0);
364 m_memDC
->SetBackgroundMode(wxTRANSPARENT
);
365 m_memDC
->SetBrush(wxBrush(*m_llist
->GetDefaults()->GetBGColour(), wxSOLID
));
366 m_memDC
->SetPen(wxPen(*m_llist
->GetDefaults()->GetBGColour(),0,wxTRANSPARENT
));
367 m_memDC
->SetLogicalFunction(wxCOPY
);
372 w
= m_BGbitmap
->GetWidth(),
373 h
= m_BGbitmap
->GetHeight();
374 for(y
= 0; y
< y1
; y
+=h
)
375 for(x
= 0; x
< x1
; x
+=w
)
376 m_memDC
->DrawBitmap(*m_BGbitmap
, x
, y
);
379 m_memDC
->DrawRectangle(0,0,x1
, y1
);
381 // The offsets give the window a tiny border on the left and top, looks nice.
382 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
383 m_llist
->Draw(*m_memDC
,offset
);
385 m_llist
->DrawCursor(*m_memDC
,m_HaveFocus
,offset
);
387 // Now copy everything to the screen:
388 wxRegionIterator
ri ( GetUpdateRegion() );
392 dc
.Blit(x0
+ri
.GetX(),y0
+ri
.GetY(),ri
.GetW(),ri
.GetH(),
393 m_memDC
,ri
.GetX(),ri
.GetY(),wxCOPY
,FALSE
);
397 // If there are no update rectangles, we got called to reflect
398 // a change in the list. Currently there is no mechanism to
399 // easily find out which bits need updating, so we update
400 // all. The wxLayoutList could handle this, creating a list or
401 // at least one large rectangle of changes. FIXME
402 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
407 // change the range and position of scroll bars
409 wxLayoutWindow::ResizeScrollbars(bool exact
)
411 wxPoint max
= m_llist
->GetSize();
413 if(max
.x
> m_maxx
|| max
.y
> m_maxy
414 || max
.x
> m_maxx
-WXLO_ROFFSET
|| max
.y
> m_maxy
-WXLO_BOFFSET
419 // add an extra bit to the sizes to avoid future updates
420 max
.x
= max
.x
+WXLO_ROFFSET
;
421 max
.y
= max
.y
+WXLO_BOFFSET
;
423 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
424 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
425 m_maxx
= max
.x
; m_maxy
= max
.y
;
430 wxLayoutWindow::Paste(void)
433 if (wxTheClipboard
->Open())
435 wxTextDataObject data
;
436 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
438 wxTheClipboard
->GetData(&data
);
439 wxLayoutImportText( m_llist
, data
.GetText());
441 wxTheClipboard
->Close();
447 wxLayoutWindow::MakeFormatMenu()
449 wxMenu
*m
= new wxMenu(_("Layout Menu"));
451 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
452 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
453 m
->AppendSeparator();
454 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
455 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
456 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
457 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
458 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
459 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
460 m
->AppendSeparator();
461 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
462 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
463 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
467 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
469 switch (event
.GetId())
471 case WXLOWIN_MENU_LARGER
:
472 m_llist
->SetFontLarger(); break;
473 case WXLOWIN_MENU_SMALLER
:
474 m_llist
->SetFontSmaller(); break;
475 case WXLOWIN_MENU_UNDERLINE_ON
:
476 m_llist
->SetFontUnderline(true); break;
477 case WXLOWIN_MENU_UNDERLINE_OFF
:
478 m_llist
->SetFontUnderline(false); break;
479 case WXLOWIN_MENU_BOLD_ON
:
480 m_llist
->SetFontWeight(wxBOLD
); break;
481 case WXLOWIN_MENU_BOLD_OFF
:
482 m_llist
->SetFontWeight(wxNORMAL
); break;
483 case WXLOWIN_MENU_ITALICS_ON
:
484 m_llist
->SetFontStyle(wxITALIC
); break;
485 case WXLOWIN_MENU_ITALICS_OFF
:
486 m_llist
->SetFontStyle(wxNORMAL
); break;
487 case WXLOWIN_MENU_ROMAN
:
488 m_llist
->SetFontFamily(wxROMAN
); break;
489 case WXLOWIN_MENU_TYPEWRITER
:
490 m_llist
->SetFontFamily(wxFIXED
); break;
491 case WXLOWIN_MENU_SANSSERIF
:
492 m_llist
->SetFontFamily(wxSWISS
); break;
497 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
500 DoPaint(); // to repaint the cursor
504 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
507 DoPaint(); // to repaint the cursor