Guitarix
gx_gui_helpers.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
3  * Copyright (C) 2011 Pete Shorthose
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  * ---------------------------------------------------------------------------
19  *
20  * This is the gx_head GUI related functionality
21  *
22  * ----------------------------------------------------------------------------
23  */
24 
25 #include "guitarix.h" // NOLINT
26 
27 /* --------------------------- gx_gui namespace ------------------------ */
28 namespace gx_gui {
29 
30 
31 void show_error_msg(const string& msg) {
32  Gtk::MessageDialog dialog(
33  msg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE, true);
34  dialog.set_title("gx_head");
35  dialog.set_keep_above();
36  dialog.run();
37 }
38 
39 void child_set_property(Gtk::Container& container, Gtk::Widget& child, const char *property_name, bool value) {
40  GValue v = {0};
41  g_value_init(&v, G_TYPE_BOOLEAN);
42  g_value_set_boolean(&v, value);
43  gtk_container_child_set_property(container.gobj(), child.gobj(), property_name, &v);
44 }
45 
46 Glib::ustring logarithmic_format_value(double v, int prec) {
47  if (v < -4) {
48  return Glib::ustring::format(std::setprecision(prec+1), pow(10.0,v));
49  } else {
50  return Glib::ustring::format(std::fixed, std::setprecision(prec-floor(v)), pow(10.0,v));
51  }
52 }
53 
54 int logarithmic_input_value(gpointer obj, gpointer nv)
55 {
56  GtkEntry *entry = GTK_ENTRY(obj);
57  double *new_val = static_cast<double*>(nv);
58  gchar *err = NULL;
59  *new_val = g_strtod(gtk_entry_get_text(entry), &err);
60  if (*err)
61  return GTK_INPUT_ERROR;
62  else {
63  *new_val = log10(*new_val);
64  return TRUE;
65  }
66 }
67 
68 /****************************************************************
69  ** message boxes
70  */
71 
72 static void on_gx_nchoice_map(GtkWidget *w, gpointer data) {
73  // since gx_nchoice_dialog_without_entry is only used for the
74  // jack starter dialog (FIXME: cleanup...):
75  // little hack to set the window non-modal
76  // after gtk_dialog_run() forced it to modal
77  // needed in case an error window is already open
78  // or gets opened by an background handler
79  gtk_window_set_modal(GTK_WINDOW(w), FALSE);
80 }
81 
82 // ---- choice dialog without text entry
84  const char* window_title,
85  const char* msg,
86  const guint nchoice,
87  const char* label[],
88  const gint resp[],
89  const gint default_response,
90  Glib::RefPtr<Gdk::Pixbuf> gw_ib) {
91  GtkWidget* dialog = gtk_dialog_new();
92  GtkWidget* text_label = gtk_label_new("");
93  GdkPixbuf *pb = gdk_pixbuf_scale_simple(gw_ib->gobj(), 64, 64, GDK_INTERP_BILINEAR);
94  GtkWidget* image = gtk_image_new_from_pixbuf(pb);
95 
96  gtk_label_set_markup(GTK_LABEL(text_label), msg);
97  GtkWidget * al = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
98  gtk_container_add(GTK_CONTAINER(al), text_label);
99  gtk_alignment_set_padding(GTK_ALIGNMENT(al), 10, 10, 10, 10);
100  gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), al);
101 
102  GtkWidget * ial = gtk_alignment_new(1.0, 0.5, 0.0, 0.0);
103  gtk_container_add(GTK_CONTAINER(ial), image);
104  gtk_container_add(GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(dialog))), ial);
105  for (guint i = 0; i < nchoice; i++)
106  gtk_dialog_add_button(GTK_DIALOG(dialog), label[i], resp[i]);
107 
108  // set default
109  gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
110  gtk_dialog_set_default_response(GTK_DIALOG(dialog), default_response);
111  gtk_window_set_title(GTK_WINDOW(dialog), window_title);
112  gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
113 
114  gtk_widget_show(text_label);
115  gtk_widget_show(image);
116  gtk_widget_show(al);
117  gtk_widget_show(ial);
118  g_signal_connect(dialog, "map", G_CALLBACK(on_gx_nchoice_map), NULL);
119 
120  // --- run dialog and check response
121  gint response = gtk_dialog_run(GTK_DIALOG(dialog));
122  gtk_widget_destroy(dialog);
123  return response;
124 }
125 
126 // ---- popup warning
127 int gx_message_popup(const char* msg) {
128  // check msg validity
129  if (!msg) {
130  gx_print_warning("Message Popup",
131  string(_("warning message does not exist")));
132  return -1;
133  }
134 
135  // build popup window
136  GtkWidget *about;
137  GtkWidget *label;
138  GtkWidget *ok_button;
139  about = gtk_dialog_new();
140  ok_button = gtk_button_new_from_stock(GTK_STOCK_OK);
141  label = gtk_label_new("");
142  gtk_label_set_markup(GTK_LABEL(label), msg);
143  gtk_label_set_selectable(GTK_LABEL(label), TRUE);
144  GtkWidget * al = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
145  gtk_container_add(GTK_CONTAINER(al), label);
146  gtk_alignment_set_padding(GTK_ALIGNMENT(al), 10, 10, 10, 10);
147  gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(about))), al);
148  gtk_container_add(GTK_CONTAINER(gtk_dialog_get_action_area(GTK_DIALOG(about))),
149  ok_button);
150  g_signal_connect_swapped(ok_button, "clicked",
151  G_CALLBACK(gtk_widget_destroy), about);
152 
153  //gtk_widget_set_redraw_on_allocate(GTK_WIDGET(GTK_DIALOG(about)->vbox),true);
154  gtk_widget_show(ok_button);
155  gtk_widget_show(label);
156  gtk_widget_show(al);
157  return gtk_dialog_run (GTK_DIALOG(about));
158 }
159 
160 } // end namespace gx_gui
gx_gui::gx_nchoice_dialog_without_entry
gint gx_nchoice_dialog_without_entry(const char *window_title, const char *msg, const guint nchoice, const char *label[], const gint resp[], const gint default_response, Glib::RefPtr< Gdk::Pixbuf > gw_ib)
Definition: gx_gui_helpers.cpp:82
gx_gui::gx_message_popup
int gx_message_popup(const char *)
Definition: gx_gui_helpers.cpp:126
gx_gui::logarithmic_format_value
Glib::ustring logarithmic_format_value(double v, int prec)
Definition: gx_gui_helpers.cpp:46
gx_print_warning
void gx_print_warning(const char *, const std::string &)
Definition: gx_logging.cpp:160
gx_gui
Definition: gx_gui_helpers.h:28
gx_gui::show_error_msg
void show_error_msg(const string &msg)
gx_gui::logarithmic_input_value
int logarithmic_input_value(gpointer obj, gpointer nv)
Definition: gx_gui_helpers.cpp:54
gx_gui::child_set_property
void child_set_property(Gtk::Container &container, Gtk::Widget &child, const char *property_name, bool value)
Definition: gx_gui_helpers.cpp:39
guitarix.h