]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlwindow.cpp
added correct coordinate handling
[wxWidgets.git] / user / wxLayout / wxlwindow.cpp
1 /*-*- c++ -*-********************************************************
2 * wxLwindow.h : a scrolled Window for displaying/entering rich text*
3 * *
4 * (C) 1998 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 "wxlwindow.h"
14
15 #define VAR(x) cout << #x"=" << x << endl;
16
17 BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
18 EVT_PAINT (wxLayoutWindow::OnPaint)
19 EVT_CHAR (wxLayoutWindow::OnChar)
20 EVT_LEFT_DOWN(wxLayoutWindow::OnMouse)
21 END_EVENT_TABLE()
22
23 wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
24 : wxScrolledWindow(parent)
25 {
26 m_ScrollbarsSet = false;
27 m_EventId = 0;
28 }
29
30 void
31 wxLayoutWindow::OnMouse(wxMouseEvent& event)
32 {
33 if(m_EventId == 0) // nothing to do
34 return;
35
36 // this is unintuitive
37 wxClientDC dc(this);
38 PrepareDC( dc );
39
40 m_FindPos.x = dc.DeviceToLogicalX( event.GetX() );
41 m_FindPos.y = dc.DeviceToLogicalY( event.GetY() );
42
43 m_FoundObject = NULL;
44
45 #ifdef WXLAYOUT_DEBUG
46 cerr << "OnMouse: " << m_FindPos.x << ',' << m_FindPos.y << endl;
47 #endif
48 Refresh();
49 if(m_FoundObject)
50 {
51 if(m_EventId)
52 {
53 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, m_EventId);
54 commandEvent.SetEventObject( this );
55 commandEvent.SetClientData((char *)m_FoundObject);
56 m_ClickPosition = wxPoint(event.GetX(), event.GetY());
57 GetEventHandler()->ProcessEvent(commandEvent);
58 }
59 }
60 }
61
62 /*
63 * some simple keyboard handling
64 */
65 void
66 wxLayoutWindow::OnChar(wxKeyEvent& event)
67 {
68 if(! m_llist.IsEditable()) // do nothing
69 {
70 event.Skip();
71 return;
72 }
73
74 long keyCode = event.KeyCode();
75 wxPoint p;
76
77 switch(event.KeyCode())
78 {
79 case WXK_RIGHT:
80 m_llist.MoveCursor(1);
81 break;
82 case WXK_LEFT:
83 m_llist.MoveCursor(-1);
84 break;
85 case WXK_UP:
86 m_llist.MoveCursor(0,-1);
87 break;
88 case WXK_DOWN:
89 m_llist.MoveCursor(0,1);
90 break;
91 case WXK_PRIOR:
92 m_llist.MoveCursor(0,-20);
93 break;
94 case WXK_NEXT:
95 m_llist.MoveCursor(0,20);
96 break;
97 case WXK_HOME:
98 p = m_llist.GetCursor();
99 p.x = 0;
100 m_llist.SetCursor(p);
101 break;
102 case WXK_END:
103 p = m_llist.GetCursor();
104 p.x = m_llist.GetLineLength(m_llist.FindCurrentObject(NULL));
105 m_llist.SetCursor(p);
106 break;
107 case WXK_DELETE :
108 m_llist.Delete(1);
109 break;
110 case WXK_BACK: // backspace
111 m_llist.MoveCursor(-1);
112 m_llist.Delete(1);
113 break;
114 case WXK_RETURN:
115 m_llist.LineBreak();
116 break;
117 #ifdef WXLAYOUT_DEBUG
118 case WXK_F1:
119 m_llist.Debug();
120 break;
121 #endif
122 default:
123 if(keyCode < 256 && keyCode >= 32)
124 {
125 String tmp;
126 tmp += keyCode;
127 m_llist.Insert(tmp);
128 }
129 break;
130 }
131 Refresh();
132 UpdateScrollbars();
133 }
134
135 void
136 wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)w) // or: OnDraw(wxDC& dc)
137 {
138 wxPaintDC dc( this ); // only when used as OnPaint for OnDraw we
139 PrepareDC( dc ); // can skip the first two lines
140
141 if(m_EventId) // look for keyclicks
142 m_FoundObject = m_llist.Draw(dc,true,m_FindPos);
143 else
144 m_llist.Draw(dc);
145 if(! m_ScrollbarsSet)
146 {
147 m_ScrollbarsSet = true; // avoid recursion
148 UpdateScrollbars();
149 }
150 }
151
152 void
153 wxLayoutWindow::UpdateScrollbars(void)
154 {
155 CoordType
156 max_x, max_y, lineHeight;
157
158 m_llist.GetSize(&max_x, &max_y, &lineHeight);
159 SetScrollbars(10, lineHeight, max_x/10+1, max_y/lineHeight+1);
160 EnableScrolling(true,true);
161 }
162
163 void
164 wxLayoutWindow::Print(void)
165 {
166 VAR(wxThePrintSetupData);
167
168 wxPostScriptDC dc("layout.ps",true,this);
169 if (dc.Ok() && dc.StartDoc((char *)_("Printing message...")))
170 {
171 //dc.SetUserScale(1.0, 1.0);
172 m_llist.Draw(dc);
173 dc.EndDoc();
174 }
175 }