Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 21 Oct 2018, 14:28
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
GTK2 questions.
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [8 Posts]  
Author Message
torm


Joined: 07 Mar 2015
Posts: 168

PostPosted: Sat 09 Dec 2017, 11:24    Post subject:  GTK2 questions.  

GtkCalendar. Is it possible to "force" the focus element on current date?
If so, how?
Back to top
View user's profile Send private message 
rufwoof

Joined: 24 Feb 2014
Posts: 2599

PostPosted: Sat 09 Dec 2017, 11:52    Post subject:  

I use yad calendar which you can set the active day, month and year i.e.
Code:
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
Back to top
View user's profile Send private message 
SFR


Joined: 26 Oct 2011
Posts: 1665

PostPosted: Sun 10 Dec 2017, 17:30    Post subject:  

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:
#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!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.

Last edited by SFR on Fri 15 Dec 2017, 18:38; edited 1 time in total
Back to top
View user's profile Send private message 
vovchik


Joined: 23 Oct 2006
Posts: 1469
Location: Ukraine

PostPosted: Wed 13 Dec 2017, 17:02    Post subject:  

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:
gcc widg.c -o widg -lX11 `pkg-config gtk+-2.0 --cflags --libs`


That works for me. Smile I have attached a Tahr 32-bin binary and the source (which includes the compile line).

Thanks and kind regards,
vovchik
widg.tar.gz
Description 
gz

 Download 
Filename  widg.tar.gz 
Filesize  4.45 KB 
Downloaded  83 Time(s) 
Back to top
View user's profile Send private message 
SFR


Joined: 26 Oct 2011
Posts: 1665

PostPosted: Thu 14 Dec 2017, 09:15    Post subject:  

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!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send private message 
torm


Joined: 07 Mar 2015
Posts: 168

PostPosted: Fri 15 Dec 2017, 16:25    Post subject:  

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 Confused

For clarity, the calendar widget I'm trying to make look "usable" is posted here:
http://murga-linux.com/puppy/viewtopic.php?t=101066&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.
Back to top
View user's profile Send private message 
SFR


Joined: 26 Oct 2011
Posts: 1665

PostPosted: Fri 15 Dec 2017, 18:39    Post subject:  

Quote:
It may need to detect from locale if to take a step "left" or not ?

Yep, that's the case indeed:
Code:
#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!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send private message 
torm


Joined: 07 Mar 2015
Posts: 168

PostPosted: Sat 23 Dec 2017, 15:30    Post subject:  

Looking into GtkCalendar source only reminded me of RTL locales.. Confused
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
calendar-test-03.pet
Description  test..
pet

 Download 
Filename  calendar-test-03.pet 
Filesize  47.75 KB 
Downloaded  61 Time(s) 
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [8 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0530s ][ Queries: 12 (0.0069s) ][ GZIP on ]