Thread: [Java-gnome-developer] Creating custom container
Brought to you by:
afcowie
From: Jacek F. <ja...@gm...> - 2008-12-18 00:49:34
|
Hi, I want to create a custom Container. I've subclassed Container, but the parent constructor expects a long pointer. How do I generate one? I see the other Containers use a static .createWhateverType() method from the base Gtk JNI classes, but I do not see the equivalent GtkContainer.create... method. Thanks for any help (I am new to GTK+ development, but very interested). Jacek |
From: Andrew C. <an...@op...> - 2008-12-18 03:43:30
|
On Wed, 2008-12-17 at 19:49 -0500, Jacek Furmankiewicz wrote: > I've subclassed Container So it's a fairly deliberate design decision that things that are abstract in the underlying library are abstract in java-gnome (I mean, duh, right?). That said, even if they weren't, you'd still be saddled with all the hassle of setting up the machinery to allocate resources and what not. So, as it stands right now, > , but the parent constructor expects a long pointer. How do I generate > one? > You don't. (Java desperately needs another visibility modifier: "library"; the problem with protected is that while that's fine for our internal use going from org.freedesktop.bindings to org.gnome.glib and on to org.gnome.gtk, it's bloody horrible that a developer can in turn subclass and then see something that is library internal. I've long toyed with ways to get around this, but nothing non-absurd and non-creating a subordinate object has come to mind) Anyway, the solution (or workaround, depending on your taste) is so obvious that you'll kick yourself: just subclass Button, or HBox, or Image or Label, or... I grant you it's not the "perfect" solution, but in practice anything I've found myself creating is actually a Button or "a bunch of stuff laid out horizontally" or an Image or a Label, or ... so that's what I've subclassed. Works out pretty well. [An example of where I used HBox is here. http://research.operationaldynamics.com/projects/objective/files/ForeignAmountEntryBox.png Just a bunch of Entries that reacts to changes in each other, recalculating as necessary. Packing all the Entries, Labels and the Combo (which was itself a custom subclass wired up for the domain specific purpose) into an HBox made the whole thing quite neat - and the end result is (still) a Widget that was subsequently instantiated as needed and added to other Containers doing such foreign currency work] Very long term, the way to address this is a package visible factory method somewhere that takes care of the allocation and setup issues underneath creating such a generic Container parent. But I haven't needed to do so in something like 50,000 lines of code using java-gnome, so I respectfully submit that it isn't urgent. That said, if there is some reason you really really need to do it directly, then I'm sure we can figure something out. There is indeed an elegance violation in subclassing HBox instead of Container, but on the other hand, the fact that GtkTreeView subclasses GtkContainer and then doesn't actually work as a GtkContainer is a bit rude, and GtkLabel extends GtkMisc, which is quite possibly the stupidest name for an intermediate class I've ever seen, especially given GtkMisc's actual role and the container / non-container split in the GtkWidget hierarchy. Alas. AfC Sydney |
From: Jacek F. <ja...@gm...> - 2008-12-18 12:57:53
|
Andrew, thanks for your response. If I subclass a different class than container, how do I override its default layout management logic so that I can do it myself? Let me maybe explain what I want to do: my end goal is to port the amazing Swing/SWT MigLayout layout manager to run on GTK+ (java-gnome actually, since it's a pure Java library). MigLayout is UI toolkit agnostic and needs only 3 classes to implement the toolkit-specific logic. http://www.miglayout.com So, since GTK+ does not have the concept of a layout manager, but just specialized containers with hardcoded layout management logic, I want to create something like a MigLayoutContainer, e.g. MigLayoutContainer c = new MigLayoutContainer("wrap 4","[pref] [grow] [pref] [pref]"); c.add(new Button(), "sg 1"); c.add(new Button(), "sg 1"); This code above would create a container that automatically wraps to the next row every 4 controls that are added. Columns 0, 2, 4 have the preffered control width, while column 1 resizes as the window resizes. The two buttons that were added would automatically have the same size (width/height) because they belong to the same size group. So, if I subclass a different GTK+ component, what do I need to do to override its layout management logic and implement my own from scratch? This is what I am trying to do. Don't get me wrong, GTK+ is an impressive toolkit, but their approach to layout management (Vbox. HBox, Table) is rather outdated compared to the best alternatives in both the Java and .Net world. Thanks, Jacek P.S. Some more info about me: I am the creator of the Java SwingBuilder and SWTBuilder, two libraries that aim to maximize Java UI development productivity by allowing declarative UI building in YAML: http://javabuilders.org My end goal is to port my library to run on top of java-gnome as well to bring the same productivity enhancements to creating native GTK+ apps in Java (since I am an avid Linux user and am saddened by the lack of Java apps on Linux...I think we're getting beaten badly in this area by Python and even Mono these days). Thanks for any assistance or guidance you may give me, much appreciated in advance. |
From: Jacek F. <ja...@gm...> - 2008-12-18 13:47:54
|
I was looking at the gtkmm docs on how they do custom containers and it seems that they can do it: http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/chapter-customwidgets.html#sec-custom-containers So there is probably a way to accomplish this from the Java side as well.....Jacek |