Kitlist
A list manager for maintaining kit lists
Loading...
Searching...
No Matches
kitlistgui.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of Kitlist, a program to maintain a simple list
4 of items and assign items to one or more categories.
5
6 Copyright (C) 2008-2025 Frank Dean
7
8 Kitlist is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 Kitlist is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Kitlist. If not, see <http://www.gnu.org/licenses/>.
20
21*/
22#ifndef KITLIST_GUI_HPP
23#define KITLIST_GUI_HPP
24
25#include "kitlist_base_app.hpp"
26#include "wx/dataview.h"
27#include "wx/filehistory.h"
28#include "wx/richtext/richtextprint.h"
29#include "wx/splitter.h"
30#include "wx/wx.h"
31#include <cassert>
32#include <stdexcept>
33#include <memory>
34#include <vector>
35
36class MyFrame;
37
45class KitListGui : public wxApp, public KitListBaseApp
46{
49public:
50
52 KitListGui() : wxApp(), KitListBaseApp(), my_frame() {}
53
59 virtual void category_entry_changed(const std::shared_ptr<Category> category) override;
60
66 virtual void item_entry_changed(const std::shared_ptr<Item> item) override;
67
72 virtual void OnInitCmdLine(wxCmdLineParser& parser) override;
73
79 virtual bool OnCmdLineParsed(wxCmdLineParser& parser) override;
80
87 virtual bool OnInit() override;
88
94 virtual int OnExit() override;
95
106 virtual void load_file(const std::string& filename) override;
107
109 virtual void update_menu_state(bool is_dirty) override;
110
111};
112
118class MyFrame : public wxFrame
119{
120 friend class KitListGui;
121
128 std::unique_ptr<wxRichTextPrinting> rich_text_printing;
129
131 static const int MIN_PANE_SIZE;
132
139
141 wxDataViewListCtrl* category_list_ctrl;
142
144 wxMenu* file_menu;
145
147 wxMenu* edit_menu;
148
151
157 wxFileHistory* file_history;
158
165
175 std::unique_ptr<std::exception> last_error;
176
178 static const int ITEM_CHECKED_COL;
180 static const int ITEM_NAME_COL;
182 static const int CATEGORY_NAME_COL;
183
184public:
186 MyFrame(KitListGui* app, const wxPoint& pos);
187
194 virtual ~MyFrame();
195
204 void OnShow(wxShowEvent& event);
205
207 void OnAbout(wxCommandEvent& event);
208
210 void OnNewFile(wxCommandEvent& event);
211
213 void OnOpen(wxCommandEvent& event);
214
219 void OnFileHistoryMenuItem(wxCommandEvent& event);
220
222 void OnSave(wxCommandEvent& event);
223
225 void OnSaveAs(wxCommandEvent& event);
226
228 void OnQuit(wxCommandEvent& event);
229
231 void OnAddItem(wxCommandEvent& WXUNUSED(event));
232
234 void OnAddCategory(wxCommandEvent& WXUNUSED(event));
235
246 void OnSelectCategory(wxDataViewEvent& event);
247
252 void OnSelectItem(wxDataViewEvent& event);
253
259
264 void OnCategoryStartEditing(wxDataViewEvent& event) {
265 const int row = category_list_ctrl->ItemToRow(event.GetItem());
266 if (row == 0)
267 event.Veto();
268 }
269
271 void OnCategoryContextMenu(wxDataViewEvent& event);
272
274 void OnItemContextMenu(wxDataViewEvent& event);
275
277 void OnCategoryListValueChanged(wxDataViewEvent& event);
278
280 void OnItemListValueChanged(wxDataViewEvent& event);
281
283 void OnShowAll(wxCommandEvent& WXUNUSED(event)) {
284 gui_app->set_filter(Model::all);
286 }
287
289 void OnShowChecked(wxCommandEvent& WXUNUSED(event)) {
290 gui_app->set_filter(Model::checked);
292 }
293
295 void OnShowUnchecked(wxCommandEvent& WXUNUSED(event)) {
296 gui_app->set_filter(Model::unchecked);
298 }
299
301 void OnSelectAllItems(wxCommandEvent& WXUNUSED(event)) {
302 item_list_ctrl->SelectAll();
304 }
305
307 void OnUnselectAllItems(wxCommandEvent& WXUNUSED(event)) {
308 item_list_ctrl->UnselectAll();
310 }
311
313 void OnCheckAll(wxCommandEvent& WXUNUSED(event)) {
314 gui_app->change_all_current_items_checked_states(Model::check);
317 }
318
320 void OnUncheckAll(wxCommandEvent& WXUNUSED(event)) {
321 gui_app->change_all_current_items_checked_states(Model::uncheck);
324 }
325
327 void OnToggleAll(wxCommandEvent& WXUNUSED(event)) {
328 gui_app->change_all_current_items_checked_states(Model::toggle);
331 }
332
334 void OnCheckSelected(wxCommandEvent& event);
335
337 void OnUncheckSelected(wxCommandEvent& event);
338
340 void OnToggleSelected(wxCommandEvent& event);
341
343 void CopyCheckedToCategories(wxCommandEvent& WXUNUSED(event));
344
351 void OnCopySelectedItemsToCategories(wxCommandEvent& event);
352
354 void OnRemoveSelectedItems(wxCommandEvent& event);
355
357 void OnRemoveCheckedItems(wxCommandEvent& WXUNUSED(event));
358
360 void OnDeleteCheckedItems(wxCommandEvent& WXUNUSED(event));
361
363 // Don't remove this method without confirming it compiles under linux.
365 return right_panel->GetSize();
366 }
367
369 static const int VSPLIT_DIST;
370private:
372 wxWindow* left_panel = nullptr;
374 wxWindow* right_panel = nullptr;
375
377 wxDataViewListCtrl* item_list_ctrl = nullptr;
378
380 wxSplitterWindow* split_window = nullptr;
381
394 void load_file(const std::string filename);
395
402 unsigned populate_category_list(int32_t select_category = Model::no_category);
403
410 void populate_item_list();
411
417 void delete_category(int32_t id) {
418 gui_app->delete_category(id);
421 }
422
428 void delete_item(int32_t id) {
429 gui_app->delete_item(id);
431 }
432
442 void remove_item(int32_t id) {
443 gui_app->remove_item(id);
445 }
446
453
465 void select_and_copy_to_categories(std::vector<std::shared_ptr<Item>> selected_items);
466
477 void copy_items_to_categories(std::vector<std::shared_ptr<Item>>& items,
478 std::vector<std::shared_ptr<Category>>& categories);
479
481 void save();
482
490 void update_file_menu(bool is_dirty);
491
498 void update_edit_menu();
499
506
514 void OnPrint(wxCommandEvent& event);
515
521 void OnPrintPreview(wxCommandEvent& event);
522
523
536 void FillBuffer(wxRichTextBuffer& buf) const;
537
545 void do_open(wxString wx_path);
546
549};
550
556class MySplitterWindow : public wxSplitterWindow {
557public:
564 : wxSplitterWindow(parent, wxID_ANY,
565 wxDefaultPosition, wxDefaultSize,
566 wxSP_3D | wxSP_LIVE_UPDATE |
567 wxCLIP_CHILDREN) {
568 frame = parent;
569 }
570
573
576
581 void OnPositionChanged(wxSplitterEvent& event);
582
583private:
586
589};
590
596class ItemDialog : public wxDialog {
597
599 wxCheckBox* checkbox_ctrl;
600
602 wxTextCtrl* name_ctrl;
603
604public:
611 ItemDialog(wxWindow* parent, const wxString& title);
612
617 const std::string get_name() const {
618 auto v = name_ctrl->GetValue();
619 return v.ToStdString();
620 }
621
627 bool is_checked() const {
628 return checkbox_ctrl->GetValue();
629 }
630};
631
632#endif // KITLIST_GUI_HPP
ItemDialog(wxWindow *parent, const wxString &title)
wxTextCtrl * name_ctrl
The input control for the item name.
const std::string get_name() const
The name.
bool is_checked() const
Whether checkbox was checked.
wxCheckBox * checkbox_ctrl
The checkbox control.
KitListBaseApp()
Default constructor.
std::string filename
The current filename the Model was loaded from.
bool is_dirty() const
KitListGui()
Default constructor.
MyFrame * my_frame
The main frame of the application.
virtual bool OnCmdLineParsed(wxCmdLineParser &parser) override
Called after the command line has been successfully parsed.
virtual void OnInitCmdLine(wxCmdLineParser &parser) override
Called from OnInit() and initializes the parser with the command line options for the application.
virtual int OnExit() override
Can be used to cleanup items created during OnInit().
virtual void update_menu_state(bool is_dirty) override
Enables or disables various menu options, as appropriate.
virtual void category_entry_changed(const std::shared_ptr< Category > category) override
Called whenever a Category name changes.
virtual bool OnInit() override
Initializes the application's main window.
virtual void load_file(const std::string &filename) override
Loads the specified file.
virtual void item_entry_changed(const std::shared_ptr< Item > item) override
Called whenever a Item name changes.
check_action
Specifies what state Item checkmarks must be changed to.
Definition model.hpp:117
@ check
Definition model.hpp:119
@ uncheck
Definition model.hpp:120
@ toggle
Definition model.hpp:118
static const int32_t no_category
Indicates no filtering by category.
Definition model.hpp:153
@ unchecked
Show only unchecked items.
Definition model.hpp:110
@ all
Show all items, i.e. no filtering.
Definition model.hpp:106
@ checked
Show only checked items.
Definition model.hpp:108
Implements the main frame of the GUI.
void OnShowAll(wxCommandEvent &WXUNUSED(event))
Menu event handler for showing all items.
void OnUncheckSelected(wxCommandEvent &event)
Menu handler called to mark all selected items as unchecked.
void delete_category(int32_t id)
Deletes the specified Category from the model.
wxSize get_right_panel_width()
Returns the minimum width of the right panel (the item list).
void OnRemoveCheckedItems(wxCommandEvent &WXUNUSED(event))
Menu handler called to remove checked items from a Category.
unsigned populate_category_list(int32_t select_category=Model::no_category)
Rebuilds the list of categories.
void OnUncheckAll(wxCommandEvent &WXUNUSED(event))
Menu handler called to uncheck all items in the list.
wxMenu * file_menu
The File menu.
void OnSave(wxCommandEvent &event)
Menu event handler to save the current file.
wxWindow * right_panel
Pointer to the right panel containing the list of items.
void OnNewFile(wxCommandEvent &event)
Menu event handler for creating a new blank model.
static const int ITEM_NAME_COL
The zero based indexed position of the item name column.
void OnCategoryContextMenu(wxDataViewEvent &event)
Implements a pop-up context menu for the Category list.
void OnCheckSelected(wxCommandEvent &event)
Menu handler called to mark all selected items as checked.
friend class KitListGui
wxMenu * file_history_menu
The recent files menu.
void OnUnselectAllItems(wxCommandEvent &WXUNUSED(event))
Menu event handler for unselecting all items.
void OnPrintPreview(wxCommandEvent &event)
Provides a print previews of the list of items.
void CopyCheckedToCategories(wxCommandEvent &WXUNUSED(event))
Menu handler called to copy checked items to one or more categories.
void do_open(wxString wx_path)
Opens the file from the specified path.
void OnToggleAll(wxCommandEvent &WXUNUSED(event))
Menu handler called to toggle the checked state of all items in the list.
wxDataViewListCtrl * category_list_ctrl
The Category list control.
void change_selected_checked_states(Model::check_action action)
Switches the checked state of all the currently selected items in the item list.
void OnItemContextMenu(wxDataViewEvent &event)
Implements a pop-up context menu for the Item list.
std::unique_ptr< std::exception > last_error
Holds the most recent exception that occurred or nullptr if no exception.
static const int MIN_PANE_SIZE
The minimum size that the split window can be changed to by the user.
void delete_item(int32_t id)
Deletes the specified Item from the model.
wxSplitterWindow * split_window
Pointer to the Category list controller.
void OnAddCategory(wxCommandEvent &WXUNUSED(event))
Menu event handler for creating a new Category.
void update_file_menu(bool is_dirty)
KitListGui * gui_app
Pointer to the main GUI class.
void OnShowUnchecked(wxCommandEvent &WXUNUSED(event))
Menu event handler for showing only unchecked items.
void OnCategoryStartEditing(wxDataViewEvent &event)
Event handler implemented to disallow editing the first row of the Category list.
void copy_items_to_categories(std::vector< std::shared_ptr< Item > > &items, std::vector< std::shared_ptr< Category > > &categories)
Copies each of the passed items to each of the passed categories.
void load_file(const std::string filename)
Loads the specified file.
virtual ~MyFrame()
Destructor.
void OnOpen(wxCommandEvent &event)
Menu event handler to open another file.
void setup_rich_text_printing()
Sets up rich text printing.
static const int CATEGORY_NAME_COL
The zero based indexed of the category name column.
void remove_item(int32_t id)
Removes references to the item from the currently selected Category.
wxWindow * left_panel
Pointer to the left panel containing the list of categories.
void selected_item_changed()
Disables or enables menu items, depending on whether any items have been selected in the Item list.
void OnFileHistoryMenuItem(wxCommandEvent &event)
Menu event handler to open a file that has been selected from the recent files menu.
std::unique_ptr< wxRichTextPrinting > rich_text_printing
Pointer to an instance supporting rich text printing.
wxFileHistory * file_history
The list of recent files.
wxMenu * edit_menu
The Edit menu.
void OnAddItem(wxCommandEvent &WXUNUSED(event))
Menu event handler for creating a new Item.
wxMenuItem * file_history_menu_item
A pointer to the recent files menu item.
void OnPrint(wxCommandEvent &event)
Prints the list of items.
void OnQuit(wxCommandEvent &event)
Called when the user chooses to quit the application.
void OnCheckAll(wxCommandEvent &WXUNUSED(event))
Menu handler called to check all items in the list.
wxDataViewListCtrl * item_list_ctrl
Pointer to the Item list controller.
void OnShow(wxShowEvent &event)
Event handler for when the window is shown.
void OnCopySelectedItemsToCategories(wxCommandEvent &event)
Menu handler called to copy selected items to one or more categories.
void OnShowChecked(wxCommandEvent &WXUNUSED(event))
Menu event handler for showing only checked items.
void FillBuffer(wxRichTextBuffer &buf) const
Fills the passed buffer with rich text to print or preview.
void OnSelectItem(wxDataViewEvent &event)
Event handler called when a user selects a Item from the list. Calls selected_item_changed().
void OnAbout(wxCommandEvent &event)
Menu event handler showing an About dialog box.
void OnSelectAllItems(wxCommandEvent &WXUNUSED(event))
Menu event handler for selecting all items.
void OnRemoveSelectedItems(wxCommandEvent &event)
Menu handler called to remove selected items from a Category.
void populate_item_list()
Rebuilds the list of items.
static const int VSPLIT_DIST
The initial width of the category column.
void update_edit_menu()
Enables or disables menu items on the Edit menu.
void OnToggleSelected(wxCommandEvent &event)
Menu handler called to toggle the checked state of all selected items.
void select_and_copy_to_categories(std::vector< std::shared_ptr< Item > > selected_items)
Displays a dialog for user to select target categories to copy selected items to.
void OnItemListValueChanged(wxDataViewEvent &event)
Event handler called when the Item name is edited.
void save()
Saves the current Model using the current KitListBaseApp::filename.
static const int ITEM_CHECKED_COL
The zero based indexed position of the item checked column.
void OnDeleteCheckedItems(wxCommandEvent &WXUNUSED(event))
Menu handler called to delete checked items from the model.
void OnSelectCategory(wxDataViewEvent &event)
Event handler called when a user selects a Category from the list.
wxDECLARE_EVENT_TABLE()
Declares the wxWidgets event handling table.
MyFrame(KitListGui *app, const wxPoint &pos)
Constructor.
void OnCategoryListValueChanged(wxDataViewEvent &event)
Event handler called when the Category name is edited.
void OnSaveAs(wxCommandEvent &event)
Menu event handler to save the model to a new file.
MySplitterWindow(const MySplitterWindow &)=delete
Prohibits copying an instance via the default copy constructor.
void OnPositionChanged(wxSplitterEvent &event)
Implements a workaround on Linux to initialize the split window correctly on startup.
MySplitterWindow(MyFrame *parent)
Constructor.
MySplitterWindow & operator=(const MySplitterWindow &)=delete
Prohibits copying an instance via assignment.
wxDECLARE_EVENT_TABLE()
Declares the wxWidgets event handling table.
MyFrame * frame
Pointer to the parent window.
Copyright 2008-2025 Frank Dean