]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
fix preamble dialog
[lyx.git] / src / paragraph_funcs.C
1 /**
2  * \file paragraph_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "paragraph_funcs.h"
14 #include "buffer.h"
15 #include "ParagraphParameters.h"
16 #include "lyxtextclasslist.h"
17 #include "debug.h"
18
19 using lyx::pos_type;
20 //using lyx::layout_type;
21 using std::endl;
22
23
24 void breakParagraph(BufferParams const & bparams,
25                     Paragraph * par,
26                     pos_type pos,
27                     int flag)
28 {
29         // create a new paragraph
30         Paragraph * tmp = new Paragraph(par);
31         // without doing that we get a crash when typing <Return> at the
32         // end of a paragraph
33         tmp->layout(bparams.getLyXTextClass().defaultLayout());
34         // remember to set the inset_owner
35         tmp->setInsetOwner(par->inInset());
36
37         // this is an idea for a more userfriendly layout handling, I will
38         // see what the users say
39
40         // layout stays the same with latex-environments
41         if (flag) {
42                 tmp->layout(par->layout());
43                 tmp->setLabelWidthString(par->params().labelWidthString());
44         }
45
46         bool const isempty = (par->layout()->keepempty && par->empty());
47
48         if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
49                 tmp->layout(par->layout());
50                 tmp->params().align(par->params().align());
51                 tmp->setLabelWidthString(par->params().labelWidthString());
52
53                 tmp->params().lineBottom(par->params().lineBottom());
54                 par->params().lineBottom(false);
55                 tmp->params().pagebreakBottom(par->params().pagebreakBottom());
56                 par->params().pagebreakBottom(false);
57                 tmp->params().spaceBottom(par->params().spaceBottom());
58                 par->params().spaceBottom(VSpace(VSpace::NONE));
59
60                 tmp->params().depth(par->params().depth());
61                 tmp->params().noindent(par->params().noindent());
62
63                 // copy everything behind the break-position
64                 // to the new paragraph
65                 pos_type pos_end = par->size() - 1;
66                 pos_type i = pos;
67                 pos_type j = pos;
68                 for (; i <= pos_end; ++i) {
69                         par->cutIntoMinibuffer(bparams, i);
70                         if (tmp->insertFromMinibuffer(j - pos))
71                                 ++j;
72                 }
73                 for (i = pos_end; i >= pos; --i) {
74                         par->erase(i);
75                 }
76         }
77
78         if (pos)
79                 return;
80  
81         tmp->params().lineTop(par->params().lineTop());
82         tmp->params().pagebreakTop(par->params().pagebreakTop());
83         tmp->params().spaceTop(par->params().spaceTop());
84         tmp->bibkey = par->bibkey;
85
86         par->bibkey = 0;
87         par->params().clear();
88
89         par->layout(bparams.getLyXTextClass().defaultLayout());
90
91         // layout stays the same with latex-environments
92         if (flag) {
93                 par->layout(tmp->layout());
94                 par->setLabelWidthString(tmp->params().labelWidthString());
95                 par->params().depth(tmp->params().depth());
96         }
97 }
98
99
100 void breakParagraphConservative(BufferParams const & bparams,
101                                 Paragraph * par,
102                                 pos_type pos)
103 {
104         // create a new paragraph
105         Paragraph * tmp = new Paragraph(par);
106         tmp->makeSameLayout(par);
107
108         // When can pos > Last()?
109         // I guess pos == Last() is possible.
110         if (par->size() > pos) {
111                 // copy everything behind the break-position to the new
112                 // paragraph
113                 pos_type pos_end = par->size() - 1;
114
115                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
116                         par->cutIntoMinibuffer(bparams, i);
117                         if (tmp->insertFromMinibuffer(j - pos))
118                                 ++j;
119                 }
120
121                 for (pos_type k = pos_end; k >= pos; --k) {
122                         par->erase(k);
123                 }
124         }
125 }
126
127
128 void mergeParagraph(BufferParams const & bparams, Paragraph * par)
129 {
130         Paragraph * the_next = par->next();
131
132         // first the DTP-stuff
133         par->params().lineBottom(the_next->params().lineBottom());
134         par->params().spaceBottom(the_next->params().spaceBottom());
135         par->params().pagebreakBottom(the_next->params().pagebreakBottom());
136
137         pos_type pos_end = the_next->size() - 1;
138         pos_type pos_insert = par->size();
139
140         // ok, now copy the paragraph
141         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
142                 the_next->cutIntoMinibuffer(bparams, i);
143                 if (par->insertFromMinibuffer(pos_insert + j))
144                         ++j;
145         }
146
147         // delete the next paragraph
148         Paragraph * ppar = the_next->previous();
149         Paragraph * npar = the_next->next();
150         delete the_next;
151         ppar->next(npar);
152 }
153
154
155 #if 0
156 Paragraph * depthHook(Paragraph * par, Paragraph::depth_type depth)
157 {
158         Paragraph * newpar = par;
159
160         do {
161                 newpar = newpar->previous();
162         } while (newpar && newpar->getDepth() > depth);
163
164         if (!newpar) {
165                 if (par->previous() || par->getDepth())
166                         lyxerr << "Error (depthHook): "
167                                << "no hook." << endl;
168                 newpar = par;
169         }
170         return newpar;
171 }
172
173
174 Paragraph * outerHook(Paragraph * par)
175 {
176         if (!par->getDepth())
177                 return 0;
178         return depthHook(par, Paragraph::depth_type(par->getDepth() - 1));
179 }
180
181
182 bool isFirstInSequence(Paragraph * par)
183 {
184         Paragraph const * dhook = depthHook(par, par->getDepth());
185         return (dhook == par
186                 || dhook->getLayout() != par->getLayout()
187                 || dhook->getDepth() != par->getDepth());
188 }
189
190
191 int getEndLabel(Paragraph * para, BufferParams const & bparams)
192 {
193         Paragraph * par = para;
194         while (par) {
195                 Paragraph::depth_type par_depth = par->getDepth();
196                 layout_type layout = par->getLayout();
197                 int const endlabeltype =
198                         textclasslist.Style(bparams.textclass,
199                                             layout).endlabeltype;
200                 if (endlabeltype != END_LABEL_NO_LABEL) {
201                         if (!para->next())
202                                 return endlabeltype;
203
204                         Paragraph::depth_type const next_depth =
205                                 para->next()->getDepth();
206                         if (par_depth > next_depth ||
207                             (par_depth == next_depth
208                              && layout != para->next()->getLayout()))
209                                 return endlabeltype;
210                         break;
211                 }
212                 if (par_depth == 0)
213                         break;
214                 par = outerHook(par);
215         }
216         return END_LABEL_NO_LABEL;
217 }
218 #endif