Mouse.cpp

Zbigniew Rebacz, 01/06/2014 09:55 PM

Download (9.51 KB)

 
1
#include "RichEdit.h"
2

    
3
NAMESPACE_UPP
4

    
5
void RichEdit::CancelMode()
6
{
7
        tabmove.table = 0;
8
        selclick = false;
9
        dropcaret.Clear();
10
}
11

    
12
void RichEdit::MouseWheel(Point p, int zdelta, dword keyflags)
13
{
14
        if(keyflags == K_CTRL && !IsNull(floating_zoom)) {
15
                floating_zoom = minmax(floating_zoom + zdelta / 480.0, 0.5, 10.0);
16
                RefreshLayoutDeep();
17
        }
18
        else
19
                sb.Wheel(zdelta);
20
}
21

    
22
RichHotPos RichEdit::GetHotPos(Point p)
23
{
24
        int x;
25
        PageY py;
26
        GetPagePoint(p, py, x);
27
        return text.GetHotPos(x, py, 4 / GetZoom(), pagesz);
28
}
29

    
30
void RichEdit::SetObjectPercent(int p)
31
{
32
        if(objectpos >= 0) {
33
                RichObject obj = GetObject();
34
                Size sz = obj.GetPhysicalSize() * p / 100;
35
                if(sz.cx > 0 && sz.cy > 0) {
36
                        obj.SetSize(sz);
37
                        obj.KeepRatio(true);
38
                        ReplaceObject(obj);
39
                }
40
        }
41
}
42

    
43
void RichEdit::SetObjectYDelta(int pt)
44
{
45
        if(objectpos >= 0) {
46
                RichObject obj = GetObject();
47
                obj.SetYDelta(pt * 25 / 3);
48
                ReplaceObject(obj);
49
        }
50
}
51

    
52
void RichEdit::SetObjectPos(int pos)
53
{
54
        Rect r = GetObjectRect(cursor);
55
        Rect rr = r.Offseted(GetTextRect().left, -sb);
56
        objectrect = GetObjectRect(pos);
57
        objectpos = cursor;
58
        PlaceCaret();
59
        Refresh(rr);
60
        ReadFormat();
61
}
62

    
63
void RichEdit::LeftDown(Point p, dword flags)
64
{
65
        useraction = true;
66
        NextUndo();
67
        SetFocus();
68
        selclick = false;
69
        tabmove = GetHotPos(p);
70
        if(tabmove.table && tabmove.column >= -2) {
71
                SaveTableFormat(tabmove.table);
72
                SetCapture();
73
                Move(text.GetCellPos(tabmove.table, 0, max(tabmove.column, 0)).pos);
74
                return;
75
        }
76
        int c = GetHotSpot(p);
77
        if(c >= 0 && objectpos >= 0) {
78
                int pos = objectpos;
79
                RectTracker tracker(*this);
80
                RichObject obj = text.GetRichPos(pos).object;
81
                tracker.MinSize(Size(16, 16))
82
                       .MaxSize(GetZoom() * pagesz)
83
                       .Animation()
84
                       .Dashed()
85
                       .KeepRatio(obj.IsKeepRatio());
86
                int tx, ty;
87
                switch(c) {
88
                case 1:
89
                        tracker.SetCursorImage(Image::SizeVert());
90
                        tx = ALIGN_CENTER; ty = ALIGN_BOTTOM;
91
                        break;
92
                case 2:
93
                        tracker.SetCursorImage(Image::SizeHorz());
94
                        tx = ALIGN_RIGHT; ty = ALIGN_CENTER;
95
                        break;
96
                default:
97
                        tracker.SetCursorImage(Image::SizeBottomRight());
98
                        tx = ALIGN_RIGHT; ty = ALIGN_RIGHT;
99
                        break;
100
                }
101
                double zoom = GetZoom().AsDouble();
102
                Size sz = obj.GetSize();
103
                sz.cx = int(zoom * sz.cx + 0.5);
104
                sz.cy = int(zoom * sz.cy + 0.5);
105
                sz = tracker.Track(Rect(objectrect.Offseted(GetTextRect().left, -sb).TopLeft(), sz), tx, ty).Size();
106
                sz.cx = int(sz.cx / zoom + 0.5);
107
                sz.cy = int(sz.cy / zoom + 0.5);
108
                obj.SetSize(sz);
109
                ReplaceObject(obj);
110
        }
111
        else {
112
                c = GetMousePos(p);
113
                if(c >= 0) {
114
                        if(InSelection(c)) {
115
                                selclick = true;
116
                                return;
117
                        }
118
                        Move(c, flags & K_SHIFT);
119
                        mpos = c;
120
                        SetCapture();
121
                        if(cursorp.object && GetObjectRect(cursor).Offseted(GetTextRect().left, -sb).Contains(p))
122
                                SetObjectPos(cursor);
123
                }
124
        }
125
}
126

    
127
void RichEdit::LeftUp(Point p, dword flags)
128
{
129
        useraction = true;
130
        NextUndo();
131
        int c = GetMousePos(p);
132
        int d = c;
133
        if(!HasCapture() && InSelection(d) && selclick) {
134
                CancelSelection();
135
                Move(c);
136
        }
137
        selclick = false;
138
}
139

    
140
void RichEdit::MouseMove(Point p, dword flags)
141
{
142
        useraction = true;
143
        if(HasCapture() && (flags & K_MOUSELEFT)) {
144
                if(tabmove.table && tabmove.column >= 0) {
145
                        RichTable::Format fmt = text.GetTableFormat(tabmove.table);
146
                        if(tabmove.column >= fmt.column.GetCount() - 1)
147
                                return;
148
                        int sum = Sum(fmt.column);
149
                        int nl = 0;
150
                        for(int i = 0; i < tabmove.column; i++)
151
                                nl += fmt.column[i];
152
                        int xn = fmt.column[tabmove.column] + fmt.column[tabmove.column + 1];
153
                        int xl = tabmove.left + tabmove.cx * nl / sum + 12;
154
                        int xh = tabmove.left + tabmove.cx * (nl + xn) / sum - 12;
155
                        if(xl >= xh)
156
                                return;
157
                        int xx = minmax(GetSnapX(p.x) - tabmove.delta, xl, xh) - tabmove.left;
158
                        fmt.column[tabmove.column] = xx * sum / tabmove.cx - nl;
159
                        fmt.column[tabmove.column + 1] = xn - fmt.column[tabmove.column];
160
                        text.SetTableFormat(tabmove.table, fmt);
161
                        Finish();
162
                }
163
                else
164
                if(tabmove.table && tabmove.column == -1) {
165
                        RichTable::Format fmt = text.GetTableFormat(tabmove.table);
166
                        fmt.lm = minmax(GetSnapX(p.x) - tabmove.textleft, 0, max(tabmove.textcx - fmt.rm - 120, 0));
167
                        text.SetTableFormat(tabmove.table, fmt);
168
                        Finish();
169
                }
170
                else
171
                if(tabmove.table && tabmove.column == -2) {
172
                        RichTable::Format fmt = text.GetTableFormat(tabmove.table);
173
                        fmt.rm = minmax(tabmove.textcx - GetSnapX(p.x) + tabmove.textleft, 0, tabmove.textcx - fmt.lm - 120);
174
                        text.SetTableFormat(tabmove.table, fmt);
175
                        Finish();
176
                }
177
                else {
178
                        PageY py = GetPageY(p.y + sb);
179
                        int c;
180
                        if(py > text.GetHeight(pagesz))
181
                                c = GetLength() + 1;
182
                        else
183
                                c = GetNearestPos(GetX(p.x), py);
184
                        if(c != mpos) {
185
                                mpos = -1;
186
                                Move(c, true);
187
                        }
188
                }
189
        }
190
}
191

    
192
void RichEdit::StdBar(Bar& menu)
193
{
194
        int l, h;
195
        Id field;
196
        String fieldparam;
197
        String ofieldparam;
198
        RichObject object;
199
        if(GetSelection(l, h)) {
200
                CutTool(menu);
201
                CopyTool(menu);
202
                PasteTool(menu);
203
        }
204
        else {
205
                if(objectpos >= 0) {
206
                        bar_object = GetObject();
207
                        if(!bar_object) return;
208
                        bar_object.Menu(menu, context);
209
                        if(!menu.IsEmpty())
210
                                menu.Separator();
211
                        Size sz = bar_object.GetPhysicalSize();
212
                        bool b = sz.cx || sz.cy;
213
                        menu.Add(t_("Object position.."), THISBACK(AdjustObjectSize));
214
                        menu.Separator();
215
                        menu.Add(b, "20 %", THISBACK1(SetObjectPercent, 20));
216
                        menu.Add(b, "40 %", THISBACK1(SetObjectPercent, 40));
217
                        menu.Add(b, "60 %", THISBACK1(SetObjectPercent, 60));
218
                        menu.Add(b, "80 %", THISBACK1(SetObjectPercent, 80));
219
                        menu.Add(b, "90 %", THISBACK1(SetObjectPercent, 90));
220
                        menu.Add(b, "100 %", THISBACK1(SetObjectPercent, 100));
221
                        menu.Break();
222
                        menu.Add(t_("3 pt up"), THISBACK1(SetObjectYDelta, -3));
223
                        menu.Add(t_("2 pt up"), THISBACK1(SetObjectYDelta, -2));
224
                        menu.Add(t_("1 pt up"), THISBACK1(SetObjectYDelta, -1));
225
                        menu.Add(t_("Baseline"), THISBACK1(SetObjectYDelta, 0));
226
                        menu.Add(t_("1 pt down"), THISBACK1(SetObjectYDelta, 1));
227
                        menu.Add(t_("2 pt down"), THISBACK1(SetObjectYDelta, 2));
228
                        menu.Add(t_("3 pt down"), THISBACK1(SetObjectYDelta, 3));
229
                        menu.Separator();
230
                        CopyTool(menu);
231
                        CutTool(menu);
232
                }
233
                else {
234
                        RichPos p = cursorp;
235
                        field = p.field;
236
                        bar_fieldparam = p.fieldparam;
237
                        RichPara::FieldType *ft = RichPara::fieldtype().Get(field, NULL);
238
                        if(ft) {
239
                                ft->Menu(menu, &bar_fieldparam);
240
                                if(!menu.IsEmpty())
241
                                        menu.Separator();
242
                                CopyTool(menu);
243
                                CutTool(menu);
244
                        }
245
                        else {
246
                                WString w = GetWordAtCursor();
247
                                if(!w.IsEmpty() && !SpellWord(w, w.GetLength(),
248
                                                              fixedlang ? fixedlang : formatinfo.language)) {
249
                                        menu.Add(t_("Add to user dictionary"), THISBACK(AddUserDict));
250
                                        menu.Separator();
251
                                }
252
                                PasteTool(menu);
253
                                ObjectTool(menu);
254
                        }
255
                }
256
                LoadImageTool(menu);
257
        }
258
}
259

    
260
void RichEdit::RightDown(Point p, dword flags)
261
{
262
        useraction = true;
263
        NextUndo();
264
        MenuBar menu;
265
        int l, h;
266
        Rect ocr = GetCaretRect();
267
        int fieldpos = -1;
268
        Id field;
269
        String ofieldparam;
270
        RichObject o;
271
        bar_object.Clear();
272
        bar_fieldparam = Null;
273
        if(!GetSelection(l, h)) {
274
                LeftDown(p, flags);
275
                if(objectpos >= 0)
276
                        o = bar_object = GetObject();
277
                else {
278
                        RichPos p = cursorp;
279
                        field = p.field;
280
                        bar_fieldparam = p.fieldparam;
281
                        RichPara::FieldType *ft = RichPara::fieldtype().Get(field, NULL);
282
                        if(ft) {
283
                                ofieldparam = bar_fieldparam;
284
                                fieldpos = cursor;
285
                        }
286
                }
287
        }
288
        WhenBar(menu);
289
        Rect r = GetCaretRect();
290
        Refresh(r);
291
        Refresh(ocr);
292
        paintcarect = true;
293
        menu.Execute();
294
        paintcarect = false;
295
        Refresh(r);
296
        if(bar_object && o && o.GetSerialId() != bar_object.GetSerialId())
297
                ReplaceObject(bar_object);
298
        if(fieldpos >= 0 && bar_fieldparam != ofieldparam) {
299
                RichText::FormatInfo f = text.GetFormatInfo(fieldpos, 1);
300
                Remove(fieldpos, 1);
301
                RichPara p;
302
                p.Cat(field, bar_fieldparam, f);
303
                RichText clip;
304
                clip.Cat(p);
305
                Insert(fieldpos, clip, false);
306
                Finish();
307
        }
308
        bar_object.Clear();
309
        bar_fieldparam = Null;
310
}
311

    
312
void RichEdit::LeftDouble(Point p, dword flags)
313
{
314
        NextUndo();
315
        int c = GetMousePos(p);
316
        if(c >= 0) {
317
                if(objectpos == c) {
318
                        RichObject object = GetObject();
319
                        if(!object) return;
320
                        RichObject o = object;
321
                        o.DefaultAction(context);
322
                        if(object.GetSerialId() != o.GetSerialId())
323
                                ReplaceObject(o);
324
                }
325
                else {
326
                        RichPos rp = cursorp;
327
                        RichPara::FieldType *ft = RichPara::fieldtype().Get(rp.field, NULL);
328
                        if(ft) {
329
                                int fieldpos = cursor;
330
                                ft->DefaultAction(&rp.fieldparam);
331
                                RichText::FormatInfo f = text.GetFormatInfo(fieldpos, 1);
332
                                Remove(fieldpos, 1);
333
                                RichPara p;
334
                                p.Cat(rp.field, rp.fieldparam, f);
335
                                RichText clip;
336
                                clip.Cat(p);
337
                                Insert(fieldpos, clip, false);
338
                                Finish();
339
                        }
340
                        else {
341
                                int l, h;
342
                                if(GetWordSelection(c, l, h))
343
                                        SetSelection(l, h);
344
                        }
345
                }
346
        }
347
}
348

    
349
void RichEdit::LeftTriple(Point p, dword flags)
350
{
351
        NextUndo();
352
        int c = GetMousePos(p);
353
        if(c >= 0 && c != objectpos) {
354
                RichPos rp = text.GetRichPos(c);
355
                Select(c - rp.posinpara, rp.paralen + 1);
356
        }
357
}
358

    
359
Image RichEdit::CursorImage(Point p, dword flags)
360
{
361
        if(tablesel)
362
                return Image::Arrow();
363

    
364
        switch(GetHotSpot(p)) {
365
        case 0:
366
                return Image::SizeBottomRight();
367
        case 1:
368
                return Image::SizeVert();
369
        case 2:
370
                return Image::SizeHorz();
371
        default:
372
                if(text.GetRichPos(GetMousePos(p)).object) {
373
                        return Image::Arrow();
374
                }
375
                else
376
                if(HasCapture() && tabmove.table && tabmove.column >= -2)
377
                        return Image::SizeHorz();
378
                else {
379
                        RichHotPos hp = GetHotPos(p);
380
                        if(hp.table > 0)
381
                                return Image::SizeHorz();
382
                        else {
383
                                int c = GetMousePos(p);
384
                                return InSelection(c) && !HasCapture() ? Image::Arrow() : Image::IBeam();
385
                        }
386
                }
387
        }
388
        return Image::Arrow();
389
}
390

    
391
void RichEdit::LeftRepeat(Point p, dword flags)
392
{
393
        NextUndo();
394
        if(HasCapture() && (flags & K_MOUSELEFT)) {
395
                if(p.y < 0)
396
                        MoveUpDown(-1, true);
397
                if(p.y > GetSize().cy)
398
                        MoveUpDown(1, true);
399
        }
400
}
401

    
402
END_UPP_NAMESPACE