~midori/midori/cmake-make-dist

« back to all changes in this revision

Viewing changes to midori/midori-console.c

  • Committer: Christian Dywan
  • Date: 2008-06-01 21:47:27 UTC
  • Revision ID: git-v1:b511f12b9b4b063610161f2229b94a24a86be0fc
Rename folder 'src' to 'midori'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
 
3
 
 
4
 This library is free software; you can redistribute it and/or
 
5
 modify it under the terms of the GNU Lesser General Public
 
6
 License as published by the Free Software Foundation; either
 
7
 version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
 See the file COPYING for the full license text.
 
10
*/
 
11
 
 
12
#include "midori-console.h"
 
13
 
 
14
#include "sokoke.h"
 
15
#include <glib/gi18n.h>
 
16
 
 
17
G_DEFINE_TYPE (MidoriConsole, midori_console, GTK_TYPE_VBOX)
 
18
 
 
19
struct _MidoriConsolePrivate
 
20
{
 
21
    GtkWidget* toolbar;
 
22
    GtkWidget* treeview;
 
23
};
 
24
 
 
25
#define MIDORI_CONSOLE_GET_PRIVATE(obj) \
 
26
    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
 
27
     MIDORI_TYPE_CONSOLE, MidoriConsolePrivate))
 
28
 
 
29
static void
 
30
midori_console_class_init (MidoriConsoleClass* class)
 
31
{
 
32
    g_type_class_add_private (class, sizeof (MidoriConsolePrivate));
 
33
}
 
34
 
 
35
static void
 
36
midori_console_button_clear_clicked_cb (GtkToolItem*   toolitem,
 
37
                                        MidoriConsole* console)
 
38
{
 
39
    MidoriConsolePrivate* priv = console->priv;
 
40
 
 
41
    GtkTreeModel* model = gtk_tree_view_get_model (
 
42
        GTK_TREE_VIEW (priv->treeview));
 
43
    gtk_tree_store_clear (GTK_TREE_STORE (model));
 
44
}
 
45
 
 
46
static void
 
47
midori_console_treeview_render_icon_cb (GtkTreeViewColumn* column,
 
48
                                        GtkCellRenderer*   renderer,
 
49
                                        GtkTreeModel*      model,
 
50
                                        GtkTreeIter*       iter,
 
51
                                        GtkWidget*         treeview)
 
52
{
 
53
    // gchar* source_id;
 
54
    // gtk_tree_model_get (model, iter, 2, &source_id, -1);
 
55
 
 
56
    g_object_set (renderer, "stock-id", GTK_STOCK_DIALOG_WARNING, NULL);
 
57
 
 
58
    // g_free (source_id);
 
59
}
 
60
 
 
61
static void
 
62
midori_console_treeview_render_text_cb (GtkTreeViewColumn* column,
 
63
                                        GtkCellRenderer*   renderer,
 
64
                                        GtkTreeModel*      model,
 
65
                                        GtkTreeIter*       iter,
 
66
                                        GtkWidget*         treeview)
 
67
{
 
68
    gchar* message;
 
69
    gint   line;
 
70
    gchar* source_id;
 
71
    gtk_tree_model_get (model, iter, 0, &message, 1, &line, 2, &source_id, -1);
 
72
 
 
73
    gchar* text = g_strdup_printf ("%d @ %s\n%s", line, source_id, message);
 
74
    g_object_set (renderer, "text", text, NULL);
 
75
    g_free (text);
 
76
 
 
77
    g_free (message);
 
78
    g_free (source_id);
 
79
}
 
80
 
 
81
static void
 
82
midori_console_treeview_row_activated_cb (GtkTreeView*       treeview,
 
83
                                          GtkTreePath*       path,
 
84
                                          GtkTreeViewColumn* column,
 
85
                                          MidoriConsole*     console)
 
86
{
 
87
    /*GtkTreeModel* model = gtk_tree_view_get_model (treeview);
 
88
    GtkTreeIter iter;
 
89
    if (gtk_tree_model_get_iter (model, &iter, path))
 
90
    {
 
91
        gchar* source_id;
 
92
        gtk_tree_model_get (model, &iter, 2, &source_id, -1);
 
93
        g_free (source_id);
 
94
    }*/
 
95
}
 
96
 
 
97
static void
 
98
midori_console_init (MidoriConsole* console)
 
99
{
 
100
    console->priv = MIDORI_CONSOLE_GET_PRIVATE (console);
 
101
 
 
102
    MidoriConsolePrivate* priv = console->priv;
 
103
 
 
104
    // Create the treeview
 
105
    GtkTreeViewColumn* column;
 
106
    GtkCellRenderer* renderer_text;
 
107
    GtkCellRenderer* renderer_pixbuf;
 
108
    GtkTreeStore* treestore = gtk_tree_store_new (3, G_TYPE_STRING,
 
109
                                                     G_TYPE_INT,
 
110
                                                     G_TYPE_STRING);
 
111
    priv->treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (treestore));
 
112
    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->treeview), FALSE);
 
113
    column = gtk_tree_view_column_new ();
 
114
    renderer_pixbuf = gtk_cell_renderer_pixbuf_new ();
 
115
    gtk_tree_view_column_pack_start (column, renderer_pixbuf, FALSE);
 
116
    gtk_tree_view_column_set_cell_data_func (column, renderer_pixbuf,
 
117
        (GtkTreeCellDataFunc)midori_console_treeview_render_icon_cb,
 
118
        priv->treeview, NULL);
 
119
    renderer_text = gtk_cell_renderer_text_new ();
 
120
    gtk_tree_view_column_pack_start (column, renderer_text, FALSE);
 
121
    gtk_tree_view_column_set_cell_data_func (column, renderer_text,
 
122
        (GtkTreeCellDataFunc)midori_console_treeview_render_text_cb,
 
123
        priv->treeview, NULL);
 
124
    gtk_tree_view_append_column (GTK_TREE_VIEW (priv->treeview), column);
 
125
    g_object_unref (treestore);
 
126
    g_signal_connect (priv->treeview, "row-activated",
 
127
                      G_CALLBACK (midori_console_treeview_row_activated_cb),
 
128
                      console);
 
129
    gtk_widget_show (priv->treeview);
 
130
    gtk_box_pack_start (GTK_BOX (console), priv->treeview, TRUE, TRUE, 0);
 
131
}
 
132
 
 
133
/**
 
134
 * midori_console_new:
 
135
 *
 
136
 * Creates a new empty console.
 
137
 *
 
138
 * Return value: a new #MidoriConsole
 
139
 **/
 
140
GtkWidget*
 
141
midori_console_new (void)
 
142
{
 
143
    MidoriConsole* console = g_object_new (MIDORI_TYPE_CONSOLE,
 
144
                                           NULL);
 
145
 
 
146
    return GTK_WIDGET (console);
 
147
}
 
148
 
 
149
/**
 
150
 * midori_console_get_toolbar:
 
151
 *
 
152
 * Retrieves the toolbar of the console. A new widget is created on
 
153
 * the first call of this function.
 
154
 *
 
155
 * Return value: a new #MidoriConsole
 
156
 **/
 
157
GtkWidget*
 
158
midori_console_get_toolbar (MidoriConsole* console)
 
159
{
 
160
    g_return_val_if_fail (MIDORI_IS_CONSOLE (console), NULL);
 
161
 
 
162
    MidoriConsolePrivate* priv = console->priv;
 
163
 
 
164
    if (!priv->toolbar)
 
165
    {
 
166
        GtkWidget* toolbar = gtk_toolbar_new ();
 
167
        gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH_HORIZ);
 
168
        gtk_toolbar_set_icon_size (GTK_TOOLBAR (toolbar), GTK_ICON_SIZE_BUTTON);
 
169
        GtkToolItem* toolitem = gtk_tool_item_new ();
 
170
        // TODO: What about a find entry here that filters e.g. by url?
 
171
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
 
172
        gtk_widget_show (GTK_WIDGET (toolitem));
 
173
        toolitem = gtk_separator_tool_item_new ();
 
174
        gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (toolitem),
 
175
                                          FALSE);
 
176
        gtk_tool_item_set_expand (toolitem, TRUE);
 
177
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
 
178
        gtk_widget_show (GTK_WIDGET (toolitem));
 
179
        toolitem = gtk_tool_button_new_from_stock (GTK_STOCK_CLEAR);
 
180
        gtk_tool_item_set_is_important (toolitem, TRUE);
 
181
        g_signal_connect (toolitem, "clicked",
 
182
            G_CALLBACK (midori_console_button_clear_clicked_cb), console);
 
183
        gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
 
184
        gtk_widget_show (GTK_WIDGET (toolitem));
 
185
        priv->toolbar = toolbar;
 
186
    }
 
187
 
 
188
    return priv->toolbar;
 
189
}
 
190
 
 
191
/**
 
192
 * midori_console_add:
 
193
 * @console: a #MidoriConsole
 
194
 * @message: a descriptive message
 
195
 * @line: the line in the source file
 
196
 * @source_id: the source
 
197
 *
 
198
 * Adds a new message to the console.
 
199
 **/
 
200
void
 
201
midori_console_add (MidoriConsole* console,
 
202
                    const gchar*   message,
 
203
                    gint           line,
 
204
                    const gchar*   source_id)
 
205
{
 
206
    g_return_if_fail (MIDORI_IS_CONSOLE (console));
 
207
 
 
208
    MidoriConsolePrivate* priv = console->priv;
 
209
 
 
210
    GtkTreeView* treeview = GTK_TREE_VIEW (priv->treeview);
 
211
    GtkTreeModel* treemodel = gtk_tree_view_get_model (treeview);
 
212
    gtk_tree_store_insert_with_values (GTK_TREE_STORE (treemodel),
 
213
                                       NULL, NULL, G_MAXINT,
 
214
                                       0, message, 1, line, 2, source_id, -1);
 
215
}