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