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"
42 #include <goffice/goffice.h>
43 #include <gsf/gsf-impl-utils.h>
45 #define GET_CLASS(instance) G_TYPE_INSTANCE_GET_CLASS (instance, GNM_HLINK_TYPE, GnmHLinkClass)
47 static GObjectClass
*gnm_hlink_parent_class
;
50 * WARNING WARNING WARNING
52 * The type names are used in the xml persistence DO NOT CHANGE THEM
57 * @wbcg: the wbcg that activated the link
59 * Returns: TRUE if the link successfully activated.
62 gnm_hlink_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
64 g_return_val_if_fail (GNM_IS_HLINK (lnk
), FALSE
);
66 return GET_CLASS (lnk
)->Activate (lnk
, wbcg
);
70 * gnm_sheet_hlink_find:
74 * Returns: (transfer none): the found #GnmHLink.
77 gnm_sheet_hlink_find (Sheet
const *sheet
, GnmCellPos
const *pos
)
79 GnmStyle
const *style
= sheet_style_get (sheet
, pos
->col
, pos
->row
);
80 return gnm_style_get_hlink (style
);
84 gnm_hlink_finalize (GObject
*obj
)
86 GnmHLink
*lnk
= (GnmHLink
*)obj
;
94 gnm_hlink_parent_class
->finalize (obj
);
98 gnm_hlink_base_set_sheet (GnmHLink
*lnk
, Sheet
*sheet
)
104 gnm_hlink_base_set_target (GnmHLink
*lnk
, gchar
const *target
)
106 gchar
*tmp
= g_strdup (target
);
107 g_free (lnk
->target
);
112 gnm_hlink_base_get_target (GnmHLink
const *lnk
)
118 gnm_hlink_class_init (GObjectClass
*object_class
)
120 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*)object_class
;
122 gnm_hlink_parent_class
= g_type_class_peek_parent (object_class
);
124 object_class
->finalize
= gnm_hlink_finalize
;
125 hlink_class
->set_sheet
= gnm_hlink_base_set_sheet
;
126 hlink_class
->set_target
= gnm_hlink_base_set_target
;
127 hlink_class
->get_target
= gnm_hlink_base_get_target
;
131 gnm_hlink_init (GObject
*obj
)
133 GnmHLink
*lnk
= (GnmHLink
* )obj
;
138 GSF_CLASS_ABSTRACT (GnmHLink
, gnm_hlink
,
139 gnm_hlink_class_init
, gnm_hlink_init
, G_TYPE_OBJECT
)
142 gnm_hlink_get_target (GnmHLink
const *lnk
)
144 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
146 return GET_CLASS (lnk
)->get_target (lnk
);
150 gnm_hlink_set_target (GnmHLink
*lnk
, gchar
const *target
)
152 g_return_if_fail (GNM_IS_HLINK (lnk
));
154 GET_CLASS (lnk
)->set_target (lnk
, target
);
158 gnm_hlink_get_tip (GnmHLink
const *lnk
)
160 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
165 gnm_hlink_set_tip (GnmHLink
*lnk
, gchar
const *tip
)
169 g_return_if_fail (GNM_IS_HLINK (lnk
));
171 tmp
= g_strdup (tip
);
177 * gnm_hlink_get_sheet:
180 * Returns: (transfer none): the sheet
183 gnm_hlink_get_sheet (GnmHLink
*lnk
)
185 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
190 gnm_hlink_set_sheet (GnmHLink
*lnk
, Sheet
*sheet
)
192 g_return_if_fail (GNM_IS_HLINK (lnk
));
193 GET_CLASS (lnk
)->set_sheet (lnk
, sheet
);
197 gnm_hlink_new (GType typ
, Sheet
*sheet
)
201 g_return_val_if_fail (typ
!= 0, NULL
);
202 g_return_val_if_fail (g_type_is_a (typ
, GNM_HLINK_TYPE
), NULL
);
203 g_return_val_if_fail (!G_TYPE_IS_ABSTRACT (typ
), NULL
);
204 g_return_val_if_fail (IS_SHEET (sheet
), NULL
);
206 lnk
= g_object_new (typ
, NULL
);
207 gnm_hlink_set_sheet (lnk
, sheet
);
213 * @lnk: Existing link
215 * Returns: (transfer full): A duplicate link.
218 gnm_hlink_dup (GnmHLink
*lnk
)
220 GnmHLink
*new_lnk
= g_object_new (G_OBJECT_TYPE (lnk
), NULL
);
222 gnm_hlink_set_sheet (new_lnk
, lnk
->sheet
);
223 gnm_hlink_set_target (new_lnk
, gnm_hlink_get_target (lnk
));
224 gnm_hlink_set_tip (new_lnk
, lnk
->tip
);
233 * @relax_sheet: if %TRUE, ignore differences solely caused by being linked into different sheets.
235 * Returns: %TRUE, if links are equal
238 gnm_hlink_equal (GnmHLink
const *a
, GnmHLink
const *b
, gboolean relax_sheet
)
240 g_return_val_if_fail (GNM_IS_HLINK (a
), FALSE
);
241 g_return_val_if_fail (GNM_IS_HLINK (b
), FALSE
);
246 if (!relax_sheet
&& a
->sheet
!= b
->sheet
)
249 return (g_strcmp0 (a
->target
, b
->target
) == 0 &&
250 g_strcmp0 (a
->tip
, b
->tip
) == 0);
253 /***************************************************************************/
254 /* Link to named regions within the current workbook */
255 typedef struct { GnmHLinkClass hlink
; } GnmHLinkCurWBClass
;
261 #define GNM_HLINK_CUR_WB(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), gnm_hlink_cur_wb_get_type (), GnmHLinkCurWB))
263 #define GNM_IS_HLINK_CUR_WB(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), gnm_hlink_cur_wb_get_type ()))
266 static GObjectClass
*gnm_hlink_cur_wb_parent_class
;
269 gnm_hlink_cur_wb_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
271 WorkbookControl
*wbc
= GNM_WBC (wbcg
);
275 if (!gnm_hlink_get_range_target (lnk
, &sr
)) {
276 go_cmd_context_error_invalid
277 (GO_CMD_CONTEXT (wbcg
),
279 lnk
->target
? lnk
->target
: "-");
283 sv
= sheet_get_view (sr
.sheet
, wb_control_view (wbc
));
284 sv_selection_set (sv
, &sr
.range
.start
,
285 sr
.range
.start
.col
, sr
.range
.start
.row
,
286 sr
.range
.end
.col
, sr
.range
.end
.row
);
287 sv_make_cell_visible (sv
, sr
.range
.start
.col
, sr
.range
.start
.row
, FALSE
);
288 if (wbcg_cur_sheet (wbcg
) != sr
.sheet
)
289 wb_view_sheet_focus (wb_control_view (wbc
), sr
.sheet
);
295 gnm_hlink_cur_wb_set_sheet (GnmHLink
*lnk
, Sheet
*sheet
)
297 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)lnk
;
298 ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
299 ->set_sheet (lnk
, sheet
);
300 dependent_managed_set_sheet (&hlcwb
->dep
, sheet
);
304 gnm_hlink_cur_wb_set_target (GnmHLink
*lnk
, const char *target
)
306 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)lnk
;
307 GnmExprTop
const *texpr
= NULL
;
309 ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
310 ->set_target (lnk
, NULL
);
312 if (target
&& lnk
->sheet
) {
314 GnmExprParseFlags flags
= GNM_EXPR_PARSE_UNKNOWN_NAMES_ARE_INVALID
;
315 GnmConventions
const *convs
= lnk
->sheet
->convs
;
317 parse_pos_init_sheet (&pp
, lnk
->sheet
);
318 texpr
= gnm_expr_parse_str (target
, &pp
, flags
, convs
, NULL
);
320 if (texpr
== NULL
|| gnm_expr_top_is_err (texpr
, GNM_ERROR_REF
)) {
322 } else if (gnm_expr_get_name (texpr
->expr
)) {
323 // Nothing, we're good
325 // Allow only ranges and normalize
326 GnmValue
*v
= gnm_expr_top_get_range (texpr
);
327 gnm_expr_top_unref (texpr
);
328 texpr
= v
? gnm_expr_top_new_constant (v
) : NULL
;
332 dependent_managed_set_expr (&hlcwb
->dep
, texpr
);
334 gnm_expr_top_unref (texpr
);
338 gnm_hlink_cur_wb_get_target (GnmHLink
const *lnk
)
340 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)lnk
;
341 GnmExprTop
const *texpr
= hlcwb
->dep
.texpr
;
343 Sheet
*sheet
= lnk
->sheet
;
345 if (texpr
&& sheet
) {
346 GnmConventions
const *convs
= sheet_get_conventions (sheet
);
348 parse_pos_init_sheet (&pp
, sheet
);
349 tgt
= gnm_expr_top_as_string (texpr
, &pp
, convs
);
352 // Use parent class for storage. Ick!
353 ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
354 ->set_target ((GnmHLink
*)lnk
, tgt
);
357 return ((GnmHLinkClass
*)gnm_hlink_cur_wb_parent_class
)
362 gnm_hlink_cur_wb_init (GObject
*obj
)
364 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)obj
;
365 dependent_managed_init (&hlcwb
->dep
, NULL
);
369 gnm_hlink_cur_wb_finalize (GObject
*obj
)
371 GnmHLinkCurWB
*hlcwb
= (GnmHLinkCurWB
*)obj
;
373 dependent_managed_set_expr (&hlcwb
->dep
, NULL
);
375 gnm_hlink_cur_wb_parent_class
->finalize (obj
);
379 gnm_hlink_cur_wb_class_init (GObjectClass
*object_class
)
381 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*) object_class
;
383 gnm_hlink_cur_wb_parent_class
= g_type_class_peek_parent (object_class
);
385 object_class
->finalize
= gnm_hlink_cur_wb_finalize
;
386 hlink_class
->Activate
= gnm_hlink_cur_wb_activate
;
387 hlink_class
->set_sheet
= gnm_hlink_cur_wb_set_sheet
;
388 hlink_class
->set_target
= gnm_hlink_cur_wb_set_target
;
389 hlink_class
->get_target
= gnm_hlink_cur_wb_get_target
;
392 GSF_CLASS (GnmHLinkCurWB
, gnm_hlink_cur_wb
,
393 gnm_hlink_cur_wb_class_init
, gnm_hlink_cur_wb_init
,
401 * gnm_hlink_get_range_target:
402 * @lnk: the hyperlink to query
403 * @sr: location to start link target range
405 * This function determines the location that a link points to. It will
408 * Returns: %TRUE, if the link refers to a range.
411 gnm_hlink_get_range_target (GnmHLink
const *lnk
, GnmSheetRange
*sr
)
413 GnmHLinkCurWB
*hlcwb
;
414 GnmExprTop
const *texpr
;
416 GnmRangeRef
const *r
;
418 Sheet
*start_sheet
, *end_sheet
;
420 g_return_val_if_fail (GNM_IS_HLINK (lnk
), FALSE
);
422 if (!GNM_IS_HLINK_CUR_WB (lnk
))
425 hlcwb
= (GnmHLinkCurWB
*)lnk
;
426 texpr
= hlcwb
->dep
.texpr
;
429 vr
= gnm_expr_top_get_range (texpr
);
432 r
= value_get_rangeref (vr
);
434 parse_pos_init_sheet (&pp
, lnk
->sheet
);
435 gnm_rangeref_normalize_pp (r
, &pp
, &start_sheet
, &end_sheet
,
437 sr
->sheet
= start_sheet
;
445 gnm_hlink_get_target_expr (GnmHLink
const *lnk
)
448 GnmHLinkCurWB
*hlcwb
;
450 g_return_val_if_fail (GNM_IS_HLINK (lnk
), NULL
);
452 if (!GNM_IS_HLINK_CUR_WB (lnk
))
455 hlcwb
= (GnmHLinkCurWB
*)lnk
;
456 return hlcwb
->dep
.texpr
;
461 /***************************************************************************/
462 /* Link to arbitrary urls */
463 typedef struct { GnmHLinkClass hlink
; } GnmHLinkURLClass
;
469 gnm_hlink_url_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
474 if (lnk
->target
== NULL
)
477 screen
= gtk_window_get_screen (wbcg_toplevel (wbcg
));
478 err
= go_gtk_url_show (lnk
->target
, screen
);
481 char *msg
= g_strdup_printf (_("Unable to activate the url '%s'"), lnk
->target
);
482 go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbcg
),
492 gnm_hlink_url_class_init (GObjectClass
*object_class
)
494 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*) object_class
;
496 hlink_class
->Activate
= gnm_hlink_url_activate
;
499 GSF_CLASS (GnmHLinkURL
, gnm_hlink_url
,
500 gnm_hlink_url_class_init
, NULL
,
503 /***************************************************************************/
504 /* email is just a url, but it is cleaner to stick it in a distinct type */
505 typedef struct { GnmHLinkURLClass hlink
; } GnmHLinkEMailClass
;
510 GSF_CLASS (GnmHLinkEMail
, gnm_hlink_email
,
512 gnm_hlink_url_get_type ())
514 /***************************************************************************/
515 /* Link to arbitrary urls */
516 typedef struct { GnmHLinkClass hlink
; } GnmHLinkExternalClass
;
522 gnm_hlink_external_activate (GnmHLink
*lnk
, WBCGtk
*wbcg
)
525 gboolean res
= FALSE
;
529 if (lnk
->target
== NULL
)
532 cmd
= go_shell_arg_to_uri (lnk
->target
);
533 screen
= gtk_window_get_screen (wbcg_toplevel (wbcg
));
534 err
= go_gtk_url_show (cmd
, screen
);
538 char *msg
= g_strdup_printf(_("Unable to open '%s'"), lnk
->target
);
539 go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbcg
),
549 gnm_hlink_external_class_init (GObjectClass
*object_class
)
551 GnmHLinkClass
*hlink_class
= (GnmHLinkClass
*) object_class
;
553 hlink_class
->Activate
= gnm_hlink_external_activate
;
556 GSF_CLASS (GnmHLinkExternal
, gnm_hlink_external
,
557 gnm_hlink_external_class_init
, NULL
,
561 _gnm_hlink_init (void)
563 /* make sure that all hlink types are registered */
564 gnm_hlink_cur_wb_get_type ();
565 gnm_hlink_url_get_type ();
566 gnm_hlink_email_get_type ();
567 gnm_hlink_external_get_type ();