1 //////////////////////////////////////////////////////////////////////////////
2 // File: contrib/samples/stc/edit.cpp
3 // Purpose: STC test module
7 // Copyright: (c) wxGuide
8 // Licence: wxWindows licence
9 //////////////////////////////////////////////////////////////////////////////
11 //----------------------------------------------------------------------------
13 //----------------------------------------------------------------------------
16 //----------------------------------------------------------------------------
18 //----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all 'standard' wxWidgets headers)
31 #include "wx/textdlg.h"
35 #include "wx/file.h" // raw file io support
36 #include "wx/filename.h" // filename support
38 //! application headers
39 #include "defsext.h" // additional definitions
41 #include "edit.h" // edit module
44 //----------------------------------------------------------------------------
46 //----------------------------------------------------------------------------
49 //============================================================================
51 //============================================================================
53 // The (uniform) style used for the annotations.
54 const int ANNOTATION_STYLE
= wxSTC_STYLE_LASTPREDEFINED
+ 1;
56 //============================================================================
58 //============================================================================
60 //----------------------------------------------------------------------------
62 //----------------------------------------------------------------------------
64 BEGIN_EVENT_TABLE (Edit
, wxStyledTextCtrl
)
66 EVT_SIZE ( Edit::OnSize
)
68 EVT_MENU (wxID_CLEAR
, Edit::OnEditClear
)
69 EVT_MENU (wxID_CUT
, Edit::OnEditCut
)
70 EVT_MENU (wxID_COPY
, Edit::OnEditCopy
)
71 EVT_MENU (wxID_PASTE
, Edit::OnEditPaste
)
72 EVT_MENU (myID_INDENTINC
, Edit::OnEditIndentInc
)
73 EVT_MENU (myID_INDENTRED
, Edit::OnEditIndentRed
)
74 EVT_MENU (wxID_SELECTALL
, Edit::OnEditSelectAll
)
75 EVT_MENU (myID_SELECTLINE
, Edit::OnEditSelectLine
)
76 EVT_MENU (wxID_REDO
, Edit::OnEditRedo
)
77 EVT_MENU (wxID_UNDO
, Edit::OnEditUndo
)
79 EVT_MENU (wxID_FIND
, Edit::OnFind
)
80 EVT_MENU (myID_FINDNEXT
, Edit::OnFindNext
)
81 EVT_MENU (myID_REPLACE
, Edit::OnReplace
)
82 EVT_MENU (myID_REPLACENEXT
, Edit::OnReplaceNext
)
83 EVT_MENU (myID_BRACEMATCH
, Edit::OnBraceMatch
)
84 EVT_MENU (myID_GOTO
, Edit::OnGoto
)
86 EVT_MENU_RANGE (myID_HILIGHTFIRST
, myID_HILIGHTLAST
,
88 EVT_MENU (myID_DISPLAYEOL
, Edit::OnDisplayEOL
)
89 EVT_MENU (myID_INDENTGUIDE
, Edit::OnIndentGuide
)
90 EVT_MENU (myID_LINENUMBER
, Edit::OnLineNumber
)
91 EVT_MENU (myID_LONGLINEON
, Edit::OnLongLineOn
)
92 EVT_MENU (myID_WHITESPACE
, Edit::OnWhiteSpace
)
93 EVT_MENU (myID_FOLDTOGGLE
, Edit::OnFoldToggle
)
94 EVT_MENU (myID_OVERTYPE
, Edit::OnSetOverType
)
95 EVT_MENU (myID_READONLY
, Edit::OnSetReadOnly
)
96 EVT_MENU (myID_WRAPMODEON
, Edit::OnWrapmodeOn
)
97 EVT_MENU (myID_CHARSETANSI
, Edit::OnUseCharset
)
98 EVT_MENU (myID_CHARSETMAC
, Edit::OnUseCharset
)
100 EVT_MENU (myID_ANNOTATION_ADD
, Edit::OnAnnotationAdd
)
101 EVT_MENU (myID_ANNOTATION_REMOVE
, Edit::OnAnnotationRemove
)
102 EVT_MENU (myID_ANNOTATION_CLEAR
, Edit::OnAnnotationClear
)
103 EVT_MENU (myID_ANNOTATION_STYLE_HIDDEN
, Edit::OnAnnotationStyle
)
104 EVT_MENU (myID_ANNOTATION_STYLE_STANDARD
, Edit::OnAnnotationStyle
)
105 EVT_MENU (myID_ANNOTATION_STYLE_BOXED
, Edit::OnAnnotationStyle
)
107 EVT_MENU (myID_CHANGELOWER
, Edit::OnChangeCase
)
108 EVT_MENU (myID_CHANGEUPPER
, Edit::OnChangeCase
)
109 EVT_MENU (myID_CONVERTCR
, Edit::OnConvertEOL
)
110 EVT_MENU (myID_CONVERTCRLF
, Edit::OnConvertEOL
)
111 EVT_MENU (myID_CONVERTLF
, Edit::OnConvertEOL
)
113 EVT_STC_MARGINCLICK (wxID_ANY
, Edit::OnMarginClick
)
114 EVT_STC_CHARADDED (wxID_ANY
, Edit::OnCharAdded
)
115 EVT_STC_KEY( wxID_ANY
, Edit::OnKey
)
118 Edit::Edit (wxWindow
*parent
, wxWindowID id
,
122 : wxStyledTextCtrl (parent
, id
, pos
, size
, style
) {
124 m_filename
= wxEmptyString
;
130 // initialize language
133 // Use all the bits in the style byte as styles, not indicators.
136 // default font for all styles
137 SetViewEOL (g_CommonPrefs
.displayEOLEnable
);
138 SetIndentationGuides (g_CommonPrefs
.indentGuideEnable
);
139 SetEdgeMode (g_CommonPrefs
.longLineOnEnable
?
140 wxSTC_EDGE_LINE
: wxSTC_EDGE_NONE
);
141 SetViewWhiteSpace (g_CommonPrefs
.whiteSpaceEnable
?
142 wxSTC_WS_VISIBLEALWAYS
: wxSTC_WS_INVISIBLE
);
143 SetOvertype (g_CommonPrefs
.overTypeInitial
);
144 SetReadOnly (g_CommonPrefs
.readOnlyInitial
);
145 SetWrapMode (g_CommonPrefs
.wrapModeInitial
?
146 wxSTC_WRAP_WORD
: wxSTC_WRAP_NONE
);
147 wxFont
font (10, wxMODERN
, wxNORMAL
, wxNORMAL
);
148 StyleSetFont (wxSTC_STYLE_DEFAULT
, font
);
149 StyleSetForeground (wxSTC_STYLE_DEFAULT
, *wxBLACK
);
150 StyleSetBackground (wxSTC_STYLE_DEFAULT
, *wxWHITE
);
151 StyleSetForeground (wxSTC_STYLE_LINENUMBER
, wxColour (wxT("DARK GREY")));
152 StyleSetBackground (wxSTC_STYLE_LINENUMBER
, *wxWHITE
);
153 StyleSetForeground(wxSTC_STYLE_INDENTGUIDE
, wxColour (wxT("DARK GREY")));
154 InitializePrefs (DEFAULT_LANGUAGE
);
157 SetVisiblePolicy (wxSTC_VISIBLE_STRICT
|wxSTC_VISIBLE_SLOP
, 1);
158 SetXCaretPolicy (wxSTC_CARET_EVEN
|wxSTC_VISIBLE_STRICT
|wxSTC_CARET_SLOP
, 1);
159 SetYCaretPolicy (wxSTC_CARET_EVEN
|wxSTC_VISIBLE_STRICT
|wxSTC_CARET_SLOP
, 1);
162 MarkerDefine (wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_DOTDOTDOT
, wxT("BLACK"), wxT("BLACK"));
163 MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_ARROWDOWN
, wxT("BLACK"), wxT("BLACK"));
164 MarkerDefine (wxSTC_MARKNUM_FOLDERSUB
, wxSTC_MARK_EMPTY
, wxT("BLACK"), wxT("BLACK"));
165 MarkerDefine (wxSTC_MARKNUM_FOLDEREND
, wxSTC_MARK_DOTDOTDOT
, wxT("BLACK"), wxT("WHITE"));
166 MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID
, wxSTC_MARK_ARROWDOWN
, wxT("BLACK"), wxT("WHITE"));
167 MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL
, wxSTC_MARK_EMPTY
, wxT("BLACK"), wxT("BLACK"));
168 MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL
, wxSTC_MARK_EMPTY
, wxT("BLACK"), wxT("BLACK"));
171 AnnotationSetVisible(wxSTC_ANNOTATION_BOXED
);
174 m_LineNrMargin
= TextWidth (wxSTC_STYLE_LINENUMBER
, wxT("_999999"));
175 m_FoldingMargin
= 16;
176 CmdKeyClear (wxSTC_KEY_TAB
, 0); // this is done by the menu accelerator key
177 SetLayoutCache (wxSTC_CACHE_PAGE
);
183 //----------------------------------------------------------------------------
184 // common event handlers
185 void Edit::OnSize( wxSizeEvent
& event
) {
186 int x
= GetClientSize().x
+
187 (g_CommonPrefs
.lineNumberEnable
? m_LineNrMargin
: 0) +
188 (g_CommonPrefs
.foldEnable
? m_FoldingMargin
: 0);
189 if (x
> 0) SetScrollWidth (x
);
193 // edit event handlers
194 void Edit::OnEditRedo (wxCommandEvent
&WXUNUSED(event
)) {
195 if (!CanRedo()) return;
199 void Edit::OnEditUndo (wxCommandEvent
&WXUNUSED(event
)) {
200 if (!CanUndo()) return;
204 void Edit::OnEditClear (wxCommandEvent
&WXUNUSED(event
)) {
205 if (GetReadOnly()) return;
209 void Edit::OnKey (wxStyledTextEvent
&WXUNUSED(event
))
211 wxMessageBox("OnKey");
214 void Edit::OnEditCut (wxCommandEvent
&WXUNUSED(event
)) {
215 if (GetReadOnly() || (GetSelectionEnd()-GetSelectionStart() <= 0)) return;
219 void Edit::OnEditCopy (wxCommandEvent
&WXUNUSED(event
)) {
220 if (GetSelectionEnd()-GetSelectionStart() <= 0) return;
224 void Edit::OnEditPaste (wxCommandEvent
&WXUNUSED(event
)) {
225 if (!CanPaste()) return;
229 void Edit::OnFind (wxCommandEvent
&WXUNUSED(event
)) {
232 void Edit::OnFindNext (wxCommandEvent
&WXUNUSED(event
)) {
235 void Edit::OnReplace (wxCommandEvent
&WXUNUSED(event
)) {
238 void Edit::OnReplaceNext (wxCommandEvent
&WXUNUSED(event
)) {
241 void Edit::OnBraceMatch (wxCommandEvent
&WXUNUSED(event
)) {
242 int min
= GetCurrentPos ();
243 int max
= BraceMatch (min
);
245 BraceHighlight (min
+1, max
);
246 SetSelection (min
+1, max
);
252 void Edit::OnGoto (wxCommandEvent
&WXUNUSED(event
)) {
255 void Edit::OnEditIndentInc (wxCommandEvent
&WXUNUSED(event
)) {
256 CmdKeyExecute (wxSTC_CMD_TAB
);
259 void Edit::OnEditIndentRed (wxCommandEvent
&WXUNUSED(event
)) {
260 CmdKeyExecute (wxSTC_CMD_DELETEBACK
);
263 void Edit::OnEditSelectAll (wxCommandEvent
&WXUNUSED(event
)) {
264 SetSelection (0, GetTextLength ());
267 void Edit::OnEditSelectLine (wxCommandEvent
&WXUNUSED(event
)) {
268 int lineStart
= PositionFromLine (GetCurrentLine());
269 int lineEnd
= PositionFromLine (GetCurrentLine() + 1);
270 SetSelection (lineStart
, lineEnd
);
273 void Edit::OnHilightLang (wxCommandEvent
&event
) {
274 InitializePrefs (g_LanguagePrefs
[event
.GetId() - myID_HILIGHTFIRST
].name
);
277 void Edit::OnDisplayEOL (wxCommandEvent
&WXUNUSED(event
)) {
278 SetViewEOL (!GetViewEOL());
281 void Edit::OnIndentGuide (wxCommandEvent
&WXUNUSED(event
)) {
282 SetIndentationGuides (!GetIndentationGuides());
285 void Edit::OnLineNumber (wxCommandEvent
&WXUNUSED(event
)) {
286 SetMarginWidth (m_LineNrID
,
287 GetMarginWidth (m_LineNrID
) == 0? m_LineNrMargin
: 0);
290 void Edit::OnLongLineOn (wxCommandEvent
&WXUNUSED(event
)) {
291 SetEdgeMode (GetEdgeMode() == 0? wxSTC_EDGE_LINE
: wxSTC_EDGE_NONE
);
294 void Edit::OnWhiteSpace (wxCommandEvent
&WXUNUSED(event
)) {
295 SetViewWhiteSpace (GetViewWhiteSpace() == 0?
296 wxSTC_WS_VISIBLEALWAYS
: wxSTC_WS_INVISIBLE
);
299 void Edit::OnFoldToggle (wxCommandEvent
&WXUNUSED(event
)) {
300 ToggleFold (GetFoldParent(GetCurrentLine()));
303 void Edit::OnSetOverType (wxCommandEvent
&WXUNUSED(event
)) {
304 SetOvertype (!GetOvertype());
307 void Edit::OnSetReadOnly (wxCommandEvent
&WXUNUSED(event
)) {
308 SetReadOnly (!GetReadOnly());
311 void Edit::OnWrapmodeOn (wxCommandEvent
&WXUNUSED(event
)) {
312 SetWrapMode (GetWrapMode() == 0? wxSTC_WRAP_WORD
: wxSTC_WRAP_NONE
);
315 void Edit::OnUseCharset (wxCommandEvent
&event
) {
317 int charset
= GetCodePage();
318 switch (event
.GetId()) {
319 case myID_CHARSETANSI
: {charset
= wxSTC_CHARSET_ANSI
; break;}
320 case myID_CHARSETMAC
: {charset
= wxSTC_CHARSET_ANSI
; break;}
322 for (Nr
= 0; Nr
< wxSTC_STYLE_LASTPREDEFINED
; Nr
++) {
323 StyleSetCharacterSet (Nr
, charset
);
325 SetCodePage (charset
);
328 void Edit::OnAnnotationAdd(wxCommandEvent
& WXUNUSED(event
))
330 const int line
= GetCurrentLine();
332 wxString ann
= AnnotationGetText(line
);
333 ann
= wxGetTextFromUser
335 wxString::Format("Enter annotation for the line %d", line
),
343 AnnotationSetText(line
, ann
);
344 AnnotationSetStyle(line
, ANNOTATION_STYLE
);
346 // Scintilla doesn't update the scroll width for annotations, even with
347 // scroll width tracking on, so do it manually.
348 const int width
= GetScrollWidth();
350 // NB: The following adjustments are only needed when using
351 // wxSTC_ANNOTATION_BOXED annotations style, but we apply them always
352 // in order to make things simpler and not have to redo the width
353 // calculations when the annotations visibility changes. In a real
354 // program you'd either just stick to a fixed annotations visibility or
355 // update the width when it changes.
357 // Take into account the fact that the annotation is shown indented, with
358 // the same indent as the line it's attached to.
359 int indent
= GetLineIndentation(line
);
361 // This is just a hack to account for the width of the box, there doesn't
362 // seem to be any way to get it directly from Scintilla.
365 const int widthAnn
= TextWidth(ANNOTATION_STYLE
, ann
+ wxString(indent
, ' '));
367 if (widthAnn
> width
)
368 SetScrollWidth(widthAnn
);
371 void Edit::OnAnnotationRemove(wxCommandEvent
& WXUNUSED(event
))
373 AnnotationSetText(GetCurrentLine(), wxString());
376 void Edit::OnAnnotationClear(wxCommandEvent
& WXUNUSED(event
))
378 AnnotationClearAll();
381 void Edit::OnAnnotationStyle(wxCommandEvent
& event
)
384 switch (event
.GetId()) {
385 case myID_ANNOTATION_STYLE_HIDDEN
:
386 style
= wxSTC_ANNOTATION_HIDDEN
;
389 case myID_ANNOTATION_STYLE_STANDARD
:
390 style
= wxSTC_ANNOTATION_STANDARD
;
393 case myID_ANNOTATION_STYLE_BOXED
:
394 style
= wxSTC_ANNOTATION_BOXED
;
398 AnnotationSetVisible(style
);
401 void Edit::OnChangeCase (wxCommandEvent
&event
) {
402 switch (event
.GetId()) {
403 case myID_CHANGELOWER
: {
404 CmdKeyExecute (wxSTC_CMD_LOWERCASE
);
407 case myID_CHANGEUPPER
: {
408 CmdKeyExecute (wxSTC_CMD_UPPERCASE
);
414 void Edit::OnConvertEOL (wxCommandEvent
&event
) {
415 int eolMode
= GetEOLMode();
416 switch (event
.GetId()) {
417 case myID_CONVERTCR
: { eolMode
= wxSTC_EOL_CR
; break;}
418 case myID_CONVERTCRLF
: { eolMode
= wxSTC_EOL_CRLF
; break;}
419 case myID_CONVERTLF
: { eolMode
= wxSTC_EOL_LF
; break;}
421 ConvertEOLs (eolMode
);
422 SetEOLMode (eolMode
);
426 void Edit::OnMarginClick (wxStyledTextEvent
&event
) {
427 if (event
.GetMargin() == 2) {
428 int lineClick
= LineFromPosition (event
.GetPosition());
429 int levelClick
= GetFoldLevel (lineClick
);
430 if ((levelClick
& wxSTC_FOLDLEVELHEADERFLAG
) > 0) {
431 ToggleFold (lineClick
);
436 void Edit::OnCharAdded (wxStyledTextEvent
&event
) {
437 char chr
= (char)event
.GetKey();
438 int currentLine
= GetCurrentLine();
439 // Change this if support for mac files with \r is needed
442 if (currentLine
> 0) {
443 lineInd
= GetLineIndentation(currentLine
- 1);
445 if (lineInd
== 0) return;
446 SetLineIndentation (currentLine
, lineInd
);
447 GotoPos(PositionFromLine (currentLine
) + lineInd
);
452 //----------------------------------------------------------------------------
454 wxString
Edit::DeterminePrefs (const wxString
&filename
) {
456 LanguageInfo
const* curInfo
;
458 // determine language from filepatterns
460 for (languageNr
= 0; languageNr
< g_LanguagePrefsSize
; languageNr
++) {
461 curInfo
= &g_LanguagePrefs
[languageNr
];
462 wxString filepattern
= curInfo
->filepattern
;
464 while (!filepattern
.empty()) {
465 wxString cur
= filepattern
.BeforeFirst (';');
466 if ((cur
== filename
) ||
467 (cur
== (filename
.BeforeLast ('.') + wxT(".*"))) ||
468 (cur
== (wxT("*.") + filename
.AfterLast ('.')))) {
469 return curInfo
->name
;
471 filepattern
= filepattern
.AfterFirst (';');
474 return wxEmptyString
;
478 bool Edit::InitializePrefs (const wxString
&name
) {
482 LanguageInfo
const* curInfo
= NULL
;
484 // determine language
487 for (languageNr
= 0; languageNr
< g_LanguagePrefsSize
; languageNr
++) {
488 curInfo
= &g_LanguagePrefs
[languageNr
];
489 if (curInfo
->name
== name
) {
494 if (!found
) return false;
496 // set lexer and language
497 SetLexer (curInfo
->lexer
);
498 m_language
= curInfo
;
500 // set margin for line numbers
501 SetMarginType (m_LineNrID
, wxSTC_MARGIN_NUMBER
);
502 StyleSetForeground (wxSTC_STYLE_LINENUMBER
, wxColour (wxT("DARK GREY")));
503 StyleSetBackground (wxSTC_STYLE_LINENUMBER
, *wxWHITE
);
504 SetMarginWidth (m_LineNrID
, 0); // start out not visible
507 StyleSetBackground(ANNOTATION_STYLE
, wxColour(244, 220, 220));
508 StyleSetForeground(ANNOTATION_STYLE
, *wxBLACK
);
509 StyleSetSizeFractional(ANNOTATION_STYLE
,
510 (StyleGetSizeFractional(wxSTC_STYLE_DEFAULT
)*4)/5);
512 // default fonts for all styles!
514 for (Nr
= 0; Nr
< wxSTC_STYLE_LASTPREDEFINED
; Nr
++) {
515 wxFont
font (10, wxMODERN
, wxNORMAL
, wxNORMAL
);
516 StyleSetFont (Nr
, font
);
520 StyleSetForeground (wxSTC_STYLE_DEFAULT
, wxColour (wxT("DARK GREY")));
521 StyleSetForeground (wxSTC_STYLE_INDENTGUIDE
, wxColour (wxT("DARK GREY")));
523 // initialize settings
524 if (g_CommonPrefs
.syntaxEnable
) {
526 for (Nr
= 0; Nr
< STYLE_TYPES_COUNT
; Nr
++) {
527 if (curInfo
->styles
[Nr
].type
== -1) continue;
528 const StyleInfo
&curType
= g_StylePrefs
[curInfo
->styles
[Nr
].type
];
529 wxFont
font (curType
.fontsize
, wxMODERN
, wxNORMAL
, wxNORMAL
, false,
531 StyleSetFont (Nr
, font
);
532 if (curType
.foreground
) {
533 StyleSetForeground (Nr
, wxColour (curType
.foreground
));
535 if (curType
.background
) {
536 StyleSetBackground (Nr
, wxColour (curType
.background
));
538 StyleSetBold (Nr
, (curType
.fontstyle
& mySTC_STYLE_BOLD
) > 0);
539 StyleSetItalic (Nr
, (curType
.fontstyle
& mySTC_STYLE_ITALIC
) > 0);
540 StyleSetUnderline (Nr
, (curType
.fontstyle
& mySTC_STYLE_UNDERL
) > 0);
541 StyleSetVisible (Nr
, (curType
.fontstyle
& mySTC_STYLE_HIDDEN
) == 0);
542 StyleSetCase (Nr
, curType
.lettercase
);
543 const char *pwords
= curInfo
->styles
[Nr
].words
;
545 SetKeyWords (keywordnr
, pwords
);
551 // set margin as unused
552 SetMarginType (m_DividerID
, wxSTC_MARGIN_SYMBOL
);
553 SetMarginWidth (m_DividerID
, 0);
554 SetMarginSensitive (m_DividerID
, false);
557 SetMarginType (m_FoldingID
, wxSTC_MARGIN_SYMBOL
);
558 SetMarginMask (m_FoldingID
, wxSTC_MASK_FOLDERS
);
559 StyleSetBackground (m_FoldingID
, *wxWHITE
);
560 SetMarginWidth (m_FoldingID
, 0);
561 SetMarginSensitive (m_FoldingID
, false);
562 if (g_CommonPrefs
.foldEnable
) {
563 SetMarginWidth (m_FoldingID
, curInfo
->folds
!= 0? m_FoldingMargin
: 0);
564 SetMarginSensitive (m_FoldingID
, curInfo
->folds
!= 0);
565 SetProperty (wxT("fold"), curInfo
->folds
!= 0? wxT("1"): wxT("0"));
566 SetProperty (wxT("fold.comment"),
567 (curInfo
->folds
& mySTC_FOLD_COMMENT
) > 0? wxT("1"): wxT("0"));
568 SetProperty (wxT("fold.compact"),
569 (curInfo
->folds
& mySTC_FOLD_COMPACT
) > 0? wxT("1"): wxT("0"));
570 SetProperty (wxT("fold.preprocessor"),
571 (curInfo
->folds
& mySTC_FOLD_PREPROC
) > 0? wxT("1"): wxT("0"));
572 SetProperty (wxT("fold.html"),
573 (curInfo
->folds
& mySTC_FOLD_HTML
) > 0? wxT("1"): wxT("0"));
574 SetProperty (wxT("fold.html.preprocessor"),
575 (curInfo
->folds
& mySTC_FOLD_HTMLPREP
) > 0? wxT("1"): wxT("0"));
576 SetProperty (wxT("fold.comment.python"),
577 (curInfo
->folds
& mySTC_FOLD_COMMENTPY
) > 0? wxT("1"): wxT("0"));
578 SetProperty (wxT("fold.quotes.python"),
579 (curInfo
->folds
& mySTC_FOLD_QUOTESPY
) > 0? wxT("1"): wxT("0"));
581 SetFoldFlags (wxSTC_FOLDFLAG_LINEBEFORE_CONTRACTED
|
582 wxSTC_FOLDFLAG_LINEAFTER_CONTRACTED
);
584 // set spaces and indention
587 SetTabIndents (true);
588 SetBackSpaceUnIndents (true);
589 SetIndent (g_CommonPrefs
.indentEnable
? 4: 0);
592 SetViewEOL (g_CommonPrefs
.displayEOLEnable
);
593 SetIndentationGuides (g_CommonPrefs
.indentGuideEnable
);
595 SetEdgeMode (g_CommonPrefs
.longLineOnEnable
? wxSTC_EDGE_LINE
: wxSTC_EDGE_NONE
);
596 SetViewWhiteSpace (g_CommonPrefs
.whiteSpaceEnable
?
597 wxSTC_WS_VISIBLEALWAYS
: wxSTC_WS_INVISIBLE
);
598 SetOvertype (g_CommonPrefs
.overTypeInitial
);
599 SetReadOnly (g_CommonPrefs
.readOnlyInitial
);
600 SetWrapMode (g_CommonPrefs
.wrapModeInitial
?
601 wxSTC_WRAP_WORD
: wxSTC_WRAP_NONE
);
606 bool Edit::LoadFile ()
611 wxFileDialog
dlg (this, wxT("Open file"), wxEmptyString
, wxEmptyString
,
612 wxT("Any file (*)|*"), wxFD_OPEN
| wxFD_FILE_MUST_EXIST
| wxFD_CHANGE_DIR
);
613 if (dlg
.ShowModal() != wxID_OK
) return false;
614 m_filename
= dlg
.GetPath();
618 return LoadFile (m_filename
);
621 #endif // wxUSE_FILEDLG
624 bool Edit::LoadFile (const wxString
&filename
) {
626 // load file in edit and clear undo
627 if (!filename
.empty()) m_filename
= filename
;
629 wxStyledTextCtrl::LoadFile(m_filename
);
633 // determine lexer language
634 wxFileName
fname (m_filename
);
635 InitializePrefs (DeterminePrefs (fname
.GetFullName()));
640 bool Edit::SaveFile ()
643 // return if no change
644 if (!Modified()) return true;
648 wxFileDialog
dlg (this, wxT("Save file"), wxEmptyString
, wxEmptyString
, wxT("Any file (*)|*"),
649 wxFD_SAVE
| wxFD_OVERWRITE_PROMPT
);
650 if (dlg
.ShowModal() != wxID_OK
) return false;
651 m_filename
= dlg
.GetPath();
655 return SaveFile (m_filename
);
658 #endif // wxUSE_FILEDLG
661 bool Edit::SaveFile (const wxString
&filename
) {
663 // return if no change
664 if (!Modified()) return true;
666 // // save edit in file and clear undo
667 // if (!filename.empty()) m_filename = filename;
668 // wxFile file (m_filename, wxFile::write);
669 // if (!file.IsOpened()) return false;
670 // wxString buf = GetText();
671 // bool okay = file.Write (buf);
673 // if (!okay) return false;
674 // EmptyUndoBuffer();
679 return wxStyledTextCtrl::SaveFile(filename
);
683 bool Edit::Modified () {
685 // return modified state
686 return (GetModify() && !GetReadOnly());
689 //----------------------------------------------------------------------------
691 //----------------------------------------------------------------------------
693 EditProperties::EditProperties (Edit
*edit
,
695 : wxDialog (edit
, wxID_ANY
, wxEmptyString
,
696 wxDefaultPosition
, wxDefaultSize
,
697 style
| wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
) {
699 // sets the application title
700 SetTitle (_("Properties"));
704 wxBoxSizer
*fullname
= new wxBoxSizer (wxHORIZONTAL
);
705 fullname
->Add (10, 0);
706 fullname
->Add (new wxStaticText (this, wxID_ANY
, _("Full filename"),
707 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
708 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
);
709 fullname
->Add (new wxStaticText (this, wxID_ANY
, edit
->GetFilename()),
710 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
);
713 wxGridSizer
*textinfo
= new wxGridSizer (4, 0, 2);
714 textinfo
->Add (new wxStaticText (this, wxID_ANY
, _("Language"),
715 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
716 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxLEFT
, 4);
717 textinfo
->Add (new wxStaticText (this, wxID_ANY
, edit
->m_language
->name
),
718 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4);
719 textinfo
->Add (new wxStaticText (this, wxID_ANY
, _("Lexer-ID: "),
720 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
721 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxLEFT
, 4);
722 text
= wxString::Format (wxT("%d"), edit
->GetLexer());
723 textinfo
->Add (new wxStaticText (this, wxID_ANY
, text
),
724 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4);
725 wxString EOLtype
= wxEmptyString
;
726 switch (edit
->GetEOLMode()) {
727 case wxSTC_EOL_CR
: {EOLtype
= wxT("CR (Unix)"); break; }
728 case wxSTC_EOL_CRLF
: {EOLtype
= wxT("CRLF (Windows)"); break; }
729 case wxSTC_EOL_LF
: {EOLtype
= wxT("CR (Macintosh)"); break; }
731 textinfo
->Add (new wxStaticText (this, wxID_ANY
, _("Line endings"),
732 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
733 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxLEFT
, 4);
734 textinfo
->Add (new wxStaticText (this, wxID_ANY
, EOLtype
),
735 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4);
738 wxStaticBoxSizer
*textinfos
= new wxStaticBoxSizer (
739 new wxStaticBox (this, wxID_ANY
, _("Informations")),
741 textinfos
->Add (textinfo
, 0, wxEXPAND
);
742 textinfos
->Add (0, 6);
745 wxGridSizer
*statistic
= new wxGridSizer (4, 0, 2);
746 statistic
->Add (new wxStaticText (this, wxID_ANY
, _("Total lines"),
747 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
748 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxLEFT
, 4);
749 text
= wxString::Format (wxT("%d"), edit
->GetLineCount());
750 statistic
->Add (new wxStaticText (this, wxID_ANY
, text
),
751 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4);
752 statistic
->Add (new wxStaticText (this, wxID_ANY
, _("Total chars"),
753 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
754 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxLEFT
, 4);
755 text
= wxString::Format (wxT("%d"), edit
->GetTextLength());
756 statistic
->Add (new wxStaticText (this, wxID_ANY
, text
),
757 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4);
758 statistic
->Add (new wxStaticText (this, wxID_ANY
, _("Current line"),
759 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
760 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxLEFT
, 4);
761 text
= wxString::Format (wxT("%d"), edit
->GetCurrentLine());
762 statistic
->Add (new wxStaticText (this, wxID_ANY
, text
),
763 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4);
764 statistic
->Add (new wxStaticText (this, wxID_ANY
, _("Current pos"),
765 wxDefaultPosition
, wxSize(80, wxDefaultCoord
)),
766 0, wxALIGN_LEFT
|wxALIGN_CENTER_VERTICAL
|wxLEFT
, 4);
767 text
= wxString::Format (wxT("%d"), edit
->GetCurrentPos());
768 statistic
->Add (new wxStaticText (this, wxID_ANY
, text
),
769 0, wxALIGN_RIGHT
|wxALIGN_CENTER_VERTICAL
|wxRIGHT
, 4);
771 // char/line statistics
772 wxStaticBoxSizer
*statistics
= new wxStaticBoxSizer (
773 new wxStaticBox (this, wxID_ANY
, _("Statistics")),
775 statistics
->Add (statistic
, 0, wxEXPAND
);
776 statistics
->Add (0, 6);
779 wxBoxSizer
*totalpane
= new wxBoxSizer (wxVERTICAL
);
780 totalpane
->Add (fullname
, 0, wxEXPAND
| wxLEFT
| wxRIGHT
| wxTOP
, 10);
781 totalpane
->Add (0, 6);
782 totalpane
->Add (textinfos
, 0, wxEXPAND
| wxLEFT
| wxRIGHT
, 10);
783 totalpane
->Add (0, 10);
784 totalpane
->Add (statistics
, 0, wxEXPAND
| wxLEFT
| wxRIGHT
, 10);
785 totalpane
->Add (0, 6);
786 wxButton
*okButton
= new wxButton (this, wxID_OK
, _("OK"));
787 okButton
->SetDefault();
788 totalpane
->Add (okButton
, 0, wxALIGN_CENTER
| wxALL
, 10);
790 SetSizerAndFit (totalpane
);
795 #if wxUSE_PRINTING_ARCHITECTURE
797 //----------------------------------------------------------------------------
799 //----------------------------------------------------------------------------
801 EditPrint::EditPrint (Edit
*edit
, const wxChar
*title
)
802 : wxPrintout(title
) {
808 bool EditPrint::OnPrintPage (int page
) {
811 if (!dc
) return false;
817 if (page
== 1) m_printed
= 0;
818 m_printed
= m_edit
->FormatRange (1, m_printed
, m_edit
->GetLength(),
819 dc
, dc
, m_printRect
, m_pageRect
);
824 bool EditPrint::OnBeginDocument (int startPage
, int endPage
) {
826 if (!wxPrintout::OnBeginDocument (startPage
, endPage
)) {
833 void EditPrint::GetPageInfo (int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
) {
841 // scale DC if possible
846 // get print page informations and convert to printer pixels
848 GetPPIScreen (&ppiScr
.x
, &ppiScr
.y
);
849 wxSize page
= g_pageSetupData
->GetPaperSize();
850 page
.x
= static_cast<int> (page
.x
* ppiScr
.x
/ 25.4);
851 page
.y
= static_cast<int> (page
.y
* ppiScr
.y
/ 25.4);
852 m_pageRect
= wxRect (0,
857 // get margins informations and convert to printer pixels
858 wxPoint pt
= g_pageSetupData
->GetMarginTopLeft();
861 pt
= g_pageSetupData
->GetMarginBottomRight();
865 top
= static_cast<int> (top
* ppiScr
.y
/ 25.4);
866 bottom
= static_cast<int> (bottom
* ppiScr
.y
/ 25.4);
867 left
= static_cast<int> (left
* ppiScr
.x
/ 25.4);
868 right
= static_cast<int> (right
* ppiScr
.x
/ 25.4);
870 m_printRect
= wxRect (left
,
872 page
.x
- (left
+ right
),
873 page
.y
- (top
+ bottom
));
876 while (HasPage (*maxPage
)) {
877 m_printed
= m_edit
->FormatRange (0, m_printed
, m_edit
->GetLength(),
878 dc
, dc
, m_printRect
, m_pageRect
);
881 if (*maxPage
> 0) *minPage
= 1;
882 *selPageFrom
= *minPage
;
883 *selPageTo
= *maxPage
;
886 bool EditPrint::HasPage (int WXUNUSED(page
)) {
888 return (m_printed
< m_edit
->GetLength());
891 bool EditPrint::PrintScaling (wxDC
*dc
){
893 // check for dc, return if none
894 if (!dc
) return false;
896 // get printer and screen sizing values
898 GetPPIScreen (&ppiScr
.x
, &ppiScr
.y
);
899 if (ppiScr
.x
== 0) { // most possible guess 96 dpi
904 GetPPIPrinter (&ppiPrt
.x
, &ppiPrt
.y
);
905 if (ppiPrt
.x
== 0) { // scaling factor to 1
909 wxSize dcSize
= dc
->GetSize();
911 GetPageSizePixels (&pageSize
.x
, &pageSize
.y
);
914 float scale_x
= (float)(ppiPrt
.x
* dcSize
.x
) /
915 (float)(ppiScr
.x
* pageSize
.x
);
916 float scale_y
= (float)(ppiPrt
.y
* dcSize
.y
) /
917 (float)(ppiScr
.y
* pageSize
.y
);
918 dc
->SetUserScale (scale_x
, scale_y
);
923 #endif // wxUSE_PRINTING_ARCHITECTURE