]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
Updated for Mahogany.
[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; //FIXME: needed? DoPaint(m_llist->GetUpdateRect());
173 }
174 if(!m_doSendEvents) // nothing to do
175 return;
176
177 // only do the menu if activated, editable and not on a clickable object
178 if(eventId == WXLOWIN_MENU_RCLICK
179 && IsEditable()
180 && (! obj || (obj && obj->GetUserData() == NULL))
181 )
182 {
183 PopupMenu(m_PopupMenu, m_ClickPosition.x, m_ClickPosition.y);
184 return;
185 }
186 // find the object at this position
187 if(obj)
188 {
189 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
190 commandEvent.SetEventObject( this );
191 commandEvent.SetClientData((char *)obj);
192 GetEventHandler()->ProcessEvent(commandEvent);
193 }
194 }
195
196 /*
197 * Some simple keyboard handling.
198 */
199 void
200 wxLayoutWindow::OnChar(wxKeyEvent& event)
201 {
202 #ifdef WXLAYOUT_DEBUG
203 if(event.KeyCode() == WXK_F1)
204 {
205 m_llist->Debug();
206 return;
207 }
208 #endif
209
210 long keyCode = event.KeyCode();
211 if(m_Selecting && ! event.ShiftDown())
212 {
213 m_llist->EndSelection();
214 m_Selecting = false;
215 }
216 else
217 if(! m_Selecting && event.ShiftDown())
218 {
219 switch(keyCode)
220 {
221 case WXK_UP:
222 case WXK_DOWN:
223 case WXK_RIGHT:
224 case WXK_LEFT:
225 case WXK_PRIOR:
226 case WXK_NEXT:
227 case WXK_HOME:
228 case WXK_END:
229 m_Selecting = true;
230 m_llist->StartSelection();
231 break;
232 default:
233 ;
234 }
235 }
236
237 if(!IsEditable()) // do nothing
238 {
239 switch(keyCode)
240 {
241 case WXK_UP:
242 m_llist->MoveCursorVertically(-1);
243 break;
244 case WXK_DOWN:
245 m_llist->MoveCursorVertically(1);
246 break;
247 case WXK_PRIOR:
248 m_llist->MoveCursorVertically(-20);
249 break;
250 case WXK_NEXT:
251 m_llist->MoveCursorVertically(20);
252 break;
253 default:
254 ;
255 }
256 return;
257 }
258
259 /* First, handle control keys */
260 if(event.ControlDown() && ! event.AltDown())
261 {
262 switch(keyCode)
263 {
264 case WXK_DELETE :
265 case 'd':
266 m_llist->Delete(1);
267 break;
268 case 'y':
269 m_llist->DeleteLines(1);
270 break;
271 case 'h': // like backspace
272 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
273 break;
274 case 'u':
275 m_llist->DeleteToBeginOfLine();
276 break;
277 case 'k':
278 m_llist->DeleteToEndOfLine();
279 break;
280 case 'v':
281 Paste();
282 break;
283 #ifdef WXLAYOUT_DEBUG
284 case WXK_F1:
285 m_llist->SetFont(-1,-1,-1,-1,true); // underlined
286 break;
287 #endif
288 default:
289 ;
290 }
291 }
292 // ALT only:
293 else if( event.AltDown() && ! event.ControlDown() )
294 {
295 switch(keyCode)
296 {
297 case WXK_DELETE:
298 case 'd':
299 m_llist->DeleteWord();
300 break;
301 default:
302 ;
303 }
304 }
305 // no control keys:
306 else if ( ! event.AltDown() && ! event.ControlDown())
307 {
308 switch(keyCode)
309 {
310 case WXK_RIGHT:
311 m_llist->MoveCursorHorizontally(1);
312 break;
313 case WXK_LEFT:
314 m_llist->MoveCursorHorizontally(-1);
315 break;
316 case WXK_UP:
317 m_llist->MoveCursorVertically(-1);
318 break;
319 case WXK_DOWN:
320 m_llist->MoveCursorVertically(1);
321 break;
322 case WXK_PRIOR:
323 m_llist->MoveCursorVertically(-20);
324 break;
325 case WXK_NEXT:
326 m_llist->MoveCursorVertically(20);
327 break;
328 case WXK_HOME:
329 m_llist->MoveCursorToBeginOfLine();
330 break;
331 case WXK_END:
332 m_llist->MoveCursorToEndOfLine();
333 break;
334 case WXK_INSERT:
335 if(event.ShiftDown())
336 Paste();
337 break;
338 case WXK_DELETE :
339 m_llist->Delete(1);
340 break;
341 case WXK_BACK: // backspace
342 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
343 break;
344 case WXK_RETURN:
345 if(m_WrapMargin > 0)
346 m_llist->WrapLine(m_WrapMargin);
347 m_llist->LineBreak();
348 break;
349 default:
350 if((!(event.ControlDown() || event.AltDown() || event.MetaDown()))
351 && (keyCode < 256 && keyCode >= 32)
352 )
353 {
354 wxString tmp;
355 tmp += keyCode;
356 if(m_WrapMargin > 0 && isspace(keyCode))
357 m_llist->WrapLine(m_WrapMargin);
358 m_llist->Insert(tmp);
359 }
360 break;
361 }
362 }
363 SetDirty();
364 SetModified();
365 m_ScrollToCursor = true;
366 //DoPaint(true); // paint and scroll to cursor
367 wxRect r = *m_llist->GetUpdateRect();
368 Refresh( FALSE, &r);
369 }
370
371 void
372 wxLayoutWindow::OnKeyUp(wxKeyEvent& event)
373 {
374 if(event.KeyCode() == WXK_SHIFT && m_llist->IsSelecting())
375 m_llist->EndSelection();
376 event.Skip();
377 }
378
379 void
380 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)) // or: OnDraw(wxDC& dc)
381 {
382 wxRect region = GetUpdateRegion().GetBox();
383 InternalPaint(& region);
384 }
385
386 void
387 wxLayoutWindow::DoPaint(const wxRect *updateRect)
388 {
389 #ifdef __WXGTK__
390 InternalPaint(updateRect);
391 #else
392 Refresh(FALSE, updateRect); // Causes bad flicker under wxGTK!!!
393 #endif
394 }
395
396 void
397 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
398 {
399 wxPaintDC dc( this );
400 PrepareDC( dc );
401
402 int x0,y0,x1,y1, dx, dy;
403
404 // Calculate where the top of the visible area is:
405 ViewStart(&x0,&y0);
406 GetScrollPixelsPerUnit(&dx, &dy);
407 x0 *= dx; y0 *= dy;
408
409 // Get the size of the visible window:
410 GetClientSize(&x1,&y1);
411 wxASSERT(x1 > 0);
412 wxASSERT(y1 > 0);
413 // As we have the values anyway, use them to avoid unnecessary
414 // scrollbar updates.
415 if(x1 > m_maxx) m_maxx = x1;
416 if(y1 > m_maxy) m_maxy = y1;
417
418
419 //m_llist->InvalidateUpdateRect();
420 //const wxRect *r = m_llist->GetUpdateRect();
421 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
422 updateRect->x, updateRect->y, updateRect->x+updateRect->width, updateRect->y+updateRect->height);
423
424 #if 0
425 //FIXME: we should never need to call Layout at all because the
426 // list does it automatically.
427 // Maybe we need to change the scrollbar sizes or positions,
428 // so layout the list and check:
429 if(IsDirty())
430 m_llist->Layout(dc);
431 wxLogDebug("Update rect after calling Layout: %ld,%ld / %ld,%ld",
432 r->x, r->y, r->x+r->width, r->y+r->height);
433 // this is needed even when only the cursor moved
434 m_llist->Layout(dc,y0+y1);
435 wxLogDebug("Update rect after calling Layout again: %ld,%ld / %ld,%ld",
436 r->x, r->y, r->x+r->width, r->y+r->height);
437 #endif
438
439 if(IsDirty())
440 ResizeScrollbars();
441
442 /* Make sure that the scrollbars are at a position so that the
443 cursor is visible if we are editing. */
444 /** Scroll so that cursor is visible! */
445 wxLogDebug("m_ScrollToCursor = %d", (int) m_ScrollToCursor);
446 if(IsEditable() && m_ScrollToCursor)
447 {
448 wxPoint cc = m_llist->GetCursorScreenPos(*m_memDC);
449 if(cc.x < x0 || cc.y < y0
450 || cc.x >= x0+(9*x1)/10 || cc.y >= y0+(9*y1/10)) // (9*x)/10 == 90%
451 {
452 int nx, ny;
453 nx = cc.x - x1/2; if(nx < 0) nx = 0;
454 ny = cc.y - y1/2; if(ny < 0) ny = 0;
455 Scroll(nx/dx,ny/dy); // new view start
456 x0 = nx; y0 = ny;
457 }
458 }
459
460 /* Check whether the window has grown, if so, we need to reallocate
461 the bitmap to be larger. */
462 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
463 {
464 wxASSERT(m_bitmapSize.x > 0);
465 wxASSERT(m_bitmapSize.y > 0);
466
467 m_memDC->SelectObject(wxNullBitmap);
468 delete m_bitmap;
469 m_bitmapSize = wxPoint(x1,y1);
470 m_bitmap = new wxBitmap(x1,y1);
471 m_memDC->SelectObject(*m_bitmap);
472 }
473
474 // Device origins on the memDC are suspect, we translate manually
475 // with the translate parameter of Draw().
476 m_memDC->SetDeviceOrigin(0,0);
477 m_memDC->SetBrush(wxBrush(m_llist->GetDefaults()->GetBGColour(), wxSOLID));
478 m_memDC->SetPen(wxPen(m_llist->GetDefaults()->GetBGColour(),0,wxTRANSPARENT));
479 m_memDC->SetLogicalFunction(wxCOPY);
480 if(m_BGbitmap)
481 {
482 CoordType
483 y, x,
484 w = m_BGbitmap->GetWidth(),
485 h = m_BGbitmap->GetHeight();
486 for(y = 0; y < y1; y+=h)
487 for(x = 0; x < x1; x+=w)
488 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
489 m_memDC->SetBackgroundMode(wxTRANSPARENT);
490 }
491 else
492 {
493 m_memDC->SetBackgroundMode(wxSOLID);
494 m_memDC->DrawRectangle(0,0,x1, y1);
495 }
496
497
498 /* This is the important bit: we tell the list to draw itself: */
499 wxLogDebug("Update rect: %ld,%ld / %ld,%ld",
500 updateRect->x, updateRect->y, updateRect->x+updateRect->width, updateRect->y+updateRect->height);
501
502 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
503 m_llist->Draw(*m_memDC,offset, y0, y0+y1);
504
505 // Now copy everything to the screen:
506 #if 0
507 //FIXME:
508 // 1. the update region as calculated by the list is wrong
509 // 2. we get wrong values here
510 // 3. how about the offset?
511 wxRegionIterator ri ( GetUpdateRegion() );
512 if(ri)
513 while(ri)
514 {
515 wxLogDebug("UpdateRegion: %ld,%ld, %ld,%ld",
516 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH());
517 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
518 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
519 ri++;
520 }
521 else
522 #endif
523 {
524 // FIXME: Trying to copy only the changed parts, but it does not seem
525 // to work:
526 // x0 = updateRect->x; y0 = updateRect->y;
527 // if(updateRect->height < y1)
528 // y1 = updateRect->height;
529 // y1 += WXLO_YOFFSET; //FIXME might not be needed
530 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
531 }
532 //FIXME: we need to make sure we blit draw the cursor!
533 // How about drawing it directly to the screen?
534 if(IsEditable())
535 //m_llist->DrawCursor(*m_memDC,m_HaveFocus,offset);
536 m_llist->DrawCursor(dc,m_HaveFocus, wxPoint(WXLO_XOFFSET,WXLO_YOFFSET)); //direct to screen
537
538 ResetDirty();
539 m_ScrollToCursor = false;
540 m_llist->InvalidateUpdateRect();
541 }
542
543 // change the range and position of scroll bars
544 void
545 wxLayoutWindow::ResizeScrollbars(bool exact)
546 {
547 wxPoint max = m_llist->GetSize();
548
549 if(max.x > m_maxx || max.y > m_maxy
550 || max.x > m_maxx-WXLO_ROFFSET || max.y > m_maxy-WXLO_BOFFSET
551 || exact)
552 {
553 if(! exact)
554 {
555 // add an extra bit to the sizes to avoid future updates
556 max.x = max.x+WXLO_ROFFSET;
557 max.y = max.y+WXLO_BOFFSET;
558 }
559 ViewStart(&m_ViewStartX, &m_ViewStartY);
560 SetScrollbars(10, 20, max.x/10+1,max.y/20+1,m_ViewStartX,m_ViewStartY,true);
561 m_maxx = max.x; m_maxy = max.y;
562 }
563 }
564
565 void
566 wxLayoutWindow::Paste(void)
567 {
568 wxString text;
569 // Read some text
570 if (wxTheClipboard->Open())
571 {
572 wxTextDataObject data;
573 if (wxTheClipboard->IsSupported( data.GetFormat() ))
574 {
575 wxTheClipboard->GetData(&data);
576 text += data.GetText();
577 }
578 wxTheClipboard->Close();
579 }
580 #if 0
581 /* Unfortunately, this little hack doesn't work. So I'll go back to
582 pure X11. */
583 if(text.Length() == 0)
584 {
585 wxTextCtrl tmp_tctrl(this,-1);
586 tmp_tctrl.Paste();
587 text += tmp_tctrl.GetValue();
588 }
589 #endif
590 wxLayoutImportText( m_llist, text);
591 }
592
593
594 wxMenu *
595 wxLayoutWindow::MakeFormatMenu()
596 {
597 wxMenu *m = new wxMenu(_("Layout Menu"));
598
599 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
600 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
601 m->AppendSeparator();
602 m->Append(WXLOWIN_MENU_UNDERLINE_ON, _("&Underline on"),_("Activate underline mode."), false);
603 m->Append(WXLOWIN_MENU_UNDERLINE_OFF,_("&Underline off"),_("Deactivate underline mode."), false);
604 m->Append(WXLOWIN_MENU_BOLD_ON ,_("&Bold on"),_("Activate bold mode."), false);
605 m->Append(WXLOWIN_MENU_BOLD_OFF ,_("&Bold off"),_("Deactivate bold mode."), false);
606 m->Append(WXLOWIN_MENU_ITALICS_ON ,_("&Italics on"),_("Activate italics mode."), false);
607 m->Append(WXLOWIN_MENU_ITALICS_OFF ,_("&Italics off"),_("Deactivate italics mode."), false);
608 m->AppendSeparator();
609 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
610 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
611 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
612 return m;
613 }
614
615 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
616 {
617 switch (event.GetId())
618 {
619 case WXLOWIN_MENU_LARGER:
620 m_llist->SetFontLarger(); break;
621 case WXLOWIN_MENU_SMALLER:
622 m_llist->SetFontSmaller(); break;
623 case WXLOWIN_MENU_UNDERLINE_ON:
624 m_llist->SetFontUnderline(true); break;
625 case WXLOWIN_MENU_UNDERLINE_OFF:
626 m_llist->SetFontUnderline(false); break;
627 case WXLOWIN_MENU_BOLD_ON:
628 m_llist->SetFontWeight(wxBOLD); break;
629 case WXLOWIN_MENU_BOLD_OFF:
630 m_llist->SetFontWeight(wxNORMAL); break;
631 case WXLOWIN_MENU_ITALICS_ON:
632 m_llist->SetFontStyle(wxITALIC); break;
633 case WXLOWIN_MENU_ITALICS_OFF:
634 m_llist->SetFontStyle(wxNORMAL); break;
635 case WXLOWIN_MENU_ROMAN:
636 m_llist->SetFontFamily(wxROMAN); break;
637 case WXLOWIN_MENU_TYPEWRITER:
638 m_llist->SetFontFamily(wxFIXED); break;
639 case WXLOWIN_MENU_SANSSERIF:
640 m_llist->SetFontFamily(wxSWISS); break;
641 }
642 }
643
644 void
645 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
646 {
647 m_HaveFocus = true;
648 //FIXME DoPaint(); // to repaint the cursor
649 }
650
651 void
652 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
653 {
654 m_HaveFocus = false;
655 //FIXME DoPaint(); // to repaint the cursor
656 }