]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
5 new lfuns, move all apply code out of ControlDocument and into the core.
[lyx.git] / src / frontends / controllers / ControlDocument.C
1 /**
2  * \file ControlDocument.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlDocument.h"
14 #include "ViewBase.h"
15
16 #include "BranchList.h"
17 #include "buffer.h"
18 #include "bufferparams.h"
19 #include "funcrequest.h"
20 #include "language.h"
21 #include "LColor.h"
22 #include "lyxtextclasslist.h"
23
24 #include "frontends/LyXView.h"
25
26 #include "support/std_sstream.h"
27
28 using std::ostringstream;
29 using std::string;
30
31
32 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
33         : ControlDialogBD(lv, d), bp_(0)
34 {}
35
36
37 ControlDocument::~ControlDocument()
38 {}
39
40
41 void ControlDocument::setParams()
42 {
43         if (!bp_.get())
44                 bp_.reset(new BufferParams);
45
46         /// Set the buffer parameters
47         *bp_ = buffer()->params();
48 }
49
50
51 BufferParams & ControlDocument::params()
52 {
53         BOOST_ASSERT(bp_.get());
54         return *bp_;
55 }
56
57
58 LyXTextClass ControlDocument::textClass()
59 {
60         return textclasslist[bp_->textclass];
61 }
62
63
64 namespace {
65
66 void dispatch_params(LyXView & lv, BufferParams const & bp, kb_action lfun)
67 {
68         ostringstream ss;
69         bp.writeFile(ss);
70         ss << "\\end_header\n";
71         lv.dispatch(FuncRequest(lfun, ss.str()));
72 }
73
74 } // namespace anon
75
76
77 void ControlDocument::apply()
78 {
79         if (!bufferIsAvailable())
80                 return;
81
82         view().apply();
83
84         // This must come first so that a language change is correctly noticed
85         setLanguage();
86
87         // Set the document class.
88         lyx::textclass_type const old_class = buffer()->params().textclass;
89         lyx::textclass_type const new_class = bp_->textclass;
90
91         if (new_class != old_class) {
92                 string const name = textclasslist[new_class].name();
93                 lv_.dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
94         }
95
96         // Apply the BufferParams.
97         dispatch_params(lv_, params(), LFUN_BUFFERPARAMS_APPLY);
98
99         // Generate the colours requested by each new branch.
100         BranchList & branchlist = params().branchlist();
101         if (branchlist.empty())
102                 return;
103
104         BranchList::const_iterator it = branchlist.begin();
105         BranchList::const_iterator const end = branchlist.end();
106         for (; it != end; ++it) {
107                 string const & current_branch = it->getBranch();
108                 Branch const * branch = branchlist.find(current_branch);
109                 string x11hexname = branch->getColor();
110                 // check that we have a valid color!
111                 if (x11hexname.empty() || x11hexname[0] != '#')
112                         x11hexname = lcolor.getX11Name(LColor::background);
113                 // display the new color
114                 string const str = current_branch  + ' ' + x11hexname;
115                 lv_.dispatch(FuncRequest(LFUN_SET_COLOR, str));
116         }
117
118         // Open insets of selected branches, close deselected ones
119         lv_.dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, "toggle branch"));
120 }
121
122
123 void ControlDocument::setLanguage()
124 {
125         Language const * const newL = bp_->language;
126         if (buffer()->params().language == newL)
127                 return;
128
129         string const lang_name = newL->lang();
130         lv_.dispatch(FuncRequest(LFUN_LANGUAGE_BUFFER, lang_name));
131 }
132
133
134 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
135 {
136         string const name = textclasslist[tc].name();
137         lv_.dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
138
139         // Report back whether we were able to change the class.
140         bool const success = textclasslist[tc].loaded();
141         return success;
142 }
143
144
145 void ControlDocument::saveAsDefault()
146 {
147         dispatch_params(lv_, params(), LFUN_SAVE_AS_DEFAULT);
148 }