~ubuntu-branches/ubuntu/breezy/gimp/breezy

« back to all changes in this revision

Viewing changes to app/widgets/gimptexteditor.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-10-04 19:04:46 UTC
  • Revision ID: james.westby@ubuntu.com-20051004190446-ukh32kwk56s4sjhu
Tags: upstream-2.2.8
ImportĀ upstreamĀ versionĀ 2.2.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * GimpTextEditor
 
5
 * Copyright (C) 2002-2003  Sven Neumann <sven@gimp.org>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <gtk/gtk.h>
 
25
 
 
26
#include "libgimpwidgets/gimpwidgets.h"
 
27
 
 
28
#include "widgets-types.h"
 
29
 
 
30
#include "core/gimpmarshal.h"
 
31
 
 
32
#include "gimphelp-ids.h"
 
33
#include "gimpmenufactory.h"
 
34
#include "gimptexteditor.h"
 
35
#include "gimpuimanager.h"
 
36
 
 
37
#include "gimp-intl.h"
 
38
 
 
39
 
 
40
enum
 
41
{
 
42
  TEXT_CHANGED,
 
43
  DIR_CHANGED,
 
44
  LAST_SIGNAL
 
45
};
 
46
 
 
47
 
 
48
static void      gimp_text_editor_class_init    (GimpTextEditorClass *klass);
 
49
static void      gimp_text_editor_init          (GimpTextEditor      *editor);
 
50
 
 
51
static void      gimp_text_editor_text_changed  (GtkTextBuffer       *buffer,
 
52
                                                 GimpTextEditor      *editor);
 
53
 
 
54
 
 
55
static GimpDialogClass *parent_class = NULL;
 
56
static guint            text_editor_signals[LAST_SIGNAL] = { 0 };
 
57
 
 
58
 
 
59
GType
 
60
gimp_text_editor_get_type (void)
 
61
{
 
62
  static GType type = 0;
 
63
 
 
64
  if (! type)
 
65
    {
 
66
      static const GTypeInfo info =
 
67
      {
 
68
        sizeof (GimpTextEditorClass),
 
69
        NULL,           /* base_init */
 
70
        NULL,           /* base_finalize */
 
71
        (GClassInitFunc) gimp_text_editor_class_init,
 
72
        NULL,           /* class_finalize */
 
73
        NULL,           /* class_data */
 
74
        sizeof (GimpTextEditor),
 
75
        0,              /* n_preallocs */
 
76
        (GInstanceInitFunc) gimp_text_editor_init,
 
77
      };
 
78
 
 
79
      type = g_type_register_static (GIMP_TYPE_DIALOG,
 
80
                                     "GimpTextEditor",
 
81
                                     &info, 0);
 
82
    }
 
83
 
 
84
  return type;
 
85
}
 
86
 
 
87
static void
 
88
gimp_text_editor_class_init (GimpTextEditorClass *klass)
 
89
{
 
90
  parent_class = g_type_class_peek_parent (klass);
 
91
 
 
92
  text_editor_signals[TEXT_CHANGED] =
 
93
    g_signal_new ("text_changed",
 
94
                  G_TYPE_FROM_CLASS (klass),
 
95
                  G_SIGNAL_RUN_FIRST,
 
96
                  G_STRUCT_OFFSET (GimpTextEditorClass, text_changed),
 
97
                  NULL, NULL,
 
98
                  gimp_marshal_VOID__VOID,
 
99
                  G_TYPE_NONE, 0);
 
100
 
 
101
  text_editor_signals[DIR_CHANGED] =
 
102
    g_signal_new ("dir_changed",
 
103
                  G_TYPE_FROM_CLASS (klass),
 
104
                  G_SIGNAL_RUN_FIRST,
 
105
                  G_STRUCT_OFFSET (GimpTextEditorClass, dir_changed),
 
106
                  NULL, NULL,
 
107
                  gimp_marshal_VOID__VOID,
 
108
                  G_TYPE_NONE, 0);
 
109
 
 
110
  klass->text_changed = NULL;
 
111
  klass->dir_changed  = NULL;
 
112
}
 
113
 
 
114
static void
 
115
gimp_text_editor_init (GimpTextEditor  *editor)
 
116
{
 
117
  editor->view        = NULL;
 
118
  editor->file_dialog = NULL;
 
119
  editor->ui_manager  = NULL;
 
120
 
 
121
  switch (gtk_widget_get_default_direction ())
 
122
    {
 
123
    case GTK_TEXT_DIR_NONE:
 
124
    case GTK_TEXT_DIR_LTR:
 
125
      editor->base_dir = GIMP_TEXT_DIRECTION_LTR;
 
126
      break;
 
127
    case GTK_TEXT_DIR_RTL:
 
128
      editor->base_dir = GIMP_TEXT_DIRECTION_RTL;
 
129
      break;
 
130
    }
 
131
}
 
132
 
 
133
 
 
134
/*  public functions  */
 
135
 
 
136
GtkWidget *
 
137
gimp_text_editor_new (const gchar     *title,
 
138
                      GimpMenuFactory *menu_factory)
 
139
{
 
140
  GimpTextEditor *editor;
 
141
  GtkTextBuffer  *buffer;
 
142
  GtkWidget      *toolbar;
 
143
  GtkWidget      *scrolled_window;
 
144
 
 
145
  g_return_val_if_fail (title != NULL, NULL);
 
146
  g_return_val_if_fail (GIMP_IS_MENU_FACTORY (menu_factory), NULL);
 
147
 
 
148
  editor = g_object_new (GIMP_TYPE_TEXT_EDITOR,
 
149
                         "title",     title,
 
150
                         "role",      "gimp-text-editor",
 
151
                         "help-func", gimp_standard_help_func,
 
152
                         "help-id",   GIMP_HELP_TEXT_EDITOR_DIALOG,
 
153
                         NULL);
 
154
 
 
155
  gtk_dialog_add_button (GTK_DIALOG (editor),
 
156
                         GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
 
157
 
 
158
  g_signal_connect (editor, "response",
 
159
                    G_CALLBACK (gtk_widget_destroy),
 
160
                    NULL);
 
161
 
 
162
  editor->ui_manager = gimp_menu_factory_manager_new (menu_factory,
 
163
                                                      "<TextEditor>",
 
164
                                                      editor, FALSE);
 
165
 
 
166
  toolbar = gimp_ui_manager_ui_get (editor->ui_manager,
 
167
                                    "/text-editor-toolbar");
 
168
 
 
169
  if (toolbar)
 
170
    {
 
171
      gtk_box_pack_start (GTK_BOX (GTK_DIALOG (editor)->vbox), toolbar,
 
172
                          FALSE, FALSE, 0);
 
173
      gtk_widget_show (toolbar);
 
174
    }
 
175
 
 
176
  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
 
177
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
 
178
                                  GTK_POLICY_AUTOMATIC,
 
179
                                  GTK_POLICY_AUTOMATIC);
 
180
  gtk_container_set_border_width (GTK_CONTAINER (scrolled_window), 2);
 
181
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (editor)->vbox),
 
182
                      scrolled_window, TRUE, TRUE, 0);
 
183
  gtk_widget_show (scrolled_window);
 
184
 
 
185
  editor->view = gtk_text_view_new ();
 
186
  gtk_container_add (GTK_CONTAINER (scrolled_window), editor->view);
 
187
  gtk_widget_show (editor->view);
 
188
 
 
189
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
 
190
 
 
191
  g_signal_connect (buffer, "changed",
 
192
                    G_CALLBACK (gimp_text_editor_text_changed),
 
193
                    editor);
 
194
 
 
195
  switch (editor->base_dir)
 
196
    {
 
197
    case GIMP_TEXT_DIRECTION_LTR:
 
198
      gtk_widget_set_direction (editor->view, GTK_TEXT_DIR_LTR);
 
199
      break;
 
200
    case GIMP_TEXT_DIRECTION_RTL:
 
201
      gtk_widget_set_direction (editor->view, GTK_TEXT_DIR_RTL);
 
202
      break;
 
203
    }
 
204
 
 
205
  gtk_widget_set_size_request (editor->view, 128, 64);
 
206
 
 
207
  gtk_widget_grab_focus (editor->view);
 
208
 
 
209
  gimp_ui_manager_update (editor->ui_manager, editor);
 
210
 
 
211
  return GTK_WIDGET (editor);
 
212
}
 
213
 
 
214
void
 
215
gimp_text_editor_set_text (GimpTextEditor *editor,
 
216
                           const gchar    *text,
 
217
                           gint            len)
 
218
{
 
219
  GtkTextBuffer *buffer;
 
220
 
 
221
  g_return_if_fail (GIMP_IS_TEXT_EDITOR (editor));
 
222
  g_return_if_fail (text != NULL || len == 0);
 
223
 
 
224
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
 
225
 
 
226
  if (text)
 
227
    gtk_text_buffer_set_text (buffer, text, len);
 
228
  else
 
229
    gtk_text_buffer_set_text (buffer, "", 0);
 
230
}
 
231
 
 
232
gchar *
 
233
gimp_text_editor_get_text (GimpTextEditor *editor)
 
234
{
 
235
  GtkTextBuffer *buffer;
 
236
  GtkTextIter    start_iter;
 
237
  GtkTextIter    end_iter;
 
238
 
 
239
  g_return_val_if_fail (GIMP_IS_TEXT_EDITOR (editor), NULL);
 
240
 
 
241
  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->view));
 
242
 
 
243
  gtk_text_buffer_get_bounds (buffer, &start_iter, &end_iter);
 
244
 
 
245
  return gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
 
246
}
 
247
 
 
248
void
 
249
gimp_text_editor_set_direction (GimpTextEditor    *editor,
 
250
                                GimpTextDirection  base_dir)
 
251
{
 
252
  g_return_if_fail (GIMP_IS_TEXT_EDITOR (editor));
 
253
 
 
254
  if (editor->base_dir == base_dir)
 
255
    return;
 
256
 
 
257
  editor->base_dir = base_dir;
 
258
 
 
259
  if (editor->view)
 
260
    {
 
261
      switch (editor->base_dir)
 
262
        {
 
263
        case GIMP_TEXT_DIRECTION_LTR:
 
264
          gtk_widget_set_direction (editor->view, GTK_TEXT_DIR_LTR);
 
265
          break;
 
266
        case GIMP_TEXT_DIRECTION_RTL:
 
267
          gtk_widget_set_direction (editor->view, GTK_TEXT_DIR_RTL);
 
268
          break;
 
269
        }
 
270
    }
 
271
 
 
272
  gimp_ui_manager_update (editor->ui_manager, editor);
 
273
 
 
274
  g_signal_emit (editor, text_editor_signals[DIR_CHANGED], 0);
 
275
}
 
276
 
 
277
GimpTextDirection
 
278
gimp_text_editor_get_direction (GimpTextEditor *editor)
 
279
{
 
280
  g_return_val_if_fail (GIMP_IS_TEXT_EDITOR (editor), GIMP_TEXT_DIRECTION_LTR);
 
281
 
 
282
  return editor->base_dir;
 
283
}
 
284
 
 
285
 
 
286
/*  private functions  */
 
287
 
 
288
static void
 
289
gimp_text_editor_text_changed (GtkTextBuffer  *buffer,
 
290
                               GimpTextEditor *editor)
 
291
{
 
292
  g_signal_emit (editor, text_editor_signals[TEXT_CHANGED], 0);
 
293
}