Patch #808669 more warngins and unicode fixes
[wxWidgets.git] / samples / stc / edit.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 // File: edit.cpp
3 // Purpose: STC test module
4 // Maintainer: Wyo
5 // Created: 2003-09-01
6 // RCS-ID: $Id$
7 // Copyright: (c) wxGuide
8 // Licence: wxWindows licence
9 //////////////////////////////////////////////////////////////////////////////
10
11 //----------------------------------------------------------------------------
12 // informations
13 //----------------------------------------------------------------------------
14
15
16 //----------------------------------------------------------------------------
17 // headers
18 //----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes <wx/wx.h>.
21 #include <wx/wxprec.h>
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all 'standard' wxWindows headers)
29 #ifndef WX_PRECOMP
30 #include <wx/wx.h>
31 #endif
32
33 //! wxWindows headers
34 #include <wx/file.h> // raw file io support
35 #include <wx/filename.h> // filename support
36
37 //! application headers
38 #include "defsext.h" // additional definitions
39
40 #include "edit.h" // edit module
41
42
43 //----------------------------------------------------------------------------
44 // resources
45 //----------------------------------------------------------------------------
46
47
48 //============================================================================
49 // declarations
50 //============================================================================
51
52
53 //============================================================================
54 // implementation
55 //============================================================================
56
57 //----------------------------------------------------------------------------
58 // Edit
59 //----------------------------------------------------------------------------
60
61 BEGIN_EVENT_TABLE (Edit, wxStyledTextCtrl)
62 // common
63 EVT_SIZE ( Edit::OnSize)
64 // edit
65 EVT_MENU (wxID_CLEAR, Edit::OnEditClear)
66 EVT_MENU (wxID_CUT, Edit::OnEditCut)
67 EVT_MENU (wxID_COPY, Edit::OnEditCopy)
68 EVT_MENU (wxID_PASTE, Edit::OnEditPaste)
69 EVT_MENU (myID_INDENTINC, Edit::OnEditIndentInc)
70 EVT_MENU (myID_INDENTRED, Edit::OnEditIndentRed)
71 EVT_MENU (wxID_SELECTALL, Edit::OnEditSelectAll)
72 EVT_MENU (myID_SELECTLINE, Edit::OnEditSelectLine)
73 EVT_MENU (wxID_REDO, Edit::OnEditRedo)
74 EVT_MENU (wxID_UNDO, Edit::OnEditUndo)
75 // find
76 EVT_MENU (wxID_FIND, Edit::OnFind)
77 EVT_MENU (myID_FINDNEXT, Edit::OnFindNext)
78 EVT_MENU (myID_REPLACE, Edit::OnReplace)
79 EVT_MENU (myID_REPLACENEXT, Edit::OnReplaceNext)
80 EVT_MENU (myID_BRACEMATCH, Edit::OnBraceMatch)
81 EVT_MENU (myID_GOTO, Edit::OnGoto)
82 // view
83 EVT_MENU_RANGE (myID_HILIGHTFIRST, myID_HILIGHTLAST,
84 Edit::OnHilightLang)
85 EVT_MENU (myID_DISPLAYEOL, Edit::OnDisplayEOL)
86 EVT_MENU (myID_INDENTGUIDE, Edit::OnIndentGuide)
87 EVT_MENU (myID_LINENUMBER, Edit::OnLineNumber)
88 EVT_MENU (myID_LONGLINEON, Edit::OnLongLineOn)
89 EVT_MENU (myID_WHITESPACE, Edit::OnWhiteSpace)
90 EVT_MENU (myID_FOLDTOGGLE, Edit::OnFoldToggle)
91 EVT_MENU (myID_OVERTYPE, Edit::OnSetOverType)
92 EVT_MENU (myID_READONLY, Edit::OnSetReadOnly)
93 EVT_MENU (myID_WRAPMODEON, Edit::OnWrapmodeOn)
94 EVT_MENU (myID_CHARSETANSI, Edit::OnUseCharset)
95 EVT_MENU (myID_CHARSETMAC, Edit::OnUseCharset)
96 // extra
97 EVT_MENU (myID_CHANGELOWER, Edit::OnChangeCase)
98 EVT_MENU (myID_CHANGEUPPER, Edit::OnChangeCase)
99 EVT_MENU (myID_CONVERTCR, Edit::OnConvertEOL)
100 EVT_MENU (myID_CONVERTCRLF, Edit::OnConvertEOL)
101 EVT_MENU (myID_CONVERTLF, Edit::OnConvertEOL)
102 // stc
103 EVT_STC_MARGINCLICK (-1, Edit::OnMarginClick)
104 EVT_STC_CHARADDED (-1, Edit::OnCharAdded)
105 END_EVENT_TABLE()
106
107 Edit::Edit (wxWindow *parent, wxWindowID id,
108 const wxPoint &pos,
109 const wxSize &size,
110 long style)
111 : wxStyledTextCtrl (parent, id, pos, size, style) {
112
113 m_filename = _T("");
114
115 m_LineNrID = 0;
116 m_DividerID = 1;
117 m_FoldingID = 2;
118
119 // initialize language
120 m_language = NULL;
121
122 // default font for all styles
123 SetViewEOL (g_CommonPrefs.displayEOLEnable);
124 SetIndentationGuides (g_CommonPrefs.indentGuideEnable);
125 SetEdgeMode (g_CommonPrefs.longLineOnEnable?
126 wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
127 SetViewWhiteSpace (g_CommonPrefs.whiteSpaceEnable?
128 wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
129 SetOvertype (g_CommonPrefs.overTypeInitial);
130 SetReadOnly (g_CommonPrefs.readOnlyInitial);
131 SetWrapMode (g_CommonPrefs.wrapModeInitial?
132 wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
133 wxFont font (10, wxMODERN, wxNORMAL, wxNORMAL);
134 StyleSetFont (wxSTC_STYLE_DEFAULT, font);
135 StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColour (_T("BLACK")));
136 StyleSetBackground (wxSTC_STYLE_DEFAULT, wxColour (_T("WHITE")));
137 StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY")));
138 StyleSetBackground (wxSTC_STYLE_LINENUMBER, wxColour (_T("WHITE")));
139 StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColour (_T("DARK GREY")));
140 InitializePrefs (DEFAULT_LANGUAGE);
141
142 // set visibility
143 SetVisiblePolicy (wxSTC_VISIBLE_STRICT|wxSTC_VISIBLE_SLOP, 1);
144 SetXCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
145 SetYCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
146
147 // markers
148 MarkerDefine (wxSTC_MARKNUM_FOLDER, wxSTC_MARK_DOTDOTDOT, _T("BLACK"), _T("BLACK"));
149 MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, _T("BLACK"), _T("BLACK"));
150 MarkerDefine (wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY, _T("BLACK"), _T("BLACK"));
151 MarkerDefine (wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_DOTDOTDOT, _T("BLACK"), _T("WHITE"));
152 MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_ARROWDOWN, _T("BLACK"), _T("WHITE"));
153 MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, _T("BLACK"), _T("BLACK"));
154 MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, _T("BLACK"), _T("BLACK"));
155
156 // miscelaneous
157 m_LineNrMargin = TextWidth (wxSTC_STYLE_LINENUMBER, _T("_999999"));
158 m_FoldingMargin = 16;
159 SetMarginWidth (m_LineNrID,
160 g_CommonPrefs.lineNumberEnable? m_LineNrMargin: 0);
161 CmdKeyClear (wxSTC_KEY_TAB, 0); // this is done by the menu accelerator key
162 UsePopUp (0);
163 SetLayoutCache (wxSTC_CACHE_PAGE);
164
165 }
166
167 Edit::~Edit () {}
168
169 //----------------------------------------------------------------------------
170 // common event handlers
171 void Edit::OnSize( wxSizeEvent& event ) {
172 int x = GetClientSize().x +
173 (g_CommonPrefs.lineNumberEnable? m_LineNrMargin: 0) +
174 (g_CommonPrefs.foldEnable? m_FoldingMargin: 0);
175 if (x > 0) SetScrollWidth (x);
176 event.Skip();
177 }
178
179 // edit event handlers
180 void Edit::OnEditRedo (wxCommandEvent &WXUNUSED(event)) {
181 if (!CanRedo()) return;
182 Redo ();
183 }
184
185 void Edit::OnEditUndo (wxCommandEvent &WXUNUSED(event)) {
186 if (!CanUndo()) return;
187 Undo ();
188 }
189
190 void Edit::OnEditClear (wxCommandEvent &WXUNUSED(event)) {
191 if (GetReadOnly()) return;
192 Clear ();
193 }
194
195 void Edit::OnEditCut (wxCommandEvent &WXUNUSED(event)) {
196 if (GetReadOnly() || (GetSelectionEnd()-GetSelectionStart() <= 0)) return;
197 Cut ();
198 }
199
200 void Edit::OnEditCopy (wxCommandEvent &WXUNUSED(event)) {
201 if (GetSelectionEnd()-GetSelectionStart() <= 0) return;
202 Copy ();
203 }
204
205 void Edit::OnEditPaste (wxCommandEvent &WXUNUSED(event)) {
206 if (!CanPaste()) return;
207 Paste ();
208 }
209
210 void Edit::OnFind (wxCommandEvent &WXUNUSED(event)) {
211 }
212
213 void Edit::OnFindNext (wxCommandEvent &WXUNUSED(event)) {
214 }
215
216 void Edit::OnReplace (wxCommandEvent &WXUNUSED(event)) {
217 }
218
219 void Edit::OnReplaceNext (wxCommandEvent &WXUNUSED(event)) {
220 }
221
222 void Edit::OnBraceMatch (wxCommandEvent &WXUNUSED(event)) {
223 int min = GetCurrentPos ();
224 int max = BraceMatch (min);
225 if (max > (min+1)) {
226 BraceHighlight (min+1, max);
227 SetSelection (min+1, max);
228 }else{
229 BraceBadLight (min);
230 }
231 }
232
233 void Edit::OnGoto (wxCommandEvent &WXUNUSED(event)) {
234 }
235
236 void Edit::OnEditIndentInc (wxCommandEvent &WXUNUSED(event)) {
237 CmdKeyExecute (wxSTC_CMD_TAB);
238 }
239
240 void Edit::OnEditIndentRed (wxCommandEvent &WXUNUSED(event)) {
241 CmdKeyExecute (wxSTC_CMD_DELETEBACK);
242 }
243
244 void Edit::OnEditSelectAll (wxCommandEvent &WXUNUSED(event)) {
245 SetSelection (0, GetTextLength ());
246 }
247
248 void Edit::OnEditSelectLine (wxCommandEvent &WXUNUSED(event)) {
249 int lineStart = PositionFromLine (GetCurrentLine());
250 int lineEnd = PositionFromLine (GetCurrentLine() + 1);
251 SetSelection (lineStart, lineEnd);
252 }
253
254 void Edit::OnHilightLang (wxCommandEvent &event) {
255 InitializePrefs (g_LanguagePrefs [event.GetId() - myID_HILIGHTFIRST].name);
256 }
257
258 void Edit::OnDisplayEOL (wxCommandEvent &WXUNUSED(event)) {
259 SetViewEOL (!GetViewEOL());
260 }
261
262 void Edit::OnIndentGuide (wxCommandEvent &WXUNUSED(event)) {
263 SetIndentationGuides (!GetIndentationGuides());
264 }
265
266 void Edit::OnLineNumber (wxCommandEvent &WXUNUSED(event)) {
267 SetMarginWidth (m_LineNrID,
268 GetMarginWidth (m_LineNrID) == 0? m_LineNrMargin: 0);
269 }
270
271 void Edit::OnLongLineOn (wxCommandEvent &WXUNUSED(event)) {
272 SetEdgeMode (GetEdgeMode() == 0? wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
273 }
274
275 void Edit::OnWhiteSpace (wxCommandEvent &WXUNUSED(event)) {
276 SetViewWhiteSpace (GetViewWhiteSpace() == 0?
277 wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
278 }
279
280 void Edit::OnFoldToggle (wxCommandEvent &WXUNUSED(event)) {
281 ToggleFold (GetFoldParent(GetCurrentLine()));
282 }
283
284 void Edit::OnSetOverType (wxCommandEvent &WXUNUSED(event)) {
285 SetOvertype (!GetOvertype());
286 }
287
288 void Edit::OnSetReadOnly (wxCommandEvent &WXUNUSED(event)) {
289 SetReadOnly (!GetReadOnly());
290 }
291
292 void Edit::OnWrapmodeOn (wxCommandEvent &WXUNUSED(event)) {
293 SetWrapMode (GetWrapMode() == 0? wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
294 }
295
296 void Edit::OnUseCharset (wxCommandEvent &event) {
297 int Nr;
298 int charset = GetCodePage();
299 switch (event.GetId()) {
300 case myID_CHARSETANSI: {charset = wxSTC_CHARSET_ANSI; break;}
301 case myID_CHARSETMAC: {charset = wxSTC_CHARSET_ANSI; break;}
302 }
303 for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {
304 StyleSetCharacterSet (Nr, charset);
305 }
306 SetCodePage (charset);
307 }
308
309 void Edit::OnChangeCase (wxCommandEvent &event) {
310 switch (event.GetId()) {
311 case myID_CHANGELOWER: {
312 CmdKeyExecute (wxSTC_CMD_LOWERCASE);
313 break;
314 }
315 case myID_CHANGEUPPER: {
316 CmdKeyExecute (wxSTC_CMD_UPPERCASE);
317 break;
318 }
319 }
320 }
321
322 void Edit::OnConvertEOL (wxCommandEvent &event) {
323 int eolMode = GetEOLMode();
324 switch (event.GetId()) {
325 case myID_CONVERTCR: { eolMode = wxSTC_EOL_CR; break;}
326 case myID_CONVERTCRLF: { eolMode = wxSTC_EOL_CRLF; break;}
327 case myID_CONVERTLF: { eolMode = wxSTC_EOL_LF; break;}
328 }
329 ConvertEOLs (eolMode);
330 SetEOLMode (eolMode);
331 }
332
333 //! misc
334 void Edit::OnMarginClick (wxStyledTextEvent &event) {
335 if (event.GetMargin() == 2) {
336 int lineClick = LineFromPosition (event.GetPosition());
337 int levelClick = GetFoldLevel (lineClick);
338 if ((levelClick & wxSTC_FOLDLEVELHEADERFLAG) > 0) {
339 ToggleFold (lineClick);
340 }
341 }
342 }
343
344 void Edit::OnCharAdded (wxStyledTextEvent &event) {
345 char chr = event.GetKey();
346 int currentLine = GetCurrentLine();
347 // Change this if support for mac files with \r is needed
348 if (chr == '\n') {
349 int lineInd = 0;
350 if (currentLine > 0) {
351 lineInd = GetLineIndentation(currentLine - 1);
352 }
353 if (lineInd == 0) return;
354 SetLineIndentation (currentLine, lineInd);
355 GotoPos(PositionFromLine (currentLine) + lineInd);
356 }
357 }
358
359
360 //----------------------------------------------------------------------------
361 // private functions
362 wxString Edit::DeterminePrefs (const wxString &filename) {
363
364 LanguageInfo const* curInfo = NULL;
365
366 // determine language from filepatterns
367 int languageNr;
368 for (languageNr = 0; languageNr < g_LanguagePrefsSize; languageNr++) {
369 curInfo = &g_LanguagePrefs [languageNr];
370 wxString filepattern = curInfo->filepattern;
371 filepattern.Lower();
372 while (!filepattern.IsEmpty()) {
373 wxString cur = filepattern.BeforeFirst (';');
374 if ((cur == filename) ||
375 (cur == (filename.BeforeLast ('.') + _T(".*"))) ||
376 (cur == (_T("*.") + filename.AfterLast ('.')))) {
377 return curInfo->name;
378 }
379 filepattern = filepattern.AfterFirst (';');
380 }
381 }
382 return wxEmptyString;
383
384 }
385
386 bool Edit::InitializePrefs (const wxString &name) {
387
388 // initialize styles
389 StyleClearAll();
390 LanguageInfo const* curInfo = NULL;
391
392 // determine language
393 bool found = false;
394 int languageNr;
395 for (languageNr = 0; languageNr < g_LanguagePrefsSize; languageNr++) {
396 curInfo = &g_LanguagePrefs [languageNr];
397 if (curInfo->name == name) {
398 found = true;
399 break;
400 }
401 }
402 if (!found) return false;
403
404 // set lexer and language
405 SetLexer (curInfo->lexer);
406 m_language = curInfo;
407
408 // set margin for line numbers
409 SetMarginType (m_LineNrID, wxSTC_MARGIN_NUMBER);
410 StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY")));
411 StyleSetBackground (wxSTC_STYLE_LINENUMBER, wxColour (_T("WHITE")));
412 SetMarginWidth (m_LineNrID,
413 g_CommonPrefs.lineNumberEnable? m_LineNrMargin: 0);
414
415 // default fonts for all styles!
416 int Nr;
417 for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {
418 wxFont font (10, wxMODERN, wxNORMAL, wxNORMAL);
419 StyleSetFont (Nr, font);
420 }
421
422 // set common styles
423 StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColour (_T("DARK GREY")));
424 StyleSetForeground (wxSTC_STYLE_INDENTGUIDE, wxColour (_T("DARK GREY")));
425
426 // initialize settings
427 if (g_CommonPrefs.syntaxEnable) {
428 int keywordnr = 0;
429 for (Nr = 0; Nr < STYLE_TYPES_COUNT; Nr++) {
430 if (curInfo->styles[Nr].type == -1) continue;
431 const StyleInfo &curType = g_StylePrefs [curInfo->styles[Nr].type];
432 wxFont font (curType.fontsize, wxMODERN, wxNORMAL, wxNORMAL, false,
433 curType.fontname);
434 StyleSetFont (Nr, font);
435 if (curType.foreground) {
436 StyleSetForeground (Nr, wxColour (curType.foreground));
437 }
438 if (curType.background) {
439 StyleSetBackground (Nr, wxColour (curType.background));
440 }
441 StyleSetBold (Nr, (curType.fontstyle & mySTC_STYLE_BOLD) > 0);
442 StyleSetItalic (Nr, (curType.fontstyle & mySTC_STYLE_ITALIC) > 0);
443 StyleSetUnderline (Nr, (curType.fontstyle & mySTC_STYLE_UNDERL) > 0);
444 StyleSetVisible (Nr, (curType.fontstyle & mySTC_STYLE_HIDDEN) == 0);
445 StyleSetCase (Nr, curType.lettercase);
446 const wxChar *pwords = curInfo->styles[Nr].words;
447 if (pwords) {
448 SetKeyWords (keywordnr, pwords);
449 keywordnr += 1;
450 }
451 }
452 }
453
454 // set margin as unused
455 SetMarginType (m_DividerID, wxSTC_MARGIN_SYMBOL);
456 SetMarginWidth (m_DividerID, 0);
457 SetMarginSensitive (m_DividerID, false);
458
459 // folding
460 SetMarginType (m_FoldingID, wxSTC_MARGIN_SYMBOL);
461 SetMarginMask (m_FoldingID, wxSTC_MASK_FOLDERS);
462 StyleSetBackground (m_FoldingID, wxColour (_T("WHITE")));
463 SetMarginWidth (m_FoldingID, 0);
464 SetMarginSensitive (m_FoldingID, false);
465 if (g_CommonPrefs.foldEnable) {
466 SetMarginWidth (m_FoldingID, curInfo->folds != 0? m_FoldingMargin: 0);
467 SetMarginSensitive (m_FoldingID, curInfo->folds != 0);
468 SetProperty (_T("fold"), curInfo->folds != 0? _T("1"): _T("0"));
469 SetProperty (_T("fold.comment"),
470 (curInfo->folds & mySTC_FOLD_COMMENT) > 0? _T("1"): _T("0"));
471 SetProperty (_T("fold.compact"),
472 (curInfo->folds & mySTC_FOLD_COMPACT) > 0? _T("1"): _T("0"));
473 SetProperty (_T("fold.preprocessor"),
474 (curInfo->folds & mySTC_FOLD_PREPROC) > 0? _T("1"): _T("0"));
475 SetProperty (_T("fold.html"),
476 (curInfo->folds & mySTC_FOLD_HTML) > 0? _T("1"): _T("0"));
477 SetProperty (_T("fold.html.preprocessor"),
478 (curInfo->folds & mySTC_FOLD_HTMLPREP) > 0? _T("1"): _T("0"));
479 SetProperty (_T("fold.comment.python"),
480 (curInfo->folds & mySTC_FOLD_COMMENTPY) > 0? _T("1"): _T("0"));
481 SetProperty (_T("fold.quotes.python"),
482 (curInfo->folds & mySTC_FOLD_QUOTESPY) > 0? _T("1"): _T("0"));
483 }
484 SetFoldFlags (wxSTC_FOLDFLAG_LINEBEFORE_CONTRACTED |
485 wxSTC_FOLDFLAG_LINEAFTER_CONTRACTED);
486
487 // set spaces and indention
488 SetTabWidth (4);
489 SetUseTabs (false);
490 SetTabIndents (true);
491 SetBackSpaceUnIndents (true);
492 SetIndent (g_CommonPrefs.indentEnable? 4: 0);
493
494 // others
495 SetViewEOL (g_CommonPrefs.displayEOLEnable);
496 SetIndentationGuides (g_CommonPrefs.indentGuideEnable);
497 SetEdgeColumn (80);
498 SetEdgeMode (g_CommonPrefs.longLineOnEnable? wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
499 SetViewWhiteSpace (g_CommonPrefs.whiteSpaceEnable?
500 wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
501 SetOvertype (g_CommonPrefs.overTypeInitial);
502 SetReadOnly (g_CommonPrefs.readOnlyInitial);
503 SetWrapMode (g_CommonPrefs.wrapModeInitial?
504 wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
505
506 return true;
507 }
508
509 bool Edit::LoadFile () {
510
511 // get filname
512 if (!m_filename) {
513 wxFileDialog dlg (this, _T("Open file"), _T(""), _T(""),
514 _T("Any file (*)|*"), wxOPEN | wxFILE_MUST_EXIST | wxCHANGE_DIR);
515 if (dlg.ShowModal() != wxID_OK) return false;
516 m_filename = dlg.GetPath();
517 }
518
519 // load file
520 return LoadFile (m_filename);
521 }
522
523 bool Edit::LoadFile (const wxString &filename) {
524
525 // load file in edit and clear undo
526 if (!filename.IsEmpty()) m_filename = filename;
527 // wxFile file (m_filename);
528 // if (!file.IsOpened()) return false;
529 ClearAll ();
530 // long lng = file.Length ();
531 // if (lng > 0) {
532 // wxString buf;
533 // wxChar *buff = buf.GetWriteBuf (lng);
534 // file.Read (buff, lng);
535 // buf.UngetWriteBuf ();
536 // InsertText (0, buf);
537 // }
538 // file.Close();
539
540 wxStyledTextCtrl::LoadFile(m_filename);
541
542 EmptyUndoBuffer();
543
544 // determine lexer language
545 wxFileName fname (m_filename);
546 InitializePrefs (DeterminePrefs (fname.GetFullName()));
547
548 return true;
549 }
550
551 bool Edit::SaveFile () {
552
553 // return if no change
554 if (!Modified()) return true;
555
556 // get filname
557 if (!m_filename) {
558 wxFileDialog dlg (this, _T("Save file"), _T(""), _T(""), _T("Any file (*)|*"),
559 wxSAVE | wxOVERWRITE_PROMPT);
560 if (dlg.ShowModal() != wxID_OK) return false;
561 m_filename = dlg.GetPath();
562 }
563
564 // save file
565 return SaveFile (m_filename);
566 }
567
568 bool Edit::SaveFile (const wxString &filename) {
569
570 // return if no change
571 if (!Modified()) return true;
572
573 // // save edit in file and clear undo
574 // if (!filename.IsEmpty()) m_filename = filename;
575 // wxFile file (m_filename, wxFile::write);
576 // if (!file.IsOpened()) return false;
577 // wxString buf = GetText();
578 // bool okay = file.Write (buf);
579 // file.Close();
580 // if (!okay) return false;
581 // EmptyUndoBuffer();
582 // SetSavePoint();
583
584 // return true;
585
586 return wxStyledTextCtrl::SaveFile(filename);
587
588 }
589
590 bool Edit::Modified () {
591
592 // return modified state
593 return (GetModify() && !GetReadOnly());
594 }
595
596 //----------------------------------------------------------------------------
597 // EditProperties
598 //----------------------------------------------------------------------------
599
600 EditProperties::EditProperties (Edit *edit,
601 long style)
602 : wxDialog (edit, -1, wxEmptyString,
603 wxDefaultPosition, wxDefaultSize,
604 style | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
605
606 // sets the application title
607 SetTitle (_("Properties"));
608 wxString text;
609
610 // fullname
611 wxBoxSizer *fullname = new wxBoxSizer (wxHORIZONTAL);
612 fullname->Add (10, 0);
613 fullname->Add (new wxStaticText (this, -1, _("Full filename"),
614 wxDefaultPosition, wxSize(80, -1)),
615 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL);
616 fullname->Add (new wxStaticText (this, -1, edit->GetFilename()),
617 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL);
618
619 // text info
620 wxGridSizer *textinfo = new wxGridSizer (4, 0, 2);
621 textinfo->Add (new wxStaticText (this, -1, _("Language"),
622 wxDefaultPosition, wxSize(80, -1)),
623 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
624 textinfo->Add (new wxStaticText (this, -1, edit->m_language->name),
625 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
626 textinfo->Add (new wxStaticText (this, -1, _("Lexer-ID: "),
627 wxDefaultPosition, wxSize(80, -1)),
628 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
629 text = wxString::Format (_T("%d"), edit->GetLexer());
630 textinfo->Add (new wxStaticText (this, -1, text),
631 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
632 wxString EOLtype = _T("");
633 switch (edit->GetEOLMode()) {
634 case wxSTC_EOL_CR: {EOLtype = _T("CR (Unix)"); break; }
635 case wxSTC_EOL_CRLF: {EOLtype = _T("CRLF (Windows)"); break; }
636 case wxSTC_EOL_LF: {EOLtype = _T("CR (Macintosh)"); break; }
637 }
638 textinfo->Add (new wxStaticText (this, -1, _("Line endings"),
639 wxDefaultPosition, wxSize(80, -1)),
640 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
641 textinfo->Add (new wxStaticText (this, -1, EOLtype),
642 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
643
644 // text info box
645 wxStaticBoxSizer *textinfos = new wxStaticBoxSizer (
646 new wxStaticBox (this, -1, _("Informations")),
647 wxVERTICAL);
648 textinfos->Add (textinfo, 0, wxEXPAND);
649 textinfos->Add (0, 6);
650
651 // statistic
652 wxGridSizer *statistic = new wxGridSizer (4, 0, 2);
653 statistic->Add (new wxStaticText (this, -1, _("Total lines"),
654 wxDefaultPosition, wxSize(80, -1)),
655 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
656 text = wxString::Format (_T("%d"), edit->GetLineCount());
657 statistic->Add (new wxStaticText (this, -1, text),
658 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
659 statistic->Add (new wxStaticText (this, -1, _("Total chars"),
660 wxDefaultPosition, wxSize(80, -1)),
661 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
662 text = wxString::Format (_T("%d"), edit->GetTextLength());
663 statistic->Add (new wxStaticText (this, -1, text),
664 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
665 statistic->Add (new wxStaticText (this, -1, _("Current line"),
666 wxDefaultPosition, wxSize(80, -1)),
667 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
668 text = wxString::Format (_T("%d"), edit->GetCurrentLine());
669 statistic->Add (new wxStaticText (this, -1, text),
670 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
671 statistic->Add (new wxStaticText (this, -1, _("Current pos"),
672 wxDefaultPosition, wxSize(80, -1)),
673 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
674 text = wxString::Format (_T("%d"), edit->GetCurrentPos());
675 statistic->Add (new wxStaticText (this, -1, text),
676 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
677
678 // char/line statistics
679 wxStaticBoxSizer *statistics = new wxStaticBoxSizer (
680 new wxStaticBox (this, -1, _("Statistics")),
681 wxVERTICAL);
682 statistics->Add (statistic, 0, wxEXPAND);
683 statistics->Add (0, 6);
684
685 // total pane
686 wxBoxSizer *totalpane = new wxBoxSizer (wxVERTICAL);
687 totalpane->Add (fullname, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10);
688 totalpane->Add (0, 6);
689 totalpane->Add (textinfos, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
690 totalpane->Add (0, 10);
691 totalpane->Add (statistics, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
692 totalpane->Add (0, 6);
693 wxButton *okButton = new wxButton (this, wxID_OK, _("OK"));
694 okButton->SetDefault();
695 totalpane->Add (okButton, 0, wxALIGN_CENTER | wxALL, 10);
696
697 SetSizerAndFit (totalpane);
698
699 ShowModal();
700 }
701
702 //----------------------------------------------------------------------------
703 // EditPrint
704 //----------------------------------------------------------------------------
705
706 EditPrint::EditPrint (Edit *edit, wxChar *title)
707 : wxPrintout(title) {
708 m_edit = edit;
709 m_printed = 0;
710
711 }
712
713 bool EditPrint::OnPrintPage (int page) {
714
715 wxDC *dc = GetDC();
716 if (!dc) return false;
717
718 // scale DC
719 PrintScaling (dc);
720
721 // print page
722 if (page == 1) m_printed = 0;
723 m_printed = m_edit->FormatRange (1, m_printed, m_edit->GetLength(),
724 dc, dc, m_printRect, m_pageRect);
725
726 return true;
727 }
728
729 bool EditPrint::OnBeginDocument (int startPage, int endPage) {
730
731 if (!wxPrintout::OnBeginDocument (startPage, endPage)) {
732 return false;
733 }
734
735 return true;
736 }
737
738 void EditPrint::GetPageInfo (int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) {
739
740 // initialize values
741 *minPage = 0;
742 *maxPage = 0;
743 *selPageFrom = 0;
744 *selPageTo = 0;
745
746 // scale DC if possible
747 wxDC *dc = GetDC();
748 if (!dc) return;
749 PrintScaling (dc);
750
751 // get print page informations and convert to printer pixels
752 wxSize ppiScr;
753 GetPPIScreen (&ppiScr.x, &ppiScr.y);
754 wxSize page = g_pageSetupData->GetPaperSize();
755 page.x = static_cast<int> (page.x * ppiScr.x / 25.4);
756 page.y = static_cast<int> (page.y * ppiScr.y / 25.4);
757 m_pageRect = wxRect (0,
758 0,
759 page.x,
760 page.y);
761
762 // get margins informations and convert to printer pixels
763 int top = 25; // default 25
764 int bottom = 25; // default 25
765 int left = 20; // default 20
766 int right = 20; // default 20
767 wxPoint (top, left) = g_pageSetupData->GetMarginTopLeft();
768 wxPoint (bottom, right) = g_pageSetupData->GetMarginBottomRight();
769 top = static_cast<int> (top * ppiScr.y / 25.4);
770 bottom = static_cast<int> (bottom * ppiScr.y / 25.4);
771 left = static_cast<int> (left * ppiScr.x / 25.4);
772 right = static_cast<int> (right * ppiScr.x / 25.4);
773 m_printRect = wxRect (left,
774 top,
775 page.x - (left + right),
776 page.y - (top + bottom));
777
778 // count pages
779 while (HasPage (*maxPage)) {
780 m_printed = m_edit->FormatRange (0, m_printed, m_edit->GetLength(),
781 dc, dc, m_printRect, m_pageRect);
782 *maxPage += 1;
783 }
784 if (*maxPage > 0) *minPage = 1;
785 *selPageFrom = *minPage;
786 *selPageTo = *maxPage;
787 }
788
789 bool EditPrint::HasPage (int WXUNUSED(page)) {
790
791 return (m_printed < m_edit->GetLength());
792 }
793
794 bool EditPrint::PrintScaling (wxDC *dc){
795
796 // check for dc, return if none
797 if (!dc) return false;
798
799 // get printer and screen sizing values
800 wxSize ppiScr;
801 GetPPIScreen (&ppiScr.x, &ppiScr.y);
802 if (ppiScr.x == 0) { // most possible guess 96 dpi
803 ppiScr.x = 96;
804 ppiScr.y = 96;
805 }
806 wxSize ppiPrt;
807 GetPPIPrinter (&ppiPrt.x, &ppiPrt.y);
808 if (ppiPrt.x == 0) { // scaling factor to 1
809 ppiPrt.x = ppiScr.x;
810 ppiPrt.y = ppiScr.y;
811 }
812 wxSize dcSize = dc->GetSize();
813 wxSize pageSize;
814 GetPageSizePixels (&pageSize.x, &pageSize.y);
815
816 // set user scale
817 float scale_x = (float)(ppiPrt.x * dcSize.x) /
818 (float)(ppiScr.x * pageSize.x);
819 float scale_y = (float)(ppiPrt.y * dcSize.y) /
820 (float)(ppiScr.y * pageSize.y);
821 dc->SetUserScale (scale_x, scale_y);
822
823 return true;
824 }
825