~ubuntu-branches/ubuntu/edgy/tilp/edgy

« back to all changes in this revision

Viewing changes to plugins/gfv/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien BLACHE
  • Date: 2004-05-22 21:12:03 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040522211203-awg2cuw03guyvyz9
Tags: 6.72-2
* debian/control
  + Build-Depends: libticables3 (>= 3.8.4-1).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  gfv - a Group File View plug-in for TiLP
 
2
 *  Copyright (C) 2003  Romain Lievin
 
3
 *
 
4
 *  This program is free software; you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU General Public License as published by
 
6
 *  the Free Software Foundation; either version 2 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  This program is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU General Public License
 
15
 *  along with this program; if not, write to the Free Software
 
16
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
 
 
20
/*
 
21
 * Initial main.c file generated by Glade. Edit as required.
 
22
 * Glade will not overwrite this file.
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#  include <config.h>
 
27
#endif
 
28
 
 
29
#include <gtk/gtk.h>
 
30
#include <stdio.h>
 
31
#include <string.h>
 
32
 
 
33
#include "interface.h"
 
34
#include "callbacks.h"
 
35
#include "support.h"
 
36
 
 
37
#include "tilp_plugin.h"
 
38
#include <tilp/tifiles.h>
 
39
 
 
40
#define GETTEXT_PACKAGE PLUGIN
 
41
#define FONT_NAME ""    //"courier"
 
42
 
 
43
TilpPluginInfo infos = 
 
44
{
 
45
  "Group File Viewer",
 
46
  "1.0.0",
 
47
  "Romain Lievin <roms@tilp.info>",
 
48
  "73g,82g,83g,8xg,85g,86g,89g,92g,9xg,v2g"
 
49
};
 
50
 
 
51
 
 
52
/* 
 
53
   Returns plug-ins properties/capabilites 
 
54
   - name [out]    : the plug-in name
 
55
   - version [out] : the plug-in version
 
56
   - fext [out]    : an array of file extensions
 
57
*/
 
58
TilpPluginInfo* plugin_query(void)
 
59
{
 
60
  return &infos;
 
61
}
 
62
 
 
63
 
 
64
static GtkWidget *window1;
 
65
static GtkTreeView *view;
 
66
static GtkTreeStore *tree;
 
67
 
 
68
 
 
69
/* Run plug-in */
 
70
int plugin_run(void)
 
71
{  
 
72
  GtkWidget *treeview1;
 
73
 
 
74
  GtkTreeModel *model;
 
75
  GtkCellRenderer *renderer;
 
76
  GtkTreeSelection *selection;
 
77
  GtkTreeViewColumn *column;
 
78
  gint i;
 
79
 
 
80
#ifdef ENABLE_NLS
 
81
  bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
 
82
  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
 
83
  textdomain(GETTEXT_PACKAGE);
 
84
#endif
 
85
 
 
86
  add_pixmap_directory (PACKAGE_DATA_DIR "/pixmaps");
 
87
 
 
88
  /* Create main window */
 
89
  window1 = create_window1 ();
 
90
  gtk_window_resize(GTK_WINDOW(window1), 320, 240);
 
91
  gtk_widget_show (window1);
 
92
  
 
93
  treeview1 = gtk_object_get_data(GTK_OBJECT(window1), "treeview1");
 
94
  view = GTK_TREE_VIEW(treeview1);
 
95
 
 
96
  /* Create CTree (borrowed from ctree.c) */
 
97
#ifdef __WIN32__   // use UTF-8 for varnames
 
98
  tifiles_translate_set_encoding(ENCODING_LATIN1);
 
99
#else
 
100
  tifiles_translate_set_encoding(ENCODING_UNICODE);
 
101
#endif
 
102
  
 
103
  tree = gtk_tree_store_new(CTREE_NUMBER, G_TYPE_STRING,
 
104
                            GDK_TYPE_PIXBUF, G_TYPE_STRING,
 
105
                            G_TYPE_STRING, G_TYPE_POINTER,
 
106
                            G_TYPE_STRING, GDK_TYPE_PIXBUF);
 
107
 
 
108
  model = GTK_TREE_MODEL(tree);
 
109
  gtk_tree_view_set_model(view, model);
 
110
  gtk_tree_view_set_headers_visible(view, TRUE);
 
111
  gtk_tree_view_set_headers_clickable(view, TRUE);
 
112
  gtk_tree_view_set_rules_hint(view, FALSE);
 
113
 
 
114
  column = gtk_tree_view_column_new();
 
115
  gtk_tree_view_append_column(view, column);
 
116
  gtk_tree_view_column_set_title(column, _("Name"));
 
117
  renderer = gtk_cell_renderer_pixbuf_new();
 
118
  gtk_tree_view_column_pack_start(GTK_TREE_VIEW_COLUMN(column),
 
119
                                  renderer, FALSE);
 
120
  gtk_tree_view_column_set_attributes(GTK_TREE_VIEW_COLUMN(column),
 
121
                                      renderer, "pixbuf",
 
122
                                      CTREE_ICON, NULL);
 
123
  renderer = gtk_cell_renderer_text_new();
 
124
  gtk_tree_view_column_pack_start(GTK_TREE_VIEW_COLUMN(column),
 
125
                                  renderer, FALSE);
 
126
  gtk_tree_view_column_set_attributes(GTK_TREE_VIEW_COLUMN(column),
 
127
                                      renderer, "text", CTREE_NAME,
 
128
                                      "font", CTREE_FONT,
 
129
                                      NULL);
 
130
  renderer = gtk_cell_renderer_pixbuf_new();
 
131
  gtk_tree_view_insert_column_with_attributes(view, -1, _("Attr"),
 
132
                                              renderer, "pixbuf",
 
133
                                              CTREE_ATTR, NULL);
 
134
  renderer = gtk_cell_renderer_text_new();
 
135
  gtk_tree_view_insert_column_with_attributes(view, -1, _("Type"),
 
136
                                              renderer, "text",
 
137
                                              CTREE_TYPE, NULL);
 
138
  renderer = gtk_cell_renderer_text_new();
 
139
  gtk_tree_view_insert_column_with_attributes(view, -1, _("Size"),
 
140
                                                    renderer, "text",
 
141
                                              CTREE_SIZE, NULL);
 
142
        
 
143
  for (i = 0; i < CTREE_NCOLS; i++) {
 
144
    GtkTreeViewColumn *col;
 
145
    col = gtk_tree_view_get_column(view, i);
 
146
    gtk_tree_view_column_set_resizable(col, TRUE);
 
147
  }
 
148
  
 
149
  selection = gtk_tree_view_get_selection(view);
 
150
  gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);
 
151
  
 
152
  return 0;
 
153
}
 
154
 
 
155
 
 
156
/* Load file */
 
157
int plugin_load(const char *filename)
 
158
{
 
159
  GtkTreeIter root_node;
 
160
  GdkPixbuf *pix1, *pix2, *pix3, *pix4, *pix5, *pix6;
 
161
  GdkPixbuf *pix9 = NULL;
 
162
  GtkTreeIter parent_node;
 
163
  GtkTreeIter child_node; 
 
164
 
 
165
  gchar *title;
 
166
  TicalcType calc_type;
 
167
  TiRegular  content;
 
168
  int **table;
 
169
  int num_folders;
 
170
  int i;
 
171
  int err;
 
172
 
 
173
  // set window title
 
174
  title = g_strconcat(_("Group File Viewer"), " (", filename, ")", NULL);
 
175
  gtk_window_set_title(GTK_WINDOW(window1), title);
 
176
  g_free(title);
 
177
 
 
178
  // set root node
 
179
  calc_type = tifiles_which_calc_type(filename);
 
180
  gtk_tree_store_append(tree, &root_node, NULL);
 
181
  gtk_tree_store_set(tree, &root_node, 
 
182
                     CTREE_NAME, tifiles_calctype_to_string(calc_type), 
 
183
                     CTREE_DATA, (gpointer)NULL, -1);
 
184
 
 
185
  // load file
 
186
  if((err = tifiles_read_regular_file(filename, &content)))
 
187
    {
 
188
      GtkWidget *dialog;
 
189
      char s[1024];
 
190
 
 
191
      tifiles_get_error(err, s);
 
192
      dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, 
 
193
                                      GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, s);
 
194
      while (gtk_events_pending())
 
195
        gtk_main_iteration();
 
196
      gtk_dialog_run(GTK_DIALOG(dialog));
 
197
      return -1;
 
198
    }
 
199
  
 
200
  // and build the table of folder & variable entries (tree view)
 
201
  tifiles_create_table_of_entries(&content, &table, &num_folders);
 
202
 
 
203
  // load pixmaps
 
204
  pix1 = create_pixbuf("ctree_close_dir.xpm");
 
205
  pix2 = create_pixbuf("TIicon2.ico");
 
206
  pix3 = create_pixbuf("ctree_open_dir.xpm");
 
207
  pix4 = create_pixbuf("attr_locked.xpm");
 
208
  pix5 = create_pixbuf("attr_archived.xpm");
 
209
  pix6 = create_pixbuf("TIicon4.ico");
 
210
 
 
211
  // populate tree
 
212
  for(i=0; table[i] != NULL; i++)
 
213
    {
 
214
      TiVarEntry *fentry;
 
215
      int j;
 
216
      int index = table[i][0];
 
217
      gchar **row_text = g_malloc0((CTREE_NCOLS + 1) * sizeof(gchar *));
 
218
      
 
219
      fentry = &(content.entries[index]);
 
220
 
 
221
      row_text[0] = g_strdup(fentry->folder);
 
222
      row_text[2] = NULL;
 
223
      for(j=0; table[i][j] != -1; j++);
 
224
      row_text[3] = g_strdup_printf("%i", j);
 
225
 
 
226
      gtk_tree_store_append(tree, &parent_node, &root_node);
 
227
      gtk_tree_store_set(tree, &parent_node, 
 
228
                         CTREE_NAME, row_text[0],
 
229
                         CTREE_SIZE, row_text[3],
 
230
                         CTREE_DATA, (gpointer) fentry,
 
231
                         CTREE_ICON, pix1, -1);
 
232
 
 
233
      g_strfreev(row_text);
 
234
      
 
235
      for(j=0; table[i][j] != -1; j++)
 
236
        {
 
237
          int index = table[i][j];
 
238
          TiVarEntry *entry = &(content.entries[index]);
 
239
          gchar **row_text = g_malloc0((CTREE_NCOLS + 1) * sizeof(gchar *));
 
240
          char icon_name[256];
 
241
 
 
242
          row_text[0] = g_strdup(entry->trans);
 
243
          //printf("<<%s>>\n", row_text[0]);
 
244
          //hexdump(row_text[0], 9);
 
245
          row_text[2] = g_strdup_printf("%s", 
 
246
                                        tifiles_vartype2string(entry->type));
 
247
          row_text[3] = g_strdup_printf("%u", entry->size);
 
248
        
 
249
          strcpy(icon_name, tifiles_vartype2icon(entry->type));
 
250
          strcat(icon_name, ".ico");
 
251
          tilp_file_underscorize(icon_name);
 
252
          pix9 = create_pixbuf(icon_name);
 
253
          
 
254
          gtk_tree_store_append(tree, &child_node, &parent_node);
 
255
          gtk_tree_store_set(tree, &child_node, 
 
256
                             CTREE_NAME, row_text[0],
 
257
                             CTREE_TYPE, row_text[2], 
 
258
                             CTREE_SIZE, row_text[3], 
 
259
                             CTREE_DATA, (gpointer) entry, 
 
260
                             CTREE_ICON, pix9,
 
261
                             CTREE_FONT, FONT_NAME,
 
262
                             -1);
 
263
          
 
264
          switch (entry->attr) {
 
265
          case ATTRB_LOCKED:
 
266
            gtk_tree_store_set(tree, &child_node, CTREE_ATTR, pix4, -1);
 
267
            break;
 
268
          case ATTRB_ARCHIVED:
 
269
            gtk_tree_store_set(tree, &child_node, CTREE_ATTR, pix5, -1);
 
270
            break;
 
271
          default:
 
272
            break;
 
273
          }
 
274
          g_object_unref(pix9);
 
275
          g_strfreev(row_text);
 
276
        }
 
277
    }
 
278
 
 
279
  gtk_tree_view_expand_all(view);
 
280
  
 
281
  // release file
 
282
  tifiles_free_regular_content(&content);
 
283
 
 
284
  // and icons
 
285
  g_object_unref(pix1);
 
286
  g_object_unref(pix2);
 
287
  g_object_unref(pix3);
 
288
  g_object_unref(pix4);
 
289
  g_object_unref(pix5);
 
290
  g_object_unref(pix6);
 
291
  
 
292
  return 0;
 
293
}
 
294
 
 
295
/* Save file */
 
296
int plugin_save(const char *filename)
 
297
{
 
298
// no saving
 
299
  return 0;
 
300
}
 
301
 
 
302
 
 
303
#ifdef MAIN
 
304
int
 
305
main (int argc, char *argv[])
 
306
{
 
307
  GtkWidget *window1;
 
308
 
 
309
#ifdef ENABLE_NLS
 
310
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
 
311
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
312
  textdomain (GETTEXT_PACKAGE);
 
313
#endif
 
314
 
 
315
  gtk_set_locale ();
 
316
  gtk_init (&argc, &argv);
 
317
 
 
318
  add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");
 
319
 
 
320
  /*
 
321
   * The following code was added by Glade to create one of each component
 
322
   * (except popup menus), just so that you see something after building
 
323
   * the project. Delete any components that you don't want shown initially.
 
324
   */
 
325
  window1 = create_window1 ();
 
326
  gtk_widget_show (window1);
 
327
 
 
328
  gtk_main ();
 
329
  return 0;
 
330
}
 
331
#endif