~ubuntu-branches/debian/sid/gsmartcontrol/sid

« back to all changes in this revision

Viewing changes to .pc/05_gcc-4.7.patch/src/gsc_text_window.h

  • Committer: Package Import Robot
  • Author(s): gregor herrmann
  • Date: 2012-04-22 14:32:59 UTC
  • Revision ID: package-import@ubuntu.com-20120422143259-8kkyjp554elglaow
Tags: 0.8.6-1.2
* Non-maintainer upload.
* Fix "ftbfs with GCC-4.7": add patch 05_gcc-4.7.patch from Paul Tagliamonte
  (adds this-> qualifier).
  Closes: #667194

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
 Copyright:
 
3
      (C) 2008 - 2011  Alexander Shaduri <ashaduri 'at' gmail.com>
 
4
 License: See LICENSE_gsmartcontrol.txt
 
5
***************************************************************************/
 
6
 
 
7
#ifndef GSC_TEXT_WINDOW_H
 
8
#define GSC_TEXT_WINDOW_H
 
9
 
 
10
#include <gtkmm/window.h>
 
11
#include <gtkmm/button.h>
 
12
#include <gtkmm/accelgroup.h>
 
13
#include <gtkmm/textview.h>
 
14
#include <gtkmm/filechooserdialog.h>
 
15
#include <gdk/gdkkeysyms.h>  // GDK_Escape
 
16
 
 
17
#include "hz/debug.h"
 
18
#include "hz/fs_file.h"
 
19
 
 
20
#include "applib/app_ui_res_utils.h"
 
21
 
 
22
 
 
23
 
 
24
struct SmartctlOutputInstance { static const bool multi_instance = true; };
 
25
 
 
26
 
 
27
 
 
28
// use create() / destroy() with this class instead of new / delete!
 
29
 
 
30
 
 
31
// InstanceSwitch: e.g. supply 3 different classes to use 3 different single-instance windows.
 
32
template<class InstanceSwitch>
 
33
class GscTextWindow : public AppUIResWidget<GscTextWindow<InstanceSwitch>, InstanceSwitch::multi_instance> {
 
34
 
 
35
        public:
 
36
 
 
37
                // name of glade/ui file without a .glade/.ui extension and quotes
 
38
                APP_UI_RES_DATA_INIT(gsc_text_window);
 
39
 
 
40
                // needed for glade, not inherited from parent because of templates
 
41
                typedef GscTextWindow<InstanceSwitch> self_type;
 
42
 
 
43
 
 
44
                // glade/gtkbuilder needs this constructor
 
45
                GscTextWindow(typename Gtk::Window::BaseObjectType* gtkcobj, const app_ui_res_ref_t& ref_ui)
 
46
                                : AppUIResWidget<GscTextWindow<InstanceSwitch>, InstanceSwitch::multi_instance>(gtkcobj, ref_ui)
 
47
                {
 
48
                        // Connect callbacks
 
49
 
 
50
                        APP_GTKMM_CONNECT_VIRTUAL(delete_event);  // make sure the event handler is called
 
51
 
 
52
                        Gtk::Button* save_as_button = 0;
 
53
                        APP_UI_RES_AUTO_CONNECT(save_as_button, clicked);
 
54
 
 
55
                        Gtk::Button* close_window_button = 0;
 
56
                        APP_UI_RES_AUTO_CONNECT(close_window_button, clicked);
 
57
 
 
58
 
 
59
                        // Accelerators
 
60
 
 
61
                        Glib::RefPtr<Gtk::AccelGroup> accel_group = this->get_accel_group();
 
62
                        if (close_window_button) {
 
63
                                close_window_button->add_accelerator("clicked", accel_group, GDK_Escape,
 
64
                                                Gdk::ModifierType(0), Gtk::AccelFlags(0));
 
65
                        }
 
66
 
 
67
 
 
68
                        // ---------------
 
69
 
 
70
                        // show();  // better show later, after set_text().
 
71
 
 
72
                        default_title_ = this->get_title();
 
73
                }
 
74
 
 
75
 
 
76
                virtual ~GscTextWindow()
 
77
                { }
 
78
 
 
79
 
 
80
                void set_text(const Glib::ustring& title, const Glib::ustring& contents,
 
81
                                bool save_visible = false, bool use_monospace = false)
 
82
                {
 
83
                        this->set_title(title + " - " + default_title_);  // something - gsmartcontrol
 
84
 
 
85
                        this->contents_ = contents;  // we save it to prevent its mangling through the widget
 
86
 
 
87
                        Gtk::TextView* textview = this->template lookup_widget<Gtk::TextView*>("main_textview");
 
88
                        if (textview) {
 
89
                                Glib::RefPtr<Gtk::TextBuffer> buffer = textview->get_buffer();
 
90
                                buffer->set_text(contents);
 
91
 
 
92
                                if (use_monospace) {
 
93
                                        Glib::RefPtr<Gtk::TextTag> tag = buffer->create_tag();
 
94
                                        tag->property_family() = "Monospace";
 
95
                                        buffer->apply_tag(tag, buffer->begin(), buffer->end());
 
96
                                }
 
97
                        }
 
98
 
 
99
                        Gtk::Button* save_as_button = this->template lookup_widget<Gtk::Button*>("save_as_button");
 
100
                        if (save_as_button) {
 
101
                                if (save_visible) {
 
102
                                        save_as_button->set_sensitive(true);
 
103
                                        save_as_button->show();
 
104
                                } else {
 
105
                                        save_as_button->hide();
 
106
                                        save_as_button->set_sensitive(false);  // disable accelerators
 
107
                                }
 
108
                        }
 
109
                }
 
110
 
 
111
 
 
112
                void set_save_filename(const std::string& filename)
 
113
                {
 
114
                        save_filename_ = filename;
 
115
                }
 
116
 
 
117
 
 
118
        protected:
 
119
 
 
120
 
 
121
                // -------------------- Callbacks
 
122
 
 
123
 
 
124
                // ---------- override virtual methods
 
125
 
 
126
                // by default, delete_event calls hide().
 
127
                bool on_delete_event_before(GdkEventAny* e)
 
128
                {
 
129
                        destroy(this);  // deletes this object and nullifies instance
 
130
                        return true;  // event handled, don't call default virtual handler
 
131
                }
 
132
 
 
133
 
 
134
                // ---------- other callbacks
 
135
 
 
136
                void on_save_as_button_clicked()
 
137
                {
 
138
                        static std::string last_dir;
 
139
 
 
140
                        Gtk::FileChooserDialog dialog(*this, "Save Data As...",
 
141
                                        Gtk::FILE_CHOOSER_ACTION_SAVE);
 
142
 
 
143
                        // Add response buttons the the dialog
 
144
                        dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
 
145
                        dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_ACCEPT);
 
146
 
 
147
#if APP_GTKMM_CHECK_VERSION(2, 8, 0)
 
148
                        dialog.set_do_overwrite_confirmation(true);  // since gtkmm 2.8
 
149
#endif
 
150
 
 
151
                        if (!last_dir.empty())
 
152
                                dialog.set_current_folder(last_dir);
 
153
 
 
154
                        if (!save_filename_.empty())
 
155
                                dialog.set_current_name(save_filename_);
 
156
 
 
157
                        // Show the dialog and wait for a user response
 
158
                        int result = dialog.run();  // the main cycle blocks here
 
159
 
 
160
                        // Handle the response
 
161
                        switch (result) {
 
162
                                case Gtk::RESPONSE_ACCEPT:
 
163
                                {
 
164
                                        last_dir = dialog.get_current_folder();  // safe for the future
 
165
 
 
166
                                        std::string file = dialog.get_filename();
 
167
                                        hz::File f(file);
 
168
                                        if (!f.put_contents(this->contents_)) {  // this will send to debug_ too.
 
169
                                                gui_show_error_dialog("Cannot save data to file", f.get_error_utf8(), this);
 
170
                                        }
 
171
                                        break;
 
172
                                }
 
173
 
 
174
                                case Gtk::RESPONSE_CANCEL: case Gtk::RESPONSE_DELETE_EVENT:
 
175
                                        // nothing, the dialog is closed already
 
176
                                        break;
 
177
 
 
178
                                default:
 
179
                                        debug_out_error("app", DBG_FUNC_MSG << "Unknown dialog response code: " << result << ".\n");
 
180
                                        break;
 
181
                        }
 
182
                }
 
183
 
 
184
 
 
185
                void on_close_window_button_clicked()
 
186
                {
 
187
                        destroy(this);
 
188
                }
 
189
 
 
190
 
 
191
 
 
192
                Glib::ustring default_title_;
 
193
                Glib::ustring contents_;
 
194
                std::string save_filename_;
 
195
 
 
196
 
 
197
};
 
198
 
 
199
 
 
200
 
 
201
 
 
202
 
 
203
 
 
204
#endif