4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "bcdisplayinfo.h"
25 #include "bcresources.h"
26 #include "bcsignals.h"
32 #include "mainsession.h"
34 #include "preferences.h"
36 #include "tipwindow.h"
39 // Table of tips of the day
43 TipWindow::TipWindow(MWindow *mwindow)
46 this->mwindow = mwindow;
49 TipWindow::~TipWindow()
54 void TipWindow::load_tips(const char *lang)
56 tips.remove_all_objects();
57 char tip[0x10000]; tip[0] = 0;
58 char string[BCTEXTLEN];
59 sprintf(string, "%s/%s.%s", File::get_cindat_path(), TIPS_FILE, lang);
60 FILE *fp = fopen(string, "r");
62 sprintf(tip, _("tip file missing:\n %s"), string);
67 while( fgets(string, sizeof(string), fp) ) {
68 if( string[0] == '\n' && string[1] == 0 && strlen(tip) > 0 ) {
79 void TipWindow::handle_close_event(int result)
84 BC_Window* TipWindow::new_gui()
86 BC_DisplayInfo display_info;
87 int x = display_info.get_abs_cursor_x();
88 int y = display_info.get_abs_cursor_y();
89 TipWindowGUI *gui = this->gui = new TipWindowGUI(mwindow, this, x, y);
90 gui->create_objects();
94 const char* TipWindow::get_current_tip(int n)
96 mwindow->session->current_tip += n;
97 if( mwindow->session->current_tip < 0 )
98 mwindow->session->current_tip = tips.size()-1;
99 else if( mwindow->session->current_tip >= tips.size() )
100 mwindow->session->current_tip = 0;
101 const char *result = tips[mwindow->session->current_tip];
102 mwindow->save_defaults();
106 void TipWindow::next_tip()
108 gui->tip_text->update(get_current_tip(1));
111 void TipWindow::prev_tip()
113 gui->tip_text->update(get_current_tip(-1));
118 TipWindowGUI::TipWindowGUI(MWindow *mwindow, TipWindow *thread, int x, int y)
119 : BC_Window(_(PROGRAM_NAME ": Tip of the day"), x, y,
120 xS(640), yS(110), xS(640), yS(110), 0, 0, 1)
122 this->mwindow = mwindow;
123 this->thread = thread;
126 void TipWindowGUI::create_objects()
128 lock_window("TipWindowGUI::create_objects");
129 int xs10 = xS(10), ys10 = yS(10), ys30 = yS(30);
130 int x = xs10, y = ys10;
131 add_subwindow(tip_text = new BC_Title(x, y, thread->get_current_tip(1)));
133 BC_CheckBox *checkbox;
134 add_subwindow(checkbox = new TipDisable(mwindow, this, x, y));
136 y = get_h() - TipClose::calculate_h(mwindow) - ys10;
137 x = get_w() - TipClose::calculate_w(mwindow) - xs10;
138 add_subwindow(button = new TipClose(mwindow, this, x, y));
139 x -= TipNext::calculate_w(mwindow) + xs10;
140 add_subwindow(button = new TipNext(mwindow, this, x, y));
141 x -= TipPrev::calculate_w(mwindow) + xs10;
142 add_subwindow(button = new TipPrev(mwindow, this, x, y));
143 x += button->get_w() + xs10;
150 int TipWindowGUI::keypress_event()
152 switch(get_keypress()) {
159 return context_help_check_and_show();
164 TipDisable::TipDisable(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
165 : BC_CheckBox(x, y, mwindow->preferences->use_tipwindow,
166 _("Show tip of the day."))
168 this->mwindow = mwindow;
172 int TipDisable::handle_event()
174 mwindow->preferences->use_tipwindow = get_value();
180 TipNext::TipNext(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
181 : BC_Button(x, y, mwindow->theme->get_image_set("next_tip"))
183 this->mwindow = mwindow;
185 set_tooltip(_("Next tip"));
187 int TipNext::handle_event()
189 gui->thread->next_tip();
193 int TipNext::calculate_w(MWindow *mwindow)
195 return mwindow->theme->get_image_set("next_tip")[0]->get_w();
200 TipPrev::TipPrev(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
201 : BC_Button(x, y, mwindow->theme->get_image_set("prev_tip"))
203 this->mwindow = mwindow;
205 set_tooltip(_("Previous tip"));
208 int TipPrev::handle_event()
210 gui->thread->prev_tip();
213 int TipPrev::calculate_w(MWindow *mwindow)
215 return mwindow->theme->get_image_set("prev_tip")[0]->get_w();
219 TipClose::TipClose(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
220 : BC_Button(x, y, mwindow->theme->get_image_set("close_tip"))
222 this->mwindow = mwindow;
224 set_tooltip(_("Close"));
227 int TipClose::handle_event()
233 int TipClose::calculate_w(MWindow *mwindow)
235 return mwindow->theme->get_image_set("close_tip")[0]->get_w();
237 int TipClose::calculate_h(MWindow *mwindow)
239 return mwindow->theme->get_image_set("close_tip")[0]->get_h();