KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_printer_list.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24// Define CUPS_LIST_PRINTERS to allow printer selection in PANEL_PRINTER_LIST
25// on Unices. It needs cups and cups dev files
26// Currently not tested and not finished for cups, so disable it
27// However the code (from wxWidgets forum) is kept here, just in case
28//#define CUPS_LIST_PRINTERS
29
30#ifdef __WXMSW__
31 #include <windows.h>
32 #include <winspool.h>
33#elif defined( CUPS_LIST_PRINTERS )
34 #include <cups/cups.h>
35#endif
36
37// GetPrinterList code comes from samples on:
38// https://forums.wxwidgets.org/viewtopic.php?t=13251
39// https://forums.wxwidgets.org/viewtopic.php?t=43930
40static bool GetPrinterList(wxArrayString &aPrinterList, wxString &aDefaultPrinterName)
41{
42 aPrinterList.Empty();
43 aDefaultPrinterName.Empty();
44
45#ifdef __WXMSW__
46 DWORD dwSize, dwPrinters;
47 BYTE *pBuffer;
48
49 DWORD szz = 255;
50 WCHAR c[256];
51 GetDefaultPrinter(&c[0], &szz);
52 aDefaultPrinterName = c;
53
54 ::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwSize, &dwPrinters);
55
56 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || dwSize == 0)
57 return false;
58
59 pBuffer = new BYTE[dwSize];
60 ::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwPrinters);
61
62 if (dwPrinters != 0)
63 {
64 PRINTER_INFO_5 *pPrnInfo = (PRINTER_INFO_5 *)pBuffer;
65
66 for (UINT i = 0; i < dwPrinters; i++)
67 {
68 aPrinterList.Add(pPrnInfo->pPrinterName);
69 pPrnInfo++;
70 }
71 }
72 if (pBuffer)
73 {
74 delete[] pBuffer;
75 pBuffer = NULL;
76 }
77
78 return true;
79#elif defined( CUPS_LIST_PRINTERS )
80 cups_dest_t* dests;
81 int num_dests = cupsGetDests(&dests);
82
83 for (int i = 0; i < num_dests; i++)
84 {
85 wxString sz;
86
87 if (dests[i].instance)
88 sz = wxString::Format("%s%s", dests[i].name, dests[i].instance);
89 else
90 sz = wxString::Format("%s", dests[i].name);
91
92 if (dests[i].is_default)
93 aDefaultPrinterName = sz;
94
95 aPrinterList.Add(sz);
96 }
97
98 //free memory from cups data
99 cupsFreeDests(num_dests, dests);
100
101 if(aPrinterList.GetCount())
102 return true;
103
104 return false;
105#else
106 return false;
107#endif
108}
109
110
112
113PANEL_PRINTER_LIST::PANEL_PRINTER_LIST( wxWindow* aParent, wxWindowID id,
114 const wxPoint& pos, const wxSize& size, long style,
115 const wxString& name ):
116 PANEL_PRINTER_LIST_BASE( aParent, id, pos, size, style, name )
117{
118
120
121 if( m_printer_list.size() )
122 {
124 m_stPrinterState->SetLabel( wxEmptyString );
125
126 bool selected = false;
127
128 if( !m_selectedPrinterName.IsEmpty() )
129 {
130 for( size_t ii = 0; ii < m_printer_list.GetCount(); ii++ )
131 {
133 {
134 m_choicePrinter->SetSelection( ii );
135 selected = true;
136 break;
137 }
138 }
139 }
140
141 if( !selected )
142 {
143 for( size_t ii = 0; ii < m_printer_list.GetCount(); ii++ )
144 {
146 {
147 m_choicePrinter->SetSelection( ii );
149 break;
150 }
151 }
152 }
153
155 m_stPrinterState->SetLabel( _( "Default printer" ) );
156 }
157}
158
159
161{
162}
163
164
166{
167 return m_choicePrinter->GetCount() > 0;
168}
169
170
171void PANEL_PRINTER_LIST::onPrinterChoice( wxCommandEvent& event )
172{
173 int select = m_choicePrinter->GetSelection();
174
175 if( m_choicePrinter->GetString( select ) == m_defaultPrinterName )
176 m_stPrinterState->SetLabel( _( "Default printer" ) );
177 else
178 m_stPrinterState->SetLabel( wxEmptyString );
179
180 m_selectedPrinterName = m_choicePrinter->GetString( select );
181}
182
183
185{
186 if( AsPrintersAvailable() )
187 {
188 int select = m_choicePrinter->GetSelection();
189 m_selectedPrinterName = m_choicePrinter->GetString( select );
190 }
191 else
192 m_selectedPrinterName.Empty();
193
195}
const char * name
Definition: DXF_plotter.cpp:62
Class PANEL_PRINTER_LIST_BASE.
wxArrayString m_printer_list
PANEL_PRINTER_LIST(wxWindow *aParent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(378, 109), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
static wxString m_selectedPrinterName
void onPrinterChoice(wxCommandEvent &event) override
#define _(s)
static bool GetPrinterList(wxArrayString &aPrinterList, wxString &aDefaultPrinterName)