]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
Suppressing selection highlighting in M.
[wxWidgets.git] / user / wxLayout / wxlwindow.cpp
1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
3 * *
4 * (C) 1998, 1999 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8
9 #ifdef __GNUG__
10 # pragma implementation "wxlwindow.h"
11 #endif
12
13 #include "wx/wxprec.h"
14 #ifdef __BORLANDC__
15 # pragma hdrstop
16 #endif
17
18
19 #include "Mpch.h"
20 #ifdef M_BASEDIR
21 # ifndef USE_PCH
22 # include "Mcommon.h"
23 # include "gui/wxMenuDefs.h"
24 # include "gui/wxMApp.h"
25 # endif // USE_PCH
26 # include "gui/wxlwindow.h"
27 # include "gui/wxlparser.h"
28 #else
29 # ifdef __WXMSW__
30 # include <windows.h>
31 # undef FindWindow
32 # undef GetCharWidth
33 # undef StartDoc
34 # endif
35
36 # include "wxlwindow.h"
37 # include "wxlparser.h"
38 #endif
39
40 #include <wx/clipbrd.h>
41 #include <wx/textctrl.h>
42 #include <wx/dataobj.h>
43
44 #include <ctype.h>
45
46 /// offsets to put a nice frame around text
47 #define WXLO_XOFFSET 4
48 #define WXLO_YOFFSET 4
49
50 /// offset to the right and bottom for when to redraw scrollbars
51 #define WXLO_ROFFSET 20
52 #define WXLO_BOFFSET 20
53
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)
65 END_EVENT_TABLE()
66
67 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
68 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
69 wxHSCROLL | wxVSCROLL | wxBORDER)
70
71 {
72 m_Editable = false;
73 m_doSendEvents = false;
74 m_ViewStartX = 0; m_ViewStartY = 0;
75 m_DoPopupMenu = true;
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();
81 m_BGbitmap = NULL;
82 m_ScrollToCursor = false;
83 SetWrapMargin(0);
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;
88 m_Selecting = false;
89 SetCursor(wxCURSOR_IBEAM);
90 SetDirty();
91 }
92
93 wxLayoutWindow::~wxLayoutWindow()
94 {
95 delete m_memDC; // deletes bitmap automatically (?)
96 delete m_bitmap;
97 delete m_llist;
98 delete m_PopupMenu;
99 SetBackgroundBitmap(NULL);
100 }
101
102 #ifdef __WXMSW__
103 long
104 wxLayoutWindow::MSWGetDlgCode()
105 {
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;
108 }
109 #endif //MSW
110
111 void
112 wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
113 {
114 wxPaintDC dc( this );
115 PrepareDC( dc );
116 SetFocus();
117
118
119 wxPoint findPos;
120 findPos.x = dc.DeviceToLogicalX(event.GetX());
121 findPos.y = dc.DeviceToLogicalY(event.GetY());
122
123 findPos.x -= WXLO_XOFFSET;
124 findPos.y -= WXLO_YOFFSET;
125
126 if(findPos.x < 0) findPos.x = 0;
127 if(findPos.y < 0) findPos.y = 0;
128
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);
133 #endif
134
135 wxPoint cursorPos;
136 bool found;
137 wxLayoutObject *obj = m_llist->FindObjectScreen(dc, findPos,
138 &cursorPos, &found);
139
140 #ifdef WXLAYOUT_DEBUG
141 // if(obj)
142 // wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
143 // obj->GetType());
144 // else
145 // wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
146 #endif
147
148 //has the mouse only been moved?
149 if(eventId == WXLOWIN_MENU_MOUSEMOVE)
150 {
151 // found is only true if we are really over an object, not just
152 // behind it
153 if(found && obj && obj->GetUserData() != NULL)
154 {
155 if(!m_HandCursor)
156 SetCursor(wxCURSOR_HAND);
157 m_HandCursor = TRUE;
158 }
159 else
160 {
161 if(m_HandCursor)
162 SetCursor(wxCURSOR_IBEAM);
163 m_HandCursor = FALSE;
164 }
165 return;
166 }
167
168 // always move cursor to mouse click:
169 if(obj && eventId == WXLOWIN_MENU_LCLICK)
170 {
171 m_llist->MoveCursorTo(cursorPos);
172 m_ScrollToCursor = true;
173 Refresh();
174 }
175 if(!m_doSendEvents) // nothing to do
176 return;
177
178 // only do the menu if activated, editable and not on a clickable object
179 if(eventId == WXLOWIN_MENU_RCLICK
180 && IsEditable()
181 && (! obj || (obj && obj->GetUserData() == NULL))
182 )
183 {
184 PopupMenu(m_PopupMenu, m_ClickPosition.x, m_ClickPosition.y);
185 return;
186 }
187 // find the object at this position
188 if(obj)
189 {
190 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
191 commandEvent.SetEventObject( this );
192 commandEvent.SetClientData((char *)obj);
193 GetEventHandler()->ProcessEvent(commandEvent);
194 }
195 }
196
197 /*
198 * Some simple keyboard handling.
199 */
200 void
201 wxLayoutWindow::OnChar(wxKeyEvent& event)
202 {
203 #ifdef WXLAYOUT_DEBUG
204 if(event.KeyCode() == WXK_F1)
205 {
206 m_llist->Debug();
207 return;
208 }
209 #endif
210
211 long keyCode = event.KeyCode();
212 if(m_Selecting && ! event.ShiftDown())
213 {
214 m_llist->EndSelection();
215 m_Selecting = false;
216 }
217 else
218 if(! m_Selecting && event.ShiftDown())
219 {
220 switch(keyCode)
221 {
222 case WXK_UP:
223 case WXK_DOWN:
224 case WXK_RIGHT:
225 case WXK_LEFT:
226 case WXK_PRIOR:
227 case WXK_NEXT:
228 case WXK_HOME:
229 case WXK_END:
230 m_Selecting = true;
231 m_llist->StartSelection();
232 break;
233 default:
234 ;
235 }
236 }
237
238 if(!IsEditable()) // do nothing
239 {
240 switch(keyCode)
241 {
242 case WXK_UP:
243 m_llist->MoveCursorVertically(-1);
244 break;
245 case WXK_DOWN:
246 m_llist->MoveCursorVertically(1);
247 break;
248 case WXK_PRIOR:
249 m_llist->MoveCursorVertically(-20);
250 break;
251 case WXK_NEXT:
252 m_llist->MoveCursorVertically(20);
253 break;
254 default:
255 ;
256 }
257 return;
258 }
259
260 /* First, handle control keys */
261 if(event.ControlDown() && ! event.AltDown())
262 {
263 switch(keyCode)
264 {
265 case WXK_DELETE :
266 case 'd':
267 m_llist->Delete(1);
268 break;
269 case 'y':
270 m_llist->DeleteLines(1);
271 break;
272 case 'h': // like backspace
273 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
274 break;
275 case 'u':
276 m_llist->DeleteToBeginOfLine();
277 break;
278 case 'k':
279 m_llist->DeleteToEndOfLine();
280 break;
281 case 'v':
282 Paste();
283 break;
284 #ifdef WXLAYOUT_DEBUG
285 case WXK_F1:
286 m_llist->SetFont(-1,-1,-1,-1,true); // underlined
287 break;
288 #endif
289 default:
290 ;
291 }
292 }
293 // ALT only:
294 else if( event.AltDown() && ! event.ControlDown() )
295 {
296 switch(keyCode)
297 {
298 case WXK_DELETE:
299 case 'd':
300 m_llist->DeleteWord();
301 break;
302 default:
303 ;
304 }
305 }
306 // no control keys:
307 else if ( ! event.AltDown() && ! event.ControlDown())
308 {
309 switch(keyCode)
310 {
311 case WXK_RIGHT:
312 m_llist->MoveCursorHorizontally(1);
313 break;
314 case WXK_LEFT:
315 m_llist->MoveCursorHorizontally(-1);
316 break;
317 case WXK_UP:
318 m_llist->MoveCursorVertically(-1);
319 break;
320 case WXK_DOWN:
321 m_llist->MoveCursorVertically(1);
322 break;
323 case WXK_PRIOR:
324 m_llist->MoveCursorVertically(-20);
325 break;
326 case WXK_NEXT:
327 m_llist->MoveCursorVertically(20);
328 break;
329 case WXK_HOME:
330 m_llist->MoveCursorToBeginOfLine();
331 break;
332 case WXK_END:
333 m_llist->MoveCursorToEndOfLine();
334 break;
335 case WXK_INSERT:
336 if(event.ShiftDown())
337 Paste();
338 break;
339 case WXK_DELETE :
340 m_llist->Delete(1);
341 break;
342 case WXK_BACK: // backspace
343 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
344 break;
345 case WXK_RETURN:
346 if(m_WrapMargin > 0)
347 m_llist->WrapLine(m_WrapMargin);
348 m_llist->LineBreak();
349 break;
350 default:
351 if((!(event.ControlDown() || event.AltDown() || event.MetaDown()))
352 && (keyCode < 256 && keyCode >= 32)
353 )
354 {
355 wxString tmp;
356 tmp += keyCode;
357 if(m_WrapMargin > 0 && isspace(keyCode))
358 m_llist->WrapLine(m_WrapMargin);
359 m_llist->Insert(tmp);
360 }
361 break;
362 }
363 }
364 SetDirty();
365 SetModified();
366 m_ScrollToCursor = true;
367 //DoPaint(true); // paint and scroll to cursor
368 wxRect r = *m_llist->GetUpdateRect();
369 Refresh( FALSE, &r);
370 }
371
372 void
373 wxLayoutWindow::OnKeyUp(wxKeyEvent& event)
374 {
375 if(event.KeyCode() == WXK_SHIFT && m_llist->IsSelecting())
376 m_llist->EndSelection();
377 event.Skip();
378 }
379
380 void
381 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)) // or: OnDraw(wxDC& dc)
382 {
383 wxRect region = GetUpdateRegion().GetBox();
384 InternalPaint(& region);
385 }
386
387 void
388 wxLayoutWindow::DoPaint(const wxRect *updateRect)
389 {
390 #ifdef __WXGTK__
391 InternalPaint(updateRect);
392 #else
393 Refresh(FALSE, updateRect); // Causes bad flicker under wxGTK!!!
394 #endif
395 }
396
397 void
398 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
399 {
400 wxPaintDC dc( this );
401 PrepareDC( dc );
402
403 int x0,y0,x1,y1, dx, dy;
404
405 // Calculate where the top of the visible area is:
406 ViewStart(&x0,&y0);
407 GetScrollPixelsPerUnit(&dx, &dy);
408 x0 *= dx; y0 *= dy;
409
410 // Get the size of the visible window:
411 GetClientSize(&x1,&y1);
412 wxASSERT(x1 > 0);
413 wxASSERT(y1 > 0);
414 // As we have the values anyway, use them to avoid unnecessary
415 // scrollbar updates.
416 if(x1 > m_maxx) m_maxx = x1;
417 if(y1 > m_maxy) m_maxy = y1;
418
419
420 //m_llist->InvalidateUpdateRect();
421 //const wxRect *r = m_llist->GetUpdateRect();
422 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
423 updateRect->x, updateRect->y, updateRect->x+updateRect->width, updateRect->y+updateRect->height);
424
425 #if 0
426 //FIXME: we should never need to call Layout at all because the
427 // list does it automatically.
428 // Maybe we need to change the scrollbar sizes or positions,
429 // so layout the list and check:
430 if(IsDirty())
431 m_llist->Layout(dc);
432 wxLogDebug("Update rect after calling Layout: %ld,%ld / %ld,%ld",
433 r->x, r->y, r->x+r->width, r->y+r->height);
434 // this is needed even when only the cursor moved
435 m_llist->Layout(dc,y0+y1);
436 wxLogDebug("Update rect after calling Layout again: %ld,%ld / %ld,%ld",
437 r->x, r->y, r->x+r->width, r->y+r->height);
438 #endif
439
440 if(IsDirty())
441 ResizeScrollbars();
442
443 /* Make sure that the scrollbars are at a position so that the
444 cursor is visible if we are editing. */
445 /** Scroll so that cursor is visible! */
446 wxLogDebug("m_ScrollToCursor = %d", (int) m_ScrollToCursor);
447 if(IsEditable() && m_ScrollToCursor)
448 {
449 wxPoint cc = m_llist->GetCursorScreenPos(*m_memDC);
450 if(cc.x < x0 || cc.y < y0
451 || cc.x >= x0+(9*x1)/10 || cc.y >= y0+(9*y1/10)) // (9*x)/10 == 90%
452 {
453 int nx, ny;
454 nx = cc.x - x1/2; if(nx < 0) nx = 0;
455 ny = cc.y - y1/2; if(ny < 0) ny = 0;
456 Scroll(nx/dx,ny/dy); // new view start
457 x0 = nx; y0 = ny;
458 }
459 }
460
461 /* Check whether the window has grown, if so, we need to reallocate
462 the bitmap to be larger. */
463 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
464 {
465 wxASSERT(m_bitmapSize.x > 0);
466 wxASSERT(m_bitmapSize.y > 0);
467
468 m_memDC->SelectObject(wxNullBitmap);
469 delete m_bitmap;
470 m_bitmapSize = wxPoint(x1,y1);
471 m_bitmap = new wxBitmap(x1,y1);
472 m_memDC->SelectObject(*m_bitmap);
473 }
474
475 // Device origins on the memDC are suspect, we translate manually
476 // with the translate parameter of Draw().
477 m_memDC->SetDeviceOrigin(0,0);
478 m_memDC->SetBrush(wxBrush(m_llist->GetDefaults()->GetBGColour(), wxSOLID));
479 m_memDC->SetPen(wxPen(m_llist->GetDefaults()->GetBGColour(),0,wxTRANSPARENT));
480 m_memDC->SetLogicalFunction(wxCOPY);
481 if(m_BGbitmap)
482 {
483 CoordType
484 y, x,
485 w = m_BGbitmap->GetWidth(),
486 h = m_BGbitmap->GetHeight();
487 for(y = 0; y < y1; y+=h)
488 for(x = 0; x < x1; x+=w)
489 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
490 m_memDC->SetBackgroundMode(wxTRANSPARENT);
491 }
492 else
493 {
494 m_memDC->SetBackgroundMode(wxSOLID);
495 m_memDC->DrawRectangle(0,0,x1, y1);
496 }
497
498
499 /* This is the important bit: we tell the list to draw itself: */
500 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
501 updateRect->x, updateRect->y, updateRect->x+updateRect->width, updateRect->y+updateRect->height);
502
503 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
504 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
505
506 // Now copy everything to the screen:
507 #if 0
508 //FIXME:
509 // 1. the update region as calculated by the list is wrong
510 // 2. we get wrong values here
511 // 3. how about the offset?
512 wxRegionIterator ri ( GetUpdateRegion() );
513 if(ri)
514 while(ri)
515 {
516 wxLogDebug("UpdateRegion: %ld,%ld, %ld,%ld",
517 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH());
518 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
519 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
520 ri++;
521 }
522 else
523 #endif
524 {
525 // FIXME: Trying to copy only the changed parts, but it does not seem
526 // to work:
527 // x0 = updateRect->x; y0 = updateRect->y;
528 // if(updateRect->height < y1)
529 // y1 = updateRect->height;
530 // y1 += WXLO_YOFFSET; //FIXME might not be needed
531 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
532 }
533 //FIXME: we need to make sure we blit draw the cursor!
534 // How about drawing it directly to the screen?
535 if(IsEditable())
536 //m_llist->DrawCursor(*m_memDC,m_HaveFocus,offset);
537 m_llist->DrawCursor(dc,m_HaveFocus, wxPoint(WXLO_XOFFSET,WXLO_YOFFSET)); //direct to screen
538
539 ResetDirty();
540 m_ScrollToCursor = false;
541 m_llist->InvalidateUpdateRect();
542 }
543
544 // change the range and position of scroll bars
545 void
546 wxLayoutWindow::ResizeScrollbars(bool exact)
547 {
548 wxPoint max = m_llist->GetSize();
549
550 if(max.x > m_maxx || max.y > m_maxy
551 || max.x > m_maxx-WXLO_ROFFSET || max.y > m_maxy-WXLO_BOFFSET
552 || exact)
553 {
554 if(! exact)
555 {
556 // add an extra bit to the sizes to avoid future updates
557 max.x = max.x+WXLO_ROFFSET;
558 max.y = max.y+WXLO_BOFFSET;
559 }
560 ViewStart(&m_ViewStartX, &m_ViewStartY);
561 SetScrollbars(10, 20, max.x/10+1,max.y/20+1,m_ViewStartX,m_ViewStartY,true);
562 m_maxx = max.x; m_maxy = max.y;
563 }
564 }
565
566 void
567 wxLayoutWindow::Paste(void)
568 {
569 wxString text;
570 // Read some text
571 if (wxTheClipboard->Open())
572 {
573 wxTextDataObject data;
574 if (wxTheClipboard->IsSupported( data.GetFormat() ))
575 {
576 wxTheClipboard->GetData(&data);
577 text += data.GetText();
578 }
579 wxTheClipboard->Close();
580 }
581 #if 0
582 /* Unfortunately, this little hack doesn't work. So I'll go back to
583 pure X11. */
584 if(text.Length() == 0)
585 {
586 wxTextCtrl tmp_tctrl(this,-1);
587 tmp_tctrl.Paste();
588 text += tmp_tctrl.GetValue();
589 }
590 #endif
591 wxLayoutImportText( m_llist, text);
592 }
593
594
595 wxMenu *
596 wxLayoutWindow::MakeFormatMenu()
597 {
598 wxMenu *m = new wxMenu(_("Layout Menu"));
599
600 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
601 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
602 m->AppendSeparator();
603 m->Append(WXLOWIN_MENU_UNDERLINE_ON, _("&Underline on"),_("Activate underline mode."), false);
604 m->Append(WXLOWIN_MENU_UNDERLINE_OFF,_("&Underline off"),_("Deactivate underline mode."), false);
605 m->Append(WXLOWIN_MENU_BOLD_ON ,_("&Bold on"),_("Activate bold mode."), false);
606 m->Append(WXLOWIN_MENU_BOLD_OFF ,_("&Bold off"),_("Deactivate bold mode."), false);
607 m->Append(WXLOWIN_MENU_ITALICS_ON ,_("&Italics on"),_("Activate italics mode."), false);
608 m->Append(WXLOWIN_MENU_ITALICS_OFF ,_("&Italics off"),_("Deactivate italics mode."), false);
609 m->AppendSeparator();
610 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
611 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
612 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
613 return m;
614 }
615
616 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
617 {
618 switch (event.GetId())
619 {
620 case WXLOWIN_MENU_LARGER:
621 m_llist->SetFontLarger(); break;
622 case WXLOWIN_MENU_SMALLER:
623 m_llist->SetFontSmaller(); break;
624 case WXLOWIN_MENU_UNDERLINE_ON:
625 m_llist->SetFontUnderline(true); break;
626 case WXLOWIN_MENU_UNDERLINE_OFF:
627 m_llist->SetFontUnderline(false); break;
628 case WXLOWIN_MENU_BOLD_ON:
629 m_llist->SetFontWeight(wxBOLD); break;
630 case WXLOWIN_MENU_BOLD_OFF:
631 m_llist->SetFontWeight(wxNORMAL); break;
632 case WXLOWIN_MENU_ITALICS_ON:
633 m_llist->SetFontStyle(wxITALIC); break;
634 case WXLOWIN_MENU_ITALICS_OFF:
635 m_llist->SetFontStyle(wxNORMAL); break;
636 case WXLOWIN_MENU_ROMAN:
637 m_llist->SetFontFamily(wxROMAN); break;
638 case WXLOWIN_MENU_TYPEWRITER:
639 m_llist->SetFontFamily(wxFIXED); break;
640 case WXLOWIN_MENU_SANSSERIF:
641 m_llist->SetFontFamily(wxSWISS); break;
642 }
643 }
644
645 void
646 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
647 {
648 m_HaveFocus = true;
649 //FIXME DoPaint(); // to repaint the cursor
650 }
651
652 void
653 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
654 {
655 m_HaveFocus = false;
656 //FIXME DoPaint(); // to repaint the cursor
657 }