//!
//! @brief Searches for the word in a webbrower in an external dictionary
//! @param widget Unused GtkWidget pointer
//! @param data Unused gpointer
//!
static void _resultpopup_search_online_cb (GtkMenuItem *widget, gpointer data)
{
    //Sanity check
    g_assert (data != NULL);

    //Declarations
    GwApplication *application;
    LwSearchItem *item;
    GError *error;
    GwSearchWindow *window;
    GtkTextView *view;
    GwSearchData *sdata;

    //Initializations
    item = LW_SEARCHITEM (data);
    if (item != NULL)
    {
      sdata = GW_SEARCHDATA (lw_searchitem_get_data (item));
      window = GW_SEARCHWINDOW (sdata->window);
      application = GW_APPLICATION (gw_window_get_application (GW_WINDOW (window)));
      view = gw_searchwindow_get_current_textview (window);
      sdata = gw_searchdata_new (view, window);
      lw_searchitem_set_data (item, sdata, LW_SEARCHITEM_DATA_FREE_FUNC (gw_searchdata_free));
      error = NULL;
      printf("%s\n", item->queryline->string);

      gtk_show_uri (NULL, item->queryline->string, gtk_get_current_event_time (), &error);
      gw_application_handle_error (application, GTK_WINDOW (window), TRUE, &error);
    }
}
Beispiel #2
0
//!
//! @brief Pagination algorithm to calculate how many pages are needed
//! @param operation Unused GtkPrintOperation
//! @param context Pointer co a GtkPrintContext to draw on
//! @param page_nr Integer of the current page number being draw
//! @param data Painter to a PageInfo struct to use for information
//! @return Return true when pagination finishes
//! @sa _done() _begin_print() draw() _begin_print()
//!
static gboolean _paginate (GtkPrintOperation *operation,
                           GtkPrintContext   *context,
                           GwPrintData       *data      )
{
    printf("paginate!\n");

    //Declarations
    GwPageInfo *page;
    GwPageInfo *prev_page;
    GList *iter;
    GtkTextIter end_bound;
    GtkTextIter start_bound;
    GtkTextView *view;
    GtkTextBuffer *buffer;

    //Initializations
    view = gw_searchwindow_get_current_textview (data->window);
    buffer = gtk_text_view_get_buffer (view);

    //Get the draw bounds
    if (gtk_text_buffer_get_has_selection (buffer))
    {
      gtk_text_buffer_get_selection_bounds (buffer, &start_bound, &end_bound);
    }
    else
    {
      gtk_text_buffer_get_start_iter (buffer, &start_bound);
      gtk_text_buffer_get_end_iter (buffer, &end_bound);
    }

    //Create the first page
    if (data->pages == NULL)
    {
      page = gw_pageinfo_new (start_bound, end_bound);
      _draw_page_results (context, page, data);
      data->pages = g_list_append (data->pages, page);
      return FALSE;
    }

    iter = g_list_last (data->pages);
    prev_page = GW_PAGEINFO (iter->data);

    //Finish paginating
    if (gtk_text_iter_get_line (&(prev_page->end)) == gtk_text_iter_get_line (&end_bound))
    {
      gtk_print_operation_set_n_pages (operation, g_list_length (data->pages));
      return TRUE;
    }

    //Add another page
    page = gw_pageinfo_new (prev_page->end, end_bound);
    _draw_page_results (context, page, data);
    data->pages = g_list_append (data->pages, page);

    return FALSE;
}
Beispiel #3
0
static void _draw_page_results (GtkPrintContext *context, GwPageInfo *page, GwPrintData *data)
{
    //Declarations
    GtkTextView *view;
    GtkTextBuffer *buffer;
    PangoLayout *layout;
    char *text;
    PangoFontDescription *desc;
    int width;
    int height;
    gdouble drawable_width, drawable_height;
    cairo_t *cr;
    gint line_start;
    gint line_end;

    //Initializations
    view = gw_searchwindow_get_current_textview (data->window);
    buffer = gtk_text_view_get_buffer (view);
    text = gtk_text_buffer_get_text (buffer, &(page->start), &(page->end), FALSE);
    layout = gtk_print_context_create_pango_layout (context);
    desc = pango_font_description_from_string ("sans 10");
    drawable_width = gtk_print_context_get_width (context);
    drawable_height = gtk_print_context_get_height (context);
    cr = gtk_print_context_get_cairo_context (context);
    line_start = 0;
    line_end = 0;

    //Draw
    if (text != NULL)
    {
      cairo_move_to (cr, 5, 10);
      pango_layout_set_font_description (layout, desc);
      pango_layout_set_markup (layout, text, -1);
      pango_layout_set_width (layout, drawable_width * PANGO_SCALE);
      pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
      pango_layout_set_height (layout, drawable_height * PANGO_SCALE);
      pango_layout_get_size (layout, &width, &height);
      pango_cairo_show_layout (cr, layout);
      
      //Adjust the end GtkTextIter to the cutoff in the visible cairo context
      line_start = gtk_text_iter_get_line (&page->start);
      line_end = line_start + pango_layout_get_line_count (layout) - 1;
      gtk_text_buffer_get_iter_at_line (buffer, &(page->end), line_end);
    }

    //Cleanup
    if (text != NULL) g_free (text);
    if (layout != NULL) g_object_unref (layout);
    if (desc != NULL) pango_font_description_free (desc);
}
//!
//! @brief Sets up an initites a new search in a new tab
//!
//! @param widget Currently unused widget pointer
//! @param data A gpointer to a LwSearchItem that hold the search information
//!
static void _searchwindow_new_tab_with_search_cb (GtkMenuItem *widget, gpointer data)
{
    //Sanity check
    g_assert (data != NULL);

    //Declarations
    GwApplication *application;
    GwSearchWindow *window;
    GwSearchWindowPrivate *priv;
    LwPreferences *preferences;
    LwSearchItem *item;
    LwSearchItem *item_new;
    GtkTextView *view;
    GwSearchData *sdata;
    int index;

    //Initializations
    item = LW_SEARCHITEM (data);
    sdata = GW_SEARCHDATA (lw_searchitem_get_data (item));
    window = GW_SEARCHWINDOW (sdata->window);
    application = gw_window_get_application (GW_WINDOW (window));
    priv = window->priv;
    preferences = gw_application_get_preferences (application);
    item_new = lw_searchitem_new (item->queryline->string, item->dictionary, preferences, NULL);

    if (!gw_application_can_start_search (application)) return;

    if (item_new != NULL)
    {
      view = gw_searchwindow_get_current_textview (window);
      sdata = gw_searchdata_new (view, window);
      lw_searchitem_set_data (item_new, sdata, LW_SEARCHITEM_DATA_FREE_FUNC (gw_searchdata_free));

      index = gw_searchwindow_new_tab (window);
      gtk_notebook_set_current_page (priv->notebook, index);
      gw_searchwindow_start_search (window, item_new);
    }
}
//!
//! @brief Populates the main contextual menu with search options
//!
//! @param view The GtkTexView that was right clicked
//! @param menu The Popup menu to populate
//! @param data  Currently unused gpointer containing user data
//!
static GtkWidget* _resultpopup_new (GwResultPopupData *rpdata)
{
    if (rpdata == NULL) return NULL;

    //Declarations
    GwSearchWindow *window;
    GwApplication *application;
    LwDictInfoList *dictinfolist;
    LwPreferences *preferences;
    LwSearchItem *item;
    GwSearchData *sdata;
    LwDictInfo *di;
    GtkWidget *popup;
    char *menu_text;
    GtkWidget *menuitem;
    int i;

    //Initializations
    window = rpdata->window;
    application = gw_window_get_application (GW_WINDOW (window));
    dictinfolist = LW_DICTINFOLIST (gw_application_get_dictinfolist (application));
    preferences = gw_application_get_preferences (application);
    popup = gtk_menu_new ();
    g_signal_connect_after (G_OBJECT (popup), "hide", G_CALLBACK (_resultpopup_hide_cb), rpdata->button);
    char *website_url_menuitems[] = {
      "Wikipedia", "http://www.wikipedia.org/wiki/%s", "wikipedia.png",
      "Goo.ne.jp", "http://dictionary.goo.ne.jp/srch/all/%s/m0u/", "goo.png",
      "Google.com", "http://www.google.com/search?q=%s", "google.png",
      NULL, NULL, NULL
    };

    //Add internal dictionary links
    i = 0;
    while ((di = lw_dictinfolist_get_dictinfo_by_load_position (dictinfolist, i)) != NULL)
    {
      if (di != NULL && (item = lw_searchitem_new (rpdata->text, di, preferences, NULL)) != NULL)
      {
        sdata = gw_searchdata_new (gw_searchwindow_get_current_textview (window), window);
        lw_searchitem_set_data (item, sdata, LW_SEARCHITEM_DATA_FREE_FUNC (gw_searchdata_free));
        menu_text = g_strdup_printf ("%s", di->longname);
        if (menu_text != NULL)
        {
          menuitem = GTK_WIDGET (gtk_image_menu_item_new_with_label (menu_text));
          g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (_searchwindow_new_tab_with_search_cb), item);
          g_signal_connect_swapped (G_OBJECT (menuitem), "destroy", G_CALLBACK (lw_searchitem_free), item);
          gtk_menu_shell_append (GTK_MENU_SHELL (popup), GTK_WIDGET (menuitem));
          gtk_widget_show (GTK_WIDGET (menuitem));
//          gtk_widget_show (GTK_WIDGET (menuimage));
          g_free (menu_text);
          menu_text = NULL;
        }
      }
      i++;
    }

    //Separator
    menuitem = gtk_separator_menu_item_new ();
    gtk_menu_shell_append (GTK_MENU_SHELL (popup), GTK_WIDGET (menuitem));
    gtk_widget_show (GTK_WIDGET (menuitem));

    //Add weblinks
    i = 0;
    di =  lw_dictinfolist_get_dictinfo_by_load_position (dictinfolist, 0);
    while (website_url_menuitems[i] != NULL)
    {
      if (di != NULL && (item = lw_searchitem_new (rpdata->text, di, preferences, NULL)) != NULL)
      {
        //Create handy variables
        sdata = gw_searchdata_new (gw_searchwindow_get_current_textview (window), window);
        lw_searchitem_set_data (item, sdata, LW_SEARCHITEM_DATA_FREE_FUNC (gw_searchdata_free));
        char *name = website_url_menuitems[i];
        char *url = g_strdup_printf(website_url_menuitems[i + 1], rpdata->text);
        if (url != NULL)
        {
          g_free (item->queryline->string);
          item->queryline->string = g_strdup (url);
          g_free (url);
          url = NULL;
        }
        char *icon_path = website_url_menuitems[i + 2];

        //Start creating
        menu_text = g_strdup_printf ("%s", name);
        if (menu_text != NULL)
        {
          menuitem = GTK_WIDGET (gtk_image_menu_item_new_with_label (menu_text));
          char *path = g_build_filename (DATADIR2, PACKAGE, icon_path, NULL);
          if (path != NULL)
          {
//            menuimage = gtk_image_new_from_file (path);
//            gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), GTK_WIDGET (menuimage));
            g_free (path);
            path = NULL;
          }
          g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (_resultpopup_search_online_cb), item);
          g_signal_connect_swapped (G_OBJECT (menuitem), "destroy", G_CALLBACK (lw_searchitem_free), item);
          gtk_menu_shell_append (GTK_MENU_SHELL (popup), GTK_WIDGET (menuitem));
          gtk_widget_show (GTK_WIDGET (menuitem));
//          gtk_widget_show (GTK_WIDGET (menuimage));
          g_free (menu_text);
          menu_text = NULL;
        }
      }
      i += 3;
    }

    return popup;
}