"Fossies" - the Fresh Open Source Software Archive

Member "gretl-2020e/gui/toolbar.c" (22 Sep 2020, 43537 Bytes) of package /linux/misc/gretl-2020e.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "toolbar.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2020d_vs_2020e.

    1 /*
    2  *  gretl -- Gnu Regression, Econometrics and Time-series Library
    3  *  Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
    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 3 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, see <http://www.gnu.org/licenses/>.
   17  *
   18  */
   19 
   20 /* toolbar.c: main-window toolbar, viewer window toolbars, etc. */
   21 
   22 #include "gretl.h"
   23 #include "console.h"
   24 #include "session.h"
   25 #include "datafiles.h"
   26 #include "selector.h"
   27 #include "textbuf.h"
   28 #include "textutil.h"
   29 #include "series_view.h"
   30 #include "model_table.h"
   31 #include "cmdstack.h"
   32 #include "dlgutils.h"
   33 #include "fileselect.h"
   34 #include "winstack.h"
   35 #include "tabwin.h"
   36 #include "fncall.h"
   37 #include "fnsave.h"
   38 #include "database.h"
   39 #include "toolbar.h"
   40 
   41 #include "uservar.h"
   42 #include "forecast.h"
   43 #include "gretl_www.h"
   44 
   45 #ifdef G_OS_WIN32
   46 # include "gretlwin32.h"
   47 #endif
   48 
   49 /* for viewer window toolbars */
   50 #include "../pixmaps/mini.tex.xpm"
   51 #include "../pixmaps/mini.tsplot.xpm"
   52 #include "../pixmaps/mini.boxplot.xpm"
   53 #include "../pixmaps/mini.pdf.xpm"
   54 #include "../pixmaps/mini.manual.xpm"
   55 #include "../pixmaps/mini.pin.xpm"
   56 #include "../pixmaps/mini.alpha.xpm"
   57 #include "../pixmaps/mini.en.xpm"
   58 #include "../pixmaps/mini.split_h.xpm"
   59 #include "../pixmaps/mini.split_v.xpm"
   60 #include "../pixmaps/mini.join_h.xpm"
   61 #include "../pixmaps/mini.join_v.xpm"
   62 #include "../pixmaps/mini.winlist.xpm"
   63 #include "../pixmaps/mini.bundle.xpm"
   64 #include "../pixmaps/mini.heatmap.xpm"
   65 #include "../pixmaps/mini.dbnomics.xpm"
   66 #include "../pixmaps/mail_16.xpm"
   67 
   68 /* for main-window toolbar */
   69 #include "../pixmaps/mini.calc.xpm"
   70 #include "../pixmaps/mini.sh.xpm"
   71 #include "../pixmaps/mini.session.xpm"
   72 #include "../pixmaps/mini.plot.xpm"
   73 #include "../pixmaps/mini.model.xpm"
   74 #include "../pixmaps/mini.func.xpm"
   75 #include "../pixmaps/mini.db.xpm"
   76 
   77 /* for window-finder */
   78 #include "../pixmaps/mini.gretl.xpm"
   79 #include "../pixmaps/mini.table.xpm"
   80 #include "../pixmaps/mini.page.xpm"
   81 #include "../pixmaps/mini.tools.xpm"
   82 
   83 /* for plot bar */
   84 #include "../pixmaps/upsize.xpm"
   85 #include "../pixmaps/downsize.xpm"
   86 #include "../pixmaps/open_menu.xpm"
   87 
   88 enum {
   89     SAVE_ITEM = 1,
   90     SAVE_AS_ITEM,
   91     EDIT_ITEM,
   92     PLOT_ITEM,
   93     EXEC_ITEM,
   94     COPY_ITEM,
   95     PRINT_ITEM,
   96     TEX_ITEM,
   97     ADD_DATA_ITEM,
   98     ADD_MATRIX_ITEM,
   99     MAIL_ITEM,
  100     HELP_ITEM,
  101     CMD_HELP_ITEM,
  102     GP_HELP_ITEM,
  103     X12A_HELP_ITEM,
  104     SORT_ITEM,
  105     SORT_BY_ITEM,
  106     FORMAT_ITEM,
  107     INDEX_ITEM,
  108     EDIT_HANSL_ITEM,
  109     STICKIFY_ITEM,
  110     ALPHA_ITEM,
  111     REFRESH_ITEM,
  112     OPEN_ITEM,
  113     SPLIT_H_ITEM,
  114     SPLIT_V_ITEM,
  115     EDITOR_ITEM,
  116     NOTES_ITEM,
  117     NEW_ITEM,
  118     CLOSE_ITEM,
  119     BUNDLE_ITEM,
  120     FIND_ITEM,
  121     COPY_SCRIPT_ITEM,
  122     BUILD_ITEM,
  123     HMAP_ITEM,
  124     DIGITS_ITEM,
  125     DBN_ITEM
  126 } viewbar_flags;
  127 
  128 struct stock_maker {
  129     char **xpm;
  130     const char *str;
  131 };
  132 
  133 void gretl_stock_icons_init (void)
  134 {
  135     struct stock_maker stocks[] = {
  136     { mini_tex_xpm, GRETL_STOCK_TEX },
  137     { mail_16_xpm, GRETL_STOCK_MAIL },
  138     { mini_tsplot_xpm, GRETL_STOCK_TS },
  139     { mini_boxplot_xpm, GRETL_STOCK_BOX },
  140     { mini_pdf_xpm, GRETL_STOCK_PDF },
  141     { mini_manual_xpm, GRETL_STOCK_BOOK },
  142     { mini_calc_xpm, GRETL_STOCK_CALC },
  143     { mini_sh_xpm, GRETL_STOCK_CONSOLE },
  144     { mini_session_xpm, GRETL_STOCK_ICONS },
  145     { mini_plot_xpm, GRETL_STOCK_SCATTER },
  146     { mini_model_xpm, GRETL_STOCK_MODEL },
  147     { mini_func_xpm, GRETL_STOCK_FUNC },
  148     { mini_pin_xpm, GRETL_STOCK_PIN },
  149     { mini_alpha_xpm, GRETL_STOCK_ALPHA },
  150     { mini_en_xpm, GRETL_STOCK_EN },
  151     { mini_split_h_xpm, GRETL_STOCK_SPLIT_H },
  152     { mini_split_v_xpm, GRETL_STOCK_SPLIT_V },
  153     { mini_join_h_xpm, GRETL_STOCK_JOIN_H },
  154     { mini_join_v_xpm, GRETL_STOCK_JOIN_V },
  155     { mini_winlist_xpm, GRETL_STOCK_WINLIST },
  156     { mini_bundle_xpm, GRETL_STOCK_BUNDLE },
  157     { mini_db_xpm, GRETL_STOCK_DB},
  158     { mini_gretl_xpm, GRETL_STOCK_GRETL},
  159     { mini_table_xpm, GRETL_STOCK_TABLE},
  160     { mini_page_xpm, GRETL_STOCK_PAGE},
  161     { mini_tools_xpm, GRETL_STOCK_TOOLS},
  162     { upsize_xpm, GRETL_STOCK_BIGGER},
  163     { downsize_xpm, GRETL_STOCK_SMALLER},
  164     { open_menu_xpm, GRETL_STOCK_MENU},
  165     { mini_heatmap_xpm, GRETL_STOCK_HMAP},
  166     { mini_dbnomics_xpm, GRETL_STOCK_DBN},
  167     };
  168     static GtkIconFactory *gretl_factory;
  169     int n = G_N_ELEMENTS(stocks);
  170 
  171     if (gretl_factory == NULL) {
  172     GtkIconSet *iset;
  173     GdkPixbuf *pbuf;
  174     int i;
  175 
  176     gretl_factory = gtk_icon_factory_new();
  177 
  178     for (i=0; i<n; i++) {
  179         pbuf = gdk_pixbuf_new_from_xpm_data((const char **) stocks[i].xpm);
  180         iset = gtk_icon_set_new_from_pixbuf(pbuf);
  181         g_object_unref(pbuf);
  182         gtk_icon_factory_add(gretl_factory, stocks[i].str, iset);
  183         gtk_icon_set_unref(iset);
  184     }
  185 
  186     gtk_icon_factory_add_default(gretl_factory);
  187     }
  188 }
  189 
  190 /* callbacks for viewer window toolbar */
  191 
  192 static void copy_to_editor (GtkWidget *w, windata_t *vwin)
  193 {
  194     gchar *buf = textview_get_text(vwin->text);
  195 
  196     if (vwin->role == VIEW_LOG) {
  197     /* allow for the possibility that the buffer is empty */
  198     gchar *s = buf;
  199     int n = 0;
  200 
  201     while (s != NULL && n < 3) {
  202         s = strchr(s, '\n');
  203         if (s != NULL) {
  204         s++;
  205         n++;
  206         }
  207     }
  208 
  209     if (s != NULL) {
  210         gchar *modbuf;
  211 
  212         modbuf = g_strdup_printf("# logged commands\n%s", s);
  213         do_new_script(EDIT_HANSL, modbuf);
  214         g_free(modbuf);
  215     } else {
  216         do_new_script(EDIT_HANSL, buf);
  217     }
  218     } else {
  219     do_new_script(EDIT_HANSL, buf);
  220     }
  221 
  222     g_free(buf);
  223 }
  224 
  225 static void save_as_callback (GtkWidget *w, windata_t *vwin)
  226 {
  227     GtkWidget *vmain = vwin_toplevel(vwin);
  228     guint u = 0;
  229 
  230     if (g_object_get_data(G_OBJECT(vmain), "text_out")) {
  231     const char *opts[] = {
  232         N_("Save to file"),
  233         N_("Save to session as icon")
  234     };
  235     int resp;
  236 
  237     resp = radio_dialog(_("gretl: save text"), _("Save text"),
  238                 opts, 2, 0, 0, vmain);
  239     if (resp < 0) {
  240         return;
  241     } else if (resp == 1) {
  242         save_output_as_text_icon(vwin);
  243         return;
  244     } else {
  245         u = SAVE_OUTPUT;
  246     }
  247     } else if (vwin->role == EDIT_HANSL) {
  248     u = SAVE_SCRIPT;
  249     } else if (vwin->role == EDIT_GP) {
  250     u = SAVE_GP_CMDS;
  251     } else if (vwin->role == EDIT_R) {
  252     u = SAVE_R_CMDS;
  253     } else if (vwin->role == EDIT_OX) {
  254     u = SAVE_OX_CMDS;
  255     } else if (vwin->role == EDIT_OCTAVE) {
  256     u = SAVE_OCTAVE_CMDS;
  257     } else if (vwin->role == EDIT_PYTHON) {
  258         u = SAVE_PYTHON_CMDS;
  259     } else if (vwin->role == EDIT_JULIA) {
  260     u = SAVE_JULIA_CODE;
  261     } else if (vwin->role == EDIT_DYNARE) {
  262     u = SAVE_DYNARE_CODE;
  263     } else if (vwin->role == EDIT_STATA) {
  264     u = SAVE_STATA_CMDS;
  265     } else if (vwin->role == EDIT_SPEC) {
  266     u = SAVE_SPEC_FILE;
  267     } else if (vwin->role == VIEW_FILE) {
  268     u = SAVE_TEXT;
  269     } else if (vwin->role == EDIT_PKG_HELP ||
  270            vwin->role == EDIT_PKG_GHLP) {
  271     u = SAVE_HELP_TEXT;
  272     } else {
  273     dummy_call();
  274     return;
  275     }
  276 
  277     file_save(vwin, u);
  278 }
  279 
  280 /* Adjust the number of significant figures used in printing
  281    coefficients and standard errors in a model viewer window,
  282    or descriptive statistics in a "summary" window. Also record
  283    the number of digits for use via Copy.
  284 */
  285 
  286 static void display_digits_callback (GtkWidget *w, windata_t *vwin)
  287 {
  288     int wdigits = widget_get_int(vwin->text, "digits");
  289     int save_digits = get_gretl_digits();
  290     int digits, resp;
  291 
  292     digits = wdigits > 0 ? wdigits : save_digits;
  293 
  294     if (vwin->role == SUMMARY) {
  295     /* desciptive statistics window */
  296     Summary *s = vwin->data;
  297     int dmax = (s->opt & OPT_S)? 4 : 5;
  298 
  299     digits = (digits > dmax)? dmax : digits;
  300     resp = spin_dialog(NULL, NULL, &digits,
  301                _("Number of digits to show for statistics"),
  302                3, dmax, 0, vwin_toplevel(vwin));
  303     } else {
  304     /* model window */
  305     resp = spin_dialog(NULL, NULL, &digits,
  306                _("Number of digits to show for coefficients"),
  307                3, 6, 0, vwin_toplevel(vwin));
  308     }
  309 
  310     if (resp != GRETL_CANCEL && digits != wdigits) {
  311     const char *buf;
  312     int save_digits;
  313     PRN *prn;
  314 
  315     if (bufopen(&prn)) return;
  316     save_digits = get_gretl_digits();
  317     set_gretl_digits(digits);
  318     if (vwin->role == SUMMARY) {
  319         print_summary(vwin->data, dataset, prn);
  320     } else {
  321         printmodel(vwin->data, dataset, OPT_NONE, prn);
  322     }
  323     buf = gretl_print_get_trimmed_buffer(prn);
  324     textview_set_text(vwin->text, buf);
  325     gretl_print_destroy(prn);
  326     set_gretl_digits(save_digits);
  327     widget_set_int(vwin->text, "digits", digits);
  328     }
  329 }
  330 
  331 static void mail_script_callback (GtkWidget *w, windata_t *vwin)
  332 {
  333     if (viewer_char_count(vwin) == 0) {
  334     infobox(_("Nothing to send"));
  335     return;
  336     }
  337 
  338     if (query_save_text(NULL, NULL, vwin)) {
  339     return;
  340     }
  341 
  342     send_attachment(vwin->fname);
  343 }
  344 
  345 /* callback for the "Open" icon in a script editing window,
  346    which enables the user to switch to a different script,
  347    or to open another tab if the editor is tab-enabled
  348 */
  349 
  350 static void file_open_callback (GtkWidget *w, windata_t *vwin)
  351 {
  352     file_selector(OPEN_SCRIPT, FSEL_DATA_VWIN, vwin);
  353 }
  354 
  355 static void open_pkg_sample (GtkWidget *w, windata_t *vwin)
  356 {
  357     if (viewer_char_count(vwin) > 0) {
  358     int resp;
  359 
  360     resp = yes_no_dialog(NULL, _("Really replace content with\n"
  361                      "a selected file?"),
  362                  vwin->main);
  363     if (resp != GRETL_YES) {
  364         return;
  365     }
  366     }
  367 
  368     file_selector(OPEN_SCRIPT, FSEL_DATA_VWIN, vwin);
  369 }
  370 
  371 static void toolbar_new_callback (GtkWidget *w, windata_t *vwin)
  372 {
  373     do_new_script(vwin->role, NULL);
  374 }
  375 
  376 static void window_print_callback (GtkWidget *w, windata_t *vwin)
  377 {
  378     if (textview_use_highlighting(vwin->role)) {
  379     int resp = yes_no_cancel_dialog(NULL,
  380                     _("Print with syntax highlighting?"),
  381                     vwin_toplevel(vwin));
  382 
  383     if (resp == GRETL_YES) {
  384         sourceview_print(vwin);
  385     } else if (resp == GRETL_NO) {
  386         window_print(NULL, vwin);
  387     }
  388     } else {
  389     window_print(NULL, vwin);
  390     }
  391 }
  392 
  393 static void window_help (GtkWidget *w, windata_t *vwin)
  394 {
  395     show_gui_help(vwin->role);
  396 }
  397 
  398 static void multi_save_as_callback (GtkWidget *w, windata_t *vwin)
  399 {
  400     copy_format_dialog(vwin, W_SAVE);
  401 }
  402 
  403 static void script_index (GtkWidget *w, windata_t *vwin)
  404 {
  405     display_files(PS_FILES, NULL);
  406 }
  407 
  408 static void toolbar_refresh (GtkWidget *w, windata_t *vwin)
  409 {
  410     if (vwin->role == VIEW_SERIES) {
  411     series_view_refresh(w, vwin);
  412     }
  413 }
  414 
  415 static void set_matrix_name (GtkWidget *widget, dialog_t *dlg)
  416 {
  417     char *vname = (char *) edit_dialog_get_data(dlg);
  418     GtkWidget *parent =  edit_dialog_get_window(dlg);
  419     const gchar *s = edit_dialog_get_text(dlg);
  420 
  421     if (s == NULL || gui_validate_varname(s, GRETL_TYPE_MATRIX, parent)) {
  422     edit_dialog_reset(dlg);
  423     } else {
  424     strcpy(vname, s);
  425     edit_dialog_close(dlg);
  426     }
  427 }
  428 
  429 static void add_matrix_callback (GtkWidget *w, windata_t *vwin)
  430 {
  431     if (vwin->role == XTAB) {
  432     char mname[VNAMELEN];
  433     gretl_matrix *m;
  434     int err, cancel = 0;
  435 
  436     blocking_edit_dialog(0, _("gretl: save matrix"),
  437                  _("Enter a name"), NULL,
  438                  set_matrix_name, mname,
  439                  VARCLICK_NONE,
  440                  vwin_toplevel(vwin),
  441                  &cancel);
  442     if (!cancel) {
  443         m = xtab_to_matrix(vwin->data);
  444         if (m == NULL) {
  445         nomem();
  446         } else {
  447         err = user_var_add_or_replace(mname,
  448                           GRETL_TYPE_MATRIX,
  449                           m);
  450         if (err) {
  451             gretl_matrix_free(m);
  452             gui_errmsg(err);
  453         } else {
  454             infobox_printf(_("Saved matrix as %s"), mname);
  455         }
  456         }
  457     }
  458     }
  459 }
  460 
  461 static void add_data_callback (GtkWidget *w, windata_t *vwin)
  462 {
  463     int oldv = dataset->v;
  464 
  465     if (vwin->role == PCA) {
  466     add_pca_data(vwin);
  467     } else if (vwin->role == LEVERAGE) {
  468     add_leverage_data(vwin);
  469     } else if (vwin->role == MAHAL) {
  470     add_mahalanobis_data(vwin);
  471     } else if (vwin->role == FCAST) {
  472     add_fcast_data(vwin, M_FCAST);
  473     } else if (vwin->role == LOESS || vwin->role == NADARWAT) {
  474     add_nonparam_data(vwin);
  475     } else if (vwin->role == VIEW_DBNOMICS) {
  476     add_dbnomics_data(vwin);
  477     }
  478 
  479     if (dataset->v > oldv) {
  480     populate_varlist();
  481     mark_dataset_as_modified();
  482     }
  483 }
  484 
  485 static void real_coeffint_set_alpha (GtkWidget *w, GtkWidget *dialog)
  486 {
  487     windata_t *vwin = g_object_get_data(G_OBJECT(dialog), "vwin");
  488     double *x = g_object_get_data(G_OBJECT(dialog), "xptr");
  489     CoeffIntervals *cf = vwin->data;
  490     GtkTextBuffer *buf;
  491     const char *newtext;
  492     PRN *prn;
  493 
  494     if (bufopen(&prn)) {
  495     return;
  496     }
  497 
  498     reset_coeff_intervals(cf, 1.0 - *x);
  499     text_print_model_confints(cf, prn);
  500     newtext = gretl_print_get_buffer(prn);
  501     buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(vwin->text));
  502     gtk_text_buffer_set_text(buf, "", -1);
  503     textview_set_text(vwin->text, newtext);
  504     gretl_print_destroy(prn);
  505 
  506     gtk_widget_destroy(dialog);
  507 }
  508 
  509 static void alpha_button_callback (GtkToggleButton *b, double *x)
  510 {
  511     if (gtk_toggle_button_get_active(b)) {
  512     int i = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(b), "i"));
  513 
  514     if (i == 0) {
  515         *x = 0.90;
  516     } else if (i == 1) {
  517         *x = 0.95;
  518     } else if (i == 2) {
  519         *x = 0.99;
  520     }
  521     }
  522 }
  523 
  524 static void reformat_callback (GtkWidget *w, windata_t *vwin)
  525 {
  526     if (vwin->role == VIEW_MODELTABLE) {
  527     format_model_table(vwin);
  528     } else {
  529     series_view_format_dialog(vwin);
  530     }
  531 }
  532 
  533 static void split_pane_callback (GtkWidget *w, windata_t *vwin)
  534 {
  535     GtkWidget *hb = g_object_get_data(G_OBJECT(w), "hpane");
  536     GtkWidget *vb = g_object_get_data(G_OBJECT(w), "vpane");
  537     int vertical = 0;
  538 
  539     if (hb != NULL) {
  540     vb = w;
  541     vertical = 1;
  542     } else {
  543     hb = w;
  544     }
  545 
  546     /* Note: by "vertical" here we mean that the split runs vertically,
  547        dividing the pane into left- and right-hand sections; otherwise
  548        the split runs horizontally. In a "gnuplot commands" window we
  549        only offer a horizontal split, which means that @vb ("vertical
  550        button") may be NULL.
  551     */
  552 
  553     if (g_object_get_data(G_OBJECT(vwin->vbox), "sw") != NULL) {
  554     /* currently in single-view mode: so split */
  555     viewer_split_pane(vwin, vertical);
  556     if (vb != NULL) {
  557         gtk_widget_set_sensitive(vb, vertical);
  558     }
  559     gtk_widget_set_sensitive(hb, !vertical);
  560     if (vertical) {
  561         gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(vb),
  562                      GRETL_STOCK_JOIN_V);
  563     } else {
  564         gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(hb),
  565                      GRETL_STOCK_JOIN_H);
  566     }
  567     } else {
  568     GtkWidget *paned;
  569 
  570     paned = g_object_get_data(G_OBJECT(vwin->vbox), "paned");
  571 
  572     if (paned != NULL) {
  573         /* currently in split-view mode: so rejoin */
  574         vertical = GTK_IS_HPANED(paned);
  575         viewer_close_pane(vwin);
  576         gtk_widget_set_sensitive(hb, TRUE);
  577         if (vb != NULL) {
  578         gtk_widget_set_sensitive(vb, TRUE);
  579         }
  580         if (vertical) {
  581         gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(vb),
  582                          GRETL_STOCK_SPLIT_V);
  583         } else {
  584         gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(hb),
  585                          GRETL_STOCK_SPLIT_H);
  586         }
  587     }
  588     }
  589 }
  590 
  591 static void toggle_alpha_spin (GtkToggleButton *b, GtkWidget *w)
  592 {
  593     gtk_widget_set_sensitive(w, gtk_toggle_button_get_active(b));
  594 }
  595 
  596 static void coeffint_set_alpha (GtkWidget *w, windata_t *vwin)
  597 {
  598     CoeffIntervals *cf = vwin->data;
  599     GtkWidget *dialog, *tmp, *hbox;
  600     GtkWidget *vbox, *b, *hb2;
  601     GSList *group = NULL;
  602     GtkAdjustment *adj;
  603     gchar txt[16];
  604     double x = 1.0 - cf->alpha;
  605     gboolean defset = FALSE;
  606     int i;
  607 
  608     if (maybe_raise_dialog()) {
  609     return;
  610     }
  611 
  612     dialog = gretl_dialog_new(_("gretl: coefficient confidence intervals"),
  613                   vwin_toplevel(vwin), GRETL_DLG_BLOCK);
  614 
  615     hbox = gtk_hbox_new(FALSE, 5);
  616     hb2 = gtk_hbox_new(FALSE, 5);
  617     tmp = gtk_label_new(_("Confidence level"));
  618     gtk_box_pack_start(GTK_BOX(hb2), tmp, FALSE, FALSE, 0);
  619 
  620     tmp = gtk_label_new("1 - α :");
  621     gtk_box_pack_start(GTK_BOX(hb2), tmp, FALSE, FALSE, 0);
  622     gtk_box_pack_start(GTK_BOX(hbox), hb2, TRUE, TRUE, 10);
  623     vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
  624     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
  625 
  626     hbox = gtk_hbox_new(FALSE, 5);
  627     vbox = gtk_vbox_new(FALSE, 5);
  628 
  629     /* radio button for 90%, 95%, 99% confidence */
  630 
  631     for (i=0; i<3; i++) {
  632     double a = (i == 0)? 0.90 : (i == 1)? 0.95 : 0.99;
  633 
  634     sprintf(txt, "%.2f", a);
  635     b = gtk_radio_button_new_with_label(group, txt);
  636     gtk_box_pack_start(GTK_BOX(vbox), b, FALSE, FALSE, 0);
  637     group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(b));
  638     if (a == x) {
  639         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b), TRUE);
  640         defset = TRUE;
  641     } else {
  642         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b), FALSE);
  643     }
  644     g_object_set_data(G_OBJECT(b), "i", GINT_TO_POINTER(i));
  645     g_signal_connect(G_OBJECT(b), "toggled",
  646              G_CALLBACK(alpha_button_callback),
  647              &x);
  648     }
  649 
  650     /* radio button for "other" confidence level, plus spinner */
  651 
  652     hb2 = gtk_hbox_new(FALSE, 0);
  653     b = gtk_radio_button_new_with_label(group, _("Other"));
  654     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(b), !defset);
  655     gtk_box_pack_start(GTK_BOX(hb2), b, FALSE, FALSE, 0);
  656     adj = (GtkAdjustment *) gtk_adjustment_new(x, 0.60, 0.99, 0.01, 0, 0);
  657     tmp = gtk_spin_button_new(GTK_ADJUSTMENT(adj), 0.01, 2);
  658     g_signal_connect(G_OBJECT(tmp), "value-changed",
  659              G_CALLBACK(set_double_from_spinner), &x);
  660     gtk_widget_set_sensitive(tmp, !defset);
  661     g_signal_connect(G_OBJECT(b), "toggled",
  662              G_CALLBACK(toggle_alpha_spin),
  663              tmp);
  664     gtk_entry_set_activates_default(GTK_ENTRY(tmp), TRUE);
  665     gtk_box_pack_start(GTK_BOX(hb2), tmp, FALSE, FALSE, 5);
  666     gtk_box_pack_start(GTK_BOX(vbox), hb2, FALSE, FALSE, 0);
  667 
  668     gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 10);
  669 
  670     vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
  671     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
  672 
  673     hbox = gtk_dialog_get_action_area(GTK_DIALOG(dialog));
  674 
  675     /* Cancel button */
  676     cancel_delete_button(hbox, dialog);
  677 
  678     g_object_set_data(G_OBJECT(dialog), "vwin", vwin);
  679     g_object_set_data(G_OBJECT(dialog), "xptr", &x);
  680 
  681     /* "OK" button */
  682     tmp = ok_button(hbox);
  683     g_signal_connect(G_OBJECT(tmp), "clicked",
  684              G_CALLBACK(real_coeffint_set_alpha), dialog);
  685     gtk_widget_grab_default(tmp);
  686 
  687     gtk_widget_show_all(dialog);
  688 }
  689 
  690 static void stickiness_callback (GtkWidget *w, windata_t *vwin)
  691 {
  692     output_policy_dialog(vwin, vwin, 1);
  693 }
  694 
  695 static void do_corr_plot (windata_t *vwin)
  696 {
  697     VMatrix *corr = vwin->data;
  698     int err;
  699 
  700     err = plot_corrmat(corr, OPT_NONE);
  701     gui_graph_handler(err);
  702 }
  703 
  704 static void toolbar_plot_callback (GtkWidget *w, windata_t *vwin)
  705 {
  706     if (vwin->role == VIEW_SERIES) {
  707     series_view_graph(w, vwin);
  708     } else if (vwin->role == VIEW_BUNDLE) {
  709     exec_bundle_plot_function(vwin->data, NULL);
  710     } else if (vwin->role == CORR) {
  711     do_corr_plot(vwin);
  712     } else if (vwin->role == VIEW_DBNOMICS) {
  713     show_dbnomics_data(vwin, 1);
  714     } else {
  715     do_nonparam_plot(vwin);
  716     }
  717 }
  718 
  719 static void dbnomics_show_series (GtkWidget *w, windata_t *vwin)
  720 {
  721     show_dbnomics_data(vwin, 0);
  722 }
  723 
  724 static void editor_prefs_callback (GtkWidget *w, windata_t *vwin)
  725 {
  726     preferences_dialog(TAB_EDITOR, NULL, vwin_toplevel(vwin));
  727 }
  728 
  729 static void build_pkg_callback (GtkWidget *w, windata_t *vwin)
  730 {
  731     if (vwin_content_changed(vwin)) {
  732     int resp;
  733 
  734     resp = yes_no_cancel_dialog("gretl", _("Save changes?"),
  735                     vwin->main);
  736     if (resp == GRETL_CANCEL) {
  737         return;
  738     }
  739     if (resp == GRETL_YES) {
  740         vwin_save_callback(NULL, vwin);
  741     }
  742     }
  743 
  744     build_package_from_spec_file(vwin);
  745 }
  746 
  747 static int bundle_plot_ok (windata_t *vwin)
  748 {
  749     gretl_bundle *b = vwin->data;
  750     gchar *pf = get_bundle_plot_function(b);
  751     int ret = 0;
  752 
  753     if (pf != NULL) {
  754     ret = 1;
  755     g_free(pf);
  756     }
  757 
  758     return ret;
  759 }
  760 
  761 static int suppress_hmap (VMatrix *corr)
  762 {
  763     return corr->dim < 3;
  764 }
  765 
  766 static void activate_script_help (GtkWidget *widget, windata_t *vwin)
  767 {
  768     text_set_cursor(vwin->text, GDK_QUESTION_ARROW);
  769     set_window_help_active(vwin);
  770 }
  771 
  772 static int edit_script_popup_item (GretlToolItem *item)
  773 {
  774     if (item->icon == NULL) return 0;
  775 
  776     return !strcmp(item->icon, GTK_STOCK_COPY) ||
  777     !strcmp(item->icon, GTK_STOCK_PASTE) ||
  778     !strcmp(item->icon, GTK_STOCK_FIND) ||
  779     !strcmp(item->icon, GTK_STOCK_UNDO) ||
  780     !strcmp(item->icon, GTK_STOCK_FIND_AND_REPLACE);
  781 }
  782 
  783 static void set_plot_icon (GretlToolItem *item, int role)
  784 {
  785     if (role == LOESS || role == NADARWAT || role == VIEW_BUNDLE) {
  786     item->icon = GRETL_STOCK_SCATTER;
  787     } else if (role == VIEW_SERIES && dataset_is_cross_section(dataset)) {
  788     item->icon = GRETL_STOCK_BOX;
  789     } /* else stay with the default, a time series plot icon */
  790 }
  791 
  792 static void vwin_cut_callback (GtkWidget *w, windata_t *vwin)
  793 {
  794     gtk_text_buffer_cut_clipboard(gtk_text_view_get_buffer(GTK_TEXT_VIEW(vwin->text)),
  795                   gtk_clipboard_get(GDK_NONE),
  796                   TRUE);
  797 }
  798 
  799 static GretlToolItem viewbar_items[] = {
  800     { N_("New window"), GTK_STOCK_NEW, G_CALLBACK(toolbar_new_callback), NEW_ITEM },
  801     { N_("Open..."), GTK_STOCK_OPEN, G_CALLBACK(file_open_callback), OPEN_ITEM },
  802     { N_("Save"), GTK_STOCK_SAVE, G_CALLBACK(vwin_save_callback), SAVE_ITEM },
  803     { N_("Save as..."), GTK_STOCK_SAVE_AS, G_CALLBACK(save_as_callback), SAVE_AS_ITEM },
  804     { N_("Open in script editor"), GTK_STOCK_EDIT, G_CALLBACK(copy_to_editor), COPY_SCRIPT_ITEM },
  805     { N_("Save bundle content..."), GRETL_STOCK_BUNDLE, NULL, BUNDLE_ITEM },
  806     { N_("Print..."), GTK_STOCK_PRINT, G_CALLBACK(window_print_callback), PRINT_ITEM },
  807     { N_("Show/hide"), GRETL_STOCK_PIN, G_CALLBACK(session_notes_callback), NOTES_ITEM },
  808     { N_("Display values"), GTK_STOCK_MEDIA_PLAY, G_CALLBACK(dbnomics_show_series), DBN_ITEM },
  809     { N_("Run"), GTK_STOCK_EXECUTE, G_CALLBACK(do_run_script), EXEC_ITEM },
  810     { N_("Build package"), GRETL_STOCK_TOOLS, G_CALLBACK(build_pkg_callback), BUILD_ITEM },
  811     { N_("Cut"), GTK_STOCK_CUT, G_CALLBACK(vwin_cut_callback), EDIT_ITEM },
  812     { N_("Copy"), GTK_STOCK_COPY, G_CALLBACK(vwin_copy_callback), COPY_ITEM },
  813     { N_("Paste"), GTK_STOCK_PASTE, G_CALLBACK(text_paste), EDIT_ITEM },
  814     { N_("Find..."), GTK_STOCK_FIND, G_CALLBACK(text_find), FIND_ITEM },
  815     { N_("Replace..."), GTK_STOCK_FIND_AND_REPLACE, G_CALLBACK(text_replace), EDIT_ITEM },
  816     { N_("Undo"), GTK_STOCK_UNDO, G_CALLBACK(text_undo), EDIT_ITEM },
  817     { N_("Redo"), GTK_STOCK_REDO, G_CALLBACK(text_redo), EDIT_ITEM },
  818     { N_("Sort"), GTK_STOCK_SORT_ASCENDING, G_CALLBACK(series_view_toggle_sort), SORT_ITEM },
  819     { N_("Sort by..."), GTK_STOCK_SORT_ASCENDING, G_CALLBACK(multi_series_view_sort_by), SORT_BY_ITEM },
  820     { N_("Preferences..."), GTK_STOCK_PREFERENCES, G_CALLBACK(editor_prefs_callback), EDIT_HANSL_ITEM },
  821     { N_("Auto-indent script"), GTK_STOCK_INDENT, G_CALLBACK(indent_hansl), EDIT_HANSL_ITEM },
  822     { N_("Send To..."), GRETL_STOCK_MAIL, G_CALLBACK(mail_script_callback), MAIL_ITEM },
  823     { N_("Scripts index"), GTK_STOCK_INDEX, G_CALLBACK(script_index), INDEX_ITEM },
  824     { N_("Confidence level..."), GRETL_STOCK_ALPHA, G_CALLBACK(coeffint_set_alpha), ALPHA_ITEM },
  825     { N_("LaTeX"), GRETL_STOCK_TEX, G_CALLBACK(window_tex_callback), TEX_ITEM },
  826     { N_("Graph"), GRETL_STOCK_TS, G_CALLBACK(toolbar_plot_callback), PLOT_ITEM },
  827     { N_("Heatmap"), GRETL_STOCK_HMAP, G_CALLBACK(toolbar_plot_callback), HMAP_ITEM },
  828     { N_("Reformat..."), GTK_STOCK_CONVERT, G_CALLBACK(reformat_callback), FORMAT_ITEM },
  829     { N_("Edit values..."), GTK_STOCK_EDIT, G_CALLBACK(series_view_edit), EDITOR_ITEM },
  830     { N_("Refresh"), GTK_STOCK_REFRESH, G_CALLBACK(toolbar_refresh), REFRESH_ITEM },
  831     { N_("Add to dataset..."), GTK_STOCK_ADD, G_CALLBACK(add_data_callback), ADD_DATA_ITEM },
  832     { N_("Add as matrix..."), GTK_STOCK_ADD, G_CALLBACK(add_matrix_callback), ADD_MATRIX_ITEM },
  833     { N_("Stickiness..."), GRETL_STOCK_PIN, G_CALLBACK(stickiness_callback), STICKIFY_ITEM },
  834     { N_("Toggle split pane"), GRETL_STOCK_SPLIT_H, G_CALLBACK(split_pane_callback), SPLIT_H_ITEM },
  835     { N_("Toggle split pane"), GRETL_STOCK_SPLIT_V, G_CALLBACK(split_pane_callback), SPLIT_V_ITEM },
  836     { N_("Help on command"), GTK_STOCK_HELP, G_CALLBACK(activate_script_help), CMD_HELP_ITEM },
  837     { N_("Help"), GTK_STOCK_HELP, G_CALLBACK(window_help), HELP_ITEM },
  838     { N_("Help"), GTK_STOCK_HELP, G_CALLBACK(display_gnuplot_help), GP_HELP_ITEM },
  839     { N_("Help"), GTK_STOCK_HELP, G_CALLBACK(display_x12a_help), X12A_HELP_ITEM },
  840     { N_("Digits..."), NULL, G_CALLBACK(NULL), DIGITS_ITEM }
  841 };
  842 
  843 static int n_viewbar_items = G_N_ELEMENTS(viewbar_items);
  844 
  845 #define exec_ok(r) (vwin_editing_script(r) || \
  846             r == VIEW_SCRIPT || \
  847             r == VIEW_PKG_SAMPLE || \
  848                 r == EDIT_PKG_SAMPLE)
  849 
  850 #define open_ok(r) (vwin_editing_script(r))
  851 
  852 #define new_ok(r) (vwin_editing_script(r))
  853 
  854 #define edit_ok(r) (vwin_editing_script(r) || \
  855             vwin_editing_buffer(r) || \
  856                     r == EDIT_PKG_CODE || \
  857             r == EDIT_PKG_SAMPLE || \
  858             r == EDIT_PKG_HELP || \
  859             r == EDIT_PKG_GHLP)
  860 
  861 #define save_as_ok(r) (r != EDIT_HEADER && \
  862                    r != EDIT_NOTES && \
  863                    r != EDIT_PKG_CODE && \
  864                r != EDIT_PKG_SAMPLE && \
  865                r != CONSOLE && \
  866                r != VIEW_BUNDLE && \
  867                r != VIEW_DBNOMICS)
  868 
  869 #define help_ok(r) (r == LEVERAGE || \
  870             r == COINT2 || \
  871             r == HURST || \
  872             r == RMPLOT || \
  873             r == MAHAL)
  874 
  875 #define cmd_help_ok(r) (r == EDIT_HANSL || \
  876                     r == EDIT_PKG_CODE || \
  877                         r == EDIT_PKG_SAMPLE || \
  878             r == VIEW_PKG_SAMPLE || \
  879             r == VIEW_PKG_CODE || \
  880             r == VIEW_SCRIPT || \
  881             r == VIEW_LOG)
  882 
  883 /* for a non-editable script: can offer option to copy
  884    content into an editor window */
  885 #define copy_script_ok(r) (r == VIEW_PKG_SAMPLE || \
  886                r == VIEW_PKG_CODE || \
  887                r == VIEW_SCRIPT || \
  888                r == VIEW_LOG)
  889 
  890 #define sort_ok(r) (r == VIEW_SERIES)
  891 
  892 #define plot_ok(r) (r == VIEW_SERIES || \
  893             r == LOESS || \
  894             r == NADARWAT || \
  895             r == VIEW_DBNOMICS)
  896 
  897 #define add_data_ok(r) (r == PCA || r == LEVERAGE || \
  898                         r == MAHAL || r == FCAST || \
  899             r == LOESS || r == NADARWAT || \
  900             r == VIEW_DBNOMICS)
  901 
  902 #define split_h_ok(r) (r == SCRIPT_OUT || r == FNCALL_OUT || \
  903                r == VIEW_LOG || r == VIEW_PKG_CODE || \
  904                vwin_editing_script(r))
  905 
  906 #define split_v_ok(r) (r == SCRIPT_OUT || r == FNCALL_OUT)
  907 
  908 /* Screen out unwanted menu items depending on the context; also
  909    adjust the callbacks associated with some items based on
  910    context.
  911 */
  912 
  913 static GCallback tool_item_get_callback (GretlToolItem *item, windata_t *vwin,
  914                      int latex_ok, int sortby_ok,
  915                      int format_ok, int save_ok)
  916 {
  917     static int mail_ok = -1;
  918     GCallback func = item->func;
  919     int f = item->flag;
  920     int r = vwin->role;
  921 
  922     if (mail_ok < 0) {
  923     mail_ok = curl_does_smtp();
  924     }
  925 
  926     if (r == EDIT_SPEC) {
  927     /* This is a "special" that should maybe be regularized:
  928        a bit like editing a script, but different...
  929     */
  930     if (f == NEW_ITEM || f == OPEN_ITEM || f == EXEC_ITEM) {
  931         return NULL;
  932     }
  933     } else if (f == BUILD_ITEM) {
  934     return NULL;
  935     }
  936 
  937     /* popup use only */
  938     if (f == DIGITS_ITEM) {
  939     return NULL;
  940     }
  941 
  942     if (use_toolbar_search_box(r) && f == FIND_ITEM) {
  943     /* using an "inline" search box: skip the
  944        "Find" button */
  945     return NULL;
  946     }
  947 
  948     if (r == VIEW_DBNOMICS) {
  949     if (f == PRINT_ITEM || f == FIND_ITEM) {
  950         return NULL;
  951     }
  952     } else if (f == DBN_ITEM) {
  953     return NULL;
  954     }
  955 
  956     if (copy_script_ok(r)) {
  957     if (f == SAVE_AS_ITEM) {
  958         return NULL;
  959     }
  960     } else if (f == COPY_SCRIPT_ITEM) {
  961     return NULL;
  962     }
  963 
  964     if (r == EDIT_PKG_SAMPLE && f == OPEN_ITEM) {
  965     return G_CALLBACK(open_pkg_sample);
  966     } else if (!edit_ok(r) && f == EDIT_ITEM) {
  967     return NULL;
  968     } else if (!open_ok(r) && f == OPEN_ITEM) {
  969     return NULL;
  970     } else if (!new_ok(r) && f == NEW_ITEM) {
  971     return NULL;
  972     } else if (!exec_ok(r) && f == EXEC_ITEM) {
  973     return NULL;
  974     } else if (!cmd_help_ok(r) && f == CMD_HELP_ITEM) {
  975     return NULL;
  976     } else if ((!mail_ok || r != EDIT_HANSL) && f == MAIL_ITEM) {
  977     return NULL;
  978     } else if (!help_ok(r) && f == HELP_ITEM) {
  979     return NULL;
  980     } else if ((!latex_ok || !multiple_formats_ok(vwin)) && f == TEX_ITEM) {
  981     return NULL;
  982     } else if (!add_data_ok(r) && f == ADD_DATA_ITEM) {
  983     return NULL;
  984     } else if (r != XTAB && f == ADD_MATRIX_ITEM) {
  985     return NULL;
  986     } else if (!sort_ok(r) && f == SORT_ITEM) {
  987     return NULL;
  988     } else if (!sortby_ok && f == SORT_BY_ITEM) {
  989     return NULL;
  990     } else if (!plot_ok(r) && f == PLOT_ITEM) {
  991     if (r == VIEW_BUNDLE && bundle_plot_ok(vwin)) {
  992         ; /* alright then */
  993     } else {
  994         return NULL;
  995     }
  996     } else if (!split_h_ok(r) && f == SPLIT_H_ITEM) {
  997     return NULL;
  998     } else if (!split_v_ok(r) && f == SPLIT_V_ITEM) {
  999     return NULL;
 1000     } else if (!format_ok && f == FORMAT_ITEM) {
 1001     return NULL;
 1002     } else if (r != VIEW_SERIES && f == EDITOR_ITEM) {
 1003     return NULL;
 1004     } else if (r != EDIT_HANSL && r != EDIT_PKG_CODE &&
 1005            r != EDIT_PKG_SAMPLE && f == EDIT_HANSL_ITEM) {
 1006     return NULL;
 1007     } else if (r != VIEW_SCRIPT && f == INDEX_ITEM) {
 1008     return NULL;
 1009     } else if (r != SCRIPT_OUT && f == STICKIFY_ITEM) {
 1010     return NULL;
 1011     } else if (r != COEFFINT && f == ALPHA_ITEM) {
 1012     return NULL;
 1013     } else if (r != VIEW_SERIES && f == REFRESH_ITEM) {
 1014     return NULL;
 1015     } else if (r != EDIT_GP && f == GP_HELP_ITEM) {
 1016     return NULL;
 1017     } else if (r != EDIT_X12A && f == X12A_HELP_ITEM) {
 1018     return NULL;
 1019     } else if (f == SAVE_ITEM && !save_ok) {
 1020     return NULL;
 1021     } else if (r != EDIT_NOTES && f == NOTES_ITEM) {
 1022     return NULL;
 1023     } else if (r != VIEW_BUNDLE && r != VIEW_DBNOMICS && f == BUNDLE_ITEM) {
 1024     return NULL;
 1025     } else if (f == HMAP_ITEM) {
 1026     if (r != CORR) {
 1027         return NULL;
 1028     } else if (suppress_hmap(vwin->data)) {
 1029         return NULL;
 1030     }
 1031     } else if (f == SAVE_AS_ITEM) {
 1032     if (!save_as_ok(r) || (vwin->flags & VWIN_NO_SAVE)) {
 1033         return NULL;
 1034     } else if (multiple_formats_ok(vwin) ||
 1035            (vwin->flags & VWIN_MULTI_SERIES)) {
 1036         func = G_CALLBACK(multi_save_as_callback);
 1037     }
 1038     }
 1039 
 1040     return func;
 1041 }
 1042 
 1043 static void fcast_save_call (GtkAction *action, gpointer p)
 1044 {
 1045     const char *s = gtk_action_get_name(action);
 1046     ModelDataIndex idx;
 1047 
 1048     idx = (strcmp(s, "SaveSderr") == 0)? M_FCSE : M_FCAST;
 1049 
 1050     add_fcast_data((windata_t *) p, idx);
 1051 }
 1052 
 1053 static GtkWidget *make_fcast_save_menu (windata_t *vwin)
 1054 {
 1055     GtkWidget *menu = gtk_menu_new();
 1056     GtkAction *action;
 1057     GtkWidget *item;
 1058     int i;
 1059 
 1060     for (i=0; i<2; i++) {
 1061     action = gtk_action_new(i == 0 ? "SaveFcast" : "SaveSderr",
 1062                 i == 0 ? _("_Save forecast...") :
 1063                 _("Save standard errors..."),
 1064                 NULL, NULL);
 1065     g_signal_connect(G_OBJECT(action), "activate",
 1066              G_CALLBACK(fcast_save_call), vwin);
 1067     item = gtk_action_create_menu_item(action);
 1068     gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
 1069     }
 1070 
 1071     return menu;
 1072 }
 1073 
 1074 static GtkWidget *tool_item_get_menu (GretlToolItem *item, windata_t *vwin)
 1075 {
 1076     GtkWidget *menu = NULL;
 1077 
 1078     if (vwin->role == VIEW_BUNDLE) {
 1079     if (item->flag == BUNDLE_ITEM) {
 1080         menu = make_bundle_content_menu(vwin);
 1081     } else if (item->flag == PLOT_ITEM) {
 1082         menu = make_bundle_plot_menu(vwin);
 1083     } else if (item->flag == SAVE_ITEM) {
 1084         menu = make_bundle_save_menu(vwin);
 1085         if (menu != NULL) {
 1086         item->tip = N_("Save...");
 1087         }
 1088     }
 1089     } else if (vwin->role == VIEW_DBNOMICS) {
 1090     if (item->flag == BUNDLE_ITEM) {
 1091         menu = make_bundle_content_menu(vwin);
 1092     } else if (item->flag == SAVE_ITEM) {
 1093         menu = make_bundle_save_menu(vwin);
 1094         if (menu != NULL) {
 1095         item->tip = N_("Save...");
 1096         }
 1097     }
 1098     } else if (vwin->role == FCAST && item->flag == ADD_DATA_ITEM) {
 1099     FITRESID *fr = vwin->data;
 1100 
 1101     if (fr->sderr != NULL) {
 1102         menu = make_fcast_save_menu(vwin);
 1103     }
 1104     }
 1105 
 1106     if (menu != NULL) {
 1107     /* don't leak: record pointer to menu so it can
 1108        be destroyed when the window is closed */
 1109     vwin_record_toolbar_popup(vwin, menu);
 1110     }
 1111 
 1112     return menu;
 1113 }
 1114 
 1115 static void gretl_toolbar_flat (GtkWidget *w)
 1116 {
 1117     static int style_done;
 1118 
 1119     gtk_widget_set_name(w, "gretl_toolbar");
 1120 
 1121     if (!style_done) {
 1122     gtk_rc_parse_string("style \"gretl-tb-style\"\n{\n"
 1123                 "  GtkToolbar::shadow-type = GTK_SHADOW_NONE\n"
 1124                 "}\n"
 1125                 "widget \"*.gretl_toolbar\" style \"gretl-tb-style\"");
 1126     style_done = 1;
 1127     }
 1128 }
 1129 
 1130 GtkWidget *gretl_toolbar_new (GtkWidget *sibling)
 1131 {
 1132     GtkWidget *tb = gtk_toolbar_new();
 1133 
 1134     gtk_toolbar_set_icon_size(GTK_TOOLBAR(tb), GTK_ICON_SIZE_MENU);
 1135     gtk_toolbar_set_style(GTK_TOOLBAR(tb), GTK_TOOLBAR_ICONS);
 1136     gtk_toolbar_set_show_arrow(GTK_TOOLBAR(tb), FALSE);
 1137 
 1138     if (sibling == NULL) {
 1139     /* if we're not alongside a menu bar ("sibling"),
 1140        show the toolbar without a shadow
 1141     */
 1142     gretl_toolbar_flat(tb);
 1143     }
 1144 
 1145     return tb;
 1146 }
 1147 
 1148 void gretl_tooltips_add (GtkWidget *w, const gchar *str)
 1149 {
 1150     gtk_widget_set_tooltip_text(w, str);
 1151 }
 1152 
 1153 static GtkToolItem *gretl_menu_button (const char *icon,
 1154                        const char *tip,
 1155                        GtkWidget **pw)
 1156 {
 1157     GtkWidget *img, *button = gtk_button_new();
 1158     GtkToolItem *item = gtk_tool_item_new();
 1159 
 1160     gtk_widget_set_tooltip_text(GTK_WIDGET(item), _(tip));
 1161     gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
 1162     img = gtk_image_new_from_stock(icon, GTK_ICON_SIZE_MENU);
 1163     gtk_container_add(GTK_CONTAINER(button), img);
 1164     gtk_container_add(GTK_CONTAINER(item), button);
 1165     *pw = button;
 1166 
 1167     return item;
 1168 }
 1169 
 1170 static void gretl_tool_item_set_tip (GtkWidget *item,
 1171                      GretlToolItem *tool)
 1172 {
 1173     const char *accel = NULL;
 1174 
 1175     if (tool->flag == EXEC_ITEM) {
 1176     accel = "Ctrl-R";
 1177     } else if (tool->flag == COPY_ITEM) {
 1178     accel = "Ctrl-C";
 1179     } else if (tool->flag == SAVE_ITEM) {
 1180     accel = "Ctrl-S";
 1181     } else if (tool->flag == FIND_ITEM) {
 1182     accel = "Ctrl-F";
 1183     }
 1184 
 1185     if (accel != NULL) {
 1186     gchar *s = g_strdup_printf("%s (%s)", _(tool->tip), accel);
 1187 
 1188     gtk_widget_set_tooltip_text(item, s);
 1189     g_free(s);
 1190     } else {
 1191     gtk_widget_set_tooltip_text(item, _(tool->tip));
 1192     }
 1193 }
 1194 
 1195 GtkWidget *gretl_toolbar_insert (GtkWidget *tbar,
 1196                  GretlToolItem *tool,
 1197                  GCallback func,
 1198                  gpointer data,
 1199                  gint pos)
 1200 {
 1201     GtkToolItem *item;
 1202 
 1203     item = gtk_tool_button_new_from_stock(tool->icon);
 1204     gretl_tool_item_set_tip(GTK_WIDGET(item), tool);
 1205     g_signal_connect(G_OBJECT(item), "clicked", func, data);
 1206     gtk_widget_set_size_request(GTK_WIDGET(item), 30, -1);
 1207     gtk_toolbar_insert(GTK_TOOLBAR(tbar), item, pos);
 1208 
 1209     return GTK_WIDGET(item);
 1210 }
 1211 
 1212 static void button_menu_pos (GtkMenu *menu,
 1213                  gint *x,
 1214                  gint *y,
 1215                  gboolean *push_in,
 1216                  gpointer data)
 1217 {
 1218     GtkWidget *button = data;
 1219     gint wx, wy, tx, ty;
 1220 
 1221     gdk_window_get_origin(gtk_widget_get_window(button), &wx, &wy);
 1222     gtk_widget_translate_coordinates(button, gtk_widget_get_toplevel(button),
 1223                      0, 0, &tx, &ty);
 1224     *x = wx + tx;
 1225     *y = wy + ty + 26;
 1226     *push_in = TRUE;
 1227 }
 1228 
 1229 static void tool_item_popup (GtkWidget *button, GdkEvent *event,
 1230                  GtkWidget *menu)
 1231 {
 1232     gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
 1233            button_menu_pos, button,
 1234            event->button.button, event->button.time);
 1235 }
 1236 
 1237 GtkWidget *vwin_toolbar_insert (GretlToolItem *tool,
 1238                 GCallback func,
 1239                 GtkWidget *menu,
 1240                 windata_t *vwin,
 1241                 gint pos)
 1242 {
 1243     GtkToolItem *item;
 1244 
 1245     if (menu != NULL) {
 1246     /* make and insert a button that pops down a menu */
 1247     GtkWidget *button;
 1248 
 1249     item = gretl_menu_button(tool->icon, tool->tip, &button);
 1250     g_signal_connect(G_OBJECT(button), "button-press-event",
 1251              G_CALLBACK(tool_item_popup), menu);
 1252     } else {
 1253     /* make and insert a regular callback button */
 1254     item = gtk_tool_button_new_from_stock(tool->icon);
 1255     g_signal_connect(G_OBJECT(item), "clicked", func, vwin);
 1256     if (tool->flag == NEW_ITEM && window_is_tab(vwin)) {
 1257         gtk_widget_set_tooltip_text(GTK_WIDGET(item), _("New tab"));
 1258     } else {
 1259         gretl_tool_item_set_tip(GTK_WIDGET(item), tool);
 1260     }
 1261     }
 1262 
 1263     gtk_toolbar_insert(GTK_TOOLBAR(vwin->mbar), item, pos);
 1264 
 1265     return GTK_WIDGET(item);
 1266 }
 1267 
 1268 static void viewbar_add_items (windata_t *vwin, ViewbarFlags flags)
 1269 {
 1270     int sortby_ok = has_sortable_data(vwin);
 1271     int format_ok = can_format_data(vwin);
 1272     int latex_ok = latex_is_ok();
 1273     int save_ok = (flags & VIEWBAR_EDITABLE);
 1274     GtkWidget *hpane = NULL, *vpane = NULL;
 1275     GtkWidget *button;
 1276     GtkWidget *menu;
 1277     GretlToolItem *item;
 1278     GCallback func;
 1279     int i;
 1280 
 1281     for (i=0; i<n_viewbar_items; i++) {
 1282     func = NULL;
 1283     menu = NULL;
 1284     item = &viewbar_items[i];
 1285 
 1286     /* Is there anything to hook up, in context? We
 1287        try first for a menu to attach to the toolbar
 1288        button; failing that we test for a "direct"
 1289        callback function.
 1290     */
 1291     menu = tool_item_get_menu(item, vwin);
 1292     if (menu == NULL && item->func != NULL) {
 1293         func = tool_item_get_callback(item, vwin, latex_ok, sortby_ok,
 1294                       format_ok, save_ok);
 1295     }
 1296     if (func == NULL && menu == NULL) {
 1297         /* nothing to hook up */
 1298         continue;
 1299     }
 1300 
 1301     if (item->flag == PLOT_ITEM) {
 1302         set_plot_icon(item, vwin->role);
 1303     }
 1304 
 1305     button = vwin_toolbar_insert(item, func, menu, vwin, -1);
 1306 
 1307     if (func == (GCallback) split_pane_callback) {
 1308         if (hpane == NULL) {
 1309         hpane = button;
 1310         } else {
 1311         vpane = button;
 1312         }
 1313     }
 1314 
 1315     if (item->flag == SAVE_ITEM) {
 1316         if (vwin->role != CONSOLE &&
 1317         vwin->role != VIEW_BUNDLE &&
 1318         vwin->role != VIEW_DBNOMICS) {
 1319         /* nothing to save just yet */
 1320         g_object_set_data(G_OBJECT(vwin->mbar), "save_button", button);
 1321         gtk_widget_set_sensitive(button, FALSE);
 1322         }
 1323     } else if (item->flag == SAVE_AS_ITEM) {
 1324         g_object_set_data(G_OBJECT(vwin->mbar), "save_as_button", button);
 1325         if (strstr(vwin->fname, "script_tmp")) {
 1326         gtk_widget_set_sensitive(button, FALSE);
 1327         }
 1328     }
 1329     }
 1330 
 1331     if (hpane != NULL) {
 1332     g_object_set_data(G_OBJECT(hpane), "vpane", vpane);
 1333     }
 1334 
 1335     if (vpane != NULL) {
 1336     g_object_set_data(G_OBJECT(vpane), "hpane", hpane);
 1337     }
 1338 }
 1339 
 1340 void vwin_add_viewbar (windata_t *vwin, ViewbarFlags flags)
 1341 {
 1342     if ((flags & VIEWBAR_HAS_TEXT) || vwin->role == SCRIPT_OUT) {
 1343     g_object_set_data(G_OBJECT(vwin->main), "text_out",
 1344               GINT_TO_POINTER(1));
 1345     }
 1346 
 1347     vwin->mbar = gretl_toolbar_new(NULL);
 1348     viewbar_add_items(vwin, flags);
 1349     vwin_pack_toolbar(vwin);
 1350 }
 1351 
 1352 GtkWidget *build_text_popup (windata_t *vwin)
 1353 {
 1354     GtkWidget *pmenu = gtk_menu_new();
 1355     GretlToolItem *item;
 1356     GCallback func;
 1357     GtkWidget *w;
 1358     int i;
 1359 
 1360     for (i=0; i<n_viewbar_items; i++) {
 1361     item = &viewbar_items[i];
 1362     func = G_CALLBACK(NULL);
 1363     if (item->flag == SPLIT_H_ITEM || item->flag == SPLIT_V_ITEM) {
 1364         continue;
 1365     } else if (item->flag == DIGITS_ITEM &&
 1366            (vwin->role == VIEW_MODEL || vwin->role == SUMMARY)) {
 1367         func = G_CALLBACK(display_digits_callback);
 1368     } else if (vwin->role == EDIT_HANSL) {
 1369         /* the script editor popup may have some special stuff
 1370            added: don't clutter it up */
 1371         if (edit_script_popup_item(item)) {
 1372         func = item->func;
 1373         } else {
 1374         func = NULL;
 1375         }
 1376     } else {
 1377         func = tool_item_get_callback(item, vwin, 0, 0, 0, 0);
 1378     }
 1379     if (func != G_CALLBACK(NULL)) {
 1380         if (func == G_CALLBACK(text_paste)) {
 1381         GtkClipboard *cb = gtk_clipboard_get(GDK_NONE);
 1382 
 1383         if (!gtk_clipboard_wait_is_text_available(cb)) {
 1384             continue;
 1385         }
 1386         } else if (func == G_CALLBACK(text_undo) && !text_can_undo(vwin)) {
 1387         continue;
 1388         }
 1389         w = gtk_menu_item_new_with_label(_(item->tip));
 1390         g_signal_connect(G_OBJECT(w), "activate", func, vwin);
 1391         gtk_widget_show(w);
 1392         gtk_menu_shell_append(GTK_MENU_SHELL(pmenu), w);
 1393     }
 1394     }
 1395 
 1396     if (vwin->role != EDIT_HANSL) {
 1397     if (window_is_undockable(vwin)) {
 1398         add_undock_popup_item(pmenu, vwin);
 1399     } else if (window_is_dockable(vwin)) {
 1400         add_dock_popup_item(pmenu, vwin);
 1401     }
 1402     }
 1403 
 1404     return pmenu;
 1405 }
 1406 
 1407 /* callbacks for main-window toolbar icons */
 1408 
 1409 static void tbar_calc (void)
 1410 {
 1411 #ifdef G_OS_WIN32
 1412     win32_run_async(calculator, NULL);
 1413 #else
 1414     gretl_fork("calculator", NULL, NULL);
 1415 #endif
 1416 }
 1417 
 1418 static void tbar_open_data (void)
 1419 {
 1420     display_files(TEXTBOOK_DATA, NULL);
 1421 }
 1422 
 1423 static void tbar_command_ref (void)
 1424 {
 1425     display_text_help(NULL);
 1426 }
 1427 
 1428 static void tbar_xy_graph (void)
 1429 {
 1430     if (data_status) {
 1431     if (dataset->v == 2) {
 1432         do_graph_var(mdata->active_var);
 1433     } else if (mdata_selection_count() == 2) {
 1434         plot_from_selection(GR_XY);
 1435     } else {
 1436         selection_dialog(GR_XY, _("gretl: define graph"),
 1437                  NULL, do_graph_from_selector);
 1438     }
 1439     } else {
 1440     warnbox(_("Please open a data file first"));
 1441     }
 1442 }
 1443 
 1444 static void tbar_model (void)
 1445 {
 1446     if (data_status) {
 1447     selection_dialog(OLS, _("gretl: specify model"), NULL, do_model);
 1448     } else {
 1449     warnbox(_("Please open a data file first"));
 1450     }
 1451 }
 1452 
 1453 static void tbar_new_script (void)
 1454 {
 1455     do_new_script(EDIT_HANSL, NULL);
 1456 }
 1457 
 1458 static void tbar_show_funcs (GtkWidget *w, gpointer p)
 1459 {
 1460     display_files(FUNC_FILES, NULL);
 1461 }
 1462 
 1463 /* end toolbar icon callbacks */
 1464 
 1465 static GretlToolItem mainbar_items[] = {
 1466     { N_("launch calculator"),  GRETL_STOCK_CALC,    G_CALLBACK(tbar_calc), 0 },
 1467     { N_("new script"),         GTK_STOCK_EDIT,      G_CALLBACK(tbar_new_script), 0 },
 1468     { N_("open gretl console"), GRETL_STOCK_CONSOLE, G_CALLBACK(gretl_console), 0 },
 1469     { N_("session icon view"),  GRETL_STOCK_ICONS,   G_CALLBACK(view_session), 0 },
 1470     { N_("function packages"),  GRETL_STOCK_FUNC,    G_CALLBACK(tbar_show_funcs), 0 },
 1471     { N_("command reference"),  GTK_STOCK_HELP,      G_CALLBACK(tbar_command_ref), 0 },
 1472     { N_("X-Y graph"),          GRETL_STOCK_SCATTER, G_CALLBACK(tbar_xy_graph), 0 },
 1473     { N_("OLS model"),          GRETL_STOCK_MODEL,   G_CALLBACK(tbar_model), 0 },
 1474     { N_("databases"),          GRETL_STOCK_DB,      G_CALLBACK(show_native_dbs), 0 },
 1475     { N_("open dataset"),       GTK_STOCK_OPEN,      G_CALLBACK(tbar_open_data), 0 },
 1476 };
 1477 
 1478 void add_mainwin_toolbar (GtkWidget *vbox)
 1479 {
 1480     GretlToolItem *item;
 1481     GtkWidget *hbox;
 1482     int i, n = G_N_ELEMENTS(mainbar_items);
 1483 
 1484     mdata->mbar = gretl_toolbar_new(NULL);
 1485 
 1486     for (i=0; i<n; i++) {
 1487     item = &mainbar_items[i];
 1488     gretl_toolbar_insert(mdata->mbar, item, item->func, mdata, -1);
 1489     }
 1490 
 1491     hbox = gtk_hbox_new(FALSE, 0);
 1492     gtk_box_pack_start(GTK_BOX(hbox), mdata->mbar, FALSE, FALSE, 0);
 1493     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
 1494 }
 1495 
 1496 /* Add a temporary menubar for use in a script output
 1497    window, while we're waiting for the output. If the
 1498    output window is being reused this is a bit more
 1499    complicated; we have to "hide" the regular menubar
 1500    before inserting the temporary one.
 1501  */
 1502 
 1503 void vwin_add_tmpbar (windata_t *vwin)
 1504 {
 1505     GretlToolItem item = {
 1506     N_("Stop"),
 1507     GTK_STOCK_STOP,
 1508     G_CALLBACK(do_stop_script),
 1509     0
 1510     };
 1511     GtkWidget *hbox, *tmp;
 1512 
 1513     hbox = g_object_get_data(G_OBJECT(vwin->main), "top-hbox");
 1514 
 1515     if (hbox != NULL) {
 1516     /* We're replacing a "real" menubar temporarily: ref. the
 1517        widgets in @hbox before removing them so we can put
 1518        them back later.
 1519     */
 1520     GtkWidget *winlist = g_object_get_data(G_OBJECT(hbox), "winlist");
 1521 
 1522     g_object_ref(G_OBJECT(vwin->mbar));
 1523     gtk_container_remove(GTK_CONTAINER(hbox), vwin->mbar);
 1524     if (vwin->finder != NULL) {
 1525         g_object_ref(G_OBJECT(vwin->finder));
 1526         gtk_container_remove(GTK_CONTAINER(hbox), vwin->finder);
 1527     }
 1528     if (winlist != NULL) {
 1529         g_object_ref(G_OBJECT(winlist));
 1530         gtk_container_remove(GTK_CONTAINER(hbox), winlist);
 1531     }
 1532     } else {
 1533     /* starting from scratch */
 1534     hbox = gtk_hbox_new(FALSE, 0);
 1535     g_object_set_data(G_OBJECT(vwin->main), "top-hbox", hbox);
 1536     gtk_box_pack_start(GTK_BOX(vwin->vbox), hbox, FALSE, FALSE, 0);
 1537     }
 1538 
 1539     tmp = gretl_toolbar_new(NULL);
 1540     gretl_toolbar_insert(tmp, &item, item.func, NULL, 0);
 1541     gtk_box_pack_start(GTK_BOX(hbox), tmp, FALSE, FALSE, 5);
 1542 
 1543     start_wait_for_output(vwin, hbox);
 1544     gtk_widget_show_all(hbox);
 1545 }