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