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