MutEditFile.cpp
gehe zur Dokumentation dieser Datei
00001 
00035 // ===========================================================================
00036 // declarations
00037 // ===========================================================================
00038 
00039 // ---------------------------------------------------------------------------
00040 // headers
00041 // ---------------------------------------------------------------------------
00042 
00043 // For compilers that support precompilation, includes "wx/wx.h".
00044 #include "Defs.h"
00045 #include "mhDefs.h"
00046 
00047 #include <iostream>
00048 
00049 #include "wx/wxprec.h"
00050 #include "wx/ffile.h"
00051 
00052 #ifdef __BORLANDC__
00053 #pragma hdrstop
00054 #endif
00055 
00056 #ifndef WX_PRECOMP
00057 #include "wx/wx.h"
00058 #include "wx/mdi.h"
00059 #include "wx/filename.h"
00060 #include "wx/textfile.h"
00061 #endif
00062 
00063 #include "wx/toolbar.h"
00064 
00065 
00066 #ifdef __WXMSW__
00067 #include "wx/msw/private.h"
00068 
00069 #if wxUSE_RICHEDIT
00070 
00071 #if wxUSE_INKEDIT
00072 #include "wx/dynlib.h"
00073 #endif
00074 
00075 // old mingw32 has richedit stuff directly in windows.h and doesn't have
00076 // richedit.h at all
00077 #if !defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__)
00078 #include <richedit.h>
00079 #endif
00080 
00081 #endif // wxUSE_RICHEDIT
00082 #endif // __WXMSW__
00083 
00084 
00085 //#include "Mutabor.rh"
00086 #include "MutFrame.h"
00087 #include "MutEditFile.h"
00088 #include "CompDlg.h"
00089 #include "Runtime.h"
00090 #include "MutView.h"
00091 #include "stclanguage.h"
00092 
00093 CompDlg *CompDia = NULL;
00094 
00095 // ---------------------------------------------------------------------------
00096 // event tables
00097 // ---------------------------------------------------------------------------
00098 using mutaborGUI::MutEditFile;
00099 BEGIN_EVENT_TABLE(MutEditFile, wxStyledTextCtrl)
00100 //    EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
00101 //EVT_MENU(CM_FILESAVE,            MutEditFile::CmFileSave)
00102 //EVT_MENU(CM_FILESAVEAS,          MutEditFile::CmFileSaveAs)
00103 EVT_MENU(CM_COMPILE,             MutEditFile::CmCompile)
00104 EVT_MENU(CM_COMPACT,             MutEditFile::CmCompAct)
00105 EVT_MENU(CM_ACTIVATE,            MutEditFile::CmCompAct)
00106 EVT_MENU(CM_GETLINE,             MutEditFile::CmGetLine)
00107 EVT_MENU(CM_HELPCONTEXT,         MutEditFile::CmHelpContext)
00108 // common
00109 EVT_SIZE (                       MutEditFile::OnSize)
00110 // edit
00111 EVT_MENU (wxID_CLEAR,            MutEditFile::OnEditClear)
00112 EVT_MENU (wxID_CUT,              MutEditFile::OnEditCut)
00113 EVT_MENU (wxID_COPY,             MutEditFile::OnEditCopy)
00114 EVT_MENU (wxID_PASTE,            MutEditFile::OnEditPaste)
00115 EVT_MENU (CM_INDENTINC,          MutEditFile::OnEditIndentInc)
00116 EVT_MENU (CM_INDENTRED,          MutEditFile::OnEditIndentRed)
00117 EVT_MENU (wxID_SELECTALL,        MutEditFile::OnEditSelectAll)
00118 EVT_MENU (CM_SELECTLINE,         MutEditFile::OnEditSelectLine)
00119 EVT_MENU (wxID_REDO,             MutEditFile::OnEditRedo)
00120 EVT_MENU (wxID_UNDO,             MutEditFile::OnEditUndo)
00121 // find
00122 EVT_MENU (wxID_FIND,             MutEditFile::OnFind)
00123 EVT_MENU (CM_FINDNEXT,           MutEditFile::OnFindNext)
00124 EVT_MENU (CM_REPLACE,            MutEditFile::OnReplace)
00125 EVT_MENU (CM_REPLACENEXT,        MutEditFile::OnReplaceNext)
00126 EVT_MENU (CM_BRACEMATCH,         MutEditFile::OnBraceMatch)
00127 EVT_MENU (CM_GOTO,               MutEditFile::OnGoto)
00128 // view
00129 EVT_MENU_RANGE (CM_HILIGHTFIRST, CM_HILIGHTLAST,
00130                 MutEditFile::OnHilightLang)
00131 EVT_MENU (CM_DISPLAYEOL,         MutEditFile::OnDisplayEOL)
00132 EVT_MENU (CM_INDENTGUIDE,        MutEditFile::OnIndentGuide)
00133 EVT_MENU (CM_LINENUMBER,         MutEditFile::OnLineNumber)
00134 EVT_MENU (CM_LONGLINEON,         MutEditFile::OnLongLineOn)
00135 EVT_MENU (CM_WHITESPACE,         MutEditFile::OnWhiteSpace)
00136 EVT_MENU (CM_FOLDTOGGLE,         MutEditFile::OnFoldToggle)
00137 EVT_MENU (CM_OVERTYPE,           MutEditFile::OnSetOverType)
00138 EVT_MENU (CM_READONLY,           MutEditFile::OnSetReadOnly)
00139 EVT_MENU (CM_WRAPMODEON,         MutEditFile::OnWrapmodeOn)
00140 EVT_MENU (CM_CHARSETANSI,        MutEditFile::OnUseCharset)
00141 EVT_MENU (CM_CHARSETMAC,         MutEditFile::OnUseCharset)
00142 // extra
00143 EVT_MENU (CM_CHANGELOWER,        MutEditFile::OnChangeCase)
00144 EVT_MENU (CM_CHANGEUPPER,        MutEditFile::OnChangeCase)
00145 EVT_MENU (CM_CONVERTCR,          MutEditFile::OnConvertEOL)
00146 EVT_MENU (CM_CONVERTCRLF,        MutEditFile::OnConvertEOL)
00147 EVT_MENU (CM_CONVERTLF,          MutEditFile::OnConvertEOL)
00148 // stc
00149 EVT_STC_MARGINCLICK (wxID_ANY,   MutEditFile::OnMarginClick)
00150 EVT_STC_CHARADDED (wxID_ANY,     MutEditFile::OnCharAdded)
00151 END_EVENT_TABLE()
00152 
00153 namespace mutaborGUI {
00154 // ===========================================================================
00155 // implementation
00156 // ===========================================================================
00157 
00158 // Editorfenster
00159 
00160         MutEditFile::MutEditFile(wxWindow* parent, 
00161                                  const wxPoint& pos, 
00162                                  const wxSize& size, 
00163                                  const wxString& value, 
00164                                  const wxString& name)
00165                 : wxStyledTextCtrl(parent, 
00166                                    wxID_ANY, 
00167                                    pos, 
00168                                    size, 
00169                                    wxTE_PROCESS_TAB | wxTE_MULTILINE | wxTE_RICH | wxTE_RICH2 
00170                                    | wxTE_NOHIDESEL | wxHSCROLL | wxVSCROLL | wxBORDER_SUNKEN, 
00171                                    name)
00172         {
00173                 Init();
00174         }
00175         
00176         MutEditFile::MutEditFile(MutView * v,
00177                                  wxWindow* parent, 
00178                                  const wxPoint& pos, 
00179                                  const wxSize& size, 
00180                                  const wxString& value, 
00181                                  const wxString& name)
00182                 : wxStyledTextCtrl(parent, 
00183                                    wxID_ANY, 
00184                                    pos, 
00185                                    size, 
00186                                    wxTE_PROCESS_TAB | 
00187                                    wxTE_MULTILINE | wxTE_RICH | wxTE_RICH2 
00188                                    | wxTE_NOHIDESEL | wxHSCROLL | wxVSCROLL | wxBORDER_SUNKEN , 
00189                                    name),
00190                   view(v)
00191         {
00192                 Init();
00193         }
00194 
00195 
00196         MutEditFile::~MutEditFile()
00197         {
00198                 if (view) {
00199                         view->SetTextsw(NULL);
00200                 }
00201         }
00202 
00203 
00204         bool MutEditFile::Compile(bool activate)
00205         {
00206                 wxString TmpFile = wxFileName::CreateTempFileName(wxT(PACKAGE));
00207 
00208                 if ( SaveEditor )
00209                         SaveFile(m_filename);
00210 
00211                 bool result=true;
00212 
00213                 if ( LogicOn ) {
00214                         wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, CM_STOP);
00215                         GetParent()->GetEventHandler()->ProcessEvent(event);
00216                 }
00217 
00218                 wxFile file(TmpFile,wxFile::write);
00219                 if (!file.IsOpened() || !file.Write(GetText(), *wxConvCurrent) ) {
00220                         wxMessageBox(_("Can't write temporary file."),
00221                                      _("Error"), wxOK | wxICON_HAND);
00222                         CompiledFile = wxEmptyString;
00223                         return false;
00224                 } 
00225 
00226                 CompDia = new CompDlg(this);
00227 
00228                 if (!CompDia->Show()) {
00229                         wxLogError(_("The compile dialog could not be loaded."));
00230                 };
00231 
00232                 CompDia->SetFileName(GetName());
00233 
00234                 if ( ::Compile(CompDia, TmpFile.c_str()) ) {
00235                         CompiledFile = m_filename;
00236 
00237                         if (activate) {
00238                                 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, 
00239                                                       CM_DOACTIVATE);
00240                                 GetParent()->GetEventHandler()->ProcessEvent(event);
00241                                 CompDia->Destroy();
00242                         } else
00243                                 CompDia->EnableButton(true);
00244 
00245                 } else {
00246                         CompiledFile = wxEmptyString;
00247                         CompDia->EnableButton(true);
00248                         GoErrorLine();
00249                         result = false;
00250                 }
00251 
00252                 wxRemoveFile(TmpFile);
00253                         
00254                 return result;
00255                 
00256         }
00257 
00258         bool MutEditFile::DoLoadFile(const wxString &filename, 
00259                                      int WXUNUSED(fileType))
00260         {
00261                 DEBUGLOG(mutparser,_T(""));
00262                 STUBC;
00263 #if 0
00264                 wxFFile file(filename);
00265 
00266                 if ( file.IsOpened() ) {
00267                         wxString text;
00268 
00269                         if ( file.ReadAll(&text, autoConverter) ) {
00270                                 DEBUGLOG(mutparser,_T("%s"),text.c_str());
00271 
00272                                 SetValue(text);
00273                                 DiscardEdits();
00274                                 m_filename = filename;
00275                                 file.Close();
00276                                 return true;
00277                         }
00278 
00279 #ifdef DEBUG
00280                         else
00281                                 DEBUGLOG (mutparser,
00282                                           _T("File opened, but couldn't be loaded."));
00283 
00284 #endif
00285                 }
00286 
00287                 wxLogError(_("File couldn't be loaded."));
00288 #endif
00289                 return false;
00290         }
00291 
00292         bool MutEditFile::DoSaveFile(const wxString& filename, int WXUNUSED(fileType))
00293 
00294         {
00295                 STUBC;
00296 #if 0
00297 #if wxUSE_FFILE
00298 #ifdef DEBUG
00299                 std::cerr << "MutEditFile::DoSaveFile" << std::endl
00300                           << (GetValue().fn_str()) << std::endl;
00301 #endif
00302                 wxFFile file(filename, _T("w"));
00303 
00304                 if ( file.IsOpened() && file.Write(GetValue(), autoConverter) ) {
00305                         // if it worked, save for future calls
00306                         m_filename = filename;
00307 
00308                         // it's not modified any longer
00309                         SetSavePoint();
00310 
00311                         file.Close();
00312 
00313                         return true;
00314                 }
00315 
00316 #endif // wxUSE_FFILE
00317 #endif
00318                 wxLogError(_("The text couldn't be saved."));
00319 
00320                 return false;
00321         }
00322 
00323 
00324         void MutEditFile::CmFileSave(wxCommandEvent& WXUNUSED(event))
00325 
00326         {
00327                 SaveFile(m_filename);
00328         }
00329 
00330         void MutEditFile::CmFileSaveAs(wxCommandEvent& event)
00331         {
00332                 wxString newname = FileNameDialog(GetParent(), event.GetId(), m_filename);
00333 
00334                 if (newname.IsEmpty()) return;
00335 
00336                 m_filename = newname;
00337                 CmFileSave(event);
00338 
00339                 // Überschrift in MutWin setzen
00340                 wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED,CM_SETTITLE);
00341 
00342                 //  e.ResumePropagation(wxEVENT_PROPAGATE_MAX);
00343                 wxFileName s(m_filename);
00344 
00345                 e.SetString(s.GetFullName());
00346 
00347                 AddPendingEvent(e);
00348         }
00349 
00350 
00351         void MutEditFile::CmCompile(wxCommandEvent& event)
00352         {
00353 
00354                 DEBUGLOG (other, 
00355                           _T("MutEditFile::CmCompile(Event(%d)); filename = %s"),
00356                           event.GetId(),m_filename.c_str());
00357                 Compile(false);
00358                 event.Skip(false);
00359         }
00360 
00361         void MutEditFile::CmCompAct(wxCommandEvent& event)
00362         {
00363                 Compile(true);
00364                 event.Skip(false);
00365         }
00366 
00367         void MutEditFile::CmGetLine(wxCommandEvent& WXUNUSED(event))
00368         {
00369                 STUBC;
00370                 /*  int LineFirstPos = GetLineIndex(-1);
00371                     EditLine = GetLineFromPos(LineFirstPos) + 1;
00372                     uint Sel1, Sel2;
00373                     GetSelection(Sel1, Sel2);
00374                     EditRow = Sel2 - LineFirstPos + 1;*/
00375         }
00376 
00377         void MutEditFile::GoErrorLine()
00378         {
00379                 int Line = GetErrorLine();
00380                 if ( Line == -1 ) return;
00381                 GotoLine(Line);
00382         }
00383 
00384         void MutEditFile::CmHelpContext(wxCommandEvent& WXUNUSED(event))
00385         {
00386                 STUBC;
00387                 /*  char s[1000], s1[1000];
00388                     uint a, e, i;
00389                     GetSelection(a, e);
00390                     if ( !GetLine(s, 999, GetLineFromPos(a)) ) return;
00391                     i = a - GetLineIndex(GetLineFromPos(a));
00392                     // nun Wortanfang und ende finden
00393                     if ( a == e )
00394                     {
00395                     while ( i > 0 && !strchr(" ,[]()", s[i-1]) )
00396                     i--;
00397                     a = i;
00398                     while ( s[i] != 0 && !strchr(" ,[]()", s[i]) )
00399                     i++;
00400                     e = i;
00401                     }
00402                     else
00403                     {
00404                     e = e - a + i;
00405                     a = i;
00406                     }
00407                     strncpy(s1, &(s[a]), e - a);
00408                     s1[e-a] = 0;
00409                     strupr(s1);
00410                     int HelpContext = 0;
00411                     for (i = 0; i < 2 * nKeyWords; i++)
00412                     if ( !strcmp(s1, KeyWords[i]) )
00413                     HelpContext = (i % nKeyWords) + SX_INTERVAL;
00414                     if ( HelpContext )
00415                     WinHelp(HlpFile, HELP_CONTEXT, HelpContext);
00416                     else
00417                     WinHelp(HlpFile, HELP_CONTEXT, SX_BASICS);*/
00418         }
00419 
00420 #if defined(__WXMSW__) && !(wxUSE_UNICODE || wxUSE_WCHAR_T)
00421 
00422         wxString MutEditFile::GetValue() const
00423         {
00424                 std::cout << "MutEditFile::GetValue()" << std::endl;
00425                 // range 0..-1 is special for GetRange() and means to retrieve all text
00426                 return GetRange(0, -1);
00427         }
00428 
00429 #endif
00430 
00431 
00432         void MutEditFile::Init() 
00433         {
00434                 m_filename = wxEmptyString;
00435 
00436                 m_LineNrID = 0;
00437                 m_DividerID = 1;
00438                 m_FoldingID = 2;
00439 
00440                 // initialize language
00441                 m_language = NULL;
00442 
00443                 // default font for all styles
00444                 SetViewEOL (g_CommonPrefs.displayEOLEnable);
00445                 SetIndentationGuides (g_CommonPrefs.indentGuideEnable);
00446                 SetEdgeMode (g_CommonPrefs.longLineOnEnable?
00447                              wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
00448                 SetViewWhiteSpace (g_CommonPrefs.whiteSpaceEnable?
00449                                    wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
00450                 SetOvertype (g_CommonPrefs.overTypeInitial);
00451                 SetReadOnly (g_CommonPrefs.readOnlyInitial);
00452                 SetWrapMode (g_CommonPrefs.wrapModeInitial?
00453                              wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
00454                 wxFont font (10, wxMODERN, wxNORMAL, wxNORMAL);
00455                 StyleSetFont (wxSTC_STYLE_DEFAULT, font);
00456                 StyleSetForeground (wxSTC_STYLE_DEFAULT, *wxBLACK);
00457                 StyleSetBackground (wxSTC_STYLE_DEFAULT, *wxWHITE);
00458                 StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY")));
00459                 StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE);
00460                 StyleSetForeground(wxSTC_STYLE_INDENTGUIDE, wxColour (_T("DARK GREY")));
00461                 InitializePrefs (DEFAULT_LANGUAGE);
00462 
00463                 // set visibility
00464                 SetVisiblePolicy (wxSTC_VISIBLE_STRICT|wxSTC_VISIBLE_SLOP, 1);
00465                 SetXCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
00466                 SetYCaretPolicy (wxSTC_CARET_EVEN|wxSTC_VISIBLE_STRICT|wxSTC_CARET_SLOP, 1);
00467 
00468                 // markers
00469                 MarkerDefine (wxSTC_MARKNUM_FOLDER,        wxSTC_MARK_DOTDOTDOT, _T("BLACK"), _T("BLACK"));
00470                 MarkerDefine (wxSTC_MARKNUM_FOLDEROPEN,    wxSTC_MARK_ARROWDOWN, _T("BLACK"), _T("BLACK"));
00471                 MarkerDefine (wxSTC_MARKNUM_FOLDERSUB,     wxSTC_MARK_EMPTY,     _T("BLACK"), _T("BLACK"));
00472                 MarkerDefine (wxSTC_MARKNUM_FOLDEREND,     wxSTC_MARK_DOTDOTDOT, _T("BLACK"), _T("WHITE"));
00473                 MarkerDefine (wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_ARROWDOWN, _T("BLACK"), _T("WHITE"));
00474                 MarkerDefine (wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY,     _T("BLACK"), _T("BLACK"));
00475                 MarkerDefine (wxSTC_MARKNUM_FOLDERTAIL,    wxSTC_MARK_EMPTY,     _T("BLACK"), _T("BLACK"));
00476 
00477                 // miscelaneous
00478                 m_LineNrMargin = TextWidth (wxSTC_STYLE_LINENUMBER, _T("_999999"));
00479                 m_FoldingMargin = 16;
00480                 CmdKeyClear (wxSTC_KEY_TAB, 0); // this is done by the menu accelerator key
00481                 SetLayoutCache (wxSTC_CACHE_PAGE);
00482         }
00483 
00484 
00485 //----------------------------------------------------------------------------
00486 // common event handlers
00487         void MutEditFile::OnSize( wxSizeEvent& event ) {
00488                 int x = GetClientSize().x +
00489                         (g_CommonPrefs.lineNumberEnable? m_LineNrMargin: 0) +
00490                         (g_CommonPrefs.foldEnable? m_FoldingMargin: 0);
00491                 if (x > 0) SetScrollWidth (x);
00492                 event.Skip();
00493         }
00494 
00495 // edit event handlers
00496         void MutEditFile::OnEditRedo (wxCommandEvent &WXUNUSED(event)) {
00497                 if (!CanRedo()) return;
00498                 Redo ();
00499         }
00500 
00501         void MutEditFile::OnEditUndo (wxCommandEvent &WXUNUSED(event)) {
00502                 if (!CanUndo()) return;
00503                 Undo ();
00504         }
00505 
00506         void MutEditFile::OnEditClear (wxCommandEvent &WXUNUSED(event)) {
00507                 if (GetReadOnly()) return;
00508                 Clear ();
00509         }
00510 
00511         void MutEditFile::OnEditCut (wxCommandEvent &WXUNUSED(event)) {
00512                 if (GetReadOnly() || (GetSelectionEnd()-GetSelectionStart() <= 0)) return;
00513                 Cut ();
00514         }
00515 
00516         void MutEditFile::OnEditCopy (wxCommandEvent &WXUNUSED(event)) {
00517                 if (GetSelectionEnd()-GetSelectionStart() <= 0) return;
00518                 Copy ();
00519         }
00520 
00521         void MutEditFile::OnEditPaste (wxCommandEvent &WXUNUSED(event)) {
00522                 if (!CanPaste()) return;
00523                 Paste ();
00524         }
00525 
00526         void MutEditFile::OnFind (wxCommandEvent &WXUNUSED(event)) {
00527         }
00528 
00529         void MutEditFile::OnFindNext (wxCommandEvent &WXUNUSED(event)) {
00530         }
00531 
00532         void MutEditFile::OnReplace (wxCommandEvent &WXUNUSED(event)) {
00533         }
00534 
00535         void MutEditFile::OnReplaceNext (wxCommandEvent &WXUNUSED(event)) {
00536         }
00537 
00538         void MutEditFile::OnBraceMatch (wxCommandEvent &WXUNUSED(event)) {
00539                 int min = GetCurrentPos ();
00540                 int max = BraceMatch (min);
00541                 if (max > (min+1)) {
00542                         BraceHighlight (min+1, max);
00543                         SetSelection (min+1, max);
00544                 }else{
00545                         BraceBadLight (min);
00546                 }
00547         }
00548 
00549         void MutEditFile::OnGoto (wxCommandEvent &WXUNUSED(event)) {
00550         }
00551 
00552         void MutEditFile::OnEditIndentInc (wxCommandEvent &WXUNUSED(event)) {
00553                 CmdKeyExecute (wxSTC_CMD_TAB);
00554         }
00555 
00556         void MutEditFile::OnEditIndentRed (wxCommandEvent &WXUNUSED(event)) {
00557                 CmdKeyExecute (wxSTC_CMD_DELETEBACK);
00558         }
00559 
00560         void MutEditFile::OnEditSelectAll (wxCommandEvent &WXUNUSED(event)) {
00561                 SetSelection (0, GetTextLength ());
00562         }
00563 
00564         void MutEditFile::OnEditSelectLine (wxCommandEvent &WXUNUSED(event)) {
00565                 int lineStart = PositionFromLine (GetCurrentLine());
00566                 int lineEnd = PositionFromLine (GetCurrentLine() + 1);
00567                 SetSelection (lineStart, lineEnd);
00568         }
00569 
00570         void MutEditFile::OnHilightLang (wxCommandEvent &event) {
00571                 InitializePrefs (g_LanguagePrefs [event.GetId() - CM_HILIGHTFIRST].name);
00572         }
00573 
00574         void MutEditFile::OnDisplayEOL (wxCommandEvent &WXUNUSED(event)) {
00575                 SetViewEOL (!GetViewEOL());
00576         }
00577 
00578         void MutEditFile::OnIndentGuide (wxCommandEvent &WXUNUSED(event)) {
00579                 SetIndentationGuides (!GetIndentationGuides());
00580         }
00581 
00582         void MutEditFile::OnLineNumber (wxCommandEvent &WXUNUSED(event)) {
00583                 SetMarginWidth (m_LineNrID,
00584                                 GetMarginWidth (m_LineNrID) == 0? m_LineNrMargin: 0);
00585         }
00586 
00587         void MutEditFile::OnLongLineOn (wxCommandEvent &WXUNUSED(event)) {
00588                 SetEdgeMode (GetEdgeMode() == 0? wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
00589         }
00590 
00591         void MutEditFile::OnWhiteSpace (wxCommandEvent &WXUNUSED(event)) {
00592                 SetViewWhiteSpace (GetViewWhiteSpace() == 0?
00593                                    wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
00594         }
00595 
00596         void MutEditFile::OnFoldToggle (wxCommandEvent &WXUNUSED(event)) {
00597                 ToggleFold (GetFoldParent(GetCurrentLine()));
00598         }
00599 
00600         void MutEditFile::OnSetOverType (wxCommandEvent &WXUNUSED(event)) {
00601                 SetOvertype (!GetOvertype());
00602         }
00603 
00604         void MutEditFile::OnSetReadOnly (wxCommandEvent &WXUNUSED(event)) {
00605                 SetReadOnly (!GetReadOnly());
00606         }
00607 
00608         void MutEditFile::OnWrapmodeOn (wxCommandEvent &WXUNUSED(event)) {
00609                 SetWrapMode (GetWrapMode() == 0? wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
00610         }
00611 
00612         void MutEditFile::OnUseCharset (wxCommandEvent &event) {
00613                 int Nr;
00614                 int charset = GetCodePage();
00615                 switch (event.GetId()) {
00616                 case CM_CHARSETANSI: {charset = wxSTC_CHARSET_ANSI; break;}
00617                 case CM_CHARSETMAC: {charset = wxSTC_CHARSET_ANSI; break;}
00618                 }
00619                 for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {
00620                         StyleSetCharacterSet (Nr, charset);
00621                 }
00622                 SetCodePage (charset);
00623         }
00624 
00625         void MutEditFile::OnChangeCase (wxCommandEvent &event) {
00626                 switch (event.GetId()) {
00627                 case CM_CHANGELOWER: {
00628                         CmdKeyExecute (wxSTC_CMD_LOWERCASE);
00629                         break;
00630                 }
00631                 case CM_CHANGEUPPER: {
00632                         CmdKeyExecute (wxSTC_CMD_UPPERCASE);
00633                         break;
00634                 }
00635                 }
00636         }
00637 
00638         void MutEditFile::OnConvertEOL (wxCommandEvent &event) {
00639                 int eolMode = GetEOLMode();
00640                 switch (event.GetId()) {
00641                 case CM_CONVERTCR: { eolMode = wxSTC_EOL_CR; break;}
00642                 case CM_CONVERTCRLF: { eolMode = wxSTC_EOL_CRLF; break;}
00643                 case CM_CONVERTLF: { eolMode = wxSTC_EOL_LF; break;}
00644                 }
00645                 ConvertEOLs (eolMode);
00646                 SetEOLMode (eolMode);
00647         }
00648 
00650         void MutEditFile::OnMarginClick (wxStyledTextEvent &event) {
00651                 if (event.GetMargin() == 2) {
00652                         int lineClick = LineFromPosition (event.GetPosition());
00653                         int levelClick = GetFoldLevel (lineClick);
00654                         if ((levelClick & wxSTC_FOLDLEVELHEADERFLAG) > 0) {
00655                                 ToggleFold (lineClick);
00656                         }
00657                 }
00658         }
00659 
00660         void MutEditFile::OnCharAdded (wxStyledTextEvent &event) {
00661                 char chr = (char)event.GetKey();
00662                 int currentLine = GetCurrentLine();
00663                 // Change this if support for mac files with \r is needed
00664                 if (chr == '\n') {
00665                         int lineInd = 0;
00666                         if (currentLine > 0) {
00667                                 lineInd = GetLineIndentation(currentLine - 1);
00668                         }
00669                         if (lineInd == 0) return;
00670                         SetLineIndentation (currentLine, lineInd);
00671                         GotoPos(PositionFromLine (currentLine) + lineInd);
00672                 }
00673         }
00674 
00675 
00676 //----------------------------------------------------------------------------
00677 // private functions
00678         wxString MutEditFile::DeterminePrefs (const wxString &filename) {
00679 
00680                 LanguageInfo const* curInfo;
00681 
00682                 // determine language from filepatterns
00683                 int languageNr;
00684                 for (languageNr = 0; languageNr < g_LanguagePrefsSize; languageNr++) {
00685                         curInfo = &g_LanguagePrefs [languageNr];
00686                         wxString filepattern = curInfo->filepattern;
00687                         filepattern.Lower();
00688                         while (!filepattern.empty()) {
00689                                 wxString cur = filepattern.BeforeFirst (';');
00690                                 if ((cur == filename) ||
00691                                     (cur == (filename.BeforeLast ('.') + _T(".*"))) ||
00692                                     (cur == (_T("*.") + filename.AfterLast ('.')))) {
00693                                         return curInfo->name;
00694                                 }
00695                                 filepattern = filepattern.AfterFirst (';');
00696                         }
00697                 }
00698                 return wxEmptyString;
00699 
00700         }
00701 
00702         bool MutEditFile::InitializePrefs (const wxString &name) {
00703 
00704                 // initialize styles
00705                 StyleClearAll();
00706                 LanguageInfo const* curInfo = NULL;
00707 
00708                 // determine language
00709                 bool found = false;
00710                 int languageNr;
00711                 for (languageNr = 0; languageNr < g_LanguagePrefsSize; languageNr++) {
00712                         curInfo = &g_LanguagePrefs [languageNr];
00713                         if (curInfo->name == name) {
00714                                 found = true;
00715                                 break;
00716                         }
00717                 }
00718                 if (!found) return false;
00719 
00720                 // set lexer and language
00721                 SetLexer (curInfo->lexer);
00722                 m_language = curInfo;
00723 
00724                 // set margin for line numbers
00725                 SetMarginType (m_LineNrID, wxSTC_MARGIN_NUMBER);
00726                 StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (_T("DARK GREY")));
00727                 StyleSetBackground (wxSTC_STYLE_LINENUMBER, *wxWHITE);
00728                 SetMarginWidth (m_LineNrID, 0); // start out not visible
00729 
00730                 // default fonts for all styles!
00731                 int Nr;
00732                 for (Nr = 0; Nr < wxSTC_STYLE_LASTPREDEFINED; Nr++) {
00733                         wxFont font (10, wxMODERN, wxNORMAL, wxNORMAL);
00734                         StyleSetFont (Nr, font);
00735                 }
00736 
00737                 // set common styles
00738                 StyleSetForeground (wxSTC_STYLE_DEFAULT, wxColour (_T("DARK GREY")));
00739                 StyleSetForeground (wxSTC_STYLE_INDENTGUIDE, wxColour (_T("DARK GREY")));
00740 
00741                 // initialize settings
00742                 if (g_CommonPrefs.syntaxEnable) {
00743                         int keywordnr = 0;
00744                         for (Nr = 0; Nr < STYLE_TYPES_COUNT; Nr++) {
00745                                 if (curInfo->styles[Nr].type == -1) continue;
00746                                 const StyleInfo &curType = g_StylePrefs [curInfo->styles[Nr].type];
00747                                 wxFont font (curType.fontsize, wxMODERN, wxNORMAL, wxNORMAL, false,
00748                                              curType.fontname);
00749                                 StyleSetFont (Nr, font);
00750                                 if (curType.foreground) {
00751                                         StyleSetForeground (Nr, wxColour (curType.foreground));
00752                                 }
00753                                 if (curType.background) {
00754                                         StyleSetBackground (Nr, wxColour (curType.background));
00755                                 }
00756                                 StyleSetBold (Nr, (curType.fontstyle & MutSTC_STYLE_BOLD) > 0);
00757                                 StyleSetItalic (Nr, (curType.fontstyle & MutSTC_STYLE_ITALIC) > 0);
00758                                 StyleSetUnderline (Nr, (curType.fontstyle & MutSTC_STYLE_UNDERL) > 0);
00759                                 StyleSetVisible (Nr, (curType.fontstyle & MutSTC_STYLE_HIDDEN) == 0);
00760                                 StyleSetCase (Nr, curType.lettercase);
00761                                 const wxChar *pwords = curInfo->styles[Nr].words;
00762                                 if (pwords) {
00763                                         SetKeyWords (keywordnr, pwords);
00764                                         keywordnr += 1;
00765                                 }
00766                         }
00767                 }
00768 
00769                 // set margin as unused
00770                 SetMarginType (m_DividerID, wxSTC_MARGIN_SYMBOL);
00771                 SetMarginWidth (m_DividerID, 0);
00772                 SetMarginSensitive (m_DividerID, false);
00773 
00774                 // folding
00775                 SetMarginType (m_FoldingID, wxSTC_MARGIN_SYMBOL);
00776                 SetMarginMask (m_FoldingID, wxSTC_MASK_FOLDERS);
00777                 StyleSetBackground (m_FoldingID, *wxWHITE);
00778                 SetMarginWidth (m_FoldingID, 0);
00779                 SetMarginSensitive (m_FoldingID, false);
00780                 if (g_CommonPrefs.foldEnable) {
00781                         SetMarginWidth (m_FoldingID, curInfo->folds != 0? m_FoldingMargin: 0);
00782                         SetMarginSensitive (m_FoldingID, curInfo->folds != 0);
00783                         SetProperty (_T("fold"), curInfo->folds != 0? _T("1"): _T("0"));
00784                         SetProperty (_T("fold.comment"),
00785                                      (curInfo->folds & MutSTC_FOLD_COMMENT) > 0? _T("1"): _T("0"));
00786                         SetProperty (_T("fold.compact"),
00787                                      (curInfo->folds & MutSTC_FOLD_COMPACT) > 0? _T("1"): _T("0"));
00788                         SetProperty (_T("fold.preprocessor"),
00789                                      (curInfo->folds & MutSTC_FOLD_PREPROC) > 0? _T("1"): _T("0"));
00790                         SetProperty (_T("fold.html"),
00791                                      (curInfo->folds & MutSTC_FOLD_HTML) > 0? _T("1"): _T("0"));
00792                         SetProperty (_T("fold.html.preprocessor"),
00793                                      (curInfo->folds & MutSTC_FOLD_HTMLPREP) > 0? _T("1"): _T("0"));
00794                         SetProperty (_T("fold.comment.python"),
00795                                      (curInfo->folds & MutSTC_FOLD_COMMENTPY) > 0? _T("1"): _T("0"));
00796                         SetProperty (_T("fold.quotes.python"),
00797                                      (curInfo->folds & MutSTC_FOLD_QUOTESPY) > 0? _T("1"): _T("0"));
00798                 }
00799                 SetFoldFlags (wxSTC_FOLDFLAG_LINEBEFORE_CONTRACTED |
00800                               wxSTC_FOLDFLAG_LINEAFTER_CONTRACTED);
00801 
00802                 // set spaces and indention
00803                 SetTabWidth (4);
00804                 SetUseTabs (false);
00805                 SetTabIndents (true);
00806                 SetBackSpaceUnIndents (true);
00807                 SetIndent (g_CommonPrefs.indentEnable? 4: 0);
00808 
00809                 // others
00810                 SetViewEOL (g_CommonPrefs.displayEOLEnable);
00811                 SetIndentationGuides (g_CommonPrefs.indentGuideEnable);
00812                 SetEdgeColumn (80);
00813                 SetEdgeMode (g_CommonPrefs.longLineOnEnable? wxSTC_EDGE_LINE: wxSTC_EDGE_NONE);
00814                 SetViewWhiteSpace (g_CommonPrefs.whiteSpaceEnable?
00815                                    wxSTC_WS_VISIBLEALWAYS: wxSTC_WS_INVISIBLE);
00816                 SetOvertype (g_CommonPrefs.overTypeInitial);
00817                 SetReadOnly (g_CommonPrefs.readOnlyInitial);
00818                 SetWrapMode (g_CommonPrefs.wrapModeInitial?
00819                              wxSTC_WRAP_WORD: wxSTC_WRAP_NONE);
00820 
00821                 return true;
00822         }
00823 
00824         bool MutEditFile::LoadFile ()
00825         {
00826 #if wxUSE_FILEDLG
00827                 // get filname
00828                 if (!m_filename) {
00829                         wxFileDialog dlg (this, _T("Open file"), wxEmptyString, wxEmptyString,
00830                                           _T("Any file (*)|*"), wxFD_OPEN | wxFD_FILE_MUST_EXIST | wxFD_CHANGE_DIR);
00831                         if (dlg.ShowModal() != wxID_OK) return false;
00832                         m_filename = dlg.GetPath();
00833                 }
00834 
00835                 // load file
00836                 return LoadFile (m_filename);
00837 #else
00838                 return false;
00839 #endif // wxUSE_FILEDLG
00840         }
00841 
00842         bool MutEditFile::LoadFile (const wxString &filename) {
00843 
00844                 // load file in edit and clear undo
00845                 if (!filename.empty()) m_filename = filename;
00846 //     wxFile file (m_filename);
00847 //     if (!file.IsOpened()) return false;
00848                 ClearAll ();
00849 //     long lng = file.Length ();
00850 //     if (lng > 0) {
00851 //         wxString buf;
00852 //         wxChar *buff = buf.GetWriteBuf (lng);
00853 //         file.Read (buff, lng);
00854 //         buf.UngetWriteBuf ();
00855 //         InsertText (0, buf);
00856 //     }
00857 //     file.Close();
00858 
00859                 wxStyledTextCtrl::LoadFile(m_filename);
00860 
00861                 EmptyUndoBuffer();
00862 
00863                 // determine lexer language
00864                 wxFileName fname (m_filename);
00865                 InitializePrefs (DeterminePrefs (fname.GetFullName()));
00866 
00867                 return true;
00868         }
00869 
00870         bool MutEditFile::SaveFile ()
00871         {
00872 #if wxUSE_FILEDLG
00873                 // return if no change
00874                 if (!IsModified()) return true;
00875 
00876                 // get filname
00877                 if (!m_filename) {
00878                         wxFileDialog dlg (this, _T("Save file"), wxEmptyString, wxEmptyString, _T("Any file (*)|*"),
00879                                           wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
00880                         if (dlg.ShowModal() != wxID_OK) return false;
00881                         m_filename = dlg.GetPath();
00882                 }
00883 
00884                 // save file
00885                 return SaveFile (m_filename);
00886 #else
00887                 return false;
00888 #endif // wxUSE_FILEDLG
00889         }
00890 
00891         bool MutEditFile::SaveFile (const wxString &filename) {
00892 
00893                 // return if no change
00894                 if (!IsModified()) return true;
00895 
00896 //     // save edit in file and clear undo
00897 //     if (!filename.empty()) m_filename = filename;
00898 //     wxFile file (m_filename, wxFile::write);
00899 //     if (!file.IsOpened()) return false;
00900 //     wxString buf = GetText();
00901 //     bool okay = file.Write (buf);
00902 //     file.Close();
00903 //     if (!okay) return false;
00904 //     EmptyUndoBuffer();
00905 //     SetSavePoint();
00906 
00907 //     return true;
00908 
00909                 return wxStyledTextCtrl::SaveFile(filename);
00910 
00911         }
00912 
00913         bool MutEditFile::IsModified () {
00914 
00915                 // return modified state
00916                 return (GetModify() && !GetReadOnly());
00917         }
00918 
00919 //----------------------------------------------------------------------------
00920 // MutEditProperties
00921 //----------------------------------------------------------------------------
00922 
00923         MutEditProperties::MutEditProperties (MutEditFile *edit,
00924                                         long style)
00925                 : wxDialog (edit, wxID_ANY, wxEmptyString,
00926                             wxDefaultPosition, wxDefaultSize,
00927                             style | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
00928 
00929                 // sets the application title
00930                 SetTitle (_("Properties"));
00931                 wxString text;
00932 
00933                 // fullname
00934                 wxBoxSizer *fullname = new wxBoxSizer (wxHORIZONTAL);
00935                 fullname->Add (10, 0);
00936                 fullname->Add (new wxStaticText (this, wxID_ANY, _("Full filename"),
00937                                                  wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00938                                0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL);
00939                 fullname->Add (new wxStaticText (this, wxID_ANY, edit->GetFilename()),
00940                                0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL);
00941 
00942                 // text info
00943                 wxGridSizer *textinfo = new wxGridSizer (4, 0, 2);
00944                 textinfo->Add (new wxStaticText (this, wxID_ANY, _("Language"),
00945                                                  wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00946                                0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
00947                 textinfo->Add (new wxStaticText (this, wxID_ANY, edit->m_language->name),
00948                                0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
00949                 textinfo->Add (new wxStaticText (this, wxID_ANY, _("Lexer-ID: "),
00950                                                  wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00951                                0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
00952                 text = wxString::Format (_T("%d"), edit->GetLexer());
00953                 textinfo->Add (new wxStaticText (this, wxID_ANY, text),
00954                                0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
00955                 wxString EOLtype = wxEmptyString;
00956                 switch (edit->GetEOLMode()) {
00957                 case wxSTC_EOL_CR: {EOLtype = _T("CR (Unix)"); break; }
00958                 case wxSTC_EOL_CRLF: {EOLtype = _T("CRLF (Windows)"); break; }
00959                 case wxSTC_EOL_LF: {EOLtype = _T("CR (Macintosh)"); break; }
00960                 }
00961                 textinfo->Add (new wxStaticText (this, wxID_ANY, _("Line endings"),
00962                                                  wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00963                                0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
00964                 textinfo->Add (new wxStaticText (this, wxID_ANY, EOLtype),
00965                                0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
00966 
00967                 // text info box
00968                 wxStaticBoxSizer *textinfos = new wxStaticBoxSizer (
00969                         new wxStaticBox (this, wxID_ANY, _("Informations")),
00970                         wxVERTICAL);
00971                 textinfos->Add (textinfo, 0, wxEXPAND);
00972                 textinfos->Add (0, 6);
00973 
00974                 // statistic
00975                 wxGridSizer *statistic = new wxGridSizer (4, 0, 2);
00976                 statistic->Add (new wxStaticText (this, wxID_ANY, _("Total lines"),
00977                                                   wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00978                                 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
00979                 text = wxString::Format (_T("%d"), edit->GetLineCount());
00980                 statistic->Add (new wxStaticText (this, wxID_ANY, text),
00981                                 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
00982                 statistic->Add (new wxStaticText (this, wxID_ANY, _("Total chars"),
00983                                                   wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00984                                 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
00985                 text = wxString::Format (_T("%d"), edit->GetTextLength());
00986                 statistic->Add (new wxStaticText (this, wxID_ANY, text),
00987                                 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
00988                 statistic->Add (new wxStaticText (this, wxID_ANY, _("Current line"),
00989                                                   wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00990                                 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
00991                 text = wxString::Format (_T("%d"), edit->GetCurrentLine());
00992                 statistic->Add (new wxStaticText (this, wxID_ANY, text),
00993                                 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
00994                 statistic->Add (new wxStaticText (this, wxID_ANY, _("Current pos"),
00995                                                   wxDefaultPosition, wxSize(80, wxDefaultCoord)),
00996                                 0, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL|wxLEFT, 4);
00997                 text = wxString::Format (_T("%d"), edit->GetCurrentPos());
00998                 statistic->Add (new wxStaticText (this, wxID_ANY, text),
00999                                 0, wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL|wxRIGHT, 4);
01000 
01001                 // char/line statistics
01002                 wxStaticBoxSizer *statistics = new wxStaticBoxSizer (
01003                         new wxStaticBox (this, wxID_ANY, _("Statistics")),
01004                         wxVERTICAL);
01005                 statistics->Add (statistic, 0, wxEXPAND);
01006                 statistics->Add (0, 6);
01007 
01008                 // total pane
01009                 wxBoxSizer *totalpane = new wxBoxSizer (wxVERTICAL);
01010                 totalpane->Add (fullname, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 10);
01011                 totalpane->Add (0, 6);
01012                 totalpane->Add (textinfos, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
01013                 totalpane->Add (0, 10);
01014                 totalpane->Add (statistics, 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
01015                 totalpane->Add (0, 6);
01016                 wxButton *okButton = new wxButton (this, wxID_OK, _("OK"));
01017                 okButton->SetDefault();
01018                 totalpane->Add (okButton, 0, wxALIGN_CENTER | wxALL, 10);
01019 
01020                 SetSizerAndFit (totalpane);
01021 
01022                 ShowModal();
01023         }
01024 
01025 #if wxUSE_PRINTING_ARCHITECTURE
01026 
01027 //----------------------------------------------------------------------------
01028 // MutEditPrint
01029 //----------------------------------------------------------------------------
01030 
01031         MutEditPrint::MutEditPrint (MutEditFile *edit, wxChar *title)
01032                 : wxPrintout(title) {
01033                 m_edit = edit;
01034                 m_printed = 0;
01035 
01036         }
01037 
01038         bool MutEditPrint::OnPrintPage (int page) {
01039 
01040                 wxDC *dc = GetDC();
01041                 if (!dc) return false;
01042 
01043                 // scale DC
01044                 PrintScaling (dc);
01045 
01046                 // print page
01047                 if (page == 1) m_printed = 0;
01048                 m_printed = m_edit->FormatRange (1, m_printed, m_edit->GetLength(),
01049                                                  dc, dc, m_printRect, m_pageRect);
01050 
01051                 return true;
01052         }
01053 
01054         bool MutEditPrint::OnBeginDocument (int startPage, int endPage) {
01055 
01056                 if (!wxPrintout::OnBeginDocument (startPage, endPage)) {
01057                         return false;
01058                 }
01059 
01060                 return true;
01061         }
01062 
01063         void MutEditPrint::GetPageInfo (int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) {
01064 
01065                 // initialize values
01066                 *minPage = 0;
01067                 *maxPage = 0;
01068                 *selPageFrom = 0;
01069                 *selPageTo = 0;
01070 
01071                 // scale DC if possible
01072                 wxDC *dc = GetDC();
01073                 if (!dc) return;
01074                 PrintScaling (dc);
01075 
01076                 // get print page informations and convert to printer pixels
01077                 wxSize ppiScr;
01078                 GetPPIScreen (&ppiScr.x, &ppiScr.y);
01079                 wxSize page = g_pageSetupData->GetPaperSize();
01080                 page.x = static_cast<int> (page.x * ppiScr.x / 25.4);
01081                 page.y = static_cast<int> (page.y * ppiScr.y / 25.4);
01082                 m_pageRect = wxRect (0,
01083                                      0,
01084                                      page.x,
01085                                      page.y);
01086 
01087                 // get margins informations and convert to printer pixels
01088                 wxPoint pt = g_pageSetupData->GetMarginTopLeft();
01089                 int left = pt.x;
01090                 int top = pt.y;
01091                 pt = g_pageSetupData->GetMarginBottomRight();
01092                 int right = pt.x;
01093                 int bottom = pt.y;
01094 
01095                 top = static_cast<int> (top * ppiScr.y / 25.4);
01096                 bottom = static_cast<int> (bottom * ppiScr.y / 25.4);
01097                 left = static_cast<int> (left * ppiScr.x / 25.4);
01098                 right = static_cast<int> (right * ppiScr.x / 25.4);
01099 
01100                 m_printRect = wxRect (left,
01101                                       top,
01102                                       page.x - (left + right),
01103                                       page.y - (top + bottom));
01104 
01105                 // count pages
01106                 while (HasPage (*maxPage)) {
01107                         m_printed = m_edit->FormatRange (0, m_printed, m_edit->GetLength(),
01108                                                          dc, dc, m_printRect, m_pageRect);
01109                         *maxPage += 1;
01110                 }
01111                 if (*maxPage > 0) *minPage = 1;
01112                 *selPageFrom = *minPage;
01113                 *selPageTo = *maxPage;
01114         }
01115 
01116         bool MutEditPrint::HasPage (int WXUNUSED(page)) {
01117 
01118                 return (m_printed < m_edit->GetLength());
01119         }
01120 
01121         bool MutEditPrint::PrintScaling (wxDC *dc){
01122 
01123                 // check for dc, return if none
01124                 if (!dc) return false;
01125 
01126                 // get printer and screen sizing values
01127                 wxSize ppiScr;
01128                 GetPPIScreen (&ppiScr.x, &ppiScr.y);
01129                 if (ppiScr.x == 0) { // most possible guess 96 dpi
01130                         ppiScr.x = 96;
01131                         ppiScr.y = 96;
01132                 }
01133                 wxSize ppiPrt;
01134                 GetPPIPrinter (&ppiPrt.x, &ppiPrt.y);
01135                 if (ppiPrt.x == 0) { // scaling factor to 1
01136                         ppiPrt.x = ppiScr.x;
01137                         ppiPrt.y = ppiScr.y;
01138                 }
01139                 wxSize dcSize = dc->GetSize();
01140                 wxSize pageSize;
01141                 GetPageSizePixels (&pageSize.x, &pageSize.y);
01142 
01143                 // set user scale
01144                 float scale_x = (float)(ppiPrt.x * dcSize.x) /
01145                         (float)(ppiScr.x * pageSize.x);
01146                 float scale_y = (float)(ppiPrt.y * dcSize.y) /
01147                         (float)(ppiScr.y * pageSize.y);
01148                 dc->SetUserScale (scale_x, scale_y);
01149 
01150                 return true;
01151         }
01152 
01153 #endif
01154 
01155 
01156 
01157 #if 0
01158         wxString MutEditFile::GetRange(long from, long to) const
01159 
01160         {
01161                 std::cout << "MutEditFile::GetRange ("<< from << ", " << to << ")" << std::endl;
01162                 wxString str;
01163 
01164                 if ( from >= to && to != -1 ) {
01165                         // nothing to retrieve
01166                         return str;
01167                 }
01168 
01169 #if wxUSE_RICHEDIT
01170                 if ( IsRich() ) {
01171                         int len = GetWindowTextLength(GetHwnd());
01172                         std::cout << "MutEditFile::GetRange: len = "<< len << std::endl;
01173 
01174                         if ( len > from ) {
01175                                 if ( to == -1 )
01176                                         to = len;
01177 
01178                                 std::cout << "MutEditFile::GetRange: to  = "<< to << std::endl;
01179 
01180 #if !(wxUSE_UNICODE || wxUSE_WCHAR_T)
01181                                 // we must use EM_STREAMOUT if we don't want to lose all characters
01182                                 // not representable in the current character set (EM_GETTEXTRANGE
01183                                 // simply replaces them with question marks...)
01184                                 if ( GetRichVersion() > 1 ) {
01185                                         // we must have some encoding, otherwise any 8bit chars in the
01186                                         // control are simply *lost* (replaced by '?')
01187                                         wxFontEncoding encoding = wxFONTENCODING_SYSTEM;
01188 
01189                                         wxFont font = m_defaultStyle.GetFont();
01190 
01191                                         if ( !font.Ok() )
01192                                                 font = GetFont();
01193 
01194                                         if ( font.Ok() ) {
01195                                                 encoding = font.GetEncoding();
01196                                         }
01197 
01198                                         if ( encoding == wxFONTENCODING_SYSTEM ) {
01199                                                 encoding = wxLocale::GetSystemEncoding();
01200                                         }
01201 
01202                                         if ( encoding == wxFONTENCODING_SYSTEM ) {
01203                                                 encoding = wxFONTENCODING_ISO8859_1;
01204                                         }
01205 
01206                                         str = StreamOut(encoding);
01207 
01208                                         std::cout << "str = StreamOut(encoding);" << std::endl;
01209 
01210                                         std::cout << "MutEditFile::GetRange: str.Len  = "<< (str.Len()) << std::endl;
01211 
01212                                         if ( !str.empty() ) {
01213                                                 // we have to manually extract the required part, luckily
01214                                                 // this is easy in this case as EOL characters in str are
01215                                                 // just LFs because we remove CRs in mutRichEditStreamOut
01216                                                 str = str.Mid(from, to - from);
01217                                                 std::cout << "MutEditFile::GetRange: str.Len  = "<< (str.Len()) << std::endl;
01218                                         }
01219                                 }
01220 
01221                                 // StreamOut() wasn't used or failed, try to do it in normal way
01222                                 if ( str.empty() )
01223 #endif // !wxUSE_UNICODE
01224                                 {
01225                                         // alloc one extra WORD as needed by the control
01226                                         wxStringBuffer tmp(str, ++len);
01227                                         wxChar *p = tmp;
01228 
01229                                         TEXTRANGE textRange;
01230                                         textRange.chrg.cpMin = from;
01231                                         textRange.chrg.cpMax = to;
01232                                         textRange.lpstrText = p;
01233 
01234                                         (void)::SendMessage(GetHwnd(), EM_GETTEXTRANGE,
01235                                                             0, (LPARAM)&textRange);
01236 
01237                                         if ( m_verRichEdit > 1 )
01238                                         {
01239                                                 // RichEdit 2.0 uses just CR ('\r') for the
01240                                                 // newlines which is neither Unix nor Windows
01241                                                 // style - convert it to something reasonable
01242 
01243                                                 for ( ; *p; p++ ) {
01244                                                         if ( *p == _T('\r') )
01245                                                                 *p = _T('\n');
01246                                                 }
01247                                         }
01248                                 }
01249 
01250                                 if ( m_verRichEdit == 1 ) {
01251                                         // convert to the canonical form - see comment below
01252                                         str = wxTextFile::Translate(str, wxTextFileType_Unix);
01253                                 }
01254                         }
01255 
01256                         //else: no text at all, leave the string empty
01257                 } else
01258 #endif // wxUSE_RICHEDIT {
01259                         // retrieve all text
01260                         str = wxGetWindowText(GetHWND());
01261 
01262                 std::cout << "str = wxGetWindowText(GetHWND());" << std::endl;
01263 
01264                 std::cout << "MutEditFile::GetRange: str.Len  = "<< (str.Len()) << std::endl;
01265 
01266                 // need only a range?
01267                 if ( from < to ) {
01268                         str = str.Mid(from, to - from);
01269                         std::cout << "MutEditFile::GetRange: str.Len  = "<< (str.Len()) << std::endl;
01270                 }
01271 
01272                 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
01273                 // canonical one (same one as above) for consistency with the other kinds
01274                 // of controls and, more importantly, with the other ports
01275                 str = wxTextFile::Translate(str, wxTextFileType_Unix);
01276 
01277                 std::cout << "MutEditFile::GetRange: str.Len  = "<< (str.Len()) << std::endl;
01278         }
01279 
01280         return str;
01281 }
01282 
01283 
01284 #if wxUSE_RICHEDIT && (!(wxUSE_UNICODE || wxUSE_WCHAR_T) || wxUSE_UNICODE_MSLU)
01285 
01286 // a small class used to set m_updatesCount to 0 (to filter duplicate events if
01287 // necessary) and to reset it back to -1 afterwards
01288 
01289 class UpdatesCountFilter
01290 {
01291 
01292 public:
01293         UpdatesCountFilter(int& count)
01294                 : m_count(count)
01295                 {
01296                         wxASSERT_MSG( m_count == -1 || m_count == -2,
01297                                       _T("wrong initial m_updatesCount value") );
01298 
01299                         if (m_count != -2)
01300                                 m_count = 0;
01301 
01302                         //else: we don't want to count how many update events we get as we're going
01303                         //      to ignore all of them
01304                 }
01305 
01306         ~UpdatesCountFilter()
01307                 {
01308                         m_count = -1;
01309                 }
01310 
01311         // return true if an event has been received
01312 
01313         bool GotUpdate() const
01314                 {
01315                         return m_count == 1;
01316                 }
01317 
01318 private:
01319 
01320         int& m_count;
01321 
01322         DECLARE_NO_COPY_CLASS(UpdatesCountFilter)
01323 };
01324 
01325 
01326 
01327 // TODO: using memcpy() would improve performance a lot for big amounts of text
01328 
01329 DWORD CALLBACK
01330 mutRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb)
01331 {
01332         *pcb = 0;
01333 
01334         const wchar_t ** const ppws = (const wchar_t **)dwCookie;
01335 
01336         wchar_t *wbuf = (wchar_t *)buf;
01337 
01338         const wchar_t *wpc = *ppws;
01339 
01340         while ( cb && *wpc ) {
01341                 *wbuf++ = *wpc++;
01342 
01343                 cb -= sizeof(wchar_t);
01344                 (*pcb) += sizeof(wchar_t);
01345         }
01346 
01347         *ppws = wpc;
01348 
01349         return 0;
01350 }
01351 
01352 // helper struct used to pass parameters from wxTextCtrl to mutRichEditStreamOut
01353 
01354 struct wxStreamOutData
01355 {
01356         wchar_t *wpc;
01357         size_t len;
01358 };
01359 
01360 DWORD CALLBACK
01361 mutRichEditStreamOut(DWORD_PTR dwCookie, BYTE *buf, LONG cb, LONG *pcb)
01362 {
01363         std::cout << "::mutRichEditStreamOut: dwCookie = " << dwCookie <<
01364                 " buf = " << buf <<
01365                 " cb = " << cb << " pcb = " << pcb << std::endl;
01366         *pcb = 0;
01367 
01368         wxStreamOutData *data = (wxStreamOutData *)dwCookie;
01369 
01370         const wchar_t *wbuf = (const wchar_t *)buf;
01371 
01372         wchar_t *wpc = data->wpc;
01373 
01374         std::cout << "::mutRichEditStreamOut: wpc = " << wpc << std::endl;
01375 
01376         while ( cb ) {
01377                 wchar_t wch = *wbuf++;
01378                 char string[sizeof(wchar_t)+1];
01379                 string[sizeof(wchar_t)] = 0;
01380                 (wchar_t &)*string = wch;
01381 
01382                 std::cout << string;
01383 
01384                 // turn "\r\n" into "\n" on the fly
01385 //        if ( wch != L'\r' ) {
01386                 *wpc++ = wch;
01387 //        } else {
01388 //            data->len--;
01389 //              }
01390                 cb -= sizeof(wchar_t);
01391                 (*pcb) += sizeof(wchar_t);
01392         }
01393 
01394         data->wpc = wpc;
01395 
01396         std::cout << "::mutRichEditStreamOut: wpc = " << wpc << std::endl;
01397 
01398         return 0;
01399 }
01400 
01401 
01402 #if wxUSE_UNICODE_MSLU
01403 #define UNUSED_IF_MSLU(param)
01404 #else
01405 #define UNUSED_IF_MSLU(param) param
01406 #endif
01407 
01408 bool
01409 
01410 MutEditFile::StreamIn(const wxString& value,
01411                       wxFontEncoding UNUSED_IF_MSLU(encoding),
01412                       bool selectionOnly)
01413 {
01414 #if wxUSE_UNICODE_MSLU
01415 
01416         const wchar_t *wpc = value.c_str();
01417 
01418 #else // !wxUSE_UNICODE_MSLU
01419         wxCSConv conv(encoding);
01420 
01421         const size_t len = conv.MB2WC(NULL, value, value.length());
01422 
01423 #if wxUSE_WCHAR_T
01424         wxWCharBuffer wchBuf(len);
01425 
01426         wchar_t *wpc = wchBuf.data();
01427 
01428 #else
01429         wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t));
01430 
01431         wchar_t *wpc = wchBuf;
01432 
01433 #endif
01434 
01435         conv.MB2WC(wpc, value, value.length());
01436 
01437 #endif // wxUSE_UNICODE_MSLU
01438 
01439         // finally, stream it in the control
01440         EDITSTREAM eds;
01441 
01442         wxZeroMemory(eds);
01443 
01444         eds.dwCookie = (DWORD)&wpc;
01445 
01446         // the cast below is needed for broken (very) old mingw32 headers
01447         eds.pfnCallback = (EDITSTREAMCALLBACK)mutRichEditStreamIn;
01448 
01449         // same problem as in DoWriteText(): we can get multiple events here
01450         UpdatesCountFilter ucf(m_updatesCount);
01451 
01452         ::SendMessage(GetHwnd(), EM_STREAMIN,
01453                       SF_TEXT |
01454                       SF_UNICODE |
01455                       (selectionOnly ? SFF_SELECTION : 0),
01456                       (LPARAM)&eds);
01457 
01458         // It's okay for EN_UPDATE to not be sent if the selection is empty and
01459         // the text is empty, otherwise warn the programmer about it.
01460         wxASSERT_MSG( ucf.GotUpdate() || ( !HasSelection() && value.empty() ),
01461                       _T("EM_STREAMIN didn't send EN_UPDATE?") );
01462 
01463         if ( eds.dwError ) {
01464                 wxLogLastError(_T("EM_STREAMIN"));
01465         }
01466 
01467 #if !wxUSE_WCHAR_T
01468         free(wchBuf);
01469 
01470 #endif // !wxUSE_WCHAR_T
01471 
01472         return true;
01473 }
01474 
01475 #if !wxUSE_UNICODE_MSLU
01476 
01477 wxString
01478 MutEditFile::StreamOut(wxFontEncoding encoding, bool selectionOnly) const
01479 {
01480         wxString out;
01481 
01482         const int len = GetWindowTextLength(GetHwnd());
01483 
01484         std::cout << "MutEditFile::StreamOut: len = " << len << std::endl;
01485 
01486 //#if wxUSE_WCHAR_T
01487 //    wxWCharBuffer wchBuf(len+1);
01488 //    wchar_t *wpc = wchBuf.data();
01489 //#else
01490         wchar_t *wchBuf = (wchar_t *)malloc((len + 2)*sizeof(wchar_t));
01491 
01492         wchar_t *wpc = wchBuf;
01493 
01494 //#endif
01495 
01496         wxStreamOutData data;
01497 
01498         data.wpc = wpc;
01499 
01500         data.len = len+1;
01501 
01502         EDITSTREAM eds;
01503 
01504         wxZeroMemory(eds);
01505 
01506         eds.dwCookie = (DWORD)&data;
01507 
01508         eds.pfnCallback = mutRichEditStreamOut;
01509 
01510         ::SendMessage
01511                   (
01512                           GetHwnd(),
01513                           EM_STREAMOUT,
01514                           SF_TEXT | SF_UNICODE | (selectionOnly ? SFF_SELECTION : 0),
01515                           (LPARAM)&eds
01516                           );
01517 
01518         if ( eds.dwError ) {
01519                 wxLogLastError(_T("EM_STREAMOUT"));
01520         } else // streamed out ok
01521         {
01522                 // NUL-terminate the string because its length could have been
01523                 // decreased by mutRichEditStreamOut
01524 //#ifdef wxUSE_WCHAR_T
01525 //        *(wchBuf.data() + data.len) = _T('\0');
01526 //#else
01527                 wchBuf[data.len] = 0;
01528 //#endif
01529                 std::cout << "MutEditFile::StreamOut: data.len = " << (data.len) << std::endl;
01530                 wxString test("test");
01531 
01532                 // now convert to the given encoding (this is a possibly lossful
01533                 // conversion but what else can we do)
01534                 wxCSConv conv(encoding);
01535                 size_t lenNeeded = conv.WC2MB(NULL, wchBuf, 0);
01536 //        size_t lenNeeded = conv.MB2WC( wchBuf, NULL,0);
01537                 std::cout << "MutEditFile::StreamOut: lenNeeded = " << lenNeeded << std::endl;
01538                 std::cout << wchBuf[len] << " " << std:: endl;
01539                 std::cout << wchBuf[lenNeeded] << " " << std::endl;
01540 //              std::cout << (wxStringBuffer(out,lenNeeded+1)[lenNeeded]) << std::endl;
01541                 std::cout << "MutEditFile::StreamOut: len = " << len << std::endl;
01542                 out.Alloc(lenNeeded+1);
01543                 //wxStringBuffer buf(out, lenNeeded+1);
01544 
01545                 if ( lenNeeded++ ) {
01546                         char string[sizeof(wchar_t)+1];
01547                         string[sizeof(wchar_t)] = 0;
01548 
01549                         for (int i=0; i<= len; i++) {
01550                                 (wchar_t &)*string = wchBuf[i];
01551 
01552                                 std::cout << i << ": " << string << std::endl;
01553                                 out[i] = *string;
01554                                 std::cout<< out<<std::endl;
01555                         }
01556 
01557 //            conv.WC2MB(wxStringBuffer(out, lenNeeded+1), wchBuf, lenNeeded);
01558 //                      conv.MB2WC(wchBuf,wxStringBuffer(out,lenNeeded+1),lenNeeded/2);
01559                 }
01560 
01561                 std::cout << "MutEditFile::StreamOut: lenNeeded = " << lenNeeded << std::endl;
01562 
01563 
01564 //              std::cout << "MutEditFile::StreamOut: test.Len() = " << (test.Len()) << std::endl;
01565 //              out = test;
01566         }
01567 
01568 
01569 //#if !wxUSE_WCHAR_T
01570         free(wchBuf);
01571 
01572 //#endif // !wxUSE_WCHAR_T
01573 
01574         return out;
01575 }
01576 
01577 #endif // !wxUSE_UNICODE_MSLU
01578 
01579 #endif // wxUSE_RICHEDIT
01580 #endif
01581 
01582 }

Erzeugt am Sun Aug 21 2011 10:51:54 für Mutabor von doxygen 1.7.4