Photobucket

Thursday, July 7, 2011

How to create a User Interface (UI) using Glade and pygtk - 1

This is a very basic tutorial to just get started on making small GUI.
I use Ubuntu but I believe any linux distro must do. Part 2 of this is specific to python but you could also do similar things with pure C.


<Important Fart>
I have decided to help readers by identifying the various parts of my posts as fart or not.This way anyone can dive into the actual stuff without having to bear with my runblings :D .
</Important Fart>


<Fart>
So yesterday night I had tried to give a small talk on the IRC channel #iitm-linux about GUI programming. I had intended to give the talk mainly based on what I had learnt during my first year summer as part of writing an application for Shaastra Distro( Shaastra-2010). 


So the main thing which irritated me initially while writing the application was that although I could drag and drop in Glade I couldn't exactly just drag and drop things at the location where I wanted. If I wanted a button at say location 70,30 in the window, I just couldn't place it there directly.And more irritating was that I couldn't even set the size of the button as I had wanted. 


It was not until later that I realized why was it like that and why that was actually a better way to make a UI. I might not have made much sense till now but you will understand what I mean as you proceed along this post. Ofcourse making UI has lot more things than just this dragging and dropping but, to me as a beginner, the most irritating part was this. That is why, although using glade is just something which you get after playing around with it for sometime, I am including it in this tutorial.
</Fart>


We Begin
To make UI there are two parts
One is to design how the application should look, where must a button be placed etc. The other part and the _important & interesting_ part is controlling how your app reacts to user inputs like clicking on buttons or pressing keys on the keyboard.


The easy way to do this is by using glade for the first part and using pygtk coding in python for the second part. This is the link to jump directly into part2


PART 1 : "Dragging and Dropping"


Little Introduction:
Most of the applications based in gnome desktop environment use gtk to show the UI. What is there in the gtk libraries are definitions of various types of objects possible i.e, windows, buttons and stuff like that. So what we try to do is use these libraries to create windows, button etc by setting various properties of these GTKObjects like width, height name, etc and assigning locations to these objects.
We do all this using glade to make our life easier and save time.


So first, install Glade software if you don't have it.
Open up glade. If a smaller window opens asking for some details and stuff like that just close it or click Ok.So first thing you need is a top level window. From your left side menu choose a window and just click on it. You should have it in your main space. You can see that on the right hand side towards the lower half all the window's properties.


Basically you will need to play around with them if you are making UI but in this intro I will just point you to some important things you can tweak in those properties on the right bottom. Name, resizable, default height, width etc can be changed in the 'General' settings tab. Right now you won't need to change anything in 'Signals' tab but that will come in Part 2.So I would suggest playing around a bit at this point in the tut.


Your glade window should look like this.


So now .. if you are done with playing around with those properties we shall proceed.
Important point to note is - when you intend to make UI, the best way to make it is by imagining the whole space into vertical and horizontal boxes. This is how glade wants you to make it. You will understand what I mean as you proceed.


I will use something called horizontal boxes and vertical boxes first. You can find Horizontal boxes and vertical boxes in the left side menu under containers.
Here are steps to follow


1) Click on vertical box
2) Place the vertical box on the window
3) Choose the number of Partitions to 2
4) Click on horizontal box
5) Choose the number to 3 after placing it into the bottom half of the vertical box.
It should look something like this


6) Click on button from left side panel and place it into the centre of the 3 partitions in the horizontal box.


You will observe the button to be huge in size and ugly. Choose the button now in the hierarchy on the top right and go to the 'Packing' tab in the bottom right.


7)Make the 'Fill' and 'Expand' properties to 'No'
8) Go to 'Common' tab and check the width request and height request boxes and fill both of them with a value 50 .


You will observe that the button is not exactly square. Now select the hbox from the hierarchy and go to its 'Packing' tab . And here again make the 'Fill' and 'Expand' property to 'No'. Now you will observe that the button is finally correct. So you should understand that when you are trying to decide a size for any item you are just requesting a size. Only if it is possible to have that size will you get it. The possibility depends on various things especially the container in which the item is placed. It also depends on the neighbors and other such factors. 


This way if you did not do the math properly and kept random sizes or if the user later resizes the window, everything will be taken care.I hope you now understood what I meant by " Imagine your whole UI in terms of vertical and horizontal boxes".


This tutorial was to just point you towards things. You will get better only by playing around with glade.


NICE TIP: I used this a lot. If you want to place something at some particular position but you are not getting it exactly at that position, then you could use an item called 'Label' in the left side menu near the buttons. You can place this at the right places where you want a space, may be so that you could move a button to further right or up. You have to remove the name of this label for it to mix into white space and your final user won't know.


This ends part 1 of the tutorial which I did not expect to be this big.
Find part two here.


Ignore the 'Read more' button here. This is the end of the post.
Read more!

How to create a User Interface (UI) using Glade and pygtk - 2

So this is part 2 in which I am going to explain controlling UI. You can find part 1 here. The coding part will be done in python in this post but you can also do the same in C


PART 2: "Being the God to your UI"
Before we go into the coding you need to understand what I mean by 'controlling'.It is how you would want the application to respond to user clicking and stuff like that. So for this what actually happens is something like this ....


Little Introduction:
There is something called an 'X server' running on your comp. Long long ago when there used to be long bearded hippie coders in front of giant sized terminals and everyone used mechanical keyboards. The main 'X server' would actually be running some place else on some main computer with lots of computing power . Now anyone who wants a window, suppose a gedit window, would request this 'Xserver' to send it a window of gedit You could have asked from any room in your university and the main computer which is running the server will send the data to your machine on how to render this window. 


So your machine just renders this on the screen. When you suppose click on some place, this X server is the one which gets to know where you clicked and it will hand these events to your application so that you can control what happens when a particular event occurs. So the X events include mouse click, key-press, key-release and many more which the 'X server' captures and tells your application about it if it is relevant to it.


To summarize what I said in the last paragraph, there are things called events which you need to handle which is the final step to make UI.


Let us get started.
Select the 'Signals' tab of the button and under 'GtkButton' select the 'clicked' event and under the Handler column for this event select a function name which you would like to define soon. I chose 'on_button1_clicked' .
Some thing like this
Read more!

Daskalos demo

I am storing all the content which I might want to look back at again on this blog. So this is the video of the demo of an application I wrote as part of Shaastra Distro for Shaastra-2010.

This is the github link for project Daskalos



Ignore the 'Read more' button here. This is the end of the post.
Read more!

Saturday, June 25, 2011

My pursuit to hack on an android device - 2

So this week I have been able to root the android tablet I have with me.
Frankly I thought rooting would be a real hard process where I would have to sit for long hours in front of my terminal with my android device connected and type long long commands. But a simple app did it for me after a small process of enabling USB debugging in my device the option for which is not there in my device inside the settings menu.

So this is the link for the video which helped me
http://www.youtube.com/watch?v=Y_mVnzd7w04

All credit goes to Poweruser77 for finding this.

So things I had like to understand more are
- How to hack firmware of devices ?
- How does z4root work?

Ignore the 'Read more' button here. This is the end of the post.
Read more!

Some good links and info

1) Tree style tab Firefox plugin 
------------------------------------------
 This is just too awesome. I had to reposition this to the top postion out of the 3 points in this post because it is just too awesome. I always had a big issue with tabs. 
I am sure this happens to a lot of people. You start reading an article and you come across some thing which you haven't heard of before, or else you got distracted and you opened some other link. THis way I end up opening a lot of tabs and not knowing how did I get to some link. So I craved for some extension like this. I almost decided to learn how to write firefox extensions and write it on my own during this summer. But ofcourse, as much as I like reinventing the wheel I tried finding out if there is anything like that is already available and found this.
https://addons.mozilla.org/en-US/firefox/addon/tree-style-tab/

Edit: It also works great with vimperator although you don't see vimperator in the below screenshot.

An image of my firefox


2) Real informative android internals 
explanation by Karim
----------------------------------------------------------
You can find the video and the slides here - http://www.opersys.com/blog/android-internals-101103
But apparently he also took another talk recently.
So this is the link for that
http://lanyrd.com/2011/2011-androidtm-builders-summit/sfmyr/

3) A nice material on networks 
------------------------------------------
'Professional .Net network programming' by Tim Parker, Vinod kumar and a few other names some of which I find it really hard to spell now :P

Ignore the 'Read more' button here. This is the end of the post.
Read more!

Friday, June 24, 2011

How to get a copy of flash video in firefox if you have the latest flash

I usually keep large youtube videos for buffering and watch them later after copying them from /tmp folder. But sadly updating your flash to 10.2 changes all that. And I dint know that until I finished buffering 2 large videos of more than one hr. So I began searching on how to do it ... found this awesome post on one Ubuntu forum. I would like to have easy access to it and hence I am storing the exact same thing here. Would love more solutions



1)Open flash video page (that you own of course) and let it load. Don't close the page.

2)Open terminal and give

Code:
lsof | grep Flash
lsof: list open files
In my case (opera user) it gives
Code:
operaplug 2840        teo    9u      REG        8,8  6962837     392986 /tmp/FlashXXFfe85s (deleted)
Notice the 2nd (process id) and 4rth (file descriptor - without the letter) columns.

3)
Code:
cp /proc/2840/fd/9 ~/Desktop/video 

I confess I dunno how this exactly works but one thing I know is in '/proc/2840/fd/9' fd stands for file descriptor. So we are just getting the fd for the video and then copying it.

This is where I got it from


http://ubuntuforums.org/showthread.php?t=1688948&highlight=Temp+Files&page=2

Credit to TeoBigusGeekus 
Read more!

Saturday, June 11, 2011

My pursuit to hack on an android device - 1

Ok. The first think I had like to make it clear here is that this is not a "guide" to start hacking on your android device. This is just a documentation of my pursuit to hack onto my android device.I am writing this since I am having to take a break for today and if and when I resume my pursuit again I had like a place to recap what all I did and how.

One more thing I had like to add is I am not experimenting on my own phone. I may seem dumb as you proceed through this post but I am not that dumb too. I happen to lay my hands on an android tablet from my company which I can play around with as much as I like.
Bottomline: Do not blame me if your device gets screwed up badly :D

First thing I told myself - "I wanna definitely root my device later but before that to start hacking on something you need a terminal right? So let me install a terminal."

I go to android market and search for an android terminal emulator. I get one instantly but then I realize that I don't have internet on my device because of the !@#$% proxy in my institute. So I need something like cntlm on my device for me to access internet from insti behind a proxy.So I search for this and on XDA developers I end up at this link. But alas! I need to root my device for that. So to summarize my pursuit
Want to root = > install terminal
      ^                            ||
      ||                            V
Need root &lt; =     Install Autoproxy

So I got tired of going round and round. Ofcourse you don't _need_ terminal first to root your device but it is the same process for installing anything here.
So next I decided to try to get the .apk directly and attempt to install. So I hit upon this - 3-places-to-download-android-apk/ .
I happily downloaded terminal emulator from Android Terminal Emulator for Android .And then!! I realize that I left my SD card reader in my office and hence I cannot move the apk file to my tablet!
All this reminded me of Lemony Snicket's A series of unfortunate events. So I have to wait till monday to go back to office and continue :(

I would really love comments on how else could I have done all this ( even if you are reading this post a decade after I wrote it ).


Edit #1:  I realised that I have a USB port on the tablet. I was so dumb. So I just installed terminal, droidssh and an ebook reader onto my tablet
Read more!