]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
Another one bites the dust.
[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
27 # include "gui/wxlwindow.h"
28 # include "gui/wxlparser.h"
29 #else
30 # ifdef __WXMSW__
31 # include <windows.h>
32 # undef FindWindow
33 # undef GetCharWidth
34 # undef StartDoc
35 # endif
36
37 # include "wxlwindow.h"
38 # include "wxlparser.h"
39 #endif
40
41 #include <wx/clipbrd.h>
42 #include <wx/textctrl.h>
43 #include <wx/dataobj.h>
44
45 #include <ctype.h>
46
47 /// offsets to put a nice frame around text
48 #define WXLO_XOFFSET 4
49 #define WXLO_YOFFSET 4
50
51 /// offset to the right and bottom for when to redraw scrollbars
52 #define WXLO_ROFFSET 20
53 #define WXLO_BOFFSET 20
54
55 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
56 EVT_PAINT (wxLayoutWindow::OnPaint)
57 EVT_CHAR (wxLayoutWindow::OnChar)
58 EVT_LEFT_DOWN(wxLayoutWindow::OnLeftMouseClick)
59 EVT_RIGHT_DOWN(wxLayoutWindow::OnRightMouseClick)
60 EVT_LEFT_DCLICK(wxLayoutWindow::OnMouseDblClick)
61 EVT_MENU_RANGE(WXLOWIN_MENU_FIRST, WXLOWIN_MENU_LAST, wxLayoutWindow::OnMenu)
62 EVT_SET_FOCUS(wxLayoutWindow::OnSetFocus)
63 EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus)
64 END_EVENT_TABLE()
65
66 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
67 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize,
68 wxHSCROLL | wxVSCROLL | wxBORDER)
69
70 {
71 m_Editable = false;
72 m_doSendEvents = false;
73 m_ViewStartX = 0; m_ViewStartY = 0;
74 m_DoPopupMenu = true;
75 m_PopupMenu = MakeFormatMenu();
76 m_memDC = new wxMemoryDC;
77 m_bitmap = new wxBitmap(4,4);
78 m_bitmapSize = wxPoint(4,4);
79 m_llist = new wxLayoutList();
80 m_BGbitmap = NULL;
81 m_ScrollToCursor = false;
82 SetWrapMargin(0);
83 wxPoint max = m_llist->GetSize();
84 SetScrollbars(10, 20 /*lineHeight*/, max.x/10+1, max.y/20+1);
85 EnableScrolling(true,true);
86 m_maxx = max.x; m_maxy = max.y;
87 m_Selecting = false;
88 SetCursor(wxCURSOR_IBEAM);
89 SetDirty();
90 }
91
92 wxLayoutWindow::~wxLayoutWindow()
93 {
94 delete m_memDC; // deletes bitmap automatically (?)
95 delete m_bitmap;
96 delete m_llist;
97 delete m_PopupMenu;
98 SetBackgroundBitmap(NULL);
99 }
100
101 #ifdef __WXMSW__
102 long
103 wxLayoutWindow::MSWGetDlgCode()
104 {
105 // if we don't return this, we won't get OnChar() events for TABs and ENTER
106 return DLGC_WANTCHARS | DLGC_WANTARROWS | DLGC_WANTMESSAGE;
107 }
108 #endif //MSW
109
110 void
111 wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
112 {
113 wxPaintDC dc( this );
114 PrepareDC( dc );
115 SetFocus();
116
117
118 wxPoint findPos;
119 findPos.x = dc.DeviceToLogicalX(event.GetX());
120 findPos.y = dc.DeviceToLogicalY(event.GetY());
121
122 findPos.x -= WXLO_XOFFSET;
123 findPos.y -= WXLO_YOFFSET;
124
125 if(findPos.x < 0) findPos.x = 0;
126 if(findPos.y < 0) findPos.y = 0;
127
128 m_ClickPosition = wxPoint(event.GetX(), event.GetY());
129 #ifdef WXLAYOUT_DEBUG
130 wxLogDebug("wxLayoutWindow::OnMouse: (%d, %d) -> (%d, %d)",
131 event.GetX(), event.GetY(), findPos.x, findPos.y);
132 #endif
133
134 wxPoint cursorPos;
135 wxLayoutObject *obj = m_llist->FindObjectScreen(dc, findPos, &cursorPos);
136
137 #ifdef WXLAYOUT_DEBUG
138 if(obj)
139 wxLogDebug("wxLayoutWindow::OnMouse: Found object of type %d.",
140 obj->GetType());
141 else
142 wxLogDebug("wxLayoutWindow::OnMouse: Found no object.");
143 #endif
144
145 // always move cursor to mouse click:
146 if(obj && eventId == WXLOWIN_MENU_LCLICK)
147 {
148 m_llist->MoveCursorTo(cursorPos);
149 m_ScrollToCursor = true; //FIXME: needed? DoPaint(m_llist->GetUpdateRect());
150 }
151 if(!m_doSendEvents) // nothing to do
152 return;
153
154 // only do the menu if activated, editable and not on a clickable object
155 if(eventId == WXLOWIN_MENU_RCLICK
156 && IsEditable()
157 && (! obj || (obj && obj->GetUserData() == NULL))
158 )
159 {
160 PopupMenu(m_PopupMenu, m_ClickPosition.x, m_ClickPosition.y);
161 return;
162 }
163 // find the object at this position
164 if(obj)
165 {
166 wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, eventId);
167 commandEvent.SetEventObject( this );
168 commandEvent.SetClientData((char *)obj);
169 GetEventHandler()->ProcessEvent(commandEvent);
170 }
171 }
172
173 /*
174 * Some simple keyboard handling.
175 */
176 void
177 wxLayoutWindow::OnChar(wxKeyEvent& event)
178 {
179 if(!IsEditable()) // do nothing
180 {
181 event.Skip();
182 return;
183 }
184
185 long keyCode = event.KeyCode();
186 if(event.ShiftDown())
187 m_Selecting = true;
188 else
189 {
190 if(m_Selecting)
191 m_llist->EndSelection();
192 m_Selecting = false;
193 }
194 /* First, handle control keys */
195 if(event.ControlDown() && ! event.AltDown())
196 {
197 switch(keyCode)
198 {
199 case WXK_DELETE :
200 case 'd':
201 m_llist->Delete(1);
202 break;
203 case 'y':
204 m_llist->DeleteLines(1);
205 break;
206 case 'h': // like backspace
207 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
208 break;
209 case 'u':
210 m_llist->DeleteToBeginOfLine();
211 break;
212 case 'k':
213 m_llist->DeleteToEndOfLine();
214 break;
215 #ifdef WXLAYOUT_DEBUG
216 case WXK_F1:
217 m_llist->SetFont(-1,-1,-1,-1,true); // underlined
218 break;
219 #endif
220 default:
221 ;
222 }
223 }
224 // ALT only:
225 else if( event.AltDown() && ! event.ControlDown() )
226 {
227 switch(keyCode)
228 {
229 case WXK_DELETE:
230 case 'd':
231 m_llist->DeleteWord();
232 break;
233 default:
234 ;
235 }
236 }
237 // no control keys:
238 else if ( ! event.AltDown() && ! event.ControlDown())
239 {
240 switch(keyCode)
241 {
242 case WXK_RIGHT:
243 if(m_Selecting) m_llist->StartSelection();
244 m_llist->MoveCursorHorizontally(1);
245 break;
246 case WXK_LEFT:
247 if(m_Selecting) m_llist->StartSelection();
248 m_llist->MoveCursorHorizontally(-1);
249 break;
250 case WXK_UP:
251 if(m_Selecting) m_llist->StartSelection();
252 m_llist->MoveCursorVertically(-1);
253 break;
254 case WXK_DOWN:
255 if(m_Selecting) m_llist->StartSelection();
256 m_llist->MoveCursorVertically(1);
257 break;
258 case WXK_PRIOR:
259 if(m_Selecting) m_llist->StartSelection();
260 m_llist->MoveCursorVertically(-20);
261 break;
262 case WXK_NEXT:
263 if(m_Selecting) m_llist->StartSelection();
264 m_llist->MoveCursorVertically(20);
265 break;
266 case WXK_HOME:
267 if(m_Selecting) m_llist->StartSelection();
268 m_llist->MoveCursorToBeginOfLine();
269 break;
270 case WXK_END:
271 if(m_Selecting) m_llist->StartSelection();
272 m_llist->MoveCursorToEndOfLine();
273 break;
274 case WXK_DELETE :
275 m_llist->Delete(1);
276 break;
277 case WXK_BACK: // backspace
278 if(m_llist->MoveCursorHorizontally(-1)) m_llist->Delete(1);
279 break;
280 case WXK_RETURN:
281 if(m_WrapMargin > 0)
282 m_llist->WrapLine(m_WrapMargin);
283 m_llist->LineBreak();
284 break;
285 default:
286 if((!(event.ControlDown() || event.AltDown() || event.MetaDown()))
287 && (keyCode < 256 && keyCode >= 32)
288 )
289 {
290 wxString tmp;
291 tmp += keyCode;
292 if(m_WrapMargin > 0 && isspace(keyCode))
293 m_llist->WrapLine(m_WrapMargin);
294 m_llist->Insert(tmp);
295 }
296 break;
297 }
298 }
299 SetDirty();
300 SetModified();
301 m_ScrollToCursor = true;
302 //DoPaint(true); // paint and scroll to cursor
303 wxRect r = *m_llist->GetUpdateRect();
304 r.x -= WXLO_XOFFSET; r.y -= WXLO_YOFFSET;
305 Refresh( FALSE, &r);
306 }
307
308 void
309 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)) // or: OnDraw(wxDC& dc)
310 {
311 wxRect region = GetUpdateRegion().GetBox();
312 InternalPaint(& region);
313 }
314
315 void
316 wxLayoutWindow::DoPaint(const wxRect *updateRect)
317 {
318 #ifdef __WXGTK__
319 InternalPaint(updateRect);
320 #else
321 Refresh(FALSE, updateRect); // Causes bad flicker under wxGTK!!!
322 #endif
323 }
324
325 void
326 wxLayoutWindow::InternalPaint(const wxRect *updateRect)
327 {
328 wxPaintDC dc( this );
329 PrepareDC( dc );
330
331 int x0,y0,x1,y1, dx, dy;
332
333 // Calculate where the top of the visible area is:
334 ViewStart(&x0,&y0);
335 GetScrollPixelsPerUnit(&dx, &dy);
336 x0 *= dx; y0 *= dy;
337
338 // Get the size of the visible window:
339 GetClientSize(&x1,&y1);
340 wxASSERT(x1 > 0);
341 wxASSERT(y1 > 0);
342 // As we have the values anyway, use them to avoid unnecessary
343 // scrollbar updates.
344 if(x1 > m_maxx) m_maxx = x1;
345 if(y1 > m_maxy) m_maxy = y1;
346
347
348 m_llist->InvalidateUpdateRect();
349 const wxRect *r = m_llist->GetUpdateRect();
350 wxLogDebug("Update rect before calling Layout: %ld,%ld / %ld,%ld",
351 r->x, r->y, r->x+r->width, r->y+r->height);
352
353 #if 0
354 //FIXME: we should never need to call Layout at all because the
355 // list does it automatically.
356 // Maybe we need to change the scrollbar sizes or positions,
357 // so layout the list and check:
358 if(IsDirty())
359 m_llist->Layout(dc);
360 wxLogDebug("Update rect after calling Layout: %ld,%ld / %ld,%ld",
361 r->x, r->y, r->x+r->width, r->y+r->height);
362 // this is needed even when only the cursor moved
363 m_llist->Layout(dc,y0+y1);
364 wxLogDebug("Update rect after calling Layout again: %ld,%ld / %ld,%ld",
365 r->x, r->y, r->x+r->width, r->y+r->height);
366 #endif
367
368 if(IsDirty())
369 ResizeScrollbars();
370
371 /* Make sure that the scrollbars are at a position so that the
372 cursor is visible if we are editing. */
373 /** Scroll so that cursor is visible! */
374 wxLogDebug("m_ScrollToCursor = %d", (int) m_ScrollToCursor);
375 if(IsEditable() && m_ScrollToCursor)
376 {
377 wxPoint cc = m_llist->GetCursorScreenPos(*m_memDC);
378 if(cc.x < x0 || cc.y < y0
379 || cc.x >= x0+(9*x1)/10 || cc.y >= y0+(9*y1/10)) // (9*x)/10 == 90%
380 {
381 int nx, ny;
382 nx = cc.x - x1/2; if(nx < 0) nx = 0;
383 ny = cc.y - y1/2; if(ny < 0) ny = 0;
384 Scroll(nx/dx,ny/dy); // new view start
385 x0 = nx; y0 = ny;
386 }
387 }
388
389 /* Check whether the window has grown, if so, we need to reallocate
390 the bitmap to be larger. */
391 if(x1 > m_bitmapSize.x || y1 > m_bitmapSize.y)
392 {
393 wxASSERT(m_bitmapSize.x > 0);
394 wxASSERT(m_bitmapSize.y > 0);
395
396 m_memDC->SelectObject(wxNullBitmap);
397 delete m_bitmap;
398 m_bitmapSize = wxPoint(x1,y1);
399 m_bitmap = new wxBitmap(x1,y1);
400 m_memDC->SelectObject(*m_bitmap);
401 }
402 // Device origins on the memDC are suspect, we translate manually
403 // with the translate parameter of Draw().
404 m_memDC->SetDeviceOrigin(0,0);
405 m_memDC->SetBackgroundMode(wxTRANSPARENT);
406 m_memDC->SetBrush(wxBrush(m_llist->GetDefaults()->GetBGColour(), wxSOLID));
407 m_memDC->SetPen(wxPen(m_llist->GetDefaults()->GetBGColour(),0,wxTRANSPARENT));
408 m_memDC->SetLogicalFunction(wxCOPY);
409 if(m_BGbitmap)
410 {
411 CoordType
412 y, x,
413 w = m_BGbitmap->GetWidth(),
414 h = m_BGbitmap->GetHeight();
415 for(y = 0; y < y1; y+=h)
416 for(x = 0; x < x1; x+=w)
417 m_memDC->DrawBitmap(*m_BGbitmap, x, y);
418 }
419 else
420 m_memDC->DrawRectangle(0,0,x1, y1);
421
422 // The offsets give the window a tiny border on the left and top, looks nice.
423 wxPoint offset(-x0+WXLO_XOFFSET,-y0+WXLO_YOFFSET);
424 m_llist->Draw(*m_memDC,offset);
425 if(IsEditable())
426 m_llist->DrawCursor(*m_memDC,m_HaveFocus,offset);
427
428 // Now copy everything to the screen:
429 #if 0
430 //FIXME:
431 // 1. the update region as calculated by the list is wrong
432 // 2. we get wrong values here
433 // 3. how about the offset?
434 wxRegionIterator ri ( GetUpdateRegion() );
435 if(ri)
436 while(ri)
437 {
438 wxLogDebug("UpdateRegion: %ld,%ld, %ld,%ld",
439 ri.GetX(),ri.GetY(),ri.GetW(),ri.GetH());
440 dc.Blit(x0+ri.GetX(),y0+ri.GetY(),ri.GetW(),ri.GetH(),
441 m_memDC,ri.GetX(),ri.GetY(),wxCOPY,FALSE);
442 ri++;
443 }
444 else
445 #endif
446 // If there are no update rectangles, we got called to reflect
447 // a change in the list. Currently there is no mechanism to
448 // easily find out which bits need updating, so we update
449 // all. The wxLayoutList could handle this, creating a list or
450 // at least one large rectangle of changes. FIXME
451 dc.Blit(x0,y0,x1,y1,m_memDC,0,0,wxCOPY,FALSE);
452
453 ResetDirty();
454 m_ScrollToCursor = false;
455 }
456
457 // change the range and position of scroll bars
458 void
459 wxLayoutWindow::ResizeScrollbars(bool exact)
460 {
461 wxPoint max = m_llist->GetSize();
462
463 if(max.x > m_maxx || max.y > m_maxy
464 || max.x > m_maxx-WXLO_ROFFSET || max.y > m_maxy-WXLO_BOFFSET
465 || exact)
466 {
467 if(! exact)
468 {
469 // add an extra bit to the sizes to avoid future updates
470 max.x = max.x+WXLO_ROFFSET;
471 max.y = max.y+WXLO_BOFFSET;
472 }
473 ViewStart(&m_ViewStartX, &m_ViewStartY);
474 SetScrollbars(10, 20, max.x/10+1,max.y/20+1,m_ViewStartX,m_ViewStartY,true);
475 m_maxx = max.x; m_maxy = max.y;
476 }
477 }
478
479 void
480 wxLayoutWindow::Paste(void)
481 {
482 wxString text;
483 // Read some text
484 if (wxTheClipboard->Open())
485 {
486 wxTextDataObject data;
487 if (wxTheClipboard->IsSupported( data.GetFormat() ))
488 {
489 wxTheClipboard->GetData(&data);
490 text += data.GetText();
491 }
492 wxTheClipboard->Close();
493 }
494 #ifdef __WXGTK__
495 /* wxGTK's sophisticated multi-format copy/paste is not supported
496 by 99% of the X11 clients available. If there was no selection,
497 do the dumb thing, too:
498 */
499 #if 0
500 /* Unfortunately, this little hack doesn't work. So I'll go back to
501 pure X11. */
502 if(text.Length() == 0)
503 {
504 wxTextCtrl tmp_tctrl(this,-1);
505 tmp_tctrl.Paste();
506 text += tmp_tctrl.GetValue();
507 }
508 #endif
509
510 #endif
511 wxLayoutImportText( m_llist, text);
512 }
513
514
515 wxMenu *
516 wxLayoutWindow::MakeFormatMenu()
517 {
518 wxMenu *m = new wxMenu(_("Layout Menu"));
519
520 m->Append(WXLOWIN_MENU_LARGER ,_("&Larger"),_("Switch to larger font."), false);
521 m->Append(WXLOWIN_MENU_SMALLER ,_("&Smaller"),_("Switch to smaller font."), false);
522 m->AppendSeparator();
523 m->Append(WXLOWIN_MENU_UNDERLINE_ON, _("&Underline on"),_("Activate underline mode."), false);
524 m->Append(WXLOWIN_MENU_UNDERLINE_OFF,_("&Underline off"),_("Deactivate underline mode."), false);
525 m->Append(WXLOWIN_MENU_BOLD_ON ,_("&Bold on"),_("Activate bold mode."), false);
526 m->Append(WXLOWIN_MENU_BOLD_OFF ,_("&Bold off"),_("Deactivate bold mode."), false);
527 m->Append(WXLOWIN_MENU_ITALICS_ON ,_("&Italics on"),_("Activate italics mode."), false);
528 m->Append(WXLOWIN_MENU_ITALICS_OFF ,_("&Italics off"),_("Deactivate italics mode."), false);
529 m->AppendSeparator();
530 m->Append(WXLOWIN_MENU_ROMAN ,_("&Roman"),_("Switch to roman font."), false);
531 m->Append(WXLOWIN_MENU_TYPEWRITER,_("&Typewriter"),_("Switch to typewriter font."), false);
532 m->Append(WXLOWIN_MENU_SANSSERIF ,_("&Sans Serif"),_("Switch to sans serif font."), false);
533 return m;
534 }
535
536 void wxLayoutWindow::OnMenu(wxCommandEvent& event)
537 {
538 switch (event.GetId())
539 {
540 case WXLOWIN_MENU_LARGER:
541 m_llist->SetFontLarger(); break;
542 case WXLOWIN_MENU_SMALLER:
543 m_llist->SetFontSmaller(); break;
544 case WXLOWIN_MENU_UNDERLINE_ON:
545 m_llist->SetFontUnderline(true); break;
546 case WXLOWIN_MENU_UNDERLINE_OFF:
547 m_llist->SetFontUnderline(false); break;
548 case WXLOWIN_MENU_BOLD_ON:
549 m_llist->SetFontWeight(wxBOLD); break;
550 case WXLOWIN_MENU_BOLD_OFF:
551 m_llist->SetFontWeight(wxNORMAL); break;
552 case WXLOWIN_MENU_ITALICS_ON:
553 m_llist->SetFontStyle(wxITALIC); break;
554 case WXLOWIN_MENU_ITALICS_OFF:
555 m_llist->SetFontStyle(wxNORMAL); break;
556 case WXLOWIN_MENU_ROMAN:
557 m_llist->SetFontFamily(wxROMAN); break;
558 case WXLOWIN_MENU_TYPEWRITER:
559 m_llist->SetFontFamily(wxFIXED); break;
560 case WXLOWIN_MENU_SANSSERIF:
561 m_llist->SetFontFamily(wxSWISS); break;
562 }
563 }
564
565 void
566 wxLayoutWindow::OnSetFocus(wxFocusEvent &ev)
567 {
568 m_HaveFocus = true;
569 //FIXME DoPaint(); // to repaint the cursor
570 }
571
572 void
573 wxLayoutWindow::OnKillFocus(wxFocusEvent &ev)
574 {
575 m_HaveFocus = false;
576 //FIXME DoPaint(); // to repaint the cursor
577 }