]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GMathsMatrix.C
some tabular fixes for the problems reported by Helge
[lyx.git] / src / frontends / gtk / GMathsMatrix.C
1 /**
2  * \file GMathsMatrix.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Spray
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCXX_CONCEPT_CHECKS
15 #undef _GLIBCXX_CONCEPT_CHECKS
16 #endif
17 #ifdef _GLIBCPP_CONCEPT_CHECKS
18 #undef _GLIBCPP_CONCEPT_CHECKS
19 #endif
20
21 #include "GMathsMatrix.h"
22 #include "ControlMath.h"
23
24 #include "GViewBase.h"
25 #include "ghelpers.h"
26
27 #include <sstream>
28
29
30 using std::ostringstream;
31 using std::string;
32
33 namespace lyx {
34 namespace frontend {
35
36
37 GMathsMatrix::GMathsMatrix(Dialog & parent)
38         : GViewCB<ControlMath, GViewGladeB>(parent, _("Math Matrix"), false)
39 {}
40
41
42 void GMathsMatrix::doBuild()
43 {
44         string const gladeName = findGladeFile("mathMatrix");
45         xml_ = Gnome::Glade::Xml::create(gladeName);
46
47         Gtk::Button * button;
48         xml_->get_widget("Cancel",button);
49         setCancel(button);
50         xml_->get_widget("Insert",button);
51         setOK(button);
52         // No inserting matrices into readonly docs!
53         bcview().addReadOnly(button);
54
55         // Get widget pointers
56         xml_->get_widget("Top", topradio_);
57         xml_->get_widget("Bottom", bottomradio_);
58         xml_->get_widget("Center", centerradio_);
59         xml_->get_widget("Columns", columnsspin_);
60         xml_->get_widget("Rows", rowsspin_);
61         xml_->get_widget("HorzAlign", horzalignentry_);
62
63         // Make center vertical alignment the default
64         centerradio_->set_active(true);
65
66         // Allow only [clr], keep length as number of cols
67         ignoreHorzAlign_ = false;
68         horzalignentry_->signal_changed().connect(
69                 sigc::mem_fun(*this, &GMathsMatrix::updateHorzAlignEntry));
70         columnsspin_->signal_value_changed().connect(
71                 sigc::mem_fun(*this, &GMathsMatrix::updateHorzAlignEntry));
72 }
73
74
75 void GMathsMatrix::apply()
76 {
77         string const h_align = horzalignentry_->get_text();
78         int const nx =
79                 static_cast<int>(columnsspin_->get_adjustment()->get_value());
80         int const ny =
81                 static_cast<int>(rowsspin_->get_adjustment()->get_value());
82         char v_align = 'c';
83         if (topradio_->get_active())
84                 v_align = 't';
85         else if (centerradio_->get_active())
86                 v_align = 'c';
87         else if (bottomradio_->get_active())
88                 v_align = 'b';
89
90         ostringstream os;
91         os << nx << ' ' << ny << ' ' << v_align << ' ' << h_align;
92         controller().dispatchMatrix(os.str());
93 }
94
95
96 void GMathsMatrix::update()
97 {
98         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
99         bc().input(activate);
100 }
101
102
103 void GMathsMatrix::updateHorzAlignEntry()
104 {
105         if (ignoreHorzAlign_) return;
106
107         Glib::ustring orig = horzalignentry_->get_text();
108         Glib::ustring stripped;
109
110         Glib::ustring::iterator cur;
111         for (cur = orig.begin(); cur != orig.end(); ++cur) {
112           if (*cur == 'c' || *cur == 'l' ||
113               *cur == 'r' || *cur == '|')
114             stripped += *cur;
115         }
116
117         int barcount = countbars(stripped);
118         while (stripped.length() - barcount >
119                      columnsspin_->get_adjustment()->get_value()) {
120                 // erase last character of stripped
121                 stripped = stripped.erase(stripped.length() - 1,1);
122                 barcount =      countbars(stripped);
123         }
124
125         while (stripped.length() - barcount <
126                columnsspin_->get_adjustment()->get_value()) {
127                 stripped = stripped + "c";
128                 barcount =      countbars(stripped);
129         }
130
131         if (orig.compare(stripped) != 0) {
132         ignoreHorzAlign_ = true;
133                 horzalignentry_->set_text(stripped);
134                 ignoreHorzAlign_ = false;
135         }
136 }
137
138 int GMathsMatrix::countbars(Glib::ustring str)
139 {
140         int barcount = 0;
141         Glib::ustring::iterator cur = str.begin();
142         Glib::ustring::iterator end = str.end();
143         for (; cur != end; ++cur) {
144                 if (*cur == '|')
145                         ++barcount;
146         }
147         return barcount;
148 }
149
150
151 } // namespace frontend
152 } // namespace lyx