]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
are optional arguments really only alowed in a declaration?
[lyx.git] / src / paragraph_funcs.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team. 
8  *
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         // remember to set the inset_owner
32         tmp->setInsetOwner(par->inInset());
33         
34         // this is an idea for a more userfriendly layout handling, I will
35         // see what the users say
36         
37         // layout stays the same with latex-environments
38         if (flag) {
39                 tmp->layout(par->layout());
40                 tmp->setLabelWidthString(par->params().labelWidthString());
41         }
42
43         bool isempty = (par->layout()->keepempty && par->empty());
44         
45         if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
46                 tmp->layout(par->layout());
47                 tmp->params().align(par->params().align());
48                 tmp->setLabelWidthString(par->params().labelWidthString());
49                 
50                 tmp->params().lineBottom(par->params().lineBottom());
51                 par->params().lineBottom(false);
52                 tmp->params().pagebreakBottom(par->params().pagebreakBottom());
53                 par->params().pagebreakBottom(false);
54                 tmp->params().spaceBottom(par->params().spaceBottom());
55                 par->params().spaceBottom(VSpace(VSpace::NONE));
56                 
57                 tmp->params().depth(par->params().depth());
58                 tmp->params().noindent(par->params().noindent());
59                 
60                 // copy everything behind the break-position
61                 // to the new paragraph
62                 pos_type pos_end = par->size() - 1;
63                 pos_type i = pos;
64                 pos_type j = pos;
65                 for (; i <= pos_end; ++i) {
66                         par->cutIntoMinibuffer(bparams, i);
67                         if (tmp->insertFromMinibuffer(j - pos))
68                                 ++j;
69                 }
70                 for (i = pos_end; i >= pos; --i) {
71                         par->erase(i);
72                 }
73         }
74         
75         // just an idea of me
76         if (!pos) {
77                 tmp->params().lineTop(par->params().lineTop());
78                 tmp->params().pagebreakTop(par->params().pagebreakTop());
79                 tmp->params().spaceTop(par->params().spaceTop());
80                 tmp->bibkey = par->bibkey;
81
82                 par->bibkey = 0;
83                 par->params().clear();
84
85                 par->layout(bparams.getLyXTextClass().defaultLayout());
86                 
87                 // layout stays the same with latex-environments
88                 if (flag) {
89                         par->layout(tmp->layout());
90                         par->setLabelWidthString(tmp->params().labelWidthString());
91                         par->params().depth(tmp->params().depth());
92                 }
93         }
94 }
95
96
97 void breakParagraphConservative(BufferParams const & bparams,
98                                 Paragraph * par,
99                                 pos_type pos)
100 {
101         // create a new paragraph
102         Paragraph * tmp = new Paragraph(par);
103         tmp->makeSameLayout(par);
104
105         // When can pos > Last()?
106         // I guess pos == Last() is possible.
107         if (par->size() > pos) {
108                 // copy everything behind the break-position to the new
109                 // paragraph
110                 pos_type pos_end = par->size() - 1;
111
112                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
113                         par->cutIntoMinibuffer(bparams, i);
114                         if (tmp->insertFromMinibuffer(j - pos))
115                                 ++j;
116                 }
117                 
118                 for (pos_type k = pos_end; k >= pos; --k) {
119                         par->erase(k);
120                 }
121         }
122 }
123
124
125 #if 0
126 // Be carefull, this does not make any check at all.
127 // This method has wrong name, it combined this par with the next par.
128 // In that sense it is the reverse of break paragraph. (Lgb)
129 void pasteParagraph(BufferParams const & bparams,
130                     Paragraph * par)
131 {
132         // copy the next paragraph to this one
133         Paragraph * the_next = par->next();
134
135         // first the DTP-stuff
136         par->params().lineBottom(the_next->params().lineBottom());
137         par->params().spaceBottom(the_next->params().spaceBottom());
138         par->params().pagebreakBottom(the_next->params().pagebreakBottom());
139
140         pos_type pos_end = the_next->size() - 1;
141         pos_type pos_insert = par->size();
142
143         // ok, now copy the paragraph
144         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
145                 the_next->cutIntoMinibuffer(bparams, i);
146                 if (par->insertFromMinibuffer(pos_insert + j))
147                         ++j;
148         }
149
150         // delete the next paragraph
151         Paragraph * ppar = the_next->previous();
152         Paragraph * npar = the_next->next();
153         delete the_next;
154         ppar->next(npar);
155 }
156
157
158 Paragraph * depthHook(Paragraph * par, Paragraph::depth_type depth)
159 {
160         Paragraph * newpar = par;
161
162         do {
163                 newpar = newpar->previous();
164         } while (newpar && newpar->getDepth() > depth);
165
166         if (!newpar) {
167                 if (par->previous() || par->getDepth())
168                         lyxerr << "Error (depthHook): "
169                                << "no hook." << endl;
170                 newpar = par;
171         }
172         return newpar;
173 }
174
175
176 Paragraph * outerHook(Paragraph * par) 
177 {
178         if (!par->getDepth())
179                 return 0;
180         return depthHook(par, Paragraph::depth_type(par->getDepth() - 1));
181 }
182
183
184 bool isFirstInSequence(Paragraph * par)
185 {
186         Paragraph const * dhook = depthHook(par, par->getDepth());
187         return (dhook == par
188                 || dhook->getLayout() != par->getLayout()
189                 || dhook->getDepth() != par->getDepth());
190 }
191
192
193 int getEndLabel(Paragraph * para, BufferParams const & bparams)
194 {
195         Paragraph * par = para;
196         while (par) {
197                 Paragraph::depth_type par_depth = par->getDepth();
198                 layout_type layout = par->getLayout();
199                 int const endlabeltype =
200                         textclasslist.Style(bparams.textclass,
201                                             layout).endlabeltype;
202                 if (endlabeltype != END_LABEL_NO_LABEL) {
203                         if (!para->next())
204                                 return endlabeltype;
205
206                         Paragraph::depth_type const next_depth =
207                                 para->next()->getDepth();
208                         if (par_depth > next_depth ||
209                             (par_depth == next_depth
210                              && layout != para->next()->getLayout()))
211                                 return endlabeltype;
212                         break;
213                 }
214                 if (par_depth == 0)
215                         break;
216                 par = outerHook(par);
217         }
218         return END_LABEL_NO_LABEL;
219 }
220 #endif