1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
4 * hlink.c: hyperlink support
6 * Copyright (C) 2000-2005 Jody Goldberg (jody@gnome.org)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) version 3.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 #include <gnumeric-config.h>
24 #include <glib/gi18n-lib.h>
27 #include "hlink-impl.h"
28 #include "command-context.h"
29 #include "workbook-control.h"
30 #include "workbook-view.h"
31 #include "selection.h"
33 #include "sheet-view.h"
34 #include "sheet-style.h"
37 #include "expr-name.h"
39 #include "expr-impl.h"
43 #include <goffice/goffice.h>
44 #include <gsf/gsf-impl-utils.h>
46 #define GET_CLASS(instance) G_TYPE_INSTANCE_GET_CLASS (instance, GNM_HLINK_TYPE, GnmHLinkClass)
48 static GObjectClass
*gnm_hlink_parent_class
;
51 * WARNING WARNING WARNING
53 * The type names are used in the xml persistence DO NOT CHANGE THEM
58 * @wbcg: the wbcg that activated the link
60 * Returns: TRUE if the link successfully activated.
63 gnm_hlink_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
65 g_return_val_if_fail (GNM_IS_HLINK (lnk
), FALSE
);
67 return GET_CLASS (lnk
)->Activate (lnk
, wbcg
);
71 * gnm_sheet_hlink_find:
75 * Returns: (transfer none): the found #GnmHLink.
78 gnm_sheet_hlink_find (Sheet
const *sheet
, GnmCellPos
const *pos
)
80 GnmStyle
const *style
= sheet_style_get (sheet
, pos
->col
, pos
->row
);
81 return gnm_style_get_hlink (style
);
85 gnm_hlink_finalize (GObject
*obj
)
87 GnmHLink
*lnk
= (GnmHLink
*)obj
;
95 gnm_hlink_parent_class
->finalize (obj
);
99 gnm_hlink_base_set_sheet (GnmHLink
*lnk
, Sheet
*sheet
)
105 gnm_hlink_base_set_target (GnmHLink
*lnk
, gchar
const *target
)
107 gchar
*tmp
= g_strdup (target
);
108 g_free (lnk
->target
);
113 gnm_hlink_base_get_target (GnmHLink
const *lnk
)
119 gnm_hlink_class_init (GObjectClass
*object_class
)
121 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*)object_class
;
123 gnm_hlink_parent_class
= g_type_class_peek_parent (object_class
);
125 object_class
->finalize
= gnm_hlink_finalize
;
126 hlink_class
->set_sheet
= gnm_hlink_base_set_sheet
;
127 hlink_class
->set_target
= gnm_hlink_base_set_target
;
128 hlink_class
->get_target
= gnm_hlink_base_get_target
;
132 gnm_hlink_init (GObject
*obj
)
134 GnmHLink
*lnk
= (GnmHLink
* )obj
;
139 GSF_CLASS_ABSTRACT (GnmHLink
, gnm_hlink
,
140 gnm_hlink_class_init
, gnm_hlink_init
, G_TYPE_OBJECT
)
143 gnm_hlink_get_target (GnmHLink
const *lnk
)
145 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
147 return GET_CLASS (lnk
)->get_target (lnk
);
151 gnm_hlink_set_target (GnmHLink
*lnk
, gchar
const *target
)
153 g_return_if_fail (GNM_IS_HLINK (lnk
));
155 GET_CLASS (lnk
)->set_target (lnk
, target
);
159 gnm_hlink_get_tip (GnmHLink
const *lnk
)
161 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
166 gnm_hlink_set_tip (GnmHLink
*lnk
, gchar
const *tip
)
170 g_return_if_fail (GNM_IS_HLINK (lnk
));
172 tmp
= g_strdup (tip
);
178 * gnm_hlink_get_sheet:
181 * Returns: (transfer none): the sheet
184 gnm_hlink_get_sheet (GnmHLink
*lnk
)
186 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
191 gnm_hlink_set_sheet (GnmHLink
*lnk
, Sheet
*sheet
)
193 g_return_if_fail (GNM_IS_HLINK (lnk
));
194 GET_CLASS (lnk
)->set_sheet (lnk
, sheet
);
198 gnm_hlink_new (GType typ
, Sheet
*sheet
)
202 g_return_val_if_fail (typ
!= 0, NULL
);
203 g_return_val_if_fail (g_type_is_a (typ
, GNM_HLINK_TYPE
), NULL
);
204 g_return_val_if_fail (!G_TYPE_IS_ABSTRACT (typ
), NULL
);
205 g_return_val_if_fail (IS_SHEET (sheet
), NULL
);
207 lnk
= g_object_new (typ
, NULL
);
208 gnm_hlink_set_sheet (lnk
, sheet
);
214 * @lnk: Existing link
216 * Returns: (transfer full): A duplicate link.
219 gnm_hlink_dup (GnmHLink
*lnk
)
221 GnmHLink
*new_lnk
= g_object_new (G_OBJECT_TYPE (lnk
), NULL
);
223 gnm_hlink_set_sheet (new_lnk
, lnk
->sheet
);
224 gnm_hlink_set_target (new_lnk
, gnm_hlink_get_target (lnk
));
225 gnm_hlink_set_tip (new_lnk
, lnk
->tip
);
234 * @relax_sheet: if %TRUE, ignore differences solely caused by being linked into different sheets.
236 * Returns: %TRUE, if links are equal
239 gnm_hlink_equal (GnmHLink
const *a
, GnmHLink
const *b
, gboolean relax_sheet
)
241 g_return_val_if_fail (GNM_IS_HLINK (a
), NULL
);
242 g_return_val_if_fail (GNM_IS_HLINK (b
), NULL
);
244 if (!relax_sheet
&& a
->sheet
!= b
->sheet
)
247 return (g_strcmp0 (a
->target
, b
->target
) == 0 &&
248 g_strcmp0 (a
->tip
, b
->tip
) == 0);
251 /***************************************************************************/
252 /* Link to named regions within the current workbook */
253 typedef struct { GnmHLinkClass hlink
; } GnmHLinkCurWBClass
;
259 #define GNM_HLINK_CUR_WB(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), gnm_hlink_cur_wb_get_type (), GnmHLinkCurWB))
261 #define GNM_IS_HLINK_CUR_WB(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), gnm_hlink_cur_wb_get_type ()))
264 static GObjectClass
*gnm_hlink_cur_wb_parent_class
;
267 gnm_hlink_cur_wb_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
269 WorkbookControl
*wbc
= GNM_WBC (wbcg
);
273 if (!gnm_hlink_get_range_target (lnk
, &sr
)) {
274 go_cmd_context_error_invalid
275 (GO_CMD_CONTEXT (wbcg
),
277 lnk
->target
? lnk
->target
: "-");
281 sv
= sheet_get_view (sr
.sheet
, wb_control_view (wbc
));
282 sv_selection_set (sv
, &sr
.range
.start
,
283 sr
.range
.start
.col
, sr
.range
.start
.row
,
284 sr
.range
.end
.col
, sr
.range
.end
.row
);
285 sv_make_cell_visible (sv
, sr
.range
.start
.col
, sr
.range
.start
.row
, FALSE
);
286 if (wbcg_cur_sheet (wbcg
) != sr
.sheet
)
287 wb_view_sheet_focus (wb_control_view (wbc
), sr
.sheet
);
293 gnm_hlink_cur_wb_set_sheet (GnmHLink
*lnk
, Sheet
*sheet
)
295 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)lnk
;
296 ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
297 ->set_sheet (lnk
, sheet
);
298 dependent_managed_set_sheet (&hlcwb
->dep
, sheet
);
302 gnm_hlink_cur_wb_set_target (GnmHLink
*lnk
, const char *target
)
304 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)lnk
;
305 GnmExprTop
const *texpr
= NULL
;
307 ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
308 ->set_target (lnk
, NULL
);
310 if (target
&& lnk
->sheet
) {
312 GnmExprParseFlags flags
= GNM_EXPR_PARSE_UNKNOWN_NAMES_ARE_INVALID
;
313 GnmConventions
const *convs
= lnk
->sheet
->convs
;
315 parse_pos_init_sheet (&pp
, lnk
->sheet
);
316 texpr
= gnm_expr_parse_str (target
, &pp
, flags
, convs
, NULL
);
318 if (texpr
== NULL
|| gnm_expr_top_is_err (texpr
, GNM_ERROR_REF
)) {
320 } else if (GNM_EXPR_GET_OPER (texpr
->expr
) == GNM_EXPR_OP_NAME
) {
321 // Nothing, we're good
323 // Allow only ranges and normalize
324 GnmValue
*v
= gnm_expr_top_get_range (texpr
);
325 gnm_expr_top_unref (texpr
);
326 texpr
= v
? gnm_expr_top_new_constant (v
) : NULL
;
330 dependent_managed_set_expr (&hlcwb
->dep
, texpr
);
332 gnm_expr_top_unref (texpr
);
336 gnm_hlink_cur_wb_get_target (GnmHLink
const *lnk
)
338 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)lnk
;
339 GnmExprTop
const *texpr
= hlcwb
->dep
.texpr
;
341 Sheet
*sheet
= lnk
->sheet
;
343 if (texpr
&& sheet
) {
344 GnmConventions
const *convs
= sheet_get_conventions (sheet
);
346 parse_pos_init_sheet (&pp
, sheet
);
347 tgt
= gnm_expr_top_as_string (texpr
, &pp
, convs
);
350 // Use parent class for storage. Ick!
351 ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
352 ->set_target ((GnmHLink
*)lnk
, tgt
);
355 return ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
360 gnm_hlink_cur_wb_init (GObject
*obj
)
362 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)obj
;
363 dependent_managed_init (&hlcwb
->dep
, NULL
);
367 gnm_hlink_cur_wb_finalize (GObject
*obj
)
369 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)obj
;
371 dependent_managed_set_expr (&hlcwb
->dep
, NULL
);
373 gnm_hlink_cur_wb_parent_class
->finalize (obj
);
377 gnm_hlink_cur_wb_class_init (GObjectClass
*object_class
)
379 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*) object_class
;
381 gnm_hlink_cur_wb_parent_class
= g_type_class_peek_parent (object_class
);
383 object_class
->finalize
= gnm_hlink_cur_wb_finalize
;
384 hlink_class
->Activate
= gnm_hlink_cur_wb_activate
;
385 hlink_class
->set_sheet
= gnm_hlink_cur_wb_set_sheet
;
386 hlink_class
->set_target
= gnm_hlink_cur_wb_set_target
;
387 hlink_class
->get_target
= gnm_hlink_cur_wb_get_target
;
390 GSF_CLASS (GnmHLinkCurWB
, gnm_hlink_cur_wb
,
391 gnm_hlink_cur_wb_class_init
, gnm_hlink_cur_wb_init
,
399 * gnm_hlink_get_range_target:
400 * @lnk: the hyperlink to query
401 * @sr: location to start link target range
403 * This function determines the location that a link points to. It will
406 * Returns: %TRUE, if the link refers to a range.
409 gnm_hlink_get_range_target (GnmHLink
const *lnk
, GnmSheetRange
*sr
)
411 GnmHLinkCurWB
*hlcwb
;
412 GnmExprTop
const *texpr
;
414 GnmRangeRef
const *r
;
416 Sheet
*start_sheet
, *end_sheet
;
418 g_return_val_if_fail (GNM_IS_HLINK (lnk
), FALSE
);
420 if (!GNM_IS_HLINK_CUR_WB (lnk
))
423 hlcwb
= (GnmHLinkCurWB
*)lnk
;
424 texpr
= hlcwb
->dep
.texpr
;
427 vr
= gnm_expr_top_get_range (texpr
);
430 r
= value_get_rangeref (vr
);
432 parse_pos_init_sheet (&pp
, lnk
->sheet
);
433 gnm_rangeref_normalize_pp (r
, &pp
, &start_sheet
, &end_sheet
,
435 sr
->sheet
= start_sheet
;
443 gnm_hlink_get_target_expr (GnmHLink
const *lnk
)
446 GnmHLinkCurWB
*hlcwb
;
448 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
450 if (!GNM_IS_HLINK_CUR_WB (lnk
))
453 hlcwb
= (GnmHLinkCurWB
*)lnk
;
454 return hlcwb
->dep
.texpr
;
459 /***************************************************************************/
460 /* Link to arbitrary urls */
461 typedef struct { GnmHLinkClass hlink
; } GnmHLinkURLClass
;
467 gnm_hlink_url_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
472 if (lnk
->target
== NULL
)
475 screen
= gtk_window_get_screen (wbcg_toplevel (wbcg
));
476 err
= go_gtk_url_show (lnk
->target
, screen
);
479 char *msg
= g_strdup_printf (_("Unable to activate the url '%s'"), lnk
->target
);
480 go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbcg
),
490 gnm_hlink_url_class_init (GObjectClass
*object_class
)
492 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*) object_class
;
494 hlink_class
->Activate
= gnm_hlink_url_activate
;
497 GSF_CLASS (GnmHLinkURL
, gnm_hlink_url
,
498 gnm_hlink_url_class_init
, NULL
,
501 /***************************************************************************/
502 /* email is just a url, but it is cleaner to stick it in a distinct type */
503 typedef struct { GnmHLinkURLClass hlink
; } GnmHLinkEMailClass
;
508 GSF_CLASS (GnmHLinkEMail
, gnm_hlink_email
,
510 gnm_hlink_url_get_type ())
512 /***************************************************************************/
513 /* Link to arbitrary urls */
514 typedef struct { GnmHLinkClass hlink
; } GnmHLinkExternalClass
;
520 gnm_hlink_external_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
523 gboolean res
= FALSE
;
527 if (lnk
->target
== NULL
)
530 cmd
= go_shell_arg_to_uri (lnk
->target
);
531 screen
= gtk_window_get_screen (wbcg_toplevel (wbcg
));
532 err
= go_gtk_url_show (cmd
, screen
);
536 char *msg
= g_strdup_printf(_("Unable to open '%s'"), lnk
->target
);
537 go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbcg
),
547 gnm_hlink_external_class_init (GObjectClass
*object_class
)
549 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*) object_class
;
551 hlink_class
->Activate
= gnm_hlink_external_activate
;
554 GSF_CLASS (GnmHLinkExternal
, gnm_hlink_external
,
555 gnm_hlink_external_class_init
, NULL
,
559 _gnm_hlink_init (void)
561 /* make sure that all hlink types are registered */
562 gnm_hlink_cur_wb_get_type ();
563 gnm_hlink_url_get_type ();
564 gnm_hlink_email_get_type ();
565 gnm_hlink_external_get_type ();