1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "wxUtils.h"
21 
22 #include "IO/Path.h"
23 #include "IO/ResourceUtils.h"
24 #include "View/BitmapToggleButton.h"
25 #include "View/BorderLine.h"
26 #include "View/MapFrame.h"
27 #include "View/ViewConstants.h"
28 
29 #include <wx/bitmap.h>
30 #include <wx/bmpbuttn.h>
31 #include <wx/frame.h>
32 #include <wx/listctrl.h>
33 #include <wx/sizer.h>
34 #include <wx/tglbtn.h>
35 #include <wx/window.h>
36 
37 #include <list>
38 
39 namespace TrenchBroom {
40     namespace View {
findMapFrame(wxWindow * window)41         MapFrame* findMapFrame(wxWindow* window) {
42             return wxDynamicCast(findFrame(window), MapFrame);
43         }
44 
findFrame(wxWindow * window)45         wxFrame* findFrame(wxWindow* window) {
46             if (window == NULL)
47                 return NULL;
48             return wxDynamicCast(wxGetTopLevelParent(window), wxFrame);
49         }
50 
fromWxColor(const wxColor & color)51         Color fromWxColor(const wxColor& color) {
52             const float r = static_cast<float>(color.Red())   / 255.0f;
53             const float g = static_cast<float>(color.Green()) / 255.0f;
54             const float b = static_cast<float>(color.Blue())  / 255.0f;
55             const float a = static_cast<float>(color.Alpha()) / 255.0f;
56             return Color(r, g, b, a);
57         }
58 
toWxColor(const Color & color)59         wxColor toWxColor(const Color& color) {
60             const unsigned char r = static_cast<unsigned char>(color.r() * 255.0f);
61             const unsigned char g = static_cast<unsigned char>(color.g() * 255.0f);
62             const unsigned char b = static_cast<unsigned char>(color.b() * 255.0f);
63             const unsigned char a = static_cast<unsigned char>(color.a() * 255.0f);
64             return wxColor(r, g, b, a);
65         }
66 
getListCtrlSelection(const wxListCtrl * listCtrl)67         std::vector<size_t> getListCtrlSelection(const wxListCtrl* listCtrl) {
68             assert(listCtrl != NULL);
69 
70 
71             std::vector<size_t> result(static_cast<size_t>(listCtrl->GetSelectedItemCount()));
72 
73             size_t i = 0;
74             long itemIndex = listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
75             while (itemIndex >= 0) {
76                 result[i++] = static_cast<size_t>(itemIndex);
77                 itemIndex = listCtrl->GetNextItem(itemIndex, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
78             }
79             return result;
80         }
81 
createBitmapButton(wxWindow * parent,const String & image,const String & tooltip)82         wxWindow* createBitmapButton(wxWindow* parent, const String& image, const String& tooltip) {
83             wxBitmap bitmap = IO::loadImageResource(image);
84             assert(bitmap.IsOk());
85 
86             wxBitmapButton* button = new wxBitmapButton(parent, wxID_ANY, bitmap, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
87             button->SetToolTip(tooltip);
88             button->SetBackgroundColour(*wxWHITE);
89             return button;
90         }
91 
createBitmapToggleButton(wxWindow * parent,const String & upImage,const String & downImage,const String & tooltip)92         wxWindow* createBitmapToggleButton(wxWindow* parent, const String& upImage, const String& downImage, const String& tooltip) {
93             wxBitmap upBitmap = IO::loadImageResource(upImage);
94             assert(upBitmap.IsOk());
95 
96             wxBitmap downBitmap = IO::loadImageResource(downImage);
97             assert(downBitmap.IsOk());
98 
99             /*
100             wxBitmapToggleButton* button = new wxBitmapToggleButton(parent, wxID_ANY, offBitmap, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE | wxBU_EXACTFIT);
101             button->SetBitmapPressed(onBitmap);
102             button->SetToolTip(tooltip);
103             button->SetBackgroundColour(*wxWHITE);
104             */
105 
106             BitmapToggleButton* button = new BitmapToggleButton(parent, wxID_ANY, upBitmap, downBitmap);
107             button->SetToolTip(tooltip);
108             button->SetBackgroundColour(*wxWHITE);
109             return button;
110         }
111 
wrapDialogButtonSizer(wxSizer * buttonSizer,wxWindow * parent)112         wxSizer* wrapDialogButtonSizer(wxSizer* buttonSizer, wxWindow* parent) {
113             wxSizer* sizer = new wxBoxSizer(wxVERTICAL);
114             sizer->Add(new BorderLine(parent, BorderLine::Direction_Horizontal), 0, wxEXPAND);
115             sizer->AddSpacer(LayoutConstants::DialogButtonTopMargin);
116             sizer->Add(buttonSizer, 0, wxEXPAND | wxLEFT | wxRIGHT, LayoutConstants::DialogButtonSideMargin);
117             sizer->AddSpacer(LayoutConstants::DialogButtonBottomMargin);
118             return sizer;
119         }
120 
filterBySuffix(const wxArrayString & strings,const wxString & suffix,const bool caseSensitive)121         wxArrayString filterBySuffix(const wxArrayString& strings, const wxString& suffix, const bool caseSensitive) {
122             wxArrayString result;
123             for (size_t i = 0; i < strings.size(); ++i) {
124                 const wxString& str = strings[i];
125                 if (caseSensitive) {
126                     if (str.EndsWith(suffix))
127                         result.Add(str);
128                 } else {
129                     if (str.Lower().EndsWith(suffix.Lower()))
130                         result.Add(str);
131                 }
132             }
133             return result;
134         }
135     }
136 }
137