Ejemplo n.º 1
0
gint a_uibuilder_properties_factory ( const gchar *dialog_name, GtkWindow *parent, VikLayerParam *params,
				      guint16 params_count, gchar **groups, guint8 groups_count,
				      gboolean (*setparam) (gpointer,guint16,VikLayerParamData,gpointer,gboolean),
				      gpointer pass_along1, gpointer pass_along2,
				      VikLayerParamData (*getparam) (gpointer,guint16,gboolean),
				      gpointer pass_along_getparam )
				      /* pass_along1 and pass_along2 are for set_param first and last params */
{
  guint16 i, j, widget_count = 0;
  gboolean must_redraw = FALSE;

  if ( ! params )
    return 1; /* no params == no options, so all is good */

  for ( i = 0; i < params_count; i++ )
    if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
      widget_count++;

  if ( widget_count == 0)
    return 0; /* TODO -- should be one? */
  else
  {
    /* create widgets and titles; place in table */
    GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
						      parent,
						      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
						      GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
						      GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
    gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
    GtkWidget *response_w = NULL;
#if GTK_CHECK_VERSION (2, 20, 0)
    response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
#endif
    gint resp;

    GtkWidget *table = NULL;
    GtkWidget **tables = NULL; /* for more than one group */

    GtkWidget *notebook = NULL;
    GtkWidget **widgets = g_malloc ( sizeof(GtkWidget *) * widget_count );

    if ( groups && groups_count > 1 )
    {
      guint8 current_group;
      guint16 tab_widget_count;
      notebook = gtk_notebook_new ();
      // Switch to vertical notebook mode when many groups
      if ( groups_count > 4 )
        gtk_notebook_set_tab_pos ( GTK_NOTEBOOK(notebook), GTK_POS_LEFT );
      gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), notebook, FALSE, FALSE, 0);
      tables = g_malloc ( sizeof(GtkWidget *) * groups_count );
      for ( current_group = 0; current_group < groups_count; current_group++ )
      {
        tab_widget_count = 0;
        for ( j = 0; j < params_count; j ++ )
          if ( params[j].group == current_group )
            tab_widget_count++;

        if ( tab_widget_count )
        {
          tables[current_group] = gtk_table_new ( tab_widget_count, 1, FALSE );
          gtk_notebook_append_page ( GTK_NOTEBOOK(notebook), tables[current_group], gtk_label_new(groups[current_group]) );
        }
      }
    }
    else
    {
      table = gtk_table_new( widget_count, 1, FALSE );
      gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, FALSE, FALSE, 0);
    }

    for ( i = 0, j = 0; i < params_count; i++ )
    {
      if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
      {
        if ( tables )
          table = tables[MAX(0, params[i].group)]; /* round up NOT_IN_GROUP, that's not reasonable here */

        widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );

        if ( widgets[j] ) {
          gtk_table_attach ( GTK_TABLE(table), gtk_label_new(_(params[i].title)), 0, 1, j, j+1, 0, 0, 0, 0 );
          gtk_table_attach ( GTK_TABLE(table), widgets[j], 1, 2, j, j+1, GTK_EXPAND | GTK_FILL, 0, 2, 2 );
        }
        j++;
      }
    }

    if ( response_w )
      gtk_widget_grab_focus ( response_w );

    gtk_widget_show_all ( dialog );

    resp = gtk_dialog_run (GTK_DIALOG (dialog));
    if ( resp == GTK_RESPONSE_ACCEPT )
    {
      for ( i = 0, j = 0; i < params_count; i++ )
      {
        if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
        {
          if ( setparam ( pass_along1,
			  i,
			  a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
			  pass_along2,
			  FALSE ) )
            must_redraw = TRUE;
          j++;
        }
      }

      gtk_widget_destroy ( dialog ); /* hide before redrawing. */
      g_free ( widgets );
      if ( tables )
        g_free ( tables );

      return must_redraw ? 2 : 3; /* user clicked OK */
    }

    if ( tables )
      g_free ( tables );
    gtk_widget_destroy ( dialog );
    g_free ( widgets );
    return 0;
  }
}
Ejemplo n.º 2
0
//static void draw_to_image_file_total_area_cb (GtkSpinButton *spinbutton, gpointer *pass_along)
gint a_uibuilder_properties_factory ( const gchar *dialog_name,
                                      GtkWindow *parent,
                                      VikLayerParam *params,
                                      guint16 params_count,
                                      gchar **groups,
                                      guint8 groups_count,
                                      gboolean (*setparam) (gpointer,guint16,VikLayerParamData,gpointer,gboolean),
                                      gpointer pass_along1,
                                      gpointer pass_along2,
                                      VikLayerParamData (*getparam) (gpointer,guint16,gboolean),
                                      gpointer pass_along_getparam,
                                      void (*changeparam) (GtkWidget*, ui_change_values) )
                                      /* pass_along1 and pass_along2 are for set_param first and last params */
{
  guint16 i, j, widget_count = 0;
  gboolean must_redraw = FALSE;

  if ( ! params )
    return 1; /* no params == no options, so all is good */

  for ( i = 0; i < params_count; i++ )
    if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
      widget_count++;

  if ( widget_count == 0)
    return 0; /* TODO -- should be one? */
  else
  {
    /* create widgets and titles; place in table */
    GtkWidget *dialog = gtk_dialog_new_with_buttons ( dialog_name,
						      parent,
						      GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
						      GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
						      GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL );
    gtk_dialog_set_default_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
    GtkWidget *response_w = NULL;
#if GTK_CHECK_VERSION (2, 20, 0)
    response_w = gtk_dialog_get_widget_for_response ( GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT );
#endif
    gint resp;

    GtkWidget *table = NULL;
    GtkWidget **tables = NULL; /* for more than one group */

    GtkWidget *notebook = NULL;
    GtkWidget **labels = g_malloc ( sizeof(GtkWidget *) * widget_count );
    GtkWidget **widgets = g_malloc ( sizeof(GtkWidget *) * widget_count );
    ui_change_values *change_values = g_malloc ( sizeof(ui_change_values) * widget_count );

    if ( groups && groups_count > 1 )
    {
      guint8 current_group;
      guint16 tab_widget_count;
      notebook = gtk_notebook_new ();
      // Switch to vertical notebook mode when many groups
      if ( groups_count > 4 )
        gtk_notebook_set_tab_pos ( GTK_NOTEBOOK(notebook), GTK_POS_LEFT );
      gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), notebook, FALSE, FALSE, 0);
      tables = g_malloc ( sizeof(GtkWidget *) * groups_count );
      for ( current_group = 0; current_group < groups_count; current_group++ )
      {
        tab_widget_count = 0;
        for ( j = 0; j < params_count; j ++ )
          if ( params[j].group == current_group )
            tab_widget_count++;

        if ( tab_widget_count )
        {
          tables[current_group] = gtk_table_new ( tab_widget_count, 1, FALSE );
          gtk_notebook_append_page ( GTK_NOTEBOOK(notebook), tables[current_group], gtk_label_new(groups[current_group]) );
        }
      }
    }
    else
    {
      table = gtk_table_new( widget_count, 1, FALSE );
      gtk_box_pack_start (GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), table, FALSE, FALSE, 0);
    }

    for ( i = 0, j = 0; i < params_count; i++ )
    {
      if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
      {
        if ( tables )
          table = tables[MAX(0, params[i].group)]; /* round up NOT_IN_GROUP, that's not reasonable here */

        widgets[j] = a_uibuilder_new_widget ( &(params[i]), getparam ( pass_along_getparam, i, FALSE ) );

        if ( widgets[j] ) {
          labels[j] = gtk_label_new(_(params[i].title));
          gtk_table_attach ( GTK_TABLE(table), labels[j], 0, 1, j, j+1, 0, 0, 0, 0 );
          gtk_table_attach ( GTK_TABLE(table), widgets[j], 1, 2, j, j+1, GTK_EXPAND | GTK_FILL, 0, 2, 2 );

          if ( changeparam )
          {
            change_values[j][UI_CHG_LAYER] = pass_along1;
            change_values[j][UI_CHG_PARAM] = &params[i];
            change_values[j][UI_CHG_PARAM_ID] = GINT_TO_POINTER((gint)i);
            change_values[j][UI_CHG_WIDGETS] = widgets;
            change_values[j][UI_CHG_LABELS] = labels;

            switch ( params[i].widget_type )
            {
              // Change conditions for other widget types can be added when needed
              case VIK_LAYER_WIDGET_COMBOBOX:
                g_signal_connect ( G_OBJECT(widgets[j]), "changed", G_CALLBACK(changeparam), change_values[j] );
                break;
              case VIK_LAYER_WIDGET_CHECKBUTTON:
                g_signal_connect ( G_OBJECT(widgets[j]), "toggled", G_CALLBACK(changeparam), change_values[j] );
                break;
              default: break;
            }
          }
        }
        j++;
      }
    }

    // Repeat run through to force changeparam callbacks now that the widgets have been created
    // This primarily so the widget sensitivities get set up
    if ( changeparam ) {
      for ( i = 0, j = 0; i < params_count; i++ ) {
        if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES ) {
          if ( widgets[j] ) {
            changeparam ( widgets[j], change_values[j] );
          }
          j++;
        }
      }
    }

    if ( response_w )
      gtk_widget_grab_focus ( response_w );

    gtk_widget_show_all ( dialog );

    resp = gtk_dialog_run (GTK_DIALOG (dialog));
    if ( resp == GTK_RESPONSE_ACCEPT )
    {
      for ( i = 0, j = 0; i < params_count; i++ )
      {
        if ( params[i].group != VIK_LAYER_NOT_IN_PROPERTIES )
        {
          if ( setparam ( pass_along1,
			  i,
			  a_uibuilder_widget_get_value ( widgets[j], &(params[i]) ),
			  pass_along2,
			  FALSE ) )
            must_redraw = TRUE;
          j++;
        }
      }

      g_free ( widgets );
      g_free ( labels );
      g_free ( change_values );
      if ( tables )
        g_free ( tables );
      gtk_widget_destroy ( dialog ); /* hide before redrawing. */

      return must_redraw ? 2 : 3; /* user clicked OK */
    }

    g_free ( widgets );
    g_free ( labels );
    g_free ( change_values );
    if ( tables )
      g_free ( tables );
    gtk_widget_destroy ( dialog );

    return 0;
  }
}