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