How to insert dialog in submenu with GTK+ C++? - gtk

I'm new in GTK+ (c++).I use this version of gtk: gtk+ bundle_3.4.2-20130513_win64 and I designed a menu (Help) with two sup menus: OpenCV and GTK.
I want to insert dialog for any of them in sub menu with GTK+ C++. Similar to this:
In fact my final result is:
But I can not. What ideas on how to solve this task would you suggest? Or on what resource on the internet can I find help?
this is my c++ code:
GtkWidget *vbox2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, FALSE);
gtk_container_set_border_width(GTK_CONTAINER(win), 10);
gtk_table_attach_defaults(GTK_TABLE(table), vbox2, 1, 2, 0, 1);
GtkWidget *menubar2 = gtk_menu_bar_new();
GtkWidget *helpMenu = gtk_menu_new();
GtkAccelGroup *accel_group2 = NULL;
accel_group2 = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(win), accel_group2);
GtkWidget *helpMi = gtk_menu_item_new_with_mnemonic("_Help");
//opencvMi
GtkWidget *imageOpenCV, *opencvMi;
imageOpenCV = gtk_image_new_from_file("E:/Works for Gov Project/DOC /GUI/logogui1/opencv.png");
opencvMi = gtk_image_menu_item_new_with_mnemonic("OpenCV");
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(opencvMi), GTK_WIDGET(imageOpenCV));
gtk_widget_show(opencvMi);
GtkWidget *linkbutton_opencv = gtk_menu_item_new_with_label("http://www.opencv.org");
//gtkMi
GtkWidget *imageGTK,*gtkMi;
imageGTK = gtk_image_new_from_file("E:/Works for Gov Project/DOC /GUI/logogui1/gtk.png");
gtkMi = gtk_image_menu_item_new_with_mnemonic("GTK+");
gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(gtkMi), GTK_WIDGET(imageGTK));
gtk_widget_show(gtkMi);
GtkWidget *linkbutton_gtk = gtk_menu_item_new_with_label("http://www.gtk.org/");
GtkWidget *imprMenu2 = gtk_menu_new();
GtkWidget *imprMenu3 = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(helpMi), helpMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(helpMenu), opencvMi);
gtk_menu_shell_append(GTK_MENU_SHELL(helpMenu), gtkMi);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar2), helpMi);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(opencvMi), imprMenu2);
gtk_menu_shell_append(GTK_MENU_SHELL(imprMenu2), linkbutton_opencv);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(gtkMi), imprMenu3);
gtk_menu_shell_append(GTK_MENU_SHELL(imprMenu3), linkbutton_gtk);
gtk_box_pack_start(GTK_BOX(vbox2), menubar2, FALSE, FALSE, 0);
and this is code for dialog (GtkAboutDialog):
http://zetcode.com/gui/gtk2/gtkdialogs/

Related

How to apply style using GtkSourceView library

I'm trying to style a GtkSourceView object in particular I'd like to change the color of line numbers. I'm reading Style Scheme Definition Reference: GtkSourceView 4 Reference Manual. I'm able to change text but not line-numbers. The document says:
line-numbers : Text and background colors for the left margin, on which line numbers are drawn.
This is the source:
#include <gtk/gtk.h>
#include <gtksourceview/gtksource.h>
int main (int argc,
char *argv[])
{
GtkWidget *window, *scrolled_win, *textview;
GtkWidget *vbox, *menubar, *fileMenu, *fileMi, *quit_item;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_maximize(GTK_WINDOW (window));
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 12);
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_window_set_title (GTK_WINDOW (window), "Text Views");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
textview = gtk_source_view_new();
menubar = gtk_menu_bar_new();
fileMenu = gtk_menu_new();
fileMi = gtk_menu_item_new_with_label("File");
quit_item = gtk_menu_item_new_with_label("Quit");
gtk_widget_set_hexpand(textview, TRUE);
gtk_widget_set_vexpand(textview, TRUE);
GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_path (provider,
"gtk-widgets.css", NULL);
GtkStyleContext *context;
context = gtk_widget_get_style_context(textview);
gtk_style_context_add_provider (context,
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileMi), fileMenu);
gtk_menu_shell_append(GTK_MENU_SHELL(fileMenu), quit_item);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar), fileMi);
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add(GTK_CONTAINER(vbox), scrolled_win);
gtk_container_add (GTK_CONTAINER (scrolled_win), textview);
gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW(textview), TRUE);
gtk_widget_show_all (window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(quit_item), "activate",
G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return 0;
}
This is the CSS file:
text {
background-color: #002b36;
color: #c7a21d;
}
line-numbers {
background-color: red;
color:red;
}
current-line-number {
background-color: red;
color:red;
}
selection{
color:yellow;
}
What am I doing wrong?
The API has a way to access the default styles like this:
manager = GtkSource.StyleSchemeManager().get_default()
print (manager.get_scheme_ids())
>['classic', 'cobalt', 'kate', 'oblivion', 'solarized-dark', 'solarized-light', 'tango']
scheme = manager.get_scheme("oblivion")
buffer.set_style_scheme(scheme)
An example for good measure:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
from gi.repository import GtkSource as gtksource
from gi.repository import Pango
view = gtksource.View()
view.modify_font(Pango.FontDescription('monospace 11'))
view.set_show_line_numbers(True)
view.set_show_line_marks(True)
buffer = view.get_buffer()
filename = "treeview.py"
txt = open(filename).read()
buffer.set_text(txt)
# set language, syntax highlighting
lm = gtksource.LanguageManager.new()
lang = lm.guess_language(filename)
buffer.set_highlight_syntax(True)
buffer.set_language(lang)
buffer.create_tag("invisible",invisible=True)
manager = gtksource.StyleSchemeManager().get_default()
scheme = manager.get_scheme("monokai")
buffer.set_style_scheme(scheme)
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
window.add(view)
window.show_all()
gtk.main()
From there you should be able use the guide to apply custom themes, change colours of certain elements, etc...

GTK3 - Catch error when GdkDisplay is unavailable

Given the following code:
#include <gtk/gtk.h>
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GdkDisplay *display = gdk_display_open(":2");
gtk_main();
return(0);
}
How can you catch the error, when the X server for display :2 is no longer running?
The exact error is:
Fatal IO error 11 (Resource temporarily unavailable) on X server :2.

GetPrivateProfileString failed when I use gflags to enable page heap with flags(full unaligned traces)

My code is like this:
#include <Windows.h>
#include <tchar.h>
int main () {
TCHAR conffigPath[MAX_PATH] = {0};
GetCurrentDirectory(MAX_PATH, conffigPath);
_tcscat_s(conffigPath, MAX_PATH, _T("\\config.ini"));
TCHAR szValue[MAX_PATH] = {0};
GetPrivateProfileString(_T("361"), _T("DLL"), NULL, szValue, MAX_PATH, conffigPath);
return 0;
}
The context of config.ini is following:
[361]
Dll=daemon.dll
But when GetPrivateProfileString was called, the context of szValue is empty! And when I disable the pageheap, the context of szValue is "daemon.dll".
And when I enable pageheap with flags(full traces),the context of szVaule is "daemon.dll".
Is there a bug of the API GetPrivateProfileString?

process flex+bison output in gtk hash table and list store

this question follows the discussion of flex+bison output in a glib's hash container
Let me repost it (last post remained unanswered after some discussions.)
I want to parse a bibtex file using flex and bison, and will display those data using gtk library(in C).
The lexer is
%{
#include "bib.tab.h"
%}
%%
[A-Za-z][A-Za-z0-9]* { yylval.sval = strdup(yytext); return KEY; }
\"([^\"]|\\.)*\"|\{([^\"]|\\.)*\} { yylval.sval = strdup(yytext); return VALUE; }
[ \t\n] ; /* ignore whitespace */
[{}#=,] { return *yytext; }
. { fprintf(stderr, "Unrecognized character %c in input\n", *yytext); }
%%
and the parser is:
%{
#include <stdio.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <string.h>
#include <glib/gstdio.h>
#include <fcntl.h>
enum
{
COL_BIB_KEY=0,
COL_BIB_TYPE, COL_BIB_AUTHOR, COL_BIB_YEAR,
NUM_COLS} ;
#define slen 1024
GHashTable* table;
GtkTreeIter siter;
GtkListStore *store;
%}
// Symbols.
%union
{
char *sval;
};
%token <sval> VALUE
%token <sval> KEY
%token OBRACE
%token EBRACE
%token QUOTE
%token SEMICOLON
%start Input
%%
Input:
/* empty */
| Input Entry ; /* input is zero or more entires */
Entry:
'#' KEY '{' KEY ','{ g_hash_table_insert(table, g_strdup("TYPE"), g_strdup($2));
g_hash_table_insert(table, g_strdup("ID"), g_strdup($4));
g_printf("%s:%s\n","KEY=>",g_hash_table_lookup(table,"TYPE"));
// g_printf("%s: %s\n", $2, $4);
}
KeyVals '}'
;
KeyVals:
/* empty */
| KeyVals KeyVal ; /* zero or more keyvals */
KeyVal:
KEY '=' VALUE ',' { g_hash_table_insert(table, g_strdup($1), g_strdup($3));
// g_printf("%s: %s\n", $1, $3);
};
%%
int yyerror(char *s) {
printf("yyerror : %s\n",s);
}
int main(int argc, char** argv) {
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget *tree=gtk_tree_view_new();
setup_tree(tree);
gtk_container_add (GTK_CONTAINER (window), tree);
store= gtk_list_store_new (NUM_COLS,
G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
table = g_hash_table_new(g_str_hash, g_str_equal);
gint i;
do{
g_hash_table_remove_all (table);
yyparse();
parse_entry (table);
gtk_tree_view_set_model (GTK_TREE_VIEW (tree), GTK_TREE_MODEL (store));
g_object_unref (store);
}
while(!EOF);
g_hash_table_destroy (table);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void parse_entry (GHashTable *table)
{
GHashTableIter iter;
gchar *key, *val;
char *keys[] = {"id", "type", "author", "year", "title", "publisher", "editor",
"volume", "number", "pages", "month", "note", "address", "edition", "journal",
"series", "book", "chapter", "organization", NULL};
char *vals[] = {NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL};
gchar **kiter;
gint i;
g_hash_table_iter_init (&iter, table);
while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
{
for (kiter = keys, i = 0; *kiter; kiter++, i++)
{
if (!g_ascii_strcasecmp(*kiter, key))
{
vals[i] = g_strndup(val,slen);
// g_printf("%s:%s\n",keys[i],g_hash_table_lookup(table,keys[i]));
g_printf("%d=>%s:%s\n",i,keys[i],vals[i]);
break;
}
}
}
gtk_list_store_append (store, &siter);
gtk_list_store_set (store, &siter,
COL_BIB_TYPE, vals[COL_BIB_TYPE],
COL_BIB_KEY, vals[COL_BIB_KEY],
COL_BIB_AUTHOR, vals[COL_BIB_AUTHOR],
COL_BIB_YEAR, vals[COL_BIB_YEAR],
-1);
}
void setup_tree(GtkWidget *tree){
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Type", renderer, "text",COL_BIB_TYPE , NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Author", renderer, "text", COL_BIB_AUTHOR, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes
("Year", renderer, "text",COL_BIB_YEAR, NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
g_printf("HIIIIIIIIIi");
}
The problem is on populating the hash table, and not the listview(I enclosed the list store so that people can see my final goal and suggest improvements.)
If we put the line
g_printf("%s:%s\n",$1,g_hash_table_lookup(table,$1));
at line number 50, it prints the hash table's content correctly, but if we want the content by uncommenting line number 105, then only the last entry is parsed.
So, my guess is I am not processing the hash file correctly (line no 97-107 may be?)
The makefile is:
CC=gcc -g
FLEX=flex
BISON=bison
LIBS=lfl
PROG=parse
${PROG}:bib.y bib.l
${BISON} -d bib.y
${FLEX} -i bib.l
${CC} lex.yy.c bib.tab.c `pkg-config --cflags --libs glib-2.0``pkg-config --cflags --libs gtk+-3.0` -${LIBS} -o $#
clean:
rm -f lex.yy.c bib.tab.c ${PROG}
touch bib.l bib.y
and a sample bibtex file is:
#Book{a1,
Title="ASR",
Publisher="oxf",
author = "a {\"m}ook, Rudra Banerjee",
Year="2010",
Address="UK",
Edition="1",
}
#Booklet{ab19,
Author="Rudra Banerjee and A. Mookerjee",
Title="Fe{\"Ni}Mo",
Editor="sm1",
Title="sm2",
Publisher="sm3",
Volume="sm4",
Number="sm5",
Pages="sm6",
Month="sm8",
Note="sm9",
Key="sm10",
Year="1980",
Address="osm1",
Edition="osm2",
}
I will be grateful if someone shows me some way to populate the hashtable correctly.
Please help.
It looks like you're just dumping all the data into a single hash table. So the first entry will go into the hash table unders the keys TYPE, ID, Title, Publisher, etc. Then the second entry will overwrite those same keys and values (except it uses Author instead of author), leading to a mix of the two entries.
If you want to use a hashtable, I would expect you'd need a hash table for each entry, and a hastable of hashtables to map IDs to hashtables containing the info for that entry. Alternately, you could parse each entry into a list or some other container structure, and have a single hashtable mapping IDs to lists/containers.
You're also leaking memory very rapidly, as you allocate new memory for each KEY or VALUE and then duplicate the memory to put it into the hashtable.

iphone ios sample ffplay main.c --> FFmpeg.c change and input parameter , following errer please advice

FFmpeg main c source
int main(int argc, char **argv)
{
int flags, i;
argv[1] = "-i";
argv[2] = "ipod-library://item/item.mp3?id=1218551163727305907";
//argv[2] = "mon.mp3";
argv[3] = "-acodec";
argv[4] = "libfaac";
argv[5] = "-ac";
argv[6] = "2";
argv[7] = "-ar";
argv[8] = "44100";
argv[9] = "-ab";
argv[10] = "128k";
argv[11] = "output.aac";
argv[3] = "-vn";
argv[4] = "-acodec";
argv[5] = "libfaac";
argv[6] = "-ac";
argv[7] = "2";
argv[8] = "-ar";
argv[9] = "44100";
argv[10] = "-ab";
argv[11] = "128k";
argv[12] = "output.aac";
// argv[14] = "rtsp://video.bouygtel.fr/50400/tf1.sdp";
argc += 12;
error : ipod-library://item/item.mp3?id=1218551163727305907: No such file or directory
and if
argv[2] = "mon.mp3"; ---> mon.mp3: No such file or directory
please advice.
You have to specify the full path for the file. If it is in your resources, it would be something like: [[NSBundle mainBundle] pathForResource:#"mon" ofType:#"mp3"];
Regards,
Alexandre