Debian Codegraph @

src/mars/mars_tab.cxx

raw
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2014
3 // David Freese, W1HKJ
4 //
5 // This file is part of flmsg
6 //
7 // flrig is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // flrig is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 // ----------------------------------------------------------------------------
20
21 #include <FL/Fl_Hold_Browser.H>
22
23 #include "gettext.h"
24 #include "status.h"
25 #include "util.h"
26
27 #include "flmsg_dialog.h"
28 #include "flmsg.h"
29
30 //Fl_Group *tab_mars = (Fl_Group *)0;
31 //Fl_Tabs *tab_mars_type = (Fl_Tabs *)0;
32
33 void mars_changed(Fl_Widget *, void *)
34 {
35 estimate();
36 }
37
38 void create_mars_tab()
39 {
40 create_mars_daily();
41 create_mars_ineei();
42 create_mars_net();
43 create_mars_army();
44 create_mars_msg();
45 }
46
47 //======================================================================
48 // MARS support functions
49 //======================================================================
50
51 Fl_Double_Window *mars_list_window = 0;
52 Fl_Hold_Browser *brws_mars_list = (Fl_Hold_Browser *)0;
53 const int mars_list_widths[] = {160, 100, 40, 160, 0};
54
55 std::string mars_list_lname;
56 std::string mars_list_call;
57 std::string mars_list_state;
58 std::string mars_list_bcall;
59 std::string mars_list_position;
60 static bool mars_list_ok = false;
61
62 void cb_list_ok(Fl_Widget *w, void *d)
63 {
64 std::string retstr = brws_mars_list->text(brws_mars_list->value());
65 mars_list_window->hide();
66
67 mars_list_call.clear();
68 mars_list_lname.clear();
69 mars_list_state.clear();
70 mars_list_bcall.clear();
71 mars_list_position.clear();
72
73 size_t p = retstr.find('\t');
74 if (p != std::string::npos) {
75 mars_list_call = retstr.substr(0, p);
76 retstr.erase(0,p+1);
77 }
78 p = retstr.find('\t');
79 if (p != std::string::npos) {
80 mars_list_lname = retstr.substr(0, p);
81 retstr.erase(0,p+1);
82 }
83 p = retstr.find('\t');
84 if (p != std::string::npos) {
85 mars_list_state = retstr.substr(0, p);
86 retstr.erase(0,p+1);
87 }
88 p = retstr.find('\t');
89 if (p != std::string::npos) {
90 mars_list_bcall = retstr.substr(0, p);
91 retstr.erase(0,p+1);
92 }
93 if (!retstr.empty()) {
94 mars_list_position = retstr;
95 }
96 mars_list_ok = true;
97 }
98
99 void cb_list_cancel(Fl_Widget *w, void *d)
100 {
101 mars_list_window->hide();
102 mars_list_ok = false;
103 }
104
105 Fl_Double_Window* mars_list_dialog() {
106 Fl_Double_Window* w = new Fl_Double_Window(562, 280, _("Roster"));
107 brws_mars_list = new Fl_Hold_Browser(1,1,560,250,"");
108 brws_mars_list->column_widths(mars_list_widths);
109 Fl_Button *cancel = new Fl_Button(280, 252, 80, 24, "cancel");
110 cancel->callback((Fl_Callback*)cb_list_cancel);
111 Fl_Button *ok = new Fl_Button(370, 252, 80, 24, "select");
112 ok->callback((Fl_Callback*)cb_list_ok);
113
114 FILE *flist = fopen(progStatus.mars_roster_file.c_str(), "r");
115 if (flist) {
116 char line[80];
117 char *retstr = fgets(line, 80, flist); // header
118 line[0] = 0;
119 if (retstr) {
120 while(fgets(line, 80, flist)) {
121 while( line[strlen(line)-1] == '\n') line[strlen(line)-1] = 0;
122 while( line[strlen(line)-1] == '\r') line[strlen(line)-1] = 0;
123 for (size_t i = 0; i < strlen(line); i++)
124 if (line[i] == ',') line[i] = '\t';
125 if (strlen(line)) brws_mars_list->add(line);
126 line[0] = 0;
127 }
128 }
129 fclose(flist);
130 } else
131 printf("could not open %s\n", progStatus.mars_roster_file.c_str());
132 w->set_modal();
133 w->end();
134 return w;
135 }
136
137 bool get_mars_list_text()
138 {
139 if (!mars_list_window) mars_list_window = mars_list_dialog();
140 mars_list_window->show();
141 while (mars_list_window->visible()) Fl::wait();
142 return mars_list_ok;
143 }
144
145 void cb_mars_fl_input2(Fl_Widget *w, void *d)
146 {
147 Fl_Input2 *inp = (Fl_Input2 *)w;
148 int pos = inp->position();
149 std::string text = inp->value();
150 ucase(text);
151 inp->value(text.c_str());
152 inp->position(pos);
153 estimate();
154 }
155
156 void cb_mars_text(Fl_Widget *w, void *d)
157 {
158 FTextEdit *inp = (FTextEdit *)w;
159 int pos = inp->insert_position();
160 std::string text = inp->buffer()->text();
161 ucase(text);
162 inp->clear();
163 inp->add(text.c_str());
164 inp->insert_position(pos);
165 estimate();
166 }
167
168 std::string notail(std::string s)
169 {
170 static std::string str;
171 str = s;
172 while (str[str.length()-1] == '\n')
173 str.erase(str.length()-1, 1);
174 return str;
175 }
176
177 std::string maxchars(std::string s, unsigned int nchars, int indent)
178 {
179 static std::string str;
180 str = notail(s);
181 if (str.length() <= nchars) return str;
182 size_t ccount = 0; // # chars on line
183 size_t lastspace = -1; // pointer to last space characer
184 size_t p = 0; // text pointer
185
186 nchars--;
187 while ( p < str.length() - 1 ) {
188 if (str[p] == '\n') {
189 ccount = 0;
190 if (indent) {
191 str.insert(lastspace + 1, indent, ' ');
192 p += indent;
193 ccount = indent;
194 lastspace = p;
195 }
196 } else if (str[p] == ' ') {
197 lastspace = p;
198 if (ccount > nchars) {
199 str.insert(lastspace + 1, indent, ' ');
200 p += indent;
201 ccount = indent;
202 lastspace = p;
203 }
204 }
205 else if (ccount > nchars) {
206 str.insert(p, "\n");
207 p++;
208 if (indent) {
209 str.insert(p, indent, ' ');
210 p += indent;
211 ccount = indent;
212 }
213 lastspace = p - 1;
214 ccount = indent;
215 }
216 p++;
217 ccount++;
218 }
219 return str;
220 }