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