Thursday, September 30, 2010

Animated scroll Ext JS grid row into view

I've been using Ext JS at work for a week or so now, mainly for the
grids. Everything went well until I wanted to scroll a grid row into

Googling found responses like this one:

The problem with focusRow is that the grid just jumps to the new row
with no animation. I also wanted the focused row to be centered in the
grid, but focusRow prefers to put the row at the bottom of the grid.

Happily, there's an easy answer. A little digging through the Ext JS
source and you can hack up an animated scroll to bring a grid row to
the center of the grid as:

function smoothScrollIntoView(element, container) {
var c = Ext.getDom(container) || Ext.getBody().dom,
el = element.dom,
o = element.getOffsetsTo(c),
t = o[1] + c.scrollTop,
ch = c.clientHeight;
var newCTop = t - ch / 2;
if (newCTop < 0)
newCTop = 0;
container.scrollTo('top', newCTop, true);

function gridRevealRow(grid, rowIndex) {
var row = new Ext.Element(grid.getView().getRow(rowIndex));
smoothScrollIntoView(row, grid.getView().scroller);

And then you can just call gridRevealRow(grid, rowindex) to scroll
a grid row into view.

Friday, March 20, 2009

The Name of the Wind

What a great book! I've not been following the fantasy scene closely, since most fantasy these days is complete dreck. The last good novel I read was Susanna Clarke's Jonathan Strange and Mr. Norrell, and then I happened upon Patrick Rothfuss' work in a rather roundabout way that landed me on this page:

I found it entertaining, but when I got to "P.S. Your tears are delicious to me", I realised I simply had to read this book.

And wow, what a book. Kvothe is inhumanly good, like Superman, but without being tiresomely superior[*], and seeing the world through his eyes makes him enormously more sympathetic than he might have been in the third person.

The great thing about Rothfuss is the sense of wonder that he manages to capture. I loved the interludes of music the most, far more than the descriptions of "sympathy". Looking back at the book, I can't say I remember any outstanding characters - most of them are two-dimensional compared to the hero - but I did like Master Elodin the Namer.

I read the book in two sittings, maybe around 12-14 hours, and loved almost every minute, and I now get to join the giddy throng waiting for "The Wise Man's Fear."

The waiting sucks, but I can wait. I had to wait years for Stephen King to finish the Dark Tower (that might have been better if left unfinished at say, Wizard and Glass). I'm already nine years older than I was when I read Martin's Game of Thrones, so I think the wait for Wise Man's Fear isn't going to kill me yet.

Recommend: BUY!
The Name of the Wind
Patrick Rothfuss
ISBN 978-0756404079
[*] Batman so totally kicks Superman's ass in the likeability department.

Thursday, March 19, 2009

Truncated date row in AjaxControlToolkit CalendarExtender

Recently I and a friend have been moonlighting building an ASP.NET web site. This was our first time using C#, and my first time doing any real work in Visual Studio.

I'm no Microsoft lover (I ran Visual Studio in a VM on my Linux laptop), but I have to admit Visual Studio is slick. It's light (compared to Eclipse, at least), its autocomplete is great, and the code-deploy-test cycle is gratifyingly quick.

To my surprise, I quite liked C#. Properties, delegates and var foo = new Bar() alone would make it better than Java, but its lambda expressions simply blow Java away. The only catch with lambda expressions that I could see is that you can't declare a Func that returns void (Func). You either have to declare a delegate, or make the poor Func return a junk object just to keep the compiler happy.

One of the things I needed in this web app was to use date choosers. A little Googling suggested that the AjaxControlToolkit's CalendarExtender control was the way to go. It took me a while to figure out how to actually use it in a page after including the assembly reference in the project. This involved:

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

and then attaching a calendar extender to a date input text field as:

<asp:TextBox ID="SomeDate" runat="server" />
<ajaxToolkit:CalendarExtender ID="SomeDateCalendar"
TargetControlID="SomeDate" runat="server" />

This worked great, right up until I noticed that the date chooser was missing the last row of dates, pretending that every month had 28 days or less. I checked the example project for the date chooser, and the date chooser looked fine there, so I guessed that the AWOL last row involved something in my webapp's CSS. After some mad Googling for a quick fix, and much cursing, I decided to investigate the matter with Firebug, which revealed that the calendar was likely barfing on the extra padding and margins that the page's CSS used for divs and table cells.

For posterity, here's the CSS style I had to add to my page to make the calendar behave correctly:

.ajax__calendar * {
font-family: "Tahoma", "Verdana", "Helvetica", "Sans-Serif";
padding: 0;
margin: 0;

(This is probably a bug in the calendar's own CSS - it should certainly reset margins and padding if it doesn't want it.)

Thursday, March 5, 2009


For a while now, I've been dissatisfied with the performance of my laptop, a Macbook Pro running x86_64 Arch Linux. Everything worked great most of the time, but every now and then, the laptop would go nuts and become horribly unresponsive.

When in this weird condition, the laptop had trouble even keeping up with keyboard and mouse input. I could hold down a key and I'd get "kkkk (long pause) kkkk (long pause) kkkk...". It was so bad I couldn't use the laptop at all once it hit this snag, and I'd usually have to reboot, because I couldn't work out what was causing the problem (no userspace CPU usage, no I/O activity).

Finally, several days back, I accidentally happened on the cause of the problem - my Madwifi wireless drivers. I caught the laptop in the throes of another fit of lag and did an rmmod -f of the ath* modules, and bingo! Input was smooth as silk again. I blacklisted the ath* modules, and I haven't run into the spastic-laptop syndrome since, so I'm reasonably sure that madwifi was the culprit.

Amusingly, I wasn't even _using_ my wireless connection at the time - I prefer cabled ethernet where available.

The question: how do you troubleshoot such problems, in general? I have no good material for a bug report to the madwifi folks and I'm not sure how to go about gathering evidence for this. syslog[*] had nothing to say about madwifi's woes.

[*] I've recently moved to Arch, and it took me a while to figure that /var/log/everything.log had the stuff I might look for in /var/log/syslog on Debian.

Wednesday, February 18, 2009

Dead Mac Fans

The right fan on my Macbook Pro died last week, and my mac promptly went into mourning, moodily switching itself off whenever I asked it to do a long compile session (bad), or watch a long movie (worse). The fan had been making discontented noises for the past few days, so diagnosis was easy - I googled for fan speed tools, found and installed smcFanControl, and it duly identified my right fan (0 rpm) as the slacker.

This Macbook Pro is my primary laptop, and I have no spare. To make matters worse, I'd carelessly forgotten to take the 3 year extended AppleCare protection, which meant the laptop was no longer under warranty. I didn't mind giving the laptop to Apple, but if there is one thing that I've learned about laptop repairmen, it is that they are not in a hurry.

Hand a laptop in for repair, and you can be sure that the job - whatever it is - will take lots of time. First, someone has to "evaluate" the laptop to see what the matter with it is (and telling them "The right fan isn't spinning" will not speed up this process). Once the laptop has been evaluated (after several days have passed, and your patience has worn very thin), somebody will call you up to give you valuable information ("We've examined your laptop very carefully, and discovered that - wait for it - your right fan is dead!"). Then they have to order a replacement part, choosing the cheapest possible shipping option, and after a few weeks have passed, and you have aged a few decades trying to use some other computer to do all your work, you might get your laptop back.

I might have handed in my laptop anyway if it were under warranty, but it wasn't, and I thought I could save a lot of time and money if I ordered a new fan and installed it myself. Or rather, if I ordered a new fan, and got somebody who was good at fixing things to install it. It so happened that I have a good friend who's brilliant at fixing stuff, so I went ahead and ordered a replacement right fan assembly from WeLoveMacs ($59 for the fan, $50 for Fedex International Express shipping). Fedex promised 2-5 days for delivery, but WeLoveMacs took two days to process my order, so it was 5 days before I got hold of the fan. Those were five long days, too. I'd borrowed a Compaq laptop from this same friend, and I found it trying. Its screen was too small, and the keyboard was decked out with stiff, proud, independent keys, disinclined to yield to anything less than sharp hammerblows.

Armed with the shiny new fan, my friend and I disassembled the Macbook Pro. We split the work evenly - he did all the physical work of unscrewing the case and unplugging cables, while I watched and made constructive suggestions ("This is taking frightfully long. Can't you speed it up a bit?") The laptop came apart without incident (we followed the iFixit guide), we removed the old fan and put in the new one. The insides of the laptop were filled with lots of dust - visible heaps of dust around the stalled fan -so we cleaned it up a bit, then closed it up and reassembled the Mac, switched it on, and ... it wouldn't come on.

This surprised and vexed us ("WTFF is going on?!") and we took everything apart again and looked at it, and it was all good. My suspicions centered on the keyboard cable, but we took a sharp look at it, and it looked as blameless a keyboard cable as anyone might want. My friend, having spent some time in thought, suggested that it was probably something quite minor, like a blown logic board.

I looked at the prices for replacement logic boards - iFixit quoted $999 for a replacement (used) logic board. At this point, I wondered whether I should just have given the damn laptop in to the Apple folks to start with, and cursed myself for being a thrice-damned moron.

After some dispirited discussion ("$999? DEAR GAWD!"), we decided to turn in the laptop to the Apple folks for expert review.

As expected, it took two days for the repair folks to "evaluate" the laptop, but, to my enormous relief, they found that the power button was defective and the logic board itself was fine. They had to replace the upper case (Rs. 10000 for the case and Rs 750 for labour, or about $215). Oh, and they found both fans working just fine.

Next time, I think I'll just take that 3 year extended AppleCare.

Tuesday, October 21, 2008

"Jonathan Strange and Mr. Norrell" and the difficulty in persuading your friends to read good books

A couple of days ago when visiting my parents, I found my copy of Susanna Clarke's Jonathan Strange and Mr. Norrell, and I opened it at an idle moment and glanced down the first page, and before I could stop myself I was quite caught up in rereading it. I found it nearly as entertaining as the first time I'd read it, not on account of the plot (which, having read the book once before, I already knew), but entranced by the mastery of the language.

In case you have not read this book, it is a fantasy set in an alternate history of 1800s England, where there are magicians, fairies - real fairies, malicious and very powerful, not the fuzzy cute nice fairies that modern children's literature sometimes describes - and a lot of sometimes creepy, often funny action.

It is not every author whose books I can reread. The works of Wodehouse, Jerome, and Pratchett are among my favorites to reread, and now I'm happy to include Clarke in the list.

I'd left my copy of Strange & Norrell at my parents' in an attempt to persuade my mother to read it. After reading the book for the first time, I'd been filled with an almost religious zeal to convince my friends and relatives to read this masterpiece. Reading it could do nothing but good for their minds, I reasoned, and I thought I should encourage this mental uplift with word and deed.

My father last read a book in the 1970s, before I was born, so it was no use suggesting that he read it (I'd tried persuading him to read Wodehouse in the past, to no effect). Instead, I told my mother about the book and described it in glowing terms: the story, the characters, the brilliant use of language. And I gave her my copy of the book and suggested that reading it, she would find it a far better thing than she had read in a great while - which was not difficult, for her recent taste in literature ran primarily to cooking recipes in womens' magazines.

She took the book without much enthusiasm and said she'd read it, but she did not. This, while distressing, did not surprise me greatly, because my mother has a low opinion of my taste in books, dating back to when I was in my early teens , and she looked through a copy of Stephen King's It that I was reading at the time, obviously anxious to investigate what I found so enthralling in it; she lost no time finding the one description of group sex, and gave me an exasperated lecture. I believe this is a gift given to mothers everywhere, to unerringly home in on the one or two things you're reading / doing / whatever that you'd prefer she did not know. Anyway, as a result of that look through It, and a few subsequent such books (including, notably, Clive Barker's Imajica), my mother takes a very jaundiced view of the books I read and these days she goes to great lengths to avoid reading them, or indeed, even looking at them.

I also recommended to my sister that she read Strange & Norrell, and she was impressed to the extent of picking up a copy of it herself, which pleased me. My sister is two years older than I, and when we were children it was always the case that she introduced me to new authors, while I was still reading books that she'd outgrown two years ago.

"Oh, you're still reading that!" she'd say, eyeing my Enid Blyton with a look of gentle pity. The first time she had occasion to point out the immaturity of the Enid Blyton I was reading, she herself was reading a book by Louis L'Amour. Her description of the book had me all eager to read it myself, but when I did, I was disappointed.

The book (Mojave Crossing) involved a protagonist who spent great amounts of time crossing a desert. This desert (as any sane man might expect) is hot and dry, and our hero did not enjoy the heat and lacked water. Page after page was devoted to his search for water and I found the description did not grip. Where was the exciting action, I wondered. Where were the secret passages? (I was, after all, still in my Enid Blyton phase, where secret passages were de rigueur.) I saw no point in a book that lacked secret passages and offered such a poor substitute for them as a desert with no water. I felt I could take deserts or leave them, so I left them and went back to my Enid Blyton.


Some eight years later, when I was in college, I looked at one of the Enid Blytons (Five go to Demon's Rocks) that had so fascinated me as a child, and I was appalled. The book seemed to ooze condescension at the reader, as if Enid Blyton had thought to herself, "What complete nitwits children are, to read this bilge I write, but I suppose I must keep them entertained or they will make even greater pests of themselves than they normally are!" I was quite put out with my younger self for having lapped up this patronizing malarkey. True, I had always suspected that my mentality at the age of 9 was indifferent and scarcely human, but I hadn't until then fully plumbed the depths of inanity that was my mind at 9.

A few years after that, when I was 12 or so, years during which I remained staunchly anti-L'Amour and avoided his books with the care others would take to steer clear of venomous snakes, I had to accompany my sister when she was taking a veena lesson. I was not involved in the lesson, and I did not have a very musical soul, and I found the lesson was rapidly beginning to wear on my nerves.

The veena lesson involved persistent harassment of an old, sorry-looking veena, and much singing of scales (sa-re-ga-ma-pa-da-ni-sa, sa-ni-da-pa-ma-ga-re-sa!). In short order my protesting brain had memorized the scales to such perfection that my corpse would be able to recite them without trouble, and I realized that unless I took immediate steps to distance myself from the veena lesson, I would go insane. My sister happened to be reading another Louis L'Amour at the time, so I picked it up and went as far away from the singing of the scales as possible and tried to read the book.

I was surprised to find that this Louis L'Amour was great stuff. I do not recall the title, but it involved a character being pursued across a wild western countryside by Indians eager to separate him from his scalp. Our hero had his hands full staying ahead of these Indians and staying attached to his scalp, and so had hardly any leisure at all. On the rare occasions when he appeared to have left the Indians behind and took time out to smoke a cigarette (one that he rolled himself), those sneaky Indians would tiptoe up around him and there'd be a tense showdown, with the hero crouched down in cover (having stubbed out the cigarette and pulled out his rifle) and looking around for Indians to shoot. The Indians, being reluctant to get shot, would deck themselves with bushes and twigs and stuff, and move around like homicidal mobile shrubbery, hoping to thus elude detection. Anyway, it was all very exciting, and I could not believe that I'd been so daft as to avoid Louis L'Amour all these years. The book was so fascinating that the horrible sounds of the veena lesson receded safely over my horizon of thought and my sanity - such as it was - was preserved.

Having received book recommendations from my sister for so many years, I was naturally keen to return the favor with my recommendation of Susanna Clarke. Imagine my disappointment, therefore, when she dutifully read some 50 pages of the book, and seemed to find it dull and unrewarding, because she stopped reading it thereafter.

If you've ever been in the position where you've just read a sublime, beautiful book, and you want to spread your joy by recommending it to friends and acquaintances, you'll know the keen disappointment of finding that your friends, superficially sane and reasonable though they may seem, lack that spark that would let them enjoy the book. I recall once lending a terrific book by Terry Pratchett (Men at arms) to a colleague at work who seemed more inclined to read than most, and being terribly disappointed to find that he did not like the book. I could not imagine that there was anybody who could read who could not like the book. True, a lot of my other friends did not care for Pratchett, but these others are friends who care for no books at all, and generally restrict their reading to the captions under pictures of models in the less intelligent sections of newspapers, and occasionally, when they feel in the mood for a mental challenge, read the sports columns. This friend I'd loaned my Pratchett to, on the other hand, read books all the time, but under cross-examination, he admitted that his diet consisted primarily of spy-stories and the like, and that he had hunted through the Pratchett in vain for any mention of spies, and found none, which had quite killed his interest in the book.

It's been about a year since I read Jonathan Strange and Mr. Norrell, and I still have yet to meet (in real life; various people I chat with on IRC claim to have read the book, and indeed, I found the book after it was recommended to me by someone on IRC) anybody else who has read the book, or who does not retire coyly when I recommend the book to them.

Having failed to convince my immediate circle to read the book, I'd now like to extend my appeal to the world: read this book, it is awesome!

The magic in the book lies not just in the story, but in Clarke's prose, which is almost, dare I say it, as lyrical and funny as Wodehouse. The story may not race off the page, but when the writing's as good as this, you don't want it to - you want to steep yourself in it and relish the movement of the plot.

Quite beyond the main characters, Mr. Norrell, who is so "boring" as to be endlessly diverting in his anxieties and peculiarities, and Jonathan Strange, who is obviously entertaining in himself, the minor characters are the ones that take the book beyond brilliance into greatness: Childermass, Jonathan Strange's manservant Jeremy Johns, and many others (the doctors that attended upon the King of England when he suffered his fits of insanity, for instance). All this neglects the footnotes that sometimes have complete little stories in them.

There are some moments that define the book for me: the gentleman with the thistle-down hair attempting to enchant Strange and the King, for instance - what a wonderful, chilling scene. Strange's first description of the King's roads is another, evocative of Tolkien's Moria. And these are just a few of the amazing scenes in this book.

I don't want to spoil the book by dwelling on it further, for I, like Mr. Norrell, lack a gift for making things sound interesting, and indeed many of my friends take my recommendations as things that they would do well to avoid. So, I shall say no more on this marvelous book, but do yourself a favor and read it, you'll thank me for this recommendation!

Jonathan Strange and Mr. Norrell by Susanna Clarke