summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Merry <dev@randomguy3.me.uk>2012-12-06 18:03:26 +0000
committerAlex Merry <dev@randomguy3.me.uk>2012-12-06 18:03:26 +0000
commit7aa6eda371eea746792dce412b97ed9799e3467e (patch)
treea4cd39419ef50345e8700f625066d42736a2bd9d
parentf23a2d9daa1eb62b2afac8997f1d76eb881628f7 (diff)
Add a toolbox
Just allows selecting tools, so far - no tool configuration.
-rw-r--r--tikzit/src/Makefile.am1
-rw-r--r--tikzit/src/gtk/Application.h4
-rw-r--r--tikzit/src/gtk/Application.m31
-rw-r--r--tikzit/src/gtk/BoundingBoxTool.m2
-rw-r--r--tikzit/src/gtk/CreateEdgeTool.m2
-rw-r--r--tikzit/src/gtk/CreateNodeTool.m2
-rw-r--r--tikzit/src/gtk/HandTool.m2
-rw-r--r--tikzit/src/gtk/Menu.m4
-rw-r--r--tikzit/src/gtk/SelectTool.m2
-rw-r--r--tikzit/src/gtk/Tool.h2
-rw-r--r--tikzit/src/gtk/ToolBox.h37
-rw-r--r--tikzit/src/gtk/ToolBox.m172
12 files changed, 251 insertions, 10 deletions
diff --git a/tikzit/src/Makefile.am b/tikzit/src/Makefile.am
index 83e0978..8d51c60 100644
--- a/tikzit/src/Makefile.am
+++ b/tikzit/src/Makefile.am
@@ -60,6 +60,7 @@ tikzit_SOURCES = gtk/Application.m \
gtk/StyleManager+Storage.m \
gtk/StylesPane.m \
gtk/TikzDocument.m \
+ gtk/ToolBox.m \
gtk/WidgetSurface.m \
gtk/Window.m \
gtk/cairo_helpers.m \
diff --git a/tikzit/src/gtk/Application.h b/tikzit/src/gtk/Application.h
index b05a9cd..260f7ef 100644
--- a/tikzit/src/gtk/Application.h
+++ b/tikzit/src/gtk/Application.h
@@ -25,6 +25,7 @@
@class SettingsDialog;
@class StyleManager;
@class TikzDocument;
+@class ToolBox;
@class Window;
@protocol Tool;
@@ -44,6 +45,7 @@ extern Application* app;
NSString *lastOpenFolder;
NSString *lastSaveAsFolder;
+ ToolBox *toolBox;
PreambleEditor *preambleWindow;
PreviewWindow *previewWindow;
SettingsDialog *settingsDialog;
@@ -79,7 +81,7 @@ extern Application* app;
/**
* The currently-selected tool
*/
-@property (retain) id<Tool> activeTool;
+@property (assign) id<Tool> activeTool;
/**
* The folder last actively chosen by a user for opening a file
diff --git a/tikzit/src/gtk/Application.m b/tikzit/src/gtk/Application.m
index ac6a451..38ef231 100644
--- a/tikzit/src/gtk/Application.m
+++ b/tikzit/src/gtk/Application.m
@@ -32,6 +32,7 @@
#import "StyleManager+Storage.h"
#import "SupportDir.h"
#import "TikzDocument.h"
+#import "ToolBox.h"
#import "Window.h"
#import "BoundingBoxTool.h"
@@ -47,6 +48,7 @@ Application* app = nil;
@interface Application (Notifications)
- (void) windowClosed:(NSNotification*)notification;
+- (void) selectedToolChanged:(NSNotification*)notification;
@end
@implementation Application
@@ -107,9 +109,18 @@ Application* app = nil;
[BoundingBoxTool tool],
[HandTool tool],
nil];
+ for (id<Tool> tool in tools) {
+ [tool loadConfiguration:configFile];
+ }
activeTool = [[tools objectAtIndex:0] retain];
- // FIXME: toolboxes
+ toolBox = [[ToolBox alloc] initWithTools:tools];
+ [toolBox loadConfiguration:configFile];
+ [[NSNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(selectedToolChanged:)
+ name:@"ToolSelectionChanged"
+ object:toolBox];
app = [self retain];
}
@@ -164,12 +175,18 @@ Application* app = nil;
[openWindows release];
[tools release];
[activeTool release];
+ [toolBox release];
[super dealloc];
}
- (id<Tool>) activeTool { return activeTool; }
- (void) setActiveTool:(id<Tool>)tool {
+ if (activeTool == tool)
+ return;
+
+ activeTool = tool;
+ [toolBox setSelectedTool:tool];
for (Window* window in openWindows) {
[window setActiveTool:tool];
}
@@ -258,6 +275,11 @@ Application* app = nil;
[styleManager saveStylesUsingConfigurationName:@"styles"];
+ for (id<Tool> tool in tools) {
+ [tool saveConfiguration:configFile];
+ }
+ [toolBox saveConfiguration:configFile];
+
if (lastOpenFolder != nil) {
[configFile setStringEntry:@"lastOpenFolder" inGroup:@"Paths" value:lastOpenFolder];
}
@@ -283,6 +305,13 @@ Application* app = nil;
gtk_main_quit();
}
}
+- (void) selectedToolChanged:(NSNotification*)n {
+ id<Tool> tool = [[n userInfo] objectForKey:@"tool"];
+ if (tool != nil)
+ [self setActiveTool:tool];
+ else
+ NSLog(@"nil tool!");
+}
@end
// vim:ft=objc:ts=8:et:sts=4:sw=4:foldmethod=marker
diff --git a/tikzit/src/gtk/BoundingBoxTool.m b/tikzit/src/gtk/BoundingBoxTool.m
index 03bd341..76c4f10 100644
--- a/tikzit/src/gtk/BoundingBoxTool.m
+++ b/tikzit/src/gtk/BoundingBoxTool.m
@@ -38,7 +38,7 @@ float tbHandleLeft(NSRect bbox) {
@implementation BoundingBoxTool
- (NSString*) name { return @"Bounding Box Tool"; }
-- (const gchar*) stockIcon { return TIKZIT_STOCK_BOUNDING_BOX; }
+- (const gchar*) stockId { return TIKZIT_STOCK_BOUNDING_BOX; }
- (NSString*) helpText { return @"Set the bounding box"; }
- (NSString*) shortcut { return @"b"; }
diff --git a/tikzit/src/gtk/CreateEdgeTool.m b/tikzit/src/gtk/CreateEdgeTool.m
index 4124a78..7120d5b 100644
--- a/tikzit/src/gtk/CreateEdgeTool.m
+++ b/tikzit/src/gtk/CreateEdgeTool.m
@@ -25,7 +25,7 @@
@implementation CreateEdgeTool
- (NSString*) name { return @"Create Edge Tool"; }
-- (const gchar*) stockIcon { return TIKZIT_STOCK_CREATE_EDGE; }
+- (const gchar*) stockId { return TIKZIT_STOCK_CREATE_EDGE; }
- (NSString*) helpText { return @"Create new edges"; }
- (NSString*) shortcut { return @"e"; }
@synthesize activeRenderer=renderer;
diff --git a/tikzit/src/gtk/CreateNodeTool.m b/tikzit/src/gtk/CreateNodeTool.m
index 581ace3..e6603b7 100644
--- a/tikzit/src/gtk/CreateNodeTool.m
+++ b/tikzit/src/gtk/CreateNodeTool.m
@@ -25,7 +25,7 @@
@implementation CreateNodeTool
- (NSString*) name { return @"Create Node Tool"; }
-- (const gchar*) stockIcon { return TIKZIT_STOCK_CREATE_NODE; }
+- (const gchar*) stockId { return TIKZIT_STOCK_CREATE_NODE; }
- (NSString*) helpText { return @"Create new nodes"; }
- (NSString*) shortcut { return @"n"; }
@synthesize activeRenderer=renderer;
diff --git a/tikzit/src/gtk/HandTool.m b/tikzit/src/gtk/HandTool.m
index 830fc28..3f8906c 100644
--- a/tikzit/src/gtk/HandTool.m
+++ b/tikzit/src/gtk/HandTool.m
@@ -23,7 +23,7 @@
@implementation HandTool
- (NSString*) name { return @"Drag Tool"; }
-- (const gchar*) stockIcon { return TIKZIT_STOCK_DRAG; }
+- (const gchar*) stockId { return TIKZIT_STOCK_DRAG; }
- (NSString*) helpText { return @"Move the diagram to view different parts"; }
- (NSString*) shortcut { return @"m"; }
@synthesize activeRenderer=renderer;
diff --git a/tikzit/src/gtk/Menu.m b/tikzit/src/gtk/Menu.m
index 8d0cd97..4652599 100644
--- a/tikzit/src/gtk/Menu.m
+++ b/tikzit/src/gtk/Menu.m
@@ -591,11 +591,11 @@ static void tool_cb (GtkAction *action, id<Tool> tool) {
[[tool name] UTF8String],
[[tool name] UTF8String],
[tooltip UTF8String],
- [tool stockIcon]);
+ [tool stockId]);
gtk_action_group_add_action_with_accel (
appActions,
action,
- [[tool shortcut] UTF8String]);
+ NULL);
g_signal_connect (
G_OBJECT (action),
"activate",
diff --git a/tikzit/src/gtk/SelectTool.m b/tikzit/src/gtk/SelectTool.m
index 08abbea..2501935 100644
--- a/tikzit/src/gtk/SelectTool.m
+++ b/tikzit/src/gtk/SelectTool.m
@@ -39,7 +39,7 @@ static const InputMask unionSelectMask = ShiftMask;
@implementation SelectTool
- (NSString*) name { return @"Select Tool"; }
-- (const gchar*) stockIcon { return TIKZIT_STOCK_SELECT; }
+- (const gchar*) stockId { return TIKZIT_STOCK_SELECT; }
- (NSString*) helpText { return @"Select, move and edit nodes and edges"; }
- (NSString*) shortcut { return @"s"; }
@synthesize configurationWidget=configWidget;
diff --git a/tikzit/src/gtk/Tool.h b/tikzit/src/gtk/Tool.h
index de3d3ff..6f6ba19 100644
--- a/tikzit/src/gtk/Tool.h
+++ b/tikzit/src/gtk/Tool.h
@@ -29,7 +29,7 @@
@protocol Tool <RenderDelegate,InputDelegate>
@property (readonly) NSString *name;
-@property (readonly) const gchar *stockIcon;
+@property (readonly) const gchar *stockId;
@property (readonly) NSString *helpText;
@property (readonly) NSString *shortcut;
@property (retain) GraphRenderer *activeRenderer;
diff --git a/tikzit/src/gtk/ToolBox.h b/tikzit/src/gtk/ToolBox.h
new file mode 100644
index 0000000..1d8020f
--- /dev/null
+++ b/tikzit/src/gtk/ToolBox.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2012 Alex Merry <dev@randomguy3.me.uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#import "TZFoundation.h"
+#import <gtk/gtk.h>
+
+@class Configuration;
+@protocol Tool;
+
+@interface ToolBox : NSObject {
+ GtkWidget *window;
+ GtkToolItemGroup *toolGroup;
+}
+
+@property (assign) id<Tool> selectedTool;
+
+- (id) initWithTools:(NSArray*)tools;
+
+- (void) loadConfiguration:(Configuration*)config;
+- (void) saveConfiguration:(Configuration*)config;
+@end
+
+// vim:ft=objc:ts=8:et:sts=4:sw=4
diff --git a/tikzit/src/gtk/ToolBox.m b/tikzit/src/gtk/ToolBox.m
new file mode 100644
index 0000000..5c7473d
--- /dev/null
+++ b/tikzit/src/gtk/ToolBox.m
@@ -0,0 +1,172 @@
+/*
+ * Copyright 2012 Alex Merry <dev@randomguy3.me.uk>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#import "ToolBox.h"
+
+#import "Application.h"
+#import "Configuration.h"
+#import "Tool.h"
+
+static void tool_button_toggled_cb (GtkWidget *widget, ToolBox *toolBox);
+static void unretain (gpointer data);
+
+#define TOOL_DATA_KEY "tikzit-tool"
+
+@implementation ToolBox
+
+- (id) init {
+ [self release];
+ return nil;
+}
+
+- (id) initWithTools:(NSArray*)tools {
+ self = [super init];
+
+ if (self) {
+ window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_title (GTK_WINDOW (window), "Toolbox");
+ gtk_window_set_role (GTK_WINDOW (window), "toolbox");
+ gtk_window_set_type_hint (
+ GTK_WINDOW (window),
+ GDK_WINDOW_TYPE_HINT_UTILITY);
+ gtk_window_set_default_size (GTK_WINDOW (window), 200, 500);
+ gtk_window_set_deletable (GTK_WINDOW (window), FALSE);
+
+ GtkBox *mainLayout = GTK_BOX (gtk_vbox_new (FALSE, 0));
+ gtk_widget_show (GTK_WIDGET (mainLayout));
+ gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (mainLayout));
+
+ GtkWidget *toolPalette = gtk_tool_palette_new ();
+ gtk_widget_show (toolPalette);
+ gtk_container_add (GTK_CONTAINER (mainLayout), toolPalette);
+
+ toolGroup = GTK_TOOL_ITEM_GROUP (gtk_tool_item_group_new ("Tools"));
+ g_object_ref_sink (G_OBJECT (toolGroup));
+ gtk_tool_item_group_set_label_widget (
+ toolGroup,
+ NULL);
+ gtk_container_add (GTK_CONTAINER (toolPalette), GTK_WIDGET (toolGroup));
+ gtk_widget_show (GTK_WIDGET (toolGroup));
+
+ id<Tool> activeTool = [app activeTool];
+
+ GSList *item_group = NULL;
+ for (id<Tool> tool in tools) {
+ NSString *tooltip = [NSString stringWithFormat:
+ @"%@: %@ (%@)",
+ [tool name], [tool helpText], [tool shortcut]];
+ GtkToolItem *item = gtk_radio_tool_button_new_from_stock (
+ item_group,
+ [tool stockId]);
+ gtk_tool_item_set_tooltip_text (item, [tooltip UTF8String]);
+ item_group = gtk_radio_tool_button_get_group (
+ GTK_RADIO_TOOL_BUTTON (item));
+ gtk_tool_item_group_insert (
+ toolGroup,
+ item,
+ -1);
+ gtk_widget_show (GTK_WIDGET (item));
+ g_object_set_data_full (
+ G_OBJECT(item),
+ TOOL_DATA_KEY,
+ [tool retain],
+ unretain);
+
+ if (tool == activeTool) {
+ gtk_toggle_tool_button_set_active (
+ GTK_TOGGLE_TOOL_BUTTON (item),
+ TRUE);
+ }
+
+ g_signal_connect (item, "toggled",
+ G_CALLBACK (tool_button_toggled_cb),
+ self);
+ }
+
+ gtk_widget_show (window);
+ }
+
+ return self;
+}
+
+- (void) dealloc {
+ if (window) {
+ g_object_unref (G_OBJECT (toolGroup));
+ gtk_widget_destroy (window);
+ g_object_unref (G_OBJECT (window));
+ }
+
+ [super dealloc];
+}
+
+- (id<Tool>) selectedTool {
+ guint count = gtk_tool_item_group_get_n_items (toolGroup);
+ for (guint i = 0; i < count; ++i) {
+ GtkToolItem *item = gtk_tool_item_group_get_nth_item (toolGroup, i);
+ if (gtk_toggle_tool_button_get_active (GTK_TOGGLE_TOOL_BUTTON (item))) {
+ return (id)g_object_get_data (G_OBJECT (item), TOOL_DATA_KEY);
+ }
+ }
+ return nil;
+}
+
+- (void) setSelectedTool:(id<Tool>)tool {
+ guint count = gtk_tool_item_group_get_n_items (toolGroup);
+ for (guint i = 0; i < count; ++i) {
+ GtkToolItem *item = gtk_tool_item_group_get_nth_item (toolGroup, i);
+ id<Tool> data = (id)g_object_get_data (G_OBJECT (item), TOOL_DATA_KEY);
+ if (data == tool) {
+ gtk_toggle_tool_button_set_active (
+ GTK_TOGGLE_TOOL_BUTTON (item),
+ TRUE);
+ return;
+ }
+ }
+}
+
+- (void) loadConfiguration:(Configuration*)config {
+}
+
+- (void) saveConfiguration:(Configuration*)config {
+}
+
+@end
+
+static void tool_button_toggled_cb (GtkWidget *widget, ToolBox *toolBox) {
+ if (gtk_toggle_tool_button_get_active (GTK_TOGGLE_TOOL_BUTTON (widget))) {
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ id<Tool> tool = (id)g_object_get_data (G_OBJECT(widget), TOOL_DATA_KEY);
+ [app setActiveTool:tool];
+ NSDictionary *userInfo = [NSDictionary
+ dictionaryWithObject:tool
+ forKey:@"tool"];
+ [[NSNotificationCenter defaultCenter]
+ postNotificationName:@"ToolSelectionChanged"
+ object:toolBox
+ userInfo:userInfo];
+
+ [pool drain];
+ }
+}
+
+static void unretain (gpointer data) {
+ id obj = (id)data;
+ [obj release];
+}
+
+// vim:ft=objc:ts=8:et:sts=4:sw=4