Debian Codegraph @

src/widgets/flmisc.cxx

raw
1 // ----------------------------------------------------------------------------
2 // flmisc.cxx
3 //
4 // Copyright (C) 2018
5 // David Freese, W1HKJ
6 //
7 // This file is part of flmsg.
8 //
9 // Fldigi is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // Fldigi is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with fldigi. If not, see <http://www.gnu.org/licenses/>.
21 // ----------------------------------------------------------------------------
22
23 #include <config.h>
24
25 #include <string>
26 #include <cstdlib>
27 #include <cstring>
28 #include <string>
29 #include <cstdarg>
30 #include <string>
31 #include <iostream>
32
33 #include <FL/Fl.H>
34 #include <FL/Enumerations.H>
35 #include <FL/Fl_Menu_Item.H>
36 #include <FL/Fl_Tooltip.H>
37
38 #include <FL/Fl_Pixmap.H>
39 #include <FL/Fl_Window.H>
40 #include <FL/Fl_Group.H>
41 #include <FL/Fl_Box.H>
42 #include <FL/Fl_Dial.H>
43 #include <FL/Fl_Dial.H>
44 #include <FL/Fl_Return_Button.H>
45 #include <FL/fl_draw.H>
46
47 #include <FL/x.H>
48
49 #include "flmisc.h"
50 #include "pixmaps.h"
51
52 notify_dialog::notify_dialog(int X, int Y, const char* l)
53 : Fl_Window(X, Y, l), icon(10, 10, 50, 50), message(70, 25, 330, 35),
54 dial(277, 70, 23, 23), button(309, 70, 90, 23, "Close"), resize_box(399, 26, 1, 1)
55 {
56 end();
57
58 icon.image(new Fl_Pixmap(dialog_information_48_icon));
59
60 message.type(FL_MULTILINE_OUTPUT);
61 message.box(FL_FLAT_BOX);
62 message.color(FL_BACKGROUND_COLOR);
63
64 button.callback(button_cb);
65 newx = button.x();
66
67 dial.box(FL_FLAT_BOX);
68 dial.type(FL_FILL_DIAL);
69 dial.selection_color(FL_DARK_RED);
70 dial.angle1(180);
71 dial.angle2(-180);
72 dial.minimum(0.0);
73
74 xclass(PACKAGE_TARNAME);
75 resizable(resize_box);
76 }
77
78 notify_dialog::~notify_dialog()
79 {
80 delete icon.image();
81 Fl::remove_timeout(dial_timer, &dial);
82 }
83
84 int notify_dialog::handle(int event)
85 {
86 if (event == FL_HIDE && delete_on_hide) {
87 Fl::delete_widget(this);
88 return 1;
89 }
90 else if (event == FL_PUSH) {
91 dial.hide();
92 return Fl_Window::handle(event);
93 }
94 return Fl_Window::handle(event);
95 }
96
97 void notify_dialog::button_cb(Fl_Widget* w, void*)
98 {
99 w->window()->hide();
100 }
101 void notify_dialog::dial_timer(void* arg)
102 {
103 Fl_Dial* dial = reinterpret_cast<Fl_Dial*>(arg);
104 double v = dial->value();
105 if (!dial->visible())
106 return;
107 if (v == dial->minimum())
108 return dial->window()->hide();
109 dial->value(dial->clamp(v - 0.05));
110 return Fl::repeat_timeout(0.05, dial_timer, arg);
111 }
112
113 Fl_Button* notify_dialog::make_button(int W, int H)
114 {
115 Fl_Group* cur = Fl_Group::current();
116 Fl_Group::current(this);
117 Fl_Button* b = 0;
118
119 int pad = 10;
120 int X = newx - pad - W;
121 if (X - pad - dial.w() > 0) {
122 b = new Fl_Button(newx = X, button.y(), W, H);
123 dial.position(b->x() - dial.w() - pad, dial.y());
124 }
125
126 Fl_Group::current(cur);
127 return b;
128 }
129
130 void notify_dialog::notify(const char* msg, double timeout, bool delete_on_hide_)
131 {
132 delete_on_hide = delete_on_hide_;
133 message.value(msg);
134 const char* p;
135 if ((p = strchr(msg, '\n'))) { // use first line as label
136 std::string l(msg, p - msg);
137 copy_label(l.c_str());
138 }
139 else
140 label("Notification");
141
142 fl_font(message.textfont(), message.textsize());
143 int H = 0;
144 int W = 330; // default widtth of message
145 int Wmsg = 0;
146
147 std::string smsg = msg;
148 size_t pnl = 0;
149 pnl = smsg.find('\n');
150 while (!smsg.empty()) {
151 pnl = smsg.find('\n');
152 if (pnl != std::string::npos)
153 Wmsg = fl_width(smsg.substr(0, pnl).c_str());
154 else
155 Wmsg = fl_width(smsg.c_str());
156 if (pnl != std::string::npos)
157 smsg.erase(0, pnl + 1);
158 else
159 smsg.clear();
160 H++;
161 if (Wmsg > W) W = Wmsg;
162 }
163
164 message.size(W, std::max(H, 1) * fl_height());
165
166 size(w() + (W - 330), h() - 35 + std::max(H, 0) * fl_height());
167
168 if (timeout > 0.0) {
169 dial.maximum(timeout);
170 dial.value(timeout);
171 dial.show();
172 Fl::add_timeout(0.0, dial_timer, &dial);
173 }
174 else
175 dial.hide();
176 button.take_focus();
177 show();
178 }
179