1 /** \file   uicmdline.c
2  * \brief   Dialog to display command line options
3  *
4  * \author  Bas Wassink <b.wassink@ziggo.nl>
5  */
6 
7 /*
8  * This file is part of VICE, the Versatile Commodore Emulator.
9  * See README for copyright notice.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24  *  02111-1307  USA.
25  *
26  */
27 
28 #include "vice.h"
29 
30 #include <gtk/gtk.h>
31 
32 #include "widgethelpers.h"
33 #include "debug_gtk3.h"
34 #include "machine.h"
35 #include "cmdline.h"
36 #include "ui.h"
37 
38 #include "uicmdline.h"
39 
40 
41 /** \brief  Handler for the 'response' event of the dialog
42  *
43  * \param[in,out]   dialog      dialog triggering the event
44  * \param[in]       response_id response ID
45  * \param[in]       user_data   extra event data (unused)
46  */
on_response(GtkDialog * dialog,gint response_id,gpointer user_data)47 static void on_response(GtkDialog *dialog,
48                         gint response_id,
49                         gpointer user_data)
50 {
51     if (response_id == GTK_RESPONSE_CLOSE) {
52         gtk_widget_destroy(GTK_WIDGET(dialog));
53     }
54 }
55 
56 
57 /** \brief  Create textview with scrollbars
58  *
59  * \return  GtkScrolledWindow
60  */
create_content_widget(void)61 static GtkWidget *create_content_widget(void)
62 {
63     GtkWidget *view;
64     GtkWidget *scrolled;
65     GtkTextBuffer *buffer;
66     GtkTextIter iter;
67     GtkTextTag *name_tag;
68     GtkTextTag *inv_tag;
69     GtkTextTag *desc_tag;
70     int num_options = cmdline_get_num_options();
71     int i;
72 
73     view = gtk_text_view_new();
74     gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
75     gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
76     gtk_text_view_set_monospace(GTK_TEXT_VIEW(view), TRUE);
77     gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view), GTK_WRAP_WORD_CHAR);
78     gtk_text_view_set_left_margin(GTK_TEXT_VIEW(view),16);
79     gtk_text_view_set_right_margin(GTK_TEXT_VIEW(view),16);
80 
81 
82     buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
83 
84     /* name: bold (for -option) */
85     name_tag = gtk_text_buffer_create_tag(buffer, "name_tag",
86             "weight", PANGO_WEIGHT_BOLD, NULL);
87     /* name: bold and red (for +option) */
88     inv_tag = gtk_text_buffer_create_tag(buffer, "inv_tag",
89             "weight", PANGO_WEIGHT_BOLD,
90             "foreground", "red", NULL);
91     /* description: indented and sans-serif */
92     desc_tag = gtk_text_buffer_create_tag(buffer, "desc_tag",
93             "left-margin", 48,
94             "family", "sans-serif", NULL);
95 
96     scrolled = gtk_scrolled_window_new(NULL, NULL);
97     gtk_widget_set_size_request(scrolled, 800, 600);
98     gtk_container_add(GTK_CONTAINER(scrolled), view);
99 
100     gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
101 
102     for (i = 0; i < num_options; i++) {
103         char *name;
104         char *param;
105         char *desc;
106 
107         name = cmdline_options_get_name(i);
108         param = cmdline_options_get_param(i);
109         desc = cmdline_options_get_description(i);
110 
111         if (name[0] == '-') {
112             gtk_text_buffer_insert_with_tags(buffer, &iter, name, -1,
113                     name_tag, NULL);
114         } else {
115             gtk_text_buffer_insert_with_tags(buffer, &iter, name, -1,
116                     inv_tag, NULL);
117         }
118 
119         if (param != NULL) {
120             gtk_text_buffer_insert(buffer, &iter, " ", -1);
121             gtk_text_buffer_insert(buffer, &iter, param, -1);
122         }
123         gtk_text_buffer_insert(buffer, &iter, "\n", -1);
124 
125         if (desc == NULL) {
126             fprintf(stderr, "no desc for '%s'\n", name);
127             desc = "[DESCRIPTION MISSING]";
128         }
129         gtk_text_buffer_insert_with_tags(buffer, &iter, desc, -1,
130                 desc_tag, NULL);
131         gtk_text_buffer_insert(buffer, &iter, "\n\n", -1);
132     }
133 
134     gtk_widget_show_all(scrolled);
135     return scrolled;
136 }
137 
138 
139 /** \brief  Show list of command line options
140  *
141  * \param[in]   widget      parent widget (unused)
142  * \param[in]   user_data   extra data (unused)
143  */
uicmdline_dialog_show(GtkWidget * widget,gpointer user_data)144 void uicmdline_dialog_show(GtkWidget *widget, gpointer user_data)
145 {
146     GtkWidget *dialog;
147     GtkWidget *content;
148     gchar title[256];
149 
150     g_snprintf(title, 256, "%s command line options", machine_name);
151 
152     dialog = gtk_dialog_new_with_buttons(title,
153             ui_get_active_window(),
154             GTK_DIALOG_MODAL,
155             "Close", GTK_RESPONSE_CLOSE,
156             NULL);
157 
158     content = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
159     gtk_box_pack_start(GTK_BOX(content), create_content_widget(),
160             TRUE, TRUE, 0);
161 
162     g_signal_connect(dialog, "response", G_CALLBACK(on_response), NULL);
163     gtk_widget_show_all(dialog);
164 }
165