qscidocument.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // This module implements the QsciDocument class.
  2. //
  3. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of QScintilla.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. #include "Qsci/qscidocument.h"
  20. #include "Qsci/qsciscintillabase.h"
  21. // This internal class encapsulates the underlying document and is shared by
  22. // QsciDocument instances.
  23. class QsciDocumentP
  24. {
  25. public:
  26. QsciDocumentP() : doc(0), nr_displays(0), nr_attaches(1), modified(false) {}
  27. void *doc; // The Scintilla document.
  28. int nr_displays; // The number of displays.
  29. int nr_attaches; // The number of attaches.
  30. bool modified; // Set if not at a save point.
  31. };
  32. // The ctor.
  33. QsciDocument::QsciDocument()
  34. {
  35. pdoc = new QsciDocumentP();
  36. }
  37. // The dtor.
  38. QsciDocument::~QsciDocument()
  39. {
  40. detach();
  41. }
  42. // The copy ctor.
  43. QsciDocument::QsciDocument(const QsciDocument &that)
  44. {
  45. attach(that);
  46. }
  47. // The assignment operator.
  48. QsciDocument &QsciDocument::operator=(const QsciDocument &that)
  49. {
  50. if (pdoc != that.pdoc)
  51. {
  52. detach();
  53. attach(that);
  54. }
  55. return *this;
  56. }
  57. // Attach an existing document to this one.
  58. void QsciDocument::attach(const QsciDocument &that)
  59. {
  60. ++that.pdoc->nr_attaches;
  61. pdoc = that.pdoc;
  62. }
  63. // Detach the underlying document.
  64. void QsciDocument::detach()
  65. {
  66. if (!pdoc)
  67. return;
  68. if (--pdoc->nr_attaches == 0)
  69. {
  70. if (pdoc->doc && pdoc->nr_displays == 0)
  71. {
  72. QsciScintillaBase *qsb = QsciScintillaBase::pool();
  73. // Release the explicit reference to the document. If the pool is
  74. // empty then we just accept the memory leak.
  75. if (qsb)
  76. qsb->SendScintilla(QsciScintillaBase::SCI_RELEASEDOCUMENT, 0,
  77. pdoc->doc);
  78. }
  79. delete pdoc;
  80. }
  81. pdoc = 0;
  82. }
  83. // Undisplay and detach the underlying document.
  84. void QsciDocument::undisplay(QsciScintillaBase *qsb)
  85. {
  86. if (--pdoc->nr_attaches == 0)
  87. delete pdoc;
  88. else if (--pdoc->nr_displays == 0)
  89. {
  90. // Create an explicit reference to the document to keep it alive.
  91. qsb->SendScintilla(QsciScintillaBase::SCI_ADDREFDOCUMENT, 0, pdoc->doc);
  92. }
  93. pdoc = 0;
  94. }
  95. // Display the underlying document.
  96. void QsciDocument::display(QsciScintillaBase *qsb, const QsciDocument *from)
  97. {
  98. void *ndoc = (from ? from->pdoc->doc : 0);
  99. // SCI_SETDOCPOINTER appears to reset the EOL mode so save and restore it.
  100. int eol_mode = qsb->SendScintilla(QsciScintillaBase::SCI_GETEOLMODE);
  101. qsb->SendScintilla(QsciScintillaBase::SCI_SETDOCPOINTER, 0, ndoc);
  102. ndoc = qsb->SendScintillaPtrResult(QsciScintillaBase::SCI_GETDOCPOINTER);
  103. qsb->SendScintilla(QsciScintillaBase::SCI_SETEOLMODE, eol_mode);
  104. pdoc->doc = ndoc;
  105. ++pdoc->nr_displays;
  106. }
  107. // Return the modified state of the document.
  108. bool QsciDocument::isModified() const
  109. {
  110. return pdoc->modified;
  111. }
  112. // Set the modified state of the document.
  113. void QsciDocument::setModified(bool m)
  114. {
  115. pdoc->modified = m;
  116. }