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