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