2 * \file ControlDocument.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "ControlDocument.h"
16 #include "BranchList.h"
18 #include "bufferparams.h"
19 #include "funcrequest.h"
22 #include "lyxtextclasslist.h"
24 #include "frontends/LyXView.h"
26 #include "support/std_sstream.h"
28 using std::ostringstream;
32 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
33 : ControlDialogBD(lv, d), bp_(0)
37 ControlDocument::~ControlDocument()
41 void ControlDocument::setParams()
44 bp_.reset(new BufferParams);
46 /// Set the buffer parameters
47 *bp_ = buffer()->params();
51 BufferParams & ControlDocument::params()
53 BOOST_ASSERT(bp_.get());
58 LyXTextClass ControlDocument::textClass()
60 return textclasslist[bp_->textclass];
66 void dispatch_params(LyXView & lv, BufferParams const & bp, kb_action lfun)
70 ss << "\\end_header\n";
71 lv.dispatch(FuncRequest(lfun, ss.str()));
77 void ControlDocument::apply()
79 if (!bufferIsAvailable())
84 // This must come first so that a language change is correctly noticed
87 // Set the document class.
88 lyx::textclass_type const old_class = buffer()->params().textclass;
89 lyx::textclass_type const new_class = bp_->textclass;
91 if (new_class != old_class) {
92 string const name = textclasslist[new_class].name();
93 lv_.dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
96 // Apply the BufferParams.
97 dispatch_params(lv_, params(), LFUN_BUFFERPARAMS_APPLY);
99 // Generate the colours requested by each new branch.
100 BranchList & branchlist = params().branchlist();
101 if (branchlist.empty())
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));
118 // Open insets of selected branches, close deselected ones
119 lv_.dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, "toggle branch"));
123 void ControlDocument::setLanguage()
125 Language const * const newL = bp_->language;
126 if (buffer()->params().language == newL)
129 string const lang_name = newL->lang();
130 lv_.dispatch(FuncRequest(LFUN_LANGUAGE_BUFFER, lang_name));
134 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
136 string const name = textclasslist[tc].name();
137 lv_.dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
139 // Report back whether we were able to change the class.
140 bool const success = textclasslist[tc].loaded();
145 void ControlDocument::saveAsDefault()
147 dispatch_params(lv_, params(), LFUN_SAVE_AS_DEFAULT);