making selectable the text of a GtkStatusbar - c

I have a statusbar, and I want to have its text being selectable (e.g. copy and pastable), on Linux/Debian, with GTK3 (3.21.5 on Debian/Sid). The label of the statusbar is the label field of its private data, and there is no direct API to access it.
I was able to code (in C99) the following, which is working:
mom_cmdstatusbar = // some global variable
gtk_statusbar_new ();
{
GtkWidget *statmsgarea =
gtk_statusbar_get_message_area (GTK_STATUSBAR (mom_cmdstatusbar));
MOM_ASSERTPRINTF (GTK_IS_CONTAINER (statmsgarea), "bad statmsgarea#%p",
statmsgarea);
GList *lischs = gtk_container_get_children (GTK_CONTAINER (statmsgarea));
for (GList * l = lischs; l != NULL; l = l->next)
{
GtkWidget *chwidg = l->data;
MOM_ASSERTPRINTF (GTK_IS_WIDGET (chwidg), "bad chwidg#%p", chwidg);
if (GTK_IS_LABEL (chwidg))
gtk_label_set_selectable (GTK_LABEL(chwidg), true);
}
g_list_free (lischs), lischs = NULL;
}
This does work, but is there a better or simpler way to do that? Getting the list of children inside the message area of the GtkStatusbar and testing each of them smells bad. It looks like some GtkWidget* gtk_statusbar_get_label (GtkStatusbar*); function is missing in in the GTK3 API.
PS. FWIW, the code is GPLv3+, on github in file gui.c on commit 58feb1d9473c34aca.. of the expjs branch. More details & motivation about that software project on this & that questions.

If there's no direct API to access the internal child, then that is probably on purpose, so that the GTK developers can keep their options open to reorganize the internal layout of the widget in future versions.

Related

Standard icons set in C with Win32

I am looking for the normal way (that is, the way Microsoft thought about when creating its API) to load standard icons for commands in a pure C program, like the floppy disc image for saving, etc.
The Internet doesn't seem to contain the answer. I know the toolbars of commctl32 can receive a TB_LOADIMAGES message that fills an image list with standard icons, but what if I don't need toolbars in my app? I want icons in another control, let's say on an owner-draw button, or a menu item. Is there a way to fill the image list without creating a useless toolbar? Or even better, getting HICON or HBITMAP handles to avoid the commctl32 dependency?
The loud silence about it in Microsoft's docs, and on the web, makes me think there is no such feature. Maybe when an application is developed with Microsoft tools, the application creator injects as resources in the executable files the default icons the developer chooses in a predefined list? I cannot check it because I only use Unix-like compilers, can somebody confirm that and/or tell me more about default icon usage under Windows?
Also, would it be reliable to assume we can load libraries like shell32.dll to use its embedded icons (like all the good old functions that are available since an eternity in kernel32.dll, `user32.dll, ...)? I mean, is there a guarantee that icons in libraries will keep their semantics and indexes in future versions of Windows?
Coming some days later to post the final solution I opted for, just in case somebody would need to use the same. The most normal way to show command icons is to embed them in the application, so despite being unhappy with the creation of a dummy control, I use a temporary toolbar to load the icons in global image lists. This would unlikely fail, but since icons may sometimes be the only thing that indicates to the user what a widget does, I reinforced the conditions with the ability to run a fallback code when something is wrong.
The function that loads the icons is named WIN32_initIcons(), because it is supposed to be implemented alongside with something like a GTK_initIcons() or X11_initIcons() counterpart for Unix-like platforms.
HIMAGELIST WIN32_imglst16 = NULL;
HIMAGELIST WIN32_imglst24 = NULL;
bool WIN32_initIcons(void) {
bool def16 = false;
bool def32 = false;
HWND hWnd;
WIN32_imglst16 = ImageList_Create(16, 16, ILC_COLOR32 | ILC_MASK, 80, 16);
WIN32_imglst32 = ImageList_Create(24, 24, ILC_COLOR32 | ILC_MASK, 80, 16);
if (hWnd = CreateWindowA("ToolbarWindow32", NULL, TBSTYLE_LIST, 0, 0, 0, 0, NULL, (HMENU)0, GetModuleHandle(NULL), NULL)) {
if (WIN32_imglst16) {
SendMessage(hWnd, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)WIN32_imglst16);
if (!SendMessage(hWnd, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL)) def16 = true;
if (SendMessage(hWnd, TB_LOADIMAGES, (WPARAM)IDB_VIEW_SMALL_COLOR, (LPARAM)HINST_COMMCTRL) != 15) def16 = false;
if (SendMessage(hWnd, TB_LOADIMAGES, (WPARAM)IDB_HIST_SMALL_COLOR, (LPARAM)HINST_COMMCTRL) != 27) def16 = false;
if (ImageList_GetImageCount(WIN32_imglst16) != 32) def16 = false;
}
if (WIN32_imglst24) {
SendMessage(hWnd, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)WIN32_imglst24);
if (!SendMessage(hWnd, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL)) def24 = true;
if (SendMessage(hWnd, TB_LOADIMAGES, (WPARAM)IDB_VIEW_SMALL_COLOR, (LPARAM)HINST_COMMCTRL) != 15) def24 = false;
if (SendMessage(hWnd, TB_LOADIMAGES, (WPARAM)IDB_HIST_SMALL_COLOR, (LPARAM)HINST_COMMCTRL) != 27) def24 = false;
if (ImageList_GetImageCount(WIN32_imglst24) != 32) def24 = false;
}
DestroyWindow(hWnd);
}
if (WIN32_imglst16 && !def16) {
// Load your custom 16x16 fallback icons in the list "WIN32_imglst16" here
// Don't forget to set "def16 = true" on success
}
if (WIN32_imglst24 && !def24) {
// Load your custom 24x24 fallback icons in the list "WIN32_imglst24" here
// Don't forget to set "def24 = true" on success
}
if (!def16) {
// Trigger your error management here because 16x16 icons cannot be loaded
// Maybe, call "ImageList_Destroy(WIN32_imglst16)" and set "WIN32_imglst16 = NULL"
}
if (!def24) {
// Trigger your error management here because 24x24 icons cannot be loaded
// Maybe, call "ImageList_Destroy(WIN32_imglst24)" and set "WIN32_imglst24 = NULL"
}
return (def16 && def24);
}
Note: In simple C, use <stdbool.h> to define the bool value.

How to change the cursor to a watch in GTK for C?

I'm using the C-style GTK functions in C++ and I can't figure out how to set the cursor for the main window.
Use gdk_set_cursor()
(https://developer.gnome.org/gdk3/stable/gdk3-Windows.html#gdk-window-set-cursor)
on a GdkCursor created by gdk_cursor_new()
(https://developer.gnome.org/gdk3/stable/gdk3-Cursors.html)
Use gdk_window_set_cursor() on a GdkCursor created by gdk_cursor_new_from_name().
To get the GdkWindow of a GtkWindow you can use gtk_widget_get_window() because GtkWindow is a subclass of GtkWidget.
Note: this answer is an improvement over idefixs answer (important correction + link update + minor modification to make the answer complete + improved formatting) that was rejected as an edit.
Posting this because the comment about getting the GdkWndow went unanswered so far.
For most widgets, the GdkWindow can be retrieved as the window data field of the GtkWidget structure. The following code sets a cursor on a GtkWindow widget:
GtkWidget* win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
GdkCursor* watchCursor = gdk_cursor_new(GDK_WATCH);
/* set watch cursor */
gdk_window_set_cursor(win->window, watchCursor);
/* return to normal */
gdk_window_set_cursor(win->window, NULL);
If you need to get to the GtkWindow ancestor from one of its child widgets, you can use:
GtkWidget* win = gtk_widget_get_ancestor(someWidget, GTK_TYPE_WINDOW);
My solution:
void gtkSetCursor(GdkCursorType cursorType) {
GdkScreen * screen = gdk_screen_get_default();
GdkWindow * win = gdk_screen_get_root_window(screen);
GdkCursor * cursor = gdk_cursor_new(cursorType); //http://developer.gimp.org/api/2.0/gdk/gdk-Cursors.html
gdk_window_set_cursor(win, cursor);
while (gtk_events_pending()) gtk_main_iteration();
}
...
gtkSetCursor(GDK_WATCH);
start your stuff here
...
end of your stuff
gtkSetCursor(GDK_LEFT_PTR);
jeff's answer didn't work for me (Gtk3). So here's my solution:
GdkWindow* win = gtk_widget_get_parent_window(widget);
GdkCursor* watchCursor = gdk_cursor_new(GDK_WATCH);
gdk_window_set_cursor(win, watchCursor);

Remove GTK+ container children, repopulate it, then refresh

I'm into a problem with a GTK+ C application. I have a container that, when starting the application, contains a button. During the running an user interation must cause this widget to contain more of them.
I need to write a function that removes all the "old" inner buttons, then adds all the ones from a list and finally refresh the view. This is what I'm writing but some parts are missing (TODOs)
void refresh_sequence_panel()
{
GSList* iterator = NULL;
GtkWidget* button;
// TODO: Here the container must be empty
// Now add all the buttons
for (iterator = steps; iterator; iterator = iterator->next) {
button = gtk_button_new_from_stock(GTK_STOCK_ADD);
gtk_widget_set_size_request(button, SEQ_BUTTON_W, SEQ_BUTTON_H);
gtk_box_pack_start(GTK_BOX(sequence_panel), button, FALSE, FALSE, 5);
handler_id = g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(seq_popup), GTK_BOX(sequence_panel));
}
// TODO: Now refresh the view, so I can see the changes...
}
Hope that someone can help, thanks!
Removing all children:
GList *children, *iter;
children = gtk_container_get_children(GTK_CONTAINER(container));
for(iter = children; iter != NULL; iter = g_list_next(iter))
gtk_widget_destroy(GTK_WIDGET(iter->data));
g_list_free(children);
Note that the above just deletes each child widget directly, rather than asking the container to remove it (with gtk_container_remove()), this is recommended by the documentation and matches what you intend, so it's fine in my opinion.
There's no point in "refreshing the view", as long as you actually add and show the newly built widgets to the container. GTK+ is event-based, and adding children to a container makes the container realize it needs to refresh its visual appearance automatically.
One-liner:
gtk_container_foreach (GTK_CONTAINER (container), (void*) gtk_widget_destroy, NULL);
Here is the method that I followed. Because I am using gtkmm on c++
Gtk::Box_Helpers::BoxList *childList = &vboxImgLst->children();
Box_Helpers::BoxList::iterator start = childList->begin();
Box_Helpers::BoxList::iterator end = childList->end();
childList->erase(start, end);
where vboxImgLst is,
VBox *vboxImgLst;
Hope this will help to someone who are using gtkmm and c++.
Thanks
This one worked for me (it's a variation of unwind's answer):
Glib::ListHandle<Widget*> childList = this->get_children();
Glib::ListHandle<Widget*>::iterator it = childList.begin();
while (it != childList.end()) {
remove(*(*it));
it++;
}
(GTKMM 2.4)

How to replace GDK_INCLUDE_INFERIORS?

Since most of the gdk_gc_* methods are deprecated in GTK+ 3, how does one paint on sub-windows using Cairo only? Below is a simple example of what I would like to achieve:
GdkWindow *tl_window = parent_widget->window;
GdkGC *gc = gdk_gc_new(tl_window1->window);
gdk_gc_set_subwindow(gc, GDK_INCLUDE_INFERIORS);
gdk_gc_set_exposures(gc, FALSE);
do_my_painting(tl_window, gc);
gdk_gc_unref(gc);
http://developer.gnome.org/gdk/stable/gdk-Graphics-Contexts.html#gdk-gc-set-subwindow says GDK_INCLUDE_INFERIORS will be used on sources. So I assume the trick is to create a source of the window and then ... well then ... I'm stuck.
Found a solution myself
GtkWidget *tl_window = gtk_widget_get_toplevel(widget);
cairo_t *cairo = gdk_cairo_create(gtk_widget_get_window(tl_window));
cairo_surface_t *source_surface = cairo_get_target(cairo);
cairo_t *cairo_new = cairo_create(source_surface);
cairo_set_source_surface(cairo, cairo_get_target(cairo_new), 0, 0);
do_my_painting(cairo_new);
cairo_paint(cairo);
cairo_destroy(cairo);
(do I mis a destroy of cairo_new here ?)
I also found the next link (did not test it) http://mail.gnome.org/archives/commits-list/2010-August/msg03385.html where they seem to use a pixmap instead of a surface.

GtkEntryCompletion without user input?

Hopefully this is an easy question to answer! I am trying to use GtkEntryCompletion (a la the example here) but while this code works I can't seem to get the GtkEntry to present the autocomplete results when I set the text of the field programatically. What I am trying to accomplish is a semi pre-filled text entry that is already presenting the user with some autocomplete options.
To set the text I have tried using the functions gtk_entry_set_text(...), gtk_entry_buffer_insert_text(...) and even gtk_entry_buffer_emit_inserted_text(...) but to no avail. Is there a way to do this in such a way as to act like regular user input and display the suggestions?
I think you need to call gtk_entry_completion_complete after setting the text.
EDIT
Sorry #Tylter, but wow, this is way more difficult than I imagined. The only way I can figure out how to do it is to actually send the keypress event to the window.
gtk_widget_grab_focus(entry);
GdkEvent new_event;
new_event.key.type = GDK_KEY_PRESS;
new_event.key.window = gtk_widget_get_parent_window(entry);
new_event.key.send_event = TRUE;
new_event.key.time = GDK_CURRENT_TIME;
new_event.key.keyval = 0x053; // capital S
new_event.key.state = GDK_KEY_PRESS_MASK;
new_event.key.length = 0;
new_event.key.string = 0;
new_event.key.hardware_keycode = 0;
new_event.key.group = 0;
gdk_event_put((gpointer)&new_event);
EDIT 2
Are you using a GtkDialog for your pop-up? I coded this up really quick and it seems to work. Here you would be creating the dialog in a button click event:
static void click_event( GtkWidget *widget,
gpointer data )
{
GtkWidget* window = gtk_dialog_new ();
completion = create_completion();
entry = gtk_entry_new();
gtk_entry_set_completion(GTK_ENTRY(entry), completion);
// add entry to dialog
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area),
entry, TRUE, TRUE, 0);
gtk_widget_show(entry);
gtk_widget_show(window); // you must show the window before sending the keypress event
gtk_widget_grab_focus(entry);
GdkEvent new_event;
new_event.key.type = GDK_KEY_PRESS;
new_event.key.window = gtk_widget_get_parent_window(entry);
new_event.key.send_event = TRUE;
new_event.key.time = GDK_CURRENT_TIME;
new_event.key.keyval = 0x053; // capital S
new_event.key.state = GDK_KEY_PRESS_MASK;
new_event.key.length = 0;
new_event.key.string = 0;
new_event.key.hardware_keycode = 0;
new_event.key.group = 0;
gdk_event_put((gpointer)&new_event);
}
The only gotcha I saw with this is that your must show the dialog window before sending the keypress event.

Resources