~palango/sterling/main

« back to all changes in this revision

Viewing changes to Sterling/MainWindow.cs

  • Committer: Paul Lange
  • Date: 2008-06-23 14:29:39 UTC
  • Revision ID: palango@gmx.de-20080623142939-nyxagnpo4p5xpvvr
Added context-menu to treeview (thanks to tomboy code) and small 
improvements to filtering code

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
                ListStore _store;
32
32
                
33
33
                int _filterStatus;
34
 
                string _searchText;
35
34
                
36
35
                public MainWindow() : base(Gtk.WindowType.Toplevel)
37
36
                {
39
38
                        this.DeleteEvent += delegate { Application.Quit(); };
40
39
                        
41
40
                        _filterStatus = 0;
42
 
                        _searchText = "";
43
41
                        
44
42
                        initializeGUI();
 
43
                        
 
44
                        loadData();
45
45
                }
46
46
                
 
47
#region UI stuff
47
48
                private void initializeGUI()
48
49
                {
49
50
                        //TODO better column sizing
92
93
                        
93
94
                        _store = new ListStore(typeof(Transaction), typeof(double));
94
95
                        
95
 
                        _store.AppendValues (new Transaction(-1.20, "test", DateTime.Now),1d);
96
 
                        _store.AppendValues (new Transaction(1.23, "test", DateTime.Now),-2d);
97
 
                        
98
96
                        _filter = new TreeModelFilter(_store, null);
99
97
                        _filter.VisibleFunc = new TreeModelFilterVisibleFunc(searchAndFilter);
100
98
                        
130
128
                {
131
129
                        Transaction trans = (Transaction) model.GetValue (iter, 0);
132
130
                        (cell as Gtk.CellRendererText).Text = trans.Description;
 
131
                        (cell as Gtk.CellRendererText).Alignment = Pango.Alignment.Center;
133
132
                }
134
133
                
135
134
                private void renderBalance (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
140
139
                        string plus = (balance > 0) ? "+" : "";
141
140
                        
142
141
                        (cell as Gtk.CellRendererText).Weight = 10000000;
 
142
                        (cell as Gtk.CellRendererText).Alignment = Pango.Alignment.Right;
143
143
                        (cell as Gtk.CellRendererText).Text = plus + string.Format("{0:0.00}", balance);
144
144
                }
145
 
 
 
145
#endregion
 
146
                
 
147
                private void loadData()
 
148
                {
 
149
                        _store.AppendValues (new Transaction(0d, "Start balancing", DateTime.Now), 0d);
 
150
                        
 
151
                        _store.AppendValues (new Transaction(-1.20, "test", DateTime.Now),-1.2);
 
152
                        _store.AppendValues (new Transaction(1.23, "test", DateTime.Now),0.23);
 
153
                }
 
154
                                
 
155
                [GLib.ConnectBefore]
 
156
                protected virtual void OnDataViewButtonPressEvent (object o, Gtk.ButtonPressEventArgs args)
 
157
                {
 
158
                        if (args.Event.Type == Gdk.EventType.ButtonPress && args.Event.Button == 3) 
 
159
                        {
 
160
                                Menu context = new Menu();
 
161
                                ImageMenuItem edit = new ImageMenuItem(Stock.Edit, null);
 
162
                                edit.Activated += new EventHandler(ContextMenUEditActivated);
 
163
                                
 
164
                                context.Append(edit);
 
165
                                
 
166
                                System.Console.WriteLine("cooler");
 
167
                                
 
168
                                PopupContextMenuAtLocation(context, (int)args.Event.X, (int)args.Event.Y);
 
169
                        }
 
170
                }
 
171
                
 
172
                void PopupContextMenuAtLocation (Gtk.Menu menu, int x, int y)
 
173
                {
 
174
                        menu.ShowAll ();
 
175
                        Gtk.MenuPositionFunc pos_menu_func = null;
 
176
 
 
177
                        // Set up the funtion to position the context menu
 
178
                        // if we were called by the keyboard Gdk.Key.Menu.
 
179
                        if (x == 0 && y == 0)
 
180
                            pos_menu_func = PositionContextMenu;
 
181
 
 
182
                        menu.Popup (null, null, pos_menu_func, 0, Gtk.Global.CurrentEventTime);
 
183
                }
 
184
                
 
185
                // This is needed for when the user opens
 
186
                // the context menu with the keyboard.
 
187
                void PositionContextMenu (Gtk.Menu menu, out int x, out int y, out bool push_in)
 
188
                {
 
189
                        Gtk.TreeIter iter;
 
190
                        Gtk.TreePath path;
 
191
                        Gtk.TreeSelection selection;
 
192
 
 
193
                        // Set default "return" values
 
194
                        push_in = false; // not used
 
195
                        x = 0;
 
196
                        y = 0;
 
197
 
 
198
                        selection = dataView.Selection;
 
199
                        if (!selection.GetSelected (out iter))
 
200
                            return;
 
201
 
 
202
                        path = _filter.GetPath (iter);
 
203
 
 
204
                        int pos_x = 0;
 
205
                        int pos_y = 0;
 
206
 
 
207
                        GetWidgetScreenPos (dataView, ref pos_x, ref pos_y);
 
208
                        Gdk.Rectangle cell_rect = dataView.GetCellArea (path, dataView.Columns [0]);
 
209
 
 
210
                        // Add 100 to x so it's not be at the far left
 
211
                        x = pos_x + cell_rect.X + 100;
 
212
                        y = pos_y + cell_rect.Y;
 
213
                }
 
214
 
 
215
                // Walk the widget hiearchy to figure out
 
216
                // where to position the context menu.
 
217
                void GetWidgetScreenPos (Gtk.Widget widget, ref int x, ref int y)
 
218
                {
 
219
                        int widget_x;
 
220
                        int widget_y;
 
221
 
 
222
                        if (widget is Gtk.Window) 
 
223
                        {
 
224
                                ((Gtk.Window) widget).GetPosition (out widget_x, out widget_y);
 
225
                        } else 
 
226
                        {
 
227
                                GetWidgetScreenPos (widget.Parent, ref x, ref y);
 
228
 
 
229
                                // Special case the TreeView because it adds
 
230
                                // too much since it's in a scrolled window.
 
231
                                if (widget == dataView) 
 
232
                                {
 
233
                                    widget_x = 2;
 
234
                                    widget_y = 2;
 
235
                                } else 
 
236
                                {
 
237
                                    Gdk.Rectangle widget_rect = widget.Allocation;
 
238
                                    widget_x = widget_rect.X;
 
239
                                    widget_y = widget_rect.Y;
 
240
                                }
 
241
                        }
 
242
 
 
243
                        x += widget_x;
 
244
                        y += widget_y;
 
245
                }
 
246
                
 
247
                void ContextMenUEditActivated(object o, EventArgs args)
 
248
                {
 
249
                        TreeIter iter;
 
250
                        
 
251
                        if (dataView.Selection.GetSelected(out iter))
 
252
                        {
 
253
                                Transaction item = (Transaction)_filter.GetValue(iter, 0);
 
254
                                
 
255
                                TransactionDialog dialog = new TransactionDialog(item);
 
256
                                
 
257
                                //TODO: Handle editing
 
258
                                
 
259
                                if (dialog.Run() == (int)(ResponseType.Ok))
 
260
                                {
 
261
                                        Transaction t = dialog.Current;
 
262
                                        //TODO: also store in Account or implement account usen liststore
 
263
                                        _store.AppendValues(t);
 
264
                                }
 
265
                                
 
266
                                dialog.Destroy();
 
267
                        }                       
 
268
                }
146
269
                
147
270
                private void showNewTransactionDialog()
148
271
                {
184
307
                        Application.Quit();
185
308
                }
186
309
 
 
310
#region Searching and Filtering
187
311
                protected virtual void OnFilterComboChanged (object sender, System.EventArgs e)
188
312
                {
189
313
                        ComboBox filter = sender as ComboBox;
204
328
                                        _filter.Refilter();
205
329
                                }
206
330
                        }
207
 
                }               
 
331
                }
 
332
 
 
333
                protected virtual void OnSearchEntryChanged (object sender, System.EventArgs e)
 
334
                {
 
335
                        _filter.Refilter();
 
336
                }       
208
337
                
209
338
                private bool searchAndFilter(TreeModel model, TreeIter iter)
210
339
                {
238
367
                }
239
368
                
240
369
                private bool searchItems(Transaction trans)
241
 
                {
242
 
                        System.Console.WriteLine("#####");
243
 
                        System.Console.WriteLine(_searchText);                  
244
 
                        
245
 
                        if (_searchText == "")
246
 
                                return true;
247
 
                        
248
 
                        if (trans.Date.ToShortDateString().Contains(_searchText))
249
 
                                return true;
250
 
                        
251
 
                        if (trans.Amount.ToString().Contains(_searchText))
 
370
                {                       
 
371
                        string searchText = searchEntry.Text;
 
372
                        
 
373
                        if (searchText == "")
 
374
                                return true;
 
375
                        
 
376
                        if (trans.Date.ToShortDateString().Contains(searchText))
 
377
                                return true;
 
378
                        
 
379
                        if (trans.Amount.ToString().Contains(searchText))
252
380
                                return true;                    
253
381
                        
254
 
                        return trans.Description.Contains(_searchText);
255
 
                }
256
 
 
257
 
                protected virtual void OnSearchEntryChanged (object sender, System.EventArgs e)
258
 
                {
259
 
                        Entry search = sender as Entry;
260
 
                        
261
 
                        string mask = search.Text;
262
 
                        
263
 
                        _searchText = mask;
264
 
                        _filter.Refilter();
265
 
                }
 
382
                        return trans.Description.Contains(searchText);
 
383
                }
 
384
#endregion
266
385
        }
267
386
}