GTK2 questions.

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
torm
Posts: 186
Joined: Sat 07 Mar 2015, 19:56

GTK2 questions.

#1 Post by torm »

GtkCalendar. Is it possible to "force" the focus element on current date?
If so, how?

User avatar
rufwoof
Posts: 3690
Joined: Mon 24 Feb 2014, 17:47

#2 Post by rufwoof »

I use yad calendar which you can set the active day, month and year i.e.

Code: Select all

GEO="--geometry 10x10+530+270"

CHOOSEFILE=`yad --title="Diary" $GEO --no-buttons --calendar --date-format="%Y_%m_%d"`
Y=`echo $CHOOSEFILE | awk 'BEGIN { FS = "_" } ; { print $1 }'`
m=`echo $CHOOSEFILE | awk 'BEGIN { FS = "_" } ; { print $2 }'`
d=`echo $CHOOSEFILE | awk 'BEGIN { FS = "_" } ; { print $3 }'`

while [ ! -z "$CHOOSEFILE" ]; do
	leafpad $CHOOSEFILE &
	CHOOSEFILE=`yad --year=$Y --month=$m --day=$d --title="Diary" --no-buttons --calendar --date-format="%Y_%m_%d"` 
	Y=`echo $CHOOSEFILE | awk 'BEGIN { FS = "_" } ; { print $1 }'`
	m=`echo $CHOOSEFILE | awk 'BEGIN { FS = "_" } ; { print $2 }'`
	d=`echo $CHOOSEFILE | awk 'BEGIN { FS = "_" } ; { print $3 }'`
done
Would have thought gtk-calendar would have something similar/same

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#3 Post by SFR »

I didn't find anything useful in GtkCalendar's docs, but found gtk_test_widget_send_key and here's a hacky way to do it using that function.
The side effect is the mouse pointer gets teleported to the middle of the calendar widget, but this can be fixed with Xlib's XQueryPointer & XWarpPointer.

Code: Select all

#include <X11/Xlib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <time.h>
#include <langinfo.h>

gint on_init (GtkWidget *window, gpointer calendar) {

	int i;
	time_t t = time(NULL);
	struct tm tm = *localtime(&t);

	// determine day of week of the first day of current month & year
	// https://stackoverflow.com/a/21235587
	int d    = 1;
	int m    = tm.tm_mon + 1;
	int y    = tm.tm_year + 1900;
	int weekday  = (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7; 

	// determine first weekday (Sunday=1, Monday=2)
	int first_weekday = *nl_langinfo(_NL_TIME_FIRST_WEEKDAY);

	// get current coords of mouse pointer
    Window root_window;
    int root_x, root_y;
    unsigned int mask;
    Display *display = XOpenDisplay(NULL);
    XQueryPointer(display, DefaultRootWindow(display), &root_window, &root_window, &root_x, &root_y, &root_x, &root_y, &mask);

	// move the "cursor" to current day; side effect - mouse poitner gets warped to the middle of calendar widget
	for ( i = 0; i <= tm.tm_mday + weekday - first_weekday; i++)
		gtk_test_widget_send_key (GTK_WIDGET(calendar), GDK_KEY_Right, 0);

	// move the mouse pointer to the previous coords
    XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, root_x, root_y);
    XFlush(display);
    XCloseDisplay(display);

	return TRUE;
}

int main (gint argc, gchar *argv[]) {

	gtk_init(&argc, &argv);
	
	GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

	GtkWidget *calendar = gtk_calendar_new();
	gtk_container_add (GTK_CONTAINER(window), calendar);
	g_signal_connect (window, "show", G_CALLBACK(on_init), G_OBJECT(calendar));

	gtk_widget_show_all (window);
	gtk_main();

	return 0;
}
Tried a couple of dates and it seems to work.
HTH (if that's what you actually meant by 'focus'?).

Greetings!
Last edited by SFR on Fri 15 Dec 2017, 22:38, edited 1 time in total.
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#4 Post by vovchik »

Dear SFR,

Well done and very clever. For those who don't know what to do with the source but you have a compiling environment (devx), use the following compile line:

Code: Select all

gcc widg.c -o widg -lX11 `pkg-config gtk+-2.0 --cflags --libs`
That works for me. :) I have attached a Tahr 32-bin binary and the source (which includes the compile line).

Thanks and kind regards,
vovchik
Attachments
widg.tar.gz
(4.45 KiB) Downloaded 184 times

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#5 Post by SFR »

Thanks, forgot about that.
I have GTK/X11 libs added to Geany's build command, so it builds stuff without me having to think what's actually needed. :wink:

Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
torm
Posts: 186
Joined: Sat 07 Mar 2015, 19:56

#6 Post by torm »

Thanks for suggestions.

I was hoping there would be some simple one-liner, at some level, that I've overlooked..
To set or grab focus on current day.

Focus element in GtkCalendar is one of the few that accepts png files in gtkrc and
is not drawn with cairo as a solid color.
By default, "selected" day bg is the same as "inactive area" bg for weekday names..
Maybe it's to support monochrome displays, who knows.. anyhow, it looks ugly with
most global themes, and very limiting with custom widget-level theme.
Aside from that little nasty "programmer-art" downside, GtkCalendar is correct with
fonts ( any TTF ) and locale support ( can't say the same about BB cal command.. )
and I don't think there's any good reason to try duplicate or rebuild it.
Well, Osmo is is pretty good at doing that already :?

For clarity, the calendar widget I'm trying to make look "usable" is posted here:
http://murga-linux.com/puppy/viewtopic. ... 6&start=15
( calendar-02.pet )

Now.. where it comes to Yad.
I think it uses the same GtkCalendar on the inside.
Selected, focused and marked date.. are a bit different things, that may or not overlap.
For example, you'll get a focus element with a sigle-click on the date.


// move the "cursor" to current day;
Sorry for not seeing the full math in it..
Ok, it locates the date "1" and moves "right" times "i" ?
ehh.. I quess eventualy it should move the focus element to the current date, "today" ?
On my system it moves the focus on "tomorrow"
I can only suspect, it is counting a ( nonexisting ) sunday from last month, on the first row.
So it will pick the wrong day.
It may need to detect from locale if to take a step "left" or not ?
Maybe there's some other way.. I'll need to think about it.
At least it places the focus on the calendar,
and that is a good thing to start with.

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#7 Post by SFR »

It may need to detect from locale if to take a step "left" or not ?
Yep, that's the case indeed:

Code: Select all

#include <langinfo.h>
[...]
int first_weekday = *nl_langinfo(_NL_TIME_FIRST_WEEKDAY);
[...]
for ( i = 0; i <= tm.tm_mday + weekday - first_weekday; i++)
I corrected the code in my first post.

Anyway, I believe there must be some lower-level way to do it (control the 'select' mark), perhaps with GDK, but I guess it's a matter of analyzing the gtkcalendar's source code itself...

Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
torm
Posts: 186
Joined: Sat 07 Mar 2015, 19:56

#8 Post by torm »

Looking into GtkCalendar source only reminded me of RTL locales.. :?
And that it does not even try to clean-up the marks and focus when the
month or year arrows are clicked. One strange widget..

I think I'll leave it to that for now, until have figured out if there's a way to
add and remove the focus depending on selected month/year.
Appears to work for the "selected/marked date" part, re-used some of the suggested code and some
details from GtkCalendar source. Please confirm or fix.
Below is a experimental pet package with precompiled binary, source, theme, font and etc. as it
could be used for a panel pop-up calendar.

Note, for this version there is no visible focus element at all.. :wink:
Attachments
calendar-test-03.pet
test..
(47.75 KiB) Downloaded 154 times

User avatar
torm
Posts: 186
Joined: Sat 07 Mar 2015, 19:56

#9 Post by torm »

Is there any known location where I could find
a C source for ubuntulooks GTK2 theme engine?

Only interested in source code, not the precompiled library.

User avatar
OscarTalks
Posts: 2196
Joined: Mon 06 Feb 2012, 00:58
Location: London, England

#10 Post by OscarTalks »

Is this any good?
From packages.debian.org where they usually also have the original source tarball over on the right of the page.
http://deb.debian.org/debian/pool/main/ ... rig.tar.gz
Page was this one:-
https://packages.debian.org/jessie/gtk2 ... buntulooks
Oscar in England
Image

Post Reply