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"
21 # include "gui/wxlwindow.h"
29 # include "wxlwindow.h"
30 # include "wxlparser.h"
34 #include <wx/clipbrd.h>
36 #define WXLO_XOFFSET 4
37 #define WXLO_YOFFSET 4
39 BEGIN_EVENT_TABLE(wxLayoutWindow
,wxScrolledWindow
)
40 EVT_PAINT (wxLayoutWindow::OnPaint
)
41 EVT_CHAR (wxLayoutWindow::OnChar
)
42 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick
)
43 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick
)
44 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick
)
45 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST
, WXLOWIN_MENU_LAST
, wxLayoutWindow::OnMenu
)
46 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus
)
47 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus
)
50 wxLayoutWindow::wxLayoutWindow(wxWindow
*parent
)
51 : wxScrolledWindow(parent
, -1, wxDefaultPosition
, wxDefaultSize
,
52 wxHSCROLL
| wxVSCROLL
| wxBORDER
)
56 m_doSendEvents
= false;
57 m_ViewStartX
= 0; m_ViewStartY
= 0;
59 m_PopupMenu
= MakeFormatMenu();
60 m_memDC
= new wxMemoryDC
;
61 m_bitmap
= new wxBitmap(4,4);
62 m_bitmapSize
= wxPoint(4,4);
63 m_llist
= new wxLayoutList();
65 wxPoint max
= m_llist
->GetSize();
66 SetScrollbars(10, 20 /*lineHeight*/, max
.x
/10+1, max
.y
/20+1);
67 EnableScrolling(true,true);
68 m_maxx
= max
.x
; m_maxy
= max
.y
;
69 SetCursor(wxCURSOR_IBEAM
);
73 wxLayoutWindow::~wxLayoutWindow()
75 delete m_memDC
; // deletes bitmap automatically (?)
83 wxLayoutWindow::MSWGetDlgCode()
85 // if we don't return this, we won't get OnChar() events for TABs and ENTER
86 return DLGC_WANTCHARS
| DLGC_WANTARROWS
| DLGC_WANTMESSAGE
;
91 wxLayoutWindow::OnMouse(int eventId
, wxMouseEvent
& event
)
98 findPos
.x
= dc
.DeviceToLogicalX(event
.GetX());
99 findPos
.y
= dc
.DeviceToLogicalY(event
.GetY());
101 findPos
.x
-= WXLO_XOFFSET
;
102 findPos
.y
-= WXLO_YOFFSET
;
104 if(findPos
.x
< 0) findPos
.x
= 0;
105 if(findPos
.y
< 0) findPos
.y
= 0;
107 #ifdef WXLAYOUT_DEBUG
108 wxLogDebug("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
109 event
.GetX(), event
.GetY(), findPos
.x
, findPos
.y
);
112 m_ClickPosition
= findPos
;
114 wxLayoutObject
*obj
= m_llist
->FindObjectScreen(dc
, findPos
, &cursorPos
);
116 #ifdef WXLAYOUT_DEBUG
118 wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
121 wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
124 // always move cursor to mouse click:
125 if(obj
&& eventId
== WXLOWIN_MENU_LCLICK
)
127 m_llist
->MoveCursorTo(cursorPos
);
130 if(!m_doSendEvents
) // nothing to do
133 // only do the menu if activated, editable and not on a clickable object
134 if(eventId
== WXLOWIN_MENU_RCLICK
136 && (! obj
|| (obj
&& obj
->GetUserData() == NULL
))
139 PopupMenu(m_PopupMenu
, event
.GetX(), event
.GetY());
142 // find the object at this position
145 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, eventId
);
146 commandEvent
.SetEventObject( this );
147 commandEvent
.SetClientData((char *)obj
);
148 GetEventHandler()->ProcessEvent(commandEvent
);
153 * Some simple keyboard handling.
156 wxLayoutWindow::OnChar(wxKeyEvent
& event
)
158 if(!IsEditable()) // do nothing
164 long keyCode
= event
.KeyCode();
166 /* First, handle control keys */
167 if(event
.ControlDown() && ! event
.AltDown())
169 switch(event
.KeyCode())
176 m_llist
->DeleteLines(1);
178 case 'h': // like backspace
179 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
182 m_llist
->DeleteToBeginOfLine();
185 m_llist
->DeleteToEndOfLine();
187 #ifdef WXLAYOUT_DEBUG
189 m_llist
->SetFont(-1,-1,-1,-1,true); // underlined
197 else if( event
.AltDown() && ! event
.ControlDown() )
199 switch(event
.KeyCode())
203 m_llist
->DeleteWord();
210 else if ( ! event
.AltDown() && ! event
.ControlDown())
212 switch(event
.KeyCode())
215 m_llist
->MoveCursorHorizontally(1);
218 m_llist
->MoveCursorHorizontally(-1);
221 m_llist
->MoveCursorVertically(-1);
224 m_llist
->MoveCursorVertically(1);
227 m_llist
->MoveCursorVertically(-20);
230 m_llist
->MoveCursorVertically(20);
233 m_llist
->MoveCursorToBeginOfLine();
236 m_llist
->MoveCursorToEndOfLine();
241 case WXK_BACK
: // backspace
242 if(m_llist
->MoveCursorHorizontally(-1)) m_llist
->Delete(1);
246 m_llist
->WrapLine(m_WrapMargin
);
247 m_llist
->LineBreak();
250 if((!(event
.ControlDown() || event
.AltDown() || event
.MetaDown()))
251 && (keyCode
< 256 && keyCode
>= 32)
256 if(m_WrapMargin
> 0 && isspace(keyCode
))
257 m_llist
->WrapLine(m_WrapMargin
);
258 m_llist
->Insert(tmp
);
264 DoPaint(true); // paint and scroll to cursor
268 wxLayoutWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
)) // or: OnDraw(wxDC& dc)
270 m_ScrollToCursor
= false;
275 wxLayoutWindow::DoPaint(bool scrollToCursor
)
277 m_ScrollToCursor
= scrollToCursor
;
286 wxLayoutWindow::InternalPaint(void)
288 wxPaintDC
dc( this );
291 int x0
,y0
,x1
,y1
, dx
, dy
;
293 // Calculate where the top of the visible area is:
295 GetScrollPixelsPerUnit(&dx
, &dy
);
298 // Get the size of the visible window:
299 GetClientSize(&x1
,&y1
);
304 // Maybe we need to change the scrollbar sizes or positions,
305 // so layout the list and check:
308 // this is needed even when only the cursor moved
309 m_llist
->Layout(dc
,y0
+y1
);
314 /* Make sure that the scrollbars are at a position so that the
315 cursor is visible if we are editing. */
316 /** Scroll so that cursor is visible! */
317 if(IsEditable() && m_ScrollToCursor
)
319 wxPoint cc
= m_llist
->GetCursorScreenPos();
320 if(cc
.x
< x0
|| cc
.y
< y0
321 || cc
.x
>= x0
+(9*x1
)/10 || cc
.y
>= y0
+(9*y1
/10)) // (9*x)/10 == 90%
324 nx
= cc
.x
- x1
/2; if(nx
< 0) nx
= 0;
325 ny
= cc
.y
- y1
/2; if(ny
< 0) ny
= 0;
326 Scroll(nx
/dx
,ny
/dy
); // new view start
331 /* Check whether the window has grown, if so, we need to reallocate
332 the bitmap to be larger. */
333 if(x1
> m_bitmapSize
.x
|| y1
> m_bitmapSize
.y
)
335 wxASSERT(m_bitmapSize
.x
> 0);
336 wxASSERT(m_bitmapSize
.y
> 0);
338 m_memDC
->SelectObject(wxNullBitmap
);
340 m_bitmapSize
= wxPoint(x1
,y1
);
341 m_bitmap
= new wxBitmap(x1
,y1
);
342 m_memDC
->SelectObject(*m_bitmap
);
344 // Device origins on the memDC are suspect, we translate manually
345 // with the translate parameter of Draw().
346 m_memDC
->SetDeviceOrigin(0,0);
349 // The offsets give the window a tiny border on the left and top, looks nice.
350 wxPoint
offset(-x0
+WXLO_XOFFSET
,-y0
+WXLO_YOFFSET
);
351 m_llist
->Draw(*m_memDC
,offset
);
353 m_llist
->DrawCursor(*m_memDC
,m_HaveFocus
,offset
);
354 // Now copy everything to the screen:
355 dc
.Blit(x0
,y0
,x1
,y1
,m_memDC
,0,0,wxCOPY
,FALSE
);
361 // change the range and position of scroll bars
363 wxLayoutWindow::ResizeScrollbars(bool exact
)
365 wxPoint max
= m_llist
->GetSize();
367 if(max
.x
> m_maxx
|| max
.y
> m_maxy
368 || max
.x
< (7*m_maxx
)/10 || max
.y
<< (7*m_maxy
)/10
371 if(! exact
) // add an extra 20% to the sizes to avoid future updates
373 max
.x
= (12*max
.x
)/10; // 12/20 = 120%
374 max
.y
= (12*max
.y
)/10;
376 ViewStart(&m_ViewStartX
, &m_ViewStartY
);
377 SetScrollbars(10, 20, max
.x
/10+1,max
.y
/20+1,m_ViewStartX
,m_ViewStartY
,true);
378 m_maxx
= max
.x
; m_maxy
= max
.y
;
383 wxLayoutWindow::Paste(void)
386 if (wxTheClipboard
->Open())
388 wxTextDataObject data
;
389 if (wxTheClipboard
->IsSupported(wxDF_TEXT
))
391 wxTheClipboard
->GetData(&data
);
392 wxLayoutImportText( m_llist
, data
.GetText());
394 wxTheClipboard
->Close();
400 wxLayoutWindow::MakeFormatMenu()
402 wxMenu
*m
= new wxMenu(_("Layout Menu"));
404 m
->Append(WXLOWIN_MENU_LARGER
,_("&Larger"),_("Switch to larger font."), false);
405 m
->Append(WXLOWIN_MENU_SMALLER
,_("&Smaller"),_("Switch to smaller font."), false);
406 m
->AppendSeparator();
407 m
->Append(WXLOWIN_MENU_UNDERLINE_ON
, _("&Underline on"),_("Activate underline mode."), false);
408 m
->Append(WXLOWIN_MENU_UNDERLINE_OFF
,_("&Underline off"),_("Deactivate underline mode."), false);
409 m
->Append(WXLOWIN_MENU_BOLD_ON
,_("&Bold on"),_("Activate bold mode."), false);
410 m
->Append(WXLOWIN_MENU_BOLD_OFF
,_("&Bold off"),_("Deactivate bold mode."), false);
411 m
->Append(WXLOWIN_MENU_ITALICS_ON
,_("&Italics on"),_("Activate italics mode."), false);
412 m
->Append(WXLOWIN_MENU_ITALICS_OFF
,_("&Italics off"),_("Deactivate italics mode."), false);
413 m
->AppendSeparator();
414 m
->Append(WXLOWIN_MENU_ROMAN
,_("&Roman"),_("Switch to roman font."), false);
415 m
->Append(WXLOWIN_MENU_TYPEWRITER
,_("&Typewriter"),_("Switch to typewriter font."), false);
416 m
->Append(WXLOWIN_MENU_SANSSERIF
,_("&Sans Serif"),_("Switch to sans serif font."), false);
420 void wxLayoutWindow::OnMenu(wxCommandEvent
& event
)
422 switch (event
.GetId())
424 case WXLOWIN_MENU_LARGER
:
425 m_llist
->SetFontLarger(); break;
426 case WXLOWIN_MENU_SMALLER
:
427 m_llist
->SetFontSmaller(); break;
428 case WXLOWIN_MENU_UNDERLINE_ON
:
429 m_llist
->SetFontUnderline(true); break;
430 case WXLOWIN_MENU_UNDERLINE_OFF
:
431 m_llist
->SetFontUnderline(false); break;
432 case WXLOWIN_MENU_BOLD_ON
:
433 m_llist
->SetFontWeight(wxBOLD
); break;
434 case WXLOWIN_MENU_BOLD_OFF
:
435 m_llist
->SetFontWeight(wxNORMAL
); break;
436 case WXLOWIN_MENU_ITALICS_ON
:
437 m_llist
->SetFontStyle(wxITALIC
); break;
438 case WXLOWIN_MENU_ITALICS_OFF
:
439 m_llist
->SetFontStyle(wxNORMAL
); break;
440 case WXLOWIN_MENU_ROMAN
:
441 m_llist
->SetFontFamily(wxROMAN
); break;
442 case WXLOWIN_MENU_TYPEWRITER
:
443 m_llist
->SetFontFamily(wxFIXED
); break;
444 case WXLOWIN_MENU_SANSSERIF
:
445 m_llist
->SetFontFamily(wxSWISS
); break;
450 wxLayoutWindow::OnSetFocus(wxFocusEvent
&ev
)
453 DoPaint(); // to repaint the cursor
457 wxLayoutWindow::OnKillFocus(wxFocusEvent
&ev
)
460 DoPaint(); // to repaint the cursor