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 640, 100, 640, 100, 0, 0, 1)
122 this->mwindow = mwindow;
123 this->thread = thread;
126 void TipWindowGUI::create_objects()
129 add_subwindow(tip_text = new BC_Title(x, y, thread->get_current_tip(0)));
131 BC_CheckBox *checkbox;
132 add_subwindow(checkbox = new TipDisable(mwindow, this, x, y));
134 y = get_h() - TipClose::calculate_h(mwindow) - 10;
135 x = get_w() - TipClose::calculate_w(mwindow) - 10;
136 add_subwindow(button = new TipClose(mwindow, this, x, y));
137 x -= TipNext::calculate_w(mwindow) + 10;
138 add_subwindow(button = new TipNext(mwindow, this, x, y));
139 x -= TipPrev::calculate_w(mwindow) + 10;
140 add_subwindow(button = new TipPrev(mwindow, this, x, y));
141 x += button->get_w() + 10;
147 int TipWindowGUI::keypress_event()
149 switch(get_keypress()) {
161 TipDisable::TipDisable(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
162 : BC_CheckBox(x, y, mwindow->preferences->use_tipwindow,
163 _("Show tip of the day."))
165 this->mwindow = mwindow;
169 int TipDisable::handle_event()
171 mwindow->preferences->use_tipwindow = get_value();
177 TipNext::TipNext(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
178 : BC_Button(x, y, mwindow->theme->get_image_set("next_tip"))
180 this->mwindow = mwindow;
182 set_tooltip(_("Next tip"));
184 int TipNext::handle_event()
186 gui->thread->next_tip();
190 int TipNext::calculate_w(MWindow *mwindow)
192 return mwindow->theme->get_image_set("next_tip")[0]->get_w();
197 TipPrev::TipPrev(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
198 : BC_Button(x, y, mwindow->theme->get_image_set("prev_tip"))
200 this->mwindow = mwindow;
202 set_tooltip(_("Previous tip"));
205 int TipPrev::handle_event()
207 gui->thread->prev_tip();
210 int TipPrev::calculate_w(MWindow *mwindow)
212 return mwindow->theme->get_image_set("prev_tip")[0]->get_w();
216 TipClose::TipClose(MWindow *mwindow, TipWindowGUI *gui, int x, int y)
217 : BC_Button(x, y, mwindow->theme->get_image_set("close_tip"))
219 this->mwindow = mwindow;
221 set_tooltip(_("Close"));
224 int TipClose::handle_event()
230 int TipClose::calculate_w(MWindow *mwindow)
232 return mwindow->theme->get_image_set("close_tip")[0]->get_w();
234 int TipClose::calculate_h(MWindow *mwindow)
236 return mwindow->theme->get_image_set("close_tip")[0]->get_h();