summaryrefslogtreecommitdiff
path: root/setedit/mainsrc/editwind.cc
blob: 3bc744a9bfd0cd0692d8c6f24d18416b8df1ccd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Copyright (C) 1996-2003 by Salvador E. Tropea (SET),
   see copyrigh file for details */
// That's the first include because is used to configure the editor.
#include "ceditint.h"

#define Uses_TFrame
#define Uses_TWindow
#define Uses_TStreamableClass
#define Uses_TStreamable
#define Uses_opstream
#define Uses_ipstream
#define Uses_TStringCollection
#define Uses_TPalette
#define Uses_TScreen

#define Uses_TCEditWindow
#define Uses_TCEditor_Commands
#include <ceditor.h>

//  Palette management added by Robert to allow the customization of the
// palette.
#define cpBlueEditWindow "\x08\x09\x0A\x0B\x0C\x40\x41\x42\x43\x44\x45\x46"\
                         "\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x76"\
                         "\x77\x78\x79\x7A\x7B\x7C"
#define cpCyanEditWindow "\x10\x11\x12\x13\x14\x52\x53\x54\x55\x56\x57\x58"\
                         "\x59\x5A\x5B\x5C\x5D\x5E\x5F\x60\x61\x62\x63\x76"\
                         "\x77\x78\x79\x7A\x7B\x7C"
#define cpGrayEditWindow "\x18\x19\x1A\x1B\x1C\x64\x65\x66\x67\x68\x69\x6A"\
                         "\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76"\
                         "\x77\x78\x79\x7A\x7B\x7C"

const int   TCEditWindow::ResumeVersion=6;
const char *TCEditWindow::clipboardTitle=__("Clipboard");
const char *TCEditWindow::untitled=__("Untitled");
stTVIntl   *TCEditWindow::iClipboardTitle=NULL;
stTVIntl   *TCEditWindow::iUntitled=NULL;

TPalette & TCEditWindow::getPalette() const
{
  static TPalette blue(cpBlueEditWindow,sizeof(cpBlueEditWindow)-1);
  static TPalette cyan(cpCyanEditWindow,sizeof(cpCyanEditWindow)-1);
  static TPalette gray(cpGrayEditWindow,sizeof(cpGrayEditWindow)-1);
  static TPalette * palettes[] =
  {
    &blue,
    &cyan,
    &gray
  };
  return *(palettes[palette]);
}

const TPoint minEditWinSize = {24, 6};

TCEditWindow::TCEditWindow( const TRect& bounds,
                            const char *fileName,
                            int aNumber,
                            Boolean openRO ) :
    TWindowInit( &TCEditWindow::initFrame ),
    TWindow( bounds, 0, aNumber )
{
    options |= ofTileable;

    TScrollBar *hScrollBar =
        new TScrollBar( TRect( 18, size.y - 1, size.x - 2, size.y ) );
    hScrollBar->hide();
    insert(hScrollBar);

    TScrollBar *vScrollBar =
        new TScrollBar( TRect( size.x - 1, 1, size.x, size.y - 1 ) );
    vScrollBar->hide();
    insert(vScrollBar);

    TSIndicator *indicator =
        new TSIndicator( TRect( 2, size.y - 1, 16, size.y ) );
    indicator->hide();
    insert(indicator);


    TRect r( getExtent() );
    r.grow(-1, -1);
    editor=new TCEditor(r,hScrollBar,vScrollBar,indicator,fileName,openRO);
    insert(editor);
}


void TCEditWindow::close()
{
 if (editor->isClipboard())
    hide();
 else
    TWindow::close();
}

const char *TCEditWindow::getTitle( short )
{
    if( editor->isClipboard() )
        return TVIntl::getText(clipboardTitle,iClipboardTitle);
    else if( *(editor->fileName) == EOS )
        return TVIntl::getText(untitled,iUntitled);
    else
        return editor->fileName;
}

void TCEditWindow::handleEvent( TEvent& event )
{
    TWindow::handleEvent(event);
    if( event.what == evBroadcast && event.message.command == cmcUpdateTitle )
        {
        if( frame != 0 )
            frame->drawView();
        clearEvent(event);
        }
}

void TCEditWindow::sizeLimits( TPoint& min, TPoint& max )
{
    TWindow::sizeLimits(min, max);
    min = minEditWinSize;
}

void TCEditWindow::write( opstream& os )
{
    TWindow::write( os );
    os << editor;
}

void *TCEditWindow::read( ipstream& is )
{
 TWindow::read( is );
 is >> editor;

 /* It can be done only here because I must remove the old version from the
    Group and that's inserted after the read */
 if (editor->OldIndicator)
   {
    remove(editor->OldIndicator);
    editor->indicator->hide();
    editor->indicator->editor=editor;
    insert(editor->indicator);
   }
 return this;
}

TCEditWindow::TCEditWindow( StreamableInit ) :
    TWindowInit( NULL ),
    TWindow( streamableInit )
{
}

void TCEditWindow::EnlargeSizesResume(EditorResume &r)
{
 int wS=TScreen::getCols(), hS=TScreen::getRows();
 EnlargeSizeResume(r.origin_x,r.origin_y,wS,hS);
 EnlargeSizeResume(r.size_x,r.size_y,wS,hS);
 EnlargeSizeResume(r.zorigin_x,r.zorigin_y,wS,hS);
 EnlargeSizeResume(r.zsize_x,r.zsize_y,wS,hS);
}

void TCEditWindow::ReduceSizesResume(EditorResume &r)
{
 int wS=TScreen::getCols(), hS=TScreen::getRows();
 ReduceSizeResume(r.origin_x,r.origin_y,wS,hS);
 ReduceSizeResume(r.size_x,r.size_y,wS,hS);
 ReduceSizeResume(r.zorigin_x,r.zorigin_y,wS,hS);
 ReduceSizeResume(r.zsize_x,r.zsize_y,wS,hS);
}

void TCEditWindow::EnlargeSizeResume(short &x, short &y, int wS, int hS)
{
 int max=2*wS-1;
 if (x>max) x=max;
 x=(x*0x4000+wS/2)/wS;
 max=2*hS-1;
 if (y>max) y=max;
 y=(y*0x4000+hS/2)/hS;
}

void TCEditWindow::ReduceSizeResume(short &x, short &y, int wS, int hS)
{
 x=(x*wS+0x2000)/0x4000;
 y=(y*hS+0x2000)/0x4000;
}

void TCEditWindow::FillResume(EditorResume &r)
{
 r.version=ResumeVersion;
 r.shl=editor->SyntaxHL;
 r.subshl=editor->GenericSHL;

 r.origin_x=origin.x; r.origin_y=origin.y;
 r.size_x=size.x; r.size_y=size.y;
 r.cursor_x=editor->curPos.x; r.cursor_y=editor->curPos.y;
 r.zorigin_x=zoomRect.a.x; r.zorigin_y=zoomRect.a.y;
 r.zsize_x=zoomRect.b.x; r.zsize_y=zoomRect.b.y;
 r.ed_flags=editor->CompactFlags();
 r.tabSize=editor->tabSize;
 r.indentSize=editor->indentSize;
 r.wrapCol=editor->WrapCol;

 EnlargeSizesResume(r);

 r.extraSize=sizeof(EditorResume)-(sizeof(EditorResumeV5)+sizeof(uint32));
 time(&r.dateResume);
}

void TCEditWindow::ApplyResume(EditorResume &r)
{
 ReduceSizesResume(r);
 moveTo(r.origin_x,r.origin_y);
 growTo(r.size_x,r.size_y);
 zoomRect.a.x=r.zorigin_x;
 zoomRect.a.y=r.zorigin_y;
 zoomRect.b.x=r.zsize_x;
 zoomRect.b.y=r.zsize_y;
 editor->tabSize=r.tabSize;
 editor->indentSize=r.indentSize;
 editor->WrapCol=r.wrapCol;
 editor->MoveCursorTo(r.cursor_x,r.cursor_y);
 editor->trackCursor(True);
 editor->update(ufView); // needed if trackCursor didn't do it
 if (r.shl!=255)
   {
    editor->ExpandFlags(r.ed_flags,False);
    editor->SetHighlightTo((shlState)r.shl,r.subshl);
   }
}

static inline
unsigned MoveFlags(unsigned flags, unsigned mask, unsigned pos)
{
 return (flags & mask) | ((flags & (~mask))<<pos);
}

void TCEditWindow::ReadResume(EditorResume &r, ipstream& is)
{
 uchar version;

 is >> version;
 if (version>ResumeVersion) version=2;
 r.version=version;
 if (version<3)
   { // v1 and v2
    EditorResumeV2 r2;
    is.readBytes(((char *)&r2)+1,sizeof(EditorResumeV2)-1);
    if (r.version==1)
      { // In v0.4.31 I moved some flags so now resumes changed version
       r.ed_flags=MoveFlags(r.ed_flags,0xFF,1);
       r.version=2;
      }
    // Translate it into the v3 structure.
    r.version=3;
    r.shl=r2.shl;
    r.subshl=r2.subshl;
    r.origin_x=r2.origin_x;
    r.origin_y=r2.origin_y;
    r.size_x=r2.size_x;
    r.size_y=r2.size_y;
    r.cursor_x=r2.cursor_x;
    r.cursor_y=r2.cursor_y;
    r.zorigin_x=r2.zorigin_x;
    r.zorigin_y=r2.zorigin_y;
    r.zsize_x=r2.zsize_x;
    r.zsize_y=r2.zsize_y;
    r.ed_flags=r2.ed_flags;
    r.prj_flags=r2.prj_flags;
    // exits as v3
   }
 else
   {
    if (version==3)
       // v3
       is.readBytes(((char *)&r)+1,sizeof(EditorResumeV3)-1);
    else
      {// v4 & v5
       is.readBytes(((char *)&r)+1,sizeof(EditorResumeV5)-1);
       if (version>=6)
         {
          is >> r.extraSize;
          if (r.extraSize==0) r.extraSize=4; // Workaround
          is.readBytes(&r.dateResume,r.extraSize);
         }
      }
   }
 if (r.version==3)
   {
    r.version=4;
    r.tabSize=TCEditor::staticTabSize;
    r.indentSize=TCEditor::staticIndentSize;
    r.wrapCol=TCEditor::staticWrapCol;
   }
 if (r.version==4)
   {
    r.version=5;
    EnlargeSizesResume(r);
   }
 if (r.version==5)
   {
    r.version=6;
    r.extraSize=sizeof(EditorResume)-(sizeof(EditorResumeV5)+sizeof(uint32));
    time(&r.dateResume);
   }
}

void TCEditWindow::SaveResume(EditorResume &r, opstream& os)
{
 // If the file wasn't closed the resume is empty, even the version!
 r.version=ResumeVersion;
 // It must be filled properly.
 // PrjItem *TPrjItemColl::createNewElement(char *name, int flags) doesn't
 // fill it for new (and hence empty) items.
 r.extraSize=sizeof(EditorResume)-(sizeof(EditorResumeV5)+sizeof(uint32));
 os.writeBytes(&r,sizeof(EditorResume));
}

/**[txh]********************************************************************

  Description:
  Used when reading desktop versions older than 0.3.6.
  
***************************************************************************/

void TCEditWindow::FillResumeWith(EditorResume &r, TPoint &origin,
                                  TPoint &size, TPoint &cursor)
{
 r.version=ResumeVersion;
 r.shl=255; // Unknown, is applied to the flags too
 r.zorigin_x=r.origin_x=origin.x;
 r.zorigin_y=r.origin_y=origin.y;
 r.zsize_x=r.size_x=size.x;
 r.zsize_y=r.size_y=size.y;
 r.cursor_x=cursor.x; r.cursor_y=cursor.y;
 r.tabSize=TCEditor::staticTabSize;
 r.indentSize=TCEditor::staticIndentSize;
 r.wrapCol=TCEditor::staticWrapCol;
 r.ed_flags=0;
 EnlargeSizesResume(r);
 r.extraSize=sizeof(EditorResume)-(sizeof(EditorResumeV5)+sizeof(uint32));
 time(&r.dateResume);
}

TCEditWindow::~TCEditWindow()
{
 TVIntl::freeSt(iClipboardTitle);
 TVIntl::freeSt(iUntitled);
}